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| {
73 s[..pos].chars().count() as i32
75 })
76 .unwrap_or(-1);
77 result_data.push(index);
78 result_bitvec.push(true);
79 } else {
80 result_data.push(0);
81 result_bitvec.push(false);
82 }
83 }
84
85 let result_col_data = ColumnBuffer::int4_with_bitvec(result_data, result_bitvec);
86
87 let combined_bv = match (str_bv, substr_bv) {
88 (Some(b), Some(e)) => Some(b.and(e)),
89 (Some(b), None) => Some(b.clone()),
90 (None, Some(e)) => Some(e.clone()),
91 (None, None) => None,
92 };
93
94 let final_data = match combined_bv {
95 Some(bv) => ColumnBuffer::Option {
96 inner: Box::new(result_col_data),
97 bitvec: bv,
98 },
99 None => result_col_data,
100 };
101 Ok(Columns::new(vec![ColumnWithName::new(ctx.fragment.clone(), final_data)]))
102 }
103 (
104 ColumnBuffer::Utf8 {
105 ..
106 },
107 other,
108 ) => Err(RoutineError::FunctionInvalidArgumentType {
109 function: ctx.fragment.clone(),
110 argument_index: 1,
111 expected: vec![Type::Utf8],
112 actual: other.get_type(),
113 }),
114 (other, _) => Err(RoutineError::FunctionInvalidArgumentType {
115 function: ctx.fragment.clone(),
116 argument_index: 0,
117 expected: vec![Type::Utf8],
118 actual: other.get_type(),
119 }),
120 }
121 }
122}
123
124impl Function for TextIndexOf {
125 fn kinds(&self) -> &[FunctionKind] {
126 &[FunctionKind::Scalar]
127 }
128}