relate-executor 1.0.0

In-memory graph and Cypher physical executor
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
//! ADR-0468's first live operator chain: label scan, filter, and
//! projection as batch operators over [`Chunk`], engaged for scan-shaped
//! `MATCH` clauses (LABS-359 — the substrate's first consumer).
//!
//! The engagement gate ([`compile`]) is deliberately conservative: a
//! single unpatterned node over the one unbound seed row, a static label
//! conjunction, and `WHERE` conjuncts that each read one property of the
//! scan variable against a row-invariant, deterministic bound. Anything
//! it cannot prove it declines, and the dispatcher runs today's per-row
//! `match_clause` unchanged — same plan, same rows, so the openCypher TCK
//! is the net either way.
//!
//! The chain, pull-based inside the clause (ADR-0468's discipline):
//!
//! - the **scan operator** resolves candidates through the one existing
//!   funnel ([`node_candidates`], so index seeks, the PROFILE node-read
//!   counter, and the seek-phase stamp all keep working) and builds one
//!   [`Chunk`] per [`CHUNK_ROWS`] batch: the node binding plus one column
//!   per property the conjuncts read, encoded by the type layer's one
//!   encoder;
//! - the **filter operator** walks the compiled conjuncts over the chunk,
//!   one column at a time, consulting the null bitmap before every
//!   comparison (a null operand makes the comparison null — Cypher's
//!   three-valued logic, exactly what [`binary`] computes for a null
//!   operand — and a `WHERE` keeps only `true`). It keeps a selection
//!   vector, so a row an earlier conjunct dropped never reaches a later
//!   one — the per-row short-circuit `residual_ok` keeps, lazily
//!   including the bound's own evaluation;
//! - the **breaker** materializes: the surviving chunk compacts
//!   ([`Chunk::retain_rows`]), projects down to the clause's binding
//!   columns ([`Chunk::select_columns`]) — the scratch property columns a
//!   filter read stay behind — and decodes through the lossless
//!   [`Chunk::to_rows`]. `Row` stays the boundary representation; nothing
//!   but the binding columns ever leaves the clause.
//!
//! Zone maps on batches (LABS-645, ADR-0468 layer 2): every column's
//! construction derives its zone — null-presence, and for the typed
//! kinds the min/max over the non-null values — and the filter consults
//! it before walking a conjunct's rows. The metadata is DERIVED from the
//! batch's contents, never estimated: a skip decision must be a provable
//! implication of the predicate and the min/max ([`zone_proves_no_match`]
//! states each rule), because a wrong zone map is a correctness bug, not
//! a missed optimization. The skip IS the correct result — a conjunct
//! that provably holds for no value makes the survivor set empty — and
//! the skipped evaluation is counted (the `count_zone_skip` counter,
//! PROFILEd per clause as `zone_skips`). Two
//! boundaries, stated once: the zone is consulted only when a predicate
//! exists (a predicate-free path never reads it), and there are no bloom
//! filters — build+probe costs more than a plain small-batch scan (the
//! literature-anchored call, ADR-0468 layer 2). Top-K boundary pushdown
//! (an ORDER BY limit cut against a future sort's zone) is deliberately
//! NOT here: it needs the ORDER BY plumbing, and the extension point is
//! this filter's conjunct loop — a top-K bound is one more provable
//! no-match decision over the same zone facts.
//!
//! One ordering caveat, stated once here: the per-row path evaluates a
//! row's whole conjunct list before moving to the next row, while the
//! filter walks one conjunct across the batch. Both stop at the first
//! error, so a clause that errors errors either way — but a pathological
//! `WHERE` whose *first* conjunct errors on a late row and whose *second*
//! errors on an early one can surface the other conjunct's message. Every
//! other behavior — rows kept, rows dropped, nulls, error timing against
//! an empty survivor set — is the per-row path's, by construction.

use std::cmp::Ordering;

use relate_core::{NodeId, Result, Value};
use relate_ir::{var_prop_named, BinaryOp, Expr, MatchMode, NodePattern, PathPattern, UnaryOp};
use relate_names::{NamedToken, SmallName};

use crate::ctx::{ExecCtx, WalkStart};
use crate::eval::{binary_pure, eval};
use crate::overlay::ExecGraph;
use crate::pattern_walk::{label_index_seekable, node_candidates};
use crate::row::Row;

use super::agg_fold::is_pred_op;
use super::candidates::node_ok;
use super::chunk::{Chunk, Column, ColumnZone, CHUNK_ROWS};
use super::pushdown::{flatten_and_owned, match_scope, Residual};

/// The chunk's binding slot: column 0 carries the scan variable's node
/// binding, the predicate property columns follow. The breaker's
/// projection keeps exactly this slot — the clause's output is the seed
/// row's bindings (empty, by the gate) plus the pattern's binding.
const VAR_SLOT: usize = 0;

/// Run one scan-shaped `MATCH` clause as the batch chain, or decline it.
///
/// `Ok(None)` is the decline: the shape is not scan-shaped (or a test
/// hook forced the per-row path), and the caller runs its per-row
/// `match_clause`
/// unchanged. `Ok(Some(rows))` is the clause's output — the same rows,
/// order, and bindings the per-row path produces, materialized at the
/// breaker.
pub(crate) fn try_scan_filter_batch(
    ctx: ExecCtx<'_>,
    rows: &[Row],
    optional: bool,
    match_mode: MatchMode,
    patterns: &[PathPattern],
    where_clause: Option<&Expr>,
) -> Result<Option<Vec<Row>>> {
    // LABS-359 test hook: force the decline so a differential test can
    // pin the per-row path's answer on a shape the chain would take.
    #[cfg(test)]
    if batch_disabled() {
        return Ok(None);
    }
    let conjuncts = where_clause.map(flatten_and_owned).unwrap_or_default();
    let Some(plan) = compile(rows, optional, match_mode, patterns, &conjuncts) else {
        return Ok(None);
    };
    #[cfg(test)]
    batch_engaged();
    let ExecCtx {
        graph,
        params,
        cypher_version,
        ..
    } = ctx;
    // The walk-start fact (LABS-369) the per-row path records per walk:
    // this clause's only walk starts at the pattern's only element,
    // forward, the un-optimised default — recorded once, the way the
    // single driving row records it once.
    super::record_walk_start(WalkStart::seed_side_forward());
    let seed = &rows[0];
    let scope = match_scope(seed, patterns);
    let residual = Residual {
        conjuncts: &conjuncts,
        scope: &scope,
    };
    let candidates = node_candidates(
        graph,
        plan.node,
        seed,
        params,
        cypher_version,
        residual.conjuncts,
    )?;
    // Admission is the walker's own per-candidate check, unchanged: the
    // same `node_ok` call the per-row path makes per candidate, outside
    // the seek phase like it is there.
    let mut admitted: Vec<NodeId> = Vec::with_capacity(candidates.len());
    for id in candidates {
        if node_ok(graph, id, plan.node, seed, params, cypher_version)? {
            admitted.push(id);
        }
    }
    let mut out = Vec::with_capacity(admitted.len());
    for batch in admitted.chunks(CHUNK_ROWS) {
        graph.cancel_tick()?;
        let chunk = scan_chunk(graph, &plan, batch);
        let survivors = filter_chunk(ctx, &plan, &chunk)?;
        let kept = chunk.retain_rows(&survivors);
        let bound = kept.select_columns(&[VAR_SLOT]);
        out.extend(bound.to_rows());
    }
    Row::share_schemas(&mut out);
    Ok(Some(out))
}

/// The compiled engagement: the scan's subject, the binding column's
/// name, the scratch property columns, and the filter's predicates —
/// everything the chain needs at run time, derived once here.
struct BatchPlan<'a> {
    /// The lone node pattern — the scan operator's subject (its label
    /// conjunction drives [`node_candidates`] and [`node_ok`]).
    node: &'a NodePattern,
    /// The variable the pattern binds — the binding column's name.
    var: String,
    /// The distinct properties the conjuncts read, first-mention order —
    /// one scratch column each, at slot `1 + index`.
    keys: Vec<NamedToken>,
    /// The compiled `WHERE` conjuncts, in evaluation order.
    preds: Vec<BatchPred>,
}

/// One `WHERE` conjunct in the filter operator's vocabulary. The slot is
/// the chunk column the predicate reads — the binding column is slot 0,
/// each key in [`BatchPlan::keys`] follows at `1 + index`.
enum BatchPred {
    /// `<var>.<key> <op> <bound>`: the column compared against a
    /// row-invariant bound. The bound is evaluated lazily, once per
    /// chunk, at this conjunct's turn — the timing the per-row
    /// short-circuit keeps (an earlier conjunct that empties the batch
    /// never pays for it).
    Cmp {
        slot: usize,
        op: BinaryOp,
        bound: Expr,
    },
    /// `<var>.<key> IS [NOT] NULL` — answered from the column's null
    /// bitmap alone; no value is decoded and nothing can error.
    Null { slot: usize, is_null: bool },
}

/// The engagement gate. Every condition is a proof, not a heuristic: the
/// driving table is exactly the one unbound seed row (the statement's
/// first clause, a `NEXT` stage's unit seed), the pattern is one node
/// with no traversal and no GPM-only feature, and every flattened `WHERE`
/// conjunct compiles. A mid-pipeline clause — bindings carried, multiple
/// driving rows, an `OPTIONAL`, a traversal — keeps today's path.
fn compile<'a>(
    rows: &[Row],
    optional: bool,
    match_mode: MatchMode,
    patterns: &'a [PathPattern],
    conjuncts: &[Expr],
) -> Option<BatchPlan<'a>> {
    if rows.len() != 1 || !rows[0].is_empty() {
        return None;
    }
    if optional || match_mode != MatchMode::DifferentRelationships {
        return None;
    }
    let [path] = patterns else {
        return None;
    };
    if path.needs_gpm()
        || path.is_shortest_path_function
        || path.has_path_name()
        || !path.rels.is_empty()
        || path.nodes.len() != 1
    {
        return None;
    }
    let node = &path.nodes[0];
    let var = node.variable.as_ref()?;
    if node.properties.is_some() || node.where_clause.is_some() || !label_index_seekable(node) {
        return None;
    }
    let mut plan = BatchPlan {
        node,
        var: var.to_string(),
        keys: Vec::new(),
        preds: Vec::new(),
    };
    for conjunct in conjuncts {
        plan.preds
            .push(compile_pred(conjunct, var, &mut plan.keys)?);
    }
    Some(plan)
}

/// Compile one conjunct, appending any new scratch key to `keys`. `None`
/// declines the whole clause: the per-row path evaluates shapes this
/// vocabulary does not cover (functions over the row, `OR`, `NOT`,
/// comparisons of two properties, …), and declining keeps those answers
/// byte-identical.
fn compile_pred(expr: &Expr, var: &SmallName, keys: &mut Vec<NamedToken>) -> Option<BatchPred> {
    /// The chunk slot a predicate's key reads: an already-seen key keeps
    /// its column, a new one claims the next scratch slot.
    fn slot_of(key: NamedToken, keys: &mut Vec<NamedToken>) -> usize {
        // Compare by written name: one token per name within a plan.
        match keys.iter().position(|(name, _)| *name == key.0) {
            Some(index) => 1 + index,
            None => {
                keys.push(key);
                keys.len()
            }
        }
    }
    match expr {
        Expr::Unary { op, expr } => {
            let is_null = match op {
                UnaryOp::IsNull => true,
                UnaryOp::IsNotNull => false,
                _ => return None,
            };
            let (v, key) = var_prop_named(expr)?;
            if v != var {
                return None;
            }
            Some(BatchPred::Null {
                slot: slot_of(key, keys),
                is_null,
            })
        }
        Expr::Binary { op, left, right } if is_pred_op(*op) => {
            // The property must be the scan variable's, on the left: a
            // mirrored comparison declines (conservative, like the fold's
            // operand-asymmetric declines).
            let (v, key) = var_prop_named(left)?;
            if v != var {
                return None;
            }
            // The bound is row-invariant — no variables, no
            // nondeterministic call, no scope-bearing variant — the one
            // canonical proof (kernels/program.rs), so the filter
            // evaluates it once per chunk instead of per row.
            if !super::kernels::program::bound_is_row_invariant(right) {
                return None;
            }
            Some(BatchPred::Cmp {
                slot: slot_of(key, keys),
                op: *op,
                bound: (**right).clone(),
            })
        }
        _ => None,
    }
}

/// The scan operator: one chunk over a batch of admitted candidates —
/// the node binding at [`VAR_SLOT`], one scratch column per key, each
/// read through the store's canonical property accessor (the same
/// `node_prop` read the per-row `eval` of `<var>.<key>` makes).
fn scan_chunk(graph: &ExecGraph<'_>, plan: &BatchPlan<'_>, batch: &[NodeId]) -> Chunk {
    let mut binding: Vec<Value> = Vec::with_capacity(batch.len());
    let mut scratch: Vec<Vec<Value>> = plan
        .keys
        .iter()
        .map(|_| Vec::with_capacity(batch.len()))
        .collect();
    for &id in batch {
        binding.push(crate::pattern_walk::bind_node_stub(graph, id));
        for (key, column) in plan.keys.iter().zip(&mut scratch) {
            column.push(graph.node_prop_resolved(id, key).unwrap_or(Value::Null));
        }
    }
    let mut names: Vec<String> = Vec::with_capacity(1 + plan.keys.len());
    names.push(plan.var.clone());
    names.extend(plan.keys.iter().map(|(name, _)| name.to_string()));
    let mut columns: Vec<Column> = Vec::with_capacity(names.len());
    columns.push(Column::from_values(&binding));
    for column in &scratch {
        columns.push(Column::from_values(column));
    }
    Chunk::from_columns(&names, columns)
}

/// The filter operator: the chunk's surviving rows, as a selection
/// vector in row order. Each conjunct walks its column once — the zone
/// check first (LABS-645), then the rows; a row the conjunct drops is
/// gone before the next conjunct runs.
fn filter_chunk(ctx: ExecCtx<'_>, plan: &BatchPlan<'_>, chunk: &Chunk) -> Result<Vec<usize>> {
    let ExecCtx {
        graph,
        params,
        cypher_version,
        ..
    } = ctx;
    // The bound evaluates against a row with no bindings — it is
    // variable-free by the gate, so the row it cannot read is inert; this
    // is the seed-row evaluation the per-row path makes against each
    // matched row, minus the row it never reads.
    let seed = Row::new();
    let mut survivors: Vec<usize> = (0..chunk.len()).collect();
    for pred in &plan.preds {
        if survivors.is_empty() {
            break;
        }
        match pred {
            BatchPred::Null { slot, is_null } => {
                survivors = apply_null(chunk.column(*slot), *is_null, survivors);
            }
            BatchPred::Cmp { slot, op, bound } => {
                let bound_value = eval(graph, &seed, bound, params, cypher_version)?;
                if bound_value.is_null() {
                    // A null bound makes every comparison null, and a
                    // `WHERE` drops null — every row goes, here, without
                    // evaluating anything else.
                    survivors.clear();
                    continue;
                }
                survivors = apply_cmp(chunk.column(*slot), *op, &bound_value, survivors)?;
            }
        }
    }
    Ok(survivors)
}

/// The zone skip's one decision (LABS-645): whether `op` against `bound`
/// provably cannot hold for any non-null value of a column whose zone is
/// `zone`. Every answer is an implication of the engine's own comparison
/// semantics — the zone's bounds are `Value`s of the column's kind, so
/// [`binary`] and [`Value::compare`] decide over them exactly as the row
/// loop decides over the column's values, and a typed column's single
/// kind makes comparability uniform across its slots:
///
/// - the ordering operators are monotone over the totally ordered kind,
///   so `<`/`<=` hold for some value iff they hold at the minimum,
///   `>`/`>=` at the maximum; an incomparable pair (cross-kind) is null
///   for every slot uniformly, and a null comparison drops;
/// - `=` holds for some value iff the bound lies within the bounds — the
///   kind is totally ordered, so a bound outside them equals no value,
///   and cross-kind equality is false, so it holds for no value either;
/// - `<>` holds for no value only when both bounds fail it, which pins
///   every value between them to the bound.
///
/// `Ok(false)` for the ops the zone cannot speak to (`IN`, the string
/// predicates, `=~`) and for every column without bounds (the fallback,
/// a NaN-bearing Float buffer, no non-null value) — no facts, no skip,
/// never an estimate.
fn zone_proves_no_match(op: BinaryOp, zone: &ColumnZone, bound: &Value) -> Result<bool> {
    let Some((min, max)) = zone.min_max() else {
        return Ok(false);
    };
    Ok(match op {
        BinaryOp::Lt | BinaryOp::Lte => {
            binary_pure(op, min.clone(), bound.clone())?.truthy() != Some(true)
        }
        BinaryOp::Gt | BinaryOp::Gte => {
            binary_pure(op, max.clone(), bound.clone())?.truthy() != Some(true)
        }
        BinaryOp::Eq => {
            !(min.compare(bound).is_some_and(|o| o != Ordering::Greater)
                && max.compare(bound).is_some_and(|o| o != Ordering::Less))
        }
        BinaryOp::Neq => {
            binary_pure(op, min.clone(), bound.clone())?.truthy() != Some(true)
                && binary_pure(op, max.clone(), bound.clone())?.truthy() != Some(true)
        }
        _ => false,
    })
}

/// One `Cmp` conjunct's pass over the survivor set (LABS-645: the zone
/// check first). The zone describes the WHOLE chunk's column — an
/// over-approximation once an earlier conjunct selected rows — so a skip
/// stays a proof and a non-skip falls through to the row loop.
fn apply_cmp(
    column: &Column,
    op: BinaryOp,
    bound: &Value,
    survivors: Vec<usize>,
) -> Result<Vec<usize>> {
    if zone_proves_no_match(op, column.zone(), bound)? {
        // The skip IS the correct result: the conjunct holds for no
        // non-null value in the column, so the survivor set is empty and
        // this conjunct's row walk for the chunk never runs.
        super::count_zone_skip();
        return Ok(Vec::new());
    }
    let mut kept = Vec::with_capacity(survivors.len());
    for row in survivors {
        // The null bitmap first: a null property makes the comparison
        // null (three-valued), which the `WHERE` drops — the shortcut is
        // `binary`'s own null arm.
        if column.is_null(row) {
            continue;
        }
        let value = column.value_at(row);
        if binary_pure(op, value, bound.clone())?.truthy() == Some(true) {
            kept.push(row);
        }
    }
    Ok(kept)
}

/// One `IS [NOT] NULL` conjunct's pass over the survivor set (LABS-645:
/// the zone's null-presence fact answers the `IS NULL` direction — no
/// null slot anywhere in the column means no row can satisfy it).
fn apply_null(column: &Column, is_null: bool, survivors: Vec<usize>) -> Vec<usize> {
    if is_null && !column.zone().null_present() {
        // See `apply_cmp`: the provable no-match skip is the correct
        // empty result.
        super::count_zone_skip();
        return Vec::new();
    }
    survivors
        .into_iter()
        .filter(|&row| column.is_null(row) == is_null)
        .collect()
}

#[cfg(test)]
use std::cell::Cell;

#[cfg(test)]
thread_local! {
    /// LABS-359 test hook: force the decline, so a differential test can
    /// pin the per-row path's answer on a shape the chain would take.
    static BATCH_DISABLED: Cell<bool> = const { Cell::new(false) };
    /// LABS-359 test counter: how many clauses the chain took since the
    /// last take.
    static BATCH_ENGAGED: Cell<u32> = const { Cell::new(0) };
}

/// Whether the test hook forced the per-row path.
#[cfg(test)]
fn batch_disabled() -> bool {
    BATCH_DISABLED.with(Cell::get)
}

/// Count one engagement.
#[cfg(test)]
fn batch_engaged() {
    BATCH_ENGAGED.with(|c| c.set(c.get() + 1));
}

/// LABS-359 test helper: run `f` with the batch chain forced off (the
/// per-row path), restoring the flag afterwards — including on unwind,
/// so a panicking assertion inside the closure cannot leave the chain
/// disabled for every later test on the thread (the shared guard).
#[cfg(test)]
pub(crate) fn with_batch_disabled<R>(f: impl FnOnce() -> R) -> R {
    super::test_support::with_flag_disabled(&BATCH_DISABLED, f)
}

/// LABS-359 test helper: drain the engagement counter (0 = the per-row
/// path took every scan-shaped clause since the last take).
#[cfg(test)]
pub(crate) fn take_batch_engagements() -> u32 {
    BATCH_ENGAGED.with(|c| c.replace(0))
}

/// LABS-645's generated-batch contracts. The filter's conjunct step is
/// exercised directly over generated columns — the private helpers the
/// `filter_chunk` orchestration reads — because the differential's
/// ground truth is the per-row evaluation itself (`binary` over every
/// slot), which no query-level fixture can state row by row.
#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use proptest::collection::vec;
    use proptest::prelude::*;

    use super::*;
    use crate::test_support::config_with_floor;

    /// The comparison vocabulary the zone can decide (LABS-645): the six
    /// operators over the typed kinds.
    fn pred_op() -> BoxedStrategy<BinaryOp> {
        prop_oneof![
            Just(BinaryOp::Eq),
            Just(BinaryOp::Neq),
            Just(BinaryOp::Lt),
            Just(BinaryOp::Gt),
            Just(BinaryOp::Lte),
            Just(BinaryOp::Gte),
        ]
        .boxed()
    }

    /// A scalar bound from anywhere — matching kinds and mismatching
    /// ones (the cross-kind rules), NaN included (the bound side's own
    /// dead end: `binary` answers false or null for it, never true).
    fn bound() -> BoxedStrategy<Value> {
        prop_oneof![
            4 => any::<i64>().prop_map(Value::Int),
            3 => prop_oneof![
                7 => any::<f64>().prop_filter("finite", |f| f.is_finite()),
                1 => Just(f64::NAN),
            ]
            .prop_map(Value::Float),
            3 => "[a-zäöü]{0,6}".prop_map(|s| Value::String(Arc::from(s))),
            2 => any::<bool>().prop_map(Value::Bool),
        ]
        .boxed()
    }

    /// One generated typed column — a single kind's values with nulls
    /// weighted, so the one encoder claims a typed buffer (the zone's
    /// subject). Floats carry NaN sometimes: the bounds-voiding rule
    /// must be generated, not just pinned.
    fn typed_column() -> BoxedStrategy<Vec<Value>> {
        prop_oneof![
            4 => vec(
                prop_oneof![
                    1 => Just(Value::Null),
                    6 => any::<i64>().prop_map(Value::Int),
                ],
                0..=48,
            ),
            3 => vec(
                prop_oneof![
                    1 => Just(Value::Null),
                    6 => prop_oneof![
                        7 => any::<f64>().prop_filter("finite", |f| f.is_finite()),
                        1 => Just(f64::NAN),
                    ]
                    .prop_map(Value::Float),
                ],
                0..=48,
            ),
            3 => vec(
                prop_oneof![
                    1 => Just(Value::Null),
                    6 => "[a-zäöü]{0,6}".prop_map(|s| Value::String(Arc::from(s))),
                ],
                0..=48,
            ),
            2 => vec(
                prop_oneof![
                    1 => Just(Value::Null),
                    6 => any::<bool>().prop_map(Value::Bool),
                ],
                0..=48,
            ),
        ]
        .boxed()
    }

    /// The ground truth: the full row-by-row filter — every non-null
    /// slot through the shared `binary`, exactly what the per-row path
    /// evaluates. The comparison operators cannot error (their `binary`
    /// arms return values), so the expect states an invariant.
    fn full_filter(values: &[Value], op: BinaryOp, bound: &Value) -> Vec<usize> {
        values
            .iter()
            .enumerate()
            .filter(|(_, value)| {
                !(*value).is_null()
                    && binary_pure(op, (*value).clone(), bound.clone())
                        .expect("comparison operators cannot error")
                        .truthy()
                        == Some(true)
            })
            .map(|(row, _)| row)
            .collect()
    }

    proptest! {
        #![proptest_config(config_with_floor(1000))]

        /// The no-false-negative contract, 1000+ random batches: random
        /// data of every typed kind, random nulls, random comparison
        /// predicates over matching and mismatching bounds — the
        /// zone-skipped filter's output equals the full filter's,
        /// exactly.
        #[test]
        fn zone_skipped_filtering_equals_full_filtering(
            (values, op, bound) in (typed_column(), pred_op(), bound()),
        ) {
            let column = Column::from_values(&values);
            let zoned = apply_cmp(&column, op, &bound, (0..values.len()).collect())
                .expect("comparison operators cannot error");
            prop_assert_eq!(zoned, full_filter(&values, op, &bound));
        }
    }

    proptest! {
        #![proptest_config(config_with_floor(1000))]

        /// The null-presence fact's contract, 1000+ random batches:
        /// `IS NULL` over a column with no null slot provably empties
        /// the set, and every other case is the bitmap's own answer —
        /// equality with the full per-row filter.
        #[test]
        fn zone_null_skips_equal_full_filtering(
            (values, is_null) in (typed_column(), any::<bool>()),
        ) {
            let column = Column::from_values(&values);
            let zoned = apply_null(&column, is_null, (0..values.len()).collect());
            let full: Vec<usize> = values
                .iter()
                .enumerate()
                .filter(|(_, value)| value.is_null() == is_null)
                .map(|(row, _)| row)
                .collect();
            prop_assert_eq!(zoned, full);
        }
    }
}