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


##########
native/spark-expr/benches/check_overflow.rs:
##########
@@ -85,6 +95,9 @@ fn criterion_benchmark(c: &mut Criterion) {
     c.bench_function("check_overflow: ansi no overflow", |b| {
         b.iter(|| black_box(ansi.evaluate(black_box(&no_overflow)).unwrap()))
     });
+    c.bench_function("check_overflow: ansi no overflow, with nulls", |b| {
+        b.iter(|| 
black_box(ansi.evaluate(black_box(&no_overflow_nulls)).unwrap()))
+    });

Review Comment:
   Added `last row overflow` as you wrote it. With the scan in this revision it 
takes 7.38 µs against 8.72 µs on `main`, whose scan also reads the whole batch 
there before `null_if_overflow_precision` runs. I also added `no overflow, null 
slot out of range`, the worst case for a scan that includes null slots: the 
`with nulls` batch with a value that doesn't fit in its last slot, which is 
null. The numbers are in the description.
   



##########
native/spark-expr/src/math_funcs/internal/checkoverflow.rs:
##########
@@ -249,6 +248,38 @@ impl PhysicalExpr for CheckOverflow {
     }
 }
 
+/// Values per chunk in the no-null scan of [`fits_precision`].
+const FITS_PRECISION_CHUNK: usize = 64;
+
+/// Whether every non-null value of `array` fits `precision`, the check that
+/// `Decimal128Type::is_valid_decimal_precision` makes for one value.
+#[inline]
+fn fits_precision(array: &Decimal128Array, precision: u8) -> bool {
+    let bounds = (
+        MIN_DECIMAL128_FOR_EACH_PRECISION.get(precision as usize),
+        MAX_DECIMAL128_FOR_EACH_PRECISION.get(precision as usize),
+    );
+    match bounds {
+        (Some(&min), Some(&max)) if array.null_count() == 0 => {
+            // Without nulls every slot holds a value, so scan the values 
buffer directly instead
+            // of reading the null bitmap per slot. Shifting by `min` turns 
the two bounds checks
+            // into one unsigned comparison. The check is branchless within a 
chunk, which lets
+            // the compiler unroll it, and the scan still stops at the first 
chunk that holds an
+            // overflow.
+            let range = max.wrapping_sub(min) as u128;
+            array.values().chunks(FITS_PRECISION_CHUNK).all(|chunk| {
+                chunk.iter().fold(true, |fits, &v| {
+                    fits & (v.wrapping_sub(min) as u128 <= range)
+                })
+            })
+        }
+        _ => array
+            .iter()
+            .flatten()
+            .all(|v| Decimal128Type::is_valid_decimal_precision(v, precision)),
+    }

Review Comment:
   Done, with one change to the fallback. Re-running the scan that skips nulls 
makes a batch pay for both passes when a null slot holds a value that doesn't 
fit. With one in the last slot, your version took 7.94 µs and a plain-loop 
version 7.82 µs, against 4.92 µs on `main`. The scan now reads the validity bit 
only at a value that doesn't fit, and if that slot is null it resumes right 
after it, so the same batch takes 3.02 µs. `with nulls` went from 4.86 to 2.98 
µs and `dense nulls` from 4.03 to 2.98 µs. The table in the description has 
every shape.
   



##########
native/spark-expr/src/math_funcs/internal/checkoverflow.rs:
##########
@@ -249,6 +248,38 @@ impl PhysicalExpr for CheckOverflow {
     }
 }
 
+/// Values per chunk in the no-null scan of [`fits_precision`].
+const FITS_PRECISION_CHUNK: usize = 64;
+
+/// Whether every non-null value of `array` fits `precision`, the check that
+/// `Decimal128Type::is_valid_decimal_precision` makes for one value.
+#[inline]
+fn fits_precision(array: &Decimal128Array, precision: u8) -> bool {
+    let bounds = (
+        MIN_DECIMAL128_FOR_EACH_PRECISION.get(precision as usize),
+        MAX_DECIMAL128_FOR_EACH_PRECISION.get(precision as usize),
+    );
+    match bounds {
+        (Some(&min), Some(&max)) if array.null_count() == 0 => {
+            // Without nulls every slot holds a value, so scan the values 
buffer directly instead
+            // of reading the null bitmap per slot. Shifting by `min` turns 
the two bounds checks
+            // into one unsigned comparison. The check is branchless within a 
chunk, which lets
+            // the compiler unroll it, and the scan still stops at the first 
chunk that holds an
+            // overflow.
+            let range = max.wrapping_sub(min) as u128;
+            array.values().chunks(FITS_PRECISION_CHUNK).all(|chunk| {
+                chunk.iter().fold(true, |fits, &v| {
+                    fits & (v.wrapping_sub(min) as u128 <= range)
+                })
+            })

Review Comment:
   You're right on aarch64. In the bench binary the fold built a 32-value loop 
of about 390 instructions, with 80 stack loads and stores and 32 `dup`s. A 
plain loop has no spills: 8 instructions per value with `all`, and 9 with 
`position`, which also counts the index. Over six alternating rounds on Apple 
M-series, the plain `all` over every slot was as fast as the fold over every 
slot or slightly faster: `no overflow` 2.97 µs against 3.04 µs, `with nulls` 
2.97 against 3.05, `ansi no overflow` 2.99 against 3.10.
   
   The pushed scan uses `position`, so that it can resume after a null slot. In 
the same rounds it took 3.09 µs and 3.10 µs on the two no-null shapes, against 
3.04 µs and 3.05 µs for the first revision's fold. So I dropped 
`FITS_PRECISION_CHUNK` and the fold, and the comment now says what the loop 
compiles to. I don't have an x86-64 machine, so that side is unmeasured.
   



##########
native/spark-expr/src/math_funcs/internal/checkoverflow.rs:
##########
@@ -509,4 +540,22 @@ mod tests {
         let out = eval_array(&array_check_overflow(3, 0, false), &batch);
         assert_eq!(out.iter().collect::<Vec<_>>(), vec![Some(999), None]);
     }
+
+    #[test]
+    fn test_array_no_nulls_overflow_past_first_chunk_and_sliced() {
+        // The no-null scan reads the values buffer in chunks, so an overflow 
in a later chunk
+        // must still be found, and a sliced array must only be checked within 
its slice.
+        let mut values = vec![Some(5); 200];
+        values[130] = Some(1000);
+        let batch = array_batch(values, 38, 0);
+        let out = eval_array(&array_check_overflow(3, 0, false), &batch);
+        assert_eq!(out.null_count(), 1);
+        assert!(out.is_null(130));
+
+        // Starts right after the overflowing value, so the slice has a 
non-zero offset.
+        let sliced = batch.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);
+    }
 }

Review Comment:
   Done. I folded the sweep into the test this PR added, now 
`test_array_overflow_at_every_position_and_sliced` (both signs, 203 rows, 
legacy and ANSI, plus the slice), and added precision 38's bounds and 
`i128::MIN`/`i128::MAX` to 
`test_array_boundary_precision_max_passes_and_over_by_one_overflows`. 
`test_array_null_slot_out_of_range_is_ignored` is new. It puts a null slot that 
doesn't fit at every position of a 70-row batch and checks that it changes 
nothing, and that a valid value that doesn't fit right after it or in the last 
row is still found. It also checks a slice, whose null bitmap has an offset.
   



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