use relate_core::Value;
use crate::metrics::KernelRung;
use super::super::test_support::{line_graph, profiled, served, Builder};
use super::frontier_rider_tests::ring_graph;
fn three_hop_corridor() -> crate::graph::Graph {
let b = Builder::new();
let a1 = b.prop_node("a1", &["Alpha"], &[("k", Value::Int(1))]);
let a2 = b.prop_node("a2", &["Alpha"], &[("k", Value::Int(2))]);
let b1 = b.node("b1", &["Beta"]);
let b2 = b.node("b2", &["Beta"]);
let g1 = b.node("g1", &["Gamma"]);
let g2 = b.node("g2", &["Gamma"]);
let d1 = b.prop_node("d1", &["Delta"], &[("name", Value::String("one".into()))]);
let d2 = b.prop_node("d2", &["Delta"], &[("name", Value::String("two".into()))]);
b.rel(a1, b1, "R", &[]);
b.rel(a2, b2, "R", &[]);
b.rel(b1, g1, "S", &[]);
b.rel(b2, g2, "S", &[]);
b.rel(g1, d1, "T", &[]);
b.rel(g2, d2, "T", &[]);
b.done()
}
#[test]
fn profile_names_the_chain_kernel_for_a_fixed_chain() {
let graph = three_hop_corridor();
let records = profiled(&graph, relate_test_support::plan_rung::THREE_HOP_MATCH);
assert_eq!(
served(&records).kernel,
Some(KernelRung::Chain { hops: 3 }),
"the fixed 3-hop chain runs the chain kernel"
);
}
#[test]
fn profile_names_the_frontier_rung_for_a_varlen_pattern() {
let graph = ring_graph();
let records = profiled(
&graph,
"MATCH (x:Geo {id:'n0'})-[:LINK*]-(y:Geo) RETURN count(DISTINCT y.id) AS c",
);
assert_eq!(
served(&records).kernel,
Some(KernelRung::FrontierNfa),
"the unbounded distinct-endpoint shape runs the frontier rung"
);
}
#[test]
fn profile_names_the_frontier_rung_for_a_multi_row_clause() {
let graph = ring_graph();
let records = profiled(
&graph,
"MATCH (x:Geo) WITH x LIMIT 2 \
MATCH (x)-[:LINK*]-(y:Geo) RETURN count(DISTINCT y.id) AS c",
);
assert_eq!(
served(&records).kernel,
Some(KernelRung::FrontierNfa),
"a two-row driving table is served per row by the frontier rung"
);
}
#[test]
fn profile_names_the_walker_for_a_declined_pattern() {
let graph = line_graph();
let records = profiled(&graph, "MATCH (a:Person)-->(b:Person) RETURN b");
assert_eq!(served(&records).kernel, Some(KernelRung::Walker));
assert_eq!(
records
.iter()
.filter(|record| record.kernel.is_some())
.count(),
1,
"only the MATCH clause carries a rung"
);
}
#[test]
fn an_untyped_quantified_hop_reports_the_frontier() {
let graph = line_graph();
let records = profiled(
&graph,
"MATCH (a:Person)-[*1..]-(b:Person) WHERE a <> b \
WITH DISTINCT a, b RETURN count(*) AS c",
);
assert_eq!(
served(&records).kernel,
Some(KernelRung::FrontierNfa),
"an untyped quantified hop compiles as the any-type set"
);
}
#[test]
fn a_nested_match_rung_does_not_leak_onto_the_enclosing_clause() {
let graph = three_hop_corridor();
let chain = "MATCH (a:Alpha)-[:R]->(b:Beta)-[:S]->(g:Gamma)-[:T]->(d:Delta)";
for cypher in [
format!(
"CALL {{ {chain} WHERE a.k >= 1 AND d.name CONTAINS 'o' \
RETURN a.k AS ak, d.name AS dn LIMIT 99 }} RETURN ak LIMIT 10"
),
format!("RETURN EXISTS {{ {chain} }} AS hit"),
] {
let records = profiled(&graph, &cypher);
let rungs: Vec<_> = records.iter().map(|record| record.kernel).collect();
assert!(
rungs.iter().all(Option::is_none),
"no clause reports the nested MATCH's rung for {cypher}: {rungs:?}"
);
}
}
fn r_chain() -> crate::graph::Graph {
let b = Builder::new();
let n: Vec<_> = (0..40)
.map(|i| b.node(&format!("c{i}"), &["Chain"]))
.collect();
for i in 0..39 {
b.rel(n[i], n[i + 1], "R", &[]);
}
b.done()
}
fn rung_and_count(graph: &crate::graph::Graph, cypher: &str) -> (Option<KernelRung>, Value) {
let records = profiled(graph, cypher);
let rung = served(&records).kernel;
let result = super::super::test_support::exec_cypher(graph, cypher);
(rung, result.rows[0][0].clone())
}
#[test]
fn count_distinct_end_over_an_acyclic_bounded_path_stays_on_the_walker() {
let (rung, count) = rung_and_count(
&r_chain(),
"MATCH (h)-[:R*1..2]->(e) RETURN count(DISTINCT e) AS c",
);
assert_eq!(rung, Some(KernelRung::Walker));
assert_eq!(count, Value::Int(39));
}
#[test]
fn path_needing_shapes_stay_on_the_walker() {
let graph = r_chain();
for (cypher, expected) in [
(
"MATCH p = (h)-[:R*1..2]->(e) RETURN count(DISTINCT p) AS c",
77,
),
("MATCH (h)-[:R*1..2]->(e) RETURN count(*) AS c", 77),
(
"MATCH (h)-[r:R*1..2]->(e) RETURN count(DISTINCT r) AS c",
77,
),
(
"MATCH p = (h)-[:R*1..2]->(e) WHERE length(p) = 2 \
RETURN count(DISTINCT e) AS c",
38,
),
] {
let (rung, count) = rung_and_count(&graph, cypher);
assert_eq!(rung, Some(KernelRung::Walker), "{cypher}");
assert_eq!(count, Value::Int(expected), "{cypher}");
}
}