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