Skip to content
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 4 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions datafusion/functions/src/math/gcd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,16 @@ 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();

if a == 0 {
return b;
if x == 0 {
return y;
}
if b == 0 {
return a;
if y == 0 {
return x;
}

let mut a = x.unsigned_abs();
let mut b = y.unsigned_abs();

let shift = (a | b).trailing_zeros();
a >>= shift;
b >>= shift;
Expand All @@ -112,7 +112,8 @@ pub fn compute_gcd(x: i64, y: i64) -> i64 {
b -= a;

if b == 0 {
return a << shift;
// because the input values are i64, casting this back to i64 is safe
return (a << shift) as i64;
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions datafusion/sqllogictest/test_files/scalar.slt
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,16 @@ select gcd(null, null);
----
NULL

# scalar maxes and/or negative 1
query III rowsort
select
gcd(9223372036854775807, -9223372036854775808), -- i64::MIN, i64::MAX
-- wait till fix, cause it fails gcd(-9223372036854775808, -9223372036854775808), -- -i64::MIN, i64::MIN
gcd(9223372036854775807, -1), -- i64::MAX, -1
gcd(-9223372036854775808, -1); -- i64::MIN, -1
----
1 1 1

# gcd with columns
query III rowsort
select gcd(a, b), gcd(c, d), gcd(e, f) from signed_integers;
Expand Down