HaoYang670 commented on code in PR #2643:
URL: https://github.com/apache/arrow-rs/pull/2643#discussion_r962151477
##########
arrow/src/compute/kernels/arithmetic.rs:
##########
@@ -1042,15 +1196,18 @@ pub fn divide_dyn(left: &dyn Array, right: &dyn Array)
-> Result<ArrayRef> {
/// Perform `left / right` operation on two arrays without checking for
division by zero.
/// The result of dividing by zero follows normal floating point rules.
Review Comment:
We should update the doc
##########
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:
What is the difference between this function and `math_checked_divide_op`
and why do we need both of them?
##########
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:
`Some(self - rhs)` here
--
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]