relate-executor 1.1.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
//! MATCH limit pushdown caps (DISTINCT/ORDERED), one-hop aggregate
//! folding (ADR-0036), residual predicates, and liveness trimming.

use std::collections::{BTreeMap, HashSet};
use std::sync::atomic::{AtomicU64, Ordering};

use relate_core::{Result, Value};
use relate_ir::{BinaryOp, Clause, Expr, PathPattern, RemoveItem, SetItem};
use relate_names::SmallName;

use crate::eval::{expr_vars, label_expr_vars, predicate_holds};
use crate::overlay::ExecGraph;
use crate::row::Row;

pub(crate) use super::agg_fold::FoldedReturn;
pub(crate) use super::match_cap::{match_limit_cap, MatchCap};
use super::scope::{
    add_gpm_element_names, call_scope_reads, for_each_pattern_bound_name, has_star_item,
    is_internal_column, ProjectionFields,
};

static ANON: AtomicU64 = AtomicU64::new(1);

pub(crate) struct Residual<'a> {
    pub conjuncts: &'a [Expr],
    pub scope: &'a HashSet<String>,
}

/// The degenerate residual — no conjuncts, empty scope: the one spelling
/// of "read with no residual pushdown", used by every caller that reads
/// candidates without a MATCH predicate of its own (the eval walk engine,
/// the agg folds, the exists probe, the shortest-route start).
impl Residual<'_> {
    /// The degenerate case of [`Self::with_conjuncts`]: no conjuncts
    /// either.
    pub(crate) fn empty() -> Residual<'static> {
        Self::with_conjuncts(&[])
    }

    /// Seek material without a scope: conjuncts to consider, no names
    /// bound — the one spelling of that shape, so no caller fabricates a
    /// dead local scope beside its conjuncts.
    pub(crate) fn with_conjuncts(conjuncts: &[Expr]) -> Residual<'_> {
        static EMPTY_SCOPE: std::sync::OnceLock<HashSet<String>> = std::sync::OnceLock::new();
        Residual {
            conjuncts,
            scope: EMPTY_SCOPE.get_or_init(HashSet::new),
        }
    }
}

/// The unscoped readiness kernel: every variable the expression names is
/// bound in `row`. [`conjunct_ready`] narrows it to a residual's scope.
pub(crate) fn expr_ready(expr: &Expr, row: &Row) -> bool {
    let mut vars = HashSet::new();
    expr_vars(expr, &mut vars);
    vars.iter().all(|name| row.contains_key(name))
}

/// Whether `expr` can be evaluated against `row`: every variable it names
/// that `scope` can bind is present. Names outside the scope are
/// subquery-local — `EXISTS { MATCH (z) … }` names `z`, which the
/// subquery itself binds — so they never gate readiness; the caller's
/// later predicate pass covers an expression that is not ready here.
pub(crate) fn conjunct_ready(expr: &Expr, row: &Row, scope: &HashSet<String>) -> bool {
    let mut vars = HashSet::new();
    expr_vars(expr, &mut vars);
    vars.retain(|v| scope.contains(v));
    vars.iter().all(|v| row.contains_key(v))
}

/// The degenerate residual pass — no settled conjuncts — the one-line
/// form every no-skip caller reads; the body lives once in
/// [`residual_ok_skipping`].
pub(crate) fn residual_ok(
    graph: &ExecGraph<'_>,
    row: &Row,
    params: &BTreeMap<String, Value>,
    residual: &Residual<'_>,
) -> Result<bool> {
    residual_ok_skipping(graph, row, params, residual, &[])
}

/// The residual's conjuncts that are ready on `row` — every scope name
/// they reference bound — plus, when the caller's loop binds one more
/// variable on every candidate before the residual runs (`binding`, the
/// element's own variable), the conjuncts that become ready with it —
/// minus the conjuncts whose indices `settled` names: the scan-predicate
/// gate already evaluated each of those for exactly their one variable
/// under the candidate's own store read, and the compile rules
/// (predicate_spec.rs) exist to make that evaluation's inputs identical
/// to this pass's, so the re-run is redundant work, never a second
/// verdict. A skipped conjunct is known-true for the row — the gate
/// rejected every candidate it failed. Indices are into the `Residual`'s
/// own conjunct slice, the one slice the compiling walk and every one of
/// its residual passes share. Computed **once per candidate loop**:
/// readiness and settledness are facts of the walk, not of any one
/// candidate, so a scan-side loop that re-derives them per candidate
/// pays a variable-set allocation and an expression walk per scanned
/// node for an answer that never changes (GBL SF0.1 q8: 13,750
/// label-scan candidates re-derived it per forum).
pub(crate) fn ready_conjuncts<'a>(
    residual: &'a Residual<'_>,
    row: &Row,
    binding: Option<&SmallName>,
    settled: &[usize],
) -> Vec<&'a Expr> {
    residual
        .conjuncts
        .iter()
        .enumerate()
        .filter(|(index, c)| {
            if settled.contains(index) {
                return false;
            }
            let mut vars = HashSet::new();
            expr_vars(c, &mut vars);
            vars.retain(|v| residual.scope.contains(v));
            if let Some(bound) = binding {
                bound.with_str(|text| {
                    vars.remove(text);
                });
            }
            vars.iter().all(|v| row.contains_key(v))
        })
        .map(|(_, c)| c)
        .collect()
}

/// Whether every conjunct of `ready` (a [`ready_conjuncts`] list) holds on
/// `row`: the per-candidate half of [`residual_ok`], the evaluation the
/// once-per-loop readiness derivation feeds.
pub(crate) fn conjuncts_hold(
    graph: &ExecGraph<'_>,
    row: &Row,
    ready: &[&Expr],
    params: &BTreeMap<String, Value>,
) -> Result<bool> {
    for c in ready {
        if !predicate_holds(graph, row, c, params)? {
            return Ok(false);
        }
    }
    Ok(true)
}

/// The single-candidate residual pass ([`residual_ok`] with skips): the
/// backward walker's per-candidate spelling of the same selection
/// [`ready_conjuncts`] makes — the settled conjuncts skipped, the rest
/// evaluated when ready on the bound row — for callers with no candidate
/// loop to hoist the derivation out of.
pub(crate) fn residual_ok_skipping(
    graph: &ExecGraph<'_>,
    row: &Row,
    params: &BTreeMap<String, Value>,
    residual: &Residual<'_>,
    settled: &[usize],
) -> Result<bool> {
    let ready = ready_conjuncts(residual, row, None, settled);
    conjuncts_hold(graph, row, &ready, params)
}

pub(crate) fn flatten_and_owned(expr: &Expr) -> Vec<Expr> {
    match expr {
        Expr::Binary {
            op: BinaryOp::And,
            left,
            right,
        } => {
            let mut v = flatten_and_owned(left);
            v.extend(flatten_and_owned(right));
            v
        }
        other => vec![other.clone()],
    }
}

/// The variables a projection (`RETURN` / `WITH`) still needs from the
/// carried row: its item expressions, its explicit grouping keys (CIP-236:
/// evaluated *before* projection), its sort keys, its `SKIP`/`LIMIT`, and —
/// for `WITH` — its `WHERE`. One helper for both clause arms, so a new
/// projection field lands in one place.
fn projection_needs(fields: &ProjectionFields<'_>, needed: &mut HashSet<String>) {
    for i in fields.items {
        expr_vars(&i.expr, needed);
    }
    if let Some(keys) = fields.group_by {
        for k in keys {
            expr_vars(k, needed);
        }
    }
    for o in fields.order {
        expr_vars(&o.expr, needed);
    }
    if let Some(s) = fields.skip {
        expr_vars(s, needed);
    }
    if let Some(l) = fields.limit {
        expr_vars(l, needed);
    }
    if let Some(w) = fields.where_clause {
        expr_vars(w, needed);
    }
}

/// A `RETURN`/`WITH` projection's live set: `None` under a `*` item (the
/// star expands the whole row) or when any of its expressions carries a
/// scope-bearing form, otherwise the names its fields need.
///
/// An expression subquery (`EXISTS`/`COUNT`/`COLLECT`) is seeded at runtime
/// from the *whole* enclosing row, and its body may reach outer names through
/// forms `expr_vars` does not enumerate (a naked `CALL { … }` body, a
/// `CALL (vars)` scope list, an inner `CALL`'s import list). Trimming to the
/// statically-referenced names would silently drop such an import, so the
/// projection keeps every incoming column.
fn projection_live(
    fields: &ProjectionFields<'_>,
    needed: &mut HashSet<String>,
) -> Option<HashSet<String>> {
    if has_star_item(fields.items) || projection_has_scope_bearing_expr(fields) {
        return None;
    }
    projection_needs(fields, needed);
    Some(std::mem::take(needed))
}

/// Whether any expression the projection evaluates carries a scope-bearing
/// form (a subquery, pattern predicate/comprehension, or `shortestPath`).
fn projection_has_scope_bearing_expr(fields: &ProjectionFields<'_>) -> bool {
    fn scope_bearing(expr: &Expr) -> bool {
        expr.any(&mut |node| node.is_scope_bearing())
    }
    fields.items.iter().any(|item| scope_bearing(&item.expr))
        || fields
            .group_by
            .as_ref()
            .is_some_and(|keys| keys.iter().any(scope_bearing))
        || fields.order.iter().any(|item| scope_bearing(&item.expr))
        || fields.skip.is_some_and(scope_bearing)
        || fields.limit.is_some_and(scope_bearing)
        || fields.where_clause.is_some_and(scope_bearing)
}

pub(crate) fn live_from(suffix: &[Clause]) -> Option<HashSet<String>> {
    let mut needed = HashSet::new();
    for c in suffix {
        match c {
            Clause::Catalog(_) => return None,
            Clause::Match {
                patterns,
                where_clause,
                search,
                ..
            } => {
                add_pattern_names(patterns, &mut needed);
                if let Some(w) = where_clause {
                    expr_vars(w, &mut needed);
                }
                if let Some(search) = search {
                    for expr in search.spec.exprs() {
                        expr_vars(expr, &mut needed);
                    }
                }
            }
            Clause::Create { patterns } => add_pattern_names(patterns, &mut needed),
            Clause::CreateIndex { .. }
            | Clause::DropIndex { .. }
            | Clause::ShowIndexes
            | Clause::Finish => {}
            Clause::LoadCsv { url, .. } => expr_vars(url, &mut needed),
            Clause::CallSubquery {
                in_transactions,
                scope,
                ..
            } => {
                // A whole-row scope keeps everything live; an import list
                // keeps the names it names (the one `call_scope_reads` rule).
                let vars = call_scope_reads(scope)?;
                needed.extend(vars.iter().map(|name| name.to_string()));
                if let Some(tx) = in_transactions {
                    if let Some(n) = &tx.batch_size {
                        expr_vars(n, &mut needed);
                    }
                }
            }
            Clause::Merge {
                pattern,
                on_create,
                on_match,
            } => {
                add_pattern_names(std::slice::from_ref(pattern), &mut needed);
                for s in on_create.iter().chain(on_match.iter()) {
                    set_item_vars(s, &mut needed);
                }
            }
            Clause::Delete { expressions, .. } => {
                for e in expressions {
                    expr_vars(e, &mut needed);
                }
            }
            Clause::Set { items } => {
                for i in items {
                    set_item_vars(i, &mut needed);
                }
            }
            Clause::Remove { items } => {
                for i in items {
                    remove_item_vars(i, &mut needed);
                }
            }
            Clause::Unwind { expression, .. } => expr_vars(expression, &mut needed),
            Clause::Call { args, .. } => {
                for a in args.args() {
                    expr_vars(a, &mut needed);
                }
            }
            Clause::Conditional { branches, .. } => {
                for (predicate, _) in branches {
                    expr_vars(predicate, &mut needed);
                }
                return None;
            }
            Clause::Foreach { list, .. } => {
                expr_vars(list, &mut needed);
                return None;
            }
            // A bare USE graph name is a catalog identifier, not a row variable.
            Clause::Use { .. } => {}
            Clause::Return { .. } | Clause::With { .. } => {
                let fields = ProjectionFields::from_clause(c)
                    .expect("Return/With always extract to ProjectionFields");
                return projection_live(&fields, &mut needed);
            }
        }
    }
    None
}

pub(crate) fn add_pattern_names(patterns: &[PathPattern], out: &mut HashSet<String>) {
    for p in patterns {
        if let Some(v) = &p.variable {
            out.insert(v.to_string());
        }
        if let Some(predicate) = p.where_clause.as_deref() {
            expr_vars(predicate, out);
        }
        for n in &p.nodes {
            if let Some(v) = &n.variable {
                out.insert(v.to_string());
            }
            if let Some(props) = &n.properties {
                expr_vars(props, out);
            }
            if let Some(expr) = &n.label_expr {
                label_expr_vars(expr, out);
            }
        }
        for r in &p.rels {
            if let Some(v) = &r.variable {
                out.insert(v.to_string());
            }
            if let Some(props) = &r.properties {
                expr_vars(props, out);
            }
            if let Some(expr) = &r.type_expr {
                label_expr_vars(expr, out);
            }
        }
        add_gpm_element_names(&p.elements, out);
    }
}

pub(crate) fn set_item_vars(item: &SetItem, out: &mut HashSet<String>) {
    match item {
        SetItem::Property { target, value }
        | SetItem::ReplaceAll { target, value }
        | SetItem::MergeMap { target, value } => {
            expr_vars(target, out);
            expr_vars(value, out);
        }
        SetItem::Labels { target, labels } => {
            expr_vars(target, out);
            for label in labels {
                label_expr_vars(label, out);
            }
        }
        SetItem::LabelsFunction { target, value, .. } => {
            expr_vars(target, out);
            expr_vars(value, out);
        }
    }
}

pub(crate) fn remove_item_vars(item: &RemoveItem, out: &mut HashSet<String>) {
    match item {
        RemoveItem::Property { target } => expr_vars(target, out),
        RemoveItem::Labels { target, labels } => {
            expr_vars(target, out);
            for label in labels {
                label_expr_vars(label, out);
            }
        }
    }
}

pub(crate) fn trim_rows(rows: &mut [Row], live: &HashSet<String>) {
    for row in rows {
        row.retain(|k, _| live.contains(k) || is_internal_column(k));
    }
}

pub(crate) fn match_scope(row: &Row, patterns: &[PathPattern]) -> HashSet<String> {
    let mut s: HashSet<String> = row
        .keys()
        .filter(|k| !is_internal_column(k))
        .cloned()
        .collect();
    for pattern in patterns {
        for_each_pattern_bound_name(pattern, &mut |name| {
            s.insert(name.to_string());
        });
    }
    s
}

pub(crate) fn anon_name() -> String {
    format!("__a{}", ANON.fetch_add(1, Ordering::Relaxed))
}