reifydb_engine/function/text/
trim.rs

1// Copyright (c) reifydb.com 2025
2// This file is licensed under the AGPL-3.0-or-later, see license.md file
3
4use reifydb_core::value::{column::ColumnData, container::Utf8Container};
5
6use crate::function::{ScalarFunction, ScalarFunctionContext};
7
8pub struct TextTrim;
9
10impl TextTrim {
11	pub fn new() -> Self {
12		Self
13	}
14}
15
16impl ScalarFunction for TextTrim {
17	fn scalar(&self, ctx: ScalarFunctionContext) -> crate::Result<ColumnData> {
18		let columns = ctx.columns;
19		let row_count = ctx.row_count;
20
21		if columns.is_empty() {
22			return Ok(ColumnData::utf8(Vec::<String>::new()));
23		}
24
25		let column = columns.get(0).unwrap();
26
27		match &column.data() {
28			ColumnData::Utf8 {
29				container,
30				max_bytes,
31			} => {
32				let mut result_data = Vec::with_capacity(container.data().len());
33				let mut result_bitvec = Vec::with_capacity(row_count);
34
35				for i in 0..row_count {
36					if container.is_defined(i) {
37						let original_str = &container[i];
38						let trimmed_str = original_str.trim();
39						result_data.push(trimmed_str.to_string());
40						result_bitvec.push(true);
41					} else {
42						result_data.push(String::new());
43						result_bitvec.push(false);
44					}
45				}
46
47				Ok(ColumnData::Utf8 {
48					container: Utf8Container::new(result_data, result_bitvec.into()),
49					max_bytes: *max_bytes,
50				})
51			}
52			_ => unimplemented!("text::trim only supports text input"),
53		}
54	}
55}