Skip to main content

reifydb_function/math/scalar/
clamp.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2// Copyright (c) 2025 ReifyDB
3
4use num_traits::ToPrimitive;
5use reifydb_core::value::column::data::ColumnData;
6use reifydb_type::value::r#type::Type;
7
8use crate::{ScalarFunction, ScalarFunctionContext, error::ScalarFunctionError, propagate_options};
9
10pub struct Clamp;
11
12impl Clamp {
13	pub fn new() -> Self {
14		Self
15	}
16}
17
18fn numeric_to_f64(data: &ColumnData, i: usize) -> Option<f64> {
19	match data {
20		ColumnData::Int1(c) => c.get(i).map(|&v| v as f64),
21		ColumnData::Int2(c) => c.get(i).map(|&v| v as f64),
22		ColumnData::Int4(c) => c.get(i).map(|&v| v as f64),
23		ColumnData::Int8(c) => c.get(i).map(|&v| v as f64),
24		ColumnData::Int16(c) => c.get(i).map(|&v| v as f64),
25		ColumnData::Uint1(c) => c.get(i).map(|&v| v as f64),
26		ColumnData::Uint2(c) => c.get(i).map(|&v| v as f64),
27		ColumnData::Uint4(c) => c.get(i).map(|&v| v as f64),
28		ColumnData::Uint8(c) => c.get(i).map(|&v| v as f64),
29		ColumnData::Uint16(c) => c.get(i).map(|&v| v as f64),
30		ColumnData::Float4(c) => c.get(i).map(|&v| v as f64),
31		ColumnData::Float8(c) => c.get(i).copied(),
32		ColumnData::Int {
33			container,
34			..
35		} => container.get(i).map(|v| v.0.to_f64().unwrap_or(0.0)),
36		ColumnData::Uint {
37			container,
38			..
39		} => container.get(i).map(|v| v.0.to_f64().unwrap_or(0.0)),
40		ColumnData::Decimal {
41			container,
42			..
43		} => container.get(i).map(|v| v.0.to_f64().unwrap_or(0.0)),
44		_ => None,
45	}
46}
47
48impl ScalarFunction for Clamp {
49	fn scalar(&self, ctx: ScalarFunctionContext) -> crate::error::ScalarFunctionResult<ColumnData> {
50		if let Some(result) = propagate_options(self, &ctx) {
51			return result;
52		}
53		let columns = ctx.columns;
54		let row_count = ctx.row_count;
55
56		if columns.len() != 3 {
57			return Err(ScalarFunctionError::ArityMismatch {
58				function: ctx.fragment.clone(),
59				expected: 3,
60				actual: columns.len(),
61			});
62		}
63
64		let val_col = columns.get(0).unwrap();
65		let min_col = columns.get(1).unwrap();
66		let max_col = columns.get(2).unwrap();
67
68		for (idx, col) in [(0, val_col), (1, min_col), (2, max_col)] {
69			if !col.data().get_type().is_number() {
70				return Err(ScalarFunctionError::InvalidArgumentType {
71					function: ctx.fragment.clone(),
72					argument_index: idx,
73					expected: vec![
74						Type::Int1,
75						Type::Int2,
76						Type::Int4,
77						Type::Int8,
78						Type::Int16,
79						Type::Uint1,
80						Type::Uint2,
81						Type::Uint4,
82						Type::Uint8,
83						Type::Uint16,
84						Type::Float4,
85						Type::Float8,
86						Type::Int,
87						Type::Uint,
88						Type::Decimal,
89					],
90					actual: col.data().get_type(),
91				});
92			}
93		}
94
95		let mut result = Vec::with_capacity(row_count);
96		let mut bitvec = Vec::with_capacity(row_count);
97
98		for i in 0..row_count {
99			match (
100				numeric_to_f64(val_col.data(), i),
101				numeric_to_f64(min_col.data(), i),
102				numeric_to_f64(max_col.data(), i),
103			) {
104				(Some(val), Some(min), Some(max)) => {
105					result.push(val.clamp(min, max));
106					bitvec.push(true);
107				}
108				_ => {
109					result.push(0.0);
110					bitvec.push(false);
111				}
112			}
113		}
114
115		Ok(ColumnData::float8_with_bitvec(result, bitvec))
116	}
117
118	fn return_type(&self, _input_types: &[Type]) -> Type {
119		Type::Float8
120	}
121}