relate-planner 1.0.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
//! ADR-0116 namespace classifiers: the §2 database-administration
//! classifiers (the admin-command classifier and the `SHOW DATABASES` +
//! `NEXT` rule), the §4 `system` gate and the statement read/write
//! classification they build on. This module is the one home for the
//! namespace rules — the sibling §3 `USE` rules live in
//! [`crate::use_target`]; the facade (`relate::connection`) calls here
//! and keeps only `CALL`-shape validation. The `system_ontologies` gate is
//! ADR-0229's — every non-administration statement routed there fails with
//! one fixed text, so it needs no classifier here (the facade raises it in
//! `Connection::namespace_gate_error`).
//!
//! The procedure-aware check ([`plan_calls_write_procedure`]) takes the
//! write-procedure predicate as an argument: the canonical procedure
//! signature registry lives in the executor, which depends on this crate
//! (never the reverse).

use relate_ir::Clause;

use crate::clause_kind::ir_clause_keyword;
use crate::LogicalPlan;

/// The write classifier is a projection of the one shared clause fold
/// ([`crate::access::plan_access`]): each part's TOP-LEVEL clauses (plus
/// `NEXT` stages and `WHEN`/`ELSE` branch bodies) only — it deliberately
/// does NOT descend into `CALL { … }` bodies or expression subqueries,
/// because the fold already classifies a top-level `CALL { … }` as a
/// write outright and a read-only `CALL { … }` nested in an
/// `EXISTS`/`COUNT`/`COLLECT` expression must not reclassify the statement
/// as a write. Schema (index/constraint DDL) counts as a write here; see
/// [`crate::access::Access::is_write`].
pub fn is_write(plan: &LogicalPlan) -> bool {
    crate::access::plan_access(plan, &mut |_| false).is_write()
}

/// Whether any `CALL` of a write procedure appears anywhere in the plan.
/// [`crate::walk_plan_clauses`] descends into `CALL { … }` subqueries,
/// `WHEN`/`ELSE` branches, `FOREACH` bodies, expression subqueries and
/// `NEXT` stages, so a write procedure nested in any of those positions
/// still counts — the query-type classification reads it
/// ([`crate::query_type`]).
pub fn plan_calls_write_procedure(
    plan: &LogicalPlan,
    is_write_procedure: &mut impl FnMut(&str) -> bool,
) -> bool {
    let mut calls_write = false;
    crate::walk_plan_clauses(plan, &mut |clause| {
        if let Clause::Call { name, .. } = clause {
            calls_write |= name.with_str(|text| is_write_procedure(text));
        }
    });
    calls_write
}

/// ADR-0116 §2: the database-administration `Catalog` vocabulary, matched
/// once. [`is_database_admin_command`] asks whether a clause is one of the
/// two commands; the `SHOW DATABASES` + `NEXT` rule
/// ([`is_show_databases_with_next`]) asks for `ShowDatabases` specifically.
#[derive(Clone, Copy, PartialEq, Eq)]
enum DatabaseAdminCommand {
    ShowDatabases,
    AlterDatabase,
}

/// ADR-0116 §2: classify a parser `Catalog` command as one of the
/// database-administration commands, or `None` for every other `Catalog`
/// clause. Matched once here, lowered once per clause by
/// [`admin_command_clause`] — every §2 classifier reads one of the two.
fn database_admin_command(command: &relate_parser::Clause) -> Option<DatabaseAdminCommand> {
    use relate_parser::Clause as C;
    match command {
        C::ShowDatabases(_) => Some(DatabaseAdminCommand::ShowDatabases),
        C::AlterDatabase(_) => Some(DatabaseAdminCommand::AlterDatabase),
        _ => None,
    }
}

/// ADR-0116 §2: the §2 command a top-level IR clause is, or `None` for
/// every other clause — the one clause-level lowering every §2 classifier
/// reads: [`is_database_admin_command`]'s all-quantifier,
/// [`is_show_databases_with_next`]'s `SHOW DATABASES` refinement,
/// [`admin_command_heads_statement`]'s head probe and the §4 composability
/// walk's containment test all go through it, so the vocabulary is matched
/// once.
fn admin_command_clause(clause: &Clause) -> Option<DatabaseAdminCommand> {
    match clause {
        Clause::Catalog(command) => database_admin_command(command.as_ref()),
        _ => None,
    }
}

/// ADR-0116 §2: database-administration commands run from any namespace and
/// execute against the store's catalog (the `neo4j` graph). A plan qualifies
/// when every clause is a database-administration `Catalog` command.
pub fn is_database_admin_command(plan: &LogicalPlan) -> bool {
    !plan.parts.is_empty()
        && plan.next.is_none()
        && plan
            .parts
            .iter()
            .flatten()
            .all(|clause| admin_command_clause(clause).is_some())
}

/// ADR-0116 §2 / CIP-213 NE1.4: whether the plan combines `SHOW DATABASES`
/// with a `NEXT` stage. The front-end's NEXT-chain validation rejects the
/// combination before planning; this classifier is the execution-phase
/// backstop the facade checks after its EXPLAIN early return (EXPLAIN plans
/// without executing, so the rule does not apply there). Part-granular over
/// every stage's TOP-LEVEL clauses, matching the front-end's per-body scan:
/// a `SHOW DATABASES` nested in an expression subquery does not participate
/// in a `NEXT` chain.
pub fn is_show_databases_with_next(plan: &LogicalPlan) -> bool {
    plan.next.is_some()
        && plan.parts().any(|part| {
            part.iter().any(|clause| {
                admin_command_clause(clause) == Some(DatabaseAdminCommand::ShowDatabases)
            })
        })
}

/// ADR-0116 §2, amended 2026-11-04: whether the statement's body is
/// *headed* by a database-administration command — the first top-level
/// clause after any leading `USE` clauses is one of the §2 commands. This
/// is the head shape CE's front end guards with the 42N79 auto-routing
/// rejection; an administration command later in the body (after e.g. a
/// `MATCH`) is a plain catalog read, not an administration-command
/// statement — live CE 2026.09 runs `USE neo4j MATCH (n) SHOW DATABASES
/// YIELD name RETURN name`, while every headed spelling under a
/// non-system `USE` (bare, `YIELD … RETURN …`, `UNION` arm, `ALTER
/// DATABASE`) is 42N79. Part-granular on the head part only: a command
/// headed in a `NEXT` stage is already refused by the NEXT-chain rule
/// ([`is_show_databases_with_next`]), and `USE` clauses never split parts.
pub fn admin_command_heads_statement(plan: &LogicalPlan) -> bool {
    plan.parts.first().is_some_and(|head| {
        head.iter()
            .find(|clause| !matches!(clause, Clause::Use { .. }))
            .and_then(admin_command_clause)
            .is_some()
    })
}

/// ADR-0116 §4: on a `system` session only SHOW commands, supported
/// administration commands and standalone procedure calls
/// (`CALL … [YIELD …] [RETURN …]`) run. Returns the offending clause kinds
/// (deduplicated, in order of appearance) for CE's 42N17 text, or `None`
/// when the statement is allowed.
pub fn unsupported_system_clause_names(plan: &LogicalPlan) -> Option<String> {
    let mut offending: Vec<&'static str> = Vec::new();
    // Part-granular coverage is deliberate: only each part's TOP-LEVEL
    // clauses (plus `NEXT` stages, via `walk_plan_parts`) are judged — a
    // read MATCH nested in an expression subquery attached to a standalone
    // CALL is allowed on `system`. `walk_plan_clauses` would descend into
    // those nested positions and is the wrong walk here.
    //
    // Deliberate contrast with the facade's `validate_calls`, which walks
    // the FULL clause tree (`walk_plan_clauses`) so a `CALL` nested in a
    // `CALL { … }` subquery is still validated against the procedure
    // registry: 42N17 judges the statement's top-level shape, while CALL
    // validation must cover every call site. Do not "unify" the two walks.
    crate::walk_plan_parts(plan, &mut |clauses| {
        collect_system_offenders(clauses, &mut offending);
    });
    if offending.is_empty() {
        None
    } else {
        Some(offending.join(", "))
    }
}

/// ADR-0116 §4: the tail-clause keywords CE suppresses from the 42N17
/// offender list when a non-tail offender is present. Encoded ONCE — both
/// the offender collection (`is_tail` below) and the suppression
/// (`partition` below) read this list, so the two can never drift apart.
const SYSTEM_TAIL_KEYWORDS: &[&str] = &["RETURN", "WITH", "FINISH"];

fn collect_system_offenders(clauses: &[Clause], out: &mut Vec<&'static str>) {
    // A RETURN/WITH/FINISH is only allowed as the tail of a SHOW or a
    // standalone procedure call in the same query part. The anchor set is
    // encoded ONCE: both `has_show_or_call` and `allowed` read
    // `is_system_head`, so the two can never drift apart. `Use` is
    // deliberately absent — it is allowed on `system` but does not anchor
    // a tail.
    let is_system_head = |clause: &Clause| match clause {
        Clause::Catalog(command) => catalog_allowed_on_system(command.as_ref()),
        Clause::Call { .. } | Clause::ShowIndexes => true,
        _ => false,
    };
    let has_show_or_call = clauses.iter().any(&is_system_head);
    let is_tail = |clause: &Clause| SYSTEM_TAIL_KEYWORDS.contains(&ir_clause_keyword(clause));
    let allowed = |clause: &Clause| {
        is_system_head(clause)
            || matches!(clause, Clause::Use { .. })
            || (is_tail(clause) && has_show_or_call)
    };
    // CE lists only the clause kinds outside its supported
    // CALL/YIELD/RETURN structure: `MATCH (n) RETURN count(n)` reports
    // `MATCH`, not `MATCH, RETURN` (testdata/ce-errors/neo4j-ce.json
    // `system_data_clause`). A lone tail offender (`RETURN 1`) still
    // reports itself.
    let (non_tail, tail): (Vec<&'static str>, Vec<&'static str>) = clauses
        .iter()
        .filter(|clause| !allowed(clause))
        .map(ir_clause_keyword)
        .partition(|name| !SYSTEM_TAIL_KEYWORDS.contains(name));
    let part_offenders = if non_tail.is_empty() { tail } else { non_tail };
    for name in part_offenders {
        if !out.contains(&name) {
            out.push(name);
        }
    }
}

/// SHOW commands and database-administration commands (ADR-0116 §2) are the
/// only `Catalog` clauses allowed on `system`.
fn catalog_allowed_on_system(command: &relate_parser::Clause) -> bool {
    use relate_parser::Clause as C;
    matches!(
        command,
        C::ShowFunctions(_)
            | C::ShowConstraints(_)
            | C::ShowIndexes(_)
            | C::ShowDatabases(_)
            | C::ShowRoles(_)
            | C::ShowServers(_)
            | C::ShowUsers(_)
            | C::ShowProcedures(_)
            | C::ShowSettings(_)
            | C::ShowTransactions(_)
            | C::ShowGraphType(_)
            | C::ShowCurrentGraphType(_)
            | C::AlterDatabase(_)
    )
}

/// ADR-0116 §4, completed 2026-11-04: the clause kind CE forbids in a
/// query part that composes a database-administration command — `WITH`,
/// the one tail clause the §4 whitelist (`collect_system_offenders`)
/// still allowed after an administration command's `YIELD`. Live CE
/// 2026.09 rejects the composed command with
/// `The following clauses are not allowed on a system database: WITH.`
/// for both the `… YIELD … WITH …` and the `WITH … <command> …` spelling
/// (position within the part is not part of the rule); the other §4
/// offenders keep CE's 42N17 text. Scope is the §2 administration
/// commands (the `admin_command_clause` vocabulary): composing the
/// other catalog SHOW commands (`SHOW SETTINGS`, `SHOW PROCEDURES`, …)
/// with `WITH` stays allowed — a recorded divergence, not this rule's
/// business. Returns the offending clause's keyword, or `None` when no
/// part composes an administration command with a `WITH`.
///
/// The engine's 42N17 text still lists an `UNWIND`/`MATCH` tail after an
/// administration command's `YIELD` (the whitelist above rejects it);
/// live CE words those with this family's text instead — same outcome,
/// different message, recorded in ADR-0116 §4's Later references.
pub fn composed_admin_disallowed_clause(plan: &LogicalPlan) -> Option<&'static str> {
    let mut offending = None;
    crate::walk_plan_parts(plan, &mut |clauses| {
        let composes_admin_command = clauses
            .iter()
            .any(|clause| admin_command_clause(clause).is_some());
        if !composes_admin_command {
            return;
        }
        for clause in clauses {
            if offending.is_none() && matches!(clause, Clause::With { .. }) {
                offending = Some(ir_clause_keyword(clause));
            }
        }
    });
    offending
}

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

    #[test]
    fn writes_classify_as_writes() {
        for cypher in [
            "CREATE (n)",
            "MERGE (n:N {id: 1})",
            "MATCH (n) SET n.x = 1",
            "MATCH (n) DELETE n",
            "MATCH (n) REMOVE n.x",
            "CREATE INDEX i FOR (n:N) ON (n.p)",
            "CREATE CONSTRAINT c FOR (n:N) REQUIRE n.id IS UNIQUE",
            "FOREACH (x IN [1] | CREATE (n))",
        ] {
            assert!(is_write(&plan_of(cypher)), "{cypher}");
        }
    }

    #[test]
    fn reads_classify_as_reads() {
        for cypher in [
            "RETURN 1 AS n",
            "MATCH (n) RETURN n",
            "SHOW INDEXES",
            "CALL db.labels()",
        ] {
            assert!(!is_write(&plan_of(cypher)), "{cypher}");
        }
    }

    #[test]
    fn read_only_call_in_expression_subqueries_classifies_as_read() {
        // Regression: `is_write` must not descend into expression
        // subqueries — a read-only `CALL { … }` there is legal (plan-time
        // validation only rejects actual writes), and classifying the
        // statement as a write would reject a pure read on the read-only
        // `system_ontologies` namespace and misroute it on `neo4j`.
        for cypher in [
            "RETURN EXISTS { CALL { RETURN 1 AS x } } AS x",
            "RETURN COUNT { CALL { RETURN 1 AS x } } AS x",
            "RETURN COLLECT { CALL { RETURN 1 AS x } RETURN x } AS x",
        ] {
            assert!(!is_write(&plan_of(cypher)), "{cypher}");
        }
    }

    #[test]
    fn nested_queries_recurse_through_one_predicate() {
        // WHEN branches, ELSE bodies and NEXT tails are all recursed into
        // by `is_write`'s shared fold (`access::plan_access`), so the one
        // clause classification classifies them.
        assert!(is_write(&plan_of("WHEN true THEN { CREATE (n) }")));
        assert!(!is_write(&plan_of(
            "WHEN true THEN { RETURN 1 AS x } ELSE { RETURN 2 AS x }"
        )));
        assert!(is_write(&plan_of(
            "WHEN false THEN { MATCH (n) RETURN count(n) AS x } \
             ELSE { CREATE (m) RETURN 0 AS x }"
        )));
        assert!(is_write(&plan_of("RETURN 1 AS x NEXT CREATE (n)")));
        assert!(!is_write(&plan_of("RETURN 1 AS x UNION RETURN 2 AS x")));
    }

    #[test]
    fn write_procedure_calls_are_found_anywhere_in_the_clause_tree() {
        // The facade's predicate classifies `db.createLabel` as a write
        // (it is flagged `writes` in the canonical registry); the tests
        // here stand in with a name predicate.
        let is_write_proc = &mut |name: &str| name == "db.createLabel";
        for cypher in [
            "CALL db.createLabel('X')",
            "WHEN true THEN { CALL db.createLabel('X') }",
            "WHEN false THEN { RETURN 1 AS x } ELSE { CALL db.createLabel('X') RETURN 1 AS x }",
            "CALL { CALL db.createLabel('X') }",
            // A FOREACH body admits only updating clauses, so a procedure
            // call cannot appear there (42I01, live Neo4j).
            "RETURN 1 AS x NEXT CALL db.createLabel('X')",
        ] {
            assert!(
                plan_calls_write_procedure(&plan_of(cypher), is_write_proc),
                "{cypher}"
            );
        }
        // Read procedures and write-free statements still classify as reads.
        for cypher in [
            "CALL db.labels()",
            "WHEN true THEN { CALL db.labels() YIELD label RETURN label }",
            "RETURN 1 AS x",
        ] {
            assert!(
                !plan_calls_write_procedure(&plan_of(cypher), is_write_proc),
                "{cypher}"
            );
        }
    }

    #[test]
    fn database_admin_commands_classify() {
        assert!(is_database_admin_command(&plan_of("SHOW DATABASES")));
        assert!(is_database_admin_command(&plan_of(
            "ALTER DATABASE neo4j SET DEFAULT LANGUAGE CYPHER 25"
        )));
        assert!(!is_database_admin_command(&plan_of("MATCH (n) RETURN n")));
        assert!(!is_database_admin_command(&plan_of("SHOW INDEXES")));
        // A non-administrative clause disqualifies the statement.
        assert!(!is_database_admin_command(&plan_of(
            "SHOW DATABASES YIELD name RETURN name"
        )));
    }

    /// A lowered `SHOW DATABASES` catalog clause. The front-end's
    /// NEXT-chain validation rejects the command in a chain before
    /// planning, so the classifier's chain inputs are assembled directly
    /// here — this is exactly the shape the execution-phase backstop sees.
    fn show_databases_clause() -> Clause {
        Clause::Catalog(Box::new(relate_parser::Clause::ShowDatabases(
            relate_parser::ShowDatabasesClause {
                filter: relate_parser::DatabaseFilter::All,
                yields: Default::default(),
                where_expr: None,
                stages: Vec::new(),
            },
        )))
    }

    #[test]
    fn show_databases_with_next_classifies_the_chain() {
        // The command in the head stage.
        let mut head = plan_of("RETURN 1 AS x NEXT RETURN x");
        head.parts[0] = vec![show_databases_clause()];
        assert!(is_show_databases_with_next(&head));

        // The command in a NEXT stage: `parts()` spans every stage.
        let mut tail = plan_of("RETURN 1 AS x NEXT RETURN x");
        tail.next.as_mut().unwrap().parts[0] = vec![show_databases_clause()];
        assert!(is_show_databases_with_next(&tail));

        // SHOW DATABASES without a NEXT stage is the plain §2 command.
        let mut plain = plan_of("RETURN 1 AS x");
        plain.parts[0] = vec![show_databases_clause()];
        assert!(!is_show_databases_with_next(&plain));

        // ALTER DATABASE is §2 but not a SHOW DATABASES chain participant.
        let mut alter = plan_of("ALTER DATABASE neo4j SET DEFAULT LANGUAGE CYPHER 25");
        alter.next = Some(Box::new(plan_of("RETURN 1 AS x")));
        assert!(!is_show_databases_with_next(&alter));

        // An ordinary NEXT chain does not classify.
        assert!(!is_show_databases_with_next(&plan_of(
            "RETURN 1 AS x NEXT RETURN x + 1 AS y"
        )));
    }

    #[test]
    fn system_offender_names_follow_ce() {
        // Data clauses offend; CE suppresses the tail when a non-tail
        // offender is present.
        assert_eq!(
            unsupported_system_clause_names(&plan_of("MATCH (n) RETURN count(n) AS c")),
            Some("MATCH".to_string())
        );
        // A lone tail offender still reports itself.
        assert_eq!(
            unsupported_system_clause_names(&plan_of("RETURN 1 AS x")),
            Some("RETURN".to_string())
        );
        // Standalone procedure calls, SHOW commands and administration
        // commands are allowed.
        for cypher in [
            "CALL db.labels()",
            "CALL db.labels() YIELD label RETURN label",
            "SHOW INDEXES",
            "SHOW DATABASES",
        ] {
            assert_eq!(
                unsupported_system_clause_names(&plan_of(cypher)),
                None,
                "{cypher}"
            );
        }
    }

    /// ADR-0116 §4 completion (2026-11-04): a `WITH` in a part that
    /// composes a database-administration command — the classifier is
    /// namespace-blind (the facade gate applies it to `system` only), and
    /// position within the part is not part of the rule (live CE rejects
    /// the `WITH … <command> …` spelling with the same text).
    #[test]
    fn composed_admin_disallowed_clause_flags_with_around_admin_commands() {
        for cypher in [
            "SHOW DATABASES YIELD name AS d1 WITH 1 AS n RETURN n",
            "USE system SHOW DATABASES YIELD name AS d1 WITH 1 AS n RETURN n",
            "WITH 1 AS n SHOW DATABASES YIELD name RETURN name",
            "SHOW DEFAULT DATABASE YIELD name WITH 1 AS n RETURN n",
            "SHOW HOME DATABASE YIELD name WITH 1 AS n RETURN n",
            "SHOW DATABASE neo4j YIELD name WITH 1 AS n RETURN n",
        ] {
            assert_eq!(
                composed_admin_disallowed_clause(&plan_of(cypher)),
                Some("WITH"),
                "{cypher}"
            );
        }
        // No administration command composed: the catalog SHOW commands
        // keep their `WITH` composability (out of the 2026-11-04 scope).
        for cypher in [
            "SHOW SETTINGS YIELD name WITH 1 AS n RETURN n",
            "CALL db.labels() YIELD label WITH 1 AS n RETURN n",
            "WITH 1 AS n RETURN n",
        ] {
            assert_eq!(
                composed_admin_disallowed_clause(&plan_of(cypher)),
                None,
                "{cypher}"
            );
        }
        // Administration commands without a `WITH` stay composable with
        // their CE tails (WHERE/RETURN live in or after the command).
        for cypher in [
            "SHOW DATABASES",
            "SHOW DATABASES YIELD name RETURN name",
            "USE system SHOW DEFAULT DATABASE YIELD name AS defaultDb SHOW DATABASES YIELD name AS db, access WHERE defaultDb = db RETURN *",
            "ALTER DATABASE neo4j SET DEFAULT LANGUAGE CYPHER 25",
        ] {
            assert_eq!(
                composed_admin_disallowed_clause(&plan_of(cypher)),
                None,
                "{cypher}"
            );
        }
    }

    /// ADR-0116 §2 amendment (2026-11-04): the statement-body head shape
    /// CE's 42N79 rule guards — first clause after any leading `USE` is a
    /// database-administration command.
    #[test]
    fn admin_command_heads_statement_classifies_the_head() {
        for cypher in [
            "SHOW DATABASES",
            "USE system SHOW DATABASES",
            "USE neo4j SHOW DATABASES",
            "USE neo4j SHOW DEFAULT DATABASE",
            "USE neo4j SHOW HOME DATABASE",
            "USE neo4j SHOW DATABASE neo4j",
            "USE neo4j ALTER DATABASE neo4j SET DEFAULT LANGUAGE CYPHER 5",
            "USE neo4j SHOW DATABASES YIELD name RETURN name",
            // A UNION arm's head counts through the statement's own head
            // part (live CE 2026.09: 42N79 for this shape).
            "USE neo4j SHOW DATABASES YIELD name AS a RETURN a UNION SHOW DATABASES YIELD name AS a RETURN a",
        ] {
            assert!(
                admin_command_heads_statement(&plan_of(cypher)),
                "{cypher}"
            );
        }
        for cypher in [
            "MATCH (n) RETURN n",
            // An administration command later in the body is a catalog
            // read, not a headed command statement (live CE runs this).
            "USE neo4j MATCH (n) SHOW DATABASES YIELD name RETURN name",
            // A leading WITH heads the body instead (live CE: 51N28, not
            // 42N79 — the off-system composed-form boundary this engine
            // deliberately does not implement).
            "USE neo4j WITH 1 AS n SHOW DATABASES YIELD name RETURN name",
            "SHOW INDEXES",
        ] {
            assert!(!admin_command_heads_statement(&plan_of(cypher)), "{cypher}");
        }
    }
}