relate-planner 1.5.0

Bind OpenCypher AST to a logical plan
Documentation
//! `relate-planner`: bind a lowered `Query` to a [`LogicalPlan`]
//! (ADR-0009), then rewrite MATCH (ADR-0024).
//!
//! Module map:
//! - [`plan`] — plan types, the `plan` entry point (consumes the `Query`)
//!   and the canonical plan walks ([`walk_plan_clauses`] for the full
//!   clause tree, [`walk_plan_parts`] for part-granular top-level clauses,
//!   [`walk_query_parts`] for the same part-granular walk over a
//!   not-yet-planned `ir::Query`)
//! - [`binding`] — the variable-binding environment
//! - [`validate`] — clause-level semantic validation
//! - [`expr_check`] / [`pattern`] / [`agg`] — expression, pattern, and
//!   aggregation checks used by validation
//! - [`names`] — output-column computation (UNION agreement, subquery
//!   exports, NEXT-stage seeds)
//! - [`rewrite`] — post-bind MATCH rewrites
//! - [`order`] — cardinality estimates and MATCH pattern order
//! - [`clause_kind`] — the Cypher keyword for a parser / planner-IR clause,
//!   the one read/write/schema clause classification behind
//!   [`clause_mutates_store`] and [`clause_projects_rows`] (its published
//!   per-clause answers)
//! - [`access`] — the one read/write/schema clause fold over a statement,
//!   shared by [`namespaces`] and [`query_type`]
//! - [`namespaces`] — the ADR-0116 namespace classifiers (§2
//!   database-administration commands, §4 `system` / `system_ontologies`
//!   gates, read/write classification); the §3 `USE` rules live in
//!   [`use_target`]
//! - [`query_type`] — the Neo4j Query API v2 `queryType` classifier
//!   (r / w / rw / s), a projection of the shared [`access`] fold plus the
//!   statement's returned columns (ADR-0332)
//! - [`inline`] — the bounded-work plan predicate (which planned
//!   statements are quick at this engine's scale and may run on the
//!   calling thread of a worker-stack-asserting connection, ADR-0504)
//!   and the cache-hit inline route's containment rule (whether a
//!   raw-string plan-cache hit may run on a thread that never asserted
//!   the worker's stack, ADR-0663)

mod access;
mod agg;
mod binding;
mod clause_kind;
mod expr_check;
mod expr_text;
mod inline;
mod literal;
mod names;
mod namespaces;
mod order;
mod pattern;
mod plan;
mod query_type;
mod resolve;
mod rewrite;
mod scan_order;
mod stats;
mod use_target;
mod validate;

pub use crate::clause_kind::{
    clause_is_ddl_command, clause_keyword, clause_mutates_store, clause_projects_rows,
    clause_writes_store,
};
pub use crate::expr_text::expr_text;
pub use crate::inline::{hit_is_inline_safe, plan_is_bounded_work, plan_references_auto_parameter};
pub use crate::names::{conditional_declared_columns, query_with_persistent_imports};
pub use crate::namespaces::{
    admin_command_heads_statement, composed_show_current_user, composed_show_databases,
    composed_system_offence, is_database_admin_command, is_show_databases_with_next, is_write,
    plan_calls_write_procedure, unsupported_system_clause_names, SystemComposition,
};
pub use crate::order::{
    apply_degree, apply_hop_bound, apply_path_degree, best_seek, can_reverse_path,
    can_reverse_varlen_leg, detect_junction_split, endpoint_estimate, evaluate_cross_clause_cost,
    junction_legs_have_cross_inline_refs, reverse_path, reverse_varlen_leg, seed_on_rel_start,
    seed_rows, split_junction_legs, walk_cost, where_conjuncts_of, Basis, Bound, CrossClauseCost,
    CrossClauseSearchEnd, Estimate, Hop, JunctionSplitPoint, MatchStrategy, SeekPick,
    HASH_JOIN_MIN_SIDE_ROWS,
};
/// ADR-0493's range-seek crossover policy — `pub` for the executor
/// funnel, the policy's second consumer (the ladder is the first).
pub use crate::order::{crossover_scan_rows, range_seek_gated};
pub use crate::plan::{
    plan, plan_any_clause, plan_contains_dynamic_operands, plan_contains_in_transactions,
    plan_has_bound_float_integer_site, plan_is_create_only, plan_missing_parameters,
    plan_output_names, plan_output_names_expect, plan_parameter_names, plan_with_stats,
    plan_with_tokens, query_any_clause, walk_plan_clauses, walk_plan_parts, walk_query_parts,
    LogicalPlan,
};
pub use crate::query_type::{query_type, QueryType};
pub use crate::stats::{
    ContainsCount, DegreeMoments, HeavyValue, KeyStats, NoPatternStats, PatternStats, Provenance,
    RowEstimate,
};
pub use crate::validate::check_union_joiners;

/// Test helper: parse `cypher` and plan it with no write procedures
/// registered. Shared by the `namespaces`, `query_type` and `inline` test
/// modules, and the composition pins' acceptance loop; both unwraps carry
/// the statement so a failure names it.
#[cfg(test)]
pub(crate) fn plan_of(cypher: &str) -> LogicalPlan {
    let ast =
        relate_front::parse_cypher(cypher).unwrap_or_else(|error| panic!("{cypher}: {error}"));
    plan(ast, &mut |_| false).unwrap_or_else(|error| panic!("{cypher}: {error}"))
}

/// Test helper: the part-scoped sibling of [`rejection`] — run the
/// semantic clause validation on an already-lowered single-part
/// statement's one part, returning the rejection's text. The pins whose
/// planner arm is a backstop (a rule the front end answers first for
/// every admitted statement) ride this — the parse-and-delegate spelling
/// or the already-lowered form a mutation pin holds; whole-statement
/// pins ride [`rejection`]/[`plan_of`].
#[cfg(test)]
pub(crate) fn part_error(query: &relate_ir::Query) -> String {
    let [part] = query.parts.as_slice() else {
        panic!("expected one part");
    };
    validate::validate_clauses_with_env(
        &part.clauses,
        validate::Scope::stage(binding::Env::new()),
        validate::write::WriteHistory::empty(),
        &mut |_| false,
    )
    .expect_err("the composition rule rejects this statement")
    .to_string()
}

/// Test helper: parse and lower `cypher` to the [`relate_ir::Query`] the
/// cache carries beside its plan — the inline suite's composer pins read
/// it as the entry's carried AST; the unwrap carries the statement so a
/// failure names it.
#[cfg(test)]
pub(crate) fn query_of(cypher: &str) -> relate_ir::Query {
    relate_front::parse_and_lower(cypher).unwrap_or_else(|error| panic!("{cypher}: {error}"))
}

/// Test helper: the error text of a statement the product parser or the
/// planner rejects. Either stage is a rejection, so both are folded into
/// one string — the one helper every planner test module uses.
#[cfg(test)]
pub(crate) fn rejection(src: &str) -> String {
    match relate_front::parse_and_lower(src) {
        Ok(q) => plan(q, &mut |_| false).unwrap_err().to_string(),
        Err(e) => e.to_string(),
    }
}

#[cfg(test)]
mod tests;