mbutrovich commented on code in PR #7658:
URL: https://github.com/apache/datafusion-comet/pull/7658#discussion_r4775806130


##########
native/spark-expr/src/math_funcs/internal/checkoverflow.rs:
##########
@@ -508,5 +549,100 @@ mod tests {
         let batch = array_batch(vec![Some(999), Some(9999)], 38, 0);
         let out = eval_array(&array_check_overflow(3, 0, false), &batch);
         assert_eq!(out.iter().collect::<Vec<_>>(), vec![Some(999), None]);
+
+        // The scan shifts by the precision's minimum and compares unsigned, 
so also check the
+        // values where that shift wraps, at the widest precision.
+        let max = MAX_DECIMAL128_FOR_EACH_PRECISION[38];
+        let min = MIN_DECIMAL128_FOR_EACH_PRECISION[38];
+        let fits = array_batch(vec![Some(max), Some(min), Some(0)], 38, 0);
+        let out = eval_array(&array_check_overflow(38, 0, true), &fits);
+        assert_eq!(out.null_count(), 0);
+        for v in [max + 1, min - 1, i128::MAX, i128::MIN] {
+            let batch = array_batch(vec![Some(0), Some(v)], 38, 0);
+            let out = eval_array(&array_check_overflow(38, 0, false), &batch);
+            assert!(out.is_null(1), "{v}");
+            let ansi = array_check_overflow(38, 0, true).evaluate(&batch);
+            assert!(ansi.is_err(), "{v}");
+        }
+    }
+
+    #[test]
+    fn test_array_overflow_at_every_position_and_sliced() {
+        // The scan stops at the first value that doesn't fit, and a compiled 
loop can split the
+        // batch into an unrolled part and a remainder, so move one value that 
doesn't fit
+        // through every row. 203 rows are not a multiple of any unroll factor.
+        for overflow in [1000, -1000] {
+            for i in 0..203 {
+                let mut values = vec![Some(5); 203];
+                values[i] = Some(overflow);
+                let batch = array_batch(values, 38, 0);
+                let out = eval_array(&array_check_overflow(3, 0, false), 
&batch);
+                assert_eq!(out.null_count(), 1, "{overflow} at {i}");
+                assert!(out.is_null(i), "{overflow} at {i}");
+                let ansi = array_check_overflow(3, 0, true).evaluate(&batch);
+                assert!(ansi.is_err(), "{overflow} at {i}");
+            }
+        }
+
+        // A sliced array must only be checked within its slice. This one 
starts right after
+        // the overflowing value, so the slice has a non-zero offset.
+        let mut values = vec![Some(5); 200];
+        values[130] = Some(1000);
+        let sliced = array_batch(values, 38, 0).slice(131, 69);
+        let out = eval_array(&array_check_overflow(3, 0, true), &sliced);
+        assert_eq!(out.len(), 69);
+        assert_eq!(out.null_count(), 0);
+    }
+
+    /// A Decimal128(38, 0) batch whose null slots keep the values given for 
them.
+    fn array_batch_with_null_slots(values: Vec<i128>, valid: Vec<bool>) -> 
RecordBatch {
+        let arr = Decimal128Array::new(values.into(), 
Some(NullBuffer::from(valid)))
+            .with_precision_and_scale(38, 0)
+            .unwrap();
+        let schema = Schema::new(vec![Field::new("d", arr.data_type().clone(), 
true)]);
+        RecordBatch::try_new(Arc::new(schema), vec![Arc::new(arr)]).unwrap()
+    }
+
+    #[test]
+    fn test_array_null_slot_out_of_range_is_ignored() {
+        // A null slot can hold any value. One that doesn't fit the precision 
must not null out
+        // valid values or raise an ANSI error, and the scan must still find a 
value that doesn't
+        // fit in a valid slot after it, next to it or at the end.
+        let n = 70;
+        for i in 0..n - 1 {
+            let mut values = vec![5_i128; n];
+            let mut valid = vec![true; n];
+            values[i] = 1000;
+            valid[i] = false;
+            let mut expected = vec![Some(5_i128); n];
+            expected[i] = None;
+            let batch = array_batch_with_null_slots(values.clone(), 
valid.clone());
+            for fail_on_error in [false, true] {
+                let out = eval_array(&array_check_overflow(3, 0, 
fail_on_error), &batch);
+                assert_eq!(out.iter().collect::<Vec<_>>(), expected, "null 
slot {i}");
+            }
+            for j in [i + 1, n - 1] {

Review Comment:
   The loop stops at `n - 2`, so no case puts the null slot that doesn't fit in 
the last row. That is the case where `fits_precision` resumes with `start == 
values.len()`, and the one the `null slot out of range` bench measures. A bug 
at that boundary gets through today: I added `if start == values.len() { return 
false; }` after `start = index + 1` in `fits_precision`, and all 18 
`checkoverflow` tests still passed. With the change below, this test fails on 
that bug.
   
   ```suggestion
           for i in 0..n {
               let mut values = vec![5_i128; n];
               let mut valid = vec![true; n];
               values[i] = 1000;
               valid[i] = false;
               let mut expected = vec![Some(5_i128); n];
               expected[i] = None;
               let batch = array_batch_with_null_slots(values.clone(), 
valid.clone());
               for fail_on_error in [false, true] {
                   let out = eval_array(&array_check_overflow(3, 0, 
fail_on_error), &batch);
                   assert_eq!(out.iter().collect::<Vec<_>>(), expected, "null 
slot {i}");
               }
               // A null slot in the last row has no valid slot after it.
               for j in [i + 1, n - 1].into_iter().filter(|&j| i < j && j < n) {
   ```



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