oxistore-columnar 0.1.2

OxiStore columnar storage: Parquet read/write via Arrow RecordBatch with OxiARC compression
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
use arrow::array::{Array, Scalar as ArrowScalar};
/// Predicate AST and row-group pruning engine for Parquet column statistics.
///
/// This module implements a predicate algebra that can be evaluated against
/// Parquet row-group statistics to skip entire row groups that provably cannot
/// satisfy the predicate.  The engine is conservative: when uncertain (no
/// statistics, type mismatch, `Not`) it always keeps the row group.
///
/// # Design principles
///
/// - **Interval arithmetic only**: min/max statistics are used to determine
///   whether the row group _could_ contain a value satisfying the predicate.
/// - **No complement logic for `Not`**: negation of a range is not computed;
///   the engine always keeps groups guarded by `Not`.
/// - **Type-safe dispatch**: each `Scalar` variant maps to exactly one
///   `Statistics` variant; mismatches result in keeping the group.
use arrow::array::{BooleanArray, Float32Array, Float64Array, Int32Array, Int64Array, StringArray};
use arrow::compute::kernels::cmp as arrow_cmp;
use parquet::data_type::ByteArray;
use parquet::file::metadata::RowGroupMetaData;
use parquet::file::statistics::Statistics;
use parquet::schema::types::SchemaDescriptor;

use crate::ColumnarError;

/// A scalar value used in predicate comparisons.
///
/// `Float32` stores `f32` directly.  NaN is never equal to anything in
/// Parquet statistics so NaN predicates will always evaluate conservatively
/// (keep the row group).
#[derive(Clone, Debug, PartialEq)]
pub enum Scalar {
    /// Boolean scalar.
    Bool(bool),
    /// 32-bit signed integer scalar.
    Int32(i32),
    /// 64-bit signed integer scalar.
    Int64(i64),
    /// 32-bit floating-point scalar.
    Float32(f32),
    /// 64-bit floating-point scalar.
    Float64(f64),
    /// Raw byte string scalar (compared lexicographically against Parquet
    /// `BYTE_ARRAY` statistics).
    Bytes(Vec<u8>),
    /// The SQL NULL scalar.  Any comparison involving Null keeps the group.
    Null,
}

/// A comparison operator used in leaf predicates.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum CmpOp {
    /// Equal (`=`).
    Eq,
    /// Not equal (`<>`).
    Ne,
    /// Strictly less than (`<`).
    Lt,
    /// Less than or equal (`<=`).
    Le,
    /// Strictly greater than (`>`).
    Gt,
    /// Greater than or equal (`>=`).
    Ge,
}

/// A predicate that can be pushed down to the Parquet row-group level.
///
/// Predicates are evaluated against column-chunk statistics (min / max /
/// null count) to determine whether a row group _might_ contain any rows
/// that satisfy the predicate.  Returning `false` from
/// [`Predicate::row_group_might_match`] means the row group can be skipped
/// entirely; returning `true` means the row group must be read (it may or
/// may not actually contain matching rows — further evaluation at the row
/// level is required).
#[derive(Clone, Debug)]
pub enum Predicate {
    /// A single column comparison: `column op value`.
    Cmp {
        /// Column name (must match a leaf column name in the Parquet schema).
        column: String,
        /// Comparison operator.
        op: CmpOp,
        /// Right-hand side scalar value.
        value: Scalar,
    },
    /// Logical conjunction: all sub-predicates must be satisfiable.
    And(Vec<Predicate>),
    /// Logical disjunction: at least one sub-predicate must be satisfiable.
    Or(Vec<Predicate>),
    /// Logical negation.  Conservative: always keeps the row group (no
    /// complement-range computation is performed).
    Not(Box<Predicate>),
    /// The tautology — always matches every row group.
    All,
    /// The contradiction — never matches any row group.  Useful as the
    /// identity element for `Or` construction.
    None,
}

impl Predicate {
    /// Evaluate this predicate against all rows in a [`arrow::record_batch::RecordBatch`],
    /// returning a [`BooleanArray`] mask.
    ///
    /// A `true` element in the mask means the corresponding row satisfies the
    /// predicate.  `Predicate::All` always returns all-true; `Predicate::None`
    /// always returns all-false.
    ///
    /// # Errors
    ///
    /// Returns [`ColumnarError::SchemaMismatch`] if a column named in the
    /// predicate is missing from the batch, or if the scalar type is
    /// incompatible with the column type.  Returns [`ColumnarError::Arrow`] on
    /// Arrow compute errors.
    pub fn evaluate_batch(
        &self,
        batch: &arrow::record_batch::RecordBatch,
    ) -> Result<BooleanArray, ColumnarError> {
        match self {
            Predicate::All => Ok(BooleanArray::from(vec![true; batch.num_rows()])),
            Predicate::None => Ok(BooleanArray::from(vec![false; batch.num_rows()])),
            Predicate::Cmp { column, op, value } => {
                let col_idx = batch.schema().index_of(column).map_err(|_| {
                    ColumnarError::SchemaMismatch(format!("column '{}' not found in batch", column))
                })?;
                let col = batch.column(col_idx);
                evaluate_cmp_column(col.as_ref(), op, value)
            }
            Predicate::And(preds) => {
                let mut result = BooleanArray::from(vec![true; batch.num_rows()]);
                for pred in preds {
                    let mask = pred.evaluate_batch(batch)?;
                    result = arrow::compute::and(&result, &mask).map_err(ColumnarError::Arrow)?;
                }
                Ok(result)
            }
            Predicate::Or(preds) => {
                let mut result = BooleanArray::from(vec![false; batch.num_rows()]);
                for pred in preds {
                    let mask = pred.evaluate_batch(batch)?;
                    result = arrow::compute::or(&result, &mask).map_err(ColumnarError::Arrow)?;
                }
                Ok(result)
            }
            Predicate::Not(pred) => {
                let mask = pred.evaluate_batch(batch)?;
                arrow::compute::not(&mask).map_err(ColumnarError::Arrow)
            }
        }
    }

    /// Returns `true` if the predicate _might_ be satisfied by some row in
    /// this row group, or `false` if statistics _prove_ that no row can match.
    ///
    /// # Conservatism
    ///
    /// The engine always keeps the row group when:
    /// - The column is not found in the schema.
    /// - No statistics are present for the column chunk.
    /// - The `Scalar` type does not match the statistics type.
    /// - The predicate is `Not(_)`.
    /// - The `Scalar` is `Null`.
    pub fn row_group_might_match(&self, rg: &RowGroupMetaData, schema: &SchemaDescriptor) -> bool {
        match self {
            Predicate::All => true,
            Predicate::None => false,
            Predicate::Not(_) => true,
            Predicate::And(preds) => preds.iter().all(|p| p.row_group_might_match(rg, schema)),
            Predicate::Or(preds) => preds.iter().any(|p| p.row_group_might_match(rg, schema)),
            Predicate::Cmp { column, op, value } => cmp_might_match(rg, schema, column, op, value),
        }
    }
}

/// Evaluate a column-level comparison against each row, returning a boolean mask.
///
/// The column type and scalar type must match; a type mismatch returns
/// [`ColumnarError::SchemaMismatch`].
fn evaluate_cmp_column(
    col: &dyn Array,
    op: &CmpOp,
    value: &Scalar,
) -> Result<BooleanArray, ColumnarError> {
    match value {
        Scalar::Int32(v) => {
            let arr = col.as_any().downcast_ref::<Int32Array>().ok_or_else(|| {
                ColumnarError::SchemaMismatch("type mismatch: expected Int32".into())
            })?;
            let rhs = ArrowScalar::new(Int32Array::from(vec![*v]));
            apply_cmp_i32(arr, op, &rhs)
        }
        Scalar::Int64(v) => {
            let arr = col.as_any().downcast_ref::<Int64Array>().ok_or_else(|| {
                ColumnarError::SchemaMismatch("type mismatch: expected Int64".into())
            })?;
            let rhs = ArrowScalar::new(Int64Array::from(vec![*v]));
            apply_cmp_i64(arr, op, &rhs)
        }
        Scalar::Float32(v) => {
            let arr = col.as_any().downcast_ref::<Float32Array>().ok_or_else(|| {
                ColumnarError::SchemaMismatch("type mismatch: expected Float32".into())
            })?;
            let rhs = ArrowScalar::new(Float32Array::from(vec![*v]));
            apply_cmp_f32(arr, op, &rhs)
        }
        Scalar::Float64(v) => {
            let arr = col.as_any().downcast_ref::<Float64Array>().ok_or_else(|| {
                ColumnarError::SchemaMismatch("type mismatch: expected Float64".into())
            })?;
            let rhs = ArrowScalar::new(Float64Array::from(vec![*v]));
            apply_cmp_f64(arr, op, &rhs)
        }
        Scalar::Bytes(v) => {
            // StringArray is the Arrow Utf8 type; raw bytes must be valid UTF-8.
            let arr = col.as_any().downcast_ref::<StringArray>().ok_or_else(|| {
                ColumnarError::SchemaMismatch(
                    "type mismatch: expected Utf8 (StringArray) for Bytes comparison".into(),
                )
            })?;
            let s = std::str::from_utf8(v).map_err(|_| {
                ColumnarError::SchemaMismatch(
                    "Bytes scalar is not valid UTF-8; cannot compare against Utf8 column".into(),
                )
            })?;
            let rhs = ArrowScalar::new(StringArray::from(vec![s]));
            apply_cmp_str(arr, op, &rhs)
        }
        Scalar::Bool(_) | Scalar::Null => Err(ColumnarError::SchemaMismatch(format!(
            "unsupported scalar type for row-level filter: {value:?}",
        ))),
    }
}

/// Apply a comparison op to an Int32 column and a scalar Datum.
fn apply_cmp_i32(
    col: &Int32Array,
    op: &CmpOp,
    rhs: &ArrowScalar<Int32Array>,
) -> Result<BooleanArray, ColumnarError> {
    match op {
        CmpOp::Eq => arrow_cmp::eq(col, rhs).map_err(ColumnarError::Arrow),
        CmpOp::Ne => arrow_cmp::neq(col, rhs).map_err(ColumnarError::Arrow),
        CmpOp::Lt => arrow_cmp::lt(col, rhs).map_err(ColumnarError::Arrow),
        CmpOp::Le => arrow_cmp::lt_eq(col, rhs).map_err(ColumnarError::Arrow),
        CmpOp::Gt => arrow_cmp::gt(col, rhs).map_err(ColumnarError::Arrow),
        CmpOp::Ge => arrow_cmp::gt_eq(col, rhs).map_err(ColumnarError::Arrow),
    }
}

/// Apply a comparison op to an Int64 column and a scalar Datum.
fn apply_cmp_i64(
    col: &Int64Array,
    op: &CmpOp,
    rhs: &ArrowScalar<Int64Array>,
) -> Result<BooleanArray, ColumnarError> {
    match op {
        CmpOp::Eq => arrow_cmp::eq(col, rhs).map_err(ColumnarError::Arrow),
        CmpOp::Ne => arrow_cmp::neq(col, rhs).map_err(ColumnarError::Arrow),
        CmpOp::Lt => arrow_cmp::lt(col, rhs).map_err(ColumnarError::Arrow),
        CmpOp::Le => arrow_cmp::lt_eq(col, rhs).map_err(ColumnarError::Arrow),
        CmpOp::Gt => arrow_cmp::gt(col, rhs).map_err(ColumnarError::Arrow),
        CmpOp::Ge => arrow_cmp::gt_eq(col, rhs).map_err(ColumnarError::Arrow),
    }
}

/// Apply a comparison op to a Float32 column and a scalar Datum.
fn apply_cmp_f32(
    col: &Float32Array,
    op: &CmpOp,
    rhs: &ArrowScalar<Float32Array>,
) -> Result<BooleanArray, ColumnarError> {
    match op {
        CmpOp::Eq => arrow_cmp::eq(col, rhs).map_err(ColumnarError::Arrow),
        CmpOp::Ne => arrow_cmp::neq(col, rhs).map_err(ColumnarError::Arrow),
        CmpOp::Lt => arrow_cmp::lt(col, rhs).map_err(ColumnarError::Arrow),
        CmpOp::Le => arrow_cmp::lt_eq(col, rhs).map_err(ColumnarError::Arrow),
        CmpOp::Gt => arrow_cmp::gt(col, rhs).map_err(ColumnarError::Arrow),
        CmpOp::Ge => arrow_cmp::gt_eq(col, rhs).map_err(ColumnarError::Arrow),
    }
}

/// Apply a comparison op to a Float64 column and a scalar Datum.
fn apply_cmp_f64(
    col: &Float64Array,
    op: &CmpOp,
    rhs: &ArrowScalar<Float64Array>,
) -> Result<BooleanArray, ColumnarError> {
    match op {
        CmpOp::Eq => arrow_cmp::eq(col, rhs).map_err(ColumnarError::Arrow),
        CmpOp::Ne => arrow_cmp::neq(col, rhs).map_err(ColumnarError::Arrow),
        CmpOp::Lt => arrow_cmp::lt(col, rhs).map_err(ColumnarError::Arrow),
        CmpOp::Le => arrow_cmp::lt_eq(col, rhs).map_err(ColumnarError::Arrow),
        CmpOp::Gt => arrow_cmp::gt(col, rhs).map_err(ColumnarError::Arrow),
        CmpOp::Ge => arrow_cmp::gt_eq(col, rhs).map_err(ColumnarError::Arrow),
    }
}

/// Apply a comparison op to a StringArray column and a scalar Datum.
fn apply_cmp_str(
    col: &StringArray,
    op: &CmpOp,
    rhs: &ArrowScalar<StringArray>,
) -> Result<BooleanArray, ColumnarError> {
    match op {
        CmpOp::Eq => arrow_cmp::eq(col, rhs).map_err(ColumnarError::Arrow),
        CmpOp::Ne => arrow_cmp::neq(col, rhs).map_err(ColumnarError::Arrow),
        CmpOp::Lt => arrow_cmp::lt(col, rhs).map_err(ColumnarError::Arrow),
        CmpOp::Le => arrow_cmp::lt_eq(col, rhs).map_err(ColumnarError::Arrow),
        CmpOp::Gt => arrow_cmp::gt(col, rhs).map_err(ColumnarError::Arrow),
        CmpOp::Ge => arrow_cmp::gt_eq(col, rhs).map_err(ColumnarError::Arrow),
    }
}

/// Evaluate a leaf comparison predicate against row-group statistics.
///
/// Returns `true` (keep) when uncertain; `false` (skip) only when statistics
/// prove impossibility.
fn cmp_might_match(
    rg: &RowGroupMetaData,
    schema: &SchemaDescriptor,
    column: &str,
    op: &CmpOp,
    value: &Scalar,
) -> bool {
    // Conservative: Null rhs never prunes.
    if matches!(value, Scalar::Null) {
        return true;
    }

    // Find the leaf column index by name.
    let col_idx = match schema.columns().iter().position(|c| c.name() == column) {
        Some(idx) => idx,
        Option::None => return true, // unknown column — keep
    };

    // Bounds-check against the row group (schemas may differ).
    if col_idx >= rg.num_columns() {
        return true;
    }

    // Get column chunk statistics.
    let stats = match rg.column(col_idx).statistics() {
        Some(s) => s,
        Option::None => return true, // no statistics — keep
    };

    // Dispatch to type-specific evaluation.
    evaluate_cmp(stats, op, value)
}

/// Dispatch a comparison to the correct typed statistics handler.
fn evaluate_cmp(stats: &Statistics, op: &CmpOp, value: &Scalar) -> bool {
    match (stats, value) {
        (Statistics::Boolean(s), Scalar::Bool(v)) => {
            let (min, max) = match (s.min_opt(), s.max_opt()) {
                (Some(mn), Some(mx)) => (*mn, *mx),
                _ => return true,
            };
            apply_bool_op(op, min, max, *v)
        }
        (Statistics::Int32(s), Scalar::Int32(v)) => {
            let (min, max) = match (s.min_opt(), s.max_opt()) {
                (Some(mn), Some(mx)) => (*mn, *mx),
                _ => return true,
            };
            apply_ord_op(op, min, max, *v, s.null_count_opt())
        }
        (Statistics::Int64(s), Scalar::Int64(v)) => {
            let (min, max) = match (s.min_opt(), s.max_opt()) {
                (Some(mn), Some(mx)) => (*mn, *mx),
                _ => return true,
            };
            apply_ord_op(op, min, max, *v, s.null_count_opt())
        }
        (Statistics::Float(s), Scalar::Float32(v)) => {
            let (min, max) = match (s.min_opt(), s.max_opt()) {
                (Some(mn), Some(mx)) => (*mn, *mx),
                _ => return true,
            };
            apply_float_op_f32(op, min, max, *v, s.null_count_opt())
        }
        (Statistics::Double(s), Scalar::Float64(v)) => {
            let (min, max) = match (s.min_opt(), s.max_opt()) {
                (Some(mn), Some(mx)) => (*mn, *mx),
                _ => return true,
            };
            apply_float_op_f64(op, min, max, *v, s.null_count_opt())
        }
        (Statistics::ByteArray(s), Scalar::Bytes(v)) => {
            let (min, max) = match (s.min_opt(), s.max_opt()) {
                (Some(mn), Some(mx)) => (mn, mx),
                _ => return true,
            };
            apply_bytes_op(op, min, max, v.as_slice(), s.null_count_opt())
        }
        // Type mismatch or unsupported combination — keep conservatively.
        _ => true,
    }
}

/// Apply a comparison operator to boolean min/max statistics.
///
/// Booleans have only two possible values so the interval is just the pair
/// `(min, max)`.  Note: `Ne` on booleans is always kept conservatively
/// because we do not have null-count available in this path.
fn apply_bool_op(op: &CmpOp, min: bool, max: bool, v: bool) -> bool {
    match op {
        // min <= v && v <= max for booleans, rewritten to avoid bool_comparison:
        // v must be at least min (i.e., !min || v) and at most max (i.e., !v || max)
        CmpOp::Eq => (!min || v) && (!v || max),
        // Conservatively keep — we have no null-count available here.
        CmpOp::Ne => true,
        // min < v  for booleans means min=false and v=true.
        CmpOp::Lt => !min && v,
        // min <= v means v=true OR min=false.
        CmpOp::Le => !min || v,
        // max > v  for booleans means max=true and v=false.
        CmpOp::Gt => max && !v,
        // max >= v means max=true OR v=false.
        CmpOp::Ge => max || !v,
    }
}

/// Apply a comparison operator to ordered (Ord) min/max statistics.
///
/// Generic over any `PartialOrd + PartialEq` value type (i32, i64).
fn apply_ord_op<T: PartialOrd + PartialEq>(
    op: &CmpOp,
    min: T,
    max: T,
    v: T,
    null_count: Option<u64>,
) -> bool {
    match op {
        CmpOp::Eq => min <= v && v <= max,
        CmpOp::Ne => {
            // Skip ONLY if every value in the chunk equals v AND there are no nulls.
            let all_same = min == v && max == v;
            let no_nulls = null_count == Some(0);
            !(all_same && no_nulls)
        }
        CmpOp::Lt => min < v,
        CmpOp::Le => min <= v,
        CmpOp::Gt => max > v,
        CmpOp::Ge => max >= v,
    }
}

/// Apply a comparison operator to f32 min/max statistics.
///
/// NaN comparisons always keep the row group (NaN is not ordered).
fn apply_float_op_f32(op: &CmpOp, min: f32, max: f32, v: f32, null_count: Option<u64>) -> bool {
    // NaN predicates are not prunable — keep.
    if v.is_nan() || min.is_nan() || max.is_nan() {
        return true;
    }
    match op {
        CmpOp::Eq => min <= v && v <= max,
        CmpOp::Ne => {
            let all_same = (min - v).abs() < f32::EPSILON && (max - v).abs() < f32::EPSILON;
            let no_nulls = null_count == Some(0);
            !(all_same && no_nulls)
        }
        CmpOp::Lt => min < v,
        CmpOp::Le => min <= v,
        CmpOp::Gt => max > v,
        CmpOp::Ge => max >= v,
    }
}

/// Apply a comparison operator to f64 min/max statistics.
fn apply_float_op_f64(op: &CmpOp, min: f64, max: f64, v: f64, null_count: Option<u64>) -> bool {
    if v.is_nan() || min.is_nan() || max.is_nan() {
        return true;
    }
    match op {
        CmpOp::Eq => min <= v && v <= max,
        CmpOp::Ne => {
            let all_same = (min - v).abs() < f64::EPSILON && (max - v).abs() < f64::EPSILON;
            let no_nulls = null_count == Some(0);
            !(all_same && no_nulls)
        }
        CmpOp::Lt => min < v,
        CmpOp::Le => min <= v,
        CmpOp::Gt => max > v,
        CmpOp::Ge => max >= v,
    }
}

/// Apply a comparison operator to ByteArray min/max statistics (lexicographic).
fn apply_bytes_op(
    op: &CmpOp,
    min: &ByteArray,
    max: &ByteArray,
    v: &[u8],
    null_count: Option<u64>,
) -> bool {
    let min_data = min.data();
    let max_data = max.data();
    match op {
        CmpOp::Eq => min_data <= v && v <= max_data,
        CmpOp::Ne => {
            let all_same = min_data == v && max_data == v;
            let no_nulls = null_count == Some(0);
            !(all_same && no_nulls)
        }
        CmpOp::Lt => min_data < v,
        CmpOp::Le => min_data <= v,
        CmpOp::Gt => max_data > v,
        CmpOp::Ge => max_data >= v,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn predicate_all_and_none() {
        // Trivial predicates need no schema — just verify compile + logic.
        // We can't easily construct RowGroupMetaData in unit tests without a
        // full builder chain, so we test the leaf logic via evaluate_cmp.
        let stats = Statistics::int32(Some(10), Some(20), None, Some(0), false);
        // Eq: value in range
        assert!(evaluate_cmp(&stats, &CmpOp::Eq, &Scalar::Int32(15)));
        // Eq: value below range
        assert!(!evaluate_cmp(&stats, &CmpOp::Eq, &Scalar::Int32(5)));
        // Gt: max > v
        assert!(evaluate_cmp(&stats, &CmpOp::Gt, &Scalar::Int32(15)));
        // Gt: max not > v
        assert!(!evaluate_cmp(&stats, &CmpOp::Gt, &Scalar::Int32(25)));
        // Lt: min < v
        assert!(evaluate_cmp(&stats, &CmpOp::Lt, &Scalar::Int32(15)));
        // Lt: min not < v
        assert!(!evaluate_cmp(&stats, &CmpOp::Lt, &Scalar::Int32(5)));
    }

    #[test]
    fn ne_pruning_requires_no_nulls() {
        let stats_no_nulls = Statistics::int32(Some(10), Some(10), None, Some(0), false);
        // All values equal 10, no nulls → prune when Ne 10
        assert!(!evaluate_cmp(
            &stats_no_nulls,
            &CmpOp::Ne,
            &Scalar::Int32(10)
        ));
        // Keep when Ne 5 (10 != 5 is possible)
        assert!(evaluate_cmp(&stats_no_nulls, &CmpOp::Ne, &Scalar::Int32(5)));

        let stats_with_nulls = Statistics::int32(Some(10), Some(10), None, Some(1), false);
        // Same range but has nulls → keep (Ne might succeed where null is present)
        assert!(evaluate_cmp(
            &stats_with_nulls,
            &CmpOp::Ne,
            &Scalar::Int32(10)
        ));
    }

    #[test]
    fn type_mismatch_keeps_group() {
        let stats = Statistics::int32(Some(10), Some(20), None, Some(0), false);
        // Int64 scalar against Int32 statistics → type mismatch → keep
        assert!(evaluate_cmp(&stats, &CmpOp::Eq, &Scalar::Int64(15)));
    }

    #[test]
    fn null_scalar_keeps_group() {
        // Null scalar never prunes — test via a fake stats set.
        let stats = Statistics::int32(Some(10), Some(20), None, Some(0), false);
        assert!(evaluate_cmp(&stats, &CmpOp::Eq, &Scalar::Null));
    }

    #[test]
    fn ge_boundary_keeps_group_at_max() {
        // max = 100, query Ge 100 → 100 >= 100 → keep
        let stats = Statistics::int64(Some(1), Some(100), None, Some(0), false);
        assert!(evaluate_cmp(&stats, &CmpOp::Ge, &Scalar::Int64(100)));
        // Ge 101 → max 100 < 101 → prune
        assert!(!evaluate_cmp(&stats, &CmpOp::Ge, &Scalar::Int64(101)));
    }
}