relate-executor 1.5.0

In-memory graph and Cypher physical executor
Documentation
//! Composable `ExpandInto` for bound-both-ends 1-hop relationship expansion.
//!
//! When both ends of a fixed 1-hop relationship are row-bound, `expand_into_hop`
//! probes the smaller-degree side (flipping the scan direction when the target has
//! smaller typed degree) and verifies connectivity in O(1) via relationship endpoints
//! before property evaluation or row cloning.

use relate_core::{Error, NodeId, Result};
use relate_ir::Direction;

use crate::pattern_walk::{commit_trail, RelHop, UsedRels};
use crate::row::Row;

use super::expand::{admit_candidate, PathMatch};

/// Expand a fixed 1-hop relationship between two pre-bound nodes (`from` and `target`).
///
/// Probes the smaller-degree side, checks relationship endpoints in O(1), and runs
/// the canonical `admit_candidate` with `from` and `dir` so relationship properties,
/// filters, `used` rel tracking, and `WalkTallies` behave identically to standard
/// expansion.
#[allow(clippy::too_many_arguments)]
pub(super) fn expand_into_hop(
    pm: &PathMatch<'_, '_>,
    rel_idx: usize,
    from: NodeId,
    target: NodeId,
    dir: Direction,
    row: Row,
    used: UsedRels,
    descend: &mut dyn FnMut(Row, UsedRels) -> Result<bool>,
) -> Result<()> {
    let PathMatch {
        graph,
        path,
        params,
        ..
    } = *pm;
    let rp = &path.rels[rel_idx];
    let filter = &pm.rel_filters[rel_idx];
    let types = &filter.types;

    let from_deg = graph.typed_adj_len_resolved(from, dir, types);
    let to_deg = graph.typed_adj_len_resolved(target, dir.flip(), types);

    let (anchor, scan_dir, reversed) = if to_deg < from_deg {
        (target, dir.flip(), true)
    } else {
        (from, dir, false)
    };

    pm.tallies.expand_into(reversed);

    let rel_rung = &pm.ladder.rel[rel_idx];
    let hop = RelHop {
        pattern: rp,
        filter,
    };

    graph.try_find_neighbor_resolved::<Error>(anchor, scan_dir, types, |rid| {
        graph.cancel_tick()?;

        // Fast O(1) endpoint check before property evaluation or Row/UsedRels cloning.
        if graph.hop(from, rid, dir) != Some(target) {
            return Ok(false);
        }

        // Canonical per-hop admission: evaluates edge predicates (type, properties),
        // checks used set, and increments walk tallies. Always called with `from` and
        // the forward traversal `dir`.
        let Some(_next) = admit_candidate(
            graph,
            &row,
            params,
            hop,
            rel_idx,
            dir,
            &used,
            &[],
            from,
            rid,
            pm.tallies,
        )?
        else {
            return Ok(false);
        };

        let mut row2 = row.clone();
        let mut used2 = used.clone();
        if !commit_trail(
            &mut row2,
            &mut used2,
            rp,
            std::slice::from_ref(&rid),
            graph,
            rel_idx,
            path.has_path_name(),
            rel_rung,
        ) {
            return Ok(false);
        }

        descend(row2, used2)
    })?;

    Ok(())
}