sunchao commented on code in PR #2740:
URL: https://github.com/apache/arrow-rs/pull/2740#discussion_r973509486
##########
arrow/src/compute/kernels/arithmetic.rs:
##########
@@ -78,6 +80,30 @@ where
Ok(binary(left, right, op))
}
+/// 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
Review Comment:
hmm what's the "given operation"?
##########
arrow/src/compute/kernels/arithmetic.rs:
##########
@@ -570,6 +596,28 @@ macro_rules! math_dict_op {
}};
}
+/// Helper function to perform math lambda function on values from two
dictionary arrays, this
+/// version does not attempt to use SIMD explicitly (though the compiler may
auto vectorize)
+macro_rules! math_dict_checked_op {
Review Comment:
hmm this can't be a generic function?
##########
arrow/src/compute/kernels/arithmetic.rs:
##########
@@ -734,7 +802,84 @@ pub fn add_dyn(left: &dyn Array, right: &dyn Array) ->
Result<ArrayRef> {
_ => {
downcast_primitive_array!(
(left, right) => {
- math_op(left, right, |a, b| a + b).map(|a| Arc::new(a) as
ArrayRef)
+ math_op(left, right, |a, b| a.add_wrapping(b)).map(|a|
Arc::new(a) as ArrayRef)
+ }
+ _ => Err(ArrowError::CastError(format!(
+ "Unsupported data type {}, {}",
+ left.data_type(), right.data_type()
+ )))
+ )
+ }
+ }
+}
+
+/// Perform `left + right` operation on two arrays. If either left or right
value is null
+/// then the result is also null.
+///
+/// This detects overflow and returns an `Err` for that. For an
non-overflow-checking variant,
+/// use `add_dyn` instead.
+pub fn add_dyn_checked(left: &dyn Array, right: &dyn Array) ->
Result<ArrayRef> {
+ match left.data_type() {
+ DataType::Dictionary(_, _) => {
+ typed_dict_math_op!(
+ left,
+ right,
+ |a, b| a.add_checked(b),
+ math_checked_op_dict
+ )
+ }
+ DataType::Date32 => {
Review Comment:
nit: I wonder if the handling of `Date32` and `Date64` can be extracted as a
separate function so it can be shared with `add_dyn`, but feel free to ignore
if this is not easy.
##########
arrow/src/compute/kernels/arithmetic.rs:
##########
@@ -570,6 +596,28 @@ macro_rules! math_dict_op {
}};
}
+/// Helper function to perform math lambda function on values from two
dictionary arrays, this
+/// version does not attempt to use SIMD explicitly (though the compiler may
auto vectorize)
+macro_rules! math_dict_checked_op {
+ ($left: expr, $right:expr, $op:expr, $value_ty:ty) => {{
+ if $left.len() != $right.len() {
+ return Err(ArrowError::ComputeError(format!(
+ "Cannot perform operation on arrays of different length ({},
{})",
+ $left.len(),
+ $right.len()
+ )));
+ }
+
+ // Safety justification: Since the inputs are valid Arrow arrays, all
values are
Review Comment:
is there any unsafe code 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]