-
Notifications
You must be signed in to change notification settings - Fork 847
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add modulus ops into ArrowNativeTypeOp
#2756
Changes from 2 commits
d2497d2
40f8166
ac7dbb5
efa2c74
c3f3b88
3557bd7
5680942
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -47,7 +47,7 @@ pub(crate) mod native_op { | |||||
use super::ArrowNativeType; | ||||||
use crate::error::{ArrowError, Result}; | ||||||
use num::Zero; | ||||||
use std::ops::{Add, Div, Mul, Sub}; | ||||||
use std::ops::{Add, Div, Mul, Rem, Sub}; | ||||||
|
||||||
/// Trait for ArrowNativeType to provide overflow-checking and non-overflow-checking | ||||||
/// variants for arithmetic operations. For floating point types, this provides some | ||||||
|
@@ -65,6 +65,7 @@ pub(crate) mod native_op { | |||||
+ Sub<Output = Self> | ||||||
+ Mul<Output = Self> | ||||||
+ Div<Output = Self> | ||||||
+ Rem<Output = Self> | ||||||
+ Zero | ||||||
{ | ||||||
fn add_checked(self, rhs: Self) -> Result<Self> { | ||||||
|
@@ -102,6 +103,28 @@ pub(crate) mod native_op { | |||||
fn div_wrapping(self, rhs: Self) -> Self { | ||||||
self / rhs | ||||||
} | ||||||
|
||||||
/// Check `DivideByZero` error and `Overflow` error. | ||||||
fn mod_fully_checked(self, rhs: Self) -> Result<Self> { | ||||||
if rhs.is_zero() { | ||||||
Err(ArrowError::DivideByZero) | ||||||
} else { | ||||||
Ok(self.mod_wrapping(rhs)) | ||||||
} | ||||||
} | ||||||
|
||||||
/// Only check `DivideByZero` error | ||||||
fn mod_checked_divide_by_zero(self, rhs: Self) -> Result<Self> { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't really like to pollute the API arbitrarily. Could you keep two APIs (_wrapping, _checked) for mod? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reason I add 3 APIs here is that
An alternative I think is that we could make There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As you can see, it is very easy to do checking of division by zero only check (as did in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This makes sense. I will update the PR. |
||||||
if rhs.is_zero() { | ||||||
Err(ArrowError::DivideByZero) | ||||||
} else { | ||||||
Ok(self.mod_wrapping(rhs)) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is default implementation for floating point values... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, the panic only occurs on signed integers There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not changed yet. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! |
||||||
} | ||||||
} | ||||||
|
||||||
fn mod_wrapping(self, rhs: Self) -> Self { | ||||||
self % rhs | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
|
@@ -163,6 +186,23 @@ macro_rules! native_type_op { | |||||
fn div_wrapping(self, rhs: Self) -> Self { | ||||||
self.wrapping_div(rhs) | ||||||
} | ||||||
|
||||||
fn mod_fully_checked(self, rhs: Self) -> Result<Self> { | ||||||
if rhs.is_zero() { | ||||||
Err(ArrowError::DivideByZero) | ||||||
} else { | ||||||
self.checked_rem(rhs).ok_or_else(|| { | ||||||
ArrowError::ComputeError(format!( | ||||||
"Overflow happened on: {:?} % {:?}", | ||||||
self, rhs | ||||||
)) | ||||||
}) | ||||||
} | ||||||
} | ||||||
|
||||||
fn mod_wrapping(self, rhs: Self) -> Self { | ||||||
self.wrapping_rem(rhs) | ||||||
} | ||||||
} | ||||||
}; | ||||||
} | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SIMD op doesn't support checking/wrapping variants. You just change its scalar op. We should leave it as it is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a % b
will panic atoverflow
, buta.wrapping_rem(b)
won't:https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=6d04230ac4454c895c2fe9e417c13938
If we don't want
simd_checked_divide_op
to panic, I guess we should usemod_wrapping
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
simd_checked_divide_op
has two parts, one is SIMD op, one is scalar op. SIMD op doesn't support checking/wrapping variants, and I don't think we should change only scalar op.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for explanation. This makes sense. I will update the code and add tests to freeze the behaviour.