relate-executor 1.5.0

In-memory graph and Cypher physical executor
Documentation
//! The chain count fold's differential suite (LABS-1293): every statement
//! runs through the differential core — the kernel path and the kernels-
//! stood-down walker must answer identically, and the statement asserts
//! its own engagement — over one adversarial multigraph: self-loops at
//! the start, middle and end, parallel same-type and cross-type edges,
//! reciprocal pairs, a hub, unlabelled nodes.
//!
//! The engagement each statement expects is stated per class below, and
//! every class names the gate that decides it — so a silent decline, or a
//! rung quietly taking a shape it must not, fails one assertion instead
//! of hiding inside an aggregate.

use relate_core::Value;

use super::super::kernels::matcher::MAX_UNROLL_HOPS;
use super::{adversarial_graph, differential, ExpectedEngagement};

/// One step spelling as the sweep states it, with the engagement its
/// queries assert: the single-hop form (repeated `k` times) and the
/// starred form (with `{k}` where the hop count goes) — both stated,
/// never derived from one another — plus whether the count fold unrolls
/// the starred form, and the first `k` at which the spelled form engages
/// a kernel (`None` when no `k` does; a two-hop count rides the path
/// aggregate fold, which is no kernel — except a directed same-way
/// two-hop count, which the two-hop tally fold (REL8-2214) takes).
struct Step {
    hop: &'static str,
    star: &'static str,
    star_engages: ExpectedEngagement,
    spelled_engages_from: Option<usize>,
    /// Whether a two-hop chain of this step is the two-hop tally fold's
    /// shape (directed, same-way, a static type list): the one fold with
    /// no cost gate, so it engages from a bare head to a bare tail too.
    tally_shape: bool,
}

/// The directed single-hop steps — the family the count fold's own gates
/// accept (a static type list or none, and a directed hop whose trail
/// corrections the tally does not need). Spelled out, they engage from
/// two hops, where the two-hop tally fold (REL8-2214) takes the directed
/// same-way count before the path aggregate fold sees it, and spelled
/// longer chains by the chain count.
const DIRECTED: [Step; 4] = [
    Step {
        hop: "-[:F]->",
        star: "-[:F*{k}]->",
        star_engages: ExpectedEngagement::Engaged,
        spelled_engages_from: Some(2),
        tally_shape: true,
    },
    Step {
        hop: "<-[:F]-",
        star: "<-[:F*{k}]-",
        star_engages: ExpectedEngagement::Engaged,
        spelled_engages_from: Some(2),
        tally_shape: true,
    },
    Step {
        hop: "-[:G]->",
        star: "-[:G*{k}]->",
        star_engages: ExpectedEngagement::Engaged,
        spelled_engages_from: Some(2),
        tally_shape: true,
    },
    Step {
        hop: "-->",
        star: "-[*{k}]->",
        star_engages: ExpectedEngagement::Engaged,
        spelled_engages_from: Some(2),
        tally_shape: true,
    },
];

/// An undirected hop: the count fold tallies it like a directed one — the
/// shared expansion's lineage check already settles a rel met from both
/// ends (the self-loops and reciprocal pairs the corpus carries), so the
/// differential ties it to the walker (LABS-1384).
const UNDIRECTED: Step = Step {
    hop: "-[:F]-",
    star: "-[:F*{k}]-",
    star_engages: ExpectedEngagement::Engaged,
    spelled_engages_from: Some(3),
    tally_shape: false,
};

/// A disjunctive type list: not the static conjunction the compile's type
/// proof requires, so every rung declines it, spelled or starred.
const DISJUNCTIVE: Step = Step {
    hop: "-[:F|G]->",
    star: "-[:F|G*{k}]->",
    star_engages: ExpectedEngagement::Declined,
    spelled_engages_from: None,
    tally_shape: false,
};

/// Every step class the sweep spells, one population.
fn all_steps() -> impl Iterator<Item = &'static Step> {
    DIRECTED.iter().chain([&UNDIRECTED, &DISJUNCTIVE])
}

const LABELLED_HEADS: [&str; 2] = ["(a:U {id:'a'})", "(a:U)"];
const BARE_HEAD: &str = "(a)";
const BARE_TAIL: &str = "(b)";
const LABELLED_TAILS: [&str; 2] = ["(b:U)", "(b:Hub)"];

fn spelled(head: &str, tail: &str, step: &Step, k: usize) -> String {
    format!(
        "MATCH {head}{}{tail} RETURN count(b)",
        vec![step.hop; k].join("()")
    )
}

fn starred(head: &str, tail: &str, step: &Step, k: usize) -> String {
    format!(
        "MATCH {head}{}{tail} RETURN count(b)",
        step.star.replace("{k}", &k.to_string())
    )
}

impl Step {
    /// Whether a chain of `k` hops engages a kernel — the one engagement
    /// policy. The two-hop tally fold carries no cost gate, so a directed
    /// same-way two-hop count engages whatever the scan; otherwise a
    /// `bare_scan` (an unlabelled head and tail) is declined outright by
    /// the cost gate, and any other chain engages from the step's stated
    /// first `k`.
    fn engages(&self, k: usize, bare_scan: bool) -> ExpectedEngagement {
        let engaged = if k == 2 && self.tally_shape {
            true
        } else {
            !bare_scan && self.spelled_engages_from.is_some_and(|from| k >= from)
        };
        if engaged {
            ExpectedEngagement::Engaged
        } else {
            ExpectedEngagement::Declined
        }
    }
}

#[test]
fn chain_count_matches_the_walker_on_adversarial_shapes() {
    let g = adversarial_graph();
    let mut nonzero_answers = 0usize;
    // One sweep over every step class: each statement asserts its own
    // engagement from the step's stated policy, and the two spellings of
    // one step denote the same query, so their answers tie — the tie is
    // what binds each `Step`'s `hop` and `star` to one edge
    // specification.
    for head in LABELLED_HEADS {
        for tail in std::iter::once(BARE_TAIL).chain(LABELLED_TAILS) {
            for step in all_steps() {
                for k in 2..=MAX_UNROLL_HOPS.min(6) {
                    let starred_rows =
                        differential(&g, &starred(head, tail, step, k), step.star_engages).rows;
                    nonzero_answers += usize::from(
                        step.star_engages == ExpectedEngagement::Engaged
                            && starred_rows != vec![vec![Value::Int(0)]],
                    );
                    let spelled_query = spelled(head, tail, step, k);
                    let spelled_rows =
                        differential(&g, &spelled_query, step.engages(k, false)).rows;
                    assert_eq!(spelled_rows, starred_rows, "{spelled_query}");
                }
            }
        }
    }
    // A bare scan head with a labelled tail: the cost gate's estimate
    // from an unlabelled start still prices the spelled chain by the
    // step's own policy — the labelled tail re-prices it under the stage
    // ceiling — so the expectation stays the step's, derived, never
    // re-stated.
    for tail in LABELLED_TAILS {
        for step in all_steps() {
            for k in 2..=MAX_UNROLL_HOPS.min(6) {
                let engages = step.engages(k, false);
                differential(&g, &spelled(BARE_HEAD, tail, step, k), engages);
            }
        }
    }
    // A bare scan head with a bare tail: the cost gate's estimate from an
    // unlabelled start declines every chain outright — the block's one
    // fact, stated once here, overriding the step policies above.
    // The one exception is the two-hop tally fold, which carries no cost
    // gate (its memory is the distinct middle nodes, not the stages).
    for step in all_steps() {
        for k in 2..=MAX_UNROLL_HOPS.min(6) {
            let engages = step.engages(k, true);
            differential(&g, &starred(BARE_HEAD, BARE_TAIL, step, k), engages);
            differential(&g, &spelled(BARE_HEAD, BARE_TAIL, step, k), engages);
        }
    }
    // The sweep is not vacuous: the engaging shapes answer over real paths.
    assert!(nonzero_answers > 0, "the sweep never answered nonzero");
}

#[test]
fn chain_count_engages_on_count_star() {
    // The one engaging shape outside the sweep's `count(<var>)`
    // vocabulary.
    let g = adversarial_graph();
    let q = "MATCH (a:U {id:'a'})-[:F*2]->(b) RETURN count(*)";
    let result = differential(&g, q, ExpectedEngagement::Engaged);
    assert_ne!(result.rows, vec![vec![Value::Int(0)]], "vacuous: {q}");
}

#[test]
fn chain_count_declines_what_it_cannot_prove() {
    let g = adversarial_graph();
    for q in [
        // ranged and named spellings keep the walker
        "MATCH (a:U {id:'a'})-[:F*2..3]->(b) RETURN count(b)",
        "MATCH (a:U {id:'a'})-[r:F*3]->(b) RETURN count(b)",
        // a zero-length hop has no fixed-hop rendering: unrolling it
        // would drop its end node, so the spelling keeps the walker
        "MATCH (a:U {id:'a'})-[:F*0]->(b) RETURN count(b)",
        "MATCH (a:U {id:'a'})-[:F*0]->(m:U)-[:G]->()-[:H]->(b) RETURN count(b)",
    ] {
        differential(&g, q, ExpectedEngagement::Declined);
    }
}

#[test]
fn chain_count_engages_five_and_six_hops() {
    let g = adversarial_graph();
    for q in [
        "MATCH (a:U {id:'a'})-[:F*5]->(b) RETURN count(b)",
        "MATCH (a:U {id:'a'})-[:F]->(b)-[:G*4]->(c) RETURN count(c)",
        "MATCH (a:U {id:'a'})-[:F*6]->(b) RETURN count(b)",
    ] {
        differential(&g, q, ExpectedEngagement::Engaged);
    }
}