HaoYang670 commented on code in PR #2717:
URL: https://github.com/apache/arrow-rs/pull/2717#discussion_r970277843
##########
arrow/src/datatypes/native.rs:
##########
@@ -172,32 +175,56 @@ pub(crate) mod native_op {
macro_rules! native_type_op {
($t:tt) => {
impl native_op::ArrowNativeTypeOp for $t {
- fn add_checked(self, rhs: Self) -> Option<Self> {
- self.checked_add(rhs)
+ fn add_checked(self, rhs: Self) -> Result<Self> {
+ self.checked_add(rhs).ok_or_else(|| {
+ ArrowError::ComputeError(format!(
+ "Overflow happened on: {:?} + {:?}",
+ self, rhs
+ ))
+ })
}
fn add_wrapping(self, rhs: Self) -> Self {
self.wrapping_add(rhs)
}
- fn sub_checked(self, rhs: Self) -> Option<Self> {
- self.checked_sub(rhs)
+ fn sub_checked(self, rhs: Self) -> Result<Self> {
+ self.checked_sub(rhs).ok_or_else(|| {
+ ArrowError::ComputeError(format!(
+ "Overflow happened on: {:?} - {:?}",
+ self, rhs
+ ))
+ })
}
fn sub_wrapping(self, rhs: Self) -> Self {
self.wrapping_sub(rhs)
}
- fn mul_checked(self, rhs: Self) -> Option<Self> {
- self.checked_mul(rhs)
+ fn mul_checked(self, rhs: Self) -> Result<Self> {
+ self.checked_mul(rhs).ok_or_else(|| {
+ ArrowError::ComputeError(format!(
+ "Overflow happened on: {:?} * {:?}",
+ self, rhs
+ ))
+ })
}
fn mul_wrapping(self, rhs: Self) -> Self {
self.wrapping_mul(rhs)
}
- fn div_checked(self, rhs: Self) -> Option<Self> {
- self.checked_div(rhs)
+ fn div_checked(self, rhs: Self) -> Result<Self> {
+ if rhs.is_zero() {
+ Err(ArrowError::DivideByZero)
+ } else {
Review Comment:
> it doesn't match the behavior of C++ DivideChecked kernel
Thank you @viirya, I find the code in C++:
https://github.com/apache/arrow/blob/master/cpp/src/arrow/compute/kernels/base_arithmetic_internal.h#L410-L419
and it does the check for float point values.
If we follow the c++ behaviour, maybe we should add some docs such as
```
Float point Division by zero will return an error in `checked_div` but not
panic when using `unchecked_div`
--
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]