viirya commented on code in PR #7807:
URL: https://github.com/apache/datafusion-comet/pull/7807#discussion_r4764359504


##########
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:
   Nit: this shadows the `expected` slice inside the loop. Another name, such 
as `want`, would read more easily.



##########
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:
   This doesn't yet pin the "NaNs pass through" half of the comment on 
`invalid`. Arrow's `ArrowNativeTypeOp::is_le` compares floats by total order. 
If `NullIfNonPositive` were rewritten as `value.is_le(T::ZERO)`, this test 
would still pass, because `-0.0` is still `<= 0` and the positive `f64::NAN` 
still sorts above zero. A NaN with the sign bit set sorts below `-inf`, so it 
would be nulled, while Spark returns NaN for `ln` of any NaN. Such a NaN can 
come from data, e.g. `ln(-d)` on a NaN row. Adding `Some(-f64::NAN)` to 
`input`, `divisors` and `log_args` makes the test fail on that rewrite and pass 
on the current code.



-- 
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]

Reply via email to