reddb-io-server 1.1.1

RedDB server-side engine: storage, runtime, replication, MCP, AI, and the gRPC/HTTP/RedWire/PG-wire dispatchers. Re-exported by the umbrella `reddb` crate.
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
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
//! Partition pruning — given a table's partition spec, a list of
//! child partitions with their bounds, and a simplified view of the
//! WHERE clause, return the subset of partitions that can possibly
//! contain rows matching the filter.
//!
//! This module is deliberately decoupled from the full planner AST:
//! callers distill the relevant WHERE fragment into a [`PrunePredicate`]
//! tree before invoking the pruner. That keeps this unit-testable
//! end-to-end without dragging half the query engine into the test
//! binary, and lets three different entry points (hypertable router,
//! classic `PARTITION BY`, projection dispatcher) share the same
//! pruning rules.
//!
//! **Pruning rules**
//!
//! * RANGE: keep a child when its `[low, high)` interval overlaps the
//!   predicate's derived interval on the partition key column.
//! * LIST: keep a child when its literal values intersect the
//!   predicate's allowed set.
//! * HASH: only equality predicates on the key are actionable —
//!   otherwise every child is kept (nothing to prune safely).
//!
//! When the predicate references columns other than the partition
//! key, the pruner is **conservative**: it keeps every child. That
//! preserves correctness at the cost of missing some opportunities —
//! exactly the Timescale / Postgres contract.

use std::ops::Bound;

/// Simplified predicate tree the planner hands to the pruner. Only
/// the shapes we can actually act on are named; anything else the
/// planner tags `Opaque`, which keeps every partition.
#[derive(Debug, Clone)]
pub enum PrunePredicate {
    And(Vec<PrunePredicate>),
    Or(Vec<PrunePredicate>),
    /// `column op value`.
    Compare {
        column: String,
        op: PruneOp,
        value: PruneValue,
    },
    /// `column IN (v1, v2, …)`.
    In {
        column: String,
        values: Vec<PruneValue>,
    },
    /// Something we can't interpret. Acts as "every row possibly
    /// matches" for pruning purposes.
    Opaque,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PruneOp {
    Eq,
    Lt,
    LtEq,
    Gt,
    GtEq,
    NotEq,
}

/// Minimal typed value compared with bounds. Keeping it tiny and
/// comparable in-crate avoids wiring against the massive `Value`
/// enum from schema layer — callers lower their storage value to
/// one of these variants before calling the pruner.
#[derive(Debug, Clone, PartialEq)]
pub enum PruneValue {
    Int(i64),
    Float(f64),
    Text(String),
    Bool(bool),
}

impl PartialOrd for PruneValue {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        use PruneValue::*;
        match (self, other) {
            (Int(a), Int(b)) => a.partial_cmp(b),
            (Float(a), Float(b)) => a.partial_cmp(b),
            (Int(a), Float(b)) => (*a as f64).partial_cmp(b),
            (Float(a), Int(b)) => a.partial_cmp(&(*b as f64)),
            (Text(a), Text(b)) => a.partial_cmp(b),
            (Bool(a), Bool(b)) => a.partial_cmp(b),
            _ => None,
        }
    }
}

/// Partitioning strategy (mirrors `storage::query::PartitionKind` but
/// kept local so this module has no upstream AST dependency).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PruneKind {
    Range,
    List,
    Hash,
}

/// Spec + key column name. Hash prunes need a modulus count that
/// matches the catalog — the pruner stores it as metadata only.
#[derive(Debug, Clone)]
pub struct PrunePartitioning {
    pub kind: PruneKind,
    pub column: String,
}

/// Bounds of a single RANGE child.
#[derive(Debug, Clone)]
pub struct RangeChild {
    pub name: String,
    /// `None` on either side means "unbounded".
    pub low: Option<PruneValue>,
    pub high_exclusive: Option<PruneValue>,
}

/// A single LIST child that holds one or more literal values.
#[derive(Debug, Clone)]
pub struct ListChild {
    pub name: String,
    pub values: Vec<PruneValue>,
}

/// A single HASH child — `remainder` is the residue this partition
/// owns, `modulus` is the global modulus.
#[derive(Debug, Clone)]
pub struct HashChild {
    pub name: String,
    pub modulus: u32,
    pub remainder: u32,
}

/// The pruner's output: the subset of child names that cannot be
/// proven to miss every matching row. Ordering mirrors the input.
pub fn prune_range(
    partitioning: &PrunePartitioning,
    children: &[RangeChild],
    predicate: &PrunePredicate,
) -> Vec<String> {
    debug_assert!(matches!(partitioning.kind, PruneKind::Range));
    let interval = derive_interval(predicate, &partitioning.column);
    children
        .iter()
        .filter(|child| child_range_overlaps(child, &interval))
        .map(|c| c.name.clone())
        .collect()
}

pub fn prune_list(
    partitioning: &PrunePartitioning,
    children: &[ListChild],
    predicate: &PrunePredicate,
) -> Vec<String> {
    debug_assert!(matches!(partitioning.kind, PruneKind::List));
    let allowed = derive_allowed_set(predicate, &partitioning.column);
    children
        .iter()
        .filter(|child| match &allowed {
            AllowedSet::Any => true,
            AllowedSet::Only(set) => child.values.iter().any(|v| set.iter().any(|a| a == v)),
        })
        .map(|c| c.name.clone())
        .collect()
}

pub fn prune_hash(
    partitioning: &PrunePartitioning,
    children: &[HashChild],
    predicate: &PrunePredicate,
) -> Vec<String> {
    debug_assert!(matches!(partitioning.kind, PruneKind::Hash));
    let Some(value) = derive_single_eq(predicate, &partitioning.column) else {
        return children.iter().map(|c| c.name.clone()).collect();
    };
    let hashed = hash_prune_value(&value);
    children
        .iter()
        .filter(|child| {
            child.modulus == 0 || (hashed % child.modulus as u64) == child.remainder as u64
        })
        .map(|c| c.name.clone())
        .collect()
}

// -------------------------------------------------------------------------
// Interval derivation (RANGE pruning)
// -------------------------------------------------------------------------

/// Closed/open interval on the partition key derived from the
/// predicate. `low` uses `Bound::Included/Excluded` semantics.
#[derive(Debug, Clone)]
struct Interval {
    low: Bound<PruneValue>,
    high: Bound<PruneValue>,
    /// A `NotEq(val)` hole — pruner ignores for RANGE (holes rarely
    /// eliminate chunks) but LIST pruning uses the allowed set.
    _holes: Vec<PruneValue>,
}

impl Interval {
    fn unbounded() -> Self {
        Self {
            low: Bound::Unbounded,
            high: Bound::Unbounded,
            _holes: Vec::new(),
        }
    }
}

fn derive_interval(predicate: &PrunePredicate, column: &str) -> Interval {
    match predicate {
        PrunePredicate::Compare {
            column: c,
            op,
            value,
        } if c == column => match op {
            PruneOp::Eq => Interval {
                low: Bound::Included(value.clone()),
                high: Bound::Included(value.clone()),
                _holes: Vec::new(),
            },
            PruneOp::Lt => Interval {
                low: Bound::Unbounded,
                high: Bound::Excluded(value.clone()),
                _holes: Vec::new(),
            },
            PruneOp::LtEq => Interval {
                low: Bound::Unbounded,
                high: Bound::Included(value.clone()),
                _holes: Vec::new(),
            },
            PruneOp::Gt => Interval {
                low: Bound::Excluded(value.clone()),
                high: Bound::Unbounded,
                _holes: Vec::new(),
            },
            PruneOp::GtEq => Interval {
                low: Bound::Included(value.clone()),
                high: Bound::Unbounded,
                _holes: Vec::new(),
            },
            PruneOp::NotEq => {
                let mut i = Interval::unbounded();
                i._holes.push(value.clone());
                i
            }
        },
        PrunePredicate::In { column: c, values } if c == column && !values.is_empty() => {
            let mut min = None;
            let mut max = None;
            for v in values {
                if min.as_ref().map(|m: &PruneValue| v < m).unwrap_or(true) {
                    min = Some(v.clone());
                }
                if max.as_ref().map(|m: &PruneValue| v > m).unwrap_or(true) {
                    max = Some(v.clone());
                }
            }
            Interval {
                low: min.map(Bound::Included).unwrap_or(Bound::Unbounded),
                high: max.map(Bound::Included).unwrap_or(Bound::Unbounded),
                _holes: Vec::new(),
            }
        }
        PrunePredicate::And(children) => {
            let mut acc = Interval::unbounded();
            for child in children {
                let i = derive_interval(child, column);
                acc = intersect(&acc, &i);
            }
            acc
        }
        PrunePredicate::Or(children) => {
            // Conservative: union of child intervals. If any child is
            // unbounded or opaque, the union is unbounded.
            let mut min: Option<Bound<PruneValue>> = None;
            let mut max: Option<Bound<PruneValue>> = None;
            for child in children {
                let i = derive_interval(child, column);
                if matches!(i.low, Bound::Unbounded) {
                    min = Some(Bound::Unbounded);
                } else if min != Some(Bound::Unbounded) {
                    min = Some(widest_low(min.clone(), i.low.clone()));
                }
                if matches!(i.high, Bound::Unbounded) {
                    max = Some(Bound::Unbounded);
                } else if max != Some(Bound::Unbounded) {
                    max = Some(widest_high(max.clone(), i.high.clone()));
                }
            }
            Interval {
                low: min.unwrap_or(Bound::Unbounded),
                high: max.unwrap_or(Bound::Unbounded),
                _holes: Vec::new(),
            }
        }
        _ => Interval::unbounded(),
    }
}

fn widest_low(current: Option<Bound<PruneValue>>, next: Bound<PruneValue>) -> Bound<PruneValue> {
    match (current, next) {
        (None, b) => b,
        (Some(Bound::Unbounded), _) | (_, Bound::Unbounded) => Bound::Unbounded,
        (Some(a), b) => {
            if bound_low_less_or_equal(&a, &b) {
                a
            } else {
                b
            }
        }
    }
}

fn widest_high(current: Option<Bound<PruneValue>>, next: Bound<PruneValue>) -> Bound<PruneValue> {
    match (current, next) {
        (None, b) => b,
        (Some(Bound::Unbounded), _) | (_, Bound::Unbounded) => Bound::Unbounded,
        (Some(a), b) => {
            if bound_high_greater_or_equal(&a, &b) {
                a
            } else {
                b
            }
        }
    }
}

fn bound_low_less_or_equal(a: &Bound<PruneValue>, b: &Bound<PruneValue>) -> bool {
    match (a, b) {
        (Bound::Unbounded, _) => true,
        (_, Bound::Unbounded) => false,
        (Bound::Included(x), Bound::Included(y)) | (Bound::Excluded(x), Bound::Excluded(y)) => {
            x.partial_cmp(y).map(|o| o.is_le()).unwrap_or(true)
        }
        (Bound::Included(x), Bound::Excluded(y)) => {
            x.partial_cmp(y).map(|o| o.is_le()).unwrap_or(true)
        }
        (Bound::Excluded(x), Bound::Included(y)) => {
            x.partial_cmp(y).map(|o| o.is_lt()).unwrap_or(true)
        }
    }
}

fn bound_high_greater_or_equal(a: &Bound<PruneValue>, b: &Bound<PruneValue>) -> bool {
    match (a, b) {
        (Bound::Unbounded, _) => true,
        (_, Bound::Unbounded) => false,
        (Bound::Included(x), Bound::Included(y)) | (Bound::Excluded(x), Bound::Excluded(y)) => {
            x.partial_cmp(y).map(|o| o.is_ge()).unwrap_or(true)
        }
        (Bound::Included(x), Bound::Excluded(y)) => {
            x.partial_cmp(y).map(|o| o.is_ge()).unwrap_or(true)
        }
        (Bound::Excluded(x), Bound::Included(y)) => {
            x.partial_cmp(y).map(|o| o.is_gt()).unwrap_or(true)
        }
    }
}

fn intersect(a: &Interval, b: &Interval) -> Interval {
    let low = tighter_low(&a.low, &b.low);
    let high = tighter_high(&a.high, &b.high);
    Interval {
        low,
        high,
        _holes: Vec::new(),
    }
}

fn tighter_low(a: &Bound<PruneValue>, b: &Bound<PruneValue>) -> Bound<PruneValue> {
    match (a, b) {
        (Bound::Unbounded, other) | (other, Bound::Unbounded) => other.clone(),
        (x, y) => {
            if bound_low_less_or_equal(x, y) {
                y.clone()
            } else {
                x.clone()
            }
        }
    }
}

fn tighter_high(a: &Bound<PruneValue>, b: &Bound<PruneValue>) -> Bound<PruneValue> {
    match (a, b) {
        (Bound::Unbounded, other) | (other, Bound::Unbounded) => other.clone(),
        (x, y) => {
            if bound_high_greater_or_equal(x, y) {
                y.clone()
            } else {
                x.clone()
            }
        }
    }
}

fn child_range_overlaps(child: &RangeChild, interval: &Interval) -> bool {
    // Child `[low, high_exclusive)` vs interval `[i.low, i.high]`.
    // Conservatively report overlap whenever either side is unknown.
    let child_low_ok = match &interval.high {
        Bound::Unbounded => true,
        Bound::Included(upper) => match &child.low {
            None => true,
            Some(cl) => cl.partial_cmp(upper).map(|o| o.is_le()).unwrap_or(true),
        },
        Bound::Excluded(upper) => match &child.low {
            None => true,
            Some(cl) => cl.partial_cmp(upper).map(|o| o.is_lt()).unwrap_or(true),
        },
    };
    let child_high_ok = match &interval.low {
        Bound::Unbounded => true,
        Bound::Included(lower) => match &child.high_exclusive {
            None => true,
            Some(ch) => ch.partial_cmp(lower).map(|o| o.is_gt()).unwrap_or(true),
        },
        Bound::Excluded(lower) => match &child.high_exclusive {
            None => true,
            Some(ch) => ch.partial_cmp(lower).map(|o| o.is_gt()).unwrap_or(true),
        },
    };
    child_low_ok && child_high_ok
}

// -------------------------------------------------------------------------
// LIST pruning
// -------------------------------------------------------------------------

enum AllowedSet {
    Any,
    Only(Vec<PruneValue>),
}

fn derive_allowed_set(predicate: &PrunePredicate, column: &str) -> AllowedSet {
    match predicate {
        PrunePredicate::Compare {
            column: c,
            op: PruneOp::Eq,
            value,
        } if c == column => AllowedSet::Only(vec![value.clone()]),
        PrunePredicate::In { column: c, values } if c == column && !values.is_empty() => {
            AllowedSet::Only(values.clone())
        }
        PrunePredicate::And(children) => {
            // AND tightens: start with Any, intersect as we go.
            let mut acc: Option<Vec<PruneValue>> = None;
            for child in children {
                match derive_allowed_set(child, column) {
                    AllowedSet::Any => {}
                    AllowedSet::Only(set) => {
                        acc = Some(match acc {
                            None => set,
                            Some(existing) => existing
                                .into_iter()
                                .filter(|v| set.iter().any(|w| w == v))
                                .collect(),
                        });
                    }
                }
            }
            acc.map(AllowedSet::Only).unwrap_or(AllowedSet::Any)
        }
        PrunePredicate::Or(children) => {
            // OR widens: union. Any opaque child makes the set Any.
            let mut merged: Vec<PruneValue> = Vec::new();
            for child in children {
                match derive_allowed_set(child, column) {
                    AllowedSet::Any => return AllowedSet::Any,
                    AllowedSet::Only(set) => {
                        for v in set {
                            if !merged.iter().any(|m| m == &v) {
                                merged.push(v);
                            }
                        }
                    }
                }
            }
            AllowedSet::Only(merged)
        }
        _ => AllowedSet::Any,
    }
}

// -------------------------------------------------------------------------
// HASH pruning
// -------------------------------------------------------------------------

fn derive_single_eq(predicate: &PrunePredicate, column: &str) -> Option<PruneValue> {
    match predicate {
        PrunePredicate::Compare {
            column: c,
            op: PruneOp::Eq,
            value,
        } if c == column => Some(value.clone()),
        PrunePredicate::And(children) => {
            for child in children {
                if let Some(v) = derive_single_eq(child, column) {
                    return Some(v);
                }
            }
            None
        }
        _ => None,
    }
}

fn hash_prune_value(v: &PruneValue) -> u64 {
    // FNV-1a 64-bit. Good-enough, zero-dep.
    const OFFSET: u64 = 0xcbf29ce484222325;
    const PRIME: u64 = 0x100000001b3;
    let mut h = OFFSET;
    let bytes: Vec<u8> = match v {
        PruneValue::Int(i) => i.to_le_bytes().to_vec(),
        PruneValue::Float(f) => f.to_le_bytes().to_vec(),
        PruneValue::Text(s) => s.as_bytes().to_vec(),
        PruneValue::Bool(b) => vec![*b as u8],
    };
    for b in bytes {
        h ^= b as u64;
        h = h.wrapping_mul(PRIME);
    }
    h
}

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

    fn pint(v: i64) -> PruneValue {
        PruneValue::Int(v)
    }

    fn range_child(name: &str, low: Option<i64>, high_exclusive: Option<i64>) -> RangeChild {
        RangeChild {
            name: name.to_string(),
            low: low.map(pint),
            high_exclusive: high_exclusive.map(pint),
        }
    }

    fn spec_range() -> PrunePartitioning {
        PrunePartitioning {
            kind: PruneKind::Range,
            column: "ts".to_string(),
        }
    }

    fn spec_list() -> PrunePartitioning {
        PrunePartitioning {
            kind: PruneKind::List,
            column: "region".to_string(),
        }
    }

    fn spec_hash() -> PrunePartitioning {
        PrunePartitioning {
            kind: PruneKind::Hash,
            column: "user_id".to_string(),
        }
    }

    #[test]
    fn range_keeps_only_chunks_overlapping_equality() {
        let children = vec![
            range_child("c0", Some(0), Some(100)),
            range_child("c1", Some(100), Some(200)),
            range_child("c2", Some(200), Some(300)),
        ];
        let pred = PrunePredicate::Compare {
            column: "ts".into(),
            op: PruneOp::Eq,
            value: pint(150),
        };
        assert_eq!(prune_range(&spec_range(), &children, &pred), vec!["c1"]);
    }

    #[test]
    fn range_handles_greater_than_predicate() {
        let children = vec![
            range_child("c0", Some(0), Some(100)),
            range_child("c1", Some(100), Some(200)),
            range_child("c2", Some(200), Some(300)),
        ];
        let pred = PrunePredicate::Compare {
            column: "ts".into(),
            op: PruneOp::GtEq,
            value: pint(150),
        };
        let kept = prune_range(&spec_range(), &children, &pred);
        assert_eq!(kept, vec!["c1", "c2"]);
    }

    #[test]
    fn range_handles_between_via_and() {
        let children = vec![
            range_child("c0", Some(0), Some(100)),
            range_child("c1", Some(100), Some(200)),
            range_child("c2", Some(200), Some(300)),
            range_child("c3", Some(300), Some(400)),
        ];
        let pred = PrunePredicate::And(vec![
            PrunePredicate::Compare {
                column: "ts".into(),
                op: PruneOp::GtEq,
                value: pint(150),
            },
            PrunePredicate::Compare {
                column: "ts".into(),
                op: PruneOp::Lt,
                value: pint(250),
            },
        ]);
        let kept = prune_range(&spec_range(), &children, &pred);
        assert_eq!(kept, vec!["c1", "c2"]);
    }

    #[test]
    fn range_conservative_when_predicate_on_different_column() {
        let children = vec![range_child("c0", Some(0), Some(100))];
        let pred = PrunePredicate::Compare {
            column: "other".into(),
            op: PruneOp::Eq,
            value: pint(42),
        };
        assert_eq!(prune_range(&spec_range(), &children, &pred), vec!["c0"]);
    }

    #[test]
    fn range_handles_unbounded_children() {
        let children = vec![
            range_child("past", None, Some(0)),
            range_child("mid", Some(0), Some(100)),
            range_child("future", Some(100), None),
        ];
        let pred = PrunePredicate::Compare {
            column: "ts".into(),
            op: PruneOp::GtEq,
            value: pint(50),
        };
        let kept = prune_range(&spec_range(), &children, &pred);
        assert_eq!(kept, vec!["mid", "future"]);
    }

    #[test]
    fn range_in_clause_bounds() {
        let children = vec![
            range_child("c0", Some(0), Some(100)),
            range_child("c1", Some(100), Some(200)),
            range_child("c2", Some(200), Some(300)),
        ];
        let pred = PrunePredicate::In {
            column: "ts".into(),
            values: vec![pint(50), pint(250)],
        };
        let kept = prune_range(&spec_range(), &children, &pred);
        // Conservative: covers [50, 250] so touches c0, c1, c2.
        assert_eq!(kept, vec!["c0", "c1", "c2"]);
    }

    #[test]
    fn list_prunes_non_matching_value_sets() {
        let children = vec![
            ListChild {
                name: "us".to_string(),
                values: vec![PruneValue::Text("us-east".into())],
            },
            ListChild {
                name: "eu".to_string(),
                values: vec![PruneValue::Text("eu-west".into())],
            },
            ListChild {
                name: "apac".to_string(),
                values: vec![PruneValue::Text("ap-south".into())],
            },
        ];
        let pred = PrunePredicate::Compare {
            column: "region".into(),
            op: PruneOp::Eq,
            value: PruneValue::Text("eu-west".into()),
        };
        assert_eq!(prune_list(&spec_list(), &children, &pred), vec!["eu"]);
    }

    #[test]
    fn list_handles_in_clause() {
        let children = vec![
            ListChild {
                name: "a".to_string(),
                values: vec![PruneValue::Text("a".into())],
            },
            ListChild {
                name: "b".to_string(),
                values: vec![PruneValue::Text("b".into())],
            },
            ListChild {
                name: "c".to_string(),
                values: vec![PruneValue::Text("c".into())],
            },
        ];
        let pred = PrunePredicate::In {
            column: "region".into(),
            values: vec![PruneValue::Text("a".into()), PruneValue::Text("c".into())],
        };
        let kept = prune_list(&spec_list(), &children, &pred);
        assert_eq!(kept, vec!["a", "c"]);
    }

    #[test]
    fn list_keeps_all_when_predicate_is_opaque() {
        let children = vec![ListChild {
            name: "x".to_string(),
            values: vec![PruneValue::Text("x".into())],
        }];
        let pred = PrunePredicate::Opaque;
        assert_eq!(prune_list(&spec_list(), &children, &pred), vec!["x"]);
    }

    #[test]
    fn hash_routes_to_single_partition_on_equality() {
        let children = vec![
            HashChild {
                name: "h0".to_string(),
                modulus: 4,
                remainder: 0,
            },
            HashChild {
                name: "h1".to_string(),
                modulus: 4,
                remainder: 1,
            },
            HashChild {
                name: "h2".to_string(),
                modulus: 4,
                remainder: 2,
            },
            HashChild {
                name: "h3".to_string(),
                modulus: 4,
                remainder: 3,
            },
        ];
        let pred = PrunePredicate::Compare {
            column: "user_id".into(),
            op: PruneOp::Eq,
            value: pint(42),
        };
        let kept = prune_hash(&spec_hash(), &children, &pred);
        assert_eq!(
            kept.len(),
            1,
            "hash eq must land on one partition: {kept:?}"
        );
    }

    #[test]
    fn hash_keeps_all_without_equality() {
        let children = vec![
            HashChild {
                name: "h0".to_string(),
                modulus: 2,
                remainder: 0,
            },
            HashChild {
                name: "h1".to_string(),
                modulus: 2,
                remainder: 1,
            },
        ];
        let pred = PrunePredicate::Compare {
            column: "user_id".into(),
            op: PruneOp::Gt,
            value: pint(0),
        };
        let kept = prune_hash(&spec_hash(), &children, &pred);
        assert_eq!(kept.len(), 2);
    }

    #[test]
    fn and_tightens_across_child_predicates() {
        let children = vec![
            range_child("c0", Some(0), Some(50)),
            range_child("c1", Some(50), Some(100)),
            range_child("c2", Some(100), Some(150)),
        ];
        let pred = PrunePredicate::And(vec![
            PrunePredicate::Compare {
                column: "ts".into(),
                op: PruneOp::GtEq,
                value: pint(60),
            },
            PrunePredicate::Compare {
                column: "ts".into(),
                op: PruneOp::Lt,
                value: pint(90),
            },
        ]);
        assert_eq!(prune_range(&spec_range(), &children, &pred), vec!["c1"]);
    }

    #[test]
    fn or_widens_across_child_predicates() {
        let children = vec![
            range_child("c0", Some(0), Some(100)),
            range_child("c1", Some(100), Some(200)),
            range_child("c2", Some(200), Some(300)),
        ];
        let pred = PrunePredicate::Or(vec![
            PrunePredicate::Compare {
                column: "ts".into(),
                op: PruneOp::Eq,
                value: pint(50),
            },
            PrunePredicate::Compare {
                column: "ts".into(),
                op: PruneOp::Eq,
                value: pint(250),
            },
        ]);
        let kept = prune_range(&spec_range(), &children, &pred);
        // Conservative OR widens to [50, 250] → touches all 3 chunks.
        assert_eq!(kept, vec!["c0", "c1", "c2"]);
    }
}