Skip to main content

reifydb_routine/function/text/
contains.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4use 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 TextContains {
10	info: RoutineInfo,
11}
12
13impl Default for TextContains {
14	fn default() -> Self {
15		Self::new()
16	}
17}
18
19impl TextContains {
20	pub fn new() -> Self {
21		Self {
22			info: RoutineInfo::new("text::contains"),
23		}
24	}
25}
26
27impl<'a> Routine<FunctionContext<'a>> for TextContains {
28	fn info(&self) -> &RoutineInfo {
29		&self.info
30	}
31
32	fn return_type(&self, _input_types: &[Type]) -> Type {
33		Type::Boolean
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						result_data.push(s.contains(substr));
71						result_bitvec.push(true);
72					} else {
73						result_data.push(false);
74						result_bitvec.push(false);
75					}
76				}
77
78				let result_col_data = ColumnBuffer::bool_with_bitvec(result_data, result_bitvec);
79
80				let combined_bv = match (str_bv, substr_bv) {
81					(Some(b), Some(e)) => Some(b.and(e)),
82					(Some(b), None) => Some(b.clone()),
83					(None, Some(e)) => Some(e.clone()),
84					(None, None) => None,
85				};
86
87				let final_data = match combined_bv {
88					Some(bv) => ColumnBuffer::Option {
89						inner: Box::new(result_col_data),
90						bitvec: bv,
91					},
92					None => result_col_data,
93				};
94				Ok(Columns::new(vec![ColumnWithName::new(ctx.fragment.clone(), final_data)]))
95			}
96			(
97				ColumnBuffer::Utf8 {
98					..
99				},
100				other,
101			) => Err(RoutineError::FunctionInvalidArgumentType {
102				function: ctx.fragment.clone(),
103				argument_index: 1,
104				expected: vec![Type::Utf8],
105				actual: other.get_type(),
106			}),
107			(other, _) => Err(RoutineError::FunctionInvalidArgumentType {
108				function: ctx.fragment.clone(),
109				argument_index: 0,
110				expected: vec![Type::Utf8],
111				actual: other.get_type(),
112			}),
113		}
114	}
115}
116
117impl Function for TextContains {
118	fn kinds(&self) -> &[FunctionKind] {
119		&[FunctionKind::Scalar]
120	}
121}