vortex-expr 0.94.0

Analyses and rewrites over Vortex expression trees
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors

use vortex_array::aggregate_fn::AggregateFnRef;
use vortex_array::aggregate_fn::fns::all_nan::AllNan;
use vortex_array::aggregate_fn::fns::all_non_nan::AllNonNan;
use vortex_array::aggregate_fn::fns::all_non_null::AllNonNull;
use vortex_array::aggregate_fn::fns::all_null::AllNull;
use vortex_array::aggregate_fn::fns::max::Max;
use vortex_array::aggregate_fn::fns::min::Min;
use vortex_array::aggregate_fn::fns::nan_count::NanCount;
use vortex_array::aggregate_fn::fns::null_count::NullCount;
use vortex_array::expr::BoundExpression;
use vortex_array::scalar_fn::ScalarFnId;
use vortex_array::scalar_fn::ScalarFnVTable;
use vortex_array::scalar_fn::builtin::boolean::Boolean;
use vortex_array::scalar_fn::builtin::cast::Cast;
use vortex_array::scalar_fn::builtin::compare::Compare;
use vortex_array::scalar_fn::builtin::is_not_null::IsNotNull;
use vortex_array::scalar_fn::builtin::literal::Literal;
use vortex_array::scalar_fn::builtin::operators::BooleanOperator;
use vortex_array::scalar_fn::builtin::operators::CompareOperator;
use vortex_error::VortexResult;
use vortex_session::VortexSession;

use crate::bound::and;
use crate::bound::cast;
use crate::bound::eq;
use crate::bound::gt;
use crate::bound::gt_eq;
use crate::bound::lit;
use crate::bound::lt;
use crate::bound::lt_eq;
use crate::bound::or_collect;
use crate::stats::derive::StatDerivation;
use crate::stats::derive::stat_expr;
use crate::stats::rewrite::StatsRewriteRule;
use crate::stats::rewrite::falsify;
use crate::stats::rewrite::helpers::*;
use crate::stats::session::StatsSession;

/// Register the stats rewrite rules for the scalar functions vortex-array is built on.
pub(crate) fn register_builtins(session: &StatsSession) {
    session.register_rewrite(CompareNanCountStatsRewrite);
    session.register_rewrite(CompareAllNonNanStatsRewrite);
    session.register_rewrite(BooleanNanCountStatsRewrite);
    session.register_rewrite(BooleanAllNonNanStatsRewrite);
    session.register_rewrite(LiteralStatsRewrite);
    session.register_rewrite(IsNotNullNullCountStatsRewrite);
    session.register_rewrite(IsNotNullAllNullStatsRewrite);
    session.register_rewrite(IsNotNullAllNonNullStatsRewrite);
    session.register_stat_derivation(LiteralStatDerivation);
    session.register_stat_derivation(CastStatDerivation);
}

#[derive(Debug)]
struct LiteralStatsRewrite;

impl StatsRewriteRule for LiteralStatsRewrite {
    fn scalar_fn_id(&self) -> ScalarFnId {
        Literal.id()
    }

    fn falsify(
        &self,
        expr: &BoundExpression,
        _session: &VortexSession,
    ) -> VortexResult<Option<BoundExpression>> {
        Ok(literal_truth(expr).map(|value| lit(!value)))
    }

    fn satisfy(
        &self,
        expr: &BoundExpression,
        _session: &VortexSession,
    ) -> VortexResult<Option<BoundExpression>> {
        Ok(literal_truth(expr).map(lit))
    }
}

/// A literal's statistics are its value. A statistic that is not meaningful for the literal, such
/// as `NaNCount` over a non-float value, is unknown rather than read from the literal.
#[derive(Debug)]
struct LiteralStatDerivation;

impl StatDerivation for LiteralStatDerivation {
    fn scalar_fn_id(&self) -> ScalarFnId {
        Literal.id()
    }

    fn derive_stat(
        &self,
        expr: &BoundExpression,
        aggregate_fn: &AggregateFnRef,
        _session: &VortexSession,
    ) -> Option<BoundExpression> {
        let scalar = expr.as_::<Literal>();
        if aggregate_fn.is::<Min>() || aggregate_fn.is::<Max>() {
            return Some(lit(scalar.clone()));
        }
        if aggregate_fn.is::<NullCount>() {
            return Some(lit(if scalar.is_null() { 1u64 } else { 0u64 }));
        }
        if aggregate_fn.is::<AllNull>() {
            return Some(lit(scalar.is_null()));
        }
        if aggregate_fn.is::<AllNonNull>() {
            return Some(lit(!scalar.is_null()));
        }
        let value = scalar.as_primitive_opt()?;
        if !value.ptype().is_float() {
            return None;
        }
        if aggregate_fn.is::<NanCount>() {
            return Some(lit(if value.is_nan() { 1u64 } else { 0u64 }));
        }
        // Whether a null is NaN is unknown, so only a non-null value answers these.
        if scalar.is_null() {
            return None;
        }
        if aggregate_fn.is::<AllNan>() {
            return Some(lit(value.is_nan()));
        }
        if aggregate_fn.is::<AllNonNan>() {
            return Some(lit(!value.is_nan()));
        }
        None
    }
}

/// Derives the statistics of `cast(input)` from the statistics of `input`.
#[derive(Debug)]
struct CastStatDerivation;

impl StatDerivation for CastStatDerivation {
    fn scalar_fn_id(&self) -> ScalarFnId {
        Cast.id()
    }

    fn derive_stat(
        &self,
        expr: &BoundExpression,
        aggregate_fn: &AggregateFnRef,
        session: &VortexSession,
    ) -> Option<BoundExpression> {
        let input = expr.child(0);
        if aggregate_fn.is::<Min>() || aggregate_fn.is::<Max>() {
            return stat_expr(input, aggregate_fn, session)
                .map(|stat| cast(stat, expr.as_::<Cast>().clone()));
        }
        // A cast keeps NaNs as NaNs. It changes the sum's dtype and the size of each value, so
        // neither carries over from the input.
        if aggregate_fn.is::<NanCount>() {
            return stat_expr(input, aggregate_fn, session);
        }
        None
    }
}

/// Bool literal predicate value
fn literal_truth(expr: &BoundExpression) -> Option<bool> {
    expr.as_::<Literal>().as_bool_opt().and_then(|b| b.value())
}

#[derive(Debug)]
struct CompareNanCountStatsRewrite;

impl StatsRewriteRule for CompareNanCountStatsRewrite {
    fn scalar_fn_id(&self) -> ScalarFnId {
        Compare.id()
    }

    fn falsify(
        &self,
        expr: &BoundExpression,
        session: &VortexSession,
    ) -> VortexResult<Option<BoundExpression>> {
        compare_falsify::<NanCountProof>(expr, session)
    }
}

#[derive(Debug)]
struct CompareAllNonNanStatsRewrite;

impl StatsRewriteRule for CompareAllNonNanStatsRewrite {
    fn scalar_fn_id(&self) -> ScalarFnId {
        Compare.id()
    }

    fn falsify(
        &self,
        expr: &BoundExpression,
        session: &VortexSession,
    ) -> VortexResult<Option<BoundExpression>> {
        compare_falsify::<AllNonNanProof>(expr, session)
    }
}

#[derive(Debug)]
struct BooleanNanCountStatsRewrite;

impl StatsRewriteRule for BooleanNanCountStatsRewrite {
    fn scalar_fn_id(&self) -> ScalarFnId {
        Boolean.id()
    }

    fn falsify(
        &self,
        expr: &BoundExpression,
        session: &VortexSession,
    ) -> VortexResult<Option<BoundExpression>> {
        boolean_falsify::<NanCountProof>(expr, session)
    }
}

#[derive(Debug)]
struct BooleanAllNonNanStatsRewrite;

impl StatsRewriteRule for BooleanAllNonNanStatsRewrite {
    fn scalar_fn_id(&self) -> ScalarFnId {
        Boolean.id()
    }

    fn falsify(
        &self,
        expr: &BoundExpression,
        session: &VortexSession,
    ) -> VortexResult<Option<BoundExpression>> {
        boolean_falsify::<AllNonNanProof>(expr, session)
    }
}

fn compare_falsify<P: NonNanProof>(
    expr: &BoundExpression,
    session: &VortexSession,
) -> VortexResult<Option<BoundExpression>> {
    let operator = expr.as_::<Compare>();
    let lhs = expr.child(0);
    let rhs = expr.child(1);

    Ok(match operator {
        CompareOperator::Eq => {
            let left = min(lhs, session)
                .zip(max(rhs, session))
                .map(|(a, b)| gt(a, b));
            let right = min(rhs, session)
                .zip(max(lhs, session))
                .map(|(a, b)| gt(a, b));
            or_collect(left.into_iter().chain(right))
                .map(|value_predicate| {
                    with_non_nan_guards::<P>([lhs, rhs], value_predicate, session)
                })
                .transpose()?
                .flatten()
        }
        CompareOperator::NotEq => min(lhs, session)
            .zip(max(rhs, session))
            .zip(max(lhs, session).zip(min(rhs, session)))
            .map(|((min_lhs, max_rhs), (max_lhs, min_rhs))| {
                with_non_nan_guards::<P>(
                    [lhs, rhs],
                    and(eq(min_lhs, max_rhs), eq(max_lhs, min_rhs)),
                    session,
                )
            })
            .transpose()?
            .flatten(),
        CompareOperator::Gt => max(lhs, session)
            .zip(min(rhs, session))
            .map(|(a, b)| with_non_nan_guards::<P>([lhs, rhs], lt_eq(a, b), session))
            .transpose()?
            .flatten(),
        CompareOperator::Gte => max(lhs, session)
            .zip(min(rhs, session))
            .map(|(a, b)| with_non_nan_guards::<P>([lhs, rhs], lt(a, b), session))
            .transpose()?
            .flatten(),
        CompareOperator::Lt => min(lhs, session)
            .zip(max(rhs, session))
            .map(|(a, b)| with_non_nan_guards::<P>([lhs, rhs], gt_eq(a, b), session))
            .transpose()?
            .flatten(),
        CompareOperator::Lte => min(lhs, session)
            .zip(max(rhs, session))
            .map(|(a, b)| with_non_nan_guards::<P>([lhs, rhs], gt(a, b), session))
            .transpose()?
            .flatten(),
    })
}

fn boolean_falsify<P: NonNanProof>(
    expr: &BoundExpression,
    session: &VortexSession,
) -> VortexResult<Option<BoundExpression>> {
    let operator = expr.as_::<Boolean>();
    let lhs = expr.child(0);
    let rhs = expr.child(1);

    Ok(match operator {
        BooleanOperator::And => {
            if !P::EMIT_UNGUARDED_REWRITES {
                return Ok(None);
            }

            let lhs_falsifier = falsify(lhs, session)?;
            let rhs_falsifier = falsify(rhs, session)?;
            or_collect(lhs_falsifier.into_iter().chain(rhs_falsifier))
        }
        BooleanOperator::Or => {
            // Check before recursing: falsifying the children first would repeat the
            // whole recursive rewrite once per registered `Boolean` rule, which is
            // exponential in `Or`-nesting depth for chains like `a = 1 OR a = 2 OR ...`.
            if !P::EMIT_UNGUARDED_REWRITES {
                return Ok(None);
            }

            match (falsify(lhs, session)?, falsify(rhs, session)?) {
                (Some(lhs), Some(rhs)) => Some(and(lhs, rhs)),
                _ => None,
            }
        }
    })
}

#[derive(Debug)]
struct IsNotNullNullCountStatsRewrite;

impl StatsRewriteRule for IsNotNullNullCountStatsRewrite {
    fn scalar_fn_id(&self) -> ScalarFnId {
        IsNotNull.id()
    }

    fn falsify(
        &self,
        expr: &BoundExpression,
        session: &VortexSession,
    ) -> VortexResult<Option<BoundExpression>> {
        Ok(null_count(expr.child(0), session).map(|null_count| eq(null_count, row_count())))
    }

    fn satisfy(
        &self,
        expr: &BoundExpression,
        session: &VortexSession,
    ) -> VortexResult<Option<BoundExpression>> {
        Ok(null_count(expr.child(0), session).map(|null_count| eq(null_count, lit(0u64))))
    }
}

#[derive(Debug)]
struct IsNotNullAllNullStatsRewrite;

impl StatsRewriteRule for IsNotNullAllNullStatsRewrite {
    fn scalar_fn_id(&self) -> ScalarFnId {
        IsNotNull.id()
    }

    fn falsify(
        &self,
        expr: &BoundExpression,
        session: &VortexSession,
    ) -> VortexResult<Option<BoundExpression>> {
        Ok(all_null(expr.child(0), session))
    }
}

#[derive(Debug)]
struct IsNotNullAllNonNullStatsRewrite;

impl StatsRewriteRule for IsNotNullAllNonNullStatsRewrite {
    fn scalar_fn_id(&self) -> ScalarFnId {
        IsNotNull.id()
    }

    fn satisfy(
        &self,
        expr: &BoundExpression,
        session: &VortexSession,
    ) -> VortexResult<Option<BoundExpression>> {
        Ok(all_non_null(expr.child(0), session))
    }
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;
    use std::sync::LazyLock;
    use std::sync::atomic::AtomicUsize;
    use std::sync::atomic::Ordering;

    use vortex_array::aggregate_fn::AggregateFnRef;
    use vortex_array::aggregate_fn::AggregateFnVTableExt;
    use vortex_array::aggregate_fn::EmptyOptions as AggregateEmptyOptions;
    use vortex_array::aggregate_fn::fns::all_non_nan::AllNonNan;
    use vortex_array::dtype::DType;
    use vortex_array::dtype::Nullability;
    use vortex_array::dtype::PType;
    use vortex_array::dtype::StructFields;
    use vortex_array::scalar::Scalar;
    use vortex_array::scalar_fn::EmptyOptions;
    use vortex_array::scalar_fn::ScalarFnId;
    use vortex_array::scalar_fn::ScalarFnVTable;
    use vortex_array::scalar_fn::builtin::boolean::Boolean;
    use vortex_array::scalar_fn::builtin::compare::Compare;
    use vortex_array::stats::Stat;
    use vortex_error::VortexResult;
    use vortex_session::VortexSession;

    use crate::BoundExpression;
    use crate::Expression;
    use crate::ScalarFnExprExt;
    use crate::and;
    use crate::cast;
    use crate::col;
    use crate::eq;
    use crate::gt;
    use crate::gt_eq;
    use crate::is_not_null;
    use crate::lambda::row_count::RowCount;
    use crate::lit;
    use crate::lt;
    use crate::lt_eq;
    use crate::or;
    use crate::stats::expr::StatFn;
    use crate::stats::expr::StatOptions;
    use crate::stats::rewrite::BoundExpressionPruneExt;
    use crate::stats::rewrite::StatsRewriteRule;
    use crate::stats::session::StatsSessionExt;

    static SESSION: LazyLock<VortexSession> = LazyLock::new(VortexSession::empty);

    fn stat(expr: Expression, stat: Stat) -> Expression {
        let aggregate_fn = stat.aggregate_fn().expect("stat should have aggregate fn");
        stat_fn(expr, aggregate_fn)
    }

    fn stat_fn(expr: Expression, aggregate_fn: AggregateFnRef) -> Expression {
        StatFn.new_expr(StatOptions::new(aggregate_fn), [expr])
    }

    fn test_scope() -> DType {
        DType::Struct(
            StructFields::from_iter([
                ("a", DType::Primitive(PType::I32, Nullability::NonNullable)),
                ("b", DType::Primitive(PType::I32, Nullability::NonNullable)),
                ("f", DType::Primitive(PType::F32, Nullability::NonNullable)),
                ("s", DType::Utf8(Nullability::NonNullable)),
                ("t", DType::Utf8(Nullability::NonNullable)),
                ("n", nested_struct_dtype()),
            ]),
            Nullability::NonNullable,
        )
    }

    fn nested_struct_dtype() -> DType {
        DType::Struct(
            StructFields::from_iter([("x", DType::Primitive(PType::F32, Nullability::Nullable))]),
            Nullability::NonNullable,
        )
    }

    fn falsify(expr: &Expression) -> VortexResult<Option<BoundExpression>> {
        expr.bind(&test_scope())?.falsify(&SESSION)
    }

    fn satisfy(expr: &Expression) -> VortexResult<Option<BoundExpression>> {
        expr.bind(&test_scope())?.satisfy(&SESSION)
    }

    fn bind_expected(expr: Option<Expression>) -> VortexResult<Option<BoundExpression>> {
        expr.map(|expr| expr.bind(&test_scope())).transpose()
    }

    fn all_null(expr: &Expression) -> Expression {
        crate::stats::all_null(expr.clone())
    }

    fn all_non_null(expr: &Expression) -> Expression {
        crate::stats::all_non_null(expr.clone())
    }

    macro_rules! assert_rewrite_eq {
        ($actual:expr, $expected:expr) => {
            assert_eq!($actual, bind_expected($expected)?)
        };
    }

    fn nan_guarded(expr: Expression, value_predicate: Expression) -> Expression {
        or(
            and(
                eq(stat(expr.clone(), Stat::NaNCount), lit(0u64)),
                value_predicate.clone(),
            ),
            and(
                stat_fn(expr, AllNonNan.bind(AggregateEmptyOptions)),
                value_predicate,
            ),
        )
    }

    #[test]
    fn rewrites_comparison_falsifier() -> VortexResult<()> {
        let expr = gt(col("a"), lit(10));
        assert_rewrite_eq!(
            falsify(&expr)?,
            Some(lt_eq(stat(col("a"), Stat::Max), lit(10)))
        );

        let expr = eq(col("a"), col("b"));
        assert_rewrite_eq!(
            falsify(&expr)?,
            Some(or(
                gt(stat(col("a"), Stat::Min), stat(col("b"), Stat::Max)),
                gt(stat(col("b"), Stat::Min), stat(col("a"), Stat::Max)),
            ))
        );

        let expr = eq(col("s"), col("t"));
        assert_rewrite_eq!(
            falsify(&expr)?,
            Some(or(
                gt(stat(col("s"), Stat::Min), stat(col("t"), Stat::Max)),
                gt(stat(col("t"), Stat::Min), stat(col("s"), Stat::Max)),
            ))
        );
        Ok(())
    }

    #[test]
    fn rewrites_boolean_falsifiers() -> VortexResult<()> {
        let expr = and(gt(col("a"), lit(10)), lt(col("a"), lit(50)));
        assert_rewrite_eq!(
            falsify(&expr)?,
            Some(or(
                lt_eq(stat(col("a"), Stat::Max), lit(10)),
                gt_eq(stat(col("a"), Stat::Min), lit(50)),
            ))
        );

        let expr = or(gt(col("a"), lit(10)), lt(col("a"), lit(5)));
        assert_rewrite_eq!(
            falsify(&expr)?,
            Some(and(
                lt_eq(stat(col("a"), Stat::Max), lit(10)),
                gt_eq(stat(col("a"), Stat::Min), lit(5)),
            ))
        );
        Ok(())
    }

    /// Counts how many times the stats rewrite visits a node of one scalar function.
    #[derive(Debug)]
    struct VisitCounter(ScalarFnId, Arc<AtomicUsize>);

    impl StatsRewriteRule for VisitCounter {
        fn scalar_fn_id(&self) -> ScalarFnId {
            self.0
        }

        fn falsify(
            &self,
            _expr: &BoundExpression,
            _session: &VortexSession,
        ) -> VortexResult<Option<BoundExpression>> {
            self.1.fetch_add(1, Ordering::Relaxed);
            Ok(None)
        }
    }

    #[test]
    fn or_chain_falsify_visits_each_node_once() -> VortexResult<()> {
        let session = VortexSession::empty();
        let visits = Arc::new(AtomicUsize::new(0));
        session
            .stats()
            .register_rewrite(VisitCounter(Compare.id(), Arc::clone(&visits)));
        session
            .stats()
            .register_rewrite(VisitCounter(Boolean.id(), Arc::clone(&visits)));

        let expr = (1..16).fold(eq(col("a"), lit(0)), |chain, i| {
            or(chain, eq(col("a"), lit(i)))
        });
        let falsifier = expr.bind(&test_scope())?.falsify(&session)?;
        assert!(falsifier.is_some());

        // One visit per `Compare` or `Boolean` node: 16 comparisons plus 15 `or`s.
        assert_eq!(visits.load(Ordering::Relaxed), 31);
        Ok(())
    }

    #[test]
    fn rewrites_null_falsifiers() -> VortexResult<()> {
        assert_rewrite_eq!(
            falsify(&is_not_null(col("a")))?,
            Some(or(
                eq(
                    stat(col("a"), Stat::NullCount),
                    RowCount.new_expr(EmptyOptions, []),
                ),
                all_null(&col("a")),
            ))
        );
        Ok(())
    }

    #[test]
    fn rewrites_null_satisfiers() -> VortexResult<()> {
        assert_rewrite_eq!(
            satisfy(&is_not_null(col("a")))?,
            Some(or(
                eq(stat(col("a"), Stat::NullCount), lit(0u64)),
                all_non_null(&col("a")),
            ))
        );
        Ok(())
    }

    #[test]
    fn nan_guard_tracks_cast_source_dtype() -> VortexResult<()> {
        let dtype = DType::Primitive(PType::I32, Nullability::NonNullable);
        let expr = gt(cast(col("f"), dtype.clone()), lit(5i32));

        assert_rewrite_eq!(
            falsify(&expr)?,
            Some(nan_guarded(
                col("f"),
                lt_eq(cast(stat(col("f"), Stat::Max), dtype), lit(5i32)),
            ))
        );
        Ok(())
    }

    #[test]
    fn skips_falsifier_when_min_max_unsupported_for_dtype() -> VortexResult<()> {
        // Struct comparisons are valid predicates, but min/max aggregates do not
        // support struct inputs, so no stats-backed falsifier should be produced.
        let struct_scalar = Scalar::struct_(
            nested_struct_dtype(),
            vec![Scalar::primitive(1.0f32, Nullability::Nullable)],
        );
        assert_rewrite_eq!(falsify(&lt_eq(col("n"), lit(struct_scalar.clone())))?, None);
        assert_rewrite_eq!(falsify(&eq(col("n"), lit(struct_scalar)))?, None);
        Ok(())
    }

    #[test]
    fn forwards_min_max_through_safe_cast() -> VortexResult<()> {
        let dtype = DType::Primitive(PType::I64, Nullability::NonNullable);
        let expr = eq(cast(col("a"), dtype.clone()), lit(42i64));

        assert_rewrite_eq!(
            falsify(&expr)?,
            Some(or(
                gt(cast(stat(col("a"), Stat::Min), dtype.clone()), lit(42i64)),
                gt(lit(42i64), cast(stat(col("a"), Stat::Max), dtype)),
            ))
        );
        Ok(())
    }
}