vortex_expr/
like.rs

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
60impl VortexExpr for Like {
61    fn as_any(&self) -> &dyn Any {
62        self
63    }
64
65    fn unchecked_evaluate(&self, batch: &dyn Array) -> VortexResult<ArrayRef> {
66        let child = self.child().evaluate(batch)?;
67        let pattern = self.pattern().evaluate(&child)?;
68        like(
69            &child,
70            &pattern,
71            LikeOptions {
72                negated: self.negated,
73                case_insensitive: self.case_insensitive,
74            },
75        )
76    }
77
78    fn children(&self) -> Vec<&ExprRef> {
79        vec![&self.child, &self.pattern]
80    }
81
82    fn replacing_children(self: Arc<Self>, children: Vec<ExprRef>) -> ExprRef {
83        assert_eq!(children.len(), 2);
84        Like::new_expr(
85            children[0].clone(),
86            children[1].clone(),
87            self.negated,
88            self.case_insensitive,
89        )
90    }
91
92    fn return_dtype(&self, scope_dtype: &DType) -> VortexResult<DType> {
93        let input = self.child().return_dtype(scope_dtype)?;
94        let pattern = self.pattern().return_dtype(scope_dtype)?;
95        Ok(DType::Bool(
96            (input.is_nullable() || pattern.is_nullable()).into(),
97        ))
98    }
99}
100
101impl PartialEq for Like {
102    fn eq(&self, other: &Like) -> bool {
103        other.case_insensitive == self.case_insensitive
104            && other.negated == self.negated
105            && other.pattern.eq(&self.pattern)
106            && other.child.eq(&self.child)
107    }
108}
109
110#[cfg(test)]
111mod tests {
112    use vortex_array::ToCanonical;
113    use vortex_array::arrays::BoolArray;
114    use vortex_dtype::{DType, Nullability};
115
116    use crate::{Like, ident, lit, not};
117
118    #[test]
119    fn invert_booleans() {
120        let not_expr = not(ident());
121        let bools = BoolArray::from_iter([false, true, false, false, true, true]);
122        assert_eq!(
123            not_expr
124                .evaluate(&bools)
125                .unwrap()
126                .to_bool()
127                .unwrap()
128                .boolean_buffer()
129                .iter()
130                .collect::<Vec<_>>(),
131            vec![true, false, true, true, false, false]
132        );
133    }
134
135    #[test]
136    fn dtype() {
137        let dtype = DType::Utf8(Nullability::NonNullable);
138        let like_expr = Like::new_expr(ident(), lit("%test%"), false, false);
139        assert_eq!(
140            like_expr.return_dtype(&dtype).unwrap(),
141            DType::Bool(Nullability::NonNullable)
142        );
143    }
144}