Skip to main content

reifydb_function/math/scalar/
ceil.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::{container::number::NumberContainer, decimal::Decimal, r#type::Type};
7
8use crate::{
9	ScalarFunction, ScalarFunctionContext,
10	error::{ScalarFunctionError, ScalarFunctionResult},
11	propagate_options,
12};
13
14pub struct Ceil;
15
16impl Ceil {
17	pub fn new() -> Self {
18		Self
19	}
20}
21
22impl ScalarFunction for Ceil {
23	fn scalar(&self, ctx: ScalarFunctionContext) -> ScalarFunctionResult<ColumnData> {
24		if let Some(result) = propagate_options(self, &ctx) {
25			return result;
26		}
27		let columns = ctx.columns;
28		let row_count = ctx.row_count;
29
30		if columns.len() != 1 {
31			return Err(ScalarFunctionError::ArityMismatch {
32				function: ctx.fragment.clone(),
33				expected: 1,
34				actual: columns.len(),
35			});
36		}
37
38		let column = columns.get(0).unwrap();
39
40		match column.data() {
41			ColumnData::Float4(container) => {
42				let mut data = Vec::with_capacity(row_count);
43				let mut bitvec = Vec::with_capacity(row_count);
44				for i in 0..row_count {
45					if let Some(&value) = container.get(i) {
46						data.push(value.ceil());
47						bitvec.push(true);
48					} else {
49						data.push(0.0);
50						bitvec.push(false);
51					}
52				}
53				Ok(ColumnData::float4_with_bitvec(data, bitvec))
54			}
55			ColumnData::Float8(container) => {
56				let mut data = Vec::with_capacity(row_count);
57				let mut bitvec = Vec::with_capacity(row_count);
58				for i in 0..row_count {
59					if let Some(&value) = container.get(i) {
60						data.push(value.ceil());
61						bitvec.push(true);
62					} else {
63						data.push(0.0);
64						bitvec.push(false);
65					}
66				}
67				Ok(ColumnData::float8_with_bitvec(data, bitvec))
68			}
69			ColumnData::Decimal {
70				container,
71				precision,
72				scale,
73			} => {
74				let mut data = Vec::with_capacity(row_count);
75				for i in 0..row_count {
76					if let Some(value) = container.get(i) {
77						let f = value.0.to_f64().unwrap_or(0.0);
78						data.push(Decimal::from(f.ceil()));
79					} else {
80						data.push(Decimal::default());
81					}
82				}
83				Ok(ColumnData::Decimal {
84					container: NumberContainer::new(data),
85					precision: *precision,
86					scale: *scale,
87				})
88			}
89			other if other.get_type().is_number() => Ok(column.data().clone()),
90			other => Err(ScalarFunctionError::InvalidArgumentType {
91				function: ctx.fragment.clone(),
92				argument_index: 0,
93				expected: vec![
94					Type::Int1,
95					Type::Int2,
96					Type::Int4,
97					Type::Int8,
98					Type::Int16,
99					Type::Uint1,
100					Type::Uint2,
101					Type::Uint4,
102					Type::Uint8,
103					Type::Uint16,
104					Type::Float4,
105					Type::Float8,
106					Type::Int,
107					Type::Uint,
108					Type::Decimal,
109				],
110				actual: other.get_type(),
111			}),
112		}
113	}
114
115	fn return_type(&self, input_types: &[Type]) -> Type {
116		input_types[0].clone()
117	}
118}