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
//! The exec engine's node-match residency: the relationship twin of the
//! node id seek ([`rel_seek_expr`], the LABS-161 decode), the node
//! admission ([`node_ok`]), and the shared SEARCH scan contract — index
//! resolution, survivor materialization, the ranked ordering and the
//! bounded [`TopWindow`], the one spelling every ranked scan (vector,
//! fulltext, WRRF fusion) calls — which lives here (stellar round 3,
//! std-6), not in either feature module. The node-candidate funnel
//! itself — which index, if any, answers a node pattern's residual
//! predicates, and the label/property scan it falls back to — is not
//! this engine's: both walk engines read it, so it lives at the shared
//! walk tier ([`crate::pattern_walk::node_candidates`]). Relationship
//! expansion lives in `expand`.

use std::cmp::Ordering as CmpOrdering;
use std::cmp::Reverse;
use std::collections::{BTreeMap, BinaryHeap};
use std::sync::Arc;

use relate_core::{Error, Node, NodeId, PropertyMap, RelId, Result, Value};
use relate_ir::{BinaryOp, Direction, Expr, NodePattern, RelPattern};
use relate_model::EntityKind;
use relate_names::{NamedToken, SmallName, Token};

use crate::eval::{eval_bound, label_expr_matches};
use crate::overlay::ExecGraph;
use crate::pattern_walk::{
    id_fn_conversion, pattern_match_props, static_rel_type_ok, IdConversion,
};
use crate::props::props_all_match;
use crate::redo::{IndexDetailSpec, IndexKindSpec};
use crate::row::Row;
use crate::store::RelTypes;

/// The relationship twin of the node ids seek (`crate::pattern_walk`'s
/// `ids_seek_expr`, LABS-161): `id(r) = <expr>`
/// (integer) or `elementId(r) = <expr>` (the `5:rl-id:r<id>` string
/// form, which self-encodes the RelId) on the relationship variable `var`
/// resolves the RelId directly, so the walk can seek the id-keyed
/// relationship storage instead of walking every node's adjacency. Only
/// equality — an `IN` list stays a walk (the residual re-evaluates the
/// predicate either way, so a declined seek never changes the result).
pub(crate) fn rel_seek_expr(
    var: &SmallName,
    expr: &Expr,
    graph: &ExecGraph<'_>,
    row: &Row,
    params: &BTreeMap<String, Value>,
) -> Option<RelId> {
    let Expr::Binary { op, left, right } = expr else {
        return None;
    };
    if !matches!(op, BinaryOp::Eq) {
        return None;
    }
    let to_id: IdConversion = id_fn_conversion(var, left, relate_core::parse_rel_element_id)?;
    // The id evaluates on the entry row — the same readiness-proof
    // pairing the node-side seek recognizers read (`crate::eval::eval_bound`).
    let val = eval_bound(right, graph, row, params)?;
    to_id.apply(&val)
}

/// Outcome of the shared LABS-161 seek decode ([`resolve_rel_seek`]),
/// before the caller applies its own anchoring policy.
pub(crate) enum SeekedRel {
    /// No seek applies: the caller enumerates the adjacency.
    Decline,
    /// The resolved rel id cannot match: not visible (`rel_ends` misses),
    /// or rejected by the pattern's static type filter.
    Gone,
    /// The rel id with both raw endpoints; orientation is the caller's.
    Resolved {
        start: NodeId,
        end: NodeId,
        rid: RelId,
    },
}

/// The decode core shared by the walk's first-hop seek (`first_rel_seek`
/// in `expand`) and the one-hop aggregate fold: resolve the row-bound rel
/// or the `elementId(r) = …` / `id(r) = …` equality conjunct to a RelId,
/// check visibility via `rel_ends`, and apply the pattern's static type
/// filter (the adjacency enumeration applies it while scanning, so the
/// seek applies it in place). Declines for a var-length or anonymous rel,
/// a null or non-relationship row binding (the pre-bound reconciliation
/// keeps its existing behavior), or no equality conjunct. The residual
/// re-evaluates the predicate either way, so the decode narrows the
/// search, never the result.
pub(crate) fn resolve_rel_seek(
    graph: &ExecGraph<'_>,
    rp: &RelPattern,
    types: &RelTypes,
    conjuncts: &[Expr],
    row: &Row,
    params: &BTreeMap<String, Value>,
) -> SeekedRel {
    if rp.var_length {
        return SeekedRel::Decline;
    }
    let Some(v) = rp.variable.as_ref() else {
        return SeekedRel::Decline;
    };
    let rid = match row.get_name(v) {
        Some(Value::Relationship(r)) => r.id,
        Some(_) => return SeekedRel::Decline,
        None => match conjuncts
            .iter()
            .find_map(|c| rel_seek_expr(v, c, graph, row, params))
        {
            Some(rid) => rid,
            None => return SeekedRel::Decline,
        },
    };
    let Some((start, end)) = graph.rel_ends(rid) else {
        return SeekedRel::Gone;
    };
    // The caller resolved the pattern's type filter once for the walk
    // (LABS-801); the seek applies it in place of the adjacency
    // enumeration's.
    if !static_rel_type_ok(graph, rid, types) {
        return SeekedRel::Gone;
    }
    SeekedRel::Resolved { start, end, rid }
}

/// The direction-oriented start of a resolved seek rel; `None` on
/// `Direction::Both` — the undirected decline, decided once here: an
/// undirected pattern matches each rel in both orientations, and one
/// anchor would drop the reverse hop, so the scan keeps serving it. The
/// one spelling both seek consumers read (the walk's `first_rel_seek`,
/// the one-hop fold's `fold_rel_seek`).
pub(crate) fn oriented_start(rp: &RelPattern, start: NodeId, end: NodeId) -> Option<NodeId> {
    match rp.direction {
        Direction::Outgoing => Some(start),
        Direction::Incoming => Some(end),
        Direction::Both => None,
    }
}

/// Fetch a candidate the enumeration claimed is present; a miss is an
/// invariant violation, not a skip. One spelling for every read of an
/// enumeration-filtered candidate — the vector and fulltext scans'
/// materialization (`exec/vector.rs`) and the GPM walk's id-slice reads
/// (`exec/gpm/mod.rs`'s `materialize_path` and the selector's rel-type
/// read — every id there was claimed by the walk's neighbor enumeration,
/// which yields only rels that exist, or passed its node visibility
/// read). A caller whose candidate set is NOT
/// enumeration-filtered (the LABS-161 rel seek, which can point at a
/// since-deleted rel) spells its own miss handling — `SeekedRel::Gone`.
pub(crate) fn expect_enumerated_candidate<T>(candidate: Option<T>, id: i64) -> T {
    candidate.unwrap_or_else(|| vanished_enumerated_candidate(id))
}

/// The one spelling of the enumerated-candidate invariant's panic.
fn vanished_enumerated_candidate(id: i64) -> ! {
    panic!("enumerated candidate {id} vanished from the read snapshot")
}

/// The one SEARCH index resolution (stellar round 3, std-6): the schema
/// read, the missing-index policy, the wrong-kind rejection, and the
/// still-populating rejection of an index the current transaction created
/// (ADR-0678) — one spelling both SEARCH paths (vector, fulltext) call
/// instead of hand-building the errors a second time. The two catalog maps
/// are installed by the same redo op under the same name (apply_redo.rs,
/// theater.rs; drops remove both), so either miss is a missing index.
pub(crate) fn resolve_search_index(
    graph: &ExecGraph<'_>,
    index_name: &str,
    expected_kind: IndexKindSpec,
) -> Result<(IndexDetailSpec, BTreeMap<String, Value>)> {
    let theater = graph.show_schema()?;
    let (detail, options) = match (
        theater.data.index_details.get(index_name),
        theater.data.index_options.get(index_name),
    ) {
        (Some(detail), Some(options)) => (detail.clone(), options.clone()),
        _ => return Err(Error::execution(format!("IndexDoesNotExist: {index_name}"))),
    };
    if detail.kind != expected_kind {
        return Err(Error::execution(format!("WrongIndexType: {index_name}")));
    }
    // An index created inside the current transaction is populating until
    // the transaction commits — a transient rejection, not a scan over a
    // half-installed catalog entry.
    if graph.index_created_in_scratch(index_name) {
        return Err(Error::execution(format!(
            "IndexStillPopulating: Index `{index_name}` is not ready yet. \
             Wait until it finishes populating and retry the transaction."
        )));
    }
    Ok((detail, options))
}

/// Materialize one scored survivor (stellar round 2, std-6): the id was
/// just read from this same read-only graph, so a failed re-fetch is an
/// invariant violation — a panic, not a silently short result set. The
/// shared scan contract's kind-dispatching composite (stellar round 3,
/// std-6): both SEARCH paths (vector, fulltext) read it from here, not
/// from the vector feature module.
pub(crate) fn materialize_candidate(graph: &ExecGraph<'_>, kind: EntityKind, id: i64) -> Value {
    let candidate = if kind.is_relationship() {
        graph.rel(id).map(Value::from_relationship)
    } else {
        graph.node(id).map(Value::from_node)
    };
    expect_enumerated_candidate(candidate, id)
}

/// The survivor materialization every ranked scan ends with: windowed
/// `(id, score)` rows — order already decided, best-first under
/// [`ranked_better_cmp`] — become `(entity, score)` candidates. One
/// composition over [`materialize_candidate`]: the vector scan, the
/// fulltext orchestration, and the fulltext procedure all end here,
/// never each re-spelling the loop.
pub(crate) fn ranked_candidates(
    graph: &ExecGraph<'_>,
    kind: EntityKind,
    ranked: impl IntoIterator<Item = (i64, f64)>,
) -> Vec<(Value, f64)> {
    ranked
        .into_iter()
        .map(|(id, score)| (materialize_candidate(graph, kind, id), score))
        .collect()
}

/// The presence invariant of an enumerated node: the enumeration named it,
/// so it must exist in the read snapshot — the shared panic, without
/// fetching the record.
fn expect_enumerated_node(graph: &ExecGraph<'_>, id: NodeId) {
    if !graph.node_exists(id) {
        vanished_enumerated_candidate(id);
    }
}

/// One property of an enumerated node, by a single-key read. `None` only
/// for a genuinely absent key: a node the enumeration named that has
/// vanished from the read snapshot is an invariant violation, as
/// [`index_population`]'s record read treats it.
pub(crate) fn enumerated_node_prop(graph: &ExecGraph<'_>, id: NodeId, key: &str) -> Option<Value> {
    let value = graph.node_prop(id, key);
    if value.is_none() {
        expect_enumerated_node(graph, id);
    }
    value
}

/// The node arm's enumeration alone — the exact per-label ids, sorted and
/// deduplicated (the multi-label index is the union) — for a scan that
/// reads one property per id instead of materializing every node's full
/// map ([`index_population`] is this plus the record reads).
pub(crate) fn node_population_ids(graph: &ExecGraph<'_>, detail: &IndexDetailSpec) -> Vec<NodeId> {
    let mut ids: Vec<NodeId> = detail
        .labels
        .iter()
        .flat_map(|label| graph.live_node_ids_with_label(label))
        .collect();
    ids.sort_unstable();
    ids.dedup();
    ids
}

/// One FULLTEXT/VECTOR index's entity population: every live entity the
/// index covers as `(id, properties)` — the population half of the shared
/// scan contract (the vector and fulltext scans both read it). It carries
/// the two per-kind enumeration policies:
///
/// * node: the exact per-label enumeration (`live_node_ids_with_label`;
///   LABS-442 — a sparse label skips the other labels' rows and lookups
///   instead of sweeping every id and label-checking each record). The
///   multi-label index is the per-label union; no labels enumerates
///   nothing, as a sweep's label check did.
/// * relationship: the sweep, because the base has no relationship-by-type
///   postings (`live_rel_ids_of_type` is itself an O(R) walk, so a per-type
///   enumeration would pay k whole-table walks). The type filter runs
///   before the record clone.
///
/// Both arms hand back an enumerated record via
/// [`expect_enumerated_candidate`] — the type read included — so a miss
/// anywhere is a catalog/visibility invariant violation, never a silently
/// dropped candidate.
pub(crate) fn index_population(
    graph: &ExecGraph<'_>,
    detail: &IndexDetailSpec,
) -> Vec<(i64, Arc<PropertyMap>)> {
    if detail.relationship {
        graph
            .all_rel_ids()
            .into_iter()
            .filter_map(|id| {
                let rel_type = expect_enumerated_candidate(graph.rel_type(id), id);
                if !detail.labels.iter().any(|label| label == &rel_type) {
                    return None;
                }
                let rel = expect_enumerated_candidate(graph.rel(id), id);
                Some((id, Arc::clone(&rel.properties)))
            })
            .collect()
    } else {
        node_population_ids(graph, detail)
            .into_iter()
            .map(|id| {
                let node = expect_enumerated_candidate(graph.node(id), id);
                (id, Arc::clone(&node.properties))
            })
            .collect()
    }
}

/// The one canonical CIP-225 candidate ordering: `a` is `Greater` (better)
/// than `b` when its score is higher, or the scores tie and its element id
/// is lower. Scores order by `f64::total_cmp` — the crate's canonical
/// float order (the aggregate sort's), total and transitive where
/// `partial_cmp` + tie is not (a pathological NaN score now orders above
/// every finite one instead of tying intransitively with each). Shared by
/// every ranked scan (vector, fulltext, WRRF fusion).
pub(crate) fn ranked_better_cmp(score_a: f64, id_a: i64, score_b: f64, id_b: i64) -> CmpOrdering {
    score_a.total_cmp(&score_b).then_with(|| id_b.cmp(&id_a))
}

/// A scan candidate ordered by [`ranked_better_cmp`]: `Greater` is better.
/// `PartialEq` agrees with `Ord` by construction (stellar round 1, std-5):
/// the derived-eq disagreement on NaN ties is gone.
#[derive(Clone, Copy)]
struct Ranked {
    score: f64,
    id: i64,
}

// Total under `ranked_better_cmp` (`total_cmp` scores), so `Ord` applies
// even though the bare `f64` score is not `Eq`.
impl Eq for Ranked {}

impl PartialEq for Ranked {
    fn eq(&self, other: &Self) -> bool {
        self.cmp(other) == CmpOrdering::Equal
    }
}

impl Ord for Ranked {
    fn cmp(&self, other: &Self) -> CmpOrdering {
        ranked_better_cmp(self.score, self.id, other.score, other.id)
    }
}

impl PartialOrd for Ranked {
    fn partial_cmp(&self, other: &Self) -> Option<CmpOrdering> {
        Some(self.cmp(other))
    }
}

/// What [`TopWindow`] does with a candidate, carrying the candidate it was
/// decided for (`None` from `admission_for` drops it): the decision is
/// enacted instead of re-derived, and [`TopWindow::push_if_passes`] pre-gates
/// per-candidate work on it (ADR-0705's filter reads).
enum Admission {
    /// Room in the window: append.
    Append(Ranked),
    /// Window full and the candidate beats the worst retained entry:
    /// replace it.
    Replace(Ranked),
}

/// ADR-0256: the bounded top-k collector for the indexed scans — the one
/// CIP-225 result window (score descending, element id ascending,
/// `SKIP`/`LIMIT`). Retains only the best `offset + limit` candidates —
/// worst at the heap root — so the scan costs `O(n log window)` with no
/// full-length collect, and materializes the element `Value` only for the
/// survivors. Owns `(offset, limit)` itself so no caller can let the
/// retained window and the windowed rows drift (stellar round 1, std-5).
pub(crate) struct TopWindow {
    worst_first: BinaryHeap<Reverse<Ranked>>,
    offset: usize,
    limit: usize,
    /// `offset + limit`, computed once: the heap's retention bound and the
    /// per-push comparison, never re-derived on the scan's hot path.
    window: usize,
}

impl TopWindow {
    pub(crate) fn new(offset: usize, limit: usize) -> Self {
        // `offset + limit`, computed once here — the only place the
        // retention bound is derived.
        let window = offset.saturating_add(limit);
        Self {
            // The heap grows on demand; the clamp only avoids reserving an
            // unbounded window's capacity up front.
            worst_first: BinaryHeap::with_capacity(window.min(1024)),
            offset,
            limit,
            window,
        }
    }

    /// The window of a VECTOR source's scan: the LIMIT alone. The scan
    /// ranks from the top — a VECTOR source admits no SKIP (the front's
    /// 42N14) — and the caller's growth loop pages the survivors
    /// (ADR-0678). The one home of that policy.
    pub(crate) fn vector_window(limit: usize) -> Self {
        Self::new(0, limit)
    }

    /// The admission decision for `(score, id)`: what the window would do
    /// with it — `None` drops it. The window has room, or the candidate
    /// beats the worst retained entry; equal `(score, id)` pairs never
    /// displace each other, and ids are unique per candidate so an
    /// equal-ranked push is always the same candidate. The one spelling
    /// of the decision: [`Self::push`] and [`Self::push_if_passes`] both
    /// enact it.
    fn admission_for(&self, score: f64, id: i64) -> Option<Admission> {
        let candidate = Ranked { score, id };
        if self.worst_first.len() < self.window {
            return Some(Admission::Append(candidate));
        }
        self.worst_first
            .peek()
            .is_some_and(|worst| candidate > worst.0)
            .then_some(Admission::Replace(candidate))
    }

    /// The one gated collector: push `(score, id)` only when the window
    /// would keep it AND `passes` (the filter gate, evaluated lazily — a
    /// row that cannot place never reads its properties) accepts it.
    /// Every windowed scan (matrix, node, relationship) offers its
    /// candidates through this, so the pre-ranking filter semantics have
    /// one spelling.
    pub(crate) fn push_if_passes(
        &mut self,
        score: f64,
        id: i64,
        passes: impl FnOnce() -> Result<bool>,
    ) -> Result<()> {
        // The decision is made once and enacted once: the window is
        // untouched between the two.
        let Some(admission) = self.admission_for(score, id) else {
            return Ok(());
        };
        if passes()? {
            self.enact(admission);
        }
        Ok(())
    }

    /// The ungated entry point: decide via `admission_for`, hand the
    /// decision to `enact`.
    pub(crate) fn push(&mut self, score: f64, id: i64) {
        if let Some(admission) = self.admission_for(score, id) {
            self.enact(admission);
        }
    }

    /// Enact a decided admission: append into room, or replace the worst
    /// of a full window.
    fn enact(&mut self, admission: Admission) {
        match admission {
            Admission::Append(candidate) => self.worst_first.push(Reverse(candidate)),
            Admission::Replace(candidate) => {
                // A retained entry exists exactly when the window is full,
                // which is why this arm is Replace.
                *self
                    .worst_first
                    .peek_mut()
                    .expect("a full window has a worst entry") = Reverse(candidate);
            }
        }
    }

    /// The retained candidates best-first, windowed by the constructor's
    /// `offset`/`limit` — the heap emits best-first already
    /// (`Reverse<Ranked>` ascending is `Ranked` best-first).
    pub(crate) fn into_sorted_rows(self) -> Vec<(i64, f64)> {
        self.worst_first
            .into_sorted_vec()
            .into_iter()
            .map(|Reverse(r)| (r.id, r.score))
            .skip(self.offset)
            .take(self.limit)
            .collect()
    }
}

pub(crate) fn node_ok(
    graph: &ExecGraph<'_>,
    id: NodeId,
    np: &NodePattern,
    row: &Row,
    params: &BTreeMap<String, Value>,
) -> Result<bool> {
    // Cypher keeps a deleted-but-bound entity addressable for the rest of
    // the query: when the row already binds this candidate's variable to
    // the node a DELETE removed, the checks below read that binding's
    // tombstone (`refresh_rows` writes it: the same id, no labels, no
    // properties) instead of the store, which no longer has the node. So
    // `MATCH (u) DELETE u WITH u MATCH (u)` still matches, while a labelled
    // re-match fails like it does against the tombstone's empty label set.
    // Nothing else may see a deleted node — a pattern over an unbound
    // variable still matches nothing.
    let snapshot: Option<Arc<Node>> =
        np.variable
            .as_ref()
            .and_then(|v| row.get_name(v))
            .and_then(|value| match value {
                Value::Node(n) if n.id == id && graph.is_deleted_node(id) => Some(Arc::clone(n)),
                _ => None,
            });
    // The tombstone-vs-store fork, decided once per candidate (F-1-8):
    // every read below — label, label list, bare visibility, property —
    // goes through the view's one spelling of it, never four inline
    // matches each deciding visibility differently.
    let view = match snapshot.as_deref() {
        Some(node) => ReadView::Tombstone(node),
        None => ReadView::Store,
    };
    // One visibility read per path, and label checks without
    // materializing the label list (LABS-347, stellar F-1-17/F-1-36).
    match &np.label_expr {
        // The common case: even a plain `MATCH (n:Person)` carries a
        // synthesized static conjunction. Checking each label by name
        // pays one interner lookup per (candidate, label) — no
        // `Vec<String>` materialization, the q19 hot-path cost.
        // A static conjunction implies a non-empty label list at every
        // construction site (the planner's `add_labels` rebuilds
        // `label_expr` as `And(...)` over exactly `n.labels`; nothing
        // builds `And([])`), so the guard needs no labels cross-check —
        // the same single criterion `NodePattern::has_static_labels` uses.
        Some(expr) if expr.is_static_conjunction() => {
            if !np
                .labels
                .iter()
                .all(|(want, token)| view.has_label(graph, id, want, *token))
            {
                return Ok(false);
            }
        }
        // Genuinely dynamic label expressions need the labels as
        // strings; `node_labels` returning None is this path's
        // visibility read (invisible nodes have no labels).
        Some(expr) => {
            let Some(labels) = view.labels(graph, id) else {
                return Ok(false);
            };
            if !label_expr_matches(graph, row, params, expr, &labels, false)? {
                return Ok(false);
            }
        }
        // No label expression at all: bare visibility.
        None => {
            if !view.visible(graph, id) {
                return Ok(false);
            }
        }
    }
    let want = pattern_match_props(graph, row, np.properties.as_ref(), &np.prop_keys, params)?;
    Ok(props_all_match(&want, |key| view.prop(graph, id, key)))
}

/// The candidate's one read view (F-1-8): the row's tombstone for a
/// deleted-but-bound node, or the store for every live candidate. The
/// fork `node_ok` once re-decided at four reads — label, label list,
/// bare visibility, property — each spelling visibility differently; it
/// is spelled once here, as this type's reads.
#[derive(Clone, Copy)]
enum ReadView<'a> {
    Tombstone(&'a Node),
    Store,
}

impl ReadView<'_> {
    /// One label check: the tombstone's own string list, or the
    /// interning check — whose `None` (an invisible node) fails the
    /// check exactly like an absent label.
    fn has_label(&self, graph: &ExecGraph<'_>, id: NodeId, want: &SmallName, token: Token) -> bool {
        match self {
            Self::Tombstone(node) => node.labels.iter().any(|have| want == have.as_str()),
            // Plan-resolved ids skip the lookup; an unresolved plan keeps the
            // string check. A name the store had not interned at planning
            // fails without a store read unless a write overlay may carry
            // it (its labels stay strings until commit).
            Self::Store => {
                let seen = want.with_str(|text| match token {
                    Token::Id(tid) => graph.node_has_label_id(id, tid.0, text),
                    Token::Unresolved => graph.node_has_label(id, text),
                    Token::Absent => graph.node_has_uninterned_label(id, text),
                });
                seen == Some(true)
            }
        }
    }

    /// The whole label list a dynamic expression matches against: the
    /// tombstone's own, or the store's — whose `None` (an invisible
    /// node) the caller reads as "matches nothing".
    fn labels(&self, graph: &ExecGraph<'_>, id: NodeId) -> Option<Vec<String>> {
        match self {
            Self::Tombstone(node) => Some((*node.labels).clone()),
            Self::Store => graph.node_labels(id),
        }
    }

    /// Bare visibility: a bound tombstone stays addressable; a store
    /// candidate must exist.
    fn visible(&self, graph: &ExecGraph<'_>, id: NodeId) -> bool {
        match self {
            Self::Tombstone(_) => true,
            Self::Store => graph.node_exists(id),
        }
    }

    /// One property read: the tombstone's own map, or the store's
    /// plan-resolved read.
    fn prop(&self, graph: &ExecGraph<'_>, id: NodeId, key: &NamedToken) -> Option<Value> {
        match self {
            Self::Tombstone(node) => key.0.with_str(|text| node.properties.get(text).cloned()),
            Self::Store => graph.node_prop_resolved(id, key),
        }
    }
}