relate-executor 1.0.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
480
481
482
483
484
485
486
//! 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 indexmap::map::Entry;
use indexmap::IndexMap;
use relate_core::{Result, Value};
use relate_ir::{Expr, Projection};
use relate_names::SmallName;

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

use super::super::order::{park_order_keys, OrderKeys};
use super::super::scope::{has_star_item, is_internal_column, is_star_item, visible_keys};
use super::super::unwind::RowFeed;
use super::eval::{agg_eval, AggScope};
use super::stream::{AggSites, GroupAccs};

/// The projection's per-group subclause state, bundled to keep
/// [`aggregate_ex`] under the argument-count lint.
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 WHERE predicate when it contains aggregate calls
    /// (ADR-0453): HAVING semantics — 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`]). `None` for
    /// every other shape.
    pub having: Option<&'a Expr>,
    /// 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 group's state: its first row — the seed every non-aggregate leaf
/// reads — and its aggregate accumulators. Never the group's rows.
struct GroupState<'e> {
    /// `None` only for the keyless group seeded before any row arrives (the
    /// ADR-0453 phantom group of an empty input).
    seed: Option<Row>,
    accs: GroupAccs<'e>,
}

/// 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>,
    cypher_version: Option<u32>,
    order: &'a OrderKeys<'a>,
    having: Option<&'a Expr>,
    import_scope: Option<&'a Row>,
    key_exprs: Vec<Cow<'a, Expr>>,
    star: bool,
    account: bool,
    sites: AggSites<'a>,
    groups: IndexMap<Vec<GroupKey>, GroupState<'a>>,
    groups_charge: relate_core::tokens::WorkspaceCharge,
    meter: crate::workspace::GroupMapMeter,
    retained: Option<crate::workspace::RetainedRows>,
    input_charge: relate_core::tokens::WorkspaceCharge,
    group_retained: crate::overlay::RetainedTotal,
}

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. `feed` supplies the first-row shape for the `*` key
    /// desugar and, when held, the input vector to bill.
    pub(crate) fn open(
        graph: &ExecGraph<'_>,
        feed: &RowFeed<'_>,
        items: &'a [Projection],
        group_by: Option<&'a [Expr]>,
        params: &'a BTreeMap<String, Value>,
        cypher_version: Option<u32>,
        subclauses: &GroupSubclauses<'a>,
    ) -> Result<Self> {
        let GroupSubclauses {
            order,
            having,
            import_scope,
        } = *subclauses;
        // 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(),
            },
            feed.probe(),
        );
        // 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();
        // ADR-0198 ruling 1: with aggregation in play, a `*` item groups by
        // all of the row's carried variables and projects them through to
        // the output row alongside the aggregate aliases.
        let star = has_star_item(items);
        // 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 sites = AggSites::discover(
            graph,
            items
                .iter()
                .filter(|p| !is_star_item(p) && is_agg_expr(&p.expr))
                .map(|p| &p.expr),
            order.exprs().chain(having),
            params,
            cypher_version,
        )?;
        let account = relate_core::tokens::workspace_accounting();
        // 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 retained = match feed {
            RowFeed::Held(held) if account => Some(crate::workspace::RetainedRows::measure(held)),
            _ => None,
        };
        let input_charge = match &retained {
            Some(retained) => {
                let charge = relate_core::tokens::WorkspaceCharge::retain(retained.bytes())?;
                relate_core::tokens::peak_hold("aggregation");
                charge
            }
            None => relate_core::tokens::WorkspaceCharge::empty(),
        };
        let mut this = Self {
            items,
            params,
            cypher_version,
            order,
            having,
            import_scope,
            key_exprs,
            star,
            account,
            sites,
            groups: IndexMap::new(),
            groups_charge: relate_core::tokens::WorkspaceCharge::empty(),
            meter: crate::workspace::GroupMapMeter::default(),
            retained,
            input_charge,
            // ADR-0463 (LABS-781): the group map is one retaining
            // container. Each landed group — its key, its seed row and its
            // accumulators — grows a 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 its own
            // container.)
            group_retained: graph.retain_bytes(0)?,
        };
        // A keyless aggregation has exactly one group whose key is the
        // empty set: seed it here, through the same charge-and-meter
        // 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.group_retained.add_group_entry(graph, 0, &[], 0)?;
            let accs = this.sites.open_group();
            this.group_retained.add_bytes(graph, accs.footprint())?;
            this.groups
                .insert(Vec::new(), GroupState { seed: None, accs });
            if account {
                this.meter.add_empty_group();
                charge_group_map(&mut this.groups_charge, &this.meter)?;
            }
        }
        Ok(this)
    }

    /// Fold one input row into its group.
    pub(crate) fn offer(&mut self, graph: &ExecGraph<'_>, row: Row) -> Result<()> {
        let Self {
            params,
            cypher_version,
            key_exprs,
            account,
            sites,
            groups,
            groups_charge,
            meter,
            retained,
            input_charge,
            group_retained,
            ..
        } = self;
        let (params, cypher_version, account) = (*params, *cypher_version, *account);
        graph.cancel_tick()?;
        // Star-free after the desugar in `open`: one `eval` per key, and the
        // capacity is exact.
        let mut key = Vec::with_capacity(key_exprs.len());
        for k in key_exprs.iter() {
            key.push(GroupKey::new(&eval(
                graph,
                &row,
                k,
                params,
                cypher_version,
            )?));
        }
        if let Some(retained) = retained.as_mut() {
            retained.release_row(&row);
            input_charge.resize(retained.bytes())?;
        }
        let map_slots = groups.len();
        match groups.entry(key) {
            Entry::Occupied(mut entry) => {
                let index = entry.index();
                let state = entry.get_mut();
                // Only the keyless group's first row lands as a seed; it is
                // charged while it still solely owns its payload, ahead of
                // the offer's clones. 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.
                let lands_as_seed = state.seed.is_none();
                if lands_as_seed {
                    if account {
                        meter.add_row(index, &row);
                    }
                    group_retained.add_row(graph, row.values())?;
                }
                state.accs.offer(graph, &row, params, cypher_version)?;
                if lands_as_seed {
                    state.seed = Some(row);
                } else {
                    drop(row);
                }
                state.accs.settle(graph, group_retained)?;
            }
            Entry::Vacant(entry) => {
                if account {
                    meter.add_group(entry.key(), &row);
                }
                // The map entry the new group lands in: the slot's buffer
                // growth plus the key's slots and owned payload — the
                // meter's own figures for the same landing. A `GROUP BY`
                // over a constructed key expression retains fresh key
                // bytes per group; the running total is what must see
                // them.
                group_retained.add_bytes(
                    graph,
                    crate::workspace::group_entry_insert_delta(map_slots, entry.key()),
                )?;
                // The seed row's landing charge. The retained key keeps its
                // `Arc` clone of the row's key payload alive, so the row's
                // own payload adds nothing here and the key charge above
                // counted it once — the exact figure the meter's
                // `add_group` computes for the same landing.
                group_retained.add_row(graph, row.values())?;
                let mut accs = sites.open_group();
                group_retained.add_bytes(graph, accs.footprint())?;
                accs.offer(graph, &row, params, cypher_version)?;
                // The row stays as the seed and holds its share of what the
                // sites kept; that payload was charged once with the seed.
                accs.settle(graph, group_retained)?;
                entry.insert(GroupState {
                    seed: Some(row),
                    accs,
                });
            }
        }
        if account {
            charge_group_map(groups_charge, meter)?;
        }
        Ok(())
    }

    /// 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,
            cypher_version,
            order,
            having,
            import_scope,
            star,
            groups,
            groups_charge,
            input_charge,
            ..
        } = self;
        drop(input_charge);
        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)?;
        for (_, state) in groups {
            // The group's own first row 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 GroupState { seed, accs } = state;
            let seed_row = seed.as_ref().or(import_scope);
            let mut n = seed_row.cloned().unwrap_or_default();
            let folded = accs.finish(graph, seed.as_ref(), params, cypher_version);
            if 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) in items.iter().enumerate() {
                if is_star_item(p) {
                    continue;
                }
                let name = p.output_name(i);
                let val = if is_agg_expr(&p.expr) {
                    agg_eval(
                        graph,
                        AggScope::of_group(&folded, seed_row),
                        &p.expr,
                        params,
                        cypher_version,
                    )?
                } else if let Some(row) = seed_row {
                    eval(graph, row, &p.expr, params, cypher_version)?
                } else {
                    Value::Null
                };
                n.insert(name, 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 evaluated orders as null rather than failing
            // the statement — the documented `ORDER BY` policy.
            park_order_keys(order, &mut n, |expr, scope| {
                agg_eval(
                    graph,
                    AggScope::of_row(&folded, scope),
                    expr,
                    params,
                    cypher_version,
                )
            });
            // 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_row(&folded, &n),
                    predicate,
                    params,
                    cypher_version,
                )?;
                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(groups_charge);
        Ok(out)
    }
}

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

/// Charge the group map's growth after an entry insert, then sample the
/// peak hold. The one spelling of that pair: the row loop and the keyless
/// empty-group seed both bill the map through it, and both derive the
/// charge from `meter.bytes` (already updated by `add_row`/`add_group`).
fn charge_group_map(
    charge: &mut relate_core::tokens::WorkspaceCharge,
    meter: &crate::workspace::GroupMapMeter,
) -> Result<()> {
    charge.resize(meter.bytes)?;
    relate_core::tokens::peak_hold("aggregation");
    Ok(())
}

/// 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
}