relate-executor 1.3.0

In-memory graph and Cypher physical executor
Documentation
//! The chain kernel's cost gate on hops the walk model cannot price
//! (LABS-991): an untyped rel, or a hop leaving an unlabeled node, rides
//! the any-label out-degree tail instead of passing the row estimate
//! through — so a graph with a high-degree node declines the chain to
//! the streaming walker (with the walker's own answer), while a graph
//! without one keeps the kernel.

use relate_core::Value;

use crate::exec::test_support::{build, exec_cypher, Built};

use super::{take_kernel_engagements, with_kernels_disabled};

/// A five-node `:N` chain (`0 -> 1 -> 2 -> 3 -> 4`, rel type `R`), plus —
/// when `hub_leaves > 0` — a separate hub node fanning out to that many
/// leaves. The chain is the only 4-hop path, so the answer is one row
/// either way; the hub only moves the any-label out-degree tail (a
/// hub of 64+ leaves puts four unlabeled hops past the gate's 2^20
/// stage ceiling).
fn chain_with_hub(hub_leaves: usize) -> Built {
    let total = 5 + usize::from(hub_leaves > 0) + hub_leaves;
    let labels: Vec<&[&str]> = (0..total).map(|_| &["N"][..]).collect();
    let mut rels: Vec<(usize, usize, &str)> = (0..4).map(|i| (i, i + 1, "R")).collect();
    rels.extend((0..hub_leaves).map(|leaf| (5, 6 + leaf, "R")));
    build(&labels, &rels)
}

/// Every pattern the walk model leaves unpriced: untyped, typed through
/// unlabeled interior nodes, and untyped through labeled ones.
const SHAPES: [&str; 3] = [
    "(s:N {id: 0})-->()-->()-->()-->(n:N)",
    "(s:N {id: 0})-[:R]->()-[:R]->()-[:R]->()-[:R]->(n:N)",
    "(s:N {id: 0})-->(:N)-->(:N)-->(:N)-->(n:N)",
];

/// The rows-kernel statement over `pattern`: a `LIMIT` keeps the count
/// fold (LABS-1293) out — a plain `RETURN count` rides the fold, which
/// never lands the last stage and so prices one hop fewer — so the rows
/// kernel's gate is what the two tests pin.
fn rows_statement(pattern: &str) -> String {
    format!("MATCH {pattern} RETURN count(n) AS c LIMIT 5")
}

#[test]
fn a_high_degree_node_declines_the_unpriced_chain_to_the_walker() {
    let built = chain_with_hub(64);
    for pattern in SHAPES {
        let cypher = rows_statement(pattern);
        let _ = take_kernel_engagements();
        let answer = exec_cypher(&built.graph, &cypher).rows;
        assert_eq!(take_kernel_engagements(), 0, "declined: {cypher}");
        assert_eq!(answer, vec![vec![Value::Int(1)]], "{cypher}");
        let walker = with_kernels_disabled(|| exec_cypher(&built.graph, &cypher).rows);
        assert_eq!(answer, walker, "{cypher}");
    }
}

#[test]
fn a_thin_graph_keeps_the_kernel_on_the_unpriced_chain() {
    // No node out-degree above 1: the any-label tail is 1, the bound
    // never grows, and the kernel stays engaged.
    let built = chain_with_hub(0);
    for pattern in SHAPES {
        let cypher = rows_statement(pattern);
        let _ = take_kernel_engagements();
        let answer = exec_cypher(&built.graph, &cypher).rows;
        assert_eq!(take_kernel_engagements(), 1, "engaged: {cypher}");
        assert_eq!(answer, vec![vec![Value::Int(1)]], "{cypher}");
    }
}