1use std::any::Any;
2use std::fmt::Display;
3use std::hash::Hash;
4use std::sync::Arc;
5
6use vortex_array::compute::{LikeOptions, like};
7use vortex_array::{Array, ArrayRef};
8use vortex_dtype::DType;
9use vortex_error::VortexResult;
10
11use crate::{ExprRef, VortexExpr};
12
13#[derive(Debug, Eq, Hash)]
14#[allow(clippy::derived_hash_with_manual_eq)]
15pub struct Like {
16 child: ExprRef,
17 pattern: ExprRef,
18 negated: bool,
19 case_insensitive: bool,
20}
21
22impl Like {
23 pub fn new_expr(
24 child: ExprRef,
25 pattern: ExprRef,
26 negated: bool,
27 case_insensitive: bool,
28 ) -> ExprRef {
29 Arc::new(Self {
30 child,
31 pattern,
32 negated,
33 case_insensitive,
34 })
35 }
36
37 pub fn child(&self) -> &ExprRef {
38 &self.child
39 }
40
41 pub fn pattern(&self) -> &ExprRef {
42 &self.pattern
43 }
44
45 pub fn negated(&self) -> bool {
46 self.negated
47 }
48
49 pub fn case_insensitive(&self) -> bool {
50 self.case_insensitive
51 }
52}
53
54impl Display for Like {
55 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
56 write!(f, "{} LIKE {}", self.child(), self.pattern())
57 }
58}
59
60#[cfg(feature = "proto")]
61pub(crate) mod proto {
62 use vortex_error::{VortexResult, vortex_bail};
63 use vortex_proto::expr::kind;
64 use vortex_proto::expr::kind::Kind;
65
66 use crate::{ExprDeserialize, ExprRef, ExprSerializable, Id, Like};
67
68 pub(crate) struct LikeSerde;
69
70 impl Id for LikeSerde {
71 fn id(&self) -> &'static str {
72 "like"
73 }
74 }
75
76 impl ExprSerializable for Like {
77 fn id(&self) -> &'static str {
78 LikeSerde.id()
79 }
80
81 fn serialize_kind(&self) -> VortexResult<Kind> {
82 Ok(Kind::Like(kind::Like {
83 negated: self.negated,
84 case_insensitive: self.case_insensitive,
85 }))
86 }
87 }
88
89 impl ExprDeserialize for LikeSerde {
90 fn deserialize(&self, kind: &Kind, children: Vec<ExprRef>) -> VortexResult<ExprRef> {
91 let Kind::Like(like) = kind else {
92 vortex_bail!("wrong kind {:?}, want like", kind)
93 };
94
95 Ok(Like::new_expr(
96 children[0].clone(),
97 children[1].clone(),
98 like.negated,
99 like.case_insensitive,
100 ))
101 }
102 }
103}
104
105impl VortexExpr for Like {
106 fn as_any(&self) -> &dyn Any {
107 self
108 }
109
110 fn unchecked_evaluate(&self, batch: &dyn Array) -> VortexResult<ArrayRef> {
111 let child = self.child().evaluate(batch)?;
112 let pattern = self.pattern().evaluate(&child)?;
113 like(
114 &child,
115 &pattern,
116 LikeOptions {
117 negated: self.negated,
118 case_insensitive: self.case_insensitive,
119 },
120 )
121 }
122
123 fn children(&self) -> Vec<&ExprRef> {
124 vec![&self.child, &self.pattern]
125 }
126
127 fn replacing_children(self: Arc<Self>, children: Vec<ExprRef>) -> ExprRef {
128 assert_eq!(children.len(), 2);
129 Like::new_expr(
130 children[0].clone(),
131 children[1].clone(),
132 self.negated,
133 self.case_insensitive,
134 )
135 }
136
137 fn return_dtype(&self, scope_dtype: &DType) -> VortexResult<DType> {
138 let input = self.child().return_dtype(scope_dtype)?;
139 let pattern = self.pattern().return_dtype(scope_dtype)?;
140 Ok(DType::Bool(
141 (input.is_nullable() || pattern.is_nullable()).into(),
142 ))
143 }
144}
145
146impl PartialEq for Like {
147 fn eq(&self, other: &Like) -> bool {
148 other.case_insensitive == self.case_insensitive
149 && other.negated == self.negated
150 && other.pattern.eq(&self.pattern)
151 && other.child.eq(&self.child)
152 }
153}
154
155#[cfg(test)]
156mod tests {
157 use vortex_array::ToCanonical;
158 use vortex_array::arrays::BoolArray;
159 use vortex_dtype::{DType, Nullability};
160
161 use crate::{Like, ident, lit, not};
162
163 #[test]
164 fn invert_booleans() {
165 let not_expr = not(ident());
166 let bools = BoolArray::from_iter([false, true, false, false, true, true]);
167 assert_eq!(
168 not_expr
169 .evaluate(&bools)
170 .unwrap()
171 .to_bool()
172 .unwrap()
173 .boolean_buffer()
174 .iter()
175 .collect::<Vec<_>>(),
176 vec![true, false, true, true, false, false]
177 );
178 }
179
180 #[test]
181 fn dtype() {
182 let dtype = DType::Utf8(Nullability::NonNullable);
183 let like_expr = Like::new_expr(ident(), lit("%test%"), false, false);
184 assert_eq!(
185 like_expr.return_dtype(&dtype).unwrap(),
186 DType::Bool(Nullability::NonNullable)
187 );
188 }
189}