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
//! DISTINCT and row-limit caps for MATCH expansion.

use std::collections::BTreeMap;

use relate_core::{Result, Value};
use relate_ir::{Clause, Direction, Expr, OrderItem, PathPattern, Projection};
use relate_names::SmallName;

use crate::distinct_set::DistinctSet;
use crate::eval::{eval, is_agg_expr, var_prop};
use crate::overlay::ExecGraph;
use crate::row::Row;

use super::order::resolve_page_bounds;
use super::order::window_capacity;
use super::sink::SinkProjection;

/// Stop MATCH once `RETURN/WITH DISTINCT … LIMIT n` has enough distinct keys.
/// OpenCypher does not specify which rows `LIMIT` keeps without `ORDER BY`.
pub(crate) struct DistinctLimitCap<'a> {
    items: &'a [Projection],
    params: &'a BTreeMap<String, Value>,
    seen: DistinctSet,
    want: usize,
}

impl DistinctLimitCap<'_> {
    fn full(&self) -> bool {
        self.seen.len() >= self.want
    }

    pub(crate) fn offer(&mut self, graph: &ExecGraph<'_>, row: &Row) -> Result<()> {
        if self.full() {
            return Ok(());
        }
        // The arity-1 keying (a one-column row is its value's key) lives in
        // `DistinctSet::insert_row` alone.
        let mut vals = Vec::with_capacity(self.items.len());
        for p in self.items {
            vals.push(eval(graph, row, &p.expr, self.params)?);
        }
        self.seen.insert_row(&vals)?;
        Ok(())
    }
}

/// The RETURN/WITH suffix fields every MATCH cap gate reads: the first
/// clause of the suffix, destructured once (`distinct_limit_cap`,
/// `ordered_limit_cap`, and `plain_limit_cap` each apply only their own
/// shape gates on top of this one parse — and the frontier matcher arm's
/// answer-shape decoder, exec/kernels/frontier_match.rs, reads the same parse).
pub(crate) struct ReturnWithSuffix<'a> {
    pub(crate) distinct: bool,
    /// CIP-236: an explicit GROUP BY changes what the clause projects
    /// (grouped rows, not one row per match), so a per-match cap no
    /// longer describes the output — every cap declines it.
    pub(crate) group_by: Option<&'a [Expr]>,
    pub(crate) items: &'a [Projection],
    pub(crate) order: &'a [OrderItem],
    pub(crate) skip: Option<&'a Expr>,
    pub(crate) limit: Option<&'a Expr>,
    pub(crate) where_clause: Option<&'a Expr>,
}

/// The first suffix clause as a `RETURN` or `WITH` (`None` for anything
/// else): the one parse every cap constructor reads its shape from.
pub(crate) fn return_with_suffix(clause: &Clause) -> Option<ReturnWithSuffix<'_>> {
    match clause {
        Clause::Return {
            distinct,
            group_by,
            items,
            order,
            skip,
            limit,
        } => Some(ReturnWithSuffix {
            distinct: *distinct,
            group_by: group_by.as_deref(),
            items,
            order,
            skip: skip.as_ref(),
            limit: limit.as_ref(),
            where_clause: None,
        }),
        Clause::With {
            distinct,
            group_by,
            items,
            order,
            skip,
            limit,
            where_clause,
            ..
        } => Some(ReturnWithSuffix {
            distinct: *distinct,
            group_by: group_by.as_deref(),
            items,
            order,
            skip: skip.as_ref(),
            limit: limit.as_ref(),
            where_clause: where_clause.as_ref(),
        }),
        _ => None,
    }
}

impl ReturnWithSuffix<'_> {
    /// Whether the clause carries a `*` or aggregate item — grouped
    /// output, not one row per match, which no per-match cap describes
    /// (CIP-236, the `group_by` note's rule for the inferred form).
    fn star_or_aggregate(&self) -> bool {
        self.items
            .iter()
            .any(|p| matches!(p.expr, Expr::Star) || is_agg_expr(&p.expr))
    }

    /// Whether the suffix's clause outputs one row per matched row — the
    /// per-match caps' shared fact: no explicit GROUP BY (grouped rows,
    /// CIP-236), no following WHERE (its filter re-filters rows the walk
    /// would have to produce whole), and no `*` or aggregate item
    /// (grouped output again). [`row_limit_cap`] and
    /// [`distinct_limit_cap`] both read this; each keeps only its own
    /// polarity and its own `ORDER BY`/`SKIP` gates.
    fn one_row_per_match(&self) -> bool {
        self.group_by.is_none() && self.where_clause.is_none() && !self.star_or_aggregate()
    }

    /// The row count the suffix's `SKIP s LIMIT n` truncates the clause's
    /// output to (`s + n`): the want both row-limit caps share. `None`
    /// when the suffix carries no `LIMIT` or the add overflows. Both
    /// bounds resolve through the one paging spelling
    /// ([`resolve_page_bounds`]; a missing `SKIP` defaults to 0), so the
    /// cap's bound errors are the projection's own. `distinct_limit_cap`
    /// reaches this only after declining any `SKIP`, so its want there is
    /// the bare `LIMIT` count.
    pub(crate) fn skip_limit_want(
        &self,
        graph: &ExecGraph<'_>,
        params: &BTreeMap<String, Value>,
    ) -> Result<Option<usize>> {
        if self.limit.is_none() {
            return Ok(None);
        }
        let bounds = resolve_page_bounds(graph, params, self.skip, self.limit)?;
        Ok(window_capacity(bounds.skip, bounds.limit))
    }
}

pub(crate) fn distinct_limit_cap<'a>(
    suffix: &'a [Clause],
    graph: &ExecGraph<'_>,
    params: &'a BTreeMap<String, Value>,
) -> Result<Option<DistinctLimitCap<'a>>> {
    let Some(clause) = suffix.first() else {
        return Ok(None);
    };
    let Some(s) = return_with_suffix(clause) else {
        return Ok(None);
    };
    if !s.distinct || !s.one_row_per_match() || !s.order.is_empty() || s.skip.is_some() {
        return Ok(None);
    }
    let Some(want) = s.skip_limit_want(graph, params)? else {
        return Ok(None);
    };
    Ok(Some(DistinctLimitCap {
        items: s.items,
        params,
        seen: DistinctSet::default(),
        want,
    }))
}

/// Stop MATCH once `SKIP+LIMIT` visible rows have been produced: the row
/// counter behind both limit caps. `Ordered`'s gate (an `ORDER BY r.ts
/// DESC LIMIT` whose walk order is the stored typed-chunk order,
/// ADR-0035) and `Plain`'s (an un-ordered `LIMIT`, below) construct it.
/// It counts every offered row: a `WHERE`-bearing walk the plain cap
/// rides runs the clause filter in its sink ahead of the count
/// (`match_clause`'s filter-only reduce, LABS-853), so a row the filter
/// drops is never offered.
pub(crate) struct RowLimitCap {
    want: usize,
    produced: usize,
}

impl RowLimitCap {
    fn full(&self) -> bool {
        self.produced >= self.want
    }
}

pub(crate) enum MatchCap<'a> {
    Distinct(DistinctLimitCap<'a>),
    Ordered(RowLimitCap),
    Plain(RowLimitCap),
}

impl MatchCap<'_> {
    pub(crate) fn full(&self) -> bool {
        match self {
            MatchCap::Distinct(c) => c.full(),
            MatchCap::Ordered(c) | MatchCap::Plain(c) => c.full(),
        }
    }

    /// The plain cap's want — the row count the walk halts at — when this
    /// is a plain cap (the ladder's one yielding shape; ordered and
    /// distinct caps never yield, so they answer `None`). The contracted
    /// kernels read it as their rows-mode halt (LABS-990): the same
    /// count, the same prefix-contract-free semantics, spelled at the
    /// kernel's own final collection instead of the walker's sink. Its
    /// WHERE-exactness has a third end beside the two below: the
    /// kernels' compiled admissions are their WHERE (a conjunct outside
    /// the closed vocabulary declines the kernel), so only kept rows
    /// land and the count stays exact without a sink filter.
    pub(crate) fn plain_want(&self) -> Option<usize> {
        match self {
            MatchCap::Plain(c) => Some(c.want),
            MatchCap::Ordered(_) | MatchCap::Distinct(_) => None,
        }
    }

    pub(crate) fn offer(&mut self, graph: &ExecGraph<'_>, row: &Row) -> Result<()> {
        match self {
            MatchCap::Distinct(c) => c.offer(graph, row),
            MatchCap::Ordered(c) | MatchCap::Plain(c) => {
                if !c.full() {
                    c.produced += 1;
                }
                Ok(())
            }
        }
    }
}

pub(crate) fn rel_var_of_ts(expr: &Expr) -> Option<&SmallName> {
    var_prop(expr)
        .filter(|(_, key)| *key == "ts")
        .map(|(var, _)| var)
}

pub(crate) fn order_rel_ts_desc_var<'a>(
    order: &'a [relate_ir::OrderItem],
    items: &'a [Projection],
) -> Option<&'a SmallName> {
    let item = order.first()?;
    if order.len() != 1 || item.ascending {
        return None;
    }
    if let Some(v) = rel_var_of_ts(&item.expr) {
        return Some(v);
    }
    if let Expr::Variable(name) = &item.expr {
        for (i, p) in items.iter().enumerate() {
            if name == p.output_name(i).as_str() {
                return rel_var_of_ts(&p.expr);
            }
        }
    }
    None
}

pub(crate) fn ordered_expand_pattern(patterns: &[PathPattern], rel_var: &SmallName) -> bool {
    if patterns.len() != 1 {
        return false;
    }
    let p = &patterns[0];
    if p.rels.len() != 1 {
        return false;
    }
    let rp = &p.rels[0];
    !rp.var_length
        && rp.types.len() == 1
        && rp.direction != Direction::Both
        && rp.variable.as_ref() == Some(rel_var)
}

/// The decline vocabulary and the want-to-counter tail both row-limit
/// caps share (`ordered_limit_cap`'s and `plain_limit_cap`'s): the
/// DISTINCT decline (the distinct cap owns that shape) plus the
/// [`ReturnWithSuffix::one_row_per_match`] decline (grouped or re-filtered
/// output is not one row per match), then the want both resolve through
/// [`ReturnWithSuffix::skip_limit_want`], the decline of an empty want
/// (`LIMIT 0`: nothing to halt on), and the `RowLimitCap` counter
/// itself. Each cap function keeps only its own gates on top of this
/// one plumbing.
fn row_limit_cap(
    suffix: &ReturnWithSuffix<'_>,
    graph: &ExecGraph<'_>,
    params: &BTreeMap<String, Value>,
) -> Result<Option<RowLimitCap>> {
    if suffix.distinct || !suffix.one_row_per_match() {
        return Ok(None);
    }
    let Some(want) = suffix.skip_limit_want(graph, params)? else {
        return Ok(None);
    };
    if want == 0 {
        return Ok(None);
    }
    Ok(Some(RowLimitCap { want, produced: 0 }))
}

pub(crate) fn ordered_limit_cap(
    suffix: &[Clause],
    patterns: &[PathPattern],
    graph: &ExecGraph<'_>,
    params: &BTreeMap<String, Value>,
    input_rows: usize,
) -> Result<Option<RowLimitCap>> {
    if input_rows != 1 || graph.has_write_overlay() {
        return Ok(None);
    }
    let Some(clause) = suffix.first() else {
        return Ok(None);
    };
    let Some(s) = return_with_suffix(clause) else {
        return Ok(None);
    };
    let Some(rel_var) = order_rel_ts_desc_var(s.order, s.items) else {
        return Ok(None);
    };
    if !ordered_expand_pattern(patterns, rel_var) {
        return Ok(None);
    }
    row_limit_cap(&s, graph, params)
}

/// Stop MATCH once an un-ordered `RETURN/WITH … [SKIP s] LIMIT n` has
/// `SKIP+LIMIT` rows. LIMIT without ORDER BY keeps an unspecified prefix
/// — the engine's deterministic enumeration order, exactly the prefix the
/// ordinary projection's own truncation keeps — so halting the walk at
/// `SKIP+LIMIT` reproduces the same rows without enumerating the rest
/// (the QPP/GPM LIMIT pull-in: the walk's `Flow`-style halt, `gpm::emit`;
/// the classic walker's sink already halts on a full cap). A `WHERE` on
/// the MATCH itself does not decline (LABS-853, see [`match_limit_cap`]):
/// the walk's sink filters ahead of the count, so the counter moves only
/// on rows the eager build would keep and the halt still lands on the
/// eager prefix — the walk stops building rider rows the projection
/// would drop. This cap's own gate is the empty `ORDER BY` (ordered
/// truncation is `ordered_limit_cap`'s shape — the top rows of a sort
/// are not the walk's prefix); every other decline and the
/// want-to-counter tail are the shared [`row_limit_cap`].
fn plain_limit_cap(
    suffix: &[Clause],
    graph: &ExecGraph<'_>,
    params: &BTreeMap<String, Value>,
) -> Result<Option<RowLimitCap>> {
    let Some(clause) = suffix.first() else {
        return Ok(None);
    };
    let Some(s) = return_with_suffix(clause) else {
        return Ok(None);
    };
    if !s.order.is_empty() {
        return Ok(None);
    }
    row_limit_cap(&s, graph, params)
}

/// The one fact the clause filter's placement rides: `true` when the
/// walk's sink must run the WHERE — once per row, ahead of any cap's
/// count. The plain cap's count is exact only because this answers the
/// same way [`match_limit_cap`] arms (a `WHERE`-bearing MATCH arms the
/// plain cap and its sink filter together — the two ends of one policy,
/// stated here so they cannot drift). The contracted kernels are the
/// policy's third end, exact without a sink filter: their compiled
/// admissions are their WHERE (a conjunct outside the closed vocabulary
/// declines the kernel), so only kept rows land — see
/// [`MatchCap::plain_want`].
pub(crate) fn sink_filters_clause(
    projection: Option<&SinkProjection<'_>>,
    cap: Option<&MatchCap<'_>>,
    where_clause: Option<&Expr>,
) -> bool {
    projection.is_some() || (cap.is_some() && where_clause.is_some())
}

/// The one cap-arming entry the dispatcher reads, and the one place the
/// MATCH clause's own `WHERE` policy is stated (LABS-853): a
/// `WHERE`-bearing MATCH arms only the plain cap. The plain cap's count
/// stays exact because `match_clause` runs the clause filter in the
/// walk's sink whenever a cap rides a `WHERE` (a filter-only reduce), so
/// only kept rows are offered. The ordered cap's
/// walk-order-equals-sort-order contract (ADR-0035) and the distinct
/// cap's key count have not been verified over a filtered walk, so a
/// `WHERE` declines both.
pub(crate) fn match_limit_cap<'a>(
    suffix: &'a [Clause],
    patterns: &[PathPattern],
    where_clause: Option<&Expr>,
    graph: &ExecGraph<'_>,
    params: &'a BTreeMap<String, Value>,
    input_rows: usize,
) -> Result<Option<MatchCap<'a>>> {
    if where_clause.is_some() {
        return Ok(plain_limit_cap(suffix, graph, params)?.map(MatchCap::Plain));
    }
    if let Some(o) = ordered_limit_cap(suffix, patterns, graph, params, input_rows)? {
        return Ok(Some(MatchCap::Ordered(o)));
    }
    if let Some(p) = plain_limit_cap(suffix, graph, params)? {
        return Ok(Some(MatchCap::Plain(p)));
    }
    Ok(distinct_limit_cap(suffix, graph, params)?.map(MatchCap::Distinct))
}