• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

vortex-data / vortex / 16728097825

04 Aug 2025 04:00PM UTC coverage: 48.355% (-35.1%) from 83.429%
16728097825

Pull #4108

github

web-flow
Merge 1b2d27fd8 into 649ba9576
Pull Request #4108: perf[vortex-array]: use all_valid instead of `invalid_count() == 0`

1 of 1 new or added line in 1 file covered. (100.0%)

11596 existing lines in 378 files now uncovered.

18635 of 38538 relevant lines covered (48.35%)

151786.4 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

61.18
/vortex-expr/src/exprs/list_contains.rs
1
// SPDX-License-Identifier: Apache-2.0
2
// SPDX-FileCopyrightText: Copyright the Vortex contributors
3

4
use std::fmt::{Debug, Display, Formatter};
5
use std::hash::Hash;
6

7
use vortex_array::compute::list_contains as compute_list_contains;
8
use vortex_array::{ArrayRef, DeserializeMetadata, EmptyMetadata};
9
use vortex_dtype::DType;
10
use vortex_error::{VortexResult, vortex_bail};
11

12
use crate::{
13
    AnalysisExpr, ExprEncodingRef, ExprId, ExprRef, IntoExpr, LiteralVTable, Scope, StatsCatalog,
14
    VTable, and, gt, lit, lt, or, vtable,
15
};
16

17
vtable!(ListContains);
18

19
#[allow(clippy::derived_hash_with_manual_eq)]
20
#[derive(Debug, Clone, Hash, Eq)]
21
pub struct ListContainsExpr {
22
    list: ExprRef,
23
    value: ExprRef,
24
}
25

26
impl PartialEq for ListContainsExpr {
27
    fn eq(&self, other: &Self) -> bool {
558✔
28
        self.list.eq(&other.list) && self.value.eq(&other.value)
558✔
29
    }
558✔
30
}
31

32
pub struct ListContainsExprEncoding;
33

34
impl VTable for ListContainsVTable {
35
    type Expr = ListContainsExpr;
36
    type Encoding = ListContainsExprEncoding;
37
    type Metadata = EmptyMetadata;
38

39
    fn id(_encoding: &Self::Encoding) -> ExprId {
22✔
40
        ExprId::new_ref("list_contains")
22✔
41
    }
22✔
42

UNCOV
43
    fn encoding(_expr: &Self::Expr) -> ExprEncodingRef {
×
UNCOV
44
        ExprEncodingRef::new_ref(ListContainsExprEncoding.as_ref())
×
UNCOV
45
    }
×
46

UNCOV
47
    fn metadata(_expr: &Self::Expr) -> Option<Self::Metadata> {
×
UNCOV
48
        Some(EmptyMetadata)
×
UNCOV
49
    }
×
50

51
    fn children(expr: &Self::Expr) -> Vec<&ExprRef> {
962✔
52
        vec![&expr.list, &expr.value]
962✔
53
    }
962✔
54

55
    fn with_children(_expr: &Self::Expr, children: Vec<ExprRef>) -> VortexResult<Self::Expr> {
78✔
56
        Ok(ListContainsExpr::new(
78✔
57
            children[0].clone(),
78✔
58
            children[1].clone(),
78✔
59
        ))
78✔
60
    }
78✔
61

UNCOV
62
    fn build(
×
UNCOV
63
        _encoding: &Self::Encoding,
×
UNCOV
64
        _metadata: &<Self::Metadata as DeserializeMetadata>::Output,
×
UNCOV
65
        children: Vec<ExprRef>,
×
UNCOV
66
    ) -> VortexResult<Self::Expr> {
×
UNCOV
67
        if children.len() != 2 {
×
68
            vortex_bail!(
×
69
                "ListContains expression must have exactly 2 children, got {}",
×
70
                children.len()
×
71
            );
UNCOV
72
        }
×
UNCOV
73
        Ok(ListContainsExpr::new(
×
UNCOV
74
            children[0].clone(),
×
UNCOV
75
            children[1].clone(),
×
UNCOV
76
        ))
×
UNCOV
77
    }
×
78

79
    fn evaluate(expr: &Self::Expr, scope: &Scope) -> VortexResult<ArrayRef> {
58✔
80
        compute_list_contains(
58✔
81
            expr.list.evaluate(scope)?.as_ref(),
58✔
82
            expr.value.evaluate(scope)?.as_ref(),
58✔
83
        )
84
    }
58✔
85

86
    fn return_dtype(expr: &Self::Expr, scope: &DType) -> VortexResult<DType> {
110✔
87
        Ok(DType::Bool(
88
            expr.list.return_dtype(scope)?.nullability()
110✔
89
                | expr.value.return_dtype(scope)?.nullability(),
110✔
90
        ))
91
    }
110✔
92
}
93

94
impl ListContainsExpr {
95
    pub fn new(list: ExprRef, value: ExprRef) -> Self {
104✔
96
        Self { list, value }
104✔
97
    }
104✔
98

99
    pub fn new_expr(list: ExprRef, value: ExprRef) -> ExprRef {
×
100
        Self::new(list, value).into_expr()
×
101
    }
×
102

103
    pub fn value(&self) -> &ExprRef {
×
104
        &self.value
×
105
    }
×
106
}
107

108
pub fn list_contains(list: ExprRef, value: ExprRef) -> ExprRef {
26✔
109
    ListContainsExpr::new(list, value).into_expr()
26✔
110
}
26✔
111

112
impl Display for ListContainsExpr {
113
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
×
114
        write!(f, "contains({}, {})", &self.list, &self.value)
×
115
    }
×
116
}
117

118
impl AnalysisExpr for ListContainsExpr {
119
    // falsification(contains([1,2,5], x)) =>
120
    //   falsification(x != 1) and falsification(x != 2) and falsification(x != 5)
121

122
    fn stat_falsification(&self, catalog: &mut dyn StatsCatalog) -> Option<ExprRef> {
52✔
123
        let min = self.list.min(catalog)?;
52✔
124
        let max = self.list.max(catalog)?;
52✔
125
        // If the list is constant when we can compare each element to the value
126
        if min == max {
52✔
127
            let list_ = min
52✔
128
                .as_opt::<LiteralVTable>()
52✔
129
                .and_then(|l| l.value().as_list_opt())
52✔
130
                .and_then(|l| l.elements())?;
52✔
131
            if list_.is_empty() {
52✔
132
                // contains([], x) is always false.
133
                return Some(lit(true));
×
134
            }
52✔
135
            let value_max = self.value.max(catalog)?;
52✔
136
            let value_min = self.value.min(catalog)?;
52✔
137

138
            return list_
52✔
139
                .iter()
52✔
140
                .map(move |v| {
816✔
141
                    or(
816✔
142
                        lt(value_max.clone(), lit(v.clone())),
816✔
143
                        gt(value_min.clone(), lit(v.clone())),
816✔
144
                    )
816✔
145
                })
816✔
146
                .reduce(and);
52✔
147
        }
×
148

149
        None
×
150
    }
52✔
151
}
152

153
#[cfg(test)]
154
mod tests {
155
    use vortex_array::arrays::{BoolArray, BooleanBuffer, ListArray, PrimitiveArray};
156
    use vortex_array::stats::Stat;
157
    use vortex_array::validity::Validity;
158
    use vortex_array::{Array, ArrayRef, IntoArray};
159
    use vortex_dtype::PType::I32;
160
    use vortex_dtype::{DType, Field, FieldPath, FieldPathSet, Nullability, StructFields};
161
    use vortex_scalar::Scalar;
162
    use vortex_utils::aliases::hash_map::HashMap;
163

164
    use crate::list_contains::list_contains;
165
    use crate::pruning::checked_pruning_expr;
166
    use crate::{Arc, HashSet, Scope, and, col, get_item, gt, lit, lt, or, root};
167

168
    fn test_array() -> ArrayRef {
169
        ListArray::try_new(
170
            PrimitiveArray::from_iter(vec![1, 1, 2, 2, 2, 2, 2, 3, 3, 3]).into_array(),
171
            PrimitiveArray::from_iter(vec![0, 5, 10]).into_array(),
172
            Validity::AllValid,
173
        )
174
        .unwrap()
175
        .into_array()
176
    }
177

178
    #[test]
179
    pub fn test_one() {
180
        let arr = test_array();
181

182
        let expr = list_contains(root(), lit(1));
183
        let item = expr.evaluate(&Scope::new(arr)).unwrap();
184

185
        assert_eq!(
186
            item.scalar_at(0).unwrap(),
187
            Scalar::bool(true, Nullability::Nullable)
188
        );
189
        assert_eq!(
190
            item.scalar_at(1).unwrap(),
191
            Scalar::bool(false, Nullability::Nullable)
192
        );
193
    }
194

195
    #[test]
196
    pub fn test_all() {
197
        let arr = test_array();
198

199
        let expr = list_contains(root(), lit(2));
200
        let item = expr.evaluate(&Scope::new(arr)).unwrap();
201

202
        assert_eq!(
203
            item.scalar_at(0).unwrap(),
204
            Scalar::bool(true, Nullability::Nullable)
205
        );
206
        assert_eq!(
207
            item.scalar_at(1).unwrap(),
208
            Scalar::bool(true, Nullability::Nullable)
209
        );
210
    }
211

212
    #[test]
213
    pub fn test_none() {
214
        let arr = test_array();
215

216
        let expr = list_contains(root(), lit(4));
217
        let item = expr.evaluate(&Scope::new(arr)).unwrap();
218

219
        assert_eq!(
220
            item.scalar_at(0).unwrap(),
221
            Scalar::bool(false, Nullability::Nullable)
222
        );
223
        assert_eq!(
224
            item.scalar_at(1).unwrap(),
225
            Scalar::bool(false, Nullability::Nullable)
226
        );
227
    }
228

229
    #[test]
230
    pub fn test_empty() {
231
        let arr = ListArray::try_new(
232
            PrimitiveArray::from_iter(vec![1, 1, 2, 2, 2]).into_array(),
233
            PrimitiveArray::from_iter(vec![0, 5, 5]).into_array(),
234
            Validity::AllValid,
235
        )
236
        .unwrap()
237
        .into_array();
238

239
        let expr = list_contains(root(), lit(2));
240
        let item = expr.evaluate(&Scope::new(arr)).unwrap();
241

242
        assert_eq!(
243
            item.scalar_at(0).unwrap(),
244
            Scalar::bool(true, Nullability::Nullable)
245
        );
246
        assert_eq!(
247
            item.scalar_at(1).unwrap(),
248
            Scalar::bool(false, Nullability::Nullable)
249
        );
250
    }
251

252
    #[test]
253
    pub fn test_nullable() {
254
        let arr = ListArray::try_new(
255
            PrimitiveArray::from_iter(vec![1, 1, 2, 2, 2]).into_array(),
256
            PrimitiveArray::from_iter(vec![0, 5, 5]).into_array(),
257
            Validity::Array(BoolArray::from(BooleanBuffer::from(vec![true, false])).into_array()),
258
        )
259
        .unwrap()
260
        .into_array();
261

262
        let expr = list_contains(root(), lit(2));
263
        let item = expr.evaluate(&Scope::new(arr)).unwrap();
264

265
        assert_eq!(
266
            item.scalar_at(0).unwrap(),
267
            Scalar::bool(true, Nullability::Nullable)
268
        );
269
        assert!(!item.is_valid(1).unwrap());
270
    }
271

272
    #[test]
273
    pub fn test_return_type() {
274
        let scope = DType::Struct(
275
            StructFields::new(
276
                ["array"].into(),
277
                vec![DType::List(
278
                    Arc::new(DType::Primitive(I32, Nullability::NonNullable)),
279
                    Nullability::Nullable,
280
                )],
281
            ),
282
            Nullability::NonNullable,
283
        );
284

285
        let expr = list_contains(get_item("array", root()), lit(2));
286

287
        // Expect nullable, although scope is non-nullable
288
        assert_eq!(
289
            expr.return_dtype(&scope).unwrap(),
290
            DType::Bool(Nullability::Nullable)
291
        );
292
    }
293

294
    #[test]
295
    pub fn list_falsification() {
296
        let expr = list_contains(
297
            lit(Scalar::list(
298
                Arc::new(DType::Primitive(I32, Nullability::NonNullable)),
299
                vec![1.into(), 2.into(), 3.into()],
300
                Nullability::NonNullable,
301
            )),
302
            col("a"),
303
        );
304

305
        let (expr, st) = checked_pruning_expr(
306
            &expr,
307
            &FieldPathSet::from_iter([
308
                FieldPath::from_iter([Field::Name("a".into()), Field::Name("max".into())]),
309
                FieldPath::from_iter([Field::Name("a".into()), Field::Name("min".into())]),
310
            ]),
311
        )
312
        .unwrap();
313

314
        assert_eq!(
315
            &expr,
316
            &and(
317
                and(
318
                    or(lt(col("a_max"), lit(1i32)), gt(col("a_min"), lit(1i32)),),
319
                    or(lt(col("a_max"), lit(2i32)), gt(col("a_min"), lit(2i32)),)
320
                ),
321
                or(lt(col("a_max"), lit(3i32)), gt(col("a_min"), lit(3i32)),)
322
            )
323
        );
324

325
        assert_eq!(
326
            st.map(),
327
            &HashMap::from_iter([(
328
                FieldPath::from_name("a"),
329
                HashSet::from([Stat::Min, Stat::Max])
330
            )])
331
        );
332
    }
333
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc