mbutrovich commented on code in PR #12062:
URL: https://github.com/apache/arrow-rs/pull/12062#discussion_r4568406821


##########
arrow-arith/src/numeric.rs:
##########
@@ -1812,6 +2059,186 @@ mod tests {
         assert_eq!(err, "Divide by zero error");
     }
 
+    type Kernel = fn(&dyn Datum, &dyn Datum) -> Result<ArrayRef, ArrowError>;
+
+    fn assert_decimal_result<T: DecimalType>(
+        actual: Result<ArrayRef, ArrowError>,
+        expected: Result<Vec<Option<T::Native>>, ArrowError>,
+    ) {
+        match (actual, expected) {
+            (Ok(actual), Ok(expected)) => {
+                let actual: Vec<_> = 
actual.as_primitive::<T>().iter().collect();
+                assert_eq!(actual, expected)
+            }
+            (Err(actual), Err(expected)) => assert_eq!(actual.to_string(), 
expected.to_string()),
+            (actual, expected) => panic!("got {actual:?}, expected 
{expected:?}"),
+        }
+    }
+
+    /// Checks `kernel` against `checked` for every pair of `edges`, with
+    /// arrays on both sides, with each pair as a one-row slice, and with a
+    /// scalar on either side. Every third row of the left array is null, so
+    /// null slots also hold values that overflow.
+    fn test_checked_decimal_op<T: DecimalType>(
+        edges: &[T::Native],
+        (l_scale, r_scale): (i8, i8),
+        kernel: Kernel,
+        checked: impl Fn(T::Native, T::Native) -> Result<T::Native, 
ArrowError>,
+    ) {
+        let decimal = |array: PrimitiveArray<T>, scale| {
+            array
+                .with_precision_and_scale(T::MAX_PRECISION, scale)
+                .unwrap()
+        };
+        let (l, r): (Vec<_>, Vec<_>) = edges
+            .iter()
+            .flat_map(|&x| edges.iter().map(move |&y| (x, y)))
+            .unzip();
+        let nulls = NullBuffer::from_iter((0..l.len()).map(|i| i % 3 != 2));
+        let l_array = decimal(
+            PrimitiveArray::new(l.clone().into(), Some(nulls.clone())),
+            l_scale,
+        );
+        let r_array = 
decimal(PrimitiveArray::from_iter_values(r.iter().copied()), r_scale);
+        let row = |i: usize, x, y| match nulls.is_valid(i) {
+            true => checked(x, y).map(Some),
+            false => Ok(None),
+        };
+
+        // The kernels report the first valid row that overflows.
+        let expected = (0..l.len()).map(|i| row(i, l[i], r[i])).collect();
+        assert_decimal_result::<T>(kernel(&l_array, &r_array), expected);
+
+        // An overflow in one row sends the whole array to the checked kernel,
+        // which would hide a missed overflow in another row, so also check 
each
+        // row alone.
+        for i in 0..l.len() {
+            let actual = kernel(&l_array.slice(i, 1), &r_array.slice(i, 1));
+            assert_decimal_result::<T>(actual, row(i, l[i], r[i]).map(|v| 
vec![v]));
+        }
+
+        for &x in edges {
+            let l_scalar = 
Scalar::new(decimal(PrimitiveArray::from_iter_values([x]), l_scale));
+            let expected = r.iter().map(|&y| checked(x, 
y).map(Some)).collect();
+            assert_decimal_result::<T>(kernel(&l_scalar, &r_array), expected);
+            let r_scalar = 
Scalar::new(decimal(PrimitiveArray::from_iter_values([x]), r_scale));
+            let expected = (0..l.len()).map(|i| row(i, l[i], x)).collect();
+            assert_decimal_result::<T>(kernel(&l_array, &r_scalar), expected);
+        }
+    }
+
+    /// Checks `add`, `sub` and `mul` of `T` at its edge values, with equal
+    /// scales and with either side rescaled by 100.
+    fn test_checked_decimal_ops<T: DecimalType>() {
+        let one = T::Native::ONE;
+        let three = T::Native::usize_as(3);
+        let two = T::Native::usize_as(2);
+        let hundred = T::Native::usize_as(100);
+        let (min, max) = (T::Native::MIN_TOTAL_ORDER, 
T::Native::MAX_TOTAL_ORDER);
+        let edges = [
+            min,
+            min.add_wrapping(one),
+            min.div_wrapping(two),
+            min.div_wrapping(hundred),
+            min.div_wrapping(hundred).sub_wrapping(one),
+            three.neg_wrapping(),
+            one.neg_wrapping(),
+            T::Native::ZERO,
+            one,
+            three,
+            max.div_wrapping(hundred),
+            max.div_wrapping(hundred).add_wrapping(one),
+            max.div_wrapping(two),
+            max.div_wrapping(two).add_wrapping(one),
+            max.sub_wrapping(one),
+            max,
+        ];
+
+        for scales in [(2, 2), (2, 4), (4, 2)] {
+            let (l_mul, r_mul) = match scales {
+                (2, 4) => (hundred, one),
+                (4, 2) => (one, hundred),
+                _ => (one, one),
+            };
+            let rescale = move |l: T::Native, r: T::Native| {
+                Ok::<_, ArrowError>((l.mul_checked(l_mul)?, 
r.mul_checked(r_mul)?))
+            };
+            test_checked_decimal_op::<T>(&edges, scales, add, |l, r| {
+                let (l, r) = rescale(l, r)?;
+                l.add_checked(r)
+            });
+            test_checked_decimal_op::<T>(&edges, scales, sub, |l, r| {
+                let (l, r) = rescale(l, r)?;
+                l.sub_checked(r)
+            });
+            test_checked_decimal_op::<T>(&edges, scales, add_wrapping, |l, r| {
+                let (l, r) = rescale(l, r)?;
+                l.add_checked(r)
+            });
+        }
+        // Multiplication doesn't rescale its operands.
+        test_checked_decimal_op::<T>(&edges, (2, 2), mul, |l, r| 
l.mul_checked(r));
+        test_checked_decimal_op::<T>(&edges, (2, 4), mul_wrapping, |l, r| 
l.mul_checked(r));
+    }
+
+    #[test]
+    fn test_checked_decimal_edges() {
+        test_checked_decimal_ops::<Decimal32Type>();
+        test_checked_decimal_ops::<Decimal64Type>();
+        test_checked_decimal_ops::<Decimal128Type>();
+        test_checked_decimal_ops::<Decimal256Type>();
+    }
+
+    #[test]
+    fn test_checked_decimal128_mul_i64_boundary() {
+        // `i128` multiplies through `i64` when both operands fit, so check 
both
+        // sides of each `i64` bound. `i64::MIN * i64::MIN` is 2^126.
+        let (min, max) = (i64::MIN as i128, i64::MAX as i128);
+        let edges = [min - 1, min, min + 1, -1, 0, 1, max - 1, max, max + 1];
+        test_checked_decimal_op::<Decimal128Type>(&edges, (2, 2), mul, |l, r| 
l.mul_checked(r));
+        test_checked_decimal_op::<Decimal128Type>(&edges, (2, 4), add, |l, r| {
+            l.mul_checked(100)?.add_checked(r)
+        });
+    }
+
+    #[test]
+    fn test_checked_decimal_overflow_position() {
+        let one = Scalar::new(
+            Decimal128Array::from_iter_values([1])
+                .with_precision_and_scale(38, 2)
+                .unwrap(),
+        );
+        // Every length up to 130 puts the overflow in each part of the vector 
loop
+        // for strides up to 64 rows.
+        for len in 1..=130 {
+            let mut values = vec![1; len];
+            values[len - 1] = i128::MAX;

Review Comment:
   > Could this test place the only overflowing row at different positions in a 
fixed-size `Decimal32` or `Decimal64` batch?
   
   Yes. `test_checked_decimal_overflow_positions` now moves one overflowing row 
through a 100-row batch for all four decimal types: three iterations of a 
32-row loop (8 `i32` lanes of AVX2, unrolled four times) plus a remainder. It 
covers `add`, `sub`, and `mul` at equal scales. It also covers `add` and `sub` 
at scales 2 and 4, where the row holds `MAX / 100 + 1` and only the rescale 
overflows. For `Decimal64` that value is your `92_233_720_368_547_759`. Each 
case runs with the batch against an array and against a scalar, on either side, 
with that row valid and with it null. With `collect_overflowing` changed to 
drop the flag on odd rows, it fails.



##########
arrow-arith/src/numeric.rs:
##########
@@ -1195,28 +1406,61 @@ where
             match op {
                 // Equal scales make both decimal multipliers one.
                 Op::Add | Op::AddWrapping if s1 == s2 => {
-                    try_op!(l, l_s, r, r_s, l.add_checked(r))
+                    match overflowing_op(l, l_s, r, r_s, 
T::Native::add_overflowing) {
+                        Some(array) => array,
+                        None => try_op!(l, l_s, r, r_s, l.add_checked(r)),
+                    }
                 }
                 Op::Sub | Op::SubWrapping if s1 == s2 => {
-                    try_op!(l, l_s, r, r_s, l.sub_checked(r))
+                    match overflowing_op(l, l_s, r, r_s, 
T::Native::sub_overflowing) {
+                        Some(array) => array,
+                        None => try_op!(l, l_s, r, r_s, l.sub_checked(r)),
+                    }
                 }
+                // `move` copies the multipliers into the closures. Borrowed, 
they
+                // keep the loop from vectorizing under thin LTO, like the 
borrowed
+                // captures #11525 fixed in `unary`.
                 Op::Add | Op::AddWrapping => {
-                    try_op!(
-                        l,
-                        l_s,
-                        r,
-                        r_s,
-                        
l.mul_checked(l_mul)?.add_checked(r.mul_checked(r_mul)?)
-                    )
+                    let add = T::Native::add_overflowing;
+                    let array = match s1 < s2 {
+                        true => overflowing_op(l, l_s, r, r_s, move |l, r| {
+                            rescale_left(l, l_mul, r, add)

Review Comment:
   > Could we rescale the scalar once before the loop and pass plain 
`add_overflowing` to `overflowing_op`?
   
   Done. `decimal_op` now rescales a scalar that has the smaller scale once, 
with `mul_overflowing`, and then takes the equal-scale path. Before this 
change, LLVM hoisted the multiply only in a copy of the loop guarded by a 
runtime overlap check, because the loop reached the scalar and the multiplier 
through pointers. The copy it runs for short batches redid the 128-bit multiply 
on every row (aarch64 listing of the `Decimal128` scalar-left case). 
`decimal128_add_rescale_scalar` measures `col + 1.5` with `col` at scale 4. 
Without and with 10% nulls, it measured 4.50x and 4.07x on 
[Neoverse-V2](https://github.com/apache/arrow-rs/pull/12062#issuecomment-6863263619),
 10.85x and 10.52x on [Xeon 
8581C](https://github.com/apache/arrow-rs/pull/12062#issuecomment-6863287300), 
and 10.11x and 9.96x with 
[`x86-64-v3`](https://github.com/apache/arrow-rs/pull/12062#issuecomment-6863303751).
   
   > It also means a scalar whose rescale overflows doesn't flag every row and 
force the checked rerun.
   
   When the scalar's rescale overflows, every valid row fails in the checked 
kernel too, so the batch is an error either way. In that case `decimal_op` 
leaves the scalar unscaled and the rows rescale it, so a batch with no valid 
rows still returns nulls. `test_decimal_rescaled_scalar` checks this against an 
all-null array and an empty array.



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

Reply via email to