Skip to main content

reifydb_function/text/
ascii.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2// Copyright (c) 2025 ReifyDB
3
4use reifydb_core::value::column::data::ColumnData;
5use reifydb_type::value::r#type::Type;
6
7use crate::{ScalarFunction, ScalarFunctionContext, error::ScalarFunctionError, propagate_options};
8
9pub struct TextAscii;
10
11impl TextAscii {
12	pub fn new() -> Self {
13		Self
14	}
15}
16
17impl ScalarFunction for TextAscii {
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				..
40			} => {
41				let mut result_data = Vec::with_capacity(row_count);
42				let mut result_bitvec = Vec::with_capacity(row_count);
43
44				for i in 0..row_count {
45					if container.is_defined(i) {
46						let s = &container[i];
47						let code_point = s.chars().next().map(|c| c as i32).unwrap_or(0);
48						result_data.push(code_point);
49						result_bitvec.push(true);
50					} else {
51						result_data.push(0);
52						result_bitvec.push(false);
53					}
54				}
55
56				Ok(ColumnData::int4_with_bitvec(result_data, result_bitvec))
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::Int4
69	}
70}