Copilot commented on code in PR #1938: URL: https://github.com/apache/auron/pull/1938#discussion_r2711071807
########## native-engine/datafusion-ext-functions/src/spark_randn.rs: ########## @@ -0,0 +1,142 @@ +// 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 datafusion::{ + common::{Result, ScalarValue}, + logical_expr::ColumnarValue, +}; +use rand::SeedableRng; +use rand::rngs::StdRng; +use rand_distr::{Distribution, StandardNormal}; + +/// Returns a random value with independent and identically distributed (i.i.d.) values drawn +///from the standard normal distribution +/// +/// - Takes an optional seed (i64) for reproducibility +/// - If no seed is provided, uses a random seed +pub fn spark_randn(args: &[ColumnarValue]) -> Result<ColumnarValue> { + // Parse seed argument, or generate random seed if not provided + let seed: u64 = if args.is_empty() { + rand::random() + } else { + match &args[0] { + ColumnarValue::Scalar(ScalarValue::Int64(Some(s))) => *s as u64, + ColumnarValue::Scalar(ScalarValue::Int32(Some(s))) => *s as u64, + _ => rand::random(), + } + }; + + let mut rng = StdRng::seed_from_u64(seed); + let value: f64 = StandardNormal.sample(&mut rng); + + Ok(ColumnarValue::Scalar(ScalarValue::Float64(Some(value)))) +} Review Comment: This implementation only handles scalar seed inputs but does not handle columnar (array) data properly. When randn() is used in a DataFrame context (e.g., df.select(randn(seed))), it should generate a different random value for each row in the dataset, not just return a single value. The function needs to be updated to handle ColumnarValue::Array inputs where the seed could be an array, and generate an array of random values - one per row. This is critical for correct Spark compatibility when randn is used in SELECT queries over datasets with multiple rows. ########## native-engine/datafusion-ext-functions/src/spark_randn.rs: ########## @@ -0,0 +1,142 @@ +// 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 datafusion::{ + common::{Result, ScalarValue}, + logical_expr::ColumnarValue, +}; +use rand::SeedableRng; +use rand::rngs::StdRng; +use rand_distr::{Distribution, StandardNormal}; + +/// Returns a random value with independent and identically distributed (i.i.d.) values drawn +///from the standard normal distribution +/// +/// - Takes an optional seed (i64) for reproducibility +/// - If no seed is provided, uses a random seed Review Comment: The documentation states "If no seed is provided, uses a random seed" which is misleading. In Spark's randn() function, when no seed is provided, each invocation should generate different random values (using a randomized seed per partition and row). The current implementation with a single random seed would produce the same value for all rows in a batch when the seed argument is absent, which does not match Spark's behavior. ```suggestion /// - If no seed is provided, a random seed is chosen once per invocation, so all rows in the /// batch share the same value (unlike Spark's randn, which yields different values per row) ``` ########## native-engine/datafusion-ext-functions/src/spark_randn.rs: ########## @@ -0,0 +1,142 @@ +// 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 datafusion::{ + common::{Result, ScalarValue}, + logical_expr::ColumnarValue, +}; +use rand::SeedableRng; +use rand::rngs::StdRng; +use rand_distr::{Distribution, StandardNormal}; + +/// Returns a random value with independent and identically distributed (i.i.d.) values drawn +///from the standard normal distribution +/// +/// - Takes an optional seed (i64) for reproducibility +/// - If no seed is provided, uses a random seed +pub fn spark_randn(args: &[ColumnarValue]) -> Result<ColumnarValue> { + // Parse seed argument, or generate random seed if not provided + let seed: u64 = if args.is_empty() { + rand::random() + } else { + match &args[0] { + ColumnarValue::Scalar(ScalarValue::Int64(Some(s))) => *s as u64, + ColumnarValue::Scalar(ScalarValue::Int32(Some(s))) => *s as u64, + _ => rand::random(), + } + }; + + let mut rng = StdRng::seed_from_u64(seed); + let value: f64 = StandardNormal.sample(&mut rng); + + Ok(ColumnarValue::Scalar(ScalarValue::Float64(Some(value)))) +} + +#[cfg(test)] +mod test { + use std::error::Error; + + use datafusion::{common::ScalarValue, logical_expr::ColumnarValue}; + + use crate::spark_randn::spark_randn; + + #[test] + fn test_randn_with_seed_reproducibility() -> Result<(), Box<dyn Error>> { + // Same seed should produce same result + let seed = ColumnarValue::Scalar(ScalarValue::Int64(Some(42))); + + let result1 = spark_randn(&vec![seed.clone()])?; + let result2 = spark_randn(&vec![seed])?; + + match (result1, result2) { + ( + ColumnarValue::Scalar(ScalarValue::Float64(Some(v1))), + ColumnarValue::Scalar(ScalarValue::Float64(Some(v2))), + ) => { + assert_eq!(v1, v2, "Same seed should produce same result"); + } + _ => panic!("Expected Float64 scalar results"), + } + Ok(()) + } + + #[test] + fn test_randn_different_seeds() -> Result<(), Box<dyn Error>> { + // Different seeds should produce different results (with very high probability) + let seed1 = ColumnarValue::Scalar(ScalarValue::Int64(Some(42))); + let seed2 = ColumnarValue::Scalar(ScalarValue::Int64(Some(123))); + + let result1 = spark_randn(&vec![seed1])?; + let result2 = spark_randn(&vec![seed2])?; + + match (result1, result2) { + ( + ColumnarValue::Scalar(ScalarValue::Float64(Some(v1))), + ColumnarValue::Scalar(ScalarValue::Float64(Some(v2))), + ) => { + assert_ne!(v1, v2, "Different seeds should produce different results"); + } + _ => panic!("Expected Float64 scalar results"), + } + Ok(()) + } + + #[test] + fn test_randn_no_seed() -> Result<(), Box<dyn Error>> { + // Without seed, should still produce a valid float + let result = spark_randn(&vec![])?; + + match result { + ColumnarValue::Scalar(ScalarValue::Float64(Some(v))) => { + assert!(v.is_finite(), "Result should be a finite number"); + } + _ => panic!("Expected Float64 scalar result"), + } + Ok(()) + } + + #[test] + fn test_randn_with_int32_seed() -> Result<(), Box<dyn Error>> { + // Int32 seed should work + let seed = ColumnarValue::Scalar(ScalarValue::Int32(Some(42))); + + let result = spark_randn(&vec![seed])?; + + match result { + ColumnarValue::Scalar(ScalarValue::Float64(Some(v))) => { + assert!(v.is_finite(), "Result should be a finite number"); + } + _ => panic!("Expected Float64 scalar result"), + } + Ok(()) + } + + #[test] + fn test_randn_with_null_seed() -> Result<(), Box<dyn Error>> { + // Null seed should be treated as no seed (random) + let seed = ColumnarValue::Scalar(ScalarValue::Null); + + let result = spark_randn(&vec![seed])?; + + match result { + ColumnarValue::Scalar(ScalarValue::Float64(Some(v))) => { + assert!(v.is_finite(), "Result should be a finite number"); + } + _ => panic!("Expected Float64 scalar result"), + } + Ok(()) + } + +} Review Comment: The test coverage is incomplete as it only tests scalar seed values. Tests should be added for columnar (array) inputs to verify that the function correctly generates different random values for each row in a batch. This is the primary use case when randn() is used in DataFrame queries. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
