use relate_core::Value;
use super::early::{take_early_evals, with_early_predicates_disabled};
use super::qpp_hops;
use crate::exec::test_support::{exec_cypher, try_exec_cypher, Builder};
use crate::graph::Graph;
fn mid_gate_graph() -> Graph {
let b = Builder::new();
let s = b.node("s", &["Geo"]);
let m1 = b.prop_node("m1", &["Geo"], &[("keep", Value::Bool(false))]);
let m2 = b.prop_node("m2", &["Geo"], &[("keep", Value::Bool(true))]);
let t = b.node("t", &["Geo"]);
let u1 = b.node("u1", &["Geo"]);
let u2 = b.node("u2", &["Geo"]);
b.rel(s, m1, "ROAD", &[]);
b.rel(s, m2, "ROAD", &[]);
b.rel(m1, u1, "LINK", &[]);
b.rel(u1, t, "LINK", &[]);
b.rel(m1, u2, "LINK", &[]);
b.rel(u2, t, "LINK", &[]);
b.rel(m2, t, "LINK", &[]);
b.done()
}
fn mid_gate(filter: &str, tail: &str) -> String {
format!(
"MATCH (s:Geo {{id:'s'}})-[:ROAD]->(m:Geo)-[:LINK*1..2]->(t:Geo) {filter} {tail} \
RETURN s.id AS s, m.id AS m, t.id AS t ORDER BY s, m, t"
)
}
fn early_query() -> String {
mid_gate("WHERE m.keep = true", "")
}
fn postfilter_query() -> String {
"MATCH (s:Geo {id:'s'})-[:ROAD]->(m:Geo)-[:LINK*1..2]->(t:Geo) \
WITH s, m, t WHERE m.keep = true \
RETURN s.id AS s, m.id AS m, t.id AS t ORDER BY s, m, t"
.to_string()
}
#[test]
fn hop_counter_shows_the_failed_prefix_enumerating_no_further_hops() {
let graph = mid_gate_graph();
let early = qpp_hops(&graph, &early_query());
let post = qpp_hops(&graph, &postfilter_query());
let disabled = with_early_predicates_disabled(|| qpp_hops(&graph, &early_query()));
eprintln!("HOPS early={early} post={post} disabled={disabled}");
assert!(early > 0, "the quantified tail counts its hops");
assert!(early < post, "early {early} hops vs post-filter {post}");
assert!(early < disabled, "early {early} vs disabled {disabled}");
assert_eq!(post, disabled, "the post-filter form never evaluates early");
}
#[test]
fn early_evaluations_are_strictly_fewer_than_posthop_evaluations() {
let graph = mid_gate_graph();
let unfiltered = exec_cypher(&graph, &mid_gate("", "")).rows;
assert_eq!(
unfiltered.len(),
5,
"the fixture's path count: {unfiltered:?}"
);
take_early_evals();
exec_cypher(&graph, &early_query());
let evals = take_early_evals();
eprintln!("EVALS early={evals} paths={}", unfiltered.len());
assert!(
evals < unfiltered.len() as u64,
"early {evals} evaluations vs {} post-hop evaluations",
unfiltered.len()
);
assert_eq!(evals, 2, "once per mid-node binding: {evals}");
}
#[test]
fn early_answers_match_the_postfilter_on_both_fixtures() {
let ring_early = "MATCH (a:Geo {id:'c0'}) (()-[r1:ROAD]-(x:Geo)){1,2} (m:Geo) \
(()-[r2:ROAD]-(y:Geo)){1,2} (z:Geo {id:'c5'}) \
WHERE m.id = 'c3' \
RETURN a.id AS a, m.id AS m, z.id AS z ORDER BY a, m, z";
let ring_post = "MATCH (a:Geo {id:'c0'}) (()-[r1:ROAD]-(x:Geo)){1,2} (m:Geo) \
(()-[r2:ROAD]-(y:Geo)){1,2} (z:Geo {id:'c5'}) \
WITH a, m, z WHERE m.id = 'c3' \
RETURN a.id AS a, m.id AS m, z.id AS z ORDER BY a, m, z";
let cases: [(Graph, String, String); 2] = [
(mid_gate_graph(), early_query(), postfilter_query()),
(
super::allreduce_inline_tests::ev_ring_graph(),
ring_early.to_string(),
ring_post.to_string(),
),
];
for (graph, early, postfilter) in cases {
let early_rows = exec_cypher(&graph, &early).rows;
assert_eq!(
early_rows,
exec_cypher(&graph, &postfilter).rows,
"early vs post-filter"
);
assert_eq!(
early_rows,
with_early_predicates_disabled(|| exec_cypher(&graph, &early).rows),
"early vs evaluation-disabled"
);
}
}
#[test]
fn rebinding_names_are_declined_to_the_postfilter() {
let graph = mid_gate_graph();
let early = "MATCH (s:Geo {id:'s'})-[:ROAD]->(m:Geo)-[r:LINK*1..2]->(t:Geo) \
WHERE size(r) >= 1 \
RETURN s.id AS s, m.id AS m, t.id AS t ORDER BY s, m, t";
let post = "MATCH (s:Geo {id:'s'})-[:ROAD]->(m:Geo)-[r:LINK*1..2]->(t:Geo) \
WITH s, m, t, r WHERE size(r) >= 1 \
RETURN s.id AS s, m.id AS m, t.id AS t ORDER BY s, m, t";
take_early_evals();
let rows = exec_cypher(&graph, early).rows;
assert_eq!(
take_early_evals(),
0,
"a rebinding name never evaluates early"
);
assert!(
!rows.is_empty(),
"the fixture has surviving paths: {rows:?}"
);
assert_eq!(
rows,
exec_cypher(&graph, post).rows,
"declined vs post-filter"
);
}
#[test]
fn null_verdicts_prune_exactly() {
let b = Builder::new();
let s = b.node("s", &["Geo"]);
let m1 = b.node("m1", &["Geo"]);
let m2 = b.prop_node("m2", &["Geo"], &[("keep", Value::Bool(true))]);
let t = b.node("t", &["Geo"]);
b.rel(s, m1, "ROAD", &[]);
b.rel(s, m2, "ROAD", &[]);
b.rel(m1, t, "LINK", &[]);
b.rel(m2, t, "LINK", &[]);
let graph = b.done();
let early = "MATCH (s:Geo {id:'s'})-[:ROAD]->(m:Geo)-[:LINK*1..1]->(t:Geo) \
WHERE m.keep = true \
RETURN s.id AS s, m.id AS m ORDER BY s, m";
let post = "MATCH (s:Geo {id:'s'})-[:ROAD]->(m:Geo)-[:LINK*1..1]->(t:Geo) \
WITH s, m WHERE m.keep = true \
RETURN s.id AS s, m.id AS m ORDER BY s, m";
let early_rows = exec_cypher(&graph, early).rows;
assert_eq!(
early_rows.len(),
1,
"the null mid node drops, the true one passes: {early_rows:?}"
);
assert_eq!(early_rows, exec_cypher(&graph, post).rows);
}
#[test]
fn raising_conjuncts_defer_and_surface_only_where_the_postfilter_raises() {
let b = Builder::new();
let s = b.node("s", &["Geo"]);
let m1 = b.prop_node(
"m1",
&["Geo"],
&[("keep", Value::Int(5)), ("zero", Value::Int(0))],
);
let m2 = b.prop_node(
"m2",
&["Geo"],
&[("keep", Value::Int(5)), ("zero", Value::Int(1))],
);
let t = b.node("t", &["Geo"]);
b.rel(s, m1, "ROAD", &[]);
b.rel(s, m2, "ROAD", &[]);
b.rel(m2, t, "LINK", &[]);
let graph = b.done();
let early = "MATCH (s:Geo {id:'s'})-[:ROAD]->(m:Geo)-[:LINK*1..1]->(t:Geo) \
WHERE m.keep / m.zero = 1 \
RETURN s.id AS s, m.id AS m ORDER BY s, m";
let post = "MATCH (s:Geo {id:'s'})-[:ROAD]->(m:Geo)-[:LINK*1..1]->(t:Geo) \
WITH s, m WHERE m.keep / m.zero = 1 \
RETURN s.id AS s, m.id AS m ORDER BY s, m";
let early_rows = try_exec_cypher(&graph, early);
assert!(
early_rows.is_ok(),
"the raising dead prefix defers: {:?}",
early_rows.err()
);
assert_eq!(
early_rows.unwrap().rows,
with_early_predicates_disabled(|| try_exec_cypher(&graph, early).unwrap()).rows,
"deferred vs evaluation-disabled"
);
let post_rows = try_exec_cypher(&graph, post);
assert!(
post_rows.is_ok(),
"the post-filter never sees the dead prefix"
);
let b = Builder::new();
let s = b.node("s", &["Geo"]);
let m = b.prop_node(
"m",
&["Geo"],
&[("keep", Value::Int(5)), ("zero", Value::Int(0))],
);
let t = b.node("t", &["Geo"]);
b.rel(s, m, "ROAD", &[]);
b.rel(m, t, "LINK", &[]);
let graph = b.done();
let early_err = try_exec_cypher(&graph, early).err();
let disabled_err = with_early_predicates_disabled(|| try_exec_cypher(&graph, early)).err();
assert!(
early_err.is_some(),
"the surviving row raises through the post-filter"
);
assert_eq!(
early_err.unwrap().to_string(),
disabled_err.unwrap().to_string(),
"the deferred error is the stood-down run's error"
);
}
#[test]
fn selector_patterns_never_evaluate_early() {
let b = Builder::new();
let s = b.prop_node("s", &["Acc"], &[("owner", Value::String("Scott".into()))]);
let m = b.prop_node("m", &["Acc"], &[("owner", Value::String("Mike".into()))]);
let ch = b.prop_node(
"ch",
&["Acc"],
&[("owner", Value::String("Charles".into()))],
);
let j = b.prop_node(
"j",
&["Acc"],
&[
("owner", Value::String("Jay".into())),
("blocked", Value::Bool(true)),
],
);
let k = b.prop_node("k", &["Acc"], &[("owner", Value::String("Kim".into()))]);
b.rel(s, m, "T", &[]);
b.rel(m, ch, "T", &[]);
b.rel(s, j, "T", &[]);
b.rel(j, k, "T", &[]);
b.rel(k, ch, "T", &[]);
let graph = b.done();
let early = "MATCH p = ALL SHORTEST \
(p1:Acc {owner: 'Scott'})-->+(q1:Acc)-->+(r1:Acc {owner: 'Charles'}) \
WHERE q1.blocked = true \
RETURN [n IN nodes(p) | n.owner] AS path";
take_early_evals();
let rows = exec_cypher(&graph, early).rows;
assert_eq!(
take_early_evals(),
0,
"a selector pattern never evaluates early"
);
assert!(
rows.is_empty(),
"the predicate narrows the already-selected shortest path to nothing: {rows:?}"
);
}
#[test]
fn cip28_null_prebindings_never_decide_early() {
let b = Builder::new();
let s = b.node("s", &["Geo"]);
let m = b.node("m", &["Geo"]);
let t = b.prop_node("t", &["Geo"], &[("q", Value::Int(2))]);
b.rel(s, m, "LINK", &[]);
b.rel(m, t, "LINK", &[]);
let graph = b.done();
let early = "MATCH (a:Geo {id:'s'}) ((y:Geo)-[r:LINK]-(x:Geo WHERE t.q IS NULL)){1,2} (t:Geo) \
WHERE t.q > 1 \
RETURN a.id AS a, t.id AS t ORDER BY a, t";
let post = "MATCH (a:Geo {id:'s'}) ((y:Geo)-[r:LINK]-(x:Geo WHERE t.q IS NULL)){1,2} (t:Geo) \
WITH a, t WHERE t.q > 1 \
RETURN a.id AS a, t.id AS t ORDER BY a, t";
take_early_evals();
let early_rows = exec_cypher(&graph, early).rows;
assert_eq!(
early_rows.len(),
1,
"the surviving path names t: {early_rows:?}"
);
assert!(
take_early_evals() >= 1,
"the conjunct evaluated at t's binding"
);
assert_eq!(
early_rows,
exec_cypher(&graph, post).rows,
"early vs post-filter under the pre-binding"
);
}