relate-planner 1.5.0

Bind OpenCypher AST to a logical plan
Documentation
//! Variable binding: the scope environment threaded through validation and
//! the rules for introducing pattern variables into it.

use std::collections::HashMap;

use relate_core::{Error, Result};
use relate_ir::{Expr, PathPattern, PatternElement, Projection};
use relate_names::SmallName;

use crate::literal::{static_literal, LiteralKind};

/// What kind of value a name in scope is bound to. The binder tracks only
/// what later checks need: entity-ness and literal-ness for type-conflict
/// and argument-type errors.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Binding {
    Node,
    Rel,
    Path,
    Value,
    /// Statically known scalar: a non-null literal (int/bool/string/float)
    /// or a scalar-typed subquery expression (`EXISTS {…}`, `COUNT {…}`).
    /// MATCH as a node/rel is a type conflict.
    Scalar,
    /// Statically known list: a list literal or a `COLLECT {…}` result.
    /// Property access is InvalidArgumentType.
    List,
    /// Literal map. MATCH as a node/rel is a type conflict.
    Map,
}

impl Binding {
    /// Whether the binding holds a non-entity value (a scalar, list or map
    /// literal, or the unconstrained `Value`): exactly the class a
    /// subquery-expression body's pattern may shadow.
    pub(crate) fn is_non_entity(self) -> bool {
        matches!(self, Binding::Scalar | Binding::List | Binding::Map)
    }

    /// The binding an expression's static result type implies, when the
    /// expression is a literal or a typed subquery expression — the one
    /// classifier behind a projection's next-stage scope. A variable's kind
    /// comes from the environment (the caller's arm); an expression whose
    /// type is only known per row returns `None` (the caller binds `Value`).
    ///
    /// The literal kinds are [`static_literal`]'s, so this classifier and
    /// the argument-type rules cannot disagree. `EXISTS {…}` is a BOOLEAN,
    /// `COUNT {…}` an INTEGER, and `COLLECT {…}` a LIST, so reusing a
    /// projected subquery expression as a pattern element is the same type
    /// conflict a literal reuse is (live Neo4j 2026.10.1:
    /// VariableTypeConflict).
    pub(crate) fn of_static_expr(expr: &Expr) -> Option<Binding> {
        match expr {
            Expr::ExistsSubquery { .. } | Expr::CountSubquery { .. } => Some(Binding::Scalar),
            Expr::CollectSubquery { .. } => Some(Binding::List),
            // A folded temporal constructor carries no static binding kind
            // here: the projection's name stays `Value` so component access
            // on it remains legal (`WITH date('…') AS d RETURN d.year`).
            Expr::RuntimeValue(_) => None,
            expr => match static_literal(expr)? {
                LiteralKind::List => Some(Binding::List),
                LiteralKind::Map => Some(Binding::Map),
                LiteralKind::Null => None,
                LiteralKind::Bool | LiteralKind::Int | LiteralKind::Float | LiteralKind::Other => {
                    Some(Binding::Scalar)
                }
            },
        }
    }
}

/// The non-entity names of `env`: the shadowable set a subquery-expression
/// body's pattern may rebind. The one derivation of that classification —
/// [`bind`]'s shadow branch and the 42N66 rule read the same predicate.
pub(crate) fn shadowable_imports(env: &Env) -> std::collections::HashSet<String> {
    env.iter()
        .filter(|(_, binding)| binding.is_non_entity())
        .map(|(name, _)| name.clone())
        .collect()
}

/// The one undefined-variable check: `name` must already be bound in `env`.
pub(crate) fn require_bound(env: &Env, name: &SmallName) -> Result<()> {
    if env.contains_name(name) {
        Ok(())
    } else {
        Err(name.with_str(Error::undefined_variable))
    }
}

/// Whether a NEW pattern binding of `name` collides with the scope. A
/// shadowable *non-entity* import is *fresh* for a pattern binding (the
/// pattern rebinds it), so it does not count as already bound — the
/// binder's own answer, shared with the 42N66 rule so the two cannot
/// disagree. The kind gate stays here: callers hand over name sets that
/// are wider than the policy (the FOREACH scope hands over every outer
/// name), so the predicate is what keeps an entity binding unshadowable.
pub(crate) fn effectively_bound(
    env: &Env,
    shadowable: &std::collections::HashSet<String>,
    name: &SmallName,
) -> bool {
    match env.get_name(name) {
        None => false,
        Some(binding) => {
            !(name.with_str(|text| shadowable.contains(text)) && binding.is_non_entity())
        }
    }
}

pub(crate) type Env = HashMap<String, Binding>;

/// The Env's IR-name accessors: the binding environment stays String-keyed
/// (its `next_stage_scope` feeds come from the String-rendered output-name
/// computation), so IR names — `SmallName`, ADR-0509 stage 3 — reach it
/// through these four bridges. Reads are allocation-free (the inline arm
/// decodes on the stack); the insert bridge renders text per bound name,
/// exactly the allocation the pre-SmallName clone paid, and the one
/// remove bridge is the comprehension body's shadowed path variable.
/// The Env bridges, as a trait so the methods live beside the type
/// alias they serve without a wrapper struct.
pub(crate) trait EnvBridge {
    fn get_name(&self, name: &SmallName) -> Option<&Binding>;
    fn contains_name(&self, name: &SmallName) -> bool;
    /// Like [`HashMap::insert`]: the binding the name replaced, if any.
    fn insert_name(&mut self, name: SmallName, binding: Binding) -> Option<Binding>;
    /// Like [`HashMap::remove`]: the binding the name held, if any. The
    /// comprehension body's shadowed path variable is the one caller.
    fn remove_name(&mut self, name: &SmallName) -> Option<Binding>;
}

impl EnvBridge for HashMap<String, Binding> {
    fn remove_name(&mut self, name: &SmallName) -> Option<Binding> {
        name.with_str(|text| self.remove(text))
    }

    fn get_name(&self, name: &SmallName) -> Option<&Binding> {
        name.with_str(|text| self.get(text))
    }
    fn contains_name(&self, name: &SmallName) -> bool {
        name.with_str(|text| self.contains_key(text))
    }
    fn insert_name(&mut self, name: SmallName, binding: Binding) -> Option<Binding> {
        self.insert(name.to_string(), binding)
    }
}

/// The scope a `NEXT` stage starts from: the chain's inherited environment
/// and name order with the previous stage's exported columns added
/// (CIP-213 — an imported name stays visible in every statement of the
/// chain, a stage's own columns from the stage after it). Shared by the
/// planner's stage recursion and the validation driver's, so the seeding
/// rule has one spelling.
pub(crate) fn next_stage_scope(
    seed_env: &Env,
    seed_names: &[String],
    output_names: &[String],
) -> (Env, Vec<String>) {
    let mut env = seed_env.clone();
    let mut names = seed_names.to_vec();
    for name in output_names {
        env.insert(name.clone(), Binding::Value);
        if !names.contains(name) {
            names.push(name.clone());
        }
    }
    (env, names)
}

/// The bindings a projection's non-`*` items re-bind, as one scope map —
/// the projection's own scope-continuation rule, read by both projection
/// checkers: a `WITH`'s post-clause scope and the `WITH`/`RETURN`
/// order-key environment (the incoming scope extended with this map). A
/// bare-variable item (aliased or not) keeps the variable's real
/// incoming binding — the alias names that variable's value, so a key
/// reading through either spelling sees the same kind — and every other
/// item carries its static shape when it has one ([`Binding::of_static_expr`])
/// and the unconstrained `Value` otherwise.
pub(crate) fn projected_bindings(env: &Env, items: &[Projection]) -> Env {
    let mut out = Env::new();
    for (i, item) in items.iter().enumerate() {
        if matches!(item.expr, Expr::Star) {
            continue;
        }
        let binding = match &item.expr {
            // Both projection checkers run `check_expr` over their items
            // against `env` before deriving this map, so a bare-variable
            // item is bound there.
            Expr::Variable(v) => env
                .get_name(v)
                .copied()
                .expect("projection items were validated in scope"),
            other => Binding::of_static_expr(other).unwrap_or(Binding::Value),
        };
        out.insert(item.output_name(i), binding);
    }
    out
}

/// Bind one name. `shadowable` names the bindings a MATCH pattern may
/// *shadow* instead of colliding with — a subquery-expression body's
/// imported *non-entity* names, which the body's own pattern may rebind
/// (`EXISTS { (n) … }` where the enclosing scope bound `n` to a scalar).
/// CREATE/MERGE shadowing is [`bind_create_path`]'s own policy.
pub(crate) fn bind(
    env: &mut Env,
    name: &SmallName,
    kind: Binding,
    shadowable: &std::collections::HashSet<String>,
) -> Result<()> {
    // A shadowable non-entity import is a fresh binding inside this scope
    // ([`effectively_bound`]'s answer): the pattern's own declaration
    // replaces it, whichever entity kind it is (a node, a relationship, or
    // a named path). An unbound name is fresh for the same reason.
    if !effectively_bound(env, shadowable, name) {
        env.insert_name(name.clone(), kind);
        return Ok(());
    }
    let prev = env
        .get_name(name)
        .copied()
        .expect("effectively_bound is true only for a bound name");
    // A path may not rebind an existing name at all (a path is never the
    // reuse shape: its element variables were bound moments ago).
    if kind == Binding::Path {
        return Err(name.with_str(Error::duplicate_variable));
    }
    match prev {
        prev if prev == kind => Ok(()),
        // The unconstrained `Value` adopts any concrete kind.
        Binding::Value => {
            env.insert_name(name.clone(), kind);
            Ok(())
        }
        _ => Err(variable_type_conflict(name)),
    }
}

/// The one spelling of a reused-name type clash (`VariableTypeConflict`).
fn variable_type_conflict(name: &SmallName) -> Error {
    Error::syntax(format!(
        "VariableTypeConflict: `{name}` already bound to a different type"
    ))
}

pub(crate) fn bind_path(
    env: &mut Env,
    p: &PathPattern,
    shadowable: &std::collections::HashSet<String>,
) -> Result<()> {
    for (name, binding) in pattern_local_bindings(p) {
        bind(env, name, binding, shadowable)?;
    }
    Ok(())
}

/// The local variable bindings one pattern introduces, in declaration
/// order — the single classification table behind [`bind_path`]
/// (MATCH/CREATE patterns): a path name is a path, a node variable a
/// node, and a relationship variable a relationship — or a list when it is
/// var-length or inside a quantified path, whose subpath variable is a
/// list too. GPM `elements` are classified only for the pure element form
/// (legacy `nodes`/`rels` empty), the same shape `bind_path` binds.
pub(crate) fn pattern_local_bindings(p: &PathPattern) -> Vec<(&SmallName, Binding)> {
    let mut out = Vec::new();
    for name in p.path_names() {
        out.push((name, Binding::Path));
    }
    for n in &p.nodes {
        if let Some(v) = &n.variable {
            out.push((v, Binding::Node));
        }
    }
    for r in &p.rels {
        if let Some(v) = &r.variable {
            out.push((
                v,
                if r.var_length {
                    Binding::List
                } else {
                    Binding::Rel
                },
            ));
        }
    }
    if p.nodes.is_empty() && p.rels.is_empty() {
        collect_element_bindings(&p.elements, false, &mut out);
    }
    out
}

/// The `elements` half of [`pattern_local_bindings`]: a node or
/// relationship inside a quantified path (`grouped`) binds a list, and a
/// quantified subpath's own variable is a list.
fn collect_element_bindings<'a>(
    elements: &'a [PatternElement],
    grouped: bool,
    out: &mut Vec<(&'a SmallName, Binding)>,
) {
    for element in elements {
        match element {
            PatternElement::Node(node) => {
                if let Some(name) = &node.variable {
                    out.push((
                        name,
                        if grouped {
                            Binding::List
                        } else {
                            Binding::Node
                        },
                    ));
                }
            }
            PatternElement::Relationship(rel) => {
                if let Some(name) = &rel.variable {
                    out.push((
                        name,
                        if grouped || rel.var_length {
                            Binding::List
                        } else {
                            Binding::Rel
                        },
                    ));
                }
            }
            PatternElement::Quantified(qpp) => {
                collect_element_bindings(&qpp.elements, true, out);
                if let Some(name) = &qpp.subpath_variable {
                    out.push((name, Binding::List));
                }
            }
        }
    }
}

/// Declare one CREATE-introduced binding. A shadowable name — one an
/// enclosing scope bound and a FOREACH body may rebind — is always fresh;
/// otherwise a name already in scope is a collision unless the caller's
/// shape rule says it is a *reuse* of the existing binding
/// (`may_reuse`), in which case the binding is left exactly as it was.
fn declare(
    env: &mut Env,
    name: &SmallName,
    kind: Binding,
    shadowable: &std::collections::HashSet<String>,
    may_reuse: bool,
) -> Result<()> {
    if name.with_str(|text| shadowable.contains(text)) {
        env.insert_name(name.clone(), kind);
        return Ok(());
    }
    if env.contains_name(name) {
        if may_reuse {
            return Ok(());
        }
        return Err(name.with_str(Error::duplicate_variable));
    }
    env.insert_name(name.clone(), kind);
    Ok(())
}

pub(crate) fn bind_create_path(
    env: &mut Env,
    p: &PathPattern,
    shadowable: &std::collections::HashSet<String>,
) -> Result<()> {
    if let Some(v) = &p.variable {
        declare(env, v, Binding::Path, shadowable, false)?;
    }
    // A plain variable node in a rel-bearing path (`(a)-[:R]->(b)` where the
    // pattern reuses an earlier element variable) is a reuse, not a
    // redeclaration; adding a label or a property spec makes it a
    // declaration again ([`PathPattern::node_reuses_binding`]).
    for n in &p.nodes {
        if let Some(v) = &n.variable {
            declare(env, v, Binding::Node, shadowable, p.node_reuses_binding(n))?;
        }
    }
    for r in &p.rels {
        if let Some(v) = &r.variable {
            declare(
                env,
                v,
                Binding::Rel,
                shadowable,
                r.types.is_empty() && r.properties.is_none(),
            )?;
        }
    }
    Ok(())
}