jayzhan211 commented on code in PR #10193: URL: https://github.com/apache/datafusion/pull/10193#discussion_r1577075655
########## datafusion/functions/src/math/random.rs: ########## @@ -64,45 +61,9 @@ impl ScalarUDFImpl for RandomFunc { Ok(Float64) } - fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> { - random(args) - } -} - -/// 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 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); - Ok(ColumnarValue::Array(Arc::new(array))) -} - -#[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; - - #[test] - fn test_random_expression() { - let args = vec![ColumnarValue::Array(Arc::new(NullArray::new(1)))]; - let array = random(&args) - .expect("failed to initialize function random") - .into_array(1) - .expect("Failed to convert to array"); - let floats = - as_float64_array(&array).expect("failed to initialize function random"); - - assert_eq!(floats.len(), 1); - assert!(0.0 <= floats.value(0) && floats.value(0) < 1.0); + fn invoke(&self, _args: &[ColumnarValue]) -> Result<ColumnarValue> { Review Comment: I didn't see the message, it is also a good idea. ########## datafusion/functions/src/math/random.rs: ########## @@ -64,45 +61,9 @@ impl ScalarUDFImpl for RandomFunc { Ok(Float64) } - fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> { - random(args) - } -} - -/// 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 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); - Ok(ColumnarValue::Array(Arc::new(array))) -} - -#[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; - - #[test] - fn test_random_expression() { - let args = vec![ColumnarValue::Array(Arc::new(NullArray::new(1)))]; - let array = random(&args) - .expect("failed to initialize function random") - .into_array(1) - .expect("Failed to convert to array"); - let floats = - as_float64_array(&array).expect("failed to initialize function random"); - - assert_eq!(floats.len(), 1); - assert!(0.0 <= floats.value(0) && floats.value(0) < 1.0); + fn invoke(&self, _args: &[ColumnarValue]) -> Result<ColumnarValue> { Review Comment: I didn't see the message then, it is also a good idea. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For additional commands, e-mail: github-h...@datafusion.apache.org