relate-executor 1.5.0

In-memory graph and Cypher physical executor
Documentation
//! The executor's concluding-clause verdict over the shared body-descent
//! skeleton (ADR-0740): unit-tested at the verdict level, beside the
//! behavior tests that run the same classification through the real
//! execution entry points (`call/tests.rs`'s nested-unit-call cases,
//! `conditional_tests.rs`, the integration subquery suites). Every
//! subject is built from the IR's shared `body_descent_fixtures` kit
//! (enabled through this crate's dev-dep on `relate-ir`'s `test-hooks`)
//! — the same constructors, batched call included, the IR skeleton's
//! suite builds its corpus from.

use relate_ir::body_descent_fixtures::{
    batched_call_clause, body, call_clause, create_clause, next_chain, proc_call_clause,
    return_clause, union_arms, void_proc_call_clause, when_clause,
};
use relate_ir::{Clause, Query};

use super::row_production::{body_projects_rows, parts_have_unit_output, stage_projects_rows};

#[test]
fn a_body_ending_in_a_nested_unit_call_spills_no_rows() {
    // The merge-bug shape this refinement fixed (its behavior test lives
    // in `call/tests.rs`): the verdict answers by the innermost
    // concluding clause, through unbounded nesting.
    let nested_unit = call_clause(body(vec![call_clause(body(vec![create_clause()]))]));
    assert!(!body_projects_rows(&body(vec![nested_unit])));
    let nested_projecting = call_clause(body(vec![call_clause(body(vec![return_clause()]))]));
    assert!(body_projects_rows(&body(vec![nested_projecting])));
}

#[test]
fn a_conditional_spills_rows_when_any_branch_or_the_else_does() {
    // The any-branch breadth: one projecting branch — or the ELSE alone —
    // makes the concluding conditional spill rows.
    let all_unit = when_clause(
        vec![body(vec![create_clause()]), body(vec![create_clause()])],
        Some(body(vec![create_clause()])),
    );
    assert!(!body_projects_rows(&body(vec![all_unit])));

    let one_branch = when_clause(
        vec![body(vec![create_clause()]), body(vec![return_clause()])],
        Some(body(vec![create_clause()])),
    );
    assert!(body_projects_rows(&body(vec![one_branch])));

    let else_only = when_clause(
        vec![body(vec![create_clause()]), body(vec![create_clause()])],
        Some(body(vec![return_clause()])),
    );
    assert!(body_projects_rows(&body(vec![else_only])));
}

#[test]
fn a_batched_call_spills_rows_whatever_its_body() {
    // The executor-only ruling the naming twin does not share: the
    // batched form emits its own rows (pass-through, status, pads) even
    // over a unit body.
    let batched_unit = batched_call_clause(body(vec![create_clause()]));
    assert!(body_projects_rows(&body(vec![batched_unit])));
}

#[test]
fn the_final_stage_cursor_and_any_arm_fold_carry_over_from_the_skeleton() {
    // The structure facts the verdict no longer re-spells: a NEXT chain
    // is judged by its final stage, a stage's arms by any projecting one.
    let unit_then_projecting = next_chain(create_clause(), body(vec![return_clause()]));
    assert!(body_projects_rows(&unit_then_projecting));

    let arms = union_arms(create_clause(), return_clause());
    assert!(stage_projects_rows(&arms.parts));
    let unit_arms = union_arms(create_clause(), create_clause());
    assert!(!stage_projects_rows(&unit_arms.parts));
}

#[test]
fn a_call_concluding_part_is_unit_output_for_the_next_seeding() {
    // Where the decision's unit-output answer is not the spills answer's
    // negation: `CALL { … }` is a data write in the ADR-0287 vocabulary even when its merged
    // rows are a statement result, while a projecting `RETURN` part
    // never seeds a unit row.
    let unit_output = |concluding: Clause| {
        let part = [concluding];
        parts_have_unit_output(std::iter::once(&part[..]))
    };
    assert!(unit_output(call_clause(body(vec![return_clause()]))));
    assert!(!unit_output(return_clause()));
    assert!(unit_output(create_clause()));
    // A stage concluding in a void procedure call seeds one row too; a
    // `YIELD` call's rows are the stage's own, so an empty one seeds none.
    assert!(unit_output(void_proc_call_clause()));
    assert!(!unit_output(proc_call_clause()));
}

/// The two verdicts over the shared body-descent skeleton (ADR-0740)
/// agree on the nested-body vocabulary — the structure the skeleton
/// owns — wherever the layer policies do not deliberately differ. The
/// corpus is the vocabulary validation admits (every `WHEN` branch and
/// `UNION` arm agreeing on its export shape), so a new nested-body arm
/// added to one verdict without its twin fails here; the one recorded
/// divergence is the batched call, a rows-not-columns fact the naming
/// verdict keeps answering "no columns".
#[test]
fn the_verdicts_agree_over_the_nested_body_vocabulary() {
    let ret = || body(vec![return_clause()]);
    let unit = || body(vec![create_clause()]);
    let cases: Vec<(Query, bool, bool)> = vec![
        // (body, projects_columns, spills rows)
        (body(vec![call_clause(ret())]), true, true),
        (body(vec![call_clause(unit())]), false, false),
        (
            body(vec![call_clause(body(vec![call_clause(ret())]))]),
            true,
            true,
        ),
        (
            body(vec![call_clause(body(vec![call_clause(unit())]))]),
            false,
            false,
        ),
        (body(vec![when_clause(vec![ret()], None)]), true, true),
        (body(vec![when_clause(vec![unit()], None)]), false, false),
        (
            body(vec![when_clause(vec![ret(), ret()], Some(ret()))]),
            true,
            true,
        ),
        (
            body(vec![when_clause(vec![unit(), unit()], Some(unit()))]),
            false,
            false,
        ),
        (
            body(vec![when_clause(
                vec![body(vec![call_clause(unit())])],
                None,
            )]),
            false,
            false,
        ),
        (next_chain(create_clause(), ret()), true, true),
        (union_arms(create_clause(), create_clause()), false, false),
        // A body concluding in a procedure call is unit for both: the
        // call's rows never become the body's statement result.
        (body(vec![proc_call_clause()]), false, false),
        (body(vec![void_proc_call_clause()]), false, false),
        (
            body(vec![call_clause(body(vec![proc_call_clause()]))]),
            false,
            false,
        ),
        // The one recorded divergence: a batched call spills rows (its
        // batches' own output) while staying columnless for naming.
        (body(vec![batched_call_clause(unit())]), false, true),
    ];
    for (query, projects_columns, spills) in cases {
        assert_eq!(
            query.projects_columns(),
            projects_columns,
            "naming verdict on {query:?}"
        );
        assert_eq!(
            body_projects_rows(&query),
            spills,
            "spills verdict on {query:?}"
        );
    }
}