Skip to main content

relate_planner/
lib.rs

1//! `relate-planner`: bind a lowered `Query` to a [`LogicalPlan`]
2//! (ADR-0009), then rewrite MATCH (ADR-0024).
3//!
4//! Module map:
5//! - [`plan`] — plan types, the `plan` entry point (consumes the `Query`)
6//!   and the canonical plan walks ([`walk_plan_clauses`] for the full
7//!   clause tree, [`walk_plan_parts`] for part-granular top-level clauses,
8//!   [`walk_query_parts`] for the same part-granular walk over a
9//!   not-yet-planned `ir::Query`)
10//! - [`binding`] — the variable-binding environment
11//! - [`validate`] — clause-level semantic validation
12//! - [`expr_check`] / [`pattern`] / [`agg`] — expression, pattern, and
13//!   aggregation checks used by validation
14//! - [`names`] — output-column computation (UNION agreement, subquery
15//!   exports, NEXT-stage seeds)
16//! - [`rewrite`] — post-bind MATCH rewrites
17//! - [`order`] — cardinality estimates and MATCH pattern order
18//! - [`clause_kind`] — the Cypher keyword for a parser / planner-IR clause,
19//!   the one read/write/schema clause classification behind
20//!   [`clause_mutates_store`] and [`clause_projects_rows`] (its published
21//!   per-clause answers)
22//! - [`access`] — the one read/write/schema clause fold over a statement,
23//!   shared by [`namespaces`] and [`query_type`]
24//! - [`namespaces`] — the ADR-0116 namespace classifiers (§2
25//!   database-administration commands, §4 `system` / `system_ontologies`
26//!   gates, read/write classification); the §3 `USE` rules live in
27//!   [`use_target`]
28//! - [`query_type`] — the Neo4j Query API v2 `queryType` classifier
29//!   (r / w / rw / s), a projection of the shared [`access`] fold plus the
30//!   statement's returned columns (ADR-0332)
31//! - [`inline`] — the bounded-work plan predicate (which planned
32//!   statements are quick at this engine's scale and may run on the
33//!   calling thread of a worker-stack-asserting connection, ADR-0504)
34//!   and the cache-hit inline route's containment rule (whether a
35//!   raw-string plan-cache hit may run on a thread that never asserted
36//!   the worker's stack, ADR-0663)
37
38mod access;
39mod agg;
40mod binding;
41mod clause_kind;
42mod expr_check;
43mod expr_text;
44mod inline;
45mod literal;
46mod names;
47mod namespaces;
48mod order;
49mod pattern;
50mod plan;
51mod query_type;
52mod resolve;
53mod rewrite;
54mod scan_order;
55mod stats;
56mod use_target;
57mod validate;
58
59pub use crate::clause_kind::{
60    clause_is_ddl_command, clause_keyword, clause_mutates_store, clause_projects_rows,
61    clause_writes_store,
62};
63pub use crate::expr_text::expr_text;
64pub use crate::inline::{hit_is_inline_safe, plan_is_bounded_work, plan_references_auto_parameter};
65pub use crate::names::{conditional_declared_columns, query_with_persistent_imports};
66pub use crate::namespaces::{
67    admin_command_heads_statement, composed_show_current_user, composed_show_databases,
68    composed_system_offence, is_database_admin_command, is_show_databases_with_next, is_write,
69    plan_calls_write_procedure, unsupported_system_clause_names, SystemComposition,
70};
71pub use crate::order::{
72    apply_degree, apply_hop_bound, apply_path_degree, best_seek, can_reverse_path,
73    can_reverse_varlen_leg, detect_junction_split, endpoint_estimate, evaluate_cross_clause_cost,
74    junction_legs_have_cross_inline_refs, reverse_path, reverse_varlen_leg, seed_on_rel_start,
75    seed_rows, split_junction_legs, walk_cost, where_conjuncts_of, Basis, Bound, CrossClauseCost,
76    CrossClauseSearchEnd, Estimate, Hop, JunctionSplitPoint, MatchStrategy, SeekPick,
77    HASH_JOIN_MIN_SIDE_ROWS,
78};
79/// ADR-0493's range-seek crossover policy — `pub` for the executor
80/// funnel, the policy's second consumer (the ladder is the first).
81pub use crate::order::{crossover_scan_rows, range_seek_gated};
82pub use crate::plan::{
83    plan, plan_any_clause, plan_contains_dynamic_operands, plan_contains_in_transactions,
84    plan_has_bound_float_integer_site, plan_is_create_only, plan_missing_parameters,
85    plan_output_names, plan_output_names_expect, plan_parameter_names, plan_with_stats,
86    plan_with_tokens, query_any_clause, walk_plan_clauses, walk_plan_parts, walk_query_parts,
87    LogicalPlan,
88};
89pub use crate::query_type::{query_type, QueryType};
90pub use crate::stats::{
91    ContainsCount, DegreeMoments, HeavyValue, KeyStats, NoPatternStats, PatternStats, Provenance,
92    RowEstimate,
93};
94pub use crate::validate::check_union_joiners;
95
96/// Test helper: parse `cypher` and plan it with no write procedures
97/// registered. Shared by the `namespaces`, `query_type` and `inline` test
98/// modules, and the composition pins' acceptance loop; both unwraps carry
99/// the statement so a failure names it.
100#[cfg(test)]
101pub(crate) fn plan_of(cypher: &str) -> LogicalPlan {
102    let ast =
103        relate_front::parse_cypher(cypher).unwrap_or_else(|error| panic!("{cypher}: {error}"));
104    plan(ast, &mut |_| false).unwrap_or_else(|error| panic!("{cypher}: {error}"))
105}
106
107/// Test helper: the part-scoped sibling of [`rejection`] — run the
108/// semantic clause validation on an already-lowered single-part
109/// statement's one part, returning the rejection's text. The pins whose
110/// planner arm is a backstop (a rule the front end answers first for
111/// every admitted statement) ride this — the parse-and-delegate spelling
112/// or the already-lowered form a mutation pin holds; whole-statement
113/// pins ride [`rejection`]/[`plan_of`].
114#[cfg(test)]
115pub(crate) fn part_error(query: &relate_ir::Query) -> String {
116    let [part] = query.parts.as_slice() else {
117        panic!("expected one part");
118    };
119    validate::validate_clauses_with_env(
120        &part.clauses,
121        validate::Scope::stage(binding::Env::new()),
122        validate::write::WriteHistory::empty(),
123        &mut |_| false,
124    )
125    .expect_err("the composition rule rejects this statement")
126    .to_string()
127}
128
129/// Test helper: parse and lower `cypher` to the [`relate_ir::Query`] the
130/// cache carries beside its plan — the inline suite's composer pins read
131/// it as the entry's carried AST; the unwrap carries the statement so a
132/// failure names it.
133#[cfg(test)]
134pub(crate) fn query_of(cypher: &str) -> relate_ir::Query {
135    relate_front::parse_and_lower(cypher).unwrap_or_else(|error| panic!("{cypher}: {error}"))
136}
137
138/// Test helper: the error text of a statement the product parser or the
139/// planner rejects. Either stage is a rejection, so both are folded into
140/// one string — the one helper every planner test module uses.
141#[cfg(test)]
142pub(crate) fn rejection(src: &str) -> String {
143    match relate_front::parse_and_lower(src) {
144        Ok(q) => plan(q, &mut |_| false).unwrap_err().to_string(),
145        Err(e) => e.to_string(),
146    }
147}
148
149#[cfg(test)]
150mod tests;