relate-executor 1.2.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
//! The group path: [`aggregate_ex`] builds the groups (explicit `GROUP BY`
//! keys or the implicit non-aggregating projection items), folds each one,
//! and parks its `ORDER BY` keys and HAVING predicate — plus the `*`
//! key desugaring both paths share.

use std::borrow::Cow;
use std::collections::BTreeMap;

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

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

use super::super::order::{park_order_keys, OrderKeys};
use super::super::scope::{is_internal_column, visible_keys};
use super::super::RowFeed;
use super::bill::{GroupBill, InputBill};
use super::eval::{agg_eval, AggScope};
use super::key::KeyTuple;
use super::seed::{base_role, ItemRole, SeedStore, Taken};
use super::stream::discover;
use super::table::{GroupTable, Landing};

/// ADR-0453's one WHERE split, one mode: an aggregating path whose WHERE
/// carries aggregate calls takes it as HAVING — evaluated once per group
/// through [`agg_eval`] (aggregate calls fold the group, non-aggregate
/// leaves read the projected row), a group whose predicate is not true is
/// dropped; false and null drop, a non-boolean value is the typed
/// rejection live raises (22N01) rather than a silent drop, and a
/// predicate evaluation error aborts the statement — the same
/// convert-or-raise policy the ordinary WHERE positions read
/// (ADR-0458 §7, [`crate::eval::predicate_value_holds`]). Every other
/// WHERE form — any WHERE on a non-aggregating path included — stays the
/// plain post-merge filter: the projection applies it to the emitted
/// rows, not the group, and the aggregation retains only what it reads
/// in the seed, so an emitted row still carries the grouping keys the
/// predicate names (`GROUP BY a.p WHERE a.p > 0`).
#[derive(Clone, Copy)]
pub(crate) enum WhereMode<'a> {
    Having(&'a Expr),
    Plain(&'a Expr),
}

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

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

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

/// The projection's per-group subclause state, bundled to keep
/// [`aggregate_ex`] under the argument-count lint.
#[derive(Clone, Copy)]
pub(crate) struct GroupSubclauses<'a> {
    /// The projection's `ORDER BY` key list with its per-key alias-scope
    /// decision. The group is the only place a key's aggregate calls can be
    /// answered, so each output row parks them through [`park_order_keys`]
    /// for the sort to read back.
    pub order: &'a OrderKeys<'a>,
    /// The projection's one WHERE predicate, in the mode ADR-0453 decides
    /// for it — see [`WhereMode`].
    pub where_mode: Option<WhereMode<'a>>,
    /// ADR-0453: an importing subquery's scope row — the seed the phantom
    /// keyless group over an emptied input uses, so a non-aggregate leaf
    /// naming an import (`sum(y) + tax` with no rows left) reads the
    /// import's value instead of null. `None` at every other scope.
    pub import_scope: Option<&'a Row>,
}

/// One aggregating projection, fed a row at a time: [`Self::open`] does the
/// plan setup, [`Self::offer`] folds one row into its group, and
/// [`Self::finish`] emits the group rows (`ORDER BY` keys parked, HAVING
/// applied). The graph is passed per call, as the accumulators take it.
pub(crate) struct Aggregation<'a> {
    items: &'a [Projection],
    params: &'a BTreeMap<String, Value>,
    /// The projection's `ORDER BY` keys, which the finish parks per group.
    order: &'a OrderKeys<'a>,
    /// The HAVING predicate the finish applies per group (the plain WHERE
    /// is read at planning only, so it is not kept).
    having: Option<&'a Expr>,
    /// The importing subquery's scope row the phantom group's seed stands
    /// in with.
    import_scope: Option<&'a Row>,
    key_exprs: Vec<Cow<'a, Expr>>,
    /// How each projection item is answered at the finish (a kept key
    /// value, the folded sites, or the seed row) — decided once at plan
    /// time.
    roles: Vec<ItemRole>,
    /// The groups: key index, seeds (the first-row columns the finish
    /// reads), and accumulator columns — see [`GroupTable`] for the
    /// per-group layout and what each group costs.
    groups: GroupTable<'a>,
    /// ADR-0463 (LABS-781): the group table is one retaining container.
    /// Each landed group grows one running total enforced at the per-value
    /// cap, so a high-cardinality key set refuses at ~the cap while the
    /// groups land. A row that joins an existing group adds nothing: the
    /// group keeps constant-size accumulators, never its rows. (A
    /// retaining aggregate — `collect`, a percentile, a DISTINCT call —
    /// charges its operands to the same total as they settle.)
    bill: GroupBill,
    /// The input vector's bill, released row by row as payloads move into
    /// the group table; inert when workspace accounting is off.
    input: InputBill,
}

impl<'a> Aggregation<'a> {
    /// Plan setup. `group_by` (CIP-236) carries explicit grouping keys when
    /// a GROUP BY clause is present; that is the only difference from the
    /// implicit form, whose keys come from the non-aggregating projection
    /// items. `Some(&[])` is the empty grouping set (`GROUP BY ()`): one
    /// global group. `rows` supplies the first-row shape for the `*` key
    /// desugar and the held input vector to bill (`aggregate_ex` passes the
    /// feed's probe; a streamed fold passes none).
    pub(crate) fn open(
        graph: &ExecGraph<'_>,
        rows: &[Row],
        items: &'a [Projection],
        group_by: Option<&'a [Expr]>,
        params: &'a BTreeMap<String, Value>,
        subclauses: &GroupSubclauses<'a>,
    ) -> Result<Self> {
        let GroupSubclauses {
            order,
            where_mode,
            import_scope,
        } = *subclauses;
        // The HAVING predicate the sites discover and the finish reads;
        // the plain form carries no aggregate call, so it discovers none.
        let having = where_mode.and_then(WhereMode::having);
        // The grouping strategy is loop-invariant: decide once whether the
        // key comes from the explicit `group_by` expressions or from the
        // inferred non-aggregating projection items.
        let key_exprs = desugar_star_keys(
            match group_by {
                Some(keys) => keys.iter().map(Cow::Borrowed).collect(),
                None => items
                    .iter()
                    .filter(|p| !is_agg_expr(&p.expr))
                    .map(|p| Cow::Borrowed(&p.expr))
                    .collect(),
            },
            rows,
        );
        // One row for an empty input only when there are no grouping keys
        // at all: implicit grouping over an all-aggregate projection, an
        // explicit empty grouping set (`GROUP BY ()`), or an explicit empty
        // `ALL` (which lowers to no keys). An explicit non-empty key set
        // has no group to emit for an empty input, so it yields zero rows --
        // emitting the phantom global group there would invent a row with
        // no key values. A `*` item counts as a key: it carries every
        // in-scope variable (ADR-0198).
        let keyless = key_exprs.is_empty();
        // The aggregate calls the projection evaluates — items, ORDER BY
        // keys, HAVING — found once, so every group streams its input
        // through constant-size accumulators instead of retaining its rows.
        let accs = discover(
            graph,
            items
                .iter()
                .filter(|item| base_role(item) == Some(ItemRole::Aggregate))
                .map(|item| &item.expr),
            order.exprs().chain(having),
            params,
        )?;
        // The input vector stays allocated until the fold finishes. Bill it
        // up front, then hand each row's payload to the group map as it
        // moves. A generated feed holds no input vector: nothing to bill,
        // nothing to release row by row.
        let input = InputBill::open(rows)?;
        // What a group keeps of its first row (see `seed`). The plain
        // WHERE is evaluated after emission, against the emitted row — the
        // seed keeps what it reads so the row still carries the grouping
        // keys it names.
        let (seeds, roles) = SeedStore::planned(
            items,
            &key_exprs,
            order
                .exprs()
                .chain(where_mode.map(WhereMode::predicate))
                .chain(accs.seed_reads()),
        );
        let mut this = Self {
            items,
            params,
            order,
            having,
            import_scope,
            key_exprs,
            roles,
            groups: GroupTable::new(seeds, accs),
            bill: GroupBill::open(graph)?,
            input,
        };
        // A keyless aggregation has exactly one group whose key is the
        // empty set: seed it here, through the same charge sequence a row
        // landing pays, and every input row lands in it — the empty input
        // keeps the seed as its phantom group (the ADR-0453 import scope's
        // stand-in) and no post-loop insertion is needed.
        if keyless {
            this.groups.open_keyless(graph, &mut this.bill)?;
        }
        Ok(this)
    }

    /// The bytes the group table has billed to the running total so far
    /// (the charging seam the memory-shape tests read).
    #[cfg(test)]
    pub(crate) fn billed_bytes(&self) -> u64 {
        self.bill.total()
    }

    /// Fold one input row into its group.
    pub(crate) fn offer(&mut self, graph: &ExecGraph<'_>, row: Row) -> Result<()> {
        let Self {
            params,
            key_exprs,
            groups,
            bill,
            input,
            ..
        } = self;
        let params = *params;
        graph.cancel_tick()?;
        // Star-free after the desugar in `open`: one `eval` per key.
        let (key, key_values) = KeyTuple::of(key_exprs.len(), |i| {
            eval(graph, &row, &key_exprs[i], params)
        })?;
        input.release(&row)?;
        // The landing bills what the row adds, ahead of the landing (see
        // [`GroupTable::land`]); the row's own payload is then spent into
        // the accumulators and, for the seed columns, the seed store.
        let landing = groups.land(graph, bill, key, &key_values, &row)?;
        let (group, as_seed) = match landing {
            Landing::Existing { group, as_seed } => (group, as_seed),
            Landing::New { group } => (group, true),
        };
        groups.accs.offer(group, graph, &row, params, bill)?;
        // The seed lands once per group: a new group's first row, or the
        // keyless group's. Every other row is spent once the accumulators
        // have it — dropped before the settle, so what a site kept from it
        // is solely the group's and is charged in full.
        if as_seed {
            groups.seeds.land(key_values, row)?;
        } else {
            drop(row);
        }
        groups.accs.settle(group, graph, bill)
    }

    /// Emit the group rows: projected columns, parked `ORDER BY` keys, and
    /// HAVING applied.
    pub(crate) fn finish(self, graph: &ExecGraph<'_>) -> Result<Vec<Row>> {
        let Self {
            items,
            params,
            order,
            having,
            import_scope,
            roles,
            groups,
            bill,
            input,
            ..
        } = self;
        drop(input);
        let mut out = Vec::new();
        // ADR-0463 (LABS-781): the aggregating projection's output
        // container — each emitted group row charges as it lands, ahead of
        // the landing, in its final shape (seed bindings, projected
        // values, aggregate results, parked order keys).
        let mut out_retained = graph.retain_bytes(0)?;
        let GroupTable {
            index,
            mut seeds,
            mut accs,
            ..
        } = groups;
        // Emission order is landing order; the keys are not read again.
        let group_count = index.len();
        drop(index);
        // ADR-0198 ruling 1: a `*` item projects the row's carried
        // variables through alongside the aggregate aliases.
        let has_star = roles.iter().any(|role| matches!(role, ItemRole::Star));
        for group in 0..group_count {
            // The group's own first row — narrowed to the columns this
            // finish reads — is the seed every non-aggregate leaf reads;
            // the phantom keyless group over an emptied input has none, so
            // an importing subquery's scope row stands in (ADR-0453) — the
            // imports are per-row constants, present even when the body's
            // rows are gone.
            let Taken {
                row: seed,
                keys: key_values,
            } = seeds.take(group);
            let seed_row = seed.as_ref().or(import_scope);
            let mut n = seed_row.cloned().unwrap_or_default();
            let folded = accs.finish_group(group, graph, seed.as_ref(), params);
            if has_star {
                // The variables `*` carries are already in the first-row
                // seed; keep the internal `__*` names out of the projected
                // row, the same filter the non-aggregating `*` path applies.
                n.retain(|name, _| !is_internal_column(name));
            }
            for (i, (p, role)) in items.iter().zip(&roles).enumerate() {
                let val = match *role {
                    ItemRole::Star => continue,
                    ItemRole::Aggregate => agg_eval(
                        graph,
                        AggScope::of_group(&folded, seed_row),
                        &p.expr,
                        params,
                    )?,
                    ItemRole::Key(slot) => key_values[slot].clone(),
                    ItemRole::Leaf => match seed_row {
                        Some(row) => eval(graph, row, &p.expr, params)?,
                        None => Value::Null,
                    },
                };
                n.insert(p.output_name(i), val);
            }
            // Park the `ORDER BY` keys beside the projected columns, through
            // the same decision and writer every other projection path uses.
            // A key reads `n`, the projected row, so an aggregate call in it
            // reads this group's resolved value and the deprecated
            // implicit-grouping shapes still answer from their item; a key
            // that cannot be resolved orders as null, while an ill-typed
            // read of a present value fails — the documented `ORDER BY`
            // policy.
            park_order_keys(order, &mut n, |expr, scope| {
                agg_eval(
                    graph,
                    AggScope::of_group(&folded, Some(scope)),
                    expr,
                    params,
                )
            })?;
            // HAVING (ADR-0453): the aggregating WHERE, answered per group
            // with the same scope an ORDER BY key gets. Not-true drops the
            // group — the retain's verdict for a failing row; a non-boolean
            // is the typed rejection live raises (22N01) and a list/path
            // converts, both through the one predicate policy (ADR-0458 §7).
            if let Some(predicate) = having {
                let keeps = agg_eval(
                    graph,
                    AggScope::of_group(&folded, Some(&n)),
                    predicate,
                    params,
                )?;
                if !crate::eval::predicate_value_holds(&keeps)? {
                    continue;
                }
            }
            // The resolved aggregate values are shared with `n` until the
            // folded group lets go; drop it so the output row's landing
            // charge sees the payloads it solely owns.
            drop(folded);
            out_retained.add_row(graph, n.values())?;
            out.push(n);
        }
        drop(bill);
        Ok(out)
    }
}

/// The loop over [`Aggregation`]: open, offer every row of the feed, finish.
pub(crate) fn aggregate_ex<'a>(
    graph: &ExecGraph<'_>,
    feed: RowFeed<'_>,
    items: &'a [Projection],
    group_by: Option<&'a [Expr]>,
    params: &'a BTreeMap<String, Value>,
    subclauses: &GroupSubclauses<'a>,
) -> Result<Vec<Row>> {
    let mut aggregation =
        Aggregation::open(graph, feed.probe(), items, group_by, params, subclauses)?;
    feed.try_for_each(|row| aggregation.offer(graph, row))?;
    aggregation.finish(graph)
}

/// ADR-0198 ruling 1: desugar a `*` key ONCE, before the row loop, into
/// one `Expr::Variable` key per visible column of the FIRST row, sorted
/// by name so the key does not depend on column insertion order (the
/// same expansion the non-aggregating projection path does). Pipeline
/// rows carry a uniform scope per clause, so every row in one
/// aggregation input has the same visible column set -- the positional
/// key vector already relies on that homogeneity. Expanding here keeps
/// the hot loop on the plain `eval` path: no per-key Star arm, no
/// per-row collect+sort. Empty input has no first row to expand from;
/// the unexpanded `*` then only keeps `keyless` false (a star key means
/// keys exist), and the row loop never runs.
pub(crate) fn desugar_star_keys<'a>(
    key_exprs: Vec<Cow<'a, Expr>>,
    rows: &[Row],
) -> Vec<Cow<'a, Expr>> {
    let Some(first) = rows.first() else {
        return key_exprs;
    };
    if !key_exprs.iter().any(|k| matches!(&**k, Expr::Star)) {
        return key_exprs;
    }
    let mut carried = visible_keys(first);
    carried.sort();
    // Row keys (String) enter the IR's name domain at construction.
    let star_keys: Vec<Expr> = carried
        .into_iter()
        .map(|name| Expr::Variable(SmallName::from_name(&name)))
        .collect();
    let mut expanded = Vec::with_capacity(key_exprs.len() + star_keys.len());
    for k in key_exprs {
        if matches!(&*k, Expr::Star) {
            expanded.extend(star_keys.iter().cloned().map(Cow::Owned));
        } else {
            expanded.push(k);
        }
    }
    expanded
}