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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
//! Projection (`RETURN`/`WITH`): expressions, ORDER BY, DISTINCT,
//! SKIP/LIMIT with top-k selection.

use std::cell::OnceCell;
use std::collections::{BTreeMap, HashSet};

use relate_core::{Error, Result, Value};
use relate_ir::{expr_row_vars, Clause, Projection};

use crate::ctx::ExecCtx;
use crate::eval::{eval, expr_vars, is_agg_expr};
use crate::overlay::{ExecGraph, MaterializeCache};
use crate::row::Row;

use super::aggregate::{aggregate_ex, Aggregation};
use super::classification::{ProjectionClass, ProjectionShape};
use super::feed::RowStream;
use super::order::{page, PageBounds};
use super::pattern_policies::MatchFold;
use super::post_merge::{adopt_rows, finish_projection, land_feed, Landed, LandedTail};
use super::scope::{return_columns, star_scope_names, visible_keys, ProjectionFields};
use super::{materialize_rows_strict, QueryResult, RowFeed};

/// The statement's result columns for a `RETURN`/`WITH`: the shared
/// [`return_columns`] item set, with a `RETURN *`/`WITH *` item expanded
/// from the first row's visible keys — or, when the table is empty, from
/// the declared scope `star_scope_names` walks. This answers the *result*
/// shape the dispatcher materializes; the front's compile-time admission
/// set for the following clause is a different question, asked by
/// `clause_introduced_names` / `call_subquery_exports` in `relate-front`.
pub(crate) fn projection_result_columns(
    graph: &ExecGraph<'_>,
    items: &[Projection],
    rows: &[Row],
    clauses: &[Clause],
) -> Vec<String> {
    return_columns(items, || match rows.first() {
        None => star_scope_names(clauses),
        Some(row) => {
            let mut keys = visible_keys(graph, row);
            keys.sort();
            keys
        }
    })
}

/// The statement result of a `RETURN`: the result columns, the strict cell
/// move, and the intermediate result carrying the view's own stats. The
/// caller's PROFILE tick has already read the rows, so moving the cells is
/// safe.
pub(crate) fn return_result(
    graph: &ExecGraph<'_>,
    items: &[Projection],
    clauses: &[Clause],
    rows: &mut [Row],
) -> Result<QueryResult> {
    let columns = projection_result_columns(graph, items, rows, clauses);
    // Projection rows are rectangular by contract (ADR-0339): every result
    // column is bound, so a miss is an engine contract break. Move the cells
    // out instead of cloning.
    let data = materialize_rows_strict(rows, &columns)?;
    // The view's own stats: a read view carries no scratch, so its stats are
    // already the default, and the read path's wrapper (`execute_read_at`)
    // resets them on the way out regardless — one policy, not a per-arm mode
    // branch.
    Ok(QueryResult::intermediate(columns, data, graph.stats()))
}

/// Whether a projection only maps rows: no aggregation, `DISTINCT`,
/// `ORDER BY`, `SKIP` or `LIMIT`, so each input row yields at most one
/// output row independent of every other and the clause can run on a stream
/// of bounded batches (a `WHERE` filters per row and streams). The
/// complement — the breakers — keep the held path.
pub(crate) fn maps_each_row(fields: &ProjectionFields<'_>) -> bool {
    !fields.distinct
        && fields.group_by.is_none()
        && fields.order.is_empty()
        && fields.skip.is_none()
        && fields.limit.is_none()
        && !fields.items_aggregate()
        && !fields.where_clause.is_some_and(is_agg_expr)
}

/// The next clause's projection fields and decided shape, when it is a
/// `WITH`/`RETURN` at all — the prologue the `MATCH` fold predicate reads.
/// A shape [`ProjectionShape::decide`] rejects yields `None`: the clause
/// never folds a feed, and the projection itself fails closed at the same
/// decision when the clause reaches it.
fn next_fold_shape<'a>(
    next: Option<&'a Clause>,
) -> Option<(ProjectionFields<'a>, ProjectionShape)> {
    let fields = next.and_then(ProjectionFields::from_clause)?;
    let shape = ProjectionShape::decide(&fields).ok()?;
    Some((fields, shape))
}

/// The fold pair when the clause after a `MATCH` consumes its rows as a
/// stream (a `WITH`/`RETURN` whose projection folds an aggregation as rows
/// arrive — [`ProjectionShape::streams`]): the one derivation both readers
/// take, [`streams_into_projection`]'s answer and [`open_match_fold`]'s
/// fields for the class.
fn folding_shape(next: Option<&Clause>) -> Option<(ProjectionFields<'_>, ProjectionShape)> {
    next_fold_shape(next).filter(|(_, shape)| shape.streams())
}

/// Whether the clause after a `MATCH` consumes its rows as a stream. Every
/// other follower takes the rows as a held vector, as ever.
pub(crate) fn streams_into_projection(next: Option<&Clause>) -> bool {
    folding_shape(next).is_some()
}

/// Open the aggregation a streamed `MATCH` folds into, for the
/// `WITH`/`RETURN` whose shape [`ProjectionShape::streams`] accepts: the
/// plan [`project_rows`] would have built from the collected rows, minus
/// the rows. The parked-key plan is the class's own
/// ([`ProjectionClass::order_keys`]) — one construction, read here.
fn open_fold<'a>(
    graph: &ExecGraph<'_>,
    class: &'a ProjectionClass<'a>,
    params: &'a BTreeMap<String, Value>,
    scope: Option<&'a Row>,
) -> Result<Aggregation<'a>> {
    Aggregation::open(
        graph,
        &[],
        class.fields.items,
        class.fields.group_by,
        params,
        &class.group_subclauses(scope),
    )
}

/// The fold an uncapped `MATCH` streams into when the clause after it
/// aggregates (`next`), `None` when it does not stream or a fold already
/// parked its classification in `class_slot` (the slot keeps the plan
/// alive for the fold's borrow).
pub(crate) fn open_match_fold<'a>(
    graph: &ExecGraph<'_>,
    next: Option<&'a Clause>,
    class_slot: &'a OnceCell<ProjectionClass<'a>>,
    params: &'a BTreeMap<String, Value>,
    scope: Option<&'a Row>,
) -> Result<Option<MatchFold<'a>>> {
    if class_slot.get().is_some() {
        return Ok(None);
    }
    let Some((fields, shape)) = folding_shape(next) else {
        return Ok(None);
    };
    // The slot owns the clause's one classification: the parked-key plan
    // is built exactly once, here, and kept alive for the fold's borrow
    // (a fold that already claimed the slot was declined above).
    let class = class_slot.get_or_init(|| ProjectionClass::new(graph, &fields, shape));
    open_fold(graph, class, params, scope).map(|aggregation| Some(MatchFold::new(aggregation)))
}

/// The projection's payload-inflation policy: identity, or a materializing
/// refresh through the projection's one cache. One value carries both the
/// per-value policy and whether the post-projection row sweep runs, so the
/// two cannot disagree.
enum Refresh<'a> {
    Identity,
    Materialize(&'a mut MaterializeCache),
}

impl Refresh<'_> {
    /// The policy applied to one value.
    fn apply(&mut self, graph: &ExecGraph<'_>, value: Value) -> Value {
        match self {
            Refresh::Identity => value,
            Refresh::Materialize(cache) => graph.materialize_cached(value, cache),
        }
    }

    /// The post-projection sweep: refresh every visible column of every row.
    /// The parked `__ord*` sort keys keep the values the group or sink
    /// produced — they are compared, not returned, so they are not inflation
    /// surface.
    fn rows(&mut self, graph: &ExecGraph<'_>, rows: &mut [Row]) {
        if matches!(self, Refresh::Identity) {
            return;
        }
        for row in rows {
            for (key, value) in row.iter_mut() {
                if !graph.is_internal_key(key) {
                    let taken = std::mem::replace(value, Value::Null);
                    *value = self.apply(graph, taken);
                }
            }
        }
    }
}

/// Where a `RETURN`/`WITH` projection's incoming rows come from, as the
/// dispatcher's loop carries it between clauses. The sink's caller is the
/// only authority for the projected case: a row carries no marker a user
/// alias could forge, and an ordinary batch whose bindings happen to bind
/// every output name is still evaluated, not passed through. The
/// projection itself reads the decided [`ProjectionInput`], never this
/// flag.
#[derive(Clone, Copy)]
pub(crate) enum RowSource {
    /// Bindings the projection must evaluate its items against.
    Bindings,
    /// The match sink already evaluated this clause's items on every row
    /// (their columns plus the parked `__ord*` sort keys), so only the
    /// sort, page, DISTINCT, and column-extraction stages are left.
    SinkProjected,
    /// A bounded sink already landed these rows in the clause's own
    /// container; the carried facts say which tail stages it ran, so the
    /// projection adopts the landing instead of re-opening a container.
    SinkLanded(LandedTail),
}

/// The one input type of the projection: the rows it evaluates its items
/// against ([`RowFeed`]: held, generated or already folded), or the rows a
/// producer already projected — which owns them, so the sink shape can
/// never hold a generated or folded feed.
pub(crate) enum ProjectionInput<'a> {
    /// Bindings the projection evaluates its items against.
    Feed(RowFeed<'a>),
    /// The match sink's (or a grouped shortcut's) rows, already carrying
    /// this clause's columns plus the parked `__ord*` sort keys: only the
    /// sort, page, DISTINCT and column-extraction stages are left.
    SinkProjected(Vec<Row>),
    /// A bounded sink's landing: the rows in the clause's own container and
    /// the tail stages it already ran.
    SinkLanded(Landed),
}

impl ProjectionInput<'_> {
    /// The rows already in hand, for the decisions that read the row shape
    /// before the walk starts.
    fn probe(&self) -> &[Row] {
        match self {
            Self::Feed(feed) => feed.probe(),
            Self::SinkProjected(rows) => rows,
            Self::SinkLanded(landed) => &landed.rows,
        }
    }

    /// Finish this input when the clause abandons it — a `LIMIT 0` answers
    /// no rows, but it must not cancel the input's own work: a completing
    /// stream (`CALL { … } IN TRANSACTIONS`) runs its remaining batches —
    /// and reports its PROFILE facts — on `stop`, never on drop. The rule
    /// lives on the input type, so a new input kind states its own contract
    /// instead of inheriting "nothing to finish" by silence.
    fn complete(&mut self) -> Result<()> {
        // Exhaustive: every kind names its own answer, so a new input kind
        // fails to compile until it states one.
        match self {
            Self::Feed(RowFeed::Held(stream)) => stream.stop(),
            Self::Feed(RowFeed::Folded(_)) | Self::SinkProjected(_) | Self::SinkLanded(_) => Ok(()),
        }
    }
}

pub(crate) fn project_rows(
    ctx: ExecCtx<'_>,
    input: ProjectionInput<'_>,
    fields: &ProjectionFields<'_>,
    inflate: bool,
    scope: Option<&Row>,
) -> Result<Vec<Row>> {
    let ExecCtx { graph, .. } = ctx;
    // The clause's one classification: every decision below reads it, so
    // the paths cannot disagree on the shape — and the shape's decision
    // ([`ProjectionShape::decide`]) owns the DISTINCT-* rejection.
    let class = ProjectionClass::new(graph, fields, ProjectionShape::decide(fields)?);
    project_classed(ctx, &class, input, inflate, scope)
}

/// [`project_rows`] over the caller's already-built classification — for
/// the grouped finishes, which park their folded rows through the class's
/// own parked-key plan ([`ProjectionClass::order_keys`]) before handing
/// them over, so one plan both writes and reads the parked columns and no
/// caller re-derives the plan the projection is about to build anyway.
pub(super) fn project_classed(
    ctx: ExecCtx<'_>,
    class: &ProjectionClass<'_>,
    mut input: ProjectionInput<'_>,
    inflate: bool,
    scope: Option<&Row>,
) -> Result<Vec<Row>> {
    let ExecCtx { graph, params, .. } = ctx;
    // The clause's paging bounds, resolved once, and the bounded window of
    // the ordered `LIMIT` shape (ADR-0555 §3) derived from them — the one
    // pair of facts the feed, the non-aggregating merge paths and the
    // finish tail all read, so a nondeterministic bound cannot size the
    // window differently from the page it feeds. `aggregate_rows` takes no
    // bounds: the aggregating shape's group table is its own declared
    // breaker, which is exactly what `window_keep` masks.
    let bounds = class.page_bounds(graph, params)?;
    // An empty page answers nothing, decided here before any container is
    // opened or any row is built: a `LIMIT 0` costs O(1) whatever the
    // input, and no merge path ever carries a zero-capacity container.
    if bounds.answers_nothing() {
        // The empty page stops the rows, never the input's own work (the
        // input type owns that rule).
        input.complete()?;
        return Ok(Vec::new());
    }
    // The prepared input carries its own reservation: whatever container it
    // hands back is priced for as long as this projection holds it — the
    // charge's owner is the handoff, never a caller that happens to know the
    // shape (the windowed shape's live feed was the one case priced here
    // before; a held table is the same fact).
    let (input, _input_charge) = prepare_input(input, class, bounds)?;
    // One cache for the whole projection: its expressions never write, so
    // the graph state, and with it each payload's refresh, is fixed here.
    let mut cache = MaterializeCache::default();
    let mut refresh = if inflate {
        Refresh::Materialize(&mut cache)
    } else {
        Refresh::Identity
    };
    let merged = merge_rows(graph, params, class, input, scope, &mut refresh, bounds)?;
    finish_projection(graph, params, class, merged, bounds)
}

/// The merge dispatch: which path turns the prepared input into the
/// projection's landed rows. One dispatch over both facts — the input's
/// kind and the clause's shape — so a pre-projected input has to say what
/// it means for every shape, and a shape added later cannot inherit the
/// pass-through by accident.
fn merge_rows(
    graph: &ExecGraph<'_>,
    params: &BTreeMap<String, Value>,
    class: &ProjectionClass<'_>,
    input: ProjectionInput<'_>,
    scope: Option<&Row>,
    refresh: &mut Refresh<'_>,
    bounds: PageBounds,
) -> Result<Landed> {
    match (input, class.shape) {
        // The sink already projected these rows, so the pass-through's only
        // mutation is the inflate refresh, in place, and the landing owns
        // the container (ADR-0463's retaining `Vec`, or the ordered
        // window's bounded heap).
        (ProjectionInput::SinkProjected(mut rows), ProjectionShape::Plain) => {
            refresh.rows(graph, &mut rows);
            adopt_rows(graph, params, class, bounds, rows)
        }
        // A bounded sink already ran the clause's container; the carried
        // landing's facts say so, and the only mutation is the in-place
        // inflate refresh.
        (ProjectionInput::SinkLanded(mut landed), ProjectionShape::Plain) => {
            refresh.rows(graph, &mut landed.rows);
            Ok(landed)
        }
        // `sinkable_projection` declines every aggregating and `*` shape, so
        // a pre-projected feed never arrives with one — and neither form has
        // a pre-projected policy to fall back on.
        (ProjectionInput::SinkProjected(_) | ProjectionInput::SinkLanded(_), _) => Err(
            Error::internal("a pre-projected feed reached a shape the sink never projects"),
        ),
        // The star and plain shapes consume the batch stream itself —
        // their row assembly lands each pulled row — so the arm hands
        // them the one input they can take, never a conversion that
        // refuses a variant at runtime.
        (ProjectionInput::Feed(RowFeed::Held(stream)), ProjectionShape::Star) => {
            star_rows(graph, params, stream, class, refresh, bounds)
        }
        // The aggregating path lands no container: its charge lives in
        // the aggregation's own emission loop, and its rows are an
        // unordered table the tail still sorts. It is also the one shape
        // that finishes a folded feed.
        (ProjectionInput::Feed(feed), ProjectionShape::Aggregate { .. }) => Ok(Landed {
            rows: aggregate_rows(graph, params, feed, class, scope, refresh)?,
            windowed: false,
            streamed_distinct: false,
            funnel_filtered_where: false,
            streamed_page: false,
        }),
        (ProjectionInput::Feed(RowFeed::Held(stream)), ProjectionShape::Plain) => {
            plain_rows(graph, params, stream, class, refresh, bounds)
        }
        // The impossible pairing, stated once where the pairing is
        // decided: a folded feed is finished by the aggregating shape
        // alone (the streaming match sink builds one only for a folding
        // consumer).
        (
            ProjectionInput::Feed(RowFeed::Folded(_)),
            ProjectionShape::Star | ProjectionShape::Plain,
        ) => Err(Error::internal(
            "a folded feed reached a shape that cannot finish it",
        )),
    }
}

/// The input as the merge paths consume it — the pipeline's
/// materialization stage: a collected row vector, or — when the projection
/// folds a generated feed ([`ProjectionClass::folds_feed`]: the
/// aggregation, the ordered window over its once-resolved bounds, a
/// pass-through, a DISTINCT first-seen pass) — the rows still flowing. A
/// parked feed that resolves to no window is collected here under the one
/// retaining charge, and the eager pre-projection page applies at this
/// stage, so a `LIMIT 1` must not evaluate a later row whose projection
/// raises an error (CIP-27).
fn prepare_input<'a>(
    input: ProjectionInput<'a>,
    class: &ProjectionClass<'_>,
    bounds: PageBounds,
) -> Result<(ProjectionInput<'a>, relate_core::tokens::WorkspaceCharge)> {
    // One exhaustive dispatch: every input kind states its own handoff, so
    // a new kind fails to compile until it states one. The stream forms
    // stay streams — a generated stream for the shapes that fold it as
    // rows arrive ([`ProjectionClass::folds_feed`]): the window's `land`
    // is a per-row fold, every row is evaluated to compare it, so the
    // producer is never materialized on those shapes; a folded feed is
    // already the producer's state, priced where it was built. Everything
    // else is collected: a pre-projected input stays pre-projected, every
    // collected feed becomes held rows.
    let (rows, wrap): (Vec<Row>, Rewrap<'a>) = match input {
        // A bounded sink's landing is already the clause's answer page (or
        // first-seen set); it carries its own facts and pages itself.
        ProjectionInput::SinkLanded(landed) => {
            let charge = held_charge(&landed.rows)?;
            return Ok((ProjectionInput::SinkLanded(landed), charge));
        }
        ProjectionInput::SinkProjected(rows) => (rows, ProjectionInput::SinkProjected),
        ProjectionInput::Feed(RowFeed::Folded(folded)) => {
            return Ok((
                ProjectionInput::Feed(RowFeed::Folded(folded)),
                relate_core::tokens::WorkspaceCharge::empty(),
            ));
        }
        ProjectionInput::Feed(RowFeed::Held(stream))
            if class.folds_feed(bounds) && !stream.is_held() =>
        {
            // A streamed feed is priced by its producer, never here: the
            // guard already excluded the held container (the one source
            // whose probe is non-empty), so this arm holds no reservation.
            return Ok((
                ProjectionInput::Feed(RowFeed::Held(stream)),
                relate_core::tokens::WorkspaceCharge::empty(),
            ));
        }
        // The generator's own materializing form — the one place a feed
        // that cannot fold becomes rows, so its charge cannot drift from
        // the one the clause arm pays.
        ProjectionInput::Feed(RowFeed::Held(stream)) => (stream.materialize()?, held_input),
    };
    // The eager shape pages here, on the clause's one resolved bounds;
    // every other shape leaves the page to the finish tail.
    let input = wrap(if class.eager_paged {
        page(rows, bounds)
    } else {
        rows
    });
    let charge = if matches!(class.shape, ProjectionShape::Aggregate { .. }) {
        // The aggregating shape's own input bill reserves these rows and
        // releases them per landing (InputBill's measure is the same
        // row walk) — a second reservation here would price one
        // container twice and refuse statements whose real footprint
        // fits the pool.
        relate_core::tokens::WorkspaceCharge::empty()
    } else {
        held_charge(input.probe())?
    };
    Ok((input, charge))
}

/// The reservation for the container the projection is handed: the rows it
/// holds for as long as it holds them (a streamed feed's rows belong to its
/// producer and are priced there, so its probe is empty and this is a
/// no-op).
fn held_charge(rows: &[Row]) -> Result<relate_core::tokens::WorkspaceCharge> {
    relate_core::tokens::WorkspaceCharge::retain_with(|| crate::workspace::row_buffer_bytes(rows))
}

/// How collected rows are handed back as the projection's input.
type Rewrap<'a> = fn(Vec<Row>) -> ProjectionInput<'a>;

/// Collected rows as the projection's feed input.
fn held_input(rows: Vec<Row>) -> ProjectionInput<'static> {
    ProjectionInput::Feed(RowFeed::Held(RowStream::held(rows)))
}

/// The `RETURN *` mode: the visible incoming bindings plus the explicit
/// items, with every output row charged as it lands.
fn star_rows(
    graph: &ExecGraph<'_>,
    params: &BTreeMap<String, Value>,
    stream: RowStream<'_>,
    class: &ProjectionClass<'_>,
    refresh: &mut Refresh<'_>,
    bounds: PageBounds,
) -> Result<Landed> {
    // ADR-0463 (LABS-781): the star branch's output rows are as retaining
    // as the plain branch's — same guard, same landing policy — after the
    // spent source row is dropped so `owned_heap` sees the output
    // container's real ownership. The ordered-window shape lands the same
    // built row into the bounded heap instead.
    let built = |row| star_output_row(graph, params, class, refresh, row);
    land_feed(graph, params, class, bounds, stream, built)
}

/// The `*` shape's row assembly: the visible incoming bindings plus the
/// explicit items. Shared by the collected landing ([`star_rows`]) and the
/// pass-through page fold ([`land_feed`]).
fn star_output_row(
    graph: &ExecGraph<'_>,
    params: &BTreeMap<String, Value>,
    class: &ProjectionClass<'_>,
    refresh: &mut Refresh<'_>,
    r: Row,
) -> Result<Row> {
    let mut n = Row::new();
    for (k, v) in &r {
        if !graph.is_internal_key(k) {
            n.insert(k.clone(), refresh.apply(graph, v.clone()));
        }
    }
    for (projection, name) in &class.value_items {
        let value = eval(graph, &r, &projection.expr, params)?;
        n.insert(name.clone(), refresh.apply(graph, value));
    }
    drop(r);
    Ok(n)
}

/// The aggregation mode: HAVING (the aggregate-call WHERE) is evaluated per
/// group inside the aggregation, and the group rows were charged in the
/// emission loop. The clause's items, grouping keys, parked-key plan and
/// HAVING predicate are the classification's own — read from it, never
/// handed back as loose parameters.
fn aggregate_rows(
    graph: &ExecGraph<'_>,
    params: &BTreeMap<String, Value>,
    feed: RowFeed<'_>,
    class: &ProjectionClass<'_>,
    scope: Option<&Row>,
    refresh: &mut Refresh<'_>,
) -> Result<Vec<Row>> {
    // ADR-0453: the caller routed the aggregate-call WHERE here as HAVING —
    // the aggregation path evaluates it per group (aggregate calls fold the
    // group, non-aggregate leaves read the projected row) and filters
    // there; the plain filter would evaluate the aggregates against the
    // aggregated row and fail.
    let mut agg = match feed {
        RowFeed::Folded(aggregation) => aggregation.finish(graph)?,
        // The breaker answers its own batch stream (ADR-0660); the inflate
        // refresh below rewrites each answered row in place.
        RowFeed::Held(stream) => aggregate_ex(
            graph,
            stream,
            class.fields.items,
            class.fields.group_by,
            params,
            &class.group_subclauses(scope),
        )?,
    };
    refresh.rows(graph, &mut agg);
    // The rows were charged inside aggregate_ex's emission loop, as each
    // group row landed; the inflate refresh above materializes graph-cached
    // payloads into row-owned copies whose delta rides uncharged (a bounded
    // approximation: the subquery-scope materialization path, its payloads
    // already counted once at their construction or borrowed from the
    // store).
    Ok(agg)
}

/// The plain projection mode: each item over the incoming row, the kept
/// bindings the subclauses may name, then the parked order keys — one
/// landing policy per output row. The items, the parked-key plan and the
/// plain WHERE are the classification's own — read from it, never handed
/// back as loose parameters.
fn plain_rows(
    graph: &ExecGraph<'_>,
    params: &BTreeMap<String, Value>,
    stream: RowStream<'_>,
    class: &ProjectionClass<'_>,
    refresh: &mut Refresh<'_>,
    bounds: PageBounds,
) -> Result<Landed> {
    let keep = keep_bindings(class);
    // ADR-0463: the projected rows are one retaining container. Each output
    // row's payloads plus its buffers grow the running total; the
    // per-allocation checks alone cannot see a many-row result whose rows
    // each build a large but under-cap value. The ordered-window shape
    // (ADR-0555 §3) lands the same built row into the bounded top-k heap
    // instead, its plain WHERE read as each row arrives.
    let built = |row| plain_output_row(graph, params, class, &keep, refresh, row);
    land_feed(graph, params, class, bounds, stream, built)
}

/// The bindings a plain projection keeps from its incoming row: the ones a
/// `WITH` filter or an `ORDER BY` key may name though the projection does
/// not produce them. The one derivation, read by [`plain_rows`] and the
/// pass-through page fold ([`land_feed`]).
fn keep_bindings(class: &ProjectionClass<'_>) -> HashSet<String> {
    // Which names the WHERE / `ORDER BY` keys read from the *input* row —
    // one walker for the whole clause. A plain projection keeps its input
    // names in scope for them (`WITH n.g AS g WHERE n.v > 4` still reads
    // `n`); a `DISTINCT` projection ends them, so a subquery body in the
    // WHERE that re-uses a dropped name binds it fresh (Neo4j-verified;
    // REL8-2023 issue 64) and only the subclause's own row-scope reads are
    // carried into the built row. The aggregating shapes take their own
    // seed derivation ([`super::aggregate::SeedStore`]), narrowed the same
    // way.
    let input_names: fn(&relate_ir::Expr, &mut HashSet<String>) = if class.fields.distinct {
        expr_row_vars
    } else {
        expr_vars
    };
    let mut keep = HashSet::new();
    if let Some(w) = class.plain_where() {
        input_names(w, &mut keep);
    }
    for expr in class.order_keys.exprs() {
        input_names(expr, &mut keep);
    }
    keep
}

/// The plain shape's row assembly: the item values first, against the
/// incoming row, then the kept bindings, then the items over them — the
/// projected row a key is evaluated against. Shared by the collected
/// landing ([`plain_rows`]) and the pass-through page fold
/// ([`land_feed`]).
fn plain_output_row(
    graph: &ExecGraph<'_>,
    params: &BTreeMap<String, Value>,
    class: &ProjectionClass<'_>,
    keep: &HashSet<String>,
    refresh: &mut Refresh<'_>,
    mut row: Row,
) -> Result<Row> {
    let projected: Vec<(String, Value)> = class
        .value_items
        .iter()
        .map(|(projection, name)| {
            let val = eval(graph, &row, &projection.expr, params)?;
            Ok((name.clone(), refresh.apply(graph, val)))
        })
        .collect::<Result<Vec<_>>>()?;
    let mut row2 = Row::new();
    for (name, value) in row.iter_mut() {
        if keep.contains(name.as_str()) {
            row2.insert(name.clone(), std::mem::replace(value, Value::Null));
        }
    }
    for (name, value) in projected {
        row2.insert(name, value);
    }
    // The source row is spent — its kept bindings moved, the rest dropped —
    // so the built row's values are the output container's own and
    // `owned_heap` sees their real ownership: a projected variable read is
    // a clone that shared the source row's `Arc` while it lived, and
    // charging before the drop would skip its payload.
    drop(row);
    Ok(row2)
}

#[cfg(test)]
mod refresh_tests {
    use super::*;
    use crate::graph::Graph;
    use crate::row::{interned_schema_calls, RowNames};

    /// The inflate sweep updates each visible column in place: the row keeps
    /// its schema, so a sweep over wide rows interns no schema (a
    /// remove-and-reinsert rebuilt and re-interned one per column per row —
    /// quadratic in the column count), and the column order and the parked
    /// `__ord*` key survive untouched.
    #[test]
    fn the_inflate_sweep_keeps_each_rows_schema() {
        let names: Vec<String> = ["a", "b", "c", "d", "e", "f", "g", "h", "__ord0"]
            .map(String::from)
            .to_vec();
        let schema = RowNames::new(&names);
        let mut rows: Vec<Row> = (0..1000)
            .map(|i| Row::from_names(&schema, (0..9).map(|c| Value::Int(i + c)).collect()))
            .collect();
        let graph = Graph::default();
        let view = ExecGraph::read(&graph);
        let mut cache = MaterializeCache::default();
        let before = interned_schema_calls();
        let snapshot = rows.clone();
        let ptrs: Vec<_> = rows.iter().map(Row::schema_ptr).collect();
        Refresh::Materialize(&mut cache).rows(&view, &mut rows);
        assert_eq!(interned_schema_calls() - before, 0);
        // `Row` equality compares schema names by value; the pointers pin
        // that no row swapped its shared schema for an equal copy.
        assert_eq!(
            rows.iter().map(Row::schema_ptr).collect::<Vec<_>>(),
            ptrs,
            "the sweep replaced a row's shared schema"
        );
        assert_eq!(rows, snapshot);
    }
}