relate-planner 1.4.0

Bind OpenCypher AST to a logical plan
Documentation
//! The statically-known literal vocabulary: what an expression spells when
//! its value is known at plan time (a literal, a resolved plan-cache
//! placeholder, a folded eager value), the ADR-0175 rejections the
//! integer-argument sites raise from it, and the `CALL { … } IN
//! TRANSACTIONS` modifier sites' positive-integer literal gate (whose
//! texts and class mapping live in the `relate_core::integer_args` rows,
//! REL8-2179). A crate-level module, below both
//! the expression checker and the binding classifier that read it, so
//! neither imports back up into the other.

use relate_core::integer_args::{self, range_text, IntegerArgSite};
use relate_core::{invalid_argument_text, Error, Result, Value};
use relate_ir::{Expr, UnaryOp};

/// The literal an expression spells, bucketed by the kind each caller
/// tests for — `Other` carries every value that is none of the named kinds.
#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum LiteralKind {
    Null,
    Bool,
    Int,
    Float,
    /// A list literal, whatever its elements' dynamism.
    List,
    /// A map literal, whatever its values' dynamism.
    Map,
    /// Every other value: string, temporal, entity, …
    Other,
}

/// The statically-known literal an expression spells, as the value itself: a
/// plan-cache placeholder is resolved to the literal it replaced (ADR-0503),
/// a unary sign is unwrapped (the parser splits `-1.5` and `+1.5` into a
/// unary operator over the literal), and list/map literals fold to their
/// values. A caller's own parameter and every non-literal expression are
/// `None`. The one `Expr → Option<Value>` walk of this vocabulary;
/// [`static_literal`] classifies its result.
pub(crate) fn static_arg_value(expr: &Expr) -> Option<Value> {
    match expr {
        Expr::Null => Some(Value::Null),
        Expr::Bool(value) => Some(Value::Bool(*value)),
        Expr::Int(value) => Some(Value::Int(*value)),
        Expr::Float(value) => Some(Value::Float(*value)),
        Expr::String(value) => Some(Value::String(value.as_str().into())),
        // An executor-side eager value: a temporal constructor call the
        // front end folded (`date('2020-01-01')`).
        Expr::RuntimeValue(value) => Some(value.clone()),
        Expr::Parameter(name) => relate_core::auto_literal(name),
        Expr::List(items) => Value::checked_list(
            items
                .iter()
                .map(static_arg_value)
                .collect::<Option<Vec<_>>>()?,
        )
        .ok(),
        Expr::Map(pairs) => {
            let map = pairs
                .iter()
                .map(|(key, value)| Some((key.clone(), static_arg_value(value)?)))
                .collect::<Option<std::collections::BTreeMap<_, _>>>()?;
            Value::checked_map(map).ok()
        }
        Expr::Unary {
            op: UnaryOp::Plus,
            expr,
        } => static_arg_value(expr),
        Expr::Unary {
            op: UnaryOp::Minus,
            expr,
        } => match static_arg_value(expr)? {
            Value::Int(value) => value.checked_neg().map(Value::Int),
            Value::Float(value) => Some(Value::Float(-value)),
            _ => None,
        },
        _ => None,
    }
}

/// The literal an expression spells, bucketed by the kind each caller tests
/// for. A collection literal is `List`/`Map` even when an element is dynamic:
/// the kind a site tests for is the collection. A sign is transparent
/// whatever its operand's kind (a nonsense `-'x'` is still a non-numeric
/// literal). The one classification of this vocabulary, shared by the
/// argument-type rules and [`crate::binding::Binding::of_static_expr`].
pub(crate) fn static_literal(expr: &Expr) -> Option<LiteralKind> {
    match expr {
        Expr::List(_) => Some(LiteralKind::List),
        Expr::Map(_) => Some(LiteralKind::Map),
        Expr::Unary {
            op: UnaryOp::Minus | UnaryOp::Plus,
            expr,
        } => static_literal(expr),
        other => static_arg_value(other).map(|value| literal_kind(&value)),
    }
}

fn literal_kind(value: &Value) -> LiteralKind {
    match value {
        Value::Null => LiteralKind::Null,
        Value::Bool(_) => LiteralKind::Bool,
        Value::Int(_) => LiteralKind::Int,
        Value::Float(_) => LiteralKind::Float,
        Value::List(_) => LiteralKind::List,
        Value::Map(_) => LiteralKind::Map,
        // Every other value is a non-numeric literal; an unknown one is
        // rejected at an integer-argument site (it fails closed).
        Value::String(_)
        | Value::Node(_)
        | Value::Relationship(_)
        | Value::Path(_)
        | Value::Temporal(_)
        | Value::Point(_)
        | Value::Uuid(_)
        | Value::Vector { .. }
        | Value::Bytes(_) => LiteralKind::Other,
    }
}

pub(crate) fn is_static_non_bool(expr: &Expr) -> bool {
    // Bool and null stay legal boolean operands.
    matches!(expr, Expr::CountStar)
        || matches!(
            static_literal(expr),
            Some(kind) if !matches!(kind, LiteralKind::Bool | LiteralKind::Null)
        )
}

/// Whether `expr` is a float literal, including a plan-cache placeholder
/// for one.
fn is_float_literal(expr: &Expr) -> bool {
    static_literal(expr) == Some(LiteralKind::Float)
}

/// ADR-0175 (statically-known floats): the integer-argument site an
/// expression carries whose literal gate is a float — a list index or a
/// slice bound — paired with the site whose authored detail a rejection
/// names. The one list of those forms, precedence included (a slice's
/// start before its end): the checker rejects through it and the
/// plan-cache walk classifies through it.
///
/// A float *literal*, sign included: the parser splits `-1.5`/`+1.5`
/// into a unary sign over the literal, and both spellings are the mistake
/// Neo4j's static FLOAT gate rejects at compile time (22N27 / 22NB1).
/// Constant float *expressions* (`0.0/0.0`, `head([1.0, 'x'])`, …) are
/// not literals and are handled at runtime, and the two sites differ
/// there: a list index rejects every float at runtime with the same text
/// this gate raises, while a slice bound's wording is plan-time-only — its
/// runtime coercion truncates floats (NaN → 0) and saturates into the
/// int32 range, never a range fault (ADR-0631).
pub(crate) fn float_integer_site(expr: &Expr) -> Option<&'static IntegerArgSite> {
    match expr {
        Expr::Subscript { index, .. } if is_float_literal(index) => Some(&integer_args::LIST_INDEX),
        Expr::Slice { start, .. } if start.as_deref().is_some_and(is_float_literal) => {
            Some(&integer_args::SLICE_START)
        }
        Expr::Slice { end, .. } if end.as_deref().is_some_and(is_float_literal) => {
            Some(&integer_args::SLICE_END)
        }
        _ => None,
    }
}

/// ADR-0175: reject a float literal at the integer site `expr` carries,
/// naming that site's authored detail ([`float_integer_site`] owns the
/// list).
pub(crate) fn reject_float_integer_site(expr: &Expr) -> Result<()> {
    match float_integer_site(expr) {
        Some(site) => Err(Error::invalid_argument_type(site.type_error)),
        None => Ok(()),
    }
}

/// Whether `expr` is a statically-known literal of a type that is not
/// INTEGER. Live Neo4j 2026.10.1 reports the integer-argument sites'
/// wrong-typed literals as type mismatches (`substring('abcdef','x')`,
/// `substring('abcdef',1,'x')`, `left('abcdef','x')`,
/// `right('abcdef','x')`, `range('a',5)`, and a temporal constructor call
/// such as `substring('abcdef', date('2020-01-01'))`, which the front end
/// folds to a `RuntimeValue`). `null` is legal (it nulls the call), an
/// integer literal stays, and a constant expression whose type is only
/// known at runtime (`head(['x','1'])`) is handled by the executor's own
/// rejection.
pub(crate) fn is_non_integer_literal(expr: &Expr) -> bool {
    matches!(
        static_literal(expr),
        Some(kind) if !matches!(kind, LiteralKind::Int | LiteralKind::Null)
    )
}

/// ADR-0175: reject a non-INTEGER literal at an integer-argument site
/// with the site's authored `InvalidArgumentType` detail — the plan-time
/// half of the type rejection whose runtime half is the executor's own
/// argument coercion.
pub(crate) fn reject_non_integer_literal(expr: &Expr, detail: &str) -> Result<()> {
    if is_non_integer_literal(expr) {
        return Err(Error::invalid_argument_type(detail));
    }
    Ok(())
}

/// The `CALL { … } IN TRANSACTIONS` modifier sites' positive-integer
/// literal gate (the batch size, the concurrency hint): a
/// negative numeric literal rejects with the site's range sentence
/// under the `InvalidArgument` token
/// ([`relate_core::invalid_argument_text`]), a non-INTEGER literal with
/// its type sentence — the plan-time half of the runtime coercion
/// whose texts the same row owns (REL8-2179).
pub(crate) fn check_positive_int_arg(expr: &Expr, site: &IntegerArgSite) -> Result<()> {
    // The parser's two spellings of a negative numeric literal are one
    // predicate, not two rejection sites (the shape `check_nonneg_int_arg`
    // uses); zero joins the negatives — these sites are positive-only.
    let negative = matches!(expr, Expr::Int(i) if *i <= 0)
        || matches!(
            expr,
            Expr::Unary {
                op: UnaryOp::Minus,
                expr,
            } if matches!(expr.as_ref(), Expr::Int(_) | Expr::Float(_))
        );
    if negative {
        return Err(Error::syntax(invalid_argument_text(range_text(site))));
    }
    // The helper owns the reject-a-non-INTEGER-literal policy; the class
    // is this caller's — the Syntax re-class the function sites don't take.
    reject_non_integer_literal(expr, site.type_error).map_err(Error::into_syntax)?;
    Ok(())
}