relate-ir 1.0.0

Planner IR (lowered Cypher AST)
Documentation
//! The `CALL { … } IN TRANSACTIONS` AST family: the scope clause
//! ([`CallScope`], CIP-141 / CIP-32) and the transaction modifiers it
//! carries ([`InTransactions`] and its option enums, CIP-34 / CIP-198 /
//! CIP-260). Split from [`super`] so the clause vocabulary stays one
//! concern per file; [`super`] re-exports every item, so the public path
//! is unchanged.

use super::Expr;
use relate_names::SmallName;

#[derive(Debug, Clone, PartialEq)]
pub enum CallScope {
    None,
    Wildcard,
    Vars(Vec<SmallName>),
}

impl CallScope {
    /// Whether the subquery is a scope clause (`CALL (…) { … }` /
    /// `CALL (*) { … }`) rather than the naked `CALL { … }`. Only a scope
    /// clause carries a CIP-218 Rule 10 import set; the naked form imports
    /// the whole row without one. The one statement of that fact.
    pub fn has_scope_clause(&self) -> bool {
        !matches!(self, Self::None)
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct InTransactions {
    pub concurrency: Concurrency,
    pub batch_size: Option<Expr>,
    pub on_error: OnError,
    pub report_status: Option<SmallName>,
    /// CIP-260 `DISJOINT BY` operand (a concurrent-batching hint).
    pub disjoint: DisjointBy,
}

impl InTransactions {
    /// Every value expression the modifiers carry, in source order: the
    /// batch size, the concurrency count, the retry duration, and each
    /// `DISJOINT BY` key. The one slot enumeration — the IR walker and the
    /// planner's modifier checks read it instead of re-spelling the four
    /// fields, so a new slot cannot be walked by one and forgotten by the
    /// other.
    pub fn exprs(&self) -> impl Iterator<Item = &Expr> {
        let mut out = Vec::new();
        out.extend(self.batch_size.as_ref());
        if let Concurrency::Value(expr) = &self.concurrency {
            out.push(expr);
        }
        if let OnError::Retry {
            duration: Some(duration),
            ..
        } = &self.on_error
        {
            out.push(duration);
        }
        if let DisjointBy::Expressions(keys) = &self.disjoint {
            out.extend(keys);
        }
        out.into_iter()
    }
}

/// CIP-260 `DISJOINT BY` operand: absent, the key expressions that
/// partition the input rows, or the `AUTO`/`NONE` keyword forms.
///
/// Absence is a variant rather than an `Option` wrapper so the parser
/// produces exactly one value and every downstream match is total — the
/// clause's `NONE` keyword form stays spellable next to "no clause".
#[derive(Debug, Clone, Default, PartialEq)]
pub enum DisjointBy {
    #[default]
    Absent,
    Expressions(Vec<Expr>),
    Auto,
    None,
}

#[derive(Debug, Clone, PartialEq)]
pub enum Concurrency {
    Sequential,
    Default,
    Value(Expr),
}

#[derive(Debug, Clone, PartialEq)]
pub enum OnError {
    Fail,
    Continue,
    Break,
    Retry {
        duration: Option<Expr>,
        fallback: RetryFallback,
    },
}

impl OnError {
    /// The batch's failure handling as the executor applies it: `FAIL`,
    /// `CONTINUE`, `BREAK`, or a `RETRY`'s explicit `THEN` fallback. The
    /// one projection of the `OnError` vocabulary onto [`RetryFallback`].
    pub fn outcome(&self) -> RetryFallback {
        match self {
            Self::Fail => RetryFallback::Fail,
            Self::Continue => RetryFallback::Continue,
            Self::Break => RetryFallback::Break,
            Self::Retry { fallback, .. } => *fallback,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RetryFallback {
    Fail,
    Continue,
    Break,
}