Skip to main content

relate_executor/exec/call/
in_tx_strategy.rs

1//! The `CALL { … } IN TRANSACTIONS` body's row-production strategy
2//! (ADR-0669): the statement-level admission [`batch_pass_admission`] and
3//! the two producers it chooses between — the batch pass
4//! ([`pass_batch_rows`], one clause-engine crossing over the whole batch)
5//! and the per-row path ([`per_row_batch_rows`]). Split out of
6//! `in_transactions.rs`, which keeps the arm's streaming loop, its
7//! charging invariant, the option resolution and the error/status
8//! rendering.
9
10use std::collections::BTreeMap;
11
12use relate_core::{Result, Value};
13use relate_ir::{Clause, Query};
14
15use super::{execute_query_seeded, row_product, scope_seed, SubquerySpec};
16use crate::ctx::QueryCtx;
17use crate::exec::call::in_transactions::ChargedRows;
18use crate::exec::row_production::body_projects_rows;
19use crate::overlay::ExecGraph;
20use crate::row::Row;
21
22/// Whether an `IN TRANSACTIONS` body takes the batch pass (ADR-0669): a
23/// single flat part, no `NEXT` stage, non-projecting, with no `WITH` and
24/// no schema/admin DDL among its own clauses. Everything else about the
25/// body runs in the pass exactly as its clause list would in a plain
26/// statement over the batch's seed rows — `MATCH`, procedure `CALL`,
27/// nested `CALL { … }`, conditionals, graph-reading expressions
28/// included: pipeline order, one clause-engine invocation and one
29/// nested-budget tick per batch, the per-row statement-engine loop
30/// never entered. The batch commits per batch either way (ADR-0210);
31/// what the pass removes is the per-row invocation, which is pure
32/// overhead — the caller already named the batch.
33///
34/// The exclusions are structural, not semantic preferences:
35/// - a `WITH`-carrying body needs the importing-scope re-attach
36///   (`run_nested_clauses_scoped`), which re-attaches one scope row per
37///   nested invocation — per-row by construction. The legacy
38///   importing-`WITH` form keeps the per-row path with it;
39/// - a projecting body (a trailing clause the canonical table says
40///   spills rows — [`body_projects_rows`], the same classification the
41///   per-row merge consults, never inferred from the clause set) merges
42///   each body row back onto its originating outer row, a lineage the
43///   pass does not track. A terminal `LOAD CSV` projects and stays
44///   per-row with it.
45/// - schema/admin DDL has no per-row work to batch and stays per-row.
46///
47/// The pass gives up Neo4j's per-row read-after-write interleaving — the
48/// body evaluates the batch as a unit. That decision, its within-clause
49/// coincidence and the smaller-batch remedy are recorded once in ADR-0669.
50pub fn batch_pass_admission(query: &Query) -> bool {
51    query.is_single_flat_part()
52        && !body_projects_rows(query)
53        && !relate_planner::query_any_clause(query, &mut |clause| {
54            matches!(clause, Clause::With { .. }) || relate_planner::clause_is_ddl_command(clause)
55        })
56}
57
58/// The batch pass (ADR-0669): the whole batch's scope seeds — the same
59/// per-row [`scope_seed`] policy — in one [`execute_query_seeded`]
60/// crossing, the batch's one nested-budget tick. A non-projecting
61/// body's pass-through is the outer rows (the batch twin of the
62/// per-row path's `outer.clone()`), so the crossing's own output is
63/// discarded. Rollback stays with the caller's per-batch snapshot.
64pub(super) fn pass_batch_rows(
65    graph: &ExecGraph<'_>,
66    batch: &[Row],
67    spec: &SubquerySpec<'_>,
68    params: &BTreeMap<String, Value>,
69    qctx: QueryCtx<'_>,
70    out: &mut ChargedRows,
71) -> Result<()> {
72    let mut seeds = ChargedRows::from_rows(
73        batch
74            .iter()
75            .map(|row| scope_seed(graph, spec.query, spec.scope, row))
76            .collect::<Result<Vec<_>>>()?,
77    )?;
78    // The seeds are a second copy of the batch, live for the crossing's
79    // lifetime: `seeds`'s own charge keeps them billed (it drops with the
80    // struct) while the body runs, so the ledger sees both copies. The
81    // crossing's own output is discarded — a non-projecting body's
82    // pass-through is the outer rows below.
83    execute_query_seeded(graph, spec.query, params, seeds.take_rows(), qctx)?;
84    out.extend(batch.iter().cloned())
85}
86
87/// The per-row path, for the bodies the pass cannot take (a
88/// `WITH`-carrying body, a projecting body, schema/admin DDL): one
89/// nested statement-engine invocation per row through
90/// [`row_product`], the `OPTIONAL CALL` empty-body pad included.
91pub(super) fn per_row_batch_rows(
92    graph: &ExecGraph<'_>,
93    batch: &[Row],
94    spec: &SubquerySpec<'_>,
95    params: &BTreeMap<String, Value>,
96    qctx: QueryCtx<'_>,
97    out: &mut ChargedRows,
98) -> Result<()> {
99    for row in batch {
100        out.extend(row_product(
101            graph,
102            row,
103            spec.optional,
104            spec.query,
105            spec.scope,
106            params,
107            qctx,
108        )?)?;
109    }
110    Ok(())
111}