Skip to main content

reifydb_function/text/
upper.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2// Copyright (c) 2025 ReifyDB
3
4use reifydb_core::value::column::data::ColumnData;
5use reifydb_type::value::{container::utf8::Utf8Container, r#type::Type};
6
7use crate::{
8	ScalarFunction, ScalarFunctionContext,
9	error::{ScalarFunctionError, ScalarFunctionResult},
10	propagate_options,
11};
12
13pub struct TextUpper;
14
15impl TextUpper {
16	pub fn new() -> Self {
17		Self
18	}
19}
20
21impl ScalarFunction for TextUpper {
22	fn scalar(&self, ctx: ScalarFunctionContext) -> ScalarFunctionResult<ColumnData> {
23		if let Some(result) = propagate_options(self, &ctx) {
24			return result;
25		}
26
27		let columns = ctx.columns;
28		let row_count = ctx.row_count;
29
30		// Validate exactly 1 argument
31		if columns.len() != 1 {
32			return Err(ScalarFunctionError::ArityMismatch {
33				function: ctx.fragment.clone(),
34				expected: 1,
35				actual: columns.len(),
36			});
37		}
38
39		let column = columns.get(0).unwrap();
40
41		match &column.data() {
42			ColumnData::Utf8 {
43				container,
44				max_bytes,
45			} => {
46				let mut result_data = Vec::with_capacity(container.data().len());
47
48				for i in 0..row_count {
49					if container.is_defined(i) {
50						let original_str = &container[i];
51						let upper_str = original_str.to_uppercase();
52						result_data.push(upper_str);
53					} else {
54						result_data.push(String::new());
55					}
56				}
57
58				Ok(ColumnData::Utf8 {
59					container: Utf8Container::new(result_data),
60					max_bytes: *max_bytes,
61				})
62			}
63			other => Err(ScalarFunctionError::InvalidArgumentType {
64				function: ctx.fragment.clone(),
65				argument_index: 0,
66				expected: vec![Type::Utf8],
67				actual: other.get_type(),
68			}),
69		}
70	}
71
72	fn return_type(&self, _input_types: &[Type]) -> Type {
73		Type::Utf8
74	}
75}