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
//! A `RETURN`/`WITH` projection's classification — the decided shape and
//! the per-clause plan every projection path consults. Split out of
//! `project.rs` so the merge paths, the post-merge tail and the
//! dispatcher's fold slots all read it *downward*: the classification is
//! read by `project` and by `post_merge` — which owns the landing
//! container and the tail — and reads neither of them back.

use std::collections::BTreeMap;

use relate_core::{Error, Result, Value};

use crate::eval::is_agg_expr;
use crate::overlay::ExecGraph;
use crate::row::Row;
use relate_ir::{Expr, Projection};

use super::aggregate::GroupSubclauses;
use super::order::{resolve_page_bounds, window_capacity, OrderKeys, PageBounds};
use super::scope::{has_star_item, is_star_item, projection_names, ProjectionFields};

/// Which merge path owns a `RETURN`/`WITH` projection — the three
/// exclusive shapes, decided once at classification so no consumer
/// re-derives the combination from independent flags:
/// [`ProjectionShape::Plain`] (each row projects alone),
/// [`ProjectionShape::Star`] (a `*` item, nothing aggregating: the
/// visible-bindings merge) and [`ProjectionShape::Aggregate`] (aggregate
/// items, an explicit GROUP BY, or a DISTINCT with an aggregating
/// subclause).
#[derive(Clone, Copy)]
pub(crate) enum ProjectionShape {
    Plain,
    Star,
    Aggregate {
        /// A `*` item among the items of the aggregating shape: the group
        /// keys are every carried variable (ADR-0198 ruling 1), so a feed
        /// cannot fold into it as rows arrive
        /// ([`ProjectionShape::streams`]).
        star: bool,
    },
}

impl ProjectionShape {
    /// Decide the shape from the projection's fields — the one decision
    /// point every consumer reads. The one rejection the executor owns
    /// lives here: a `DISTINCT *` whose subclause aggregates. Validation
    /// admits the shape (DISTINCT makes the clause aggregating regardless
    /// of `*`), but the executor cannot give it HAVING semantics: the
    /// aggregate would evaluate per row through scalar `eval`, whose
    /// error is swallowed (the WHERE form returned zero rows, the ORDER
    /// BY form sorted on null keys), so the combination fails closed.
    pub(crate) fn decide(fields: &ProjectionFields<'_>) -> Result<Self> {
        let aggregating_subclause = fields.subclause_aggregates();
        let star = has_star_item(fields.items);
        let has_agg = fields.items_aggregate()
            || fields.group_by.is_some()
            || (fields.distinct && aggregating_subclause);
        if fields.distinct && star && aggregating_subclause {
            return Err(Error::syntax(
                "InvalidAggregation: a DISTINCT * projection cannot aggregate in its ORDER BY or WHERE subclause",
            ));
        }
        Ok(if has_agg {
            ProjectionShape::Aggregate { star }
        } else if star {
            ProjectionShape::Star
        } else {
            ProjectionShape::Plain
        })
    }

    /// The two facts a shape carries, read from it once: every predicate
    /// below is a reading of these, so no one of them re-derives a
    /// combination another already holds.
    fn facts(self) -> ShapeFacts {
        match self {
            ProjectionShape::Plain => ShapeFacts {
                aggregating: false,
                star: false,
            },
            ProjectionShape::Star => ShapeFacts {
                aggregating: false,
                star: true,
            },
            ProjectionShape::Aggregate { star } => ShapeFacts {
                aggregating: true,
                star,
            },
        }
    }

    /// Whether a feed into this projection folds as rows arrive: an
    /// aggregating shape with no `*` item (a `*` groups by every carried
    /// variable, which needs the rows' shape up front). One decision for
    /// the dispatcher, which chooses the feed, and the projection, which
    /// consumes it.
    pub(crate) fn streams(&self) -> bool {
        let facts = self.facts();
        facts.aggregating && !facts.star
    }

    /// Whether the shape folds an aggregation — the fact the ordered
    /// window's decision reads ([`ProjectionClass::window_keep`]): an
    /// aggregating shape's group table is its own declared breaker, so it
    /// takes no top-k window.
    pub(crate) fn aggregating(&self) -> bool {
        self.facts().aggregating
    }

    /// Whether the shape passes its feed through row by row: a
    /// non-aggregating shape with no `ORDER BY`. Each item is evaluated per
    /// row, a post-projection `WHERE` filters per row, and `SKIP`/`LIMIT`
    /// and `DISTINCT` apply on the stream, so the producer is never
    /// materialized ([`ProjectionClass::streams_pass`] is the clause's
    /// reading of the same fact).
    pub(crate) fn passes_stream(&self, fields: &ProjectionFields<'_>) -> bool {
        !self.aggregating() && fields.order.is_empty()
    }

    /// Whether the projection carries a `*` item — the shape a `*` owns
    /// outright, or the aggregating form that groups by every carried
    /// variable.
    pub(crate) fn star(&self) -> bool {
        self.facts().star
    }
    /// Whether the shape is the plain one: no aggregation and no `*` item,
    /// so each row projects alone and the clause keys its rows by the
    /// declared output columns.
    pub(crate) fn plain(&self) -> bool {
        let facts = self.facts();
        !facts.aggregating && !facts.star
    }
}

/// A shape's two facts — whether it aggregates, and whether it carries a
/// `*` item. The one reading of the shape's vocabulary: the enum's three
/// variants spell them out, and the predicates above never match on the
/// variants themselves.
#[derive(Clone, Copy)]
struct ShapeFacts {
    aggregating: bool,
    star: bool,
}

/// The HAVING plan: the aggregating WHERE's predicate and the hidden
/// column its per-group value parks under — one fact, one presence. The
/// emission parks the value per group; the finish tail's page filter
/// reads it back under the same column. The aggregating counterpart of
/// [`OrderKeys`]: owned by the projection's classification, read
/// through its accessors, never re-derived.
#[derive(Clone)]
pub(crate) struct HavingPlan<'a> {
    predicate: &'a Expr,
    parked: String,
}

impl<'a> HavingPlan<'a> {
    /// Plan construction — the classification's one derivation site.
    pub(crate) fn new(predicate: &'a Expr, parked: String) -> Self {
        Self { predicate, parked }
    }

    /// The predicate, still evaluated once per group at emission
    /// (ADR-0453 §2: aggregate calls fold the group, non-aggregate
    /// leaves read the projected row).
    pub(crate) fn predicate(&self) -> &'a Expr {
        self.predicate
    }

    /// The hidden column the per-group value parks under — uncollided
    /// against the projection's own names the way the sort keys' prefix
    /// is, so the result extraction never shows it.
    pub(crate) fn parked(&self) -> &str {
        &self.parked
    }
}

/// ADR-0453's one WHERE split, as one value with one presence: the
/// aggregating arm carries its plan, the plain arm the predicate the
/// tail evaluates on the paged row. `None` is no WHERE at all — three
/// states, one field, no combination to keep in sync.
#[derive(Clone)]
pub(crate) enum WhereMode<'a> {
    Having(HavingPlan<'a>),
    Plain(&'a Expr),
}

impl<'a> WhereMode<'a> {
    /// The predicate, whichever arm — the seed keeps what it reads in
    /// both.
    pub(crate) fn predicate(&self) -> &'a Expr {
        match self {
            Self::Having(plan) => plan.predicate(),
            Self::Plain(predicate) => predicate,
        }
    }

    /// The HAVING plan, `None` for the plain arm.
    pub(crate) fn having(&self) -> Option<&HavingPlan<'a>> {
        match self {
            Self::Having(plan) => Some(plan),
            Self::Plain(_) => None,
        }
    }

    /// The plain predicate, `None` for the HAVING arm.
    pub(crate) fn plain(&self) -> Option<&'a Expr> {
        match self {
            Self::Plain(predicate) => Some(predicate),
            Self::Having(_) => None,
        }
    }
}

/// A `RETURN`/`WITH` projection's row-independent classification — the
/// plan every projection path consults, decided once per clause: the
/// decided [`ProjectionShape`], the clause's fields (the one field
/// bundle every consumer reads), ADR-0453's WHERE split, the eager-page
/// decision, and the parked-key plan and output names built from the
/// same fields. `project_rows` and `open_fold` read this one plan, so
/// the streaming decision, the fold and the projection path cannot
/// disagree on the clause's shape — and no site re-destructures the
/// fields or hand-builds a `ProjectionFields` literal. The shape alone
/// ([`ProjectionShape::decide`]) answers the dispatcher's cheap
/// questions without building the per-key plan.
pub(crate) struct ProjectionClass<'a> {
    /// The clause's fields — the one field bundle (a copy of the
    /// [`ProjectionFields`] every consumer reads).
    pub(crate) fields: ProjectionFields<'a>,
    /// The decided shape: which merge path owns the clause.
    pub(crate) shape: ProjectionShape,
    /// ADR-0453's one WHERE split, as the one value it is: the HAVING
    /// plan (the aggregating arm — the aggregation answers the
    /// predicate per group, parks the value under the plan's hidden
    /// column, and the finish tail filters the paged rows on it), the
    /// plain predicate the tail evaluates on the paged row, or no WHERE
    /// at all.
    pub(crate) where_mode: Option<WhereMode<'a>>,
    /// The eager pre-projection page ([`pages_eagerly`]) on a
    /// non-aggregating shape: `SKIP`/`LIMIT` with no `ORDER BY`,
    /// DISTINCT, or filter.
    pub(crate) eager_paged: bool,
    /// The projection's parked-key plan — the one per-key scope decision
    /// every path below parks its keys with.
    pub(crate) order_keys: OrderKeys<'a>,
    /// The non-`*` items' output names, in item order — the clause's own
    /// schema only for a non-`*` shape. A `*` shape's result columns are
    /// the incoming visible bindings plus these, resolved by the finish
    /// tail; a reader of this field on a star row must read
    /// [`Self::shape`] first.
    pub(crate) out_names: Vec<String>,
    /// The explicit items with the output name each resolves to — the
    /// clause's `*` split and its names, resolved once here rather than
    /// re-asked or re-rendered per row: the row-assembly closures iterate
    /// this and never test [`is_star_item`] or call
    /// [`Projection::output_name`] themselves.
    pub(crate) value_items: Vec<(&'a Projection, String)>,
}

/// Whether a projection takes the eager pre-projection page: `SKIP`/`LIMIT`
/// with no `ORDER BY`, `DISTINCT`, or filter. It states the paging half of
/// the rule only — the aggregating exclusion belongs to the caller that
/// decided the shape ([`ProjectionClass::new`] applies it; the match sink's
/// decline chain has already rejected every aggregating form by the time it
/// asks).
pub(crate) fn pages_eagerly(fields: &ProjectionFields<'_>) -> bool {
    !fields.distinct
        && fields.order.is_empty()
        && fields.where_clause.is_none()
        && (fields.skip.is_some() || fields.limit.is_some())
}

/// The sort's bounds-free precondition: there is an ordering to cut, a
/// `LIMIT` to bound it, and no `DISTINCT` pass (which runs *after* the
/// sort, so a pre-distinct cut would answer fewer rows than the distinct
/// page deserves). The one home of these three facts —
/// [`ProjectionClass::sort_cut`] reads them for the cutoff.
pub(crate) fn may_cut(fields: &ProjectionFields<'_>) -> bool {
    !fields.distinct && !fields.order.is_empty() && fields.limit.is_some()
}

/// Whether a projection's `DISTINCT` is the first-seen pass over its input
/// stream (ADR-0660): the pass keeps only its charged seen-set, so it
/// neither collects the input nor sorts it. A shape that aggregates, or an
/// ordering (which runs on the distinct output), keeps the post-merge pass.
/// The one home of the decision: the projection's own consumption
/// ([`ProjectionClass::streams_distinct`]) reads it.
pub(crate) fn streams_distinct(fields: &ProjectionFields<'_>, shape: ProjectionShape) -> bool {
    fields.distinct && !shape.aggregating() && fields.order.is_empty()
}

impl<'a> ProjectionClass<'a> {
    /// The plan over an already-decided shape — the one construction
    /// site of the parked-key plan and the output names.
    pub(crate) fn new(
        graph: &ExecGraph<'_>,
        fields: &ProjectionFields<'a>,
        shape: ProjectionShape,
    ) -> Self {
        let aggregating = shape.aggregating();
        // ADR-0453's one WHERE split and the HAVING form's parked column
        // are one decision, taken in one match: an aggregating path whose
        // WHERE carries aggregate calls takes the HAVING plan (the
        // predicate, and the hidden column — uncollided against the
        // projection's own names the way the sort keys' prefix is, so the
        // result extraction never shows it — its per-group value parks
        // under); every other WHERE form stays the plain post-merge
        // filter. At most one is present.
        let where_mode = match fields.where_clause {
            Some(w) if aggregating && is_agg_expr(w) => Some(WhereMode::Having(HavingPlan::new(
                w,
                graph.uncolliding_prefix(
                    "__having",
                    projection_names(fields.items).iter().map(String::as_str),
                ),
            ))),
            Some(w) => Some(WhereMode::Plain(w)),
            None => None,
        };
        let value_items: Vec<(&'a Projection, String)> = fields
            .items
            .iter()
            .enumerate()
            .filter(|(_, item)| !is_star_item(item))
            .map(|(index, item)| (item, item.output_name(index)))
            .collect();
        Self {
            fields: *fields,
            shape,
            where_mode,
            eager_paged: !aggregating && pages_eagerly(fields),
            order_keys: OrderKeys::new(
                graph,
                fields.order,
                fields.items,
                fields.group_by.is_some(),
            ),
            out_names: value_items.iter().map(|(_, name)| name.clone()).collect(),
            value_items,
        }
    }

    /// The projection's per-group subclause bundle over this plan: the
    /// parked-key plan, the WHERE mode (HAVING or the plain filter), and
    /// the import scope — the one derivation both `open_fold` and the
    /// aggregation merge path hand
    /// [`super::aggregate::Aggregation::open`].
    pub(crate) fn group_subclauses<'b>(&'b self, scope: Option<&'b Row>) -> GroupSubclauses<'b> {
        GroupSubclauses {
            order: &self.order_keys,
            where_mode: self.where_mode.as_ref(),
            import_scope: scope,
        }
    }

    /// The clause's resolved paging bounds — resolved **once** per clause,
    /// and the real bounds whatever the shape: a nondeterministic `LIMIT`
    /// bound would otherwise size the ordered window differently from the
    /// page it feeds, and a fabricated empty bound here would be a second
    /// answer to a question the feed already asked. Who *applies* them is
    /// the shape's own fact ([`Self::eager_paged`]): the feed pages on the
    /// eager shape, the finish tail on every other.
    pub(crate) fn page_bounds(
        &self,
        graph: &ExecGraph<'_>,
        params: &BTreeMap<String, Value>,
    ) -> Result<PageBounds> {
        resolve_page_bounds(graph, params, self.fields.skip, self.fields.limit)
    }

    /// The sort's cutoff — `skip + limit`, the most rows an ordered page can
    /// need ([`window_capacity`]) — or `None` when the sort must keep every
    /// row: [`may_cut`]'s precondition fails (no order keys, no `LIMIT`, a
    /// `DISTINCT` pass) or the sum overflows. The one derivation of the
    /// cutoff; [`Self::window_keep`] masks it for the shape that takes a
    /// streaming window, and the finish tail's sort reads this value rather
    /// than recomputing it.
    pub(crate) fn sort_cut(&self, bounds: PageBounds) -> Option<std::num::NonZeroUsize> {
        if !may_cut(&self.fields) {
            return None;
        }
        window_capacity(bounds.skip, bounds.limit).and_then(std::num::NonZeroUsize::new)
    }

    /// Whether a *generated* feed reaching this projection stays generated,
    /// for exactly three reasons: the aggregation folds it into the group
    /// table, the ordered window folds it into the bounded heap, and the
    /// pass-through shape consumes it row by row ([`Self::streams_pass`],
    /// whose `DISTINCT` first-seen pass — a non-aggregating, unordered
    /// shape — keeps each row only long enough to decide it). Every row is
    /// evaluated as it arrives, so folding the generator changes no answer.
    /// This is the projection's own decision over the bounds it resolved
    /// once: a parked feed that resolves to no window is collected, not
    /// folded. [`prepare_input`][super::project::prepare_input] is its one
    /// reader.
    pub(crate) fn folds_feed(&self, bounds: PageBounds) -> bool {
        self.shape.streams() || self.streams_pass() || self.window_keep(bounds).is_some()
    }

    /// Whether the clause passes its input through as a stream: the shape's
    /// own fact ([`ProjectionShape::passes_stream`]) over this clause's
    /// fields. The landing funnel's page container
    /// ([`land_feed`][super::post_merge::land_feed]) applies the page and
    /// `DISTINCT` on the stream.
    pub(crate) fn streams_pass(&self) -> bool {
        self.shape.passes_stream(&self.fields)
    }

    /// Whether this clause's `DISTINCT` is the first-seen pass on its input
    /// stream rather than the post-merge pass — the class's reading of
    /// [`streams_distinct`].
    pub(crate) fn streams_distinct(&self) -> bool {
        streams_distinct(&self.fields, self.shape)
    }

    /// Whether the clause takes the pass-through page container — the one
    /// place that rule is spelled. The fold funnel's superset
    /// ([`land_feed`][super::post_merge::land_feed]): a named-`DISTINCT`
    /// shape, a plain `WHERE`, or any page. The `MATCH` sink admits the
    /// narrower named-`DISTINCT` form alone (its rows arrive unprojected,
    /// so a plain page is the projection path's).
    pub(crate) fn takes_page_container(&self, bounds: PageBounds) -> bool {
        self.streams_pass()
            && (self.streams_distinct() || self.plain_where().is_some() || bounds.is_paged())
    }

    /// ADR-0453's plain `WHERE` on this clause — the trailing predicate the
    /// page container applies to its answered page, or the finish tail to
    /// the page it cut. The one home of the plain/HAVING split: every
    /// consumer of the fact reads it here, never re-deriving it from
    /// `where_mode`.
    pub(crate) fn plain_where(&self) -> Option<&'a Expr> {
        self.where_mode.as_ref().and_then(WhereMode::plain)
    }

    /// The bounded window this clause folds its producer into — its sort
    /// cutoff, masked for the aggregating shape: a group table is its own
    /// declared breaker and is already bounded by the group map, so the
    /// clause keeps its full group sort. The one window decision, beside
    /// the classification it reads: the landing funnels ([`super::post_merge::land_feed`],
    /// [`super::post_merge::adopt_rows`]) read it and every
    /// merge path reaches the container through that.
    /// An empty page is no window, and the capacity is a
    /// [`std::num::NonZeroUsize`] — so every window that is built holds at
    /// least one entry and no path carries a zero-capacity mode. The page's
    /// own emptiness is [`PageBounds::answers_nothing`], the same predicate
    /// the merge decides its empty result from; the invariant the capacity
    /// then carries is the type's, not a promise its callers keep.
    pub(crate) fn window_keep(&self, bounds: PageBounds) -> Option<std::num::NonZeroUsize> {
        if self.shape.aggregating() || bounds.answers_nothing() {
            return None;
        }
        self.sort_cut(bounds)
    }
}