Skip to main content

reifydb_routine/function/text/
char.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::{constraint::bytes::MaxBytes, container::utf8::Utf8Container, r#type::Type};
6
7use crate::routine::{Function, FunctionKind, Routine, RoutineInfo, context::FunctionContext, error::RoutineError};
8
9pub struct TextChar {
10	info: RoutineInfo,
11}
12
13impl Default for TextChar {
14	fn default() -> Self {
15		Self::new()
16	}
17}
18
19impl TextChar {
20	pub fn new() -> Self {
21		Self {
22			info: RoutineInfo::new("text::char"),
23		}
24	}
25}
26
27impl<'a> Routine<FunctionContext<'a>> for TextChar {
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		let result_data = match data {
50			ColumnBuffer::Int1(c) => {
51				convert_to_char(row_count, c.data().len(), |i| c.get(i).map(|&v| v as u32))
52			}
53			ColumnBuffer::Int2(c) => {
54				convert_to_char(row_count, c.data().len(), |i| c.get(i).map(|&v| v as u32))
55			}
56			ColumnBuffer::Int4(c) => {
57				convert_to_char(row_count, c.data().len(), |i| c.get(i).map(|&v| v as u32))
58			}
59			ColumnBuffer::Int8(c) => {
60				convert_to_char(row_count, c.data().len(), |i| c.get(i).map(|&v| v as u32))
61			}
62			ColumnBuffer::Uint1(c) => {
63				convert_to_char(row_count, c.data().len(), |i| c.get(i).map(|&v| v as u32))
64			}
65			ColumnBuffer::Uint2(c) => {
66				convert_to_char(row_count, c.data().len(), |i| c.get(i).map(|&v| v as u32))
67			}
68			ColumnBuffer::Uint4(c) => convert_to_char(row_count, c.data().len(), |i| c.get(i).copied()),
69			other => {
70				return Err(RoutineError::FunctionInvalidArgumentType {
71					function: ctx.fragment.clone(),
72					argument_index: 0,
73					expected: vec![Type::Int1, Type::Int2, Type::Int4, Type::Int8],
74					actual: other.get_type(),
75				});
76			}
77		};
78
79		let final_data = match bitvec {
80			Some(bv) => ColumnBuffer::Option {
81				inner: Box::new(result_data),
82				bitvec: bv.clone(),
83			},
84			None => result_data,
85		};
86		Ok(Columns::new(vec![ColumnWithName::new(ctx.fragment.clone(), final_data)]))
87	}
88}
89
90impl Function for TextChar {
91	fn kinds(&self) -> &[FunctionKind] {
92		&[FunctionKind::Scalar]
93	}
94}
95
96fn convert_to_char<F>(row_count: usize, _capacity: usize, get_value: F) -> ColumnBuffer
97where
98	F: Fn(usize) -> Option<u32>,
99{
100	let mut result_data = Vec::with_capacity(row_count);
101
102	for i in 0..row_count {
103		match get_value(i) {
104			Some(code_point) => {
105				if let Some(ch) = char::from_u32(code_point) {
106					result_data.push(ch.to_string());
107				} else {
108					result_data.push(String::new());
109				}
110			}
111			None => {
112				result_data.push(String::new());
113			}
114		}
115	}
116
117	ColumnBuffer::Utf8 {
118		container: Utf8Container::new(result_data),
119		max_bytes: MaxBytes::MAX,
120	}
121}