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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
//! Streaming clause stages (ADR-0555, LABS-1439).
//!
//! The clause driver hands the rows of one clause to the next as a whole
//! vector. For `MATCH (a), (b) … MATCH …` that vector is the cross product
//! of the first clause, retained and charged against the per-value
//! expression cap although the second clause consumes it row by row. The
//! general rule this module states: **a clause that maps each input row to
//! zero or more output rows is a stage, not a barrier**. A run of stages
//! after a `MATCH` is one push pipeline: the head clause's walk offers each
//! finished row to the first stage ([`RowFold`], the contract the
//! aggregation fold already uses), a stage buffers at most [`CHAIN_BATCH`]
//! rows and runs its transform over them, and what the transform emits goes
//! to the next stage the same way — a `MATCH` stage by handing the next
//! stage to its operator ladder as the fold, so its own walk pushes row by
//! row and never builds the batch's output.
//!
//! **Stages** (stream; hold at most a batch per stage):
//! - a plain `MATCH` (default match mode, not `OPTIONAL`, not `SEARCH`);
//! - a projection-only `WITH`: no aggregation, `DISTINCT`, `ORDER BY`,
//!   `SKIP` or `LIMIT` (the `WHERE` filter is per-row and streams).
//!
//! **Barriers** (the pipeline ends before them; the held path takes the
//! rows): the breakers — aggregation, `DISTINCT`, `ORDER BY`, `SKIP`,
//! `LIMIT` — and every clause kind not listed as a stage (`OPTIONAL MATCH`,
//! `SEARCH`, `UNWIND`, `CALL`, subqueries, writes). An aggregating
//! `WITH`/`RETURN` right after the run is the one barrier the pipeline
//! feeds directly: the run's last stage folds into it as rows are emitted
//! (the #110 mechanism), so the breaker holds its own group state and
//! nothing else. Any other barrier receives the last stage's output as a
//! vector; that vector is charged per row into one retaining container
//! (ADR-0463), exactly as the clause's own output vector was, so memory
//! scales with what the last stage emits, never with what an earlier stage
//! produced.
//!
//! Semantics are the held path's. Row order: batches are consecutive
//! slices of the upstream output and every stage emits per input row in
//! input order, so the sequence is unchanged. Relationship uniqueness is
//! per clause (a fresh per-clause used-set either way). The stages read
//! only, so side-effect order is untouched. A deadline or cancellation is
//! observed by the operator ladder's per-row ticks and the per-batch check
//! here. An error surfaces for the same statement but, as ADR-0555 notes,
//! may now surface before an upstream clause has finished.
//!
//! The pipeline stays off a stage the held path serves better or carries
//! behaviour a per-batch run would lose: a `MATCH` an aggregate fold
//! answers in one pass over the whole entering row set, a `MATCH` with a
//! limit cap (its early exit lives in the walker), and a lone trailing
//! `WITH` that would only hand a collector the rows the sink projection
//! already builds. PROFILE runs the pipeline too: a held path under
//! PROFILE would materialize the clause's whole output, changing the
//! memory shape of the very statement PROFILE reports. Each consumed
//! stage records its own facts instead — the sidecar in
//! `profile_chain.rs` ([`StageStats`]).

use std::cell::{Cell, OnceCell, RefCell};
use std::collections::HashSet;

use relate_core::Result;
use relate_ir::{Clause, Expr, MatchMode, PathPattern};

use crate::ctx::ExecCtx;
use crate::eval::rows_logical_bytes;
use crate::metrics::PlanMetricsSite;
use crate::overlay::{ExecGraph, RetainedTotal};
use crate::row::Row;

use super::aggregate::Aggregation;
use super::classification::ProjectionClass;
use super::feed::RowStream;
use super::match_clause::{
    charge_and_fold, run_match_operator_ladder, LadderRun, MatchClauseInputs,
};
use super::match_consumer::{open_sink, Sink};
use super::match_folds::fold_would_take;
use super::pattern_policies::{MatchFold, RowFold};
use super::profile_chain::{CountingFold, StageStats};
use super::project::{
    maps_each_row, open_match_fold, project_rows, streams_into_projection, ProjectionInput,
    RowSource,
};
use super::pushdown::{live_from, match_limit_cap, trim_rows};
use super::scope::{carry_scope_bindings, ProjectionFields};
use super::sink::{sinkable_projection, SinkProjection};
use super::{RowFeed, StageDelta};

/// The most rows a stage buffers before it runs its transform over them
/// (ADR-0555: bounded batches, low hundreds by default).
pub(crate) const CHAIN_BATCH: usize = 256;

/// The most stages one run covers. A stage's flush and finish recurse into
/// the next stage, so the run length bounds the native stack depth
/// independently of how long the statement is; clauses past the cap are
/// barriers and take the held path.
const MAX_STAGES: usize = 16;

/// What a stage does to a batch of rows, with everything only that kind of
/// stage reads.
enum Transform<'a> {
    /// A plain `MATCH`.
    Match {
        patterns: &'a [PathPattern],
        where_clause: Option<&'a Expr>,
        /// The sink projection of the clause after the run, set on the
        /// last stage when one is sinkable.
        projection: Option<SinkProjection<'a>>,
        /// What the stage's input rows keep (the suffix's names).
        live_in: Option<HashSet<String>>,
        /// The statement's clauses after this one: the operator ladder
        /// reads the suffix to prove answer shapes, as for the held clause.
        tail: &'a [Clause],
    },
    /// A projection-only `WITH`.
    With {
        spec: ProjectionFields<'a>,
        /// The importing subquery's scope bindings a `WITH` must keep.
        scope: Option<&'a Row>,
    },
}

/// Where a stage's output goes.
enum Next<'a> {
    /// The next stage of the run.
    Stage(Box<Stage<'a>>),
    /// The aggregation of the barrier after the run.
    Fold(Box<MatchFold<'a>>),
    /// The bounded sink of the barrier after the run (ADR-0555 §3): the last
    /// stage's projected rows fold into it as they arrive.
    Sink(Box<Sink<'a>>),
    /// The vector the barrier after the run receives.
    Rows(Collector),
}

impl Next<'_> {
    /// The consumer of what a stage emits: every kind of consumer takes
    /// rows through the one [`RowFold`] contract.
    fn consumer(&self) -> &dyn RowFold {
        match self {
            Next::Stage(stage) => stage.as_ref(),
            Next::Fold(fold) => fold.as_ref(),
            Next::Sink(sink) => sink.as_fold(),
            Next::Rows(collector) => collector,
        }
    }
}

/// The run's retained output: what the barrier after the run takes as its
/// input rows, charged per row (ADR-0463).
struct Collector {
    shape: Collected,
    rows: RefCell<Vec<Row>>,
    retained: RefCell<RetainedTotal>,
}

/// What a collector's rows are, decided once at open.
enum Collected {
    /// Bindings; the set is what the barrier after the run still names,
    /// `None` keeps every binding. A `WITH`'s rows (its projection already
    /// dropped the rest) are never trimmed.
    Bindings(Option<HashSet<String>>),
    /// The sink projection's columns (their columns are the output),
    /// never trimmed.
    SinkProjected,
}

impl Collected {
    fn source(&self) -> RowSource {
        match self {
            Self::Bindings(_) => RowSource::Bindings,
            Self::SinkProjected => RowSource::SinkProjected,
        }
    }
}

impl RowFold for Collector {
    fn offer(&self, graph: &ExecGraph<'_>, mut row: Row) -> Result<()> {
        if let Collected::Bindings(Some(live)) = &self.shape {
            trim_rows(graph, std::slice::from_mut(&mut row), live);
        }
        self.retained.borrow_mut().add_row(graph, row.values())?;
        self.rows.borrow_mut().push(row);
        Ok(())
    }
}

/// One clause of the run: its transform, its input buffer and its consumer.
struct Stage<'a> {
    ctx: ExecCtx<'a>,
    transform: Transform<'a>,
    buffer: RefCell<Vec<Row>>,
    /// Whether the transform has run at least once.
    ran: Cell<bool>,
    next: Next<'a>,
    /// Rows offered into the stage across its batches — the head stage's
    /// figure is the head clause's offered-row fact (what its walk fed
    /// the run), the count the landing reports. A production fact, not an
    /// instrumentation one: one `Cell` add per batch, the
    /// `MatchFold::offer` and `StreamedStats::note_row` precedent.
    rows_in: Cell<u64>,
    /// The stage's PROFILE facts (`profile_chain.rs`), present only when
    /// the run observes itself, reported once it drains.
    stats: Option<StageStats>,
}

/// A run of streamed clauses after a `MATCH`: the push consumer of the
/// head clause's walk.
pub(crate) struct MatchChain<'a> {
    head: Stage<'a>,
    /// How many clauses after the head the run covers.
    stages: usize,
    /// The PROFILE site with the head clause's index: present when the
    /// statement is PROFILEd — the run then observes itself through the
    /// stage sidecars and reports each consumed stage's own facts when it
    /// drains. Its absence is the untimed form: the stages' windows and
    /// notes run bare.
    record: Option<(PlanMetricsSite<'a>, usize)>,
}

/// The stage `clauses[0]` is, or `None` when it is a barrier.
///
/// A plain `MATCH` is a stage unless the held path serves it better: the
/// fold family taking it with its aggregate, a limit cap stopping its walk,
/// or any shape a per-batch run would not serve identically. A `WITH` is a
/// stage when it only maps rows.
fn stage_transform<'a>(
    ctx: ExecCtx<'a>,
    clauses: &'a [Clause],
    scope: Option<&'a Row>,
) -> Result<Option<Transform<'a>>> {
    match &clauses[0] {
        Clause::Match {
            optional: false,
            match_mode: MatchMode::DifferentRelationships,
            patterns,
            where_clause,
            search: None,
            using_index: _,
            using_join: _,
        } => {
            let ExecCtx { graph, params, .. } = ctx;
            // The widest cap arming (one entering row) is the conservative
            // question.
            if match_limit_cap(
                &clauses[1..],
                patterns,
                where_clause.as_ref(),
                graph,
                params,
                1,
            )?
            .is_some()
            {
                return Ok(None);
            }
            // The fold family answers a `MATCH` plus its aggregate in one
            // pass over the whole entering row set; a rung that would take
            // this clause keeps it out of the streaming stages.
            if fold_would_take(graph, clauses, params)? {
                return Ok(None);
            }
            Ok(Some(Transform::Match {
                patterns,
                where_clause: where_clause.as_ref(),
                projection: None,
                live_in: live_from(clauses),
                tail: &clauses[1..],
            }))
        }
        Clause::With { .. } => {
            let spec = ProjectionFields::of_projection(&clauses[0])?;
            Ok(maps_each_row(&spec).then_some(Transform::With { spec, scope }))
        }
        _ => Ok(None),
    }
}

/// The run of stages starting at `clauses[0]`. A trailing `WITH` whose
/// barrier is not an aggregation is dropped from the run: it would only
/// feed a collector what the preceding `MATCH`'s sink projection already
/// builds.
fn stage_run<'a>(
    ctx: ExecCtx<'a>,
    clauses: &'a [Clause],
    scope: Option<&'a Row>,
) -> Result<Vec<Transform<'a>>> {
    let mut run = Vec::new();
    while run.len() < MAX_STAGES {
        let Some(rest) = clauses.get(run.len()..).filter(|rest| !rest.is_empty()) else {
            break;
        };
        let Some(transform) = stage_transform(ctx, rest, scope)? else {
            break;
        };
        run.push(transform);
    }
    while matches!(run.last(), Some(Transform::With { .. }))
        && !streams_into_projection(clauses.get(run.len()))
    {
        run.pop();
    }
    Ok(run)
}

/// Open the pipeline for the `MATCH` at `clauses[0]`: the stages that
/// follow it, ending at the first barrier. `record` is the statement's
/// PROFILE site paired with the head clause's index (the consumed stages
/// report at `head clause + 1 + position`), `None` for an untimed run.
/// `None` when no clause after the
/// head streams. The caller has already decided the head streams to no
/// other consumer (no cap or fold) and is neither `OPTIONAL` nor a
/// `SEARCH`. `class_slot` is the part's one aggregation classification slot; it is
/// claimed only once every other check has passed.
pub(crate) fn open_match_chain<'a>(
    ctx: ExecCtx<'a>,
    clauses: &'a [Clause],
    class_slot: &'a OnceCell<ProjectionClass<'a>>,
    sink_class: &'a OnceCell<ProjectionClass<'a>>,
    scope: Option<&'a Row>,
    record: Option<(PlanMetricsSite<'a>, usize)>,
) -> Result<Option<MatchChain<'a>>> {
    let mut transforms = stage_run(ctx, &clauses[1..], scope)?;
    let stages = transforms.len();
    if stages == 0 {
        return Ok(None);
    }
    let ExecCtx { graph, params, .. } = ctx;
    // The run's consumer: the barrier after it. An aggregating clause is
    // fed directly (when its order slot is free); any other receives the
    // last stage's retained output.
    let after = clauses.get(1 + stages);
    let terminal = match open_match_fold(graph, after, class_slot, params, scope)? {
        Some(fold) => Next::Fold(Box::new(fold)),
        None => {
            // A last `MATCH` owns the barrier's sink projection. When that
            // barrier has a bounded form, its projected rows fold into the
            // sink instead of a collector; every other barrier keeps the
            // collector.
            let mut sink = None;
            let mut collector = Collector {
                shape: Collected::Bindings(None),
                rows: RefCell::new(Vec::new()),
                retained: RefCell::new(graph.retain_bytes(0)?),
            };
            if let Some(Transform::Match { projection, .. }) = transforms.last_mut() {
                *projection = after.and_then(|next| sinkable_projection(graph, next));
                if projection.is_some() {
                    collector.shape = Collected::SinkProjected;
                    sink = match after {
                        Some(next) => open_sink(ctx, next, sink_class, None)?,
                        None => None,
                    };
                } else {
                    collector.shape = Collected::Bindings(live_from(&clauses[1 + stages..]));
                }
            }
            match sink {
                Some(sink) => Next::Sink(Box::new(sink)),
                None => Next::Rows(collector),
            }
        }
    };
    // Built back to front: each stage owns the stage after it.
    let mut run = transforms.into_iter().rev();
    let last = run.next().expect("a non-empty run");
    let observing = record.is_some();
    let stage = |transform, next| Stage {
        ctx,
        transform,
        buffer: RefCell::new(Vec::new()),
        ran: Cell::new(false),
        next,
        rows_in: Cell::new(0),
        stats: observing.then(StageStats::new),
    };
    let head = run.fold(stage(last, terminal), |after, transform| {
        stage(transform, Next::Stage(Box::new(after)))
    });
    Ok(Some(MatchChain {
        head,
        stages,
        record,
    }))
}

impl<'a> Stage<'a> {
    /// Run the transform over the buffered rows and pass what it emits on.
    fn flush(&self, graph: &ExecGraph<'_>) -> Result<()> {
        self.observed(graph, || self.run_batch(graph))
    }

    /// Run `work` inside the stage's PROFILE window when the run observes
    /// itself, bare otherwise — the one place the sidecar's presence
    /// decides, so `flush` and `finish` state only what they wrap.
    fn observed<T>(&self, graph: &ExecGraph<'_>, work: impl FnOnce() -> Result<T>) -> Result<T> {
        match &self.stats {
            Some(stats) => stats.window(graph, work),
            None => work(),
        }
    }

    /// One transform run: the buffered batch through the transform, its
    /// output offered to the consumer. The PROFILE window — the batch
    /// flush, or the drain inside [`Stage::finish`] — is the caller's, so
    /// the stats' windows nest exactly once per run; the walk-start swap
    /// lives in the window (`profile_chain.rs`), and a `MATCH` transform
    /// captures its own first walk start while its ladder holds the slot.
    fn run_batch(&self, graph: &ExecGraph<'_>) -> Result<()> {
        let mut batch = std::mem::take(&mut *self.buffer.borrow_mut());
        graph.check_cancelled()?;
        self.ran.set(true);
        self.rows_in.set(self.rows_in.get() + batch.len() as u64);
        // The stage's consumer, wrapped to count at the emission boundary
        // when the run observes itself: a fold consumer takes rows as they
        // are offered, so the transform's returned vector holds none of
        // them and cannot be the count.
        let next_consumer = self.next.consumer();
        let counting;
        let consumer: &dyn RowFold = if let Some(stats) = &self.stats {
            counting = CountingFold::new(next_consumer, stats);
            &counting
        } else {
            next_consumer
        };
        let rows = match &self.transform {
            Transform::Match {
                patterns,
                where_clause,
                projection,
                live_in,
                tail,
            } => {
                if let Some(live) = live_in {
                    trim_rows(graph, &mut batch, live);
                }
                // A stage hands its walk the stage after it as the fold, so
                // the walk pushes row by row; the last stage's rows come
                // back (a sink projection) for the collector.
                let fold =
                    (!matches!(self.next, Next::Rows(_) | Next::Sink(_))).then_some(consumer);
                let LadderRun { rows, facts } = run_match_operator_ladder(
                    self.ctx,
                    batch,
                    MatchClauseInputs {
                        optional: false,
                        match_mode: MatchMode::DifferentRelationships,
                        patterns,
                        where_clause: *where_clause,
                    },
                    None,
                    projection.as_ref(),
                    tail,
                    fold,
                )?;
                if let Some(stats) = &self.stats {
                    stats.note_facts(facts);
                    stats.note_start(super::take_walk_start());
                }
                rows
            }
            Transform::With { spec, scope } => {
                let mut rows = project_rows(
                    self.ctx,
                    ProjectionInput::Feed(RowFeed::Held(RowStream::held(batch))),
                    spec,
                    false,
                    *scope,
                )?;
                // An importing subquery's scope bindings survive a `WITH`.
                if let Some(scope) = scope {
                    carry_scope_bindings(&mut rows, scope);
                }
                rows
            }
        };
        rows.into_iter()
            .try_for_each(|row| consumer.offer(graph, row))
    }

    /// Run the remaining buffered rows, then close the stages after this
    /// one. A stage that never ran still runs its transform once over no
    /// rows, as the held clause does. One PROFILE window covers the whole
    /// drain — this stage's last batch and every downstream stage's — so
    /// the stage's totals keep covering everything downstream did.
    fn finish(&self, graph: &ExecGraph<'_>) -> Result<()> {
        self.observed(graph, || self.drain(graph))
    }

    /// The drain itself: this stage's last batch, then the stages after
    /// it closing in turn.
    fn drain(&self, graph: &ExecGraph<'_>) -> Result<()> {
        if !self.buffer.borrow().is_empty() || !self.ran.get() {
            self.run_batch(graph)?;
        }
        match &self.next {
            Next::Stage(stage) => stage.finish(graph),
            Next::Fold(_) | Next::Sink(_) | Next::Rows(_) => Ok(()),
        }
    }
}

impl RowFold for Stage<'_> {
    fn offer(&self, graph: &ExecGraph<'_>, row: Row) -> Result<()> {
        let full = {
            let mut buffer = self.buffer.borrow_mut();
            buffer.push(row);
            buffer.len() >= CHAIN_BATCH
        };
        if full {
            self.flush(graph)?;
        }
        Ok(())
    }

    /// A rung's finished container headed to a stage is retained until it
    /// is offered; the pipeline bounds that figure as the held path did.
    fn offer_container(&self, graph: &ExecGraph<'_>, rows: Vec<Row>) -> Result<()> {
        charge_and_fold(self, graph, rows)
    }
}

impl<'a> MatchChain<'a> {
    /// Run what is still buffered through every stage and hand the run's
    /// [`Landing`] back with its PROFILE evidence beside it: the head
    /// clause's offered-row count (the head stage's input count, complete
    /// only once the drain has flushed its last batch, so it is read here
    /// and never before), the covered-clause count, what the terminal
    /// left, and — only when the run observes itself — the run's window
    /// totals for the arm's delta to subtract. The consumed stages'
    /// PROFILE records are written here too, before the terminal's rows
    /// leave the run — the terminal stage's record carries the bytes its
    /// collector retains.
    fn finish(self, graph: &ExecGraph<'_>) -> Result<(Option<StageDelta>, Landing<'a>)> {
        self.head.finish(graph)?;
        let offered = self.head.rows_in.get();
        // The run's window totals — the head stage's, which contain every
        // stage below it — for the arm's own delta to subtract, so the
        // head clause's record states its own clause's work and never the
        // run's. PROFILE's own evidence, produced only when the run
        // observes itself; the landing stays production-only.
        let observed = self.head.stats.as_ref().map(StageStats::totals);
        // The consumed stages' records are written before the terminal's
        // rows leave the run — the terminal stage's carries the bytes its
        // collector holds. The whole traversal is PROFILE's: an untimed
        // run reports nothing and walks once, straight to the terminal.
        self.report_profiled();
        let mut stage = self.head;
        let kind = loop {
            match stage.next {
                Next::Stage(next) => stage = *next,
                Next::Fold(fold) => break LandingKind::Folded(Box::new((*fold).into_parts().0)),
                Next::Sink(sink) => break sink.finish().kind,
                Next::Rows(collector) => {
                    let mut rows = collector.rows.into_inner();
                    Row::share_schemas(&mut rows);
                    break LandingKind::Rows {
                        rows,
                        source: collector.shape.source(),
                    };
                }
            }
        };
        Ok((
            observed,
            Landing {
                offered,
                consumed: self.stages,
                kind,
            },
        ))
    }

    /// Report each consumed stage's own PROFILE facts once the run has
    /// drained: one borrowed walk over the stage list, the terminal
    /// stage's retained bytes captured where the walk meets the collector,
    /// then one record per stage, tail-first, with the immediate
    /// downstream stage's totals subtracted out. Nothing at all runs
    /// without a site — an untimed run performs neither the traversal nor
    /// the byte sum.
    fn report_profiled(&self) {
        let Some((site, head_clause)) = self.record.as_ref() else {
            return;
        };
        let mut stages: Vec<&Stage> = Vec::with_capacity(self.stages);
        let mut stage = &self.head;
        let mut retained = 0;
        loop {
            stages.push(stage);
            match &stage.next {
                Next::Stage(next) => stage = next,
                Next::Rows(collector) => {
                    retained = rows_logical_bytes(&collector.rows.borrow());
                    break;
                }
                // A bounded sink's live container is the terminal's retained
                // state too — the same accounting policy as the collector.
                Next::Sink(sink) => {
                    retained = sink.retained_bytes();
                    break;
                }
                Next::Fold(_) => break,
            }
        }
        // Tail-first: each stage's own facts are its totals minus the
        // immediate downstream stage's totals — which contain every stage
        // below it, so summing the tail would count each deeper stage
        // twice — and each report hands its totals upstream as exactly
        // that subtrahend. The terminal stage's record carries the
        // retention its collector holds until the barrier consumes it;
        // every other stage's in-flight batch is bounded and reports
        // zero.
        let last = stages.len().saturating_sub(1);
        let mut downstream = StageDelta::default();
        for (idx, stage) in stages.iter().enumerate().rev() {
            let memory_bytes = if idx == last { retained } else { 0 };
            let stats = stage
                .stats
                .as_ref()
                .expect("an observing run carries every stage's sidecar");
            downstream = stats.report(site, head_clause + 1 + idx, downstream, memory_bytes);
        }
    }
}

impl RowFold for MatchChain<'_> {
    fn offer(&self, graph: &ExecGraph<'_>, row: Row) -> Result<()> {
        self.head.offer(graph, row)
    }

    fn offer_container(&self, graph: &ExecGraph<'_>, rows: Vec<Row>) -> Result<()> {
        self.head.offer_container(graph, rows)
    }
}

/// What takes a `MATCH`'s rows as they are emitted instead of the clause
/// collecting them: the aggregation of a following aggregating
/// `WITH`/`RETURN`, or the run of stages after it.
// The chain's PROFILE window totals (one `StageDelta`, now carrying the memo
// counters) put its size past the lint's variant-difference budget; a consumer
// is built once per clause and moves by value, so boxing it buys nothing.
#[allow(clippy::large_enum_variant)]
pub(crate) enum MatchConsumer<'a> {
    Fold(MatchFold<'a>),
    Chain(MatchChain<'a>),
    Sink(Sink<'a>),
}

/// What a consumer leaves for the clause after the clauses it covered:
/// the two facts every landing shares — the rows the head clause's walk
/// offered the consumer (its PROFILE row count, the same fact for a
/// directly fed fold and for a run of stages) and the clauses the run
/// covered — plus what the terminal left.
pub(crate) struct Landing<'a> {
    pub(crate) offered: u64,
    pub(crate) consumed: usize,
    pub(crate) kind: LandingKind<'a>,
}

/// The terminal's own leaving: the rows went into an aggregation, or the
/// last stage's retained output (the terminal stage's PROFILE record
/// reports the retention its collector held until the barrier consumed
/// it).
pub(crate) enum LandingKind<'a> {
    Folded(Box<Aggregation<'a>>),
    Rows { rows: Vec<Row>, source: RowSource },
}

/// The consumer for the uncapped, non-`OPTIONAL` `MATCH` at `clauses[0]`,
/// or `None` when its rows are collected. An aggregating clause right after
/// takes them directly; otherwise the stages after it do — under PROFILE
/// too. PROFILE observes the streamed run instead of holding the path —
/// through the head clause's offered-row count and each consumed stage's
/// own facts (`StageStats`) — because a held path under PROFILE would
/// materialize the clause's whole output, changing the memory shape of the
/// very statement PROFILE reports.
pub(crate) fn open_match_consumer<'a>(
    ctx: ExecCtx<'a>,
    clauses: &'a [Clause],
    class_slot: &'a OnceCell<ProjectionClass<'a>>,
    sink_class: &'a OnceCell<ProjectionClass<'a>>,
    scope: Option<&'a Row>,
    record: Option<(PlanMetricsSite<'a>, usize)>,
) -> Result<Option<MatchConsumer<'a>>> {
    let ExecCtx { graph, params, .. } = ctx;
    if let Some(fold) = open_match_fold(graph, clauses.get(1), class_slot, params, scope)? {
        return Ok(Some(MatchConsumer::Fold(fold)));
    }
    if let Some(chain) = open_match_chain(ctx, clauses, class_slot, sink_class, scope, record)? {
        return Ok(Some(MatchConsumer::Chain(chain)));
    }
    // No intermediate stage: a named `DISTINCT` or an `ORDER BY … LIMIT`
    // right after the head folds the head's rows into the bounded sink, the
    // sink projection evaluating the items per row.
    let Some(next) = clauses.get(1) else {
        return Ok(None);
    };
    let Some(projection) = sinkable_projection(graph, next) else {
        return Ok(None);
    };
    Ok(open_sink(ctx, next, sink_class, Some(projection))?.map(MatchConsumer::Sink))
}

impl<'a> MatchConsumer<'a> {
    /// The push contract the head clause's walk offers its rows to.
    pub(crate) fn as_fold(&self) -> &dyn RowFold {
        match self {
            MatchConsumer::Fold(fold) => fold,
            MatchConsumer::Chain(chain) => chain,
            MatchConsumer::Sink(sink) => sink.as_fold(),
        }
    }

    /// Close the consumer once the head clause has emitted everything:
    /// the landing, and — only when a run of stages observed itself — its
    /// window totals (a directly fed fold's offer-time grouping is the
    /// head clause's own work, the established fold semantics, so a fold
    /// lands no evidence).
    pub(crate) fn finish(self, graph: &ExecGraph<'_>) -> Result<(Option<StageDelta>, Landing<'a>)> {
        match self {
            MatchConsumer::Fold(fold) => {
                let (aggregation, offered) = fold.into_parts();
                Ok((
                    None,
                    Landing {
                        offered,
                        consumed: 0,
                        kind: LandingKind::Folded(Box::new(aggregation)),
                    },
                ))
            }
            MatchConsumer::Chain(chain) => chain.finish(graph),
            MatchConsumer::Sink(sink) => Ok((None, sink.finish())),
        }
    }
}