use std::collections::BTreeMap;
use relate_core::{CancelToken, Error, Result};
use relate_ir::{Clause, MatchMode};
use crate::ctx::ExecCtx;
use crate::exec::test_support::{clauses_of, Builder};
use crate::graph::Graph;
use crate::metrics::KernelRung;
use crate::overlay::ExecGraph;
use crate::row::Row;
use super::super::pushdown::Residual;
use super::{matcher, run, KernelMode, KernelOutput};
fn cancelled_kernel_run(
clauses: &[Clause],
graph: &Graph,
mode: KernelMode<'_>,
expect: KernelRung,
) -> Result<KernelOutput> {
let token = CancelToken::new();
token.cancel();
let view = ExecGraph::read(graph).with_cancel(Some(&token));
let params = BTreeMap::new();
let ctx = ExecCtx::new(&view, ¶ms);
let Clause::Match { patterns, .. } = &clauses[0] else {
panic!("the test statement opens on a MATCH");
};
let contract = matcher::compile(
patterns,
MatchMode::DifferentRelationships,
&Row::new(),
&[],
)
.expect("the shape is a kernel contract");
assert_eq!(
contract.rung(),
expect,
"the shape compiles to the expected rung"
);
run(ctx, &contract, &Residual::empty(), mode)
}
fn assert_kernel_cancelled(output: Result<KernelOutput>, kernel: &str) {
match output {
Err(error) => assert!(
matches!(error, Error::Cancelled),
"{kernel}: expected the typed Error::Cancelled, got {error:?}"
),
Ok(_) => panic!("{kernel}: a cancelled statement ran to the end of the kernel"),
}
}
fn chain_fan() -> Graph {
let b = Builder::new();
let start = b.node("s", &["P"]);
let m1 = b.node("m1", &["M"]);
let m2 = b.node("m2", &["M"]);
b.rel(start, m1, "E", &[]);
b.rel(start, m2, "E", &[]);
for _ in 0..2_000 {
let leaf = b.anon(&["L"]);
b.rel(m1, leaf, "E", &[]);
b.rel(m2, leaf, "E", &[]);
}
b.done()
}
const CHAIN_CYPHER: &str = "MATCH (s:P)-[:E]->(m:M)-[:E]->(n:L) RETURN n.id AS id";
#[test]
fn the_chain_kernel_observes_a_cancel_inside_a_rows_adjacency() {
let clauses = clauses_of(CHAIN_CYPHER);
let output = cancelled_kernel_run(
&clauses,
&chain_fan(),
KernelMode::Rows { want: None },
KernelRung::Chain { hops: 2 },
);
assert_kernel_cancelled(output, "chain");
}
#[test]
fn the_chain_kernel_observes_a_cancel_inside_an_existence_scan() {
let clauses = clauses_of(CHAIN_CYPHER);
let Clause::Return { items, .. } = &clauses[1] else {
panic!("the counted expression's clause");
};
let output = cancelled_kernel_run(
&clauses,
&chain_fan(),
KernelMode::Exists {
counted: Some(&items[0].expr),
},
KernelRung::Chain { hops: 2 },
);
assert_kernel_cancelled(output, "chain-exists");
}
const HUB_CYPHER: &str = "MATCH (a:A)-[:X]->(h:Hub), (h)-[:Y]->(b:B) RETURN h";
#[test]
fn the_hub_star_kernel_observes_a_cancel_inside_its_pairing_loop() {
let b = Builder::new();
let hub = b.node("h", &["Hub"]);
for _ in 0..40 {
let a = b.anon(&["A"]);
b.rel(a, hub, "X", &[]);
}
for _ in 0..40 {
let bb = b.anon(&["B"]);
b.rel(hub, bb, "Y", &[]);
}
let graph = b.done();
let clauses = clauses_of(HUB_CYPHER);
let output = cancelled_kernel_run(
&clauses,
&graph,
KernelMode::Rows { want: None },
KernelRung::HubStar,
);
assert_kernel_cancelled(output, "hub-star");
}