sunchao commented on code in PR #7807:
URL: https://github.com/apache/datafusion-comet/pull/7807#discussion_r4793778651
##########
native/spark-expr/src/math_funcs/numeric_guard.rs:
##########
@@ -113,3 +113,64 @@ pub fn create_null_if_zero_expr(
Arc::default(),
)?))
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use arrow::datatypes::{i256, Field};
+ use NumericGuard::{NullIfNonPositive, NullIfZero};
+
+ fn assert_guard<T: Copy>(
+ guard: NumericGuard,
+ scalar: impl Fn(Option<T>) -> ScalarValue,
+ input: &[Option<T>],
+ expected: &[Option<T>],
+ ) -> Result<()> {
+ let array =
ScalarValue::iter_to_array(input.iter().copied().map(&scalar))?;
+ let field = Arc::new(Field::new("x", array.data_type().clone(), true));
+ let result = guard.invoke_with_args(ScalarFunctionArgs {
+ args: vec![ColumnarValue::Array(array)],
+ arg_fields: vec![Arc::clone(&field)],
+ number_rows: input.len(),
+ return_field: field,
+ config_options: Arc::default(),
+ })?;
+ let result = result.into_array(input.len())?;
+ assert_eq!(result.len(), expected.len());
+ for (i, value) in input.iter().enumerate() {
+ let value = scalar(*value);
+ let expected = scalar(expected[i]);
+ let actual = ScalarValue::try_from_array(result.as_ref(), i)?;
+ assert_eq!(actual, expected, "{guard:?} array input {value:?}");
+ assert_eq!(
+ guard.scalar(&value)?,
+ expected,
+ "{guard:?} scalar input {value:?}"
+ );
+ }
+ Ok(())
+ }
+
+ #[test]
+ fn float_guards_mask_both_zeros_and_preserve_nan() -> Result<()> {
+ let nan = Some(f64::NAN);
+ let input = [Some(-1.0), Some(-0.0), Some(0.0), Some(1.0), nan, None];
Review Comment:
Thanks for catching this. I added a negative NaN alongside the positive NaN
for both `f32` and `f64`. The test checks that both guards preserve each NaN
for scalar and array inputs, so switching `NullIfNonPositive` to `is_le` would
fail the negative NaN case.
[4791bfeb](https://github.com/apache/datafusion-comet/commit/4791bfeb17f5df1dc0632441e392b6f20daa8351)
##########
native/spark-expr/src/math_funcs/numeric_guard.rs:
##########
@@ -113,3 +113,64 @@ pub fn create_null_if_zero_expr(
Arc::default(),
)?))
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use arrow::datatypes::{i256, Field};
+ use NumericGuard::{NullIfNonPositive, NullIfZero};
+
+ fn assert_guard<T: Copy>(
+ guard: NumericGuard,
+ scalar: impl Fn(Option<T>) -> ScalarValue,
+ input: &[Option<T>],
+ expected: &[Option<T>],
+ ) -> Result<()> {
+ let array =
ScalarValue::iter_to_array(input.iter().copied().map(&scalar))?;
+ let field = Arc::new(Field::new("x", array.data_type().clone(), true));
+ let result = guard.invoke_with_args(ScalarFunctionArgs {
+ args: vec![ColumnarValue::Array(array)],
+ arg_fields: vec![Arc::clone(&field)],
+ number_rows: input.len(),
+ return_field: field,
+ config_options: Arc::default(),
+ })?;
+ let result = result.into_array(input.len())?;
+ assert_eq!(result.len(), expected.len());
+ for (i, value) in input.iter().enumerate() {
+ let value = scalar(*value);
+ let expected = scalar(expected[i]);
Review Comment:
Renamed the loop binding to `expected_value` so `expected` refers to the
slice throughout.
[4791bfeb](https://github.com/apache/datafusion-comet/commit/4791bfeb17f5df1dc0632441e392b6f20daa8351)
--
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]