relate-executor 1.4.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
//! The seek vocabulary and its recognizers: which single-index lookup
//! (if any) answers a node pattern's residual predicate — TEXT contains /
//! prefix, RANGE bounds, SPATIAL, and the `id()`/`elementId()` direct
//! seeks — the family the parent module's funnel (`leaf_seek`), the
//! exec engine's far-node prefilter, and the rel twin's decode
//! ([`id_fn_conversion`]) consult.

use std::collections::BTreeMap;

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

use crate::eval::{eval_bound, named_call, orient};
use crate::graph::{RangeSeek, SpatialSeek};
use crate::overlay::ExecGraph;
use crate::row::Row;

#[derive(Clone)]
pub(crate) enum StringSeek {
    Contains(String),
    Prefix(String),
}

/// One index-answerable predicate a conjunct places on the node being
/// matched. Collected from every conjunct, ranked, and the first one an
/// index serves wins; the residual re-checks the predicate either way.
#[derive(Clone)]
pub(crate) enum Seek {
    /// `id(n) = …` / `id(n) IN …` / `n IN [nodes…]` (LABS-250): ids
    /// answer directly from storage or the list, so this needs no index
    /// and no static label conjunction.
    Ids(Vec<NodeId>),
    /// `CONTAINS` / `ENDS WITH` (TEXT index) and `STARTS WITH` (RANGE index).
    Text(String, StringSeek),
    /// `=`, `IN`, and comparisons (RANGE index).
    Range(String, RangeSeek),
    /// `point.distance` / `point.withinBBox` (POINT index).
    Spatial(String, SpatialSeek),
}

impl Seek {
    /// Preference when several conjuncts seek: a direct id lookup, then
    /// text, range, and spatial. Stable, so conjunct order breaks ties.
    pub(super) fn rank(&self) -> u8 {
        match self {
            Seek::Ids(_) => 0,
            Seek::Text(..) => 1,
            Seek::Range(..) => 2,
            Seek::Spatial(..) => 3,
        }
    }

    /// Whether this seek is usable at all — the funnels' ONE per-seek
    /// eligibility call, none re-assembles the compound guard. Two
    /// criteria: an index-backed seek is only servable under a static
    /// label conjunction (a non-indexed seek — the scan fallback — is
    /// usable either way), and ADR-0493's crossover (a majority range
    /// window is cheaper as the scan the funnel falls through to).
    pub(crate) fn servable(
        &self,
        graph: &ExecGraph<'_>,
        labels: &[String],
        label_seekable: bool,
    ) -> bool {
        (!self.indexed() || label_seekable) && !self.crossover_declines(graph, labels)
    }

    /// Whether this seek is a point lookup — `id(n) = …`, `n IN [nodes…]`,
    /// or a property `=` / `IN` — as opposed to a comparison window, a text
    /// scan or a spatial region. A point lookup's answer is bounded by the
    /// matches it names, so a caller that sizes a candidate list from it
    /// (the shortest route's endpoint seek) never materializes a window.
    pub(crate) fn is_point(&self) -> bool {
        matches!(
            self,
            Seek::Ids(_) | Seek::Range(_, RangeSeek::Eq(_) | RangeSeek::In(_))
        )
    }

    /// Served by a declared index over the pattern labels. `Ids` is
    /// answered by storage and is valid under any label expression —
    /// the same classification tags the served answer
    /// [`ServedIds::Exact`] (the ids seek answers exactly) or
    /// [`ServedIds::Superset`] (every index serve can over-answer).
    pub(crate) fn indexed(&self) -> bool {
        !matches!(self, Seek::Ids(_))
    }

    /// Candidate ids tagged exact-or-superset, or `None` when no index
    /// serves this seek (the TEXT index also declines an empty
    /// `CONTAINS` needle).
    ///
    /// `cap` is the point ask's stop (`None`: the full answer): a property
    /// `=` / `IN` point seek streams its postings and stops at `cap + 1`
    /// matches ([`ExecGraph::lookup_point_upto`]), so an answer longer than
    /// `cap` is an arbitrary subset meaningful only by its length. Every
    /// other seek answers in full whatever `cap` is — an ids seek is
    /// bounded by its list, and no other seek is a point lookup.
    pub(crate) fn serve(
        &self,
        graph: &ExecGraph<'_>,
        labels: &[String],
        cap: Option<usize>,
    ) -> Option<ServedIds> {
        let ids = match self {
            Seek::Ids(ids) => ids
                .iter()
                .copied()
                .filter(|&id| graph.node_exists(id))
                .collect(),
            Seek::Text(key, StringSeek::Contains(n)) => {
                graph.lookup_string_contains(labels, key, n)?
            }
            Seek::Text(key, StringSeek::Prefix(n)) => graph.lookup_string_prefix(labels, key, n)?,
            Seek::Range(key, seek) => match cap {
                Some(cap) if self.is_point() => graph.lookup_point_upto(labels, key, seek, cap)?,
                _ => graph.lookup_range(labels, key, seek)?,
            },
            Seek::Spatial(key, seek) => graph.lookup_spatial(labels, key, seek)?,
        };
        Some(if self.indexed() {
            ServedIds::Superset(ids)
        } else {
            ServedIds::Exact(ids)
        })
    }

    /// `Some(n)` is an upper bound on the ids this seek would materialize.
    /// `None` when no index serves it (text and spatial seeks included).
    pub(crate) fn cardinality(&self, graph: &ExecGraph<'_>, labels: &[String]) -> Option<usize> {
        match self {
            Seek::Ids(ids) => Some(ids.len()),
            Seek::Range(key, seek) => graph.lookup_range_cardinality(labels, key, seek),
            Seek::Text(..) | Seek::Spatial(..) => None,
        }
    }

    /// ADR-0493's crossover, this seek's own arm: a range window at or
    /// above a quarter of the scan population it replaces — the
    /// smallest label the pattern names (the label conjunction the
    /// funnel's scan arm serves), the one population rule the
    /// planner's ladder gates with too, so plan narration and
    /// execution agree on multi-label nodes. `pattern_label_rows`
    /// sizes an absent label at 0: the scan the seek would replace is
    /// empty, so declining is the same rule applied to a measurement,
    /// not an unknown. A window the index cannot size, or an empty
    /// label set, keeps the seek (no measurement, no gate). Every
    /// other seek kind never declines here.
    fn crossover_declines(&self, graph: &ExecGraph<'_>, labels: &[String]) -> bool {
        let Seek::Range(..) = self else {
            return false;
        };
        let Some(window) = self.cardinality(graph, labels) else {
            return false;
        };
        // The planner's one population fold and gate (every label is
        // measured here — an absent label sizes 0).
        let scan_rows = relate_planner::crossover_scan_rows(
            // The planner's fold rides the name representation; the label
            // rows probe reads through the free text bridge.
            labels
                .iter()
                .map(|label| SmallName::from_name(label.as_str())),
            |l| l.with_str(|text| Some(graph.base().pattern_label_rows(text))),
        );
        relate_planner::range_seek_gated(window as u64, scan_rows)
    }
}

/// One served seek answer, tagged by exactness — the tag the funnel's
/// one narrowing gate reads (LABS-811, stellar F-2-2: the guard had been
/// spelled twice with different policies). [`Exact`](ServedIds::Exact):
/// the ids are the conjunct's answer — the ids seek is answered by
/// storage, nothing can over-answer it. [`Superset`](ServedIds::Superset):
/// an index serve whose members can over-answer — a RANGE equality over
/// zoned datetimes matches both DST halves at one instant, a TEXT
/// contains serve answers ENDS WITH — so the pattern's exact
/// label/property equality still narrows them. The residual re-applies
/// every predicate either way; the tag only decides whether the funnel
/// pays the narrowing lookup.
pub(crate) enum ServedIds {
    Exact(Vec<NodeId>),
    Superset(Vec<NodeId>),
}

impl ServedIds {
    /// The ids, narrowed to the pattern's exact label/property equality
    /// when (and only when) the serve is a superset — the ONE narrowing
    /// site every served answer the funnel returns flows through, the
    /// label-disjunction union arm excepted (LABS-1083: its per-arm
    /// serves over-answer only as each arm's serve does, the pattern's
    /// own label list is empty, and the per-candidate check stays the
    /// exactness net — see `seek_union`). The
    /// property map arrives as a lazy thunk so an exact serve never
    /// evaluates it (a malformed pattern-property expression keeps
    /// surfacing at the per-candidate check instead, exactly as before).
    /// `pub(super)` — the funnel tier: every caller is this module's
    /// parent (`candidates/mod.rs`), no other walk narrows a served
    /// answer.
    pub(super) fn narrowed(
        self,
        graph: &ExecGraph<'_>,
        labels: &[String],
        props: impl FnOnce() -> Result<BTreeMap<String, Value>>,
    ) -> Result<Vec<NodeId>> {
        match self {
            ServedIds::Exact(ids) => Ok(ids),
            ServedIds::Superset(mut ids) => {
                let props = props()?;
                if props.is_empty() {
                    return Ok(ids);
                }
                // props non-empty: lookup_nodes always serves (its only
                // None is the empty-seek case). Its answer is ascending
                // and deduplicated, so the membership test is a binary
                // search on the sorted slice — no per-statement hash set
                // (LABS-803's `make_hash<i64>` churn).
                let eq = graph
                    .lookup_nodes(labels, &props)
                    .expect("props non-empty: lookup_nodes always serves");
                ids.retain(|id| eq.binary_search(id).is_ok());
                Ok(ids)
            }
        }
    }

    /// The composite of two served answers under one Boolean op's id-set
    /// fold (`union_ids` for OR, `intersect_ids` for AND): exact only
    /// when both are — one superset arm keeps the composite a superset.
    /// `pub(crate)`, beside [`into_ids`](ServedIds::into_ids): since
    /// LABS-803 the exec far probe's materialize folds its served
    /// answers through this same spelling cross-module — the one
    /// composite every served-set consumer reads. [`narrowed`](ServedIds::narrowed)
    /// stays `pub(super)`: only the funnel narrows.
    pub(crate) fn folded(
        self,
        other: ServedIds,
        fold: fn(Vec<NodeId>, Vec<NodeId>) -> Vec<NodeId>,
    ) -> ServedIds {
        let exact = matches!(self, ServedIds::Exact(_)) && matches!(other, ServedIds::Exact(_));
        let ids = fold(self.into_ids(), other.into_ids());
        if exact {
            ServedIds::Exact(ids)
        } else {
            ServedIds::Superset(ids)
        }
    }

    /// The ids without the tag — for consumers that filter by set
    /// membership only (the far probe's intersection). `pub(crate)`,
    /// deliberately wider than its `pub(super)` siblings: the exec
    /// engine's far probe (`exec/far_probe.rs`) reads this projection
    /// cross-module — the one ServedIds member with a caller outside
    /// the candidates tier.
    pub(crate) fn into_ids(self) -> Vec<NodeId> {
        match self {
            ServedIds::Exact(ids) | ServedIds::Superset(ids) => ids,
        }
    }
}

/// The seek one conjunct expresses on `var`, if any. The four recognizers
/// are disjoint: an ids seek has `id()`/`elementId()` or the bare
/// variable itself (LABS-250's `n IN [nodes…]`) on the left, a text or
/// range seek a property, a spatial seek a point function.
pub(crate) fn leaf_seek(
    var: &SmallName,
    expr: &Expr,
    graph: &ExecGraph<'_>,
    row: &Row,
    params: &BTreeMap<String, Value>,
) -> Option<Seek> {
    if let Some(ids) = ids_seek_expr(var, expr, graph, row, params) {
        return Some(Seek::Ids(ids));
    }
    if let Some((key, seek)) = string_seek_expr(var, expr, graph, row, params) {
        return Some(Seek::Text(key, seek));
    }
    if let Some((key, seek)) = range_seek_expr(var, expr, graph, row, params) {
        return Some(Seek::Range(key, seek));
    }
    point_seek_expr(var, expr, graph, row, params).map(|(key, seek)| Seek::Spatial(key, seek))
}

fn string_seek_expr(
    var: &SmallName,
    expr: &Expr,
    graph: &ExecGraph<'_>,
    row: &Row,
    params: &BTreeMap<String, Value>,
) -> Option<(String, StringSeek)> {
    let Expr::Binary { op, left, right } = expr else {
        return None;
    };
    // Classify once, binding the constructor before any paid work: the
    // `_` arm is the decline gate for every other op.
    let variant: fn(String) -> StringSeek = match op {
        BinaryOp::Contains | BinaryOp::EndsWith => StringSeek::Contains,
        BinaryOp::StartsWith => StringSeek::Prefix,
        _ => return None,
    };
    // The var's property on the left — the one canonical recognizer.
    let key = prop_key(left, var)?;
    let val = eval_bound(right, graph, row, params)?;
    let s = val.as_string()?.to_string();
    // Servability has one owner: the gram index itself declines an empty
    // needle (lookup_string_contains returns None), and node_candidates
    // falls through to its shared scan tail — `CONTAINS ''` stays correct.
    Some((key, variant(s)))
}

fn range_seek_expr(
    var: &SmallName,
    expr: &Expr,
    graph: &ExecGraph<'_>,
    row: &Row,
    params: &BTreeMap<String, Value>,
) -> Option<(String, RangeSeek)> {
    let Expr::Binary { op, left, right } = expr else {
        return None;
    };
    let (key, bound_expr, op) = orient(|e| prop_key(e, var), left, right, *op)?;
    // The bound side is served only through the readiness proof
    // (`crate::eval::bound_ready`) — this recognizer is where the
    // LABS-328 regression was caught the first time (the
    // entity-resolution report cell's `WHERE c.key = c.cluster` returned
    // 0 rows once the constraint's backing index materialized) and the
    // null check below caught only the null-evaluating subset of that
    // class. Null never satisfies an equality or a comparison, so
    // declining to the scan tail plus the residual re-applying the
    // predicate per candidate row is the same answer, correctly reached.
    let bound = eval_bound(bound_expr, graph, row, params)?;
    // A null bound is never servable.
    if bound.is_null() {
        return None;
    }
    match op {
        BinaryOp::Eq => Some((key, RangeSeek::Eq(bound))),
        BinaryOp::In => {
            let Value::List(xs) = bound else {
                return None;
            };
            Some((key, RangeSeek::In(xs)))
        }
        BinaryOp::Lt | BinaryOp::Gt | BinaryOp::Lte | BinaryOp::Gte => {
            Some((key, RangeSeek::Cmp { op, bound }))
        }
        _ => None,
    }
}

/// The var's own property key — `key_of` in owned form.
fn prop_key(expr: &Expr, var: &SmallName) -> Option<String> {
    // The seek's property key is the planner-IR name; it renders once at
    // this recognition (index lookup text, not a per-row read).
    crate::eval::key_of(expr, var).map(|key| key.to_string())
}

fn point_seek_expr(
    var: &SmallName,
    expr: &Expr,
    graph: &ExecGraph<'_>,
    row: &Row,
    params: &BTreeMap<String, Value>,
) -> Option<(String, SpatialSeek)> {
    if let Some(args) = named_call(expr, "point.withinbbox", 3) {
        let key = prop_key(&args[0], var)?;
        let lo = eval_bound(&args[1], graph, row, params)?;
        let hi = eval_bound(&args[2], graph, row, params)?;
        return Some((key, SpatialSeek::BBox { lo, hi }));
    }
    let Expr::Binary { op, left, right } = expr else {
        return None;
    };
    let (func, bound_expr, inclusive) = distance_cmp(left, right, *op)?;
    let args = named_call(func, "point.distance", 2)?;
    let (key, origin) = point_prop_and_origin(var, &args[0], &args[1], graph, row, params)?;
    let max = eval_bound(bound_expr, graph, row, params)?.as_number()?;
    Some((
        key,
        SpatialSeek::Distance {
            origin,
            max,
            inclusive,
        },
    ))
}

fn distance_cmp<'a>(
    left: &'a Expr,
    right: &'a Expr,
    op: BinaryOp,
) -> Option<(&'a Expr, &'a Expr, bool)> {
    match op {
        BinaryOp::Lt => Some((left, right, false)),
        BinaryOp::Lte => Some((left, right, true)),
        BinaryOp::Gt => Some((right, left, false)),
        BinaryOp::Gte => Some((right, left, true)),
        _ => None,
    }
}

fn point_prop_and_origin(
    var: &SmallName,
    a: &Expr,
    b: &Expr,
    graph: &ExecGraph<'_>,
    row: &Row,
    params: &BTreeMap<String, Value>,
) -> Option<(String, Value)> {
    // The distance functions are operand-symmetric — no op to mirror, so
    // only the side-selection core applies: the side carrying the var's
    // property decides, the other side is the origin.
    let (key, origin_expr) = crate::eval::var_key_side(var, a, b)?;
    let origin = eval_bound(origin_expr, graph, row, params)?;
    Some((key.to_string(), origin))
}

/// An id conversion selected by a seek recognizer's left shape: a
/// value to a node or rel id (`NodeId`/`RelId` are both `i64`). The
/// three known conversions name their shape — a typed enum, not a
/// boxed closure, so a seek classification allocates nothing
/// (LABS-950).
pub(crate) enum IdConversion {
    /// `id(var)`: the value's integer form.
    Int,
    /// `elementId(var)`: the engine's self-encoding element-id string,
    /// through the caller's namespace picker.
    ElementId(fn(&str) -> Option<i64>),
    /// The bare node variable (LABS-250): the value's node form.
    NodeValue,
}

impl IdConversion {
    /// The one application of a conversion — every consumer (the ids
    /// recognizer, its `IN` tail, the rel twin's decode) reads this
    /// spelling, never a re-derived per-variant match.
    pub(crate) fn apply(&self, value: &Value) -> Option<i64> {
        match self {
            IdConversion::Int => value.as_int(),
            IdConversion::ElementId(parse) => value.as_string().and_then(*parse),
            IdConversion::NodeValue => crate::pattern_walk::node_id(value),
        }
    }
}

/// The left-shape dispatch shared by the node and rel id seeks: a
/// single-argument `id(var)` or `elementId(var)` call selects the id
/// conversion — integer for `id()`, the engine's self-encoding element
/// id string for `elementId()` (`parse_element_id` picks the node or
/// rel namespace). Any other left shape declines.
pub(crate) fn id_fn_conversion(
    var: &SmallName,
    left: &Expr,
    parse_element_id: fn(&str) -> Option<i64>,
) -> Option<IdConversion> {
    let (args, conversion) = if let Some(args) = named_call(left, "id", 1) {
        (args, IdConversion::Int)
    } else {
        let args = named_call(left, "elementid", 1)?;
        (args, IdConversion::ElementId(parse_element_id))
    };
    let Expr::Variable(v) = &args[0] else {
        return None;
    };
    if v != var {
        return None;
    }
    Some(conversion)
}

/// The comparison an ids-seek serves, narrowed from `BinaryOp` up
/// front: `=` (the right side is the one id) or `IN` (the right side
/// is the list of ids). The narrowing lets the recognizer's collect
/// step match exhaustively — no dead arm re-splitting the op fact the
/// front match already decided.
enum IdsCmp {
    /// `=`: the right side is the one id.
    Eq,
    /// `IN`: the right side is the list of ids.
    In,
}

/// The ids recognizer: one seek whose id conversion is selected by the
/// left shape — `id(n) =/IN …` (integer), `elementId(n) =/IN …` (the
/// `4:rl-id:n<id>` string form), or the bare node variable
/// (`n = node` / `n IN [nodes…]`, LABS-250, the node-value conversion).
/// The shapes are disjoint, so at most one conversion applies per
/// conjunct.
fn ids_seek_expr(
    var: &SmallName,
    expr: &Expr,
    graph: &ExecGraph<'_>,
    row: &Row,
    params: &BTreeMap<String, Value>,
) -> Option<Vec<NodeId>> {
    let Expr::Binary { op, left, right } = expr else {
        return None;
    };
    // One classification of `op`: every other op declines before paying
    // an eval. The narrowed `cmp` keeps the collect step below
    // exhaustive.
    let cmp = match op {
        BinaryOp::Eq => IdsCmp::Eq,
        BinaryOp::In => IdsCmp::In,
        _ => return None,
    };
    // The left-shape dispatch is pure: the bare node variable is the
    // one special case (its node form, for `=` and `IN` alike —
    // LABS-250); every other left goes to the canonical call-shape
    // owner, which declines what it does not recognize. The op
    // classification is read only by the collect step below.
    let to_id = match left.as_ref() {
        Expr::Variable(v) if v == var => IdConversion::NodeValue,
        _ => id_fn_conversion(var, left, relate_core::parse_node_element_id)?,
    };
    // The id (or id list) evaluates on the entry row — the readiness
    // proof applies like every other bound side.
    let val = eval_bound(right, graph, row, params)?;
    match cmp {
        IdsCmp::Eq => to_id.apply(&val).map(|id| vec![id]),
        IdsCmp::In => in_list_ids(&val, &to_id),
    }
}

/// The `IN`-seek tail shared by every left shape (id()/elementId() and the
/// bare node variable, LABS-250): a list value mapped through the id
/// conversion, deduped — a duplicate list element is a semantic regression
/// (a duplicate row), not a harmless extra. The conversion is total over
/// the list, the same contract as the scalar arm: one element it cannot
/// read declines the seek, and the residual re-evaluates the predicate —
/// the same answer, since an unreadable element can never equal the
/// variable's id. A non-list declines the seek the same way; the residual
/// re-evaluates the predicate anyway — null matches nothing, any other
/// non-list errors with InvalidArgumentType.
fn in_list_ids(val: &Value, to_id: &IdConversion) -> Option<Vec<NodeId>> {
    let Value::List(xs) = val else {
        return None;
    };
    let mut ids: Vec<NodeId> = xs
        .iter()
        .map(|x| to_id.apply(x))
        .collect::<Option<Vec<_>>>()?;
    ids.sort_unstable();
    ids.dedup();
    Some(ids)
}