reifydb_function/text/
trim_start.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 TextTrimStart;
10
11impl TextTrimStart {
12 pub fn new() -> Self {
13 Self
14 }
15}
16
17impl ScalarFunction for TextTrimStart {
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 {
27 return Err(ScalarFunctionError::ArityMismatch {
28 function: ctx.fragment.clone(),
29 expected: 1,
30 actual: columns.len(),
31 });
32 }
33
34 let column = columns.get(0).unwrap();
35
36 match &column.data() {
37 ColumnData::Utf8 {
38 container,
39 max_bytes,
40 } => {
41 let mut result_data = Vec::with_capacity(row_count);
42
43 for i in 0..row_count {
44 if container.is_defined(i) {
45 let original_str = &container[i];
46 let trimmed_str = original_str.trim_start();
47 result_data.push(trimmed_str.to_string());
48 } else {
49 result_data.push(String::new());
50 }
51 }
52
53 Ok(ColumnData::Utf8 {
54 container: Utf8Container::new(result_data),
55 max_bytes: *max_bytes,
56 })
57 }
58 other => Err(ScalarFunctionError::InvalidArgumentType {
59 function: ctx.fragment.clone(),
60 argument_index: 0,
61 expected: vec![Type::Utf8],
62 actual: other.get_type(),
63 }),
64 }
65 }
66
67 fn return_type(&self, _input_types: &[Type]) -> Type {
68 Type::Utf8
69 }
70}