Skip to main content

reifydb_routine/function/text/
index_of.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4use reifydb_core::value::column::{Column, columns::Columns, data::ColumnData};
5use reifydb_type::value::r#type::Type;
6
7use crate::function::{Function, FunctionCapability, FunctionContext, FunctionInfo, error::FunctionError};
8
9pub struct TextIndexOf {
10	info: FunctionInfo,
11}
12
13impl Default for TextIndexOf {
14	fn default() -> Self {
15		Self::new()
16	}
17}
18
19impl TextIndexOf {
20	pub fn new() -> Self {
21		Self {
22			info: FunctionInfo::new("text::index_of"),
23		}
24	}
25}
26
27impl Function for TextIndexOf {
28	fn info(&self) -> &FunctionInfo {
29		&self.info
30	}
31
32	fn capabilities(&self) -> &[FunctionCapability] {
33		&[FunctionCapability::Scalar]
34	}
35
36	fn return_type(&self, _input_types: &[Type]) -> Type {
37		Type::Int4
38	}
39
40	fn execute(&self, ctx: &FunctionContext, args: &Columns) -> Result<Columns, FunctionError> {
41		if args.len() != 2 {
42			return Err(FunctionError::ArityMismatch {
43				function: ctx.fragment.clone(),
44				expected: 2,
45				actual: args.len(),
46			});
47		}
48
49		let str_col = &args[0];
50		let substr_col = &args[1];
51
52		let (str_data, str_bv) = str_col.data().unwrap_option();
53		let (substr_data, substr_bv) = substr_col.data().unwrap_option();
54		let row_count = str_data.len();
55
56		match (str_data, substr_data) {
57			(
58				ColumnData::Utf8 {
59					container: str_container,
60					..
61				},
62				ColumnData::Utf8 {
63					container: substr_container,
64					..
65				},
66			) => {
67				let mut result_data = Vec::with_capacity(row_count);
68				let mut result_bitvec = Vec::with_capacity(row_count);
69
70				for i in 0..row_count {
71					if str_container.is_defined(i) && substr_container.is_defined(i) {
72						let s = &str_container[i];
73						let substr = &substr_container[i];
74						let index = s
75							.find(substr.as_str())
76							.map(|pos| {
77								// Convert byte position to character position
78								s[..pos].chars().count() as i32
79							})
80							.unwrap_or(-1);
81						result_data.push(index);
82						result_bitvec.push(true);
83					} else {
84						result_data.push(0);
85						result_bitvec.push(false);
86					}
87				}
88
89				let result_col_data = ColumnData::int4_with_bitvec(result_data, result_bitvec);
90
91				let combined_bv = match (str_bv, substr_bv) {
92					(Some(b), Some(e)) => Some(b.and(e)),
93					(Some(b), None) => Some(b.clone()),
94					(None, Some(e)) => Some(e.clone()),
95					(None, None) => None,
96				};
97
98				let final_data = match combined_bv {
99					Some(bv) => ColumnData::Option {
100						inner: Box::new(result_col_data),
101						bitvec: bv,
102					},
103					None => result_col_data,
104				};
105				Ok(Columns::new(vec![Column::new(ctx.fragment.clone(), final_data)]))
106			}
107			(
108				ColumnData::Utf8 {
109					..
110				},
111				other,
112			) => Err(FunctionError::InvalidArgumentType {
113				function: ctx.fragment.clone(),
114				argument_index: 1,
115				expected: vec![Type::Utf8],
116				actual: other.get_type(),
117			}),
118			(other, _) => Err(FunctionError::InvalidArgumentType {
119				function: ctx.fragment.clone(),
120				argument_index: 0,
121				expected: vec![Type::Utf8],
122				actual: other.get_type(),
123			}),
124		}
125	}
126}