relate-executor 1.5.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
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
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
//! The Vortex property store (ADR-0665), behind the default-off
//! `vortex-store` gate (ADR-0703). This is the store ADR-0665 measured; the
//! measurement harness — the kernels, the A/B driver, the epoch probes —
//! stays in `testing/relate-vortex-lab` and drives this module.
//!
//! A node-major nullable-column store: the row index is the node id, one
//! column per promoted property, read through the executor's
//! [`PropStore`](super::prop_store::PropStore) seam for point reads, and
//! dispatched to by the executor for two lookup shapes:
//!
//! - the equality seek — the sorted `(key,row)` side-file that replaces the
//!   ADR-0614 lazy equality postings ([`VortexStore::seek`]);
//! - the `IN` member — one galloping pass over the same side-file
//!   ([`VortexStore::member`]).
//!
//! The label scan never routes here: the measured ordering keeps it on the
//! canonical label route (ADR-0665), so the store carries no label arm.
//!
//! The decoded cache keeps each touched column decoded whole — one flat,
//! row-indexed `OnceLock<Arc<Vec<Option<Value>>>>` already in the seam's
//! own shape (the `colstore` `ints()` shape the seam research measured);
//! a point hit is one locate and one clone, the same work the compact
//! records' `prop_by_id` pays.
//!
//! The read path's discipline: one relaxed atomic load while no publication
//! has appended (the unwritten fast path pays nothing), then one read-lock
//! acquisition plus an `Arc` clone per read once a delta exists, and the
//! commit-time write lock under the publication close. The per-column
//! decoded caches stay fill-once `OnceLock`s — the lock-free shape holds
//! there. The store is `Send + Sync`; the reader runtime is the fork's
//! [`CurrentThreadRuntime`] (`Arc<SmolExecutor>`, `Send` by construction).
//!
//! The store lived in the lab while the Vortex dependency sat outside the
//! workspace (ADR-0677); with the dependency vetted, the store is product
//! code behind the gate, and the lab is the harness that measures it.

use std::sync::atomic::{AtomicBool, AtomicU64, Ordering as AtomicOrdering};
use std::sync::{Arc, OnceLock, RwLock};

use relate_core::{Error, NodeId, Result, Value};
use relate_ir::BinaryOp;
use rustc_hash::FxHashMap;

use vortex::array::VortexSessionExecute;
use vortex::file::VortexFile;
use vortex::io::runtime::current::CurrentThreadRuntime;
use vortex::scalar::{PValue, Scalar, ScalarValue};
use vortex::session::VortexSession;

use super::prop_store::{PropFold, PropStore, PublishedRow};
use super::Graph;

mod build;
mod columns;
mod side;

use build::{columnar_table, gather, row_space, seal_table, Gathered};
use columns::{chunks, projected};
use side::{equal_range, member_rows, side_index};

/// One property value the store carries. The derived order is the sort
/// total the side-file needs: Null first, then ints, then strings (the
/// variant list is already in that order).
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub(crate) enum Val {
    Null,
    Int(i64),
    Str(String),
}

/// The engine value for a stored one (`Val::Null` = absent).
pub(crate) fn to_value(val: Val) -> Option<Value> {
    match val {
        Val::Null => None,
        Val::Int(int) => Some(Value::Int(int)),
        Val::Str(string) => Some(Value::String(std::sync::Arc::from(string))),
    }
}

/// A lookup value in the store's `Val` shape; `None` for a type the store
/// does not promote (the lookup then falls through to the index route).
pub(crate) fn promoted_val(value: &Value) -> Option<Val> {
    match value {
        Value::Int(int) => Some(Val::Int(*int)),
        Value::String(string) => Some(Val::Str(string.to_string())),
        _ => None,
    }
}

/// A decoded scalar in the store's `Val` shape. The columns carry only
/// ints and strings (the promoted set [`promoted_val`] admits), so any
/// other scalar is a broken column — a typed error, never a panic on the
/// read path and never silently answered as null: a null here is a live
/// property disappearing from results (the no-silent-fallbacks rule).
pub(crate) fn decoded_val(scalar: &Scalar) -> Result<Val> {
    match scalar.value() {
        None => Ok(Val::Null),
        Some(ScalarValue::Primitive(PValue::I64(v))) => Ok(Val::Int(*v)),
        Some(ScalarValue::Primitive(PValue::I32(v))) => Ok(Val::Int(*v as i64)),
        Some(ScalarValue::Utf8(text)) => Ok(Val::Str(text.as_str().to_owned())),
        Some(other) => Err(Error::storage(format!(
            "a decoded {other:?} is outside the promoted column set — a broken column"
        ))),
    }
}

/// A dense row-id set — the per-label filter and the visibility set.
#[derive(Clone)]
struct BitSet {
    words: Vec<u64>,
    len: usize,
}

impl BitSet {
    fn new(len: usize) -> Self {
        Self {
            words: vec![0; len.div_ceil(64)],
            len,
        }
    }

    /// `row < len` by construction: every caller walks ids below the
    /// `row_count` the bitset was sized to (`gather`).
    fn set(&mut self, row: usize) {
        self.words[row / 64] |= 1 << (row % 64);
    }

    fn get(&self, row: usize) -> bool {
        row < self.len && (self.words[row / 64] >> (row % 64)) & 1 == 1
    }

    fn intersect(&self, other: &BitSet) -> BitSet {
        BitSet {
            words: self
                .words
                .iter()
                .zip(&other.words)
                .map(|(a, b)| a & b)
                .collect(),
            len: self.len.min(other.len),
        }
    }
}

/// One column's decoded values, row-indexed directly, filled once by the
/// first reader to touch the column — the `colstore` `ints()` shape the
/// seam research measured. The decode memoizes the seam's own
/// `Option<Value>` shape, so a point hit is one locate plus one clone, the
/// same work the compact records' `prop_by_id` pays (ADR-0508's memo
/// lesson: convert once at fill, never per read).
type ColumnValues = Arc<Vec<Option<Value>>>;

/// One column's fill-once decoded state, shared across reader threads
/// (memory table 2). The hit path is one acquire load and a cold race
/// decodes once — no lock ever reaches a reader. The cached value is a
/// `Result`: a decode failure is cached as the column's decline (the
/// compact records keep answering) instead of retrying per read.
#[derive(Default)]
struct ColumnCache {
    values: OnceLock<Result<ColumnValues>>,
}

/// One key's gathered column before the file is sealed. The variant is the
/// column's type, so a value of the other type cannot reach a typed builder.
#[derive(Default)]
enum ColumnState {
    /// No value seen yet: the first value decides the kind.
    #[default]
    Empty,
    Int(Vec<Option<i64>>),
    Str(Vec<Option<String>>),
    /// A value the prototype cannot carry, or two promoted kinds for one
    /// key: the key is not a faithful column and stays off the store.
    Poisoned,
}

impl ColumnState {
    /// Record one property value at `row`, poisoning the key when the value
    /// does not fit the column's kind.
    fn push(&mut self, row: usize, value: &Value, row_count: usize) {
        let next = match (std::mem::replace(self, ColumnState::Poisoned), value) {
            (ColumnState::Poisoned, _) => ColumnState::Poisoned,
            (ColumnState::Empty, Value::Int(int)) => {
                let mut values = vec![None; row_count];
                values[row] = Some(*int);
                ColumnState::Int(values)
            }
            (ColumnState::Empty, Value::String(string)) => {
                let mut values = vec![None; row_count];
                values[row] = Some(string.to_string());
                ColumnState::Str(values)
            }
            (ColumnState::Int(mut values), Value::Int(int)) => {
                values[row] = Some(*int);
                ColumnState::Int(values)
            }
            (ColumnState::Str(mut values), Value::String(string)) => {
                values[row] = Some(string.to_string());
                ColumnState::Str(values)
            }
            // an unpromotable value or a second kind poisons the key
            (_, _) => ColumnState::Poisoned,
        };
        *self = next;
    }
}

/// One key's column: its gathered state and its decoded cache.
#[derive(Default)]
struct Column {
    state: ColumnState,
    cache: ColumnCache,
}

/// The node-major columnar store.
/// One committed version of one row since the base snapshot — the
/// delta-overlay tier (ADR-0665 versioning step 1): sparse, versioned,
/// appended by the publication feed, read by newest-≤-`ts`. `props` is
/// the row's FULL state for the keys the store carries (a key absent
/// here is absent at `ts` — the row state, not a patch).
#[derive(Clone)]
struct DeltaVersion {
    ts: u64,
    /// The row's labels at `ts` — carried by the feed, read by the set
    /// arms' overlay rung (step 2); step 1's point path does not read it.
    #[allow(dead_code)]
    labels: Vec<u32>,
    props: FxHashMap<u32, Option<Value>>,
    deleted: bool,
}

/// The rows changed since the base snapshot, each with its committed
/// versions in ascending `ts` — the sparse overlay the publication feed
/// fills and every read resolves before falling to the columnar base.
/// Unbounded until the seal step (the design's next rung) — a
/// prototype-scale bound, noted in ADR-0665. One `Arc` per row, so a
/// publication clones the map shallowly and deep-copies only the rows the
/// commit touched.
#[derive(Default, Clone)]
struct Delta {
    rows: FxHashMap<NodeId, Arc<Vec<DeltaVersion>>>,
}

pub struct VortexStore {
    session: VortexSession,
    /// The fork's shared-executor runtime: `Arc<SmolExecutor>`, `Send + Sync`,
    /// and the runtime the scan machinery spawns onto.
    rt: CurrentThreadRuntime,
    vfile: VortexFile,
    /// The base snapshot this store answers FROM: the columnar tier's
    /// build time. Reads at any `ts >= base` are answered (the delta
    /// overlay resolves the rows commits changed); a read at an earlier
    /// `ts` declines to the compact records.
    ts: u64,
    /// The rows commits changed since `ts` — copy-on-write per
    /// publication (the delta is small; readers load one `Arc` with a
    /// single lock acquisition and never lock again inside a read).
    delta: RwLock<Arc<Delta>>,
    /// Whether any publication has appended a delta version: the one fact
    /// the empty-path fast checks read. Clear = no version exists, so the
    /// base columns are still exact and a read pays one acquire load, no
    /// lock; set = the set/aggregate arms decline and `delta()` locks.
    delta_present: AtomicBool,
    row_count: usize,
    /// Rows that are visible live nodes at the snapshot (gaps excluded).
    visible: BitSet,
    /// Per-label dense bitsets over rows.
    label_bits: FxHashMap<u32, BitSet>,
    /// Promoted columns by interned key id, each with its fill-once cache.
    columns: FxHashMap<u32, Column>,
    /// The sorted `(key,row)` side-file, per interned key: `(value, row)` in
    /// value order.
    side: FxHashMap<u32, Vec<(Val, NodeId)>>,
    /// The lookup arms served, for the dispatch test.
    served: Served,
}

/// How many lookups each dispatch arm served — one counter per rung, so a
/// test names the arm it asserts.
#[derive(Default)]
struct Served {
    seek: AtomicU64,
    member: AtomicU64,
    folds: AtomicU64,
    grouped: AtomicU64,
}

impl std::fmt::Debug for VortexStore {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("VortexStore")
            .field("row_count", &self.row_count)
            .field("columns", &self.columns.len())
            .finish_non_exhaustive()
    }
}

impl VortexStore {
    /// Step 1's one decline gate: once a delta version exists, the
    /// side-file and columns reflect the base tier alone — every set and
    /// aggregate arm declines (merging the overlay's contributions into
    /// them is the design's next rung, ADR-0665).
    fn declines_step_one(&self) -> bool {
        self.delta_present.load(AtomicOrdering::Acquire)
    }

    /// The lookup arms served so far — the dispatch test's assertion seam:
    /// `(seek, member, folds, grouped)`, one counter per rung.
    pub fn served(&self) -> (u64, u64, u64, u64) {
        (
            self.served.seek.load(AtomicOrdering::Relaxed),
            self.served.member.load(AtomicOrdering::Relaxed),
            self.served.folds.load(AtomicOrdering::Relaxed),
            self.served.grouped.load(AtomicOrdering::Relaxed),
        )
    }

    /// Encode the graph at `ts` node-major and open it, building the label
    /// bitsets and the sorted side-file in the same pass. Admits the
    /// graph's row space first — [`row_space`] owns that refusal.
    pub fn from_graph(graph: &Graph, ts: u64) -> Result<Self> {
        let ids = graph.all_node_ids_at(ts);
        let row_count = row_space(&ids)?;
        let Gathered {
            visible,
            label_bits,
            mut columns,
        } = gather(graph, ts, ids, row_count)?;
        // A poisoned key (an unpromotable value, or two promoted kinds) is not
        // a faithful column: reading it from the store would answer null for
        // the skipped values, so it stays off the store entirely and
        // [`PropStore::carries`] keeps the compact records answering for it.
        columns.retain(|_, column| !matches!(column.state, ColumnState::Poisoned));
        let array = columnar_table(&columns, row_count)?;
        let (session, rt, vfile) = seal_table(array)?;
        let side = side_index(&mut columns);
        Ok(Self {
            session,
            rt,
            vfile,
            ts,
            delta: RwLock::new(Arc::new(Delta::default())),
            delta_present: AtomicBool::new(false),
            row_count,
            visible,
            label_bits,
            columns,
            side,
            served: Served::default(),
        })
    }

    /// The delta overlay, if any publication has appended since the base
    /// snapshot. One relaxed-path load when nothing has, else one lock
    /// acquisition and one `Arc` clone — the read never locks twice.
    fn delta(&self) -> Option<Arc<Delta>> {
        if !self.declines_step_one() {
            return None;
        }
        Some(self.delta.read().unwrap().clone())
    }

    /// The newest delta version of `id` at or before `ts`.
    fn delta_version(delta: &Delta, id: NodeId, ts: u64) -> Option<&DeltaVersion> {
        delta
            .rows
            .get(&id)?
            .iter()
            .rev()
            .find(|version| version.ts <= ts)
    }

    /// Equality seek over the sorted `(key,row)` side-file: every row whose
    /// value equals `val`, in row order — the shared run resolution.
    fn seek(&self, kid: u32, val: &Val) -> Vec<NodeId> {
        let Some(entries) = self.side.get(&kid) else {
            return Vec::new();
        };
        let (start, len) = equal_range(entries, 0, val);
        entries[start..start + len]
            .iter()
            .map(|(_, row)| *row)
            .collect()
    }

    /// Batched key check: rows whose value is any of `sorted_vals` (sorted
    /// ascending) — the shared galloping member over the side-file, the
    /// #9551 shape.
    fn member(&self, kid: u32, sorted_vals: &[Val]) -> Vec<NodeId> {
        let Some(entries) = self.side.get(&kid) else {
            return Vec::new();
        };
        member_rows(entries, sorted_vals)
    }

    /// The rows carrying every label: the reduce's `None` (no labels) is the
    /// unconstrained scan, the visible set; an uninterned label intersects
    /// with an empty set, so it matches nothing.
    fn label_candidates(&self, labels: &[u32]) -> BitSet {
        labels
            .iter()
            .map(|lid| {
                self.label_bits
                    .get(lid)
                    .cloned()
                    .unwrap_or_else(|| BitSet::new(self.row_count))
            })
            .reduce(|acc, bits| acc.intersect(&bits))
            .unwrap_or_else(|| self.visible.clone())
    }

    /// One column's decoded values, filling the cache once on first touch —
    /// the one lazy-decode resolve every read goes through, so a key the
    /// store does not carry answers `None` in one place. A cached decode
    /// failure answers `None` here; `carries` reads the same cached
    /// `Result` non-initializing, so the key stops being carried from the
    /// next read on, and the discovering read falls through to the
    /// compact records (`Graph::point`'s miss path).
    fn column_values(&self, kid: u32) -> Option<&ColumnValues> {
        let cache = self.columns.get(&kid)?;
        cache
            .cache
            .values
            .get_or_init(|| self.decode_column(kid))
            .as_ref()
            .ok()
    }

    /// The aggregate arms' shared resolve: the decoded column and the
    /// label's candidate rows. A key the store does not carry declines
    /// the shape.
    fn fold_inputs(&self, labels: &[u32], kid: u32) -> Option<(&[Option<Value>], BitSet)> {
        let values = self.column_values(kid)?;
        Some((values.as_slice(), self.label_candidates(labels)))
    }

    /// The value at `(kid,row)` through the decoded cache: the flat
    /// column-indexed values (filled once by the first reader to touch the
    /// column), already in the seam's `Option<Value>` shape — a hit is one
    /// locate plus one clone, the row store's `prop_by_id` shape.
    fn value_at(&self, kid: u32, row: usize) -> Option<Value> {
        if row >= self.row_count {
            return None;
        }
        self.column_values(kid)?.get(row).cloned().flatten()
    }

    /// One column decoded whole into its flat row-indexed values, filled
    /// once by whichever reader arrives first. The one-time decode also
    /// pays the `Val`→`Option<Value>` conversion, so no hit ever converts
    /// (first-touch cost: the column's compressed projections materialize
    /// and every chunk runs `repeated_probe` — read-heavy columns pay it
    /// once and hit forever). A decode failure — the store's own file
    /// unreadable, or a scalar outside the promoted set — is cached as
    /// the column's decline: the read falls through to the compact
    /// records (the poisoned-key policy's read-side twin), never a
    /// panic on the user read path.
    fn decode_column(&self, kid: u32) -> Result<ColumnValues> {
        let name = column_name(kid);
        // The crate's one single-column read.
        let field = projected(&self.rt, &self.vfile, &name)?;
        let chunks = chunks(&field);
        let total: usize = chunks.iter().map(|chunk| chunk.len()).sum();
        let mut values = Vec::with_capacity(total);
        for chunk in &chunks {
            let mut ctx = self.session.create_execution_ctx();
            let mut probe = chunk.repeated_probe();
            for row in 0..chunk.len() {
                let scalar = probe.execute_scalar(row, &mut ctx).map_err(vortex_error)?;
                values.push(to_value(decoded_val(&scalar)?));
            }
        }
        Ok(Arc::new(values))
    }
}

/// Build the columnar store from `graph` at its commit timestamp and install
/// it through [`Graph::attach_prop_store`]. The graph must be quiescent: the
/// store is built from its state at attach time. First attach wins, so an
/// already-attached graph is an explicit error, never a silent drop.
#[doc(hidden)]
pub fn attach_vortex_store(graph: &Graph) -> Result<()> {
    let store: Arc<dyn PropStore> = Arc::new(VortexStore::from_graph(graph, graph.commit_ts())?);
    if !graph.attach_prop_store(store) {
        return Err(Error::storage(
            "a property store is already attached to this graph",
        ));
    }
    Ok(())
}

impl PropStore for VortexStore {
    fn point(&self, ts: u64, id: NodeId, kid: u32) -> Option<Value> {
        // The delta overlay first (ADR-0665 versioning step 1): a row a
        // commit changed resolves from its newest version at or before
        // `ts` — absent rows answer `None`, present ones read the
        // version's full state for the key. The base tier below is one
        // flat column index and one clone (the decode memoizes the
        // seam's own shape, the colstore `ints()` shape the seam
        // research measured); an unchanged row never reaches the delta.
        if let Some(delta) = self.delta() {
            if let Some(version) = Self::delta_version(&delta, id, ts) {
                return if version.deleted {
                    None
                } else {
                    version.props.get(&kid).cloned().flatten()
                };
            }
        }
        // No version at or before `ts`: the row reads as it was at the
        // base — a base row falls through to its column, a row the base
        // never held (created later) answers absent, which the column
        // read's own row bound settles.
        self.value_at(kid, id as usize)
    }

    fn point_batch(&self, ts: u64, ids: &[NodeId], kid: u32, out: &mut [Option<Value>]) {
        assert_eq!(
            ids.len(),
            out.len(),
            "ids and out lengths must match for point_batch"
        );
        let Some(col) = self.column_values(kid) else {
            out.fill(None);
            return;
        };
        if let Some(delta) = self.delta() {
            for (i, &id) in ids.iter().enumerate() {
                if let Some(version) = Self::delta_version(&delta, id, ts) {
                    out[i] = if version.deleted {
                        None
                    } else {
                        version.props.get(&kid).cloned().flatten()
                    };
                } else {
                    let row = id as usize;
                    out[i] = if row < self.row_count {
                        col.get(row).cloned().flatten()
                    } else {
                        None
                    };
                }
            }
        } else {
            for (i, &id) in ids.iter().enumerate() {
                let row = id as usize;
                out[i] = if row < self.row_count {
                    col.get(row).cloned().flatten()
                } else {
                    None
                };
            }
        }
    }

    fn filter_range(
        &self,
        ts: u64,
        ids: &[NodeId],
        kid: u32,
        op: BinaryOp,
        lit: &Value,
        mask: &mut [u64],
    ) -> Option<()> {
        if !relate_ir::is_pred_op(op) {
            return None;
        }
        if lit.is_null() {
            let full_words = ids.len() / 64;
            for item in mask.iter_mut().take(full_words) {
                *item = 0;
            }
            let rem = ids.len() % 64;
            if rem > 0 {
                mask[full_words] &= !((1u64 << rem) - 1);
            }
            return Some(());
        }
        let col = self.column_values(kid)?;
        let delta = self.delta();

        // Atomic None semantics: write into local copy, only commit to mask on success.
        let mut working = mask.to_vec();

        for (i, &id) in ids.iter().enumerate() {
            let word_idx = i / 64;
            let bit = i % 64;
            if word_idx >= working.len() || (working[word_idx] & (1u64 << bit)) == 0 {
                continue;
            }
            let val = if let Some(ref delta) = delta {
                if let Some(version) = Self::delta_version(delta, id, ts) {
                    if version.deleted {
                        None
                    } else {
                        version.props.get(&kid).and_then(|v| v.as_ref())
                    }
                } else {
                    let row = id as usize;
                    if row < self.row_count {
                        col.get(row).and_then(|v| v.as_ref())
                    } else {
                        None
                    }
                }
            } else {
                let row = id as usize;
                if row < self.row_count {
                    col.get(row).and_then(|v| v.as_ref())
                } else {
                    None
                }
            };
            let matches = match val {
                Some(v) => {
                    let result = crate::eval::binary_pure(op, v.clone(), lit.clone()).ok()?;
                    crate::eval::predicate_value_holds(&result).ok()?
                }
                None => false,
            };
            if !matches {
                working[word_idx] &= !(1u64 << bit);
            }
        }
        mask.copy_from_slice(&working);
        Some(())
    }

    fn carries(&self, kid: u32) -> bool {
        // Lock-free, from the one cell that holds the fact: an unpopulated
        // cell is carried (nothing has failed yet), a cached `Ok` is
        // carried, a cached `Err` — a broken column — is not, so every
        // later read of the key falls through to the compact records.
        self.columns
            .get(&kid)
            .is_some_and(|column| column.cache.values.get().is_none_or(Result::is_ok))
    }

    fn serves_at(&self, ts: u64) -> bool {
        // The base snapshot answers FROM its build time: the delta
        // overlay resolves every commit since, so any `ts >= base` is
        // served. An earlier `ts` (a reader pinned before the store was
        // built) declines to the compact records.
        ts >= self.ts
    }

    fn equality(
        &self,
        labels: &[u32],
        kid: u32,
        values: &[Value],
        cap: Option<usize>,
    ) -> Option<Vec<NodeId>> {
        // The set arms decline behind step 1's one gate.
        if self.declines_step_one() {
            return None;
        }
        // A value the store does not promote cannot be answered: fall through
        // to the index route.
        let vals: Vec<Val> = values.iter().map(promoted_val).collect::<Option<_>>()?;
        let candidates = self.label_candidates(labels);
        let mut rows = if let [val] = vals.as_slice() {
            self.served.seek.fetch_add(1, AtomicOrdering::Relaxed);
            self.seek(kid, val)
        } else {
            let mut sorted = vals;
            sorted.sort_unstable();
            sorted.dedup();
            self.served.member.fetch_add(1, AtomicOrdering::Relaxed);
            self.member(kid, &sorted)
        };
        rows.retain(|row| candidates.get(*row as usize));
        rows.sort_unstable();
        rows.truncate(cap.map_or(usize::MAX, |cap| cap + 1));
        Some(rows)
    }

    fn folds(&self, labels: &[u32], kid: u32, wants: &[PropFold]) -> Option<Vec<Value>> {
        if self.declines_step_one() {
            return None;
        }
        let (values, rows) = self.fold_inputs(labels, kid)?;
        self.served.folds.fetch_add(1, AtomicOrdering::Relaxed);
        Some(compute_folds(present_values(values, &rows), wants))
    }

    fn grouped_counts(&self, labels: &[u32], kid: u32) -> Option<Vec<(Value, u64)>> {
        if self.declines_step_one() {
            return None;
        }
        let (values, rows) = self.fold_inputs(labels, kid)?;
        self.served.grouped.fetch_add(1, AtomicOrdering::Relaxed);
        Some(compute_grouped_counts(present_values(values, &rows)))
    }

    fn folds_masked(
        &self,
        _ts: u64,
        ids: &[NodeId],
        mask: &[u64],
        kid: u32,
        wants: &[PropFold],
    ) -> Option<Vec<Value>> {
        if self.declines_step_one() {
            return None;
        }
        let values = self.column_values(kid)?.as_slice();
        self.served.folds.fetch_add(1, AtomicOrdering::Relaxed);
        let iter = masked_values(values, ids, mask, self.row_count);
        Some(compute_folds(iter, wants))
    }

    fn grouped_counts_masked(
        &self,
        _ts: u64,
        ids: &[NodeId],
        mask: &[u64],
        kid: u32,
    ) -> Option<Vec<(Value, u64)>> {
        if self.declines_step_one() {
            return None;
        }
        let values = self.column_values(kid)?.as_slice();
        self.served.grouped.fetch_add(1, AtomicOrdering::Relaxed);
        let iter = masked_values(values, ids, mask, self.row_count);
        Some(compute_grouped_counts(iter))
    }

    fn on_publication(&self, ts: u64, rows: &[PublishedRow]) {
        // Copy-on-write per publication, the canonical `Arc::make_mut`
        // shape: a reader that cloned the `Arc` under the read lock before
        // this write lock keeps its snapshot, so the clone is shallow (one
        // `Arc` bump per changed row) and only the rows this commit touched
        // get their version vector copied — a commit pays for its own rows,
        // not the session's changed-row history. The map clone itself is
        // still O(changed-rows-total) — known debt the seal rung
        // (ADR-0665's versioning step 3) compacts. Every reader keeps
        // loading one `Arc` — no reader ever locks twice. Versions append
        // in publication order; `ts` is monotonic by the feed's contract.
        // Rows the store does not cover (ids and keys its columns never
        // carried) still land: a later promotion or the seal step reads
        // them, and an uncovered key's overlay simply holds `None`.
        //
        // ONE write guard covers clone, append, swap and the flag set: a
        // reader's empty-path check can never land between an installed
        // `Arc` and the stale flag and miss the versions already in it.
        // Observable states are only "no publication" and "publication
        // fully applied", and two publications serialize instead of one
        // clobbering the other's versions.
        let mut guard = self.delta.write().unwrap();
        let delta = Arc::make_mut(&mut *guard);
        for row in rows {
            let versions = Arc::make_mut(delta.rows.entry(row.id).or_default());
            let mut props: FxHashMap<u32, Option<Value>> = FxHashMap::default();
            if !row.deleted {
                for (kid, value) in &row.props {
                    props.insert(*kid, Some(value.clone()));
                }
            }
            versions.push(DeltaVersion {
                ts,
                labels: if row.deleted {
                    Vec::new()
                } else {
                    row.labels.clone()
                },
                props,
                deleted: row.deleted,
            });
        }
        // The one fact the flag carries is "a version exists": a publication
        // that appends none leaves the base columns exact, so the fast paths
        // stay valid.
        if !rows.is_empty() {
            self.delta_present.store(true, AtomicOrdering::Release);
        }
    }
}

/// The aggregation arms' shared walk: each candidate row's carried cell, in
/// row order. Skips rows the label bitset excludes and absent (null) cells —
/// the one home for the "present values" policy the folds and the dict-native
/// distinct both read.
fn present_values<'a>(
    values: &'a [Option<Value>],
    rows: &'a BitSet,
) -> impl Iterator<Item = &'a Value> + 'a {
    values
        .iter()
        .enumerate()
        .filter(move |(row, _)| rows.get(*row))
        .filter_map(|(_, value)| value.as_ref())
}

fn masked_values<'a>(
    values: &'a [Option<Value>],
    ids: &'a [NodeId],
    mask: &'a [u64],
    row_count: usize,
) -> impl Iterator<Item = &'a Value> + 'a {
    ids.iter().enumerate().filter_map(move |(i, &id)| {
        let word_idx = i / 64;
        let bit = i % 64;
        if word_idx >= mask.len() || (mask[word_idx] & (1u64 << bit)) == 0 {
            return None;
        }
        let row = id as usize;
        if row >= row_count {
            return None;
        }
        values.get(row).and_then(|v| v.as_ref())
    })
}

fn compute_folds<'a>(values: impl Iterator<Item = &'a Value>, wants: &[PropFold]) -> Vec<Value> {
    let mut count = 0u64;
    let mut min: Option<&Value> = None;
    let mut max: Option<&Value> = None;
    for value in values {
        count += 1;
        if min.is_none_or(|m| value.order_by_cmp(m) == std::cmp::Ordering::Less) {
            min = Some(value);
        }
        if max.is_none_or(|m| value.order_by_cmp(m) == std::cmp::Ordering::Greater) {
            max = Some(value);
        }
    }
    wants
        .iter()
        .map(|want| match want {
            PropFold::Count => Value::Int(count as i64),
            PropFold::Min => min.cloned().unwrap_or(Value::Null),
            PropFold::Max => max.cloned().unwrap_or(Value::Null),
        })
        .collect()
}

fn compute_grouped_counts<'a>(values: impl Iterator<Item = &'a Value>) -> Vec<(Value, u64)> {
    let mut counts: FxHashMap<Val, (Value, u64)> = FxHashMap::default();
    for value in values {
        let key = promoted_val(value).expect("a decoded column value is always promoted");
        let entry = counts.entry(key).or_insert_with(|| (value.clone(), 0));
        entry.1 += 1;
    }
    counts
        .into_iter()
        .map(|(_, (value, count))| (value, count))
        .collect()
}

/// The wire name of one key's column (`k{kid}`) — the one file-schema
/// fact both phases share: the builder names the struct's children with
/// it, the decode projects the column back by the same call.
pub(crate) fn column_name(kid: u32) -> String {
    format!("k{kid}")
}

fn vortex_error(error: impl std::fmt::Display) -> Error {
    Error::storage(format!("vortex store: {error}"))
}

#[cfg(test)]
mod tests;