reifydb_function/text/
concat.rs1use reifydb_core::value::column::data::ColumnData;
5use reifydb_type::value::{constraint::bytes::MaxBytes, container::utf8::Utf8Container, r#type::Type};
6
7use crate::{
8 ScalarFunction, ScalarFunctionContext,
9 error::{ScalarFunctionError, ScalarFunctionResult},
10 propagate_options,
11};
12
13pub struct TextConcat;
14
15impl TextConcat {
16 pub fn new() -> Self {
17 Self
18 }
19}
20
21impl ScalarFunction for TextConcat {
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 if columns.len() < 2 {
31 return Err(ScalarFunctionError::ArityMismatch {
32 function: ctx.fragment.clone(),
33 expected: 2,
34 actual: columns.len(),
35 });
36 }
37
38 for (idx, col) in columns.iter().enumerate() {
40 match col.data() {
41 ColumnData::Utf8 {
42 ..
43 } => {}
44 other => {
45 return Err(ScalarFunctionError::InvalidArgumentType {
46 function: ctx.fragment.clone(),
47 argument_index: idx,
48 expected: vec![Type::Utf8],
49 actual: other.get_type(),
50 });
51 }
52 }
53 }
54
55 let mut result_data = Vec::with_capacity(row_count);
56
57 for i in 0..row_count {
58 let mut all_defined = true;
59 let mut concatenated = String::new();
60
61 for col in columns.iter() {
62 if let ColumnData::Utf8 {
63 container,
64 ..
65 } = col.data()
66 {
67 if container.is_defined(i) {
68 concatenated.push_str(&container[i]);
69 } else {
70 all_defined = false;
71 break;
72 }
73 }
74 }
75
76 if all_defined {
77 result_data.push(concatenated);
78 } else {
79 result_data.push(String::new());
80 }
81 }
82
83 Ok(ColumnData::Utf8 {
84 container: Utf8Container::new(result_data),
85 max_bytes: MaxBytes::MAX,
86 })
87 }
88
89 fn return_type(&self, _input_types: &[Type]) -> Type {
90 Type::Utf8
91 }
92}