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


##########
arrow-arith/src/numeric.rs:
##########
@@ -427,22 +560,35 @@ divisor_op!(
 );
 
 /// Perform an arithmetic operation on integers
-fn integer_op<T: ArrowPrimitiveType>(
+fn integer_op<T>(
     op: Op,
     l: &dyn Array,
     l_s: bool,
     r: &dyn Array,
     r_s: bool,
-) -> Result<ArrayRef, ArrowError> {
+) -> Result<ArrayRef, ArrowError>
+where
+    T: ArrowPrimitiveType,
+    T::Native: OverflowingOp,
+{
     let l = l.as_primitive::<T>();
     let r = r.as_primitive::<T>();
     let array: PrimitiveArray<T> = match op {
         Op::AddWrapping => op!(l, l_s, r, r_s, l.add_wrapping(r)),
-        Op::Add => try_op!(l, l_s, r, r_s, l.add_checked(r)),
+        Op::Add => 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::SubWrapping => op!(l, l_s, r, r_s, l.sub_wrapping(r)),
-        Op::Sub => try_op!(l, l_s, r, r_s, l.sub_checked(r)),
+        Op::Sub => match overflowing_op(l, l_s, r, r_s, 
T::Native::sub_overflowing) {

Review Comment:
   > Could you add a nullable `UInt32` or `UInt64` array-minus-scalar benchmark 
and avoid replay on this case?
   
   Both done. When a slot overflowed and the result has nulls, `overflowing_op` 
now recomputes the overflow of every row into a bitmap with 
`BooleanBuffer::collect_bool` and masks it with the validity bits. If no valid 
row overflowed, it returns the values from the first pass and doesn't call the 
checked kernel. `test_checked_integer_slice_null_scalar_and_lengths` checks 
your example, including that the builder wrote 0 into the null slot.
   
   `uint32_subtract_checked_scalar(0.1)` and 
`uint64_subtract_checked_scalar(0.1)` measure it. 
`uint32_subtract_checked(0.1)` and `uint64_subtract_checked(0.1)` take the same 
path, because the left array's null slots hold 0 too. The `uint32` and `uint64` 
cases take their left operands from a higher range than their right operands, 
so valid rows never underflow. Against the bench commit, 
`uint32_subtract_checked_scalar(0.1)` measured 5.92x on 
[Neoverse-V2](https://github.com/apache/arrow-rs/pull/12058#issuecomment-6863769659),
 1.17x on [Xeon 
8581C](https://github.com/apache/arrow-rs/pull/12058#issuecomment-6863835999), 
and 2.20x with 
[`x86-64-v3`](https://github.com/apache/arrow-rs/pull/12058#issuecomment-6864013103).
 `uint64_subtract_checked_scalar(0.1)` measured 4.59x, 1.13x, and 2.31x.



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