-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
ScalarUDF: Remove supports_zero_argument
and avoid creating null array for empty args
#10193
Changes from 3 commits
eabfe68
7b529d9
03ec8b5
36e685e
7b04c0b
7c10382
864d197
5b51fb7
88d2a33
7c81776
bd4c65b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -63,9 +63,10 @@ impl ScalarUDFImpl for PiFunc { | |||||
} | ||||||
|
||||||
fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> { | ||||||
if !matches!(&args[0], ColumnarValue::Array(_)) { | ||||||
return exec_err!("Expect pi function to take no param"); | ||||||
} | ||||||
if !args.is_empty() { | ||||||
return exec_err!("Expect {} function to take no param", self.name()); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
}; | ||||||
|
||||||
let array = Float64Array::from_value(std::f64::consts::PI, 1); | ||||||
Ok(ColumnarValue::Array(Arc::new(array))) | ||||||
} | ||||||
|
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -69,12 +69,13 @@ impl ScalarUDFImpl for RandomFunc { | |||||||
} | ||||||||
} | ||||||||
|
||||||||
/// Random SQL function | ||||||||
fn random(args: &[ColumnarValue]) -> Result<ColumnarValue> { | ||||||||
let len: usize = match &args[0] { | ||||||||
ColumnarValue::Array(array) => array.len(), | ||||||||
_ => return exec_err!("Expect random function to take no param"), | ||||||||
let len = if args.is_empty() { | ||||||||
1 | ||||||||
} else { | ||||||||
return exec_err!("Expect random function to take no param"); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might be more consistent with the other changes in this PR:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did this before, and I noticed that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can even return ScalarValue::F64 instead of Array. Let me fix these too |
||||||||
}; | ||||||||
|
||||||||
let mut rng = thread_rng(); | ||||||||
let values = iter::repeat_with(|| rng.gen_range(0.0..1.0)).take(len); | ||||||||
let array = Float64Array::from_iter_values(values); | ||||||||
|
@@ -83,19 +84,13 @@ fn random(args: &[ColumnarValue]) -> Result<ColumnarValue> { | |||||||
|
||||||||
#[cfg(test)] | ||||||||
mod test { | ||||||||
use std::sync::Arc; | ||||||||
|
||||||||
use arrow::array::NullArray; | ||||||||
|
||||||||
use datafusion_common::cast::as_float64_array; | ||||||||
use datafusion_expr::ColumnarValue; | ||||||||
|
||||||||
use crate::math::random::random; | ||||||||
use super::*; | ||||||||
|
||||||||
#[test] | ||||||||
fn test_random_expression() { | ||||||||
let args = vec![ColumnarValue::Array(Arc::new(NullArray::new(1)))]; | ||||||||
let array = random(&args) | ||||||||
let array = random(&[]) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove this test, since it is already covered datafusion/datafusion/sqllogictest/test_files/expr.slt Lines 788 to 790 in da82cec
|
||||||||
.expect("failed to initialize function random") | ||||||||
.into_array(1) | ||||||||
.expect("Failed to convert to array"); | ||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -61,9 +61,10 @@ impl ScalarUDFImpl for UuidFunc { | |||||
/// Prints random (v4) uuid values per row | ||||||
/// uuid() = 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11' | ||||||
fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> { | ||||||
let len: usize = match &args[0] { | ||||||
ColumnarValue::Array(array) => array.len(), | ||||||
_ => return exec_err!("Expect uuid function to take no param"), | ||||||
let len = if args.is_empty() { | ||||||
1 | ||||||
} else { | ||||||
return exec_err!("Expect {} function to take no param", self.name()); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
}; | ||||||
|
||||||
let values = iter::repeat_with(|| Uuid::new_v4().to_string()).take(len); | ||||||
|
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.
It might be good to have the test match the rest of the functions for consistency