reifydb_routine/function/text/
index_of.rs1use reifydb_core::value::column::{ColumnWithName, buffer::ColumnBuffer, columns::Columns};
5use reifydb_type::value::r#type::Type;
6
7use crate::routine::{Function, FunctionKind, Routine, RoutineInfo, context::FunctionContext, error::RoutineError};
8
9pub struct TextIndexOf {
10 info: RoutineInfo,
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: RoutineInfo::new("text::index_of"),
23 }
24 }
25}
26
27impl<'a> Routine<FunctionContext<'a>> for TextIndexOf {
28 fn info(&self) -> &RoutineInfo {
29 &self.info
30 }
31
32 fn return_type(&self, _input_types: &[Type]) -> Type {
33 Type::Int4
34 }
35
36 fn execute(&self, ctx: &mut FunctionContext<'a>, args: &Columns) -> Result<Columns, RoutineError> {
37 if args.len() != 2 {
38 return Err(RoutineError::FunctionArityMismatch {
39 function: ctx.fragment.clone(),
40 expected: 2,
41 actual: args.len(),
42 });
43 }
44
45 let str_col = &args[0];
46 let substr_col = &args[1];
47
48 let (str_data, str_bv) = str_col.unwrap_option();
49 let (substr_data, substr_bv) = substr_col.unwrap_option();
50 let row_count = str_data.len();
51
52 match (str_data, substr_data) {
53 (
54 ColumnBuffer::Utf8 {
55 container: str_container,
56 ..
57 },
58 ColumnBuffer::Utf8 {
59 container: substr_container,
60 ..
61 },
62 ) => {
63 let mut result_data = Vec::with_capacity(row_count);
64 let mut result_bitvec = Vec::with_capacity(row_count);
65
66 for i in 0..row_count {
67 if str_container.is_defined(i) && substr_container.is_defined(i) {
68 let s = str_container.get(i).unwrap();
69 let substr = substr_container.get(i).unwrap();
70 let index = s
71 .find(substr)
72 .map(|pos| s[..pos].chars().count() as i32)
73 .unwrap_or(-1);
74 result_data.push(index);
75 result_bitvec.push(true);
76 } else {
77 result_data.push(0);
78 result_bitvec.push(false);
79 }
80 }
81
82 let result_col_data = ColumnBuffer::int4_with_bitvec(result_data, result_bitvec);
83
84 let combined_bv = match (str_bv, substr_bv) {
85 (Some(b), Some(e)) => Some(b.and(e)),
86 (Some(b), None) => Some(b.clone()),
87 (None, Some(e)) => Some(e.clone()),
88 (None, None) => None,
89 };
90
91 let final_data = match combined_bv {
92 Some(bv) => ColumnBuffer::Option {
93 inner: Box::new(result_col_data),
94 bitvec: bv,
95 },
96 None => result_col_data,
97 };
98 Ok(Columns::new(vec![ColumnWithName::new(ctx.fragment.clone(), final_data)]))
99 }
100 (
101 ColumnBuffer::Utf8 {
102 ..
103 },
104 other,
105 ) => Err(RoutineError::FunctionInvalidArgumentType {
106 function: ctx.fragment.clone(),
107 argument_index: 1,
108 expected: vec![Type::Utf8],
109 actual: other.get_type(),
110 }),
111 (other, _) => Err(RoutineError::FunctionInvalidArgumentType {
112 function: ctx.fragment.clone(),
113 argument_index: 0,
114 expected: vec![Type::Utf8],
115 actual: other.get_type(),
116 }),
117 }
118 }
119}
120
121impl Function for TextIndexOf {
122 fn kinds(&self) -> &[FunctionKind] {
123 &[FunctionKind::Scalar]
124 }
125}