relate-executor 1.5.0

In-memory graph and Cypher physical executor
Documentation
//! The top-K growth policy both SEARCH scan shapes follow: when the
//! growth loop is complete, and how the next round's window widens.
//!
//! Whether a wider fetched window can reach a *new* candidate — the
//! growth precondition — is not policy but a fact of each scan shape,
//! stated at its call site: both paths always grow (a candidate — the
//! binding a row names, or a pattern row's own binding — may sit
//! arbitrarily deep in the ranking, so a deeper fetched entry is always
//! a new possible survivor); termination is the index's (`may_widen`),
//! never the shape's.
//!
//! The SKIP/LIMIT window pages over entities the whole MATCH statement
//! keeps (the pattern, the outer WHERE, a pre-bound binding), so both
//! loops widen while enough survivors have not been seen — for every
//! pattern shape alike (ADR-0678).

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

use super::{check_binding_kind, search_spec_candidates, Growth, SearchEnv, SearchWindow};
use crate::row::Row;

/// What a scan shape's `take` decides after seeing one round's new
/// candidates: the round's own verdict on completion, so the loop never
/// re-derives the rule the shape already owns.
pub(super) enum Round {
    /// The round did not complete the window: widen and fetch again.
    NeedMore,
    /// The window is complete: stop growing.
    Done,
}

/// The fetch-and-widen skeleton both un-deferred row sources share: fetch
/// the ranked window, check the binding's kind, hand the round's *new*
/// candidates to `take`, and widen until `take` reports [`Round::Done`] or
/// the index cannot widen. A window that can keep nothing (LIMIT 0) ends
/// before any fetch. The scans always grow — the binding is seeded per
/// candidate, so a wider window can reach candidates the current one
/// cannot — and termination is the index's (`may_widen`).
///
/// Every wider round returns an identical superset prefix of the same
/// ranking (the index's contract), so the candidates an earlier round
/// already handed over are the first `processed` entries and only the
/// tail is fresh.
pub(super) fn grow_candidates(
    env: &SearchEnv<'_>,
    input: &Row,
    search: &SearchClause,
    binding_kind: Option<EntityKind>,
    window: SearchWindow,
    mut take: impl FnMut(Vec<(Value, f64)>) -> Result<Round>,
) -> Result<()> {
    if window.satisfied(0) {
        return Ok(());
    }
    let mut extra = 0usize;
    let mut processed = 0usize;
    while let Some(mut fetched) =
        search_spec_candidates(env, input, &search.spec, window, Growth::Widen(extra))?
    {
        check_binding_kind(binding_kind, fetched.kind)?;
        let seen = fetched.candidates.len();
        assert!(
            seen >= processed,
            "a wider SEARCH round returns a superset prefix of the narrower one"
        );
        let fresh = fetched.candidates.split_off(processed);
        processed = seen;
        if matches!(take(fresh)?, Round::Done) || !fetched.may_widen {
            break;
        }
        extra = widen_extra(extra, window.limit);
    }
    Ok(())
}

/// The top-K completion test, stated once for both paths: the loop ends
/// when the index cannot widen or enough candidates have a surviving
/// row. Both scans always grow (a candidate may sit arbitrarily deep in
/// the ranking), so no caller states a grows guard beside the test.
pub(super) fn top_k_complete(may_widen: bool, survivors: usize, needed: usize) -> bool {
    !may_widen || survivors >= needed
}

/// The next growth round's extra-candidate budget: geometric doubling
/// from `base`, clamped to at least 1 — the guarantee is this helper's
/// own, so no caller compensates with its own floor — so the window
/// grows toward the index's depth in logarithmic rounds.
pub(super) fn widen_extra(extra: usize, base: usize) -> usize {
    extra.saturating_add(extra).max(base.max(1))
}