relate-ir 1.5.0

Planner IR (lowered Cypher AST)
Documentation
//! The nested-body AST fixtures the shared body-descent skeleton's two
//! test suites build their subjects from (ADR-0740): the one-arm body,
//! the concluding-clause constructors, and the nesting shapes — spelled
//! once, so the IR skeleton's suite (this crate's
//! `ast/body_descent_tests`) and the executor verdict's suite
//! (`relate-executor`'s `exec/row_production_tests`) cannot drift apart.
//!
//! Test-only, compiled for this crate's own tests and for a dependent
//! that enables `test-hooks` from its `[dev-dependencies]` — the
//! relate-executor `test-hooks` precedent — never in a shipped build.

use relate_names::SmallName;

use super::{
    CallArgs, CallScope, Clause, Concurrency, DisjointBy, Expr, InTransactions, OnError, Query,
    SingleQuery, YieldItems,
};

/// The one `CALL { … }` constructor: both spellings of the clause —
/// plain and batched — build from here, so the clause's fields are
/// spelled once.
fn call_subquery(query: Query, in_transactions: Option<InTransactions>) -> Clause {
    Clause::CallSubquery {
        query: Box::new(query),
        optional: false,
        scope: CallScope::None,
        in_transactions,
    }
}

/// A one-arm body of `clauses` — the fixture every body-descent test
/// builds its nesting shapes from.
pub fn body(clauses: Vec<Clause>) -> Query {
    Query {
        parts: vec![SingleQuery { clauses }],
        union_all: Vec::new(),
        next: None,
    }
}

/// A concluding `RETURN` — the projecting concluding clause.
pub fn return_clause() -> Clause {
    Clause::Return {
        distinct: false,
        group_by: None,
        items: Vec::new(),
        order: Vec::new(),
        skip: None,
        limit: None,
    }
}

/// A concluding `CREATE` — the unit concluding clause.
pub fn create_clause() -> Clause {
    Clause::Create {
        patterns: Vec::new(),
    }
}

/// The one procedure-call constructor: both spellings — `YIELD` and
/// void — build from here, so the clause's fields are spelled once.
fn proc_call(name: &str, yield_items: YieldItems) -> Clause {
    Clause::Call {
        name: SmallName::from_name(name),
        args: CallArgs::Explicit(Vec::new()),
        yield_items,
        optional: false,
        where_clause: None,
    }
}

/// A procedure call `CALL db.labels() YIELD label` — the lone
/// unfiltered `YIELD` call a body may conclude in, leaving it unit.
pub fn proc_call_clause() -> Clause {
    proc_call(
        "db.labels",
        YieldItems::Named(vec![(SmallName::from_name("label"), None)]),
    )
}

/// A void procedure call `CALL db.awaitIndexes()` — no `YIELD` tail, the
/// call that seeds the next `NEXT` stage with one row as a write does.
pub fn void_proc_call_clause() -> Clause {
    proc_call("db.awaitIndexes", YieldItems::Default)
}

/// A `CALL { … }` over `query`, unbatched — the nesting clause the
/// verdicts descend through.
pub fn call_clause(query: Query) -> Clause {
    call_subquery(query, None)
}

/// A `CALL { … } IN TRANSACTIONS` over `query` — the batched nesting
/// clause, the executor verdict's one ruling the naming twin does not
/// share.
pub fn batched_call_clause(query: Query) -> Clause {
    call_subquery(
        query,
        Some(InTransactions {
            concurrency: Concurrency::Sequential,
            batch_size: None,
            on_error: OnError::Fail,
            report_status: None,
            disjoint: DisjointBy::Absent,
        }),
    )
}

/// A `NEXT`-chained body: a first stage whose one arm concludes in
/// `first_concluding`, chained to `last` — the shape the FINAL-stage
/// cursor tests judge (the first stage's answer never reaches the
/// verdict, in either direction).
pub fn next_chain(first_concluding: Clause, last: Query) -> Query {
    Query {
        parts: vec![SingleQuery {
            clauses: vec![first_concluding],
        }],
        union_all: Vec::new(),
        next: Some(Box::new(last)),
    }
}

/// A two-arm `UNION` stage: one arm concluding in `first`, one in
/// `second`, plain `UNION` joiner — the shape the any-arm fold tests
/// judge (on a validated body the arms agree; the kit spells the
/// representative-disagreement shape validation refuses).
pub fn union_arms(first: Clause, second: Clause) -> Query {
    Query {
        parts: vec![
            SingleQuery {
                clauses: vec![first],
            },
            SingleQuery {
                clauses: vec![second],
            },
        ],
        union_all: vec![false],
        next: None,
    }
}

/// A `WHEN … THEN … / ELSE` over `branches` with an optional `ELSE`
/// body: every branch predicate is the constant `true` (the predicate
/// never reaches a verdict — only the bodies do).
pub fn when_clause(branches: Vec<Query>, else_query: Option<Query>) -> Clause {
    Clause::Conditional {
        branches: branches
            .into_iter()
            .map(|query| (Expr::Bool(true), query))
            .collect(),
        else_query: else_query.map(Box::new),
    }
}