Skip to content

Apache DataFusion SQL Query Engine

Compute gcd with u64 instead of i64 because of overflows (#11036)

Lorrens Pantelis8aad936e3b80
diff --git a/datafusion/functions/src/math/gcd.rs b/datafusion/functions/src/math/gcd.rs
index 41c9e4e233..d0199f7a22 100644
--- a/datafusion/functions/src/math/gcd.rs
+++ b/datafusion/functions/src/math/gcd.rs
@@ -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;
@@ -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;
}
}
}
diff --git a/datafusion/sqllogictest/test_files/scalar.slt b/datafusion/sqllogictest/test_files/scalar.slt
index 04b0fa8aca..551c50e0a1 100644
--- a/datafusion/sqllogictest/test_files/scalar.slt
+++ b/datafusion/sqllogictest/test_files/scalar.slt
@@ -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;