Skip to main content

reifydb_routine/function/math/
lcm.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4use reifydb_core::value::column::{Column, columns::Columns, data::ColumnData};
5use reifydb_type::value::r#type::Type;
6
7use crate::function::{Function, FunctionCapability, FunctionContext, FunctionInfo, error::FunctionError};
8
9pub struct Lcm {
10	info: FunctionInfo,
11}
12
13impl Default for Lcm {
14	fn default() -> Self {
15		Self::new()
16	}
17}
18
19impl Lcm {
20	pub fn new() -> Self {
21		Self {
22			info: FunctionInfo::new("math::lcm"),
23		}
24	}
25}
26
27fn numeric_to_i64(data: &ColumnData, i: usize) -> Option<i64> {
28	match data {
29		ColumnData::Int1(c) => c.get(i).map(|&v| v as i64),
30		ColumnData::Int2(c) => c.get(i).map(|&v| v as i64),
31		ColumnData::Int4(c) => c.get(i).map(|&v| v as i64),
32		ColumnData::Int8(c) => c.get(i).copied(),
33		ColumnData::Int16(c) => c.get(i).map(|&v| v as i64),
34		ColumnData::Uint1(c) => c.get(i).map(|&v| v as i64),
35		ColumnData::Uint2(c) => c.get(i).map(|&v| v as i64),
36		ColumnData::Uint4(c) => c.get(i).map(|&v| v as i64),
37		ColumnData::Uint8(c) => c.get(i).map(|&v| v as i64),
38		_ => None,
39	}
40}
41
42fn compute_gcd(mut a: i64, mut b: i64) -> i64 {
43	a = a.abs();
44	b = b.abs();
45	while b != 0 {
46		let t = b;
47		b = a % b;
48		a = t;
49	}
50	a
51}
52
53fn compute_lcm(a: i64, b: i64) -> i64 {
54	if a == 0 || b == 0 {
55		return 0;
56	}
57	(a.abs() / compute_gcd(a, b)) * b.abs()
58}
59
60impl Function for Lcm {
61	fn info(&self) -> &FunctionInfo {
62		&self.info
63	}
64
65	fn capabilities(&self) -> &[FunctionCapability] {
66		&[FunctionCapability::Scalar]
67	}
68
69	fn return_type(&self, _input_types: &[Type]) -> Type {
70		Type::Int8
71	}
72
73	fn execute(&self, ctx: &FunctionContext, args: &Columns) -> Result<Columns, FunctionError> {
74		if args.len() != 2 {
75			return Err(FunctionError::ArityMismatch {
76				function: ctx.fragment.clone(),
77				expected: 2,
78				actual: args.len(),
79			});
80		}
81
82		let a_col = &args[0];
83		let b_col = &args[1];
84
85		let (a_data, a_bitvec) = a_col.data().unwrap_option();
86		let (b_data, b_bitvec) = b_col.data().unwrap_option();
87		let row_count = a_data.len();
88
89		let expected_types = vec![
90			Type::Int1,
91			Type::Int2,
92			Type::Int4,
93			Type::Int8,
94			Type::Uint1,
95			Type::Uint2,
96			Type::Uint4,
97			Type::Uint8,
98		];
99		if !a_data.get_type().is_number() {
100			return Err(FunctionError::InvalidArgumentType {
101				function: ctx.fragment.clone(),
102				argument_index: 0,
103				expected: expected_types,
104				actual: a_data.get_type(),
105			});
106		}
107		if !b_data.get_type().is_number() {
108			return Err(FunctionError::InvalidArgumentType {
109				function: ctx.fragment.clone(),
110				argument_index: 1,
111				expected: expected_types,
112				actual: b_data.get_type(),
113			});
114		}
115
116		let mut result = Vec::with_capacity(row_count);
117		let mut res_bitvec = Vec::with_capacity(row_count);
118
119		for i in 0..row_count {
120			match (numeric_to_i64(a_data, i), numeric_to_i64(b_data, i)) {
121				(Some(a), Some(b)) => {
122					result.push(compute_lcm(a, b));
123					res_bitvec.push(true);
124				}
125				_ => {
126					result.push(0);
127					res_bitvec.push(false);
128				}
129			}
130		}
131
132		let result_data = ColumnData::int8_with_bitvec(result, res_bitvec);
133		let combined_bitvec = match (a_bitvec, b_bitvec) {
134			(Some(a), Some(b)) => Some(a.and(b)),
135			(Some(a), None) => Some(a.clone()),
136			(None, Some(b)) => Some(b.clone()),
137			(None, None) => None,
138		};
139
140		let final_data = if let Some(bv) = combined_bitvec {
141			ColumnData::Option {
142				inner: Box::new(result_data),
143				bitvec: bv,
144			}
145		} else {
146			result_data
147		};
148
149		Ok(Columns::new(vec![Column::new(ctx.fragment.clone(), final_data)]))
150	}
151}