relate-planner 1.5.0

Bind OpenCypher AST to a logical plan
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
//! The bounded-work plan predicate (ADR-0504): which planned statements
//! are quick at this engine's scale and may run directly on the calling
//! thread of a connection whose caller asserted the query worker's own
//! stack budget (the facade's `Connection::assume_worker_stack_on_caller_thread`,
//! named without a link — the planner cannot see the facade crate).
//!
//! The predicate is a *runtime-boundedness* classification, not a
//! stack-safety one: the asserting caller has already guaranteed the
//! calling thread carries the worker's own stack
//! budget, so parse/lowering/planning/execution recursion is as safe
//! there as on the worker (ADR-0259's margin, same stack size). What the
//! worker still buys those statements is its *wait shape*: ADR-0312's
//! NOOP keep-alive ticks on the calling thread only while the statement
//! runs on the worker, so a statement that could outlive the keep-alive
//! interval must keep that shape. Every shape whose work is
//! data-proportional or has unbounded row potential escalates; the
//! residue — point and joined reads, catalog/admin commands, plain
//! projections — is bounded by scan size at this engine's scale (the
//! store's own size limit caps a linear pass) and cannot reach the
//! interval (ADR-0504's arithmetic).
//!
//! This rides the planner's canonical walks ([`plan_any_clause`], the
//! expression descent of [`crate::plan::walk_plan_exprs`]) and the IR's
//! own shape predicates ([`PathPattern::needs_gpm`],
//! [`Expr::is_scope_bearing`]) rather than a parallel classifier: the
//! shape of the planner's other classifiers
//! (`is_database_admin_command`, `query_type`).
//!
//! The same module is also the home of the cache-hit inline route's
//! containment rule (ADR-0663): [`hit_is_inline_safe`], the composer
//! over the depth bound and the auto-parameter fact that decides
//! whether a raw-string cache hit may run on a thread that never
//! asserted the worker's stack budget.

use relate_ir::{Clause, Expr, PathPattern, Query};

use crate::clause_kind::clause_mutates_store;
use crate::names::{projected_scope, stage_output_names};
use crate::plan::{plan_any_clause, plan_any_expr, LogicalPlan};

/// Whether `plan` is bounded-work — every clause a bounded shape, no
/// scope-bearing expression — the plan fact the ADR-0504 fast path routes
/// on: a bounded-work statement may run on the calling thread of a
/// connection that asserted the worker stack. The predicate errs toward
/// `false`: anything with data-proportional work (variable-length or
/// quantified paths, GPM patterns, vector search, **any write clause** —
/// an auto-commit write must never run inline, where a client that gave
/// up waiting cannot be kept alive by ADR-0312's NOOPs while its write
/// lands; and writes already pay WAL/commit latency that dwarfs the
/// handoff), unbounded row potential (`UNWIND`, `LOAD CSV`), the
/// **cartesian class** (multi-pattern MATCH whose patterns share no
/// variables — the one read shape whose row product is quadratic in the
/// store and breaks the keep-alive interval's arithmetic), nested
/// engines (`CALL {}`, `WHEN`/`ELSE`, `FOREACH`), or procedure calls
/// escalates to the query worker, whose wait keeps ADR-0312's NOOP
/// keep-alive ticking for the statements that can actually outlive it.
///
/// Conjunct order is load-bearing: the clause walk runs first and owns the
/// write refusal, so the cartesian walk ([`part_products`]) never sees a
/// mutating clause as anything but a no-op. The `writes_never_run_inline`
/// test pins that a write-bearing plan is refused whatever its pattern
/// shape.
pub fn plan_is_bounded_work(plan: &LogicalPlan) -> bool {
    !plan_any_clause(plan, &mut clause_escalates)
        && !plan_any_expr(plan, &mut Expr::is_scope_bearing)
        && !cartesian_join_graph(plan)
}

/// The maximum expression depth a shallow bounded plan may carry
/// ([`plan_is_shallow_bounded_work`]): deep enough for every realistic
/// projection or fold of the point-lookup family (a property read with
/// arithmetic, a CASE, a function call), shallow enough that evaluating
/// or packing any expression of the plan recurses a fixed handful of
/// frames — the depth-bound half of the cache-hit inline route's
/// stack-safety fact (ADR-0663). A deeply nested but bounded-work
/// statement (the parser's nesting cap is far higher) stays on the
/// query worker.
const MAX_SHALLOW_EXPRESSION_DEPTH: usize = 16;

/// Whether `expr`'s tree nests deeper than `bound` nodes along any one
/// path — measured iteratively, over [`Expr::each_child`] with an
/// explicit work stack: measuring nesting must never itself recurse
/// with it (ADR-0663), because the measurement runs on the calling
/// thread of a cache hit, whatever its stack budget. The flat child
/// walk is complete here: every scope-bearing variant already
/// escalated the plan ([`plan_is_bounded_work`]'s expression walk), so
/// no expression-subquery body can appear beneath a bounded plan.
fn deeper_than(expr: &Expr, bound: usize) -> bool {
    let mut stack = vec![(expr, 1)];
    while let Some((expr, depth)) = stack.pop() {
        if depth > bound {
            return true;
        }
        expr.each_child(&mut |child| stack.push((child, depth + 1)));
    }
    false
}

/// Whether every expression of the plan nests within
/// [`MAX_SHALLOW_EXPRESSION_DEPTH`] — the stack-safety half of the
/// cache-hit inline route's plan fact (ADR-0663), measured FIRST: the
/// walks the bounded-work classification rides recurse one frame per
/// expression level, so a plan within the bound is the only plan whose
/// walks run on an unasserted thread.
pub(crate) fn plan_is_shallow(plan: &LogicalPlan) -> bool {
    !plan_any_expr(plan, &mut |expr| {
        deeper_than(expr, MAX_SHALLOW_EXPRESSION_DEPTH)
    })
}

/// Whether a plan is bounded work ([`plan_is_bounded_work`]) whose every
/// expression also nests within [`MAX_SHALLOW_EXPRESSION_DEPTH`] — the
/// plan fact the cache-hit inline route (ADR-0663) routes on. Bounded
/// work alone is a *runtime*-boundedness classification, not a
/// stack-safety one; the depth bound adds the missing half for a thread
/// that never asserted the query worker's stack budget, and bounded
/// work's own rules already cap the plan at one fixed-length hop per
/// pipeline and exclude every nested engine and var-length shape. The
/// depth conjunct runs first (see [`plan_is_shallow`]).
pub(crate) fn plan_is_shallow_bounded_work(plan: &LogicalPlan) -> bool {
    plan_is_shallow(plan) && plan_is_bounded_work(plan)
}

/// Whether a cache entry's carried lowered AST — the replan's input
/// when the stats generation drifted (ADR-0503 stage 3) — is a single
/// stage whose every expression nests within
/// [`MAX_SHALLOW_EXPRESSION_DEPTH`]. The AST can nest deeper than the
/// plan compiled from it (shapes the planner collapses — nested
/// parentheses plan to their innermost expression), and a `NEXT` chain
/// escapes the parser's nesting cap entirely (loop-built): a replan
/// from such an AST is the same input-proportional recursion the query
/// worker's stack exists for, so the cache-hit inline route (ADR-0663)
/// declines it even when the compiled plan is shallow.
pub(crate) fn query_is_shallow(query: &Query) -> bool {
    // The structural pre-bound, measured iteratively: a `NEXT` chain is
    // built by a loop in the parser, not by recursion, so the parser's
    // nesting cap does not bound it — a cached statement can carry
    // thousands of stages within the 64 KiB cache cap, and the walk
    // that measures expressions recurses one frame per stage. The stage
    // count is bounded FIRST, with a loop; the expression trees that
    // remain are parser-recursion-built and capped at the nesting
    // limit, whose walk fits a small stack (pinned by the route's
    // suite).
    if query.next.is_some() {
        return false;
    }
    !crate::plan::query_any_expr(query, &mut |expr| {
        deeper_than(expr, MAX_SHALLOW_EXPRESSION_DEPTH)
    })
}

/// Whether any expression of the plan references an auto-extracted
/// placeholder parameter (ADR-0503's `__AUTO_*__` names). Public for its
/// two consumers, which must agree by construction (ADR-0663): the
/// containment rule's conjunct (a placeholder-referencing plan makes the
/// dispatch epilogue re-parse the statement's raw text to restore
/// authored column names — recursion with the statement's own nesting,
/// the query worker's stack, not an arbitrary caller's) and that very
/// epilogue, which consults this fact as its re-parse trigger so the
/// two sites can never diverge. Over-declining is the safe direction: a
/// placeholder in a non-projected position declines too.
pub fn plan_references_auto_parameter(plan: &LogicalPlan) -> bool {
    plan_any_expr(plan, &mut |expr| {
        matches!(expr, Expr::Parameter(name)
            if relate_parser::name_is_auto_parameter(name))
    })
}

/// The cache-hit inline route's containment rule (ADR-0663), in its one
/// home: whether a raw-string cache hit may run its statement on a
/// thread that never asserted the query worker's stack budget. Every
/// conjunct must hold —
///
/// - [`plan_is_shallow_bounded_work`]: the entry's compiled plan is
///   bounded work whose every expression nests within the depth bound —
///   execution then recurses only over depth-bounded trees;
/// - [`query_is_shallow`]: the entry's carried lowered AST nests within
///   the same bound — it is the replan's input on a stats-generation
///   mismatch, and it can nest deeper than the plan compiled from it
///   (the planner collapses shapes);
/// - [`!plan_references_auto_parameter`]: no expression references an
///   auto-extracted placeholder — the dispatch epilogue re-parses the
///   statement's raw text to restore authored column names for exactly
///   those plans, recursion with the statement's own nesting.
///
/// A hit that fails any conjunct is the probe's cold: the query worker
/// serves it on the stack the parser's nesting cap is tuned for.
pub fn hit_is_inline_safe(plan: &LogicalPlan, query: &Query) -> bool {
    plan_is_shallow_bounded_work(plan)
        && query_is_shallow(query)
        && !plan_references_auto_parameter(plan)
}

/// The clause shapes that escalate beyond the mutation table's answer
/// (the table's early return above owns every mutating clause, writes
/// and catalog DDL alike). Pattern-bearing clauses escalate on their
/// patterns ([`pattern_escalates`]); the cartesian class is assessed
/// per part at the plan level ([`cartesian_join_graph`]), not per
/// clause. The final arm lists every remaining variant so a new one
/// fails this match (no silent default) — for the mutating variants
/// among them it is unreachable (the table answered first); they are
/// listed for exhaustiveness, not as bounded work.
fn clause_escalates(clause: &Clause) -> bool {
    // Every mutating clause escalates (ADR-0504) — the native write arms
    // and the catalog passthrough's write commands (constraint and
    // graph-type DDL, `ALTER DATABASE`, the named index forms) alike,
    // one answer from the planner's published per-clause table, the same
    // answer the namespace gates and the write-attempt walk read. Two
    // reasons: an auto-commit write must never commit after its client
    // gave up waiting (inline work cannot tick ADR-0312's NOOPs), and a
    // write's WAL/commit latency dwarfs the handoff it thereby keeps.
    if clause_mutates_store(clause) {
        return true;
    }
    match clause {
        // Unbounded row potential: a range or list expression can carry
        // more elements than any keep-alive interval covers.
        Clause::Unwind { .. } => true,
        // Procedure bodies are arbitrary; their runtime is not a plan
        // fact.
        Clause::Call { .. } => true,
        // A nested engine runs per seed row (data-proportional passes).
        Clause::CallSubquery { .. } => true,
        // Branch bodies are nested engines of the same class.
        Clause::Conditional { .. } => true,
        // A vector search attached to the MATCH rides the index —
        // data-proportional work the compact walker does not bound.
        Clause::Match {
            patterns, search, ..
        } => search.is_some() || patterns.iter().any(pattern_escalates),
        // File-backed rows are unbounded by the plan: their count is a
        // property of the file, stated here (the documented contract's
        // own arm) rather than inherited from the mutation table.
        Clause::LoadCsv { .. } => true,
        // The bounded read residue the table left: projections and the
        // SHOW family.
        Clause::With { .. }
        | Clause::Return { .. }
        | Clause::ShowIndexes
        | Clause::Finish
        | Clause::Use { .. } => false,
        // Unreachable — the mutation table's early return above escalated
        // every one of these — listed as their own arm so the residue
        // above never re-answers the table's question, and so a new
        // `Clause` variant still fails this match.
        Clause::Foreach { .. }
        | Clause::Create { .. }
        | Clause::Merge { .. }
        | Clause::Delete { .. }
        | Clause::Set { .. }
        | Clause::Remove { .. }
        | Clause::CreateIndex { .. }
        | Clause::DropIndex { .. }
        | Clause::Catalog(_) => false,
    }
}

/// Whether the plan contains the cartesian class (ADR-0504): a MATCH
/// pattern that shares no variable with the rows flowing into it
/// multiplies against them — quadratic in the store, the one read shape
/// whose runtime can break the keep-alive interval's arithmetic. The
/// product is detected **where it materializes**, in the ordered clause
/// walk: each pattern is checked against the live scope the rows carry
/// at that point — the seed's names at a part's start, the earlier
/// patterns' bindings added as they run, a mid-part `WITH` projection
/// replacing the scope wholesale, a `NEXT` stage's seed being the
/// earlier stage's exported columns — so no later join can hide it: a
/// re-joining pattern after a product does not undo the product. A
/// `WHERE`-only join applies after the product, so only pattern-bound
/// variables connect. Nested bodies (subqueries, branches, `FOREACH`)
/// need no descent here: their clauses already escalate by arm above.
fn cartesian_join_graph(plan: &LogicalPlan) -> bool {
    // Whether any stage still follows the one being walked — only then is
    // the stage's seed needed; the final stage's output names are
    // discarded otherwise.
    let stages: Vec<&LogicalPlan> = plan.stages().collect();
    // The deepest pipeline hop count through the previous stage — a later
    // stage's patterns expand every row the earlier stages produced, so
    // the compounding depth carries forward (the maximum over the stage's
    // parts: a UNION arm's rows all flow into the next stage).
    let mut carried_hops: usize = 0;
    // The names that flowed in from the previous stage — its projected
    // output columns, not every pattern variable it bound (a projection
    // drops the names it does not export, so a later stage re-binding a
    // dropped name is a fresh binding). Empty for the first stage:
    // nothing flows in yet.
    let mut seed_names: Vec<String> = Vec::new();
    for (index, stage) in stages.iter().enumerate() {
        let seed_variables: Vec<&str> = seed_names.iter().map(String::as_str).collect();
        let mut stage_hops: usize = 0;
        for part in &stage.parts {
            let (product, hops) = part_products(part, &seed_variables, carried_hops);
            if product || hops >= MULTI_HOP_ESCALATION_HOPS {
                return true;
            }
            stage_hops = stage_hops.max(hops);
        }
        carried_hops = stage_hops;
        // This stage's projection is the next stage's seed: the exported
        // column names. Computed only when a next stage exists — the
        // final stage's names are never read. A planned statement's
        // per-stage names always derive (the UNION column-agreement check
        // ran in `plan`), the same expect policy
        // `plan_output_names_expect` states.
        if index + 1 < stages.len() {
            seed_names = stage_output_names(
                stage.parts.iter().map(Vec::as_slice),
                std::mem::take(&mut seed_names),
                &std::collections::HashSet::new(),
            )
            .expect("a planned statement's stage columns agree");
        }
    }
    false
}

/// One part's clause walk, answering both cartesian questions at once:
/// whether a product materialized (a MATCH pattern sharing no live name
/// with the rows flowing into it at that point), and the pipeline's
/// compounding hop count — the carried hops plus every fixed-length hop
/// the part's patterns walk (var-length and quantified hops escalated by
/// arm before this walk, so `rels.len()` is pure fixed length). `UNWIND`
/// never reaches this walk (it escalates by arm above); `RETURN` is
/// terminal; every other non-escalating clause leaves the live scope
/// alone.
fn part_products(part: &[Clause], seed_variables: &[&str], carried_hops: usize) -> (bool, usize) {
    use std::collections::HashSet;
    // The names the rows flowing into the next clause carry, and whether
    // rows flow at all: a leading anonymous pattern (`MATCH ()`) binds
    // no name but still produces rows, so the pattern after it multiplies
    // against them — the live-name set alone cannot express that.
    let mut live: HashSet<String> = seed_variables.iter().map(|n| n.to_string()).collect();
    let mut rows_flow = !seed_variables.is_empty();
    let mut hops = carried_hops;
    for clause in part {
        match clause {
            Clause::Match { patterns, .. } => {
                for pattern in patterns {
                    let variables = relate_ir::pattern_anchor_variables(pattern);
                    // The product materializes HERE: a pattern that
                    // shares no live name multiplies against the flowing
                    // rows, and nothing later undoes it.
                    if rows_flow && !variables.iter().any(|v| v.with_str(|t| live.contains(t))) {
                        return (true, hops);
                    }
                    rows_flow = true;
                    for name in variables {
                        live.insert(name.to_string());
                    }
                    // The pattern's fixed-length hops compound along the
                    // pipeline — each flowing row expands degree^hops
                    // paths — wherever the hops are spelled: one pattern,
                    // comma patterns, or consecutive MATCH clauses.
                    hops += pattern.rels.len();
                }
            }
            // A mid-part projection replaces the live scope: the names it
            // exports are the rows the next clauses see; a name it drops
            // is dead, and a later MATCH re-binding it is a fresh
            // binding — a product, exactly like a `NEXT` stage's seed. A
            // `*` item keeps the incoming names.
            Clause::With { items, .. } => {
                let incoming: Vec<String> = live.iter().cloned().collect();
                live = projected_scope(items, incoming).into_iter().collect();
                rows_flow = true;
            }
            // The inert/terminal residue: `RETURN` is the part's last
            // clause, the SHOW family reads the catalog, `USE` acts as a
            // no-op, and the mutating variants never reach this walk (the
            // table escalated them above) — listed so a new `Clause`
            // variant is a compile error here, exactly as in
            // `clause_escalates`, rather than silently scope-neutral.
            Clause::Return { .. }
            | Clause::ShowIndexes
            | Clause::Finish
            | Clause::Use { .. }
            | Clause::Catalog(_)
            | Clause::Foreach { .. }
            | Clause::LoadCsv { .. }
            | Clause::Create { .. }
            | Clause::Merge { .. }
            | Clause::Delete { .. }
            | Clause::Set { .. }
            | Clause::Remove { .. }
            | Clause::CreateIndex { .. }
            | Clause::DropIndex { .. }
            | Clause::Unwind { .. }
            | Clause::Call { .. }
            | Clause::CallSubquery { .. }
            | Clause::Conditional { .. } => {}
        }
    }
    (false, hops)
}

/// The pipeline hop count at which a statement escalates: two or more
/// fixed-length relationship hops along one pipeline compound
/// multiplicatively on a dense graph (each flowing row expands
/// degree^hops paths), the same data-proportional class as a var-length
/// traversal — while zero and one hops are linear in the store's nodes
/// and edges, the linear-scan class the residue already allows. Counted
/// along the pipeline (one pattern, comma patterns, consecutive MATCH
/// clauses, `NEXT` stages alike), in `part_products` — the one home of
/// the policy.
const MULTI_HOP_ESCALATION_HOPS: usize = 2;

/// The pattern shapes that escalate: GPM matching (the general path
/// matcher's DFS is data-proportional) and any variable-length or
/// quantified relationship in the compact `rels` view (a recursive
/// traversal whose work grows with the graph, `DEFAULT_MAX_HOPS` per
/// candidate).
fn pattern_escalates(pattern: &PathPattern) -> bool {
    pattern.needs_gpm()
        || pattern
            .rels
            .iter()
            .any(|rel| rel.var_length || rel.quantified)
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Inline classification over a planned statement, the shape of the
    /// `query_type` tests' `type_of`.
    fn bounded_work(cypher: &str) -> bool {
        plan_is_bounded_work(&crate::plan_of(cypher))
    }

    #[test]
    fn writes_never_run_inline() {
        // Disjoint-free single patterns (no product) beside a write: the
        // refusal comes from the clause walk, not the cartesian walk.
        for cypher in [
            "MATCH (a:User {id: $i}) CREATE (a)-[:T]->(:N)",
            "MATCH (a:User {id: $i}) SET a.x = 1",
            "MATCH (a:User {id: $i}) DELETE a",
            "MERGE (a:User {id: $i})",
        ] {
            assert!(!bounded_work(cypher), "{cypher}");
        }
    }

    #[test]
    fn bounded_reads_run_inline() {
        // The LABS-791 workload: point reads, joined reads, plain
        // projections, read-only catalog commands.
        for cypher in [
            "MATCH (n:User) WHERE n.id = $i RETURN n.id",
            "MATCH (n:User {id: $i}) RETURN n.id",
            "RETURN $i AS i",
            "RETURN 1 AS n",
            "MATCH (n:User) RETURN n LIMIT 10",
            "SHOW DATABASES",
            "SHOW INDEXES",
            "USE neo4j MATCH (n:User) RETURN n",
            "MATCH (n:User) WHERE n.id = $i WITH n RETURN n.id",
            "RETURN 1 AS x UNION RETURN 2 AS x",
            "MATCH (n:User) WHERE n.id = $i RETURN n.id LIMIT 1",
            // A single pattern with an anonymous endpoint is one
            // traversal — no product, inline.
            "MATCH (a:User {id: $i})-[:T]->() RETURN a",
            // Consecutive MATCHes that share a variable join; disjoint
            // ones product (the escalate list above).
            "MATCH (a:User {id: $i}) MATCH (a)-[:T]->(b) RETURN b",
            // A mid-part WITH that exports the shared name keeps the join
            // alive across the projection.
            "MATCH (a:User {id: $i}) WITH a MATCH (a)-[:T]->(b) RETURN b",
            // UNION arms are separate pipelines: their patterns never
            // join each other, so two single-pattern arms stay inline.
            "MATCH (a:User {id: $i}) RETURN a AS n UNION MATCH (b:City {id: $i}) RETURN b AS n",
            // A NEXT stage's MATCH connecting to an exported name (the
            // projection's column, not the pattern variable it aliased
            // away) joins the seed rows.
            "MATCH (a:User {id: $i}) RETURN a NEXT MATCH (a)-[:T]->(b) RETURN b AS m",
        ] {
            assert!(bounded_work(cypher), "must be bounded work: {cypher}");
        }
    }

    #[test]
    fn shallow_bounded_work_tracks_the_depth_bound() {
        // The point-lookup family: shallow and bounded.
        for cypher in [
            "MATCH (n:User {id: $i}) RETURN n.id",
            "MATCH (n:User {id: $i}) RETURN n.id + 1 AS next",
            "MATCH (a:User {id: $i})-[:T]->(b) RETURN count(b.id)",
            "RETURN 1 AS n",
        ] {
            let plan = crate::plan_of(cypher);
            assert!(
                plan_is_shallow_bounded_work(&plan),
                "must be shallow bounded work: {cypher}"
            );
        }
        // A bounded-work statement whose expression nests past the bound
        // escalates: the depth bound is the stack-safety half, and the
        // parser's own nesting cap (500) is far above it. Nested
        // parameter arithmetic — the one spelling that neither collapses
        // at lowering (parentheses do) nor constant-folds (literals do).
        let deep = format!("RETURN {}$x{} AS n", "$x+(".repeat(20), ")".repeat(20));
        let plan = crate::plan_of(&deep);
        assert!(
            plan_is_bounded_work(&plan),
            "the fixture stays bounded work: {deep}"
        );
        assert!(
            !plan_is_shallow_bounded_work(&plan),
            "a deep expression must not run inline on an unasserted thread: {deep}"
        );
        // Everything bounded work refuses is refused here too — the
        // var-length and multi-hop classes, each one representative.
        for cypher in [
            "MATCH (n)-[r:T*1..3]->(m) RETURN n",
            "MATCH (a:User {id: $i})-[:T]->(b)-[:U]->(c) RETURN b",
            "UNWIND range(1, 10) AS x RETURN x",
        ] {
            let plan = crate::plan_of(cypher);
            assert!(
                !plan_is_shallow_bounded_work(&plan),
                "must escalate: {cypher}"
            );
        }
    }

    /// The containment rule's own pins (ADR-0663): the auto-parameter
    /// conjunct declines a placeholder-referencing plan that is
    /// otherwise shallow bounded work, and the explicit-parameter family
    /// passes the whole rule.
    #[test]
    fn the_containment_rule_composes_its_conjuncts() {
        // A plan whose expression is a placeholder parameter (the
        // rewritten form a cached entry carries) fails the rule even
        // though the plan itself is shallow bounded work — the
        // spelled-out placeholder stands in for what an auto-extracted
        // literal compiles to.
        let placeholder = crate::plan_of("MATCH (n:User {id: $__AUTO_INT_0__}) RETURN n.id");
        assert!(
            plan_is_shallow_bounded_work(&placeholder),
            "the fixture is shallow bounded work"
        );
        assert!(
            plan_references_auto_parameter(&placeholder),
            "the placeholder reference is the conjunct's fact"
        );
        assert!(
            !hit_is_inline_safe(
                &placeholder,
                &crate::query_of("MATCH (n:User {id: 1}) RETURN n.id")
            ),
            "the auto-parameter conjunct declines the hit"
        );
        // And the explicit-parameter family passes the whole rule.
        let plan = crate::plan_of("MATCH (n:User {id: $i}) RETURN n.id");
        assert!(
            hit_is_inline_safe(
                &plan,
                &crate::query_of("MATCH (n:User {id: $i}) RETURN n.id")
            ),
            "the point-lookup family is inline-safe"
        );
        // A NEXT-chained AST declines structurally: the chain is
        // loop-built in the parser, outside the nesting cap, and the
        // depth-measuring walk recurses one frame per stage.
        let chained = crate::query_of(
            "MATCH (a:User {id: $i}) RETURN a AS n NEXT MATCH (b:City) RETURN b AS m",
        );
        assert!(
            !query_is_shallow(&chained),
            "a NEXT chain declines structurally"
        );
    }

    #[test]
    fn data_proportional_and_unbounded_shapes_escalate() {
        for cypher in [
            // Unbounded row potential.
            "UNWIND range(1, 10) AS x RETURN x",
            "UNWIND [1, 2, 3] AS x CREATE (:N)",
            // Every write clause escalates: an auto-commit write must
            // never commit after its client gave up waiting, and its
            // WAL/commit latency dwarfs the handoff anyway. Index DDL
            // included — a build walks the whole label.
            "CREATE (:User {id: $i})",
            "MERGE (n:User {id: $i}) ON CREATE SET n.fresh = true",
            "MATCH (n:User) WHERE n.id = $i SET n.seen = true",
            "MATCH (n:User) WHERE n.id = $i DELETE n",
            "MATCH (n:User) WHERE n.id = $i REMOVE n.seen",
            "CREATE RANGE INDEX user_id FOR (u:User) ON (u.id)",
            "DROP INDEX user_id",
            // Catalog write/DDL commands (the passthrough the SHOW
            // family shares): the per-clause table's answer.
            "ALTER DATABASE neo4j SET DEFAULT LANGUAGE CYPHER 5",
            "CREATE GRAPH TYPE gt AS { (n:Person => {name :: STRING}) }",
            "CREATE INDEX i FOR (n:User) ON (n.id)",
            // The cartesian class: disjoint patterns multiply (quadratic
            // in the store — the read shape that breaks the interval),
            // within one MATCH and across consecutive ones alike.
            "MATCH (a:User), (b:City) RETURN a, b",
            "MATCH (a:User), (b:User) RETURN count(a)",
            "MATCH (a:User), (b:User) WHERE a.id = $i AND b.id = $i RETURN a",
            "MATCH (a)-[:T]->(b), (c)-[:U]->(d) RETURN a",
            "MATCH (a:User) MATCH (b:City) RETURN count(a)",
            // ... and inside a single UNION arm: that arm's graph is
            // disconnected even though the other arm joins nothing.
            "MATCH (a:User), (b:City) RETURN a AS n UNION MATCH (c) RETURN c AS n",
            // A NEXT stage is seeded with the earlier stage's exported
            // rows: a fresh MATCH sharing no exported name products
            // against the seed.
            "MATCH (a:User {id: $i}) RETURN a AS n NEXT MATCH (b:City) RETURN b AS m",
            // An aliased-away name does not flow: stage 2's `a` is a
            // fresh binding, so the product is real even though stage 1
            // bound `a`.
            "MATCH (a:User {id: $i}) RETURN a AS n NEXT MATCH (a)-[:T]->(b) RETURN b AS m",
            // ... and the same inside one part: a WITH that drops the
            // shared name leaves the later MATCH a fresh binding, so the
            // rows product (a projection drops and renames, never joins).
            "MATCH (a:User {id: $i}) WITH 1 AS x MATCH (b:City) RETURN b",
            "MATCH (a:User {id: $i}) WITH a AS x MATCH (b:City) RETURN b",
            // ... and a cartesian inside the NEXT stage, connected to
            // the seed or not, still products internally.
            "MATCH (a:User {id: $i}) RETURN a AS n NEXT MATCH (a)-[:T]->(b), (c:City) RETURN b AS m",
            // A product followed by a re-joining pattern: the product
            // materialized at the second MATCH, and the third's join
            // cannot undo it — detected where it happens, not by a
            // final component count.
            "MATCH (a) MATCH (b) MATCH (a)-[:T]->(b) RETURN b",
            // Fixed-length multi-hop compounds multiplicatively
            // (degree^hops per row) — the same data-proportional class
            // as var-length, connected or not, wherever the hops are
            // spelled: one pattern, comma patterns, consecutive MATCH
            // clauses, or a NEXT stage.
            "MATCH (a:User {id: $i})-[:T]->(b)-[:U]->(c) RETURN b",
            "MATCH (a)-[:T]->(b)-[:T]->(c) RETURN a",
            "MATCH (a)-[:T]->(b), (b)-[:T]->(c) RETURN c",
            "MATCH (a)-[:T]->(b) MATCH (b)-[:T]->(c) RETURN c",
            "MATCH (a:User {id: $i})-[:T]->(b) RETURN b NEXT MATCH (b)-[:T]->(c) RETURN c",
            // A leading anonymous pattern binds no name but still
            // produces rows: the pattern after it multiplies against
            // them — the N² shape, whether in one clause or across two.
            "MATCH (), (n) RETURN n",
            "MATCH () MATCH (n) RETURN n",
            // Variable-length and quantified paths.
            "MATCH (n)-[r:T*1..3]->(m) RETURN n",
            "MATCH (n)-[r:T*]->(m) RETURN n",
            "MATCH (n)-[r:T]->{1,3}(m) RETURN n",
            "MATCH (a)-->(b)((x)-[e]->(y)){1,3}(c) RETURN a",
            // GPM shapes: path mode, selector, inline element predicates.
            "MATCH ANY SHORTEST (n)-[r:T]->(m) RETURN n",
            "MATCH (n WHERE n.id = $i)-[r:T]->(m) RETURN n",
            "MATCH p = (n)-[r:T WHERE r.w > 1]->(m) RETURN p",
            // Nested engines.
            "CALL { MATCH (n) RETURN n }",
            "FOREACH (x IN [1, 2] | CREATE (:N))",
            "WHEN true THEN { RETURN 1 AS x } ELSE { RETURN 2 AS x }",
            // Procedures.
            "CALL db.labels()",
            // File-backed rows — both the write-fed and the bare-read
            // form: the count is a property of the file.
            "LOAD CSV FROM 'file:///f.csv' AS row CREATE (n)",
            "LOAD CSV FROM 'file:///f.csv' AS row RETURN row",
            // Scope-bearing expressions in otherwise-bounded clauses.
            "MATCH (n:User) WHERE EXISTS { (n)-[:T]->(m) } RETURN n",
            "MATCH (n:User) WHERE count { (n)-[:T]->() } > 1 RETURN n",
            "MATCH (n:User) RETURN [(n)-[:T]->(m) | m.name] AS names",
            "MATCH (a:User), (b:User) RETURN shortestPath((a)-[:T*]->(b))",
        ] {
            assert!(!bounded_work(cypher), "must escalate: {cypher}");
        }
    }

    #[test]
    fn escalation_covers_every_nesting_position() {
        // The expression walk's coverage: an escalating shape nested
        // where a part-granular (clause-only) walk would miss it — inside
        // a bounded clause's expressions — still escalates, because the
        // nested shape is the ONLY trigger in each fixture.
        //
        // Bodies whose own clause arms escalate unconditionally
        // (`WHEN`/`ELSE`, `CALL {}`, `FOREACH`) cannot be probed this
        // way: their wrapper escalates before any body shape is
        // consulted, so a fixture there would prove nothing about the
        // descent — escalation is already correct for them by the
        // wrapper's arm.
        for cypher in [
            // A scope-bearing expression inside a WITH projection.
            "MATCH (n:User) WITH n, EXISTS { (n)-[:T]->(m) } AS live RETURN live",
            // A scope-bearing expression inside a MATCH's WHERE — the
            // clause itself stays bounded (a single plain node pattern).
            "MATCH (n:User) WHERE EXISTS { (n)-[:T]->(m) } RETURN n",
            // A pattern comprehension inside a RETURN projection.
            "MATCH (n:User) RETURN [(n)-[:T]->(m) | m.name] AS names",
        ] {
            assert!(
                !bounded_work(cypher),
                "must escalate from its nest: {cypher}"
            );
        }
    }
}