-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Compute gcd with u64 instead of i64 because of overflows #11036
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c251260
compute gcd with unsigned ints
LorrensP-2158466 129ba75
add test for the i64::MAX cases
LorrensP-2158466 2486608
move unsigned_abs below zero test to remove unnecessary casts
LorrensP-2158466 0700581
add slt test for gcd on max values instead of unit tests
LorrensP-2158466 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,14 +88,15 @@ fn gcd(args: &[ArrayRef]) -> Result<ArrayRef> { | |
|
||
/// Computes greatest common divisor using Binary GCD algorithm. | ||
pub fn compute_gcd(x: i64, y: i64) -> i64 { | ||
let mut a = x.wrapping_abs(); | ||
let mut b = y.wrapping_abs(); | ||
// because the input values are i64, casting these u64's back to i64 causes no overflow | ||
let mut a = x.unsigned_abs(); | ||
let mut b = y.unsigned_abs(); | ||
|
||
if a == 0 { | ||
return b; | ||
return b as i64; | ||
} | ||
if b == 0 { | ||
return a; | ||
return a as i64; | ||
} | ||
|
||
let shift = (a | b).trailing_zeros(); | ||
|
@@ -112,7 +113,7 @@ pub fn compute_gcd(x: i64, y: i64) -> i64 { | |
b -= a; | ||
|
||
if b == 0 { | ||
return a << shift; | ||
return (a << shift) as i64; | ||
} | ||
} | ||
} | ||
|
@@ -142,4 +143,22 @@ mod test { | |
assert_eq!(ints.value(2), 5); | ||
assert_eq!(ints.value(3), 8); | ||
} | ||
|
||
// from issue https://github.com/apache/datafusion/issues/11031 we know that the previous implementation could | ||
// not handle cases were one or both of the inputs were an i64::MAX or i64::MIN coupled with other values (neg or pos) | ||
#[test] | ||
fn test_gcd_i64_maxes() { | ||
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 think these tests can be implemented as slt. |
||
let args: Vec<ArrayRef> = vec![ | ||
Arc::new(Int64Array::from(vec![i64::MAX, i64::MIN, i64::MAX])), // x | ||
Arc::new(Int64Array::from(vec![i64::MIN, -1, -1])), // y | ||
]; | ||
|
||
let result = gcd(&args).expect("failed to initialize function gcd"); | ||
let ints = as_int64_array(&result).expect("failed to initialize function gcd"); | ||
|
||
assert_eq!(ints.len(), 3); | ||
assert_eq!(ints.value(0), 1); | ||
assert_eq!(ints.value(1), 1); | ||
assert_eq!(ints.value(1), 1); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
If we move
unsigned_abs()
below, we don't need to castThere 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.
Ah yes, nice catch! Thanks!