viirya commented on code in PR #2643:
URL: https://github.com/apache/arrow-rs/pull/2643#discussion_r962177201
##########
arrow/src/compute/kernels/arithmetic.rs:
##########
@@ -103,6 +104,99 @@ where
Ok(PrimitiveArray::<LT>::from(data))
}
+/// This is similar to `math_op` as it performs given operation between two
input primitive arrays.
+/// But the given operation can return `None` if overflow is detected. For the
case, this function
+/// returns an `Err`.
+fn math_checked_op<LT, RT, F>(
+ left: &PrimitiveArray<LT>,
+ right: &PrimitiveArray<RT>,
+ op: F,
+) -> Result<PrimitiveArray<LT>>
+where
+ LT: ArrowNumericType,
+ RT: ArrowNumericType,
+ F: Fn(LT::Native, RT::Native) -> Option<LT::Native>,
+{
+ if left.len() != right.len() {
+ return Err(ArrowError::ComputeError(
+ "Cannot perform math operation on arrays of different
length".to_string(),
+ ));
+ }
+
+ let left_iter = ArrayIter::new(left);
+ let right_iter = ArrayIter::new(right);
+
+ let values: Result<Vec<Option<<LT as ArrowPrimitiveType>::Native>>> =
left_iter
+ .into_iter()
+ .zip(right_iter.into_iter())
+ .map(|(l, r)| {
+ if let (Some(l), Some(r)) = (l, r) {
+ let result = op(l, r);
+ if let Some(r) = result {
+ Ok(Some(r))
+ } else {
+ // Overflow
+ Err(ArrowError::ComputeError("Overflow
happened".to_string()))
+ }
+ } else {
+ Ok(None)
+ }
+ })
+ .collect();
+
+ let values = values?;
+
+ Ok(PrimitiveArray::<LT>::from_iter(values))
+}
+
+/// This is similar to `math_checked_op` but just for divide op.
+fn math_checked_divide<LT, RT, F>(
Review Comment:
Finally I hope we can just have one. Currently `math_checked_divide_op` is
used by `divide_dyn` and I want to limit the range of change to primitive
kernels only.
##########
arrow/src/datatypes/native.rs:
##########
@@ -114,6 +115,98 @@ pub trait ArrowPrimitiveType: 'static {
}
}
+/// Trait for ArrowNativeType to provide overflow-aware operations.
+pub trait ArrowNativeTypeOp:
+ ArrowNativeType
+ + Add<Output = Self>
+ + Sub<Output = Self>
+ + Mul<Output = Self>
+ + Div<Output = Self>
+{
+ fn checked_add_if_applied(self, rhs: Self) -> Option<Self> {
+ Some(self + rhs)
+ }
+
+ fn wrapping_add_if_applied(self, rhs: Self) -> Self {
+ self + rhs
+ }
+
+ fn checked_sub_if_applied(self, rhs: Self) -> Option<Self> {
+ Some(self + rhs)
Review Comment:
Oops, fixed.
--
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]