relate-executor 1.3.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
//! The counted node-candidate funnel and its seek family — the
//! `candidates/` arm of the pattern-walk vocabulary; `super`'s module doc
//! maps the whole tier. Which index, if any, answers a node pattern's
//! residual predicates, and the label/property scan it falls back to:
//! every node-match path of both engines reads this one funnel, so it
//! lives at the walk tier rather than inside the exec engine that first
//! grew it. The tier's own imports stay downward — the expression core,
//! the graph view, the row schema, the PROFILE phase clock — and the
//! residual's scope never travels here: a caller passes the bare
//! conjunct slice (`exec::pushdown::Residual`'s `conjuncts`), keeping
//! exec's residual pushdown in exec.
//!
//! `seek.rs` beside this module keeps the seek vocabulary and its
//! recognizers (the single-index lookups — TEXT contains/prefix, RANGE
//! bounds, SPATIAL, `id()`/`elementId()`); this module keeps the funnel
//! that ranks them, the OR-union pass, the served-set narrowing, the
//! label-disjunction union arm (LABS-1083), and the scan tail.

use std::collections::{BTreeMap, HashSet};

use relate_core::{NodeId, Result, Value};
use relate_ir::{BinaryOp, DisjunctionArms, Expr, NodePattern};
use relate_names::SmallName;

use crate::eval::expr_vars;
use crate::overlay::{intersect_ids, union_ids, ExecGraph};
use crate::pattern_walk::PatternMapRole;
use crate::pattern_walk::{binding, pattern_props, Binding, NodeBinding};
use crate::row::Row;
use crate::tiered_set::{TieredWordSet, WordHash};

mod seek;
#[cfg(test)]
mod tests;

pub(crate) use seek::{id_fn_conversion, leaf_seek, IdConversion, Seek, ServedIds};

/// Whether the funnel's scan answers `node` with its exact matches: a
/// static label conjunction or a disjunction fold both key the label
/// scan on the property map. Any other label expression (a negation, a
/// dynamic label) falls to the all-nodes tail, a superset the per-candidate
/// check filters later — its length says nothing about how many nodes
/// match, so a capped caller must not read it as a match count.
fn scan_answers_exactly(node: &NodePattern) -> bool {
    node.has_static_labels() || node.static_disjunction_arms().is_some()
}

/// The disjunction fold's union serve (LABS-1083): every arm's label set
/// is a static conjunction, so the seek answers as the union of the
/// per-arm keyed postings — consult each disjunct arm's index in turn,
/// accumulate into one id set deduplicated by `NodeId` through
/// [`union_dedup`] (a node carrying two disjunct labels yields ONE
/// candidate; the `Or` predicate yields each node once). Every arm must
/// pass the seek eligibility and be served by its index — one declined
/// arm (a missing index, ADR-0493's crossover-declined window) declines
/// the whole union, the OR-union pass's every-arm-served rule, and the
/// funnel falls to the next seek or the scan tail. The union is a
/// superset exactly as each arm's serve is (an indexed serve can
/// over-answer); the per-candidate check — the label expression and every
/// predicate re-applied — stays the exactness net, so the union arm
/// bypasses [`ServedIds::narrowed`] whose label/property narrowing would
/// key against the pattern's own (empty) label list and scan every node.
fn seek_union(
    graph: &ExecGraph<'_>,
    seek: &Seek,
    arms: &DisjunctionArms<'_>,
) -> Result<Option<Vec<NodeId>>> {
    let mut per_arm: Vec<Vec<NodeId>> = Vec::with_capacity(arms.len());
    for arm in arms.arms() {
        // Map-bridge: the graph's label lookups stay String-keyed; one
        // arm's labels render once per candidate selection, at the graph
        // call itself — the arms stay the classifier's borrowed views.
        let labels: Vec<String> = arm.map(|label| label.to_string()).collect();
        // Each arm IS a static conjunction by construction of the arm
        // classifier, so the arm's own label criterion passes; the
        // crossover prices the arm's own scan population. The union is
        // all-or-nothing: one unserved arm declines the whole conjunct.
        let Some(ids) = served(seek, graph, &labels, true, None) else {
            return Ok(None);
        };
        per_arm.push(ids.into_ids());
    }
    union_dedup(per_arm.into_iter()).map(Some)
}

/// A node id as the raw word the tiered set holds: `NodeId` is `i64` and
/// the bit-pattern cast is injective over its whole range, so distinct ids
/// stay distinct words whatever their sign.
fn id_word(id: NodeId) -> u64 {
    id as u64
}

/// The union of the per-arm served answers, deduplicated by node id and
/// ascending — the one per-arm union spelling every disjunction serve
/// reads (the keyed seek union and the scan tail's per-arm property
/// lookup alike). The dedup set is the tiered word set (ADR-0522): its
/// inline tier holds the common keyed case's handful of ids
/// allocation-free; ids enter it as [`id_word`]s.
fn union_dedup(served: impl Iterator<Item = Vec<NodeId>>) -> Result<Vec<NodeId>> {
    let mut seen = TieredWordSet::new(WordHash::Id);
    let mut ids = Vec::new();
    for arm_ids in served {
        for id in arm_ids {
            if seen.insert(id_word(id), "union")? {
                ids.push(id);
            }
        }
    }
    ids.sort_unstable();
    Ok(ids)
}

/// Outcome of trying to answer one boolean node with indexes.
///
/// `Served` is a candidate list (possibly empty), tagged
/// exact-or-superset by the serving seek ([`ServedIds`]). `Unserved` is
/// a recognized seek the funnel does not serve — its index declined
/// (`None`), or [`Seek::servable`] gated it out (an indexed seek under
/// an unseekable label conjunction, or ADR-0493's crossover-declined
/// window) — which an `AND` may skip and an `OR` may not. `Decline` abandons the whole union: the
/// leaf is not a seek, or it names a variable other than the node being
/// expanded.
enum IndexArm {
    Served(ServedIds),
    Unserved,
    Decline,
}

/// The OR-pass chain's shared context — the evaluation inputs (graph,
/// row, params, cypher version) plus the two pattern facts the chain
/// adds, threading through `or_union_candidates` → `index_arm` →
/// `index_leaf` as one argument (the label fact rides with the rest,
/// no literal standing in for a gate the caller holds).
struct OrCtx<'a> {
    graph: &'a ExecGraph<'a>,
    row: &'a Row,
    params: &'a BTreeMap<String, Value>,
    labels: &'a [String],
    label_seekable: bool,
}

fn or_union_candidates(var: &SmallName, conjuncts: &[Expr], ctx: &OrCtx<'_>) -> Option<ServedIds> {
    // Several served OR conjuncts are AND factors, so their answers
    // intersect — every fold through the one exactness-composition
    // spelling ([`ServedIds::folded`]: a composite is exact only when
    // every arm is). `Iterator::reduce` is the stdlib spelling of the
    // chain: the first served conjunct seeds, every later one folds in
    // conjunct order, so evaluation order and tag composition are
    // exactly the per-conjunct loop's. No closing normalization: every
    // serve ([`Seek::serve`]) and every id-set fold (`union_ids`,
    // `intersect_ids`) already returns sorted, deduped ids.
    conjuncts
        .iter()
        .filter(|c| {
            matches!(
                c,
                Expr::Binary {
                    op: BinaryOp::Or,
                    ..
                }
            )
        })
        .filter_map(|c| match index_arm(var, c, ctx) {
            IndexArm::Served(served) => Some(served),
            IndexArm::Unserved | IndexArm::Decline => None,
        })
        .reduce(|previous, served| previous.folded(served, intersect_ids))
}

fn index_arm(var: &SmallName, expr: &Expr, ctx: &OrCtx<'_>) -> IndexArm {
    match expr {
        Expr::Binary {
            op: BinaryOp::Or,
            left,
            right,
        } => match (index_arm(var, left, ctx), index_arm(var, right, ctx)) {
            (IndexArm::Decline, _) | (_, IndexArm::Decline) => IndexArm::Decline,
            (IndexArm::Served(a), IndexArm::Served(b)) => IndexArm::Served(a.folded(b, union_ids)),
            _ => IndexArm::Unserved,
        },
        Expr::Binary {
            op: BinaryOp::And,
            left,
            right,
        } => match (index_arm(var, left, ctx), index_arm(var, right, ctx)) {
            (IndexArm::Decline, _) | (_, IndexArm::Decline) => IndexArm::Decline,
            (IndexArm::Served(a), IndexArm::Served(b)) => {
                IndexArm::Served(a.folded(b, intersect_ids))
            }
            // The unserved arm's predicate is still pending per
            // candidate, so the composite can only be a superset.
            (IndexArm::Served(a), IndexArm::Unserved)
            | (IndexArm::Unserved, IndexArm::Served(a)) => {
                IndexArm::Served(ServedIds::Superset(a.into_ids()))
            }
            (IndexArm::Unserved, IndexArm::Unserved) => IndexArm::Unserved,
        },
        other => index_leaf(var, other, ctx),
    }
}

/// The plain serve path's one spelling: an index answers this seek when
/// [`Seek::servable`] (the label criterion and the crossover, the caller's
/// real facts — no literal standing in for a gate two frames up) and
/// [`Seek::serve`] agree. A gated or unserved window is `None`.
fn served(
    seek: &Seek,
    graph: &ExecGraph<'_>,
    labels: &[String],
    label_seekable: bool,
    cap: Option<usize>,
) -> Option<ServedIds> {
    if !seek.servable(graph, labels, label_seekable) {
        return None;
    }
    seek.serve(graph, labels, cap)
}

fn index_leaf(var: &SmallName, expr: &Expr, ctx: &OrCtx<'_>) -> IndexArm {
    let mut vars = HashSet::new();
    expr_vars(expr, &mut vars);
    // The expr_vars walk's String sink is probed through the free
    // text bridge.
    if vars.len() != 1 || !var.with_str(|text| vars.contains(text)) {
        return IndexArm::Decline;
    }
    // ENDS WITH has no reverse-key index. `string_seek_expr` maps it to
    // `StringSeek::Contains`: every string that ends with the suffix contains
    // it, and the residual applies the real ENDS WITH. No text index, or an
    // empty suffix, is Unserved so an AND sibling can still seed the arm.
    let Some(seek) = leaf_seek(var, expr, ctx.graph, ctx.row, ctx.params) else {
        return IndexArm::Decline;
    };
    served(&seek, ctx.graph, ctx.labels, ctx.label_seekable, None)
        .map_or(IndexArm::Unserved, IndexArm::Served)
}

/// Every residual conjunct that is a seek on `v`, cheapest rank first —
/// the one spelling of the funnel's seek collection.
fn ranked_seeks(
    v: &SmallName,
    conjuncts: &[Expr],
    graph: &ExecGraph<'_>,
    row: &Row,
    params: &BTreeMap<String, Value>,
) -> Vec<Seek> {
    let mut seeks: Vec<Seek> = conjuncts
        .iter()
        .filter_map(|c| leaf_seek(v, c, graph, row, params))
        .collect();
    seeks.sort_by_key(Seek::rank);
    seeks
}

/// The pattern's labels in the graph lookups' String key space — rendered
/// once per candidate selection, never per row.
fn pattern_labels(np: &NodePattern) -> Vec<String> {
    np.label_names().map(|label| label.to_string()).collect()
}

/// One node pattern's seek-side preparation — the single derivation both
/// funnel entries read ([`binding_arm`]'s seek strategy and the point
/// ask's [`point_arm`]): the ranked seeks over the residual's conjuncts
/// on `v`, the eligibility inputs, and the label-disjunction fold's arm
/// views (LABS-1083; `None` for every shape the single keyed arm or the
/// scan tail already serves).
struct Prepared<'a> {
    seeks: Vec<Seek>,
    /// Map-bridge: the graph's label lookups stay String-keyed.
    labels: Vec<String>,
    label_seekable: bool,
    arms: Option<DisjunctionArms<'a>>,
}

/// The funnel's seek-side preparation, or `None` under `USING SCAN`: no
/// seek may answer the variable then — the funnel's own arm scans it — so
/// there is nothing to prepare, and no caller re-reads the fact.
fn prepared<'a>(
    v: &SmallName,
    np: &'a NodePattern,
    conjuncts: &[Expr],
    graph: &ExecGraph<'_>,
    row: &Row,
    params: &BTreeMap<String, Value>,
) -> Option<Prepared<'a>> {
    if np.force_scan {
        return None;
    }
    Some(Prepared {
        seeks: ranked_seeks(v, conjuncts, graph, row, params),
        labels: pattern_labels(np),
        label_seekable: np.has_static_labels(),
        arms: np.static_disjunction_arms(),
    })
}

/// Which seek answers a variable's pattern: the disjunction fold's union
/// ids, or one seek's own served ids.
enum Answered {
    Union(Vec<NodeId>),
    Serve { ids: ServedIds },
}

impl Prepared<'_> {
    /// The fold's one rule (LABS-1083), the one spelling every walk over
    /// the prepared seeks reads: under a label-disjunction fold an indexed
    /// seek serves only as the union of the per-arm postings — the arms it
    /// serves from. `None` is the plain path: a pattern with no fold, or a
    /// label-blind ids seek (its serve never reads the labels, so it
    /// answers unchanged, which the `indexed` test already states).
    fn union_step(&self, seek: &Seek) -> Option<&DisjunctionArms<'_>> {
        self.arms.as_ref().filter(|_| seek.indexed())
    }

    /// The funnel's "which seek answers" walk over the prepared seeks, in
    /// rank order: the first seek an index serves. A union-only seek
    /// answers as the union of the per-arm postings; every other seek
    /// answers through [`served`].
    fn first_answered(&self, graph: &ExecGraph<'_>) -> Result<Option<Answered>> {
        for seek in &self.seeks {
            if let Some(arms) = self.union_step(seek) {
                if let Some(ids) = seek_union(graph, seek, arms)? {
                    return Ok(Some(Answered::Union(ids)));
                }
                continue;
            }
            if let Some(ids) = self.served_plain(seek, graph, None) {
                return Ok(Some(Answered::Serve { ids }));
            }
        }
        Ok(None)
    }

    /// One non-union seek's plain answer, the rule both walks read: a
    /// union-only seek never answers this way (`None` — its answer is the
    /// per-arm union, [`Self::first_answered`]'s own arm), every other
    /// seek answers through [`served`] when an index serves it.
    fn served_plain(
        &self,
        seek: &Seek,
        graph: &ExecGraph<'_>,
        cap: Option<usize>,
    ) -> Option<ServedIds> {
        if self.union_step(seek).is_some() {
            return None;
        }
        served(seek, graph, &self.labels, self.label_seekable, cap)
    }

    /// The point-only walk ([`point_arm`]'s one question): the first seek
    /// an index serves, skipping the union-only ones — a point ask cannot
    /// use a union answer and pays no per-arm index lookups for one —
    /// answers, but only when it is itself a point lookup
    /// ([`Seek::is_point`]); the first served seek decides, so any other
    /// served seek answers the pattern without a bounded list and the ask
    /// declines. `None`: no point seek serves the pattern. `cap` is the
    /// ask's stop ([`Seek::serve`]): a served answer longer than it
    /// is a subset meaningful only by its length.
    fn first_point(&self, graph: &ExecGraph<'_>, cap: Option<usize>) -> Option<ServedIds> {
        for seek in &self.seeks {
            let Some(ids) = self.served_plain(seek, graph, cap) else {
                continue;
            };
            return seek.is_point().then_some(ids);
        }
        None
    }
}

pub(crate) fn node_candidates(
    graph: &ExecGraph<'_>,
    np: &NodePattern,
    row: &Row,
    params: &BTreeMap<String, Value>,
    conjuncts: &[Expr],
) -> Result<Vec<NodeId>> {
    funnel(
        graph,
        np,
        row,
        params,
        Ask {
            conjuncts,
            cap: None,
        },
    )
}

/// What one funnel call asks of its seek strategy: the residual's bare
/// conjuncts and the most candidates the caller will use (`None`: all).
/// Whether the ask is point-only is not a knob — it is the other entry
/// ([`point_candidates_capped`]'s [`point_arm`]): the plain funnel has no
/// point mode, and no arm of it declines.
#[derive(Clone, Copy)]
struct Ask<'a> {
    conjuncts: &'a [Expr],
    cap: Option<usize>,
}

/// A capped funnel answer: the complete candidate list when at most
/// `cap` nodes match, or `Over` — more than `cap` match — with no list
/// and no count: the caller that refuses states its own bound. Both capped
/// entries answer
/// `Option<Capped>`: `None` is "no answer a caller may size a side by" —
/// a shape whose scan is a superset of its matches (the plain funnel
/// reports it before scanning), or a point ask no point seek served.
pub(crate) enum Capped {
    Within(Vec<NodeId>),
    Over,
}

/// [`node_candidates`] for a caller that only needs the candidates when
/// there are at most `cap` of them (the shortest route's pair bound and
/// full listing): the residual scan stops at `cap + 1` matches instead of
/// listing every match first. Counted and phase-stamped exactly like the
/// uncapped funnel — the node reads are the ones the scan returned.
pub(crate) fn node_candidates_capped(
    graph: &ExecGraph<'_>,
    np: &NodePattern,
    row: &Row,
    params: &BTreeMap<String, Value>,
    conjuncts: &[Expr],
    cap: usize,
) -> Result<Option<Capped>> {
    // A shape whose scan is a superset of its matches answers no count a
    // caller may trust ([`scan_answers_exactly`]) — reported before the
    // scan runs, so no caller has to know how the funnel would answer.
    if !scan_answers_exactly(np) {
        return Ok(None);
    }
    Ok(Some(cap_answer(
        funnel(
            graph,
            np,
            row,
            params,
            Ask {
                conjuncts,
                cap: Some(cap),
            },
        )?,
        cap,
    )))
}

/// [`node_candidates_capped`] as a point-only ask: a caller that sizes a
/// candidate list from the answer (the shortest route's endpoint seek)
/// wants it only when a point lookup — `id(n) = …`, `n IN [nodes…]`, a
/// property `=` / `IN` — served it: a point lookup's answer is bounded by
/// the matches it names, where a comparison window, a text scan, a
/// spatial region or a label scan is not. `Ok(None)`: no point seek
/// served the pattern — no list, and no scan ran.
pub(crate) fn point_candidates_capped(
    graph: &ExecGraph<'_>,
    np: &NodePattern,
    row: &Row,
    params: &BTreeMap<String, Value>,
    conjuncts: &[Expr],
    cap: usize,
) -> Result<Option<Capped>> {
    point_funnel(graph, np, row, params, conjuncts, cap)
        .map(|answer| answer.map(|ids| cap_answer(ids, cap)))
}

/// The capped answer of a complete candidate list: `Over` once more than
/// `cap` nodes match.
fn cap_answer(ids: Vec<NodeId>, cap: usize) -> Capped {
    if ids.len() > cap {
        Capped::Over
    } else {
        Capped::Within(ids)
    }
}

/// The counting every funnel entry wraps its arm in (LABS-375): one
/// node-store read per candidate the match examines, counted HERE, the
/// one funnel every node-match path reads (match_node, the cheapest-start
/// expansion, gpm start nodes, the agg folds, the shortest-route and eval
/// paths), so a scan or index seek reports its reads in PROFILE Db Hits
/// wherever it runs. LABS-448's seek span lives here too, beside the read
/// count it pairs with (the funnel inversion of the per-walker stamping):
/// every path that bumps the `Nodes` counter carries seek time, and no
/// caller re-stamps — the stamp is the funnel's, exactly once. Each entry
/// names its own answered length: the plain funnel's list (always
/// answered — every classification arm ends in a list, the scan tail) and
/// the point ask's `Option` (a decline counts `0`).
fn counted<T>(arm: impl FnOnce() -> Result<T>, answered: impl Fn(&T) -> usize) -> Result<T> {
    let reads_before = crate::ctx::node_reads();
    let candidates = crate::ctx::phase_seek(arm)?;
    // A scan counts the nodes it examined as it reads them; an answer
    // served whole from postings has examined exactly what it returns.
    // A decline (the point ask's `None`) counts nothing — its zero.
    if crate::ctx::node_reads() == reads_before {
        crate::ctx::count_node_reads(answered(&candidates) as u64);
    }
    Ok(candidates)
}

fn funnel(
    graph: &ExecGraph<'_>,
    np: &NodePattern,
    row: &Row,
    params: &BTreeMap<String, Value>,
    ask: Ask<'_>,
) -> Result<Vec<NodeId>> {
    counted(
        || binding_arm(binding(np, row), np, ask, graph, row, params),
        |candidates: &Vec<NodeId>| candidates.len(),
    )
}

/// The point ask's entry ([`point_candidates_capped`]'s arm): the same
/// counting wrapper around [`point_arm`], whose `None` is the ask's
/// decline.
fn point_funnel(
    graph: &ExecGraph<'_>,
    np: &NodePattern,
    row: &Row,
    params: &BTreeMap<String, Value>,
    conjuncts: &[Expr],
    cap: usize,
) -> Result<Option<Vec<NodeId>>> {
    counted(
        || point_arm(binding(np, row), np, conjuncts, graph, row, params, cap),
        |answer: &Option<Vec<NodeId>>| answer.as_ref().map_or(0, Vec::len),
    )
}

/// The classification's one answer table, the shape every consumer of a
/// [`Binding`] starts from ([`binding_arm`] and [`point_arm`], each
/// applying its own policy to the `Scan` state; the shortest route's
/// endpoint probe, which maps the answers to its own sides): a row-bound
/// variable is its one candidate, a null or non-node binding matches
/// nothing, a nameless element has no variable a seek targets (scan
/// territory), and a named unbound variable is the seek strategy's input.
pub(crate) enum Admitted<'a> {
    /// A row-bound variable: exactly one candidate, no list to build.
    One(NodeId),
    /// A null or non-node binding: no candidate at all.
    Empty,
    /// A nameless element: only a scan answers it.
    Scan,
    /// A named unbound variable: the seek strategy's input.
    Seek(&'a SmallName),
}

pub(crate) fn admitted<'a>(class: NodeBinding<'a>) -> Admitted<'a> {
    match class {
        Binding::Bound(id) => Admitted::One(id),
        Binding::Mismatch => Admitted::Empty,
        Binding::Anonymous => Admitted::Scan,
        Binding::Unbound(v) => Admitted::Seek(v),
    }
}

/// The funnel's binding arm — the classification dispatch, the unbound
/// variable's seek strategy, and the scan tail — and the home of
/// LABS-811's decisive resolution for the pass-the-payload vs
/// derive-inside flip-flop, which the caller cites too: a consumer
/// that needs a fact the classification carries (the
/// [`Unbound`](Binding::Unbound) variable's name, the
/// [`Bound`](Binding::Bound) / [`Mismatch`](Binding::Mismatch)
/// admission) receives the classified [`NodeBinding`] and matches its
/// variants directly — it never re-derives the variable from the
/// pattern (no invariant `expect` restating `classify`'s
/// named-variable contract) and never re-spells the admission table
/// beside the vocabulary. [`Binding::accepts`] stays the canonical
/// predicate for the boolean-only per-candidate consumers (the agg
/// folds, the trail gate); [`admitted`] is the one shape-returning
/// spelling of the same table, and this entry's match is its policy over
/// that table.
///
/// The arms: a row-bound variable leaves the funnel exactly its one
/// candidate; a null or non-node binding matches nothing (the walker's
/// binding check — `bind_pattern_node` — rejects them all;
/// short-circuit the seek work); a nameless element has no variable
/// for a seek to target and takes the scan policy ([`scan_ids`])
/// directly. Only the `Unbound` arm runs the seek strategy, the
/// variable's name read straight from the payload: `USING SCAN` (the
/// scan IS the answer), the ranked seek loop over the residual's
/// conjuncts (cheapest first, an unserved seek yields to the next),
/// and the OR-union pass (a candidate set only when every arm is
/// served) — every served answer narrowed through the one site both
/// entries share ([`ServedIds::narrowed`]); when no seek
/// serves it, the scan policy answers. The
/// pattern-only facts (the label seekability, the lazy property map
/// via [`pattern_props`]) are derived inside from `np` — only the
/// classification is received, never re-derived.
fn binding_arm(
    class: NodeBinding<'_>,
    np: &NodePattern,
    ask: Ask<'_>,
    graph: &ExecGraph<'_>,
    row: &Row,
    params: &BTreeMap<String, Value>,
) -> Result<Vec<NodeId>> {
    let Ask { conjuncts, cap } = ask;
    // The pattern's property map, lazily and exactly once per arm that
    // needs it (binding the thunk itself derives nothing).
    let props = map_thunk(graph, row, np, params);
    let v = match admitted(class) {
        Admitted::One(id) => return Ok(vec![id]),
        Admitted::Empty => return Ok(Vec::new()),
        // A nameless element has no variable a seek targets: only a scan
        // answers it.
        Admitted::Scan => return scan_ids(graph, np, props, cap),
        Admitted::Seek(v) => v,
    };
    // Every conjunct that is a seek on this variable, cheapest first.
    // The first one an index serves answers the pattern; an unserved
    // seek (no index, empty CONTAINS needle) yields to the next. The
    // residual still applies every predicate.
    let Some(p) = prepared(v, np, conjuncts, graph, row, params) else {
        // `USING SCAN`: no seek may answer this variable. The scan policy
        // ([`scan_ids`]) with no property map IS the scan; `node_ok` and
        // the residual conjuncts then apply `=` to every candidate — for a
        // zoned-datetime DST overlap that matches both halves, which an
        // index seek (instant plus zone) does not.
        return scan_ids(graph, np, || Ok(BTreeMap::new()), cap);
    };
    match p.first_answered(graph)? {
        Some(Answered::Union(ids)) => return Ok(ids),
        Some(Answered::Serve { ids }) => {
            return ids.narrowed(graph, &p.labels, props);
        }
        None => {}
    }
    // An OR conjunct is a candidate set only when every arm is served.
    // One declined arm (missing index, empty CONTAINS needle, NOT, XOR,
    // or a leaf that is not exactly this variable) abandons that
    // conjunct — including, structurally, an unseekable label
    // conjunction: `servable` declines every indexed seek under a
    // false label fact, the same call the ranked loop above makes.
    // ENDS WITH is served by a text index as a contains-superset.
    // Several served OR conjuncts are AND factors, so their id lists
    // are intersected — the composite tagged exact only when every
    // served conjunct is.
    let ctx = OrCtx {
        graph,
        row,
        params,
        labels: &p.labels,
        label_seekable: p.label_seekable,
    };
    if let Some(served) = or_union_candidates(v, conjuncts, &ctx) {
        return served.narrowed(graph, &p.labels, props);
    }
    // No seek served the variable: the scan policy answers it.
    scan_ids(graph, np, props, cap)
}

/// The point ask's arm ([`point_candidates_capped`]): the classification
/// read once, then the same seek walk as [`binding_arm`]'s `Unbound` arm
/// reduced to its one question — does a point seek serve the pattern —
/// with no scan, no OR-union and no union answer: none of those is a
/// point lookup, so the ask declines (`None`) instead of running one it
/// would discard. A row-bound variable is its one candidate; a null or
/// non-node binding matches nothing; a nameless element and `USING SCAN`
/// have no point seek to serve them.
fn point_arm(
    class: NodeBinding<'_>,
    np: &NodePattern,
    conjuncts: &[Expr],
    graph: &ExecGraph<'_>,
    row: &Row,
    params: &BTreeMap<String, Value>,
    cap: usize,
) -> Result<Option<Vec<NodeId>>> {
    let v = match admitted(class) {
        Admitted::One(id) => return Ok(Some(vec![id])),
        Admitted::Empty => return Ok(Some(Vec::new())),
        // A nameless element has no variable a seek targets, so no point
        // seek serves it.
        Admitted::Scan => return Ok(None),
        Admitted::Seek(v) => v,
    };
    let Some(p) = prepared(v, np, conjuncts, graph, row, params) else {
        // `USING SCAN`: no seek may answer this variable, a point seek
        // included.
        return Ok(None);
    };
    // The seek serve stops at `cap + 1` matches only when nothing narrows
    // it afterwards: a pattern property map re-narrows a superset serve
    // ([`ServedIds::narrowed`]), which could shrink a truncated subset
    // under the cap and read as complete. Such a pattern serves in full.
    let serve_cap = np.properties.is_none().then_some(cap);
    let Some(ids) = p.first_point(graph, serve_cap) else {
        return Ok(None);
    };
    let props = map_thunk(graph, row, np, params);
    ids.narrowed(graph, &p.labels, props).map(Some)
}

/// The pattern's match-role property-map thunk, the one derivation — the
/// binding arm's scan paths, every served answer's narrowing
/// ([`ServedIds::narrowed`]) and the point ask's answer all read it;
/// bound once per arm and passed where the arm's diverging paths need it.
fn map_thunk<'a>(
    graph: &'a ExecGraph<'_>,
    row: &'a Row,
    np: &'a NodePattern,
    params: &'a BTreeMap<String, Value>,
) -> impl Fn() -> Result<BTreeMap<String, Value>> + Copy + 'a {
    || {
        pattern_props(
            graph,
            row,
            np.properties.as_ref(),
            PatternMapRole::Match,
            params,
        )
    }
}

/// The one scan policy, every site that answers a node pattern with a
/// scan: the label/property lookup a static label conjunction allows
/// (the caller's property map — the binding arm's nameless arm and
/// unserved-seek fallthrough pass the pattern's, the `USING SCAN` arm
/// passes none), then the all-nodes scan when the lookup declines. The
/// property map stays a lazy thunk so a caller whose labels are not
/// seekable never evaluates it — the error surface is unchanged. `cap`
/// is [`ExecGraph::lookup_nodes_upto`]'s: a residual scan may stop at
/// `cap + 1` matches. The disjunction fold's shapes (LABS-1083) answer
/// through their own helper ([`scan_disjunction_arms`]), never a
/// whole-store scan of their own spelling.
fn scan_ids(
    graph: &ExecGraph<'_>,
    np: &NodePattern,
    props: impl FnOnce() -> Result<BTreeMap<String, Value>>,
    cap: Option<usize>,
) -> Result<Vec<NodeId>> {
    // The two label shapes are exclusive — a static conjunction never
    // carries the `Or` the disjunction arms read, and a disjunction is
    // not a static conjunction — so the `FnOnce` map evaluates at most
    // once, on the path its pattern's shape takes.
    if np.has_static_labels() {
        // Same render-once bridge as the seek loop above.
        let labels = pattern_labels(np);
        if let Some(ids) = graph.lookup_nodes_upto(&labels, &props()?, cap) {
            return Ok(ids);
        }
        // The lookup's one decline is the both-empty shape (no labels, no
        // equalities) — a pattern that carries no disjunction arms either,
        // so the all-nodes scan below answers it.
    } else if let Some(arms) = np.static_disjunction_arms() {
        if let Some(ids) = scan_disjunction_arms(graph, &arms, &props()?, cap)? {
            return Ok(ids);
        }
    }
    Ok(graph.all_node_ids())
}

/// The disjunction fold's scan answer (LABS-1083) — the one spelling of
/// the shape's scan policy, read by [`scan_ids`]'s dispatch: a pattern
/// with property equalities answers as the union of the per-arm
/// label/property lookups (each arm's `(label, property)` index
/// consulted with the map's folded value, the per-arm analogue of the
/// conjunction path's one `lookup_nodes` call; an arm whose pair no
/// index serves falls to that arm's label scan inside the lookup,
/// exactly the single-label path's decline — and the map evaluates at
/// the funnel for these shapes, the single-label seekable path's own
/// policy: the same typed [`pattern_props`] error, raised before the
/// first candidate instead of at it). A pattern without equalities
/// (`And([C, Or([A, B])])`, no map) scans the conjunction base — the
/// pre-fold `:C` label scan, the `Or` arms left to the per-candidate
/// check. `Ok(None)` is the all-nodes fallthrough: a bare `Or` over no
/// conjunction labels, whose pre-fold form scanned the whole store.
/// `cap` rides every lookup — [`ExecGraph::lookup_nodes_upto`]'s
/// contract, per arm and per base scan alike.
fn scan_disjunction_arms(
    graph: &ExecGraph<'_>,
    arms: &DisjunctionArms<'_>,
    props: &BTreeMap<String, Value>,
    cap: Option<usize>,
) -> Result<Option<Vec<NodeId>>> {
    if !props.is_empty() {
        // The per-arm serve of the map's equalities: one arm's labels
        // render once per arm (the map-bridge), each arm's lookup
        // always serving — an arm's labels are non-empty, and a
        // non-empty seek is the lookup's one `Some` shape.
        return union_dedup(arms.arms().map(|arm| {
            let labels: Vec<String> = arm.map(|label| label.to_string()).collect();
            graph
                .lookup_nodes_upto(&labels, props, cap)
                .expect("an arm's labels are non-empty: lookup_nodes always serves")
        }))
        .map(Some);
    }
    // No equalities to key on: the fold's And-with-arms shape scans
    // its conjunction base (the pre-fold label scan), the `Or` arms
    // filtering per candidate — the base labels are non-empty, so
    // the lookup's one decline (the both-empty shape) cannot fire:
    // the same always-served expect the per-arm serve above states.
    if !arms.base().is_empty() {
        let labels: Vec<String> = arms.base().iter().map(|label| label.to_string()).collect();
        return Ok(Some(graph.lookup_nodes_upto(&labels, props, cap).expect(
            "the base labels are non-empty: lookup_nodes always serves",
        )));
    }
    Ok(None)
}