-
Notifications
You must be signed in to change notification settings - Fork 225
feat: randn expression support #2010
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+462
−111
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
aa2e480
feat: randn expression support
akupchinskiy e7913ad
added a missing license
akupchinskiy a00ec9c
added tolerance for randn test in rust
akupchinskiy bc4576a
Merge remote-tracking branch 'upstream/main' into feat/randn-expr-sup…
akupchinskiy 9bf6fbd
simplified serde logic for rand expressions and added a test case for…
akupchinskiy 1fd08b0
removed accidentally added case
akupchinskiy 754d0c8
added a missing tolerance to the new test
akupchinskiy 3a5ebab
review fix: simplifying serde
akupchinskiy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
native/spark-expr/src/nondetermenistic_funcs/internal/mod.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
mod rand_utils; | ||
|
||
pub use rand_utils::evaluate_batch_for_rand; | ||
pub use rand_utils::StatefulSeedValueGenerator; |
60 changes: 60 additions & 0 deletions
60
native/spark-expr/src/nondetermenistic_funcs/internal/rand_utils.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
use arrow::array::{Float64Array, Float64Builder}; | ||
use datafusion::logical_expr::ColumnarValue; | ||
use std::ops::Deref; | ||
use std::sync::{Arc, Mutex}; | ||
|
||
pub fn evaluate_batch_for_rand<R, S>( | ||
state_holder: &Arc<Mutex<Option<S>>>, | ||
seed: i64, | ||
num_rows: usize, | ||
) -> datafusion::common::Result<ColumnarValue> | ||
where | ||
R: StatefulSeedValueGenerator<S, f64>, | ||
S: Copy, | ||
{ | ||
let seed_state = state_holder.lock().unwrap(); | ||
let mut rnd = R::from_state_ref(seed_state, seed); | ||
let mut arr_builder = Float64Builder::with_capacity(num_rows); | ||
std::iter::repeat_with(|| rnd.next_value()) | ||
.take(num_rows) | ||
.for_each(|v| arr_builder.append_value(v)); | ||
let array_ref = Arc::new(Float64Array::from(arr_builder.finish())); | ||
let mut seed_state = state_holder.lock().unwrap(); | ||
seed_state.replace(rnd.get_current_state()); | ||
Ok(ColumnarValue::Array(array_ref)) | ||
} | ||
|
||
pub trait StatefulSeedValueGenerator<State: Copy, Value>: Sized { | ||
fn from_init_seed(init_seed: i64) -> Self; | ||
|
||
fn from_stored_state(stored_state: State) -> Self; | ||
|
||
fn next_value(&mut self) -> Value; | ||
|
||
fn get_current_state(&self) -> State; | ||
|
||
fn from_state_ref(state: impl Deref<Target = Option<State>>, init_value: i64) -> Self { | ||
if state.is_none() { | ||
Self::from_init_seed(init_value) | ||
} else { | ||
Self::from_stored_state(state.unwrap()) | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Rand struct could be used here rather than define an identical Randn struct.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. Let me also amend QueryPlanSerde.scala logic to align with that