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

vortex-data / vortex / 16473770414

23 Jul 2025 02:38PM UTC coverage: 80.988% (-0.07%) from 81.055%
16473770414

push

github

web-flow
chore[duckdb]: scan log info -> trace (#3989)

Signed-off-by: Joe Isaacs <joe.isaacs@live.co.uk>

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

35 existing lines in 7 files now uncovered.

42052 of 51924 relevant lines covered (80.99%)

173623.22 hits per line

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

83.82
/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)]
21
pub struct ListContainsExpr {
22
    list: ExprRef,
23
    value: ExprRef,
24
}
25

26
impl PartialEq for ListContainsExpr {
27
    fn eq(&self, other: &Self) -> bool {
663✔
28
        self.list.eq(&other.list) && self.value.eq(&other.value)
663✔
29
    }
663✔
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 {
128✔
40
        ExprId::new_ref("list_contains")
128✔
41
    }
128✔
42

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

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

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

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

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

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

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

94
impl ListContainsExpr {
95
    pub fn new(list: ExprRef, value: ExprRef) -> Self {
195✔
96
        Self { list, value }
195✔
97
    }
195✔
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 {
54✔
109
    ListContainsExpr::new(list, value).into_expr()
54✔
110
}
54✔
111

112
impl Display for ListContainsExpr {
UNCOV
113
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
×
UNCOV
114
        write!(f, "contains({}, {})", &self.list, &self.value)
×
UNCOV
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> {
48✔
123
        let min = self.list.min(catalog)?;
48✔
124
        let max = self.list.max(catalog)?;
48✔
125
        // If the list is constant when we can compare each element to the value
126
        if min == max {
48✔
127
            let list_ = min
48✔
128
                .as_opt::<LiteralVTable>()
48✔
129
                .and_then(|l| l.value().as_list_opt())
48✔
130
                .and_then(|l| l.elements())?;
48✔
131
            if list_.is_empty() {
48✔
132
                // contains([], x) is always false.
133
                return Some(lit(true));
×
134
            }
48✔
135
            let value_max = self.value.max(catalog)?;
48✔
136
            let value_min = self.value.min(catalog)?;
48✔
137

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

149
        None
×
150
    }
48✔
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 {
3✔
169
        ListArray::try_new(
3✔
170
            PrimitiveArray::from_iter(vec![1, 1, 2, 2, 2, 2, 2, 3, 3, 3]).into_array(),
3✔
171
            PrimitiveArray::from_iter(vec![0, 5, 10]).into_array(),
3✔
172
            Validity::AllValid,
3✔
173
        )
3✔
174
        .unwrap()
3✔
175
        .into_array()
3✔
176
    }
3✔
177

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

© 2026 Coveralls, Inc