reifydb_engine/function/text/
length.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;
5
6use crate::function::{ScalarFunction, ScalarFunctionContext};
7
8pub struct TextLength;
9
10impl TextLength {
11	pub fn new() -> Self {
12		Self
13	}
14}
15
16impl ScalarFunction for TextLength {
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::int4(Vec::<i32>::new()));
23		}
24
25		let column = columns.get(0).unwrap();
26
27		match &column.data() {
28			ColumnData::Utf8 {
29				container,
30				..
31			} => {
32				let mut result = Vec::with_capacity(row_count);
33
34				for i in 0..row_count {
35					if container.is_defined(i) {
36						let text = &container[i];
37						// Return byte length, not character count
38						result.push(text.len() as i32);
39					} else {
40						result.push(0);
41					}
42				}
43
44				Ok(ColumnData::int4(result))
45			}
46			_ => unimplemented!("TextLength only supports text input"),
47		}
48	}
49}