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
//! Depth-first existence over planner-ordered MATCH patterns.
//!
//! [`match_paths`](super::match_clause::match_paths) finishes one pattern's
//! `Vec` before starting the next, so a row cap on the last pattern cannot
//! stop the first. This probe offers each complete binding of pattern N to
//! pattern N+1 immediately and returns at the first binding that satisfies
//! the predicate.

use crate::UsedSet;
use std::cell::Cell;
use std::collections::BTreeMap;

use relate_core::{Result, Value};
use relate_ir::{
    classify_aggregate, AggregateKind, BinaryOp, Clause, Expr, MatchMode, PathPattern, Projection,
};

use crate::ctx::ExecCtx;
use crate::eval::eval;
use crate::overlay::ExecGraph;
use crate::pattern_walk::resolved_endpoints_exist;
use crate::row::Row;

use super::match_folds::FoldCtx;
use super::one_path::{match_one_path, PathFilters};
use super::pattern_policies::MatchTail;
use super::pushdown::{flatten_and_owned, match_scope, FoldedReturn, Residual};
use super::rider::{PathProbe, Rider};

/// What the existence probe counts as a hit — the two shapes it serves:
/// every complete binding (a multi-pattern `EXISTS { MATCH … }`), or the
/// boolean-count fold's `count(DISTINCT expr)` being non-null.
#[derive(Clone, Copy)]
pub(crate) enum ExistencePred<'a> {
    /// Every complete binding counts.
    Any,
    /// A complete binding counts when the expression is non-null (the
    /// boolean-count fold's counted expression).
    Counted(&'a Expr),
}

impl ExistencePred<'_> {
    /// Whether one complete binding counts — the row-shaped evaluation
    /// the probe's DFS makes per binding.
    fn satisfied(
        &self,
        graph: &ExecGraph<'_>,
        row: &Row,
        params: &BTreeMap<String, Value>,
    ) -> Result<bool> {
        match self {
            ExistencePred::Any => Ok(true),
            ExistencePred::Counted(expr) => Ok(!eval(graph, row, expr, params)?.is_null()),
        }
    }
}

/// `Ok(None)` when the patterns are not walkable by the probe (empty, or
/// a shortest-path function whose uniqueness semantics are the GPM
/// route's). GPM patterns (every var-length spelling since ADR-0471)
/// walk the GPM engine with a probe rider — restoring the early exit the
/// unification's Consequences record losing; the classic shapes keep the
/// LABS-193/LABS-249 walk below.
pub(crate) fn patterns_exist(
    ctx: ExecCtx<'_>,
    patterns: &[PathPattern],
    wheres: &[Expr],
    row: Row,
    pred: ExistencePred<'_>,
) -> Result<Option<bool>> {
    if patterns.is_empty() || patterns.iter().any(|path| path.is_shortest_path_function) {
        return Ok(None);
    }
    // ADR-0473: a contracted kernel takes the existence walk when the
    // plan matches a family and the cost gate pays — the hub-driven
    // per-hub combination with its early exit (q30's shape), or the
    // unrolled chain. `None` keeps the probe's depth-first seek. Every
    // caller's patterns came through `plain_match`, which guarantees the
    // `DIFFERENT RELATIONSHIPS` mode the kernel's uniqueness rule needs.
    let counted = match &pred {
        ExistencePred::Counted(expr) => Some(*expr),
        ExistencePred::Any => None,
    };
    if let Some(hit) = super::kernels::try_exists_kernel(
        ctx,
        patterns,
        wheres,
        &row,
        MatchMode::DifferentRelationships,
        counted,
    )? {
        // The kernel arm serves the same full-pattern walk the seek below
        // is — the clause-engine pass it replaces — so it ticks the
        // nested-evaluation budget once per evaluation too (ADR-0395 over
        // ADR-0473): no arm of this probe is the evaluation route that
        // escapes the accounting. The tick rides the engaged answer, never
        // the decline — a declined kernel falls through to the seek's own
        // tick below, so one evaluation spends exactly one either way. It
        // lands after the walk (the seek's tick lands before its walk)
        // because the kernel's own cost gate bounds the walk it admits,
        // while the seek's walk is unbounded a priori — that bound is what
        // the pre-walk tick buys there.
        ctx.graph.nested_evals().tick()?;
        return Ok(Some(hit));
    }
    let conjuncts: Vec<Expr> = wheres.iter().flat_map(flatten_and_owned).collect();
    let scope = match_scope(ctx.graph, &row, patterns);
    let residual = Residual {
        conjuncts: &conjuncts,
        scope: &scope,
    };
    // Unlike the bounded LABS-193 anchored probe (which answers without
    // ticking, ADR-0395), this depth-first probe is a full pattern walk —
    // the clause-engine pass it replaces — so it ticks the
    // nested-evaluation budget once per evaluation, keeping the security
    // bound on compounding EXISTS chains (the classic and GPM arms alike;
    // the kernel arm ticks at its own return above).
    ctx.graph.nested_evals().tick()?;
    Ok(Some(seek(
        ctx,
        patterns,
        0,
        row,
        UsedSet::default(),
        &residual,
        pred,
    )?))
}

fn seek(
    ctx: ExecCtx<'_>,
    patterns: &[PathPattern],
    idx: usize,
    row: Row,
    used: UsedSet,
    residual: &Residual<'_>,
    pred: ExistencePred<'_>,
) -> Result<bool> {
    if idx >= patterns.len() {
        let graph = ctx.graph;
        return pred.satisfied(graph, &row, ctx.params);
    }
    let halt = Cell::new(false);
    let graph = ctx.graph;
    let params = ctx.params;
    let accept = |row2: Row, used2: UsedSet| -> Result<bool> {
        seek(
            ExecCtx::new(graph, params),
            patterns,
            idx + 1,
            row2,
            used2,
            residual,
            pred,
        )
    };
    // The one engine fork is `match_one_path`'s — this probe routes
    // through it with a probe as its rider (F-1-3), never a fork of its
    // own. A GPM pattern (a quantified element, a path mode or selector,
    // an element or path predicate — every var-length spelling among
    // them) walks the GPM engine with the early-exit rider: the first
    // complete row the chain accepts halts the enumeration, so the
    // remaining frontier is never evaluated (the ADR-0471 Consequences
    // follow-up; `match_gpm_path` applies the residual's ready conjuncts
    // at the walk's binding points and the rest on each offered row). A
    // classic pattern walks the classic engine, where the same rider
    // becomes the probe sink — the same early exit, one spelling of the
    // engine choice (LABS-161: the first-hop rel seek narrows the
    // probe's walk the same way it narrows every classic match's; a Gone
    // seek probes false).
    let probe = PathProbe::new(&accept, &halt, &patterns[idx]);
    let mut rider = Some(Rider::Probe(&probe));
    // The probe's tail is the carry (`MatchTail::Carry`,
    // pattern_policies): the named fact that a probe route never reduces
    // — a probe answers a verdict through its halt cell, and no probe
    // consumer reads the tail. The filters resolve once per probe
    // (LABS-801), beside the probe's own construction.
    let filters =
        PathFilters::resolve(ctx.graph, &patterns[idx], MatchMode::DifferentRelationships);
    match_one_path(
        ctx,
        &patterns[idx],
        &filters,
        row,
        used,
        MatchMode::DifferentRelationships,
        residual,
        rider.as_mut(),
        MatchTail::Carry,
    )?;
    Ok(halt.get())
}

struct PlainMatch<'a> {
    patterns: &'a [PathPattern],
    where_clause: Option<&'a Expr>,
}

/// A non-optional, non-search `MATCH` in `DIFFERENT RELATIONSHIPS` mode.
/// The one decoder of the plain-MATCH vocabulary (optional, search, walk
/// mode): every eligibility in this module — the boolean-count span, the
/// probe ladder's endpoint rung, the depth-first walk — reads it, never a
/// second spelling of the discrimination.
fn plain_match(clause: &Clause) -> Option<PlainMatch<'_>> {
    let Clause::Match {
        optional,
        match_mode,
        patterns,
        where_clause,
        search,
        using_index: _,
        using_join: _,
    } = clause
    else {
        return None;
    };
    if *optional || search.is_some() || *match_mode != MatchMode::DifferentRelationships {
        return None;
    }
    Some(PlainMatch {
        patterns,
        where_clause: where_clause.as_ref(),
    })
}

/// The clause-list shape the `EXISTS` probe decodes: exactly one plain
/// `MATCH` clause. Relationship uniqueness is scoped to one clause, so
/// the patterns of two clauses cannot be probed as one `DIFFERENT
/// RELATIONSHIPS` list — the general engine answers multi-clause
/// bodies.
fn single_plain_match(clauses: &[Clause]) -> Option<PlainMatch<'_>> {
    let [clause] = clauses else {
        return None;
    };
    plain_match(clause)
}

struct BoolCount<'a> {
    /// The one decoded clause (issue 84) — its patterns and WHERE are
    /// the fold's span, held decoded, not re-spread field by field.
    plain: PlainMatch<'a>,
    output: String,
    counted: &'a Expr,
}

/// One plain `MATCH` clause followed by an ungrouped
/// `RETURN count(DISTINCT expr) > 0` or `>= 1` — the threshold literal or
/// a parameter resolving to it (see [`counted_existence`]). The `MATCH`
/// decodes through [`plain_match`], the module's one plain-MATCH
/// decoder: relationship uniqueness is scoped to a clause, so two
/// clauses' patterns cannot be probed as one list (issue 84). The
/// empty-patterns decline is [`patterns_exist`]'s own gate, not spelled
/// again here.
fn boolean_count_span<'a>(
    clauses: &'a [Clause],
    params: &BTreeMap<String, Value>,
) -> Option<BoolCount<'a>> {
    let [match_clause, ret] = clauses else {
        return None;
    };
    let plain = plain_match(match_clause)?;
    let Clause::Return {
        distinct,
        group_by,
        items,
        order,
        skip,
        limit,
    } = ret
    else {
        return None;
    };
    if *distinct
        || group_by.is_some()
        || !order.is_empty()
        || skip.is_some()
        || limit.is_some()
        || items.len() != 1
    {
        return None;
    }
    let Projection { expr, .. } = &items[0];
    let counted = counted_existence(expr, params)?;
    Some(BoolCount {
        plain,
        output: items[0].output_name(0),
        counted,
    })
}

/// `count(DISTINCT expr) > 0` or `count(DISTINCT expr) >= 1` — the
/// threshold as a literal, or as a parameter that resolves to it for this
/// execution (ADR-0503: an auto-parameterized `> $__AUTO_INT_n__` binds 0
/// or 1 here exactly as the spelled literal did, and a caller's own
/// `> $threshold` gains the same recognition; a parameter resolving to
/// anything else, or absent, keeps the full-count path — the safe arm of
/// every decline in this recognizer).
fn counted_existence<'a>(expr: &'a Expr, params: &BTreeMap<String, Value>) -> Option<&'a Expr> {
    let Expr::Binary { op, left, right } = expr else {
        return None;
    };
    let threshold = match op {
        BinaryOp::Gt => 0,
        BinaryOp::Gte => 1,
        _ => return None,
    };
    // The one constant policy (`Expr::seek_value`: literals fold,
    // parameters resolve) — a non-int literal or a non-int/missing
    // parameter declines here exactly as before.
    if right.seek_value(params) != Some(Value::Int(threshold)) {
        return None;
    }
    let Expr::Function {
        name,
        distinct,
        args,
    } = left.as_ref()
    else {
        return None;
    };
    if !*distinct
        || args.len() != 1
        || name.with_str(classify_aggregate) != Some(AggregateKind::Count)
    {
        return None;
    }
    Some(&args[0])
}

/// `Ok(None)` keeps the full-match path. The returned fold already includes
/// the consumed `RETURN`.
pub(crate) fn try_fold_boolean_count(ctx: &FoldCtx<'_>) -> Result<Option<FoldedReturn>> {
    let (graph, rows, clauses, params) = (&ctx.graph, ctx.rows, ctx.clauses, ctx.params);
    let Some(span) = boolean_count_span(clauses, params) else {
        return Ok(None);
    };
    // WHERE conjuncts are the residual on the walk. This predicate only
    // decides whether the counted expression is non-null.
    let pred = ExistencePred::Counted(span.counted);
    let mut hit = false;
    // The one clause's WHERE as the residual list `patterns_exist`
    // takes — at most one element, so an `Option` in the decoded
    // clause.
    let wheres: &[Expr] = match span.plain.where_clause {
        Some(expr) => std::slice::from_ref(expr),
        None => &[],
    };
    for row in rows {
        graph.cancel_tick()?;
        match patterns_exist(
            ExecCtx::new(graph, params),
            span.plain.patterns,
            wheres,
            row.clone(),
            pred,
        )? {
            None => return Ok(None),
            Some(true) => {
                hit = true;
                break;
            }
            Some(false) => {}
        }
    }
    let mut out = Row::new();
    out.insert(span.output.clone(), Value::Bool(hit));
    Ok(Some(FoldedReturn {
        result: super::QueryResult::intermediate(
            vec![span.output],
            vec![vec![Value::Bool(hit)]],
            graph.stats(),
        ),
        rows: vec![out],
        // The RETURN this fold consumed — the span's one MATCH clause
        // is the clause being dispatched.
        consumed_after: 1,
    }))
}

/// `EXISTS { MATCH ... }` bodies of one plain (non-optional, non-search,
/// WHERE-less, DIFFERENT RELATIONSHIPS) MATCH clause, one pattern or
/// many — the one clause-level probe ladder. The clause decodes through
/// [`single_plain_match`] exactly once here; a decoded list holding
/// exactly one pattern answers on the LABS-193 endpoint probe first, and
/// a declined shape (multi-hop, var-length, GPM) and every other
/// eligible body walk the depth-first probe, which stops at the first
/// hit. `Ok(None)` keeps the subquery engine; the empty-patterns decline
/// is [`patterns_exist`]'s own gate, not spelled again here.
pub(crate) fn probe_multi_pattern_exists(
    graph: &ExecGraph<'_>,
    row: &Row,
    clauses: &[Clause],
    params: &BTreeMap<String, Value>,
) -> Result<Option<bool>> {
    let Some(plain) = single_plain_match(clauses) else {
        return Ok(None);
    };
    if plain.where_clause.is_some() {
        return Ok(None);
    }
    // The LABS-193 endpoint rung. GPM eligibility is decided where it is
    // owned: needs_gpm in resolved_endpoints_exist (the path-level
    // gates), the walk mode in the plain-MATCH vocabulary plain_match
    // owns. A decline falls through to the depth-first walk below —
    // through the GPM engine with the early-exit rider for GPM shapes
    // (ADR-0471's lost early exit restored), through the classic
    // ProbeSink otherwise.
    if let [pattern] = plain.patterns {
        if let Some(hit) = resolved_endpoints_exist(graph, row, pattern, params)? {
            return Ok(Some(hit));
        }
    }
    patterns_exist(
        ExecCtx::new(graph, params),
        plain.patterns,
        &[],
        row.clone(),
        ExistencePred::Any,
    )
}

#[cfg(test)]
mod threshold_tests {
    use std::collections::BTreeMap;

    use relate_core::Value;
    use relate_ir::{BinaryOp, Expr};

    use super::counted_existence;

    fn count_cmp(op: BinaryOp, bound: Expr) -> Expr {
        Expr::Binary {
            op,
            left: Box::new(Expr::Function {
                name: "count".into(),
                distinct: true,
                args: vec![Expr::Variable("n".into())],
            }),
            right: Box::new(bound),
        }
    }

    fn count_gt(bound: Expr) -> Expr {
        count_cmp(BinaryOp::Gt, bound)
    }

    fn no_params() -> BTreeMap<String, Value> {
        BTreeMap::new()
    }

    fn params(name: &str, value: Value) -> BTreeMap<String, Value> {
        BTreeMap::from([(name.to_string(), value)])
    }

    /// The literal thresholds keep their recognition: `> 0` and `>= 1`.
    #[test]
    fn literal_thresholds_recognize() {
        assert!(counted_existence(&count_gt(Expr::Int(0)), &no_params()).is_some());
        let gte = count_cmp(BinaryOp::Gte, Expr::Int(1));
        assert!(counted_existence(&gte, &no_params()).is_some());
        // Any other literal declines.
        assert!(counted_existence(&count_gt(Expr::Int(1)), &no_params()).is_none());
    }

    /// ADR-0503: a parameter resolving to the threshold recognizes exactly
    /// as the literal did — the auto-parameterized `> $__AUTO_INT_n__`
    /// binds 0, and a caller's own `> $zero` gains the same early exit.
    /// Any other value, a missing parameter, or a non-integer binding
    /// keeps the full-count path.
    #[test]
    fn parameter_thresholds_resolve_or_decline() {
        let gt_param = count_gt(Expr::Parameter("zero".into()));
        assert!(counted_existence(&gt_param, &params("zero", Value::Int(0))).is_some());
        assert!(counted_existence(&gt_param, &params("zero", Value::Int(1))).is_none());
        assert!(counted_existence(&gt_param, &params("other", Value::Int(0))).is_none());
        assert!(counted_existence(&gt_param, &params("zero", Value::String("0".into()))).is_none());
        assert!(counted_existence(&gt_param, &no_params()).is_none());
    }
}