relate-executor 1.5.0

In-memory graph and Cypher physical executor
Documentation
//! The row-production classification — whether a concluding clause,
//! part or body projects statement rows of its own. The query-structure
//! levels — the FINAL-stage cursor, the any-arm fold, the
//! concluding-clause hand-off — are the one shared body-descent
//! skeleton in the IR (ADR-0740: `relate_ir::stage_projects_rows`,
//! `Query::final_stage_projects_rows`), into which this layer injects
//! its concluding-clause verdict ([`spills_concluding`]); this module
//! keeps the one concluding-clause decision ([`concluding`]) that verdict
//! and the NEXT-seeding one ([`unit_output_concluding`]) project, the
//! stage/body entries its consumers read, and the NEXT-seeding fold
//! ([`parts_have_unit_output`]), split out of the statement-driver root so
//! the classification lives beside its own vocabulary instead of inside
//! the drivers. The conditional's branch-set model is its own sibling
//! ([`super::conditional_branches`]); this verdict consumes it
//! downward.

use relate_ir::{stage_concluding_clauses, Clause, Query, SingleQuery};
use relate_planner::clause_projects_rows;

use super::conditional_branches::ConditionalBranches;

/// Whether a `CALL { … }` / `WHEN` body projects a statement result of
/// its own — its FINAL stage's arms, through the one shared
/// body-descent skeleton (ADR-0740) under this layer's
/// [`spills_concluding`] verdict; a body that is itself a NEXT chain is
/// judged by its final stage, never its first, the exact wrong-cursor
/// drop fixed before the skeleton had one home. Read by every
/// body-level consumer: the CALL-subquery driver's merge policy
/// (`call::call_subquery_row`), the conditional driver's per-row branch
/// read (`call::conditional::exec_conditional`), the batch-pass
/// admission (`call::in_tx_strategy::batch_pass_admission`) and
/// [`spills_concluding`]'s own nested-body descent — this layer's
/// descent under the shared skeleton, where the naming layer's
/// (`relate_ir::Query::projects_columns`) is the twin verdict over the
/// same structure walk; the agreement test beside this module pins the
/// two verdicts' answers over the nested-body vocabulary.
pub(in crate::exec) fn body_projects_rows(body: &Query) -> bool {
    body.final_stage_projects_rows(&spills_concluding)
}

/// Whether any UNION arm of one stage projects a statement result of its
/// own — the stage-level entry of the shared body-descent skeleton
/// (ADR-0740) under [`spills_concluding`], over a stage's parts slice.
/// The slice is the type-level distinction: a whole NEXT-chained body is
/// not a stage, and only [`body_projects_rows`] — the FINAL-stage
/// cursor's one owner here — may hand one in.
/// `execute_query_seeded_in_scope` (in `call`) is the one stage-level
/// caller, naming the stage it runs; every other consumer reads
/// [`body_projects_rows`]. The privacy fence covers exec-level
/// consumers; `call`'s descendants stay on it by this doc, since an
/// item fenced to `crate::exec` remains nameable from them.
pub(in crate::exec) fn stage_projects_rows(parts: &[SingleQuery]) -> bool {
    relate_ir::stage_projects_rows(parts, &spills_concluding)
}

/// The executor's one decision over the clause concluding an arm, under
/// the shared body-descent skeleton (ADR-0740): its two answers are the
/// two concluding-clause policies this layer reads, decided together per
/// clause so they cannot drift. Built by [`concluding`]; read through
/// its two projections, [`spills_concluding`] and
/// [`unit_output_concluding`].
#[derive(Clone, Copy)]
struct Concluding {
    /// Whether the clause spills statement rows when it runs — the
    /// verdict the skeleton folds into [`body_projects_rows`] /
    /// [`stage_projects_rows`], and so the `CALL { … }` merge policy, a
    /// conditional's per-row buckets and the batch-pass admission.
    spills: bool,
    /// Whether the clause leaves its part unit output for the
    /// NEXT-seeding rule (ADR-0287): an empty stage concluding in it
    /// seeds the next stage with one unit row.
    unit_output: bool,
}

/// The executor's concluding-clause decision, one exhaustive match: the
/// planner's published [`clause_projects_rows`] table — position-agnostic,
/// read once in the catch-all arm (a projecting clause spills rows and
/// is not unit output; a non-projecting one the reverse) — plus the
/// three position-specific refinements only this layer can make, each
/// arm spelling both answers together:
///
/// - a `WHEN`/`ELSE` conditional spills rows exactly when some branch
///   body does (a conditional whose branch bodies are all unit leaves
///   the per-row driver's buckets empty), and is unit output exactly
///   when it spills none;
/// - a `CALL { … }` spills its body's answer — except the batched form,
///   which keeps the table's unconditional answer: `CALL { … } IN
///   TRANSACTIONS` always spills rows (each batch's pass-through rows,
///   `REPORT STATUS` rows, `ON ERROR` pads), whatever its body, the one
///   arm the naming twin deliberately does not share (a unit batched
///   call stays columnless there: its rows are the outer pass-through,
///   not body columns). Either form is unit output: a data write in the
///   ADR-0287 vocabulary even when its merged rows are a statement
///   result of its own;
/// - a concluding procedure call spills no rows (as the naming twin
///   rules), and is unit output only when it is void.
///
/// `descend` is the skeleton's body descent under the spills answer —
/// [`body_projects_rows`] — for both projections: a unit conditional
/// body is a unit body everywhere, so its caller's outer rows pass
/// through, never drop, and its `IN TRANSACTIONS` iterations report
/// their status rows.
fn concluding(clause: &Clause, descend: &dyn Fn(&Query) -> bool) -> Concluding {
    match clause {
        Clause::Conditional {
            branches,
            else_query,
        } => {
            let spills = ConditionalBranches::new(branches, else_query.as_deref())
                .bodies()
                // The allocation-free any-branch short-circuit the per-row
                // fold needs: a conditional whose branches are all unit
                // bodies is itself unit output, so an empty result still
                // seeds the next stage with one row (ADR-0287) instead of
                // ending the chain.
                .any(|body| body.is_some_and(descend));
            Concluding {
                spills,
                unit_output: !spills,
            }
        }
        // The batched call always emits rows of its own — never judge it
        // by its body (a WHEN branch body concluding in `CALL { … } IN
        // TRANSACTIONS` dropped its batch rows under the body-classifying
        // spelling; main kept them) — the batched shape read through its
        // canonical helper, `in_transactions_body`.
        Clause::CallSubquery { query, .. } => Concluding {
            spills: clause.in_transactions_body().is_some() || descend(query),
            unit_output: true,
        },
        // A procedure call concludes a body only as a lone unfiltered
        // `YIELD` call or a void call (the call-site rules), and either
        // leaves the body unit — the outer row passes through once. A
        // standalone top-level call's own rows go through the
        // concluding-call path, never this decision. For seeding, a void
        // call seeds one row, as a write does; a `YIELD` call's rows are
        // the stage's rows, so an empty one seeds nothing. A call written
        // without `YIELD` stands for the void call: the compile-time 42N25
        // check rejects a non-void call without `YIELD` in a top-level
        // part, so there the written tail answers the void fact without a
        // registry lookup; inside a body that check does not reach yet,
        // and a non-void call without `YIELD` there is one the reference
        // rejects too.
        Clause::Call { yield_items, .. } => Concluding {
            spills: false,
            unit_output: !yield_items.has_yield(),
        },
        other => {
            let projects = clause_projects_rows(other);
            Concluding {
                spills: projects,
                unit_output: !projects,
            }
        }
    }
}

/// The spills answer of [`concluding`] — the executor's
/// [`relate_ir::ConcludingVerdict`] injected into the shared
/// body-descent skeleton (ADR-0740); the descent the skeleton hands back
/// is this same verdict's recursion.
fn spills_concluding(clause: &Clause, descend: &dyn Fn(&Query) -> bool) -> bool {
    concluding(clause, descend).spills
}

/// The unit-output answer of [`concluding`] — the NEXT-seeding rule's
/// concluding-clause verdict (ADR-0287), riding the shared verdict
/// seam's shape (the clause, with the spills descent handed in) like the
/// two policies the seam exists for.
fn unit_output_concluding(clause: &Clause, descend: &dyn Fn(&Query) -> bool) -> bool {
    concluding(clause, descend).unit_output
}

/// Whether a finished stage seeds the next `NEXT` stage with one unit row
/// when its own result is empty (ADR-0287). Data-write parts
/// (`CREATE`/`MERGE`/`DELETE`/`SET`/`REMOVE`/`FOREACH`/`CALL { … }`) and
/// `FINISH` count, and so do void procedure calls and schema/admin DDL
/// parts — the IR-native
/// index/constraint variants outright, and `Clause::Catalog` exactly when
/// the canonical `relate-front` predicate says the command is NOT
/// row-producing. Row-producing catalog commands (the SHOW family and
/// `TERMINATE TRANSACTIONS`) keep their row semantics: they seed the next
/// stage from their projected rows, never with a unit row.
///
/// The stage walk and its invariants are the skeleton's
/// ([`relate_ir::stage_concluding_clauses`] — the all-form enumeration,
/// with the non-empty-stage and non-empty-part invariants surfaced
/// there, once); this fold only supplies the seeding rule's own
/// verdict ([`unit_output_concluding`]).
pub(in crate::exec) fn parts_have_unit_output<'p>(
    parts: impl Iterator<Item = &'p [Clause]>,
) -> bool {
    stage_concluding_clauses(parts)
        .all(|clause| unit_output_concluding(clause, &body_projects_rows))
}