Skip to main content

reifydb_routine/function/text/
upper.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4use reifydb_core::value::column::{ColumnWithName, buffer::ColumnBuffer, columns::Columns};
5use reifydb_type::value::{container::utf8::Utf8Container, r#type::Type};
6
7use crate::routine::{Function, FunctionKind, Routine, RoutineInfo, context::FunctionContext, error::RoutineError};
8
9pub struct TextUpper {
10	info: RoutineInfo,
11}
12
13impl Default for TextUpper {
14	fn default() -> Self {
15		Self::new()
16	}
17}
18
19impl TextUpper {
20	pub fn new() -> Self {
21		Self {
22			info: RoutineInfo::new("text::upper"),
23		}
24	}
25}
26
27impl<'a> Routine<FunctionContext<'a>> for TextUpper {
28	fn info(&self) -> &RoutineInfo {
29		&self.info
30	}
31
32	fn return_type(&self, _input_types: &[Type]) -> Type {
33		Type::Utf8
34	}
35
36	fn execute(&self, ctx: &mut FunctionContext<'a>, args: &Columns) -> Result<Columns, RoutineError> {
37		if args.len() != 1 {
38			return Err(RoutineError::FunctionArityMismatch {
39				function: ctx.fragment.clone(),
40				expected: 1,
41				actual: args.len(),
42			});
43		}
44
45		let column = &args[0];
46		let (data, bitvec) = column.unwrap_option();
47		let row_count = data.len();
48
49		match data {
50			ColumnBuffer::Utf8 {
51				container,
52				max_bytes,
53			} => {
54				let mut result_data = Vec::with_capacity(container.len());
55
56				for i in 0..row_count {
57					if container.is_defined(i) {
58						let original_str = container.get(i).unwrap();
59						let upper_str = original_str.to_uppercase();
60						result_data.push(upper_str);
61					} else {
62						result_data.push(String::new());
63					}
64				}
65
66				let result_col_data = ColumnBuffer::Utf8 {
67					container: Utf8Container::new(result_data),
68					max_bytes: *max_bytes,
69				};
70				let final_data = match bitvec {
71					Some(bv) => ColumnBuffer::Option {
72						inner: Box::new(result_col_data),
73						bitvec: bv.clone(),
74					},
75					None => result_col_data,
76				};
77				Ok(Columns::new(vec![ColumnWithName::new(ctx.fragment.clone(), final_data)]))
78			}
79			other => Err(RoutineError::FunctionInvalidArgumentType {
80				function: ctx.fragment.clone(),
81				argument_index: 0,
82				expected: vec![Type::Utf8],
83				actual: other.get_type(),
84			}),
85		}
86	}
87}
88
89impl Function for TextUpper {
90	fn kinds(&self) -> &[FunctionKind] {
91		&[FunctionKind::Scalar]
92	}
93}