use arrow::array::{ArrayRef, Int64Array};
use arrow::error::ArrowError;
use std::any::Any;
use std::mem::swap;
use std::sync::Arc;
use arrow::datatypes::DataType;
use arrow::datatypes::DataType::Int64;
use crate::utils::make_scalar_function;
use datafusion_common::{arrow_datafusion_err, exec_err, DataFusionError, Result};
use datafusion_expr::ColumnarValue;
use datafusion_expr::{ScalarUDFImpl, Signature, Volatility};
#[derive(Debug)]
pub struct GcdFunc {
signature: Signature,
}
impl Default for GcdFunc {
fn default() -> Self {
Self::new()
}
}
impl GcdFunc {
pub fn new() -> Self {
use DataType::*;
Self {
signature: Signature::uniform(2, vec![Int64], Volatility::Immutable),
}
}
}
impl ScalarUDFImpl for GcdFunc {
fn as_any(&self) -> &dyn Any {
self
}
fn name(&self) -> &str {
"gcd"
}
fn signature(&self) -> &Signature {
&self.signature
}
fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
Ok(Int64)
}
fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> {
make_scalar_function(gcd, vec![])(args)
}
}
fn gcd(args: &[ArrayRef]) -> Result<ArrayRef> {
match args[0].data_type() {
Int64 => {
let arg1 = downcast_arg!(&args[0], "x", Int64Array);
let arg2 = downcast_arg!(&args[1], "y", Int64Array);
Ok(arg1
.iter()
.zip(arg2.iter())
.map(|(a1, a2)| match (a1, a2) {
(Some(a1), Some(a2)) => Ok(Some(compute_gcd(a1, a2)?)),
_ => Ok(None),
})
.collect::<Result<Int64Array>>()
.map(Arc::new)? as ArrayRef)
}
other => exec_err!("Unsupported data type {other:?} for function gcd"),
}
}
pub(super) fn unsigned_gcd(mut a: u64, mut b: u64) -> u64 {
if a == 0 {
return b;
}
if b == 0 {
return a;
}
let shift = (a | b).trailing_zeros();
a >>= shift;
b >>= shift;
a >>= a.trailing_zeros();
loop {
b >>= b.trailing_zeros();
if a > b {
swap(&mut a, &mut b);
}
b -= a;
if b == 0 {
return a << shift;
}
}
}
pub fn compute_gcd(x: i64, y: i64) -> Result<i64> {
let a = x.unsigned_abs();
let b = y.unsigned_abs();
let r = unsigned_gcd(a, b);
r.try_into().map_err(|_| {
arrow_datafusion_err!(ArrowError::ComputeError(format!(
"Signed integer overflow in GCD({x}, {y})"
)))
})
}
#[cfg(test)]
mod test {
use std::sync::Arc;
use arrow::{
array::{ArrayRef, Int64Array},
error::ArrowError,
};
use crate::math::gcd::gcd;
use datafusion_common::{cast::as_int64_array, DataFusionError};
#[test]
fn test_gcd_i64() {
let args: Vec<ArrayRef> = vec![
Arc::new(Int64Array::from(vec![0, 3, 25, -16])), Arc::new(Int64Array::from(vec![0, -2, 15, 8])), ];
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(), 4);
assert_eq!(ints.value(0), 0);
assert_eq!(ints.value(1), 1);
assert_eq!(ints.value(2), 5);
assert_eq!(ints.value(3), 8);
}
#[test]
fn overflow_on_both_param_i64_min() {
let args: Vec<ArrayRef> = vec![
Arc::new(Int64Array::from(vec![i64::MIN])), Arc::new(Int64Array::from(vec![i64::MIN])), ];
match gcd(&args) {
Err(DataFusionError::ArrowError(ArrowError::ComputeError(_), _)) => {}
Err(_) => {
panic!("failed to initialize function gcd")
}
Ok(_) => panic!("GCD({0}, {0}) should have overflown", i64::MIN),
};
}
}