relate-executor 1.1.0

In-memory graph and Cypher physical executor
Documentation
//! The deferred SEARCH scan: the source reads a variable this MATCH binds,
//! so the pattern matches first and each row's binding is filtered against
//! its own candidate list.

use std::collections::{HashMap, HashSet};

use relate_core::{Result, Value};
use relate_ir::{Expr, MatchMode, PathPattern, SearchClause};
use relate_model::EntityKind;

use super::growth::{top_k_complete, top_k_grows, widen_extra};
use super::{
    attach_score, candidate_entity_id, check_binding_kind, search_spec_candidates, SearchEnv,
};
use crate::eval::where_clause_holds;
use crate::exec::candidates::ranked_better_cmp;
use crate::exec::match_clause::match_clause;
use crate::row::Row;

/// The deferred shape: matching the pattern first and filtering each row's
/// binding against its own candidate list. The same complex-pattern top-K
/// rule applies — the LIMIT counts distinct candidates that have a row — so
/// a window with too few surviving candidates is widened, and the LIMIT
/// keeps the best-ranked survivors (every row of a kept candidate rides
/// along).
pub(super) fn deferred_search_rows(
    env: &SearchEnv<'_>,
    input: &Row,
    binding_kind: Option<EntityKind>,
    match_mode: MatchMode,
    patterns: &[PathPattern],
    where_clause: Option<&Expr>,
    search: &SearchClause,
) -> Result<Vec<Row>> {
    let ctx = env.ctx;
    // The pattern matches WITHOUT the MATCH's outer WHERE: each row's
    // binding and score are resolved from its own candidate list and the
    // score attached before the WHERE runs — the direct path's order of
    // operations, so a WHERE naming the SCORE AS variable behaves the
    // same in both paths.
    let matched = match_clause(
        ctx,
        vec![input.clone()],
        false,
        match_mode,
        patterns,
        None,
        None,
        None,
        None,
    )?;
    // The rows of a pre-bound binding already carry their only possible
    // candidate, so no wider window can add a survivor ([`top_k_grows`]).
    let binding_prebound = input.get_name(&search.binding_var).is_some();
    let grows = top_k_grows(search, !binding_prebound);
    let mut extra = 0usize;
    // Per matched row, the outer WHERE's verdict (cached at the row's first
    // in-window resolution: the binding's rank and score in its own
    // candidate list are window-invariant -- a wider window only appends
    // lower-ranked candidates -- so the verdict cannot change between
    // growth rounds) and the terminal round's binding and score for the
    // emitted rows. One state per row, not parallel vectors.
    let mut states = vec![RowState::default(); matched.len()];
    loop {
        let mut round = DeferredRound::default();
        for (row, state) in matched.iter().zip(states.iter_mut()) {
            // One binding entry per matched row, by construction.
            let Some((id, rank, score)) =
                deferred_row_binding(env, row, extra, binding_kind, search, &mut round)?
            else {
                continue;
            };
            let where_ok = match state.where_ok {
                Some(verdict) => verdict,
                None => {
                    let mut scored = row.clone();
                    attach_score(&mut scored, search.score_var.as_ref(), Value::Float(score));
                    // The canonical optional-WHERE gate (stellar round 2,
                    // F-2-22): the one spelling every other optional-WHERE
                    // site reads -- a non-boolean predicate is the typed
                    // rejection, never a silent drop.
                    let verdict = where_clause_holds(ctx.graph, &scored, where_clause, ctx.params)?;
                    state.where_ok = Some(verdict);
                    verdict
                }
            };
            state.resolved = Some((id, score));
            // Only a WHERE-passing row makes its candidate a survivor: the
            // LIMIT counts candidates with a surviving row, as the direct
            // path's per-candidate MATCH ... WHERE does.
            if where_ok {
                round.survive(id, rank, score);
            }
        }
        let needed = round.limit.unwrap_or(usize::MAX);
        if !top_k_complete(grows, round.may_widen, round.survivors.len(), needed) {
            extra = widen_extra(extra, needed);
            continue;
        }
        // The clause's LIMIT keeps the best-ranked survivors -- the one
        // canonical CIP-225 candidate order ([`ranked_better_cmp`] over
        // score and id), the same order the candidate lists carry.
        let mut kept: Vec<(i64, f64)> = round
            .survivors
            .iter()
            .map(|(id, (_rank, score))| (*id, *score))
            .collect();
        kept.sort_by(|a, b| ranked_better_cmp(b.1, b.0, a.1, a.0));
        kept.truncate(needed);
        let kept: HashSet<i64> = kept.into_iter().map(|(id, _)| id).collect();
        // The matched rows are consumed, not cloned: each emitted row is
        // moved out and carries the score its resolution recorded. A row
        // rides along when its WHERE verdict is true and its candidate is
        // among the kept survivors.
        let mut out = Vec::with_capacity(matched.len());
        for (state, mut row) in states.into_iter().zip(matched) {
            if state.where_ok != Some(true) {
                continue;
            }
            let Some((id, score)) = state.resolved else {
                continue;
            };
            if !kept.contains(&id) {
                continue;
            }
            attach_score(&mut row, search.score_var.as_ref(), Value::Float(score));
            out.push(row);
        }
        return Ok(out);
    }
}

/// One deferred growth round's accumulator: the clause LIMIT (evaluated
/// once — the LIMIT expression reads the input row, never a variable the
/// MATCH's pattern newly binds, enforced at validation, so every matched
/// row of one input row yields the same value; `None` until the first
/// row states it, and an absent combined LIMIT stays `usize::MAX`
/// without colliding with the sentinel), whether any row's scan could
/// widen, and each WHERE-surviving candidate's best `(rank, score)` pair —
/// replaced whole, so the score always travels with the ranking that
/// kept it.
#[derive(Default)]
struct DeferredRound {
    limit: Option<usize>,
    may_widen: bool,
    survivors: HashMap<i64, (usize, f64)>,
}

impl DeferredRound {
    /// Record a WHERE-passing row's candidate, keeping its best (lowest)
    /// rank with the score that ranking carries.
    fn survive(&mut self, id: i64, rank: usize, score: f64) {
        self.survivors
            .entry(id)
            .and_modify(|slot| {
                if rank < slot.0 {
                    *slot = (rank, score);
                }
            })
            .or_insert((rank, score));
    }
}

/// One matched row's cached deferred state across growth rounds.
#[derive(Clone, Default)]
struct RowState {
    where_ok: Option<bool>,
    resolved: Option<(i64, f64)>,
}

/// One matched row's deferred binding: the row's own candidate list
/// decides whether its binding is a survivor, and at which rank and
/// score. Exactly one value per row, by construction; the caller
/// decides survival once the row's WHERE verdict is known.
fn deferred_row_binding(
    env: &SearchEnv<'_>,
    row: &Row,
    extra: usize,
    binding_kind: Option<EntityKind>,
    search: &SearchClause,
    round: &mut DeferredRound,
) -> Result<Option<(i64, usize, f64)>> {
    let Some(fetched) = search_spec_candidates(env, row, &search.spec, extra)? else {
        return Ok(None);
    };
    check_binding_kind(binding_kind, fetched.kind)?;
    if round.limit.is_none() {
        // The first fetched row states the clause LIMIT for the whole
        // round (every row's is the same; see DeferredRound).
        round.limit = Some(fetched.limit);
    }
    round.may_widen |= fetched.may_widen;
    let Some(bound) = row.get_name(&search.binding_var) else {
        return Ok(None);
    };
    let Some(rank) = fetched
        .candidates
        .iter()
        .position(|(candidate, _)| bound.eq_non_null(candidate))
    else {
        return Ok(None);
    };
    let id = candidate_entity_id(bound)?;
    let score = fetched.candidates[rank].1;
    Ok(Some((id, rank, score)))
}