Skip to main content

reifydb_routine/function/math/
modulo.rs

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