relate-executor 1.5.0

In-memory graph and Cypher physical executor
Documentation
//! The un-deferred SEARCH scans: the source reads none of the MATCH's pattern
//! variables, so each candidate seeds the binding and the pattern matches
//! per candidate. Two row sources share the module: the ranked-window scan
//! ([`direct_search_rows`]) and the pre-bound row's single-entity
//! membership question ([`pre_bound_search_rows`]).

use relate_core::{Result, Value};

use super::growth::{grow_candidates, Round};
use super::{attach_score, spec_window, RowSourceCtx, SearchEnv};
use crate::exec::match_clause::match_clause;
use crate::row::Row;

/// Seed the binding with one candidate and match the pattern against it:
/// the one per-candidate step both row sources share.
fn match_candidate(
    env: &SearchEnv<'_>,
    input: &Row,
    source: &RowSourceCtx<'_>,
    (candidate, score): (Value, f64),
) -> Result<Vec<Row>> {
    let search = source.search;
    let mut seeded = input.clone();
    seeded.insert(search.binding_var.to_string(), candidate);
    attach_score(&mut seeded, search.score_var.as_ref(), Value::Float(score));
    match_clause(
        env.ctx,
        vec![seeded],
        false,
        source.clause.match_mode,
        source.clause.patterns,
        source.clause.where_clause,
        None,
        None,
        None,
    )
}

/// The un-deferred shape: the source specification reads none of this
/// MATCH's pattern variables, so a candidate seeds the binding variable
/// and the pattern matches per candidate. The input row's binding is
/// unbound; a pre-bound one is [`pre_bound_search_rows`]'s.
///
/// The window rule (ADR-0678, one rule for every pattern shape): SKIP and
/// LIMIT page over the *entities* that satisfy the whole MATCH statement —
/// the pattern and the outer WHERE. The loop widens the fetched window
/// (geometrically, until the index is exhausted) while too few entities
/// have survived, so a WHERE-passing candidate ranked below the raw top-K
/// is returned, and the K counts survivors, never raw hits. A constraint
/// the index definition already implies filters no index member, so for
/// such patterns the first window already carries LIMIT survivors and
/// nothing widens.
///
/// Every pattern row of a kept entity rides along: the LIMIT counts
/// entities, not rows.
pub(super) fn direct_search_rows(
    env: &SearchEnv<'_>,
    input: &Row,
    source: &RowSourceCtx<'_>,
) -> Result<Vec<Row>> {
    let search = source.search;
    // The clause's window, stated once for this input row (the window
    // expressions never read a pattern binding — the front rejects that
    // — so one statement serves every growth round; see `spec_window`).
    let window = spec_window(env, input, &search.spec)?;
    // The SKIP window's consumed survivors, and the kept ones — both
    // cumulative across growth rounds.
    let mut out = Vec::new();
    let mut skipped = 0usize;
    let mut survivors = 0usize;
    grow_candidates(
        env,
        input,
        search,
        source.binding_kind,
        window,
        |candidates| {
            for candidate in candidates {
                let matched = match_candidate(env, input, source, candidate)?;
                if matched.is_empty() {
                    continue;
                }
                // The candidate survives the whole MATCH: the SKIP window
                // consumes its leading survivors, the LIMIT keeps the rest.
                if skipped < window.skip {
                    skipped += 1;
                    continue;
                }
                out.extend(matched);
                survivors += 1;
                if window.satisfied(survivors) {
                    break;
                }
            }
            Ok(if window.satisfied(survivors) {
                Round::Done
            } else {
                Round::NeedMore
            })
        },
    )?;
    Ok(out)
}

/// The pre-bound shape: the input row already binds the SEARCH variable
/// (`MATCH (p) SEARCH p IN (VECTOR ...) WITH p MATCH (p) SEARCH p IN
/// (FULLTEXT ...)`), so the clause asks one single-entity membership
/// question — is the pre-bound entity among the index's ranked
/// candidates, and does it survive the MATCH — instead of paging a
/// ranking. A different candidate is never seeded over the binding (live
/// Neo4j 2026.09.0 intersects the two result sets). The window rule
/// (ADR-0678) reduces to a window of one: the pre-bound value is the
/// only entity that can survive, however deep it sits (live 2026.10.1:
/// one row per incoming row), so a SKIP window consumes it and a LIMIT-0
/// window keeps nothing — both answer empty without fetching — and a
/// pre-bound null (an OPTIONAL MATCH that matched nothing) joins with no
/// candidate at all.
pub(super) fn pre_bound_search_rows(
    env: &SearchEnv<'_>,
    input: &Row,
    source: &RowSourceCtx<'_>,
) -> Result<Vec<Row>> {
    let search = source.search;
    let bound = input
        .get_name(&search.binding_var)
        .expect("the pre-bound source is picked only for a row that binds the SEARCH variable");
    if bound.is_null() {
        return Ok(Vec::new());
    }
    let window = spec_window(env, input, &search.spec)?;
    if window.empty_for_one_entity() {
        return Ok(Vec::new());
    }
    // The window of one: the pre-bound entity's single verdict completes it.
    let mut verdict = Vec::new();
    grow_candidates(
        env,
        input,
        search,
        source.binding_kind,
        window,
        |candidates| {
            // An entity appears once in its own ranking (a wider window only
            // appends lower-ranked entities), so its candidate is the row's
            // one verdict: kept rows or none, never another round.
            let Some(candidate) = candidates
                .into_iter()
                .find(|(candidate, _)| bound.eq_non_null(candidate))
            else {
                return Ok(Round::NeedMore);
            };
            verdict = match_candidate(env, input, source, candidate)?;
            Ok(Round::Done)
        },
    )?;
    Ok(verdict)
}