datafusion_functions/math/
lcm.rsuse std::any::Any;
use std::sync::{Arc, OnceLock};
use arrow::array::{ArrayRef, Int64Array};
use arrow::datatypes::DataType;
use arrow::datatypes::DataType::Int64;
use arrow::error::ArrowError;
use datafusion_common::{arrow_datafusion_err, exec_err, DataFusionError, Result};
use datafusion_expr::scalar_doc_sections::DOC_SECTION_MATH;
use datafusion_expr::{
ColumnarValue, Documentation, ScalarUDFImpl, Signature, Volatility,
};
use super::gcd::unsigned_gcd;
use crate::utils::make_scalar_function;
#[derive(Debug)]
pub struct LcmFunc {
signature: Signature,
}
impl Default for LcmFunc {
fn default() -> Self {
LcmFunc::new()
}
}
impl LcmFunc {
pub fn new() -> Self {
use DataType::*;
Self {
signature: Signature::uniform(2, vec![Int64], Volatility::Immutable),
}
}
}
impl ScalarUDFImpl for LcmFunc {
fn as_any(&self) -> &dyn Any {
self
}
fn name(&self) -> &str {
"lcm"
}
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(lcm, vec![])(args)
}
fn documentation(&self) -> Option<&Documentation> {
Some(get_lcm_doc())
}
}
static DOCUMENTATION: OnceLock<Documentation> = OnceLock::new();
fn get_lcm_doc() -> &'static Documentation {
DOCUMENTATION.get_or_init(|| {
Documentation::builder()
.with_doc_section(DOC_SECTION_MATH)
.with_description(
"Returns the least common multiple of `expression_x` and `expression_y`. Returns 0 if either input is zero.",
)
.with_syntax_example("lcm(expression_x, expression_y)")
.with_standard_argument("expression_x", Some("First numeric"))
.with_standard_argument("expression_y", Some("Second numeric"))
.build()
.unwrap()
})
}
fn lcm(args: &[ArrayRef]) -> Result<ArrayRef> {
let compute_lcm = |x: i64, y: i64| {
if x == 0 || y == 0 {
return Ok(0);
}
let a = x.unsigned_abs();
let b = y.unsigned_abs();
let gcd = unsigned_gcd(a, b);
(a / gcd)
.checked_mul(b)
.and_then(|v| i64::try_from(v).ok())
.ok_or_else(|| {
arrow_datafusion_err!(ArrowError::ComputeError(format!(
"Signed integer overflow in LCM({x}, {y})"
)))
})
};
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_lcm(a1, a2)?)),
_ => Ok(None),
})
.collect::<Result<Int64Array>>()
.map(Arc::new)? as ArrayRef)
}
other => exec_err!("Unsupported data type {other:?} for function lcm"),
}
}
#[cfg(test)]
mod test {
use std::sync::Arc;
use arrow::array::{ArrayRef, Int64Array};
use datafusion_common::cast::as_int64_array;
use crate::math::lcm::lcm;
#[test]
fn test_lcm_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 = lcm(&args).expect("failed to initialize function lcm");
let ints = as_int64_array(&result).expect("failed to initialize function lcm");
assert_eq!(ints.len(), 4);
assert_eq!(ints.value(0), 0);
assert_eq!(ints.value(1), 6);
assert_eq!(ints.value(2), 75);
assert_eq!(ints.value(3), 16);
}
}