use std::time::Instant;
use relate_core::Value;
use super::qpp::with_allreduce_pruning_disabled;
use super::qpp_hops;
use crate::exec::test_support::{exec_cypher, try_exec_cypher, Builder};
use crate::graph::Graph;
fn pruned_query(start: &str, end: &str, max: u32, budget: &str) -> String {
route_query(
start,
end,
max,
&format!("WHERE allReduce(acc = 0.0, r IN rels | acc + r.distance_km, acc < {budget})"),
)
}
fn postfilter_query(start: &str, end: &str, max: u32, budget: &str) -> String {
route_query(
start,
end,
max,
&format!("WITH p, rels WHERE allReduce(acc = 0.0, r IN rels | acc + r.distance_km, acc < {budget})"),
)
}
fn route_query(start: &str, end: &str, max: u32, filter: &str) -> String {
format!(
"MATCH REPEATABLE ELEMENTS p = (a:Geo {{id:'{start}'}}) \
(()-[rels:ROAD|CHARGE]-(x:Geo)){{1,{max}}} (b:Geo {{id:'{end}'}}) \
{filter} \
RETURN length(p) AS len, reduce(s = 0.0, r IN rels | s + r.distance_km) AS cost \
ORDER BY len, cost"
)
}
fn rows(graph: &Graph, filter: &str) -> relate_core::Result<Vec<Vec<Value>>> {
try_exec_cypher(
graph,
&format!(
"MATCH REPEATABLE ELEMENTS p = (a:Geo {{id:'s'}}) \
(()-[rels:ROAD]-(x:Geo)){{1,3}} (b:Geo {{id:'t'}}) \
{filter} \
RETURN length(p) AS len",
),
)
.map(|result| result.rows)
}
fn zero_leg_graph() -> Graph {
let b = Builder::new();
let s = b.node("s", &["Geo"]);
let m = b.node("m", &["Geo"]);
let t = b.node("t", &["Geo"]);
b.rel(s, m, "ROAD", &[("distance_km", Value::Float(0.0))]);
b.rel(m, t, "ROAD", &[("distance_km", Value::Float(100.0))]);
b.done()
}
fn ev_line_graph() -> Graph {
let b = Builder::new();
let paris = b.node("Paris", &["Geo"]);
let lyon = b.node("Lyon", &["Geo"]);
let valence = b.node("Valence", &["Geo"]);
let orange = b.node("Orange", &["Geo"]);
let marseille = b.node("Marseille", &["Geo"]);
b.rel(paris, lyon, "ROAD", &[("distance_km", Value::Float(465.0))]);
b.rel(
lyon,
valence,
"ROAD",
&[("distance_km", Value::Float(230.0))],
);
b.rel(
valence,
orange,
"ROAD",
&[("distance_km", Value::Float(110.0))],
);
b.rel(
orange,
marseille,
"ROAD",
&[("distance_km", Value::Float(200.0))],
);
b.rel(
lyon,
orange,
"ROAD",
&[("distance_km", Value::Float(310.0))],
);
b.rel(
valence,
valence,
"CHARGE",
&[("distance_km", Value::Float(0.0))],
);
b.done()
}
pub(super) fn ev_ring_graph() -> Graph {
let b = Builder::new();
let cities: Vec<_> = (0..8).map(|i| b.node(&format!("c{i}"), &["Geo"])).collect();
for i in 0..8 {
b.rel(
cities[i],
cities[(i + 1) % 8],
"ROAD",
&[("distance_km", Value::Float(100.0))],
);
b.rel(
cities[i],
cities[(i + 3) % 8],
"ROAD",
&[("distance_km", Value::Float(250.0))],
);
}
b.rel(
cities[2],
cities[2],
"CHARGE",
&[("distance_km", Value::Float(20.0))],
);
b.rel(
cities[6],
cities[6],
"CHARGE",
&[("distance_km", Value::Float(20.0))],
);
b.done()
}
fn ev_bidirectional_graph() -> Graph {
let b = Builder::new();
let cities: Vec<_> = (0..6).map(|i| b.node(&format!("n{i}"), &["Geo"])).collect();
for i in 0..6 {
let j = (i + 1) % 6;
let d = Value::Float(80.0 + 40.0 * (i % 3) as f64);
b.rel(cities[i], cities[j], "ROAD", &[("distance_km", d.clone())]);
b.rel(cities[j], cities[i], "ROAD", &[("distance_km", d)]);
}
b.done()
}
#[test]
fn profile_hop_counter_shows_the_pruned_walk_expanding_fewer_hops() {
let graph = ev_ring_graph();
let query = pruned_query("c0", "c5", 8, "600");
let postfilter = postfilter_query("c0", "c5", 8, "600");
let pruned = qpp_hops(&graph, &query);
let post = qpp_hops(&graph, &postfilter);
let disabled = with_allreduce_pruning_disabled(|| qpp_hops(&graph, &query));
eprintln!("HOPS pruned={pruned} post={post} disabled={disabled}");
assert!(pruned < post, "pruned {pruned} hops vs post-filter {post}");
assert!(pruned < disabled, "pruned {pruned} vs disabled {disabled}");
assert_eq!(post, disabled, "the post-filter form never inlines");
let hop_ratio = post as f64 / pruned as f64;
assert!(
hop_ratio >= 5.0,
"post-filter {post} hops vs pruned {pruned} hops \
(ratio {hop_ratio:.1}x, expected >= 5x)"
);
}
#[test]
fn pruned_allreduce_answers_match_the_postfilter_on_three_graphs() {
let cases = [
(ev_line_graph(), "Paris", "Marseille", 6u32, "1000"),
(ev_ring_graph(), "c0", "c5", 8, "600"),
(ev_bidirectional_graph(), "n0", "n3", 6, "400"),
];
for (graph, start, end, max, budget) in cases {
let query = pruned_query(start, end, max, budget);
let postfilter = postfilter_query(start, end, max, budget);
let pruned_rows = exec_cypher(&graph, &query).rows;
assert!(
!pruned_rows.is_empty(),
"the {start}->{end} case must have surviving routes"
);
assert_eq!(
pruned_rows,
exec_cypher(&graph, &postfilter).rows,
"pruned vs post-filter on {start}->{end}"
);
assert_eq!(
pruned_rows,
with_allreduce_pruning_disabled(|| exec_cypher(&graph, &query)).rows,
"pruned vs pruning-disabled on {start}->{end}"
);
}
}
#[test]
fn non_monotone_predicates_prune_exactly() {
let b = Builder::new();
let s = b.node("s", &["Geo"]);
let m1 = b.node("m1", &["Geo"]);
let m2 = b.node("m2", &["Geo"]);
let t = b.node("t", &["Geo"]);
b.rel(s, m1, "ROAD", &[("distance_km", Value::Int(3))]);
b.rel(m1, t, "ROAD", &[("distance_km", Value::Int(5))]);
b.rel(s, m2, "ROAD", &[("distance_km", Value::Int(10))]);
b.rel(m2, t, "ROAD", &[("distance_km", Value::Int(3))]);
let graph = b.done();
let body = |filter: &str| {
format!(
"MATCH REPEATABLE ELEMENTS p = (a:Geo {{id:'s'}}) \
(()-[rels:ROAD]-(x:Geo)){{1,4}} (b:Geo {{id:'t'}}) \
{filter} \
RETURN length(p) AS len ORDER BY len",
)
};
let predicate = "allReduce(acc = 0, r IN rels | acc + r.distance_km, acc % 7 <> 3)";
let pruned = exec_cypher(&graph, &body(&format!("WHERE {predicate}"))).rows;
assert!(pruned.is_empty(), "every route fails a prefix: {pruned:?}");
assert_eq!(
pruned,
with_allreduce_pruning_disabled(|| {
exec_cypher(&graph, &body(&format!("WHERE {predicate}"))).rows
})
);
assert_eq!(
pruned,
exec_cypher(&graph, &body(&format!("WITH p, rels WHERE {predicate}"))).rows
);
}
#[test]
fn null_distances_answer_exactly() {
let b = Builder::new();
let s = b.node("s", &["Geo"]);
let m = b.node("m", &["Geo"]);
let t = b.node("t", &["Geo"]);
b.rel(s, m, "ROAD", &[("distance_km", Value::Float(100.0))]);
b.rel(m, t, "ROAD", &[("distance_km", Value::Null)]);
b.rel(s, t, "ROAD", &[("distance_km", Value::Float(300.0))]);
let graph = b.done();
let body = |filter: &str| {
format!(
"MATCH REPEATABLE ELEMENTS p = (a:Geo {{id:'s'}}) \
(()-[rels:ROAD]-(x:Geo)){{1,3}} (b:Geo {{id:'t'}}) \
{filter} \
RETURN length(p) AS len ORDER BY len",
)
};
let predicate = "allReduce(acc = 0.0, r IN rels | acc + r.distance_km, acc < 500)";
let pruned = exec_cypher(&graph, &body(&format!("WHERE {predicate}"))).rows;
assert_eq!(pruned, vec![vec![Value::Int(1)]], "only the direct road");
assert_eq!(
pruned,
with_allreduce_pruning_disabled(|| {
exec_cypher(&graph, &body(&format!("WHERE {predicate}"))).rows
})
);
assert_eq!(
pruned,
exec_cypher(&graph, &body(&format!("WITH p, rels WHERE {predicate}"))).rows
);
}
#[test]
fn non_finite_accumulator_matches_pruning_disabled() {
let predicate = "allReduce(acc = 0.0, r IN rels | acc + 100 / r.distance_km, acc < 500)";
let through = zero_leg_graph();
let pruned = rows(&through, &format!("WHERE {predicate}")).unwrap();
assert!(pruned.is_empty());
assert_eq!(
pruned,
with_allreduce_pruning_disabled(|| {
rows(&through, &format!("WHERE {predicate}")).unwrap()
})
);
let b = Builder::new();
let s = b.node("s", &["Geo"]);
let d = b.node("d", &["Geo"]);
let m = b.node("m", &["Geo"]);
let t = b.node("t", &["Geo"]);
b.rel(s, d, "ROAD", &[("distance_km", Value::Float(0.0))]);
b.rel(s, m, "ROAD", &[("distance_km", Value::Float(100.0))]);
b.rel(m, t, "ROAD", &[("distance_km", Value::Float(100.0))]);
let dead_end = b.done();
let expected = rows(&dead_end, &format!("WHERE {predicate}")).unwrap();
assert_eq!(expected, vec![vec![Value::Int(2)]]);
assert_eq!(
expected,
with_allreduce_pruning_disabled(|| {
rows(&dead_end, &format!("WHERE {predicate}")).unwrap()
})
);
}
#[test]
fn raising_updates_surface_identically() {
let predicate =
"allReduce(acc = 0, r IN rels | acc + 100 / toInteger(r.distance_km), acc < 500)";
let through = zero_leg_graph();
let pruned_err = rows(&through, &format!("WHERE {predicate}")).unwrap_err();
assert!(pruned_err.to_string().contains("/ by zero"), "{pruned_err}");
assert_eq!(
pruned_err.to_string(),
with_allreduce_pruning_disabled(|| {
rows(&through, &format!("WHERE {predicate}")).unwrap_err()
})
.to_string()
);
}
#[test]
fn an_initial_aliasing_a_later_binding_is_declined() {
let b = Builder::new();
let s = b.node("s", &["Geo"]);
let m1 = b.node("m1", &["Geo"]);
let m2 = b.node("m2", &["Geo"]);
let t = b.node("t", &["Geo"]);
b.rel(s, m1, "ROAD", &[("p", Value::Int(1))]);
b.rel(s, m2, "ROAD", &[("p", Value::Int(1))]);
b.rel(m1, m2, "ROAD", &[("p", Value::Int(1))]);
b.rel(m2, t, "ROAD", &[("p", Value::Int(1))]);
let graph = b.done();
let body = |filter: &str| {
format!(
"MATCH (a:Geo {{id:'s'}}) ((y:Geo)-[r:ROAD]-(z:Geo WHERE b IS NULL)){{1,2}} \
(b:Geo {{id:'t'}}) \
{filter} \
RETURN a.id AS aid, b.id AS bid",
)
};
let predicate = "allReduce(total = size(b.id), b IN r | total + b.p, total > 0)";
let pruned = body(&format!("WHERE {predicate}"));
let postfilter = body(&format!("WITH a, b, r WHERE {predicate}"));
let expected = vec![vec![Value::String("s".into()), Value::String("t".into())]];
assert_eq!(exec_cypher(&graph, &pruned).rows, expected, "pruned");
assert_eq!(
with_allreduce_pruning_disabled(|| exec_cypher(&graph, &pruned).rows),
expected,
"pruning-disabled"
);
assert_eq!(
exec_cypher(&graph, &postfilter).rows,
expected,
"post-filter"
);
assert_eq!(
qpp_hops(&graph, &pruned),
with_allreduce_pruning_disabled(|| qpp_hops(&graph, &pruned)),
"a declined conjunct expands identically with the pruning stood down"
);
}
#[test]
fn a_null_prefix_then_a_raising_element_errors_identically() {
let b = Builder::new();
let s = b.node("s", &["Geo"]);
let m = b.node("m", &["Geo"]);
let n = b.node("n", &["Geo"]);
let t = b.node("t", &["Geo"]);
b.rel(
s,
m,
"ROAD",
&[("distance_km", Value::String("abc".into()))],
);
b.rel(m, n, "ROAD", &[("distance_km", Value::Null)]);
b.rel(n, t, "ROAD", &[("distance_km", Value::Float(5.0))]);
let graph = b.done();
let predicate = "allReduce(acc = 0, r IN rels | acc + 1, size(r.distance_km) < 5)";
let body = |filter: &str| {
format!(
"MATCH REPEATABLE ELEMENTS p = (a:Geo {{id:'s'}}) \
(()-[rels:ROAD]-(x:Geo)){{1,3}} (b:Geo {{id:'t'}}) \
{filter} \
RETURN length(p) AS len",
)
};
let run = |filter: &str| try_exec_cypher(&graph, &body(filter)).map(|result| result.rows);
let pruned_err = run(&format!("WHERE {predicate}")).unwrap_err();
assert!(
pruned_err
.to_string()
.contains("InvalidArgumentValue: Expected the value to be of type"),
"the raising element surfaces: {pruned_err}"
);
assert_eq!(
pruned_err.to_string(),
with_allreduce_pruning_disabled(|| run(&format!("WHERE {predicate}")))
.unwrap_err()
.to_string(),
"pruned errors identically to pruning-disabled"
);
}
#[test]
fn a_predicate_naming_a_later_binding_stays_a_postfilter() {
let graph = ev_ring_graph();
let inlinable = pruned_query("c0", "c5", 6, "600");
let declined = "MATCH REPEATABLE ELEMENTS p = (a:Geo {id:'c0'}) \
(()-[rels:ROAD|CHARGE]-(x:Geo)){1,6} (b:Geo {id:'c5'}) \
WHERE allReduce(acc = 0.0, r IN rels | acc + r.distance_km, acc < 600 + size(b.id) * 0) \
RETURN length(p) AS len, reduce(s = 0.0, r IN rels | s + r.distance_km) AS cost \
ORDER BY len, cost"
.to_string();
assert_eq!(
exec_cypher(&graph, &inlinable).rows,
exec_cypher(&graph, &declined).rows,
"both spellings filter identically"
);
let declined_hops = qpp_hops(&graph, &declined);
let postfilter_hops = qpp_hops(&graph, &postfilter_query("c0", "c5", 6, "600"));
let pruned_hops = qpp_hops(&graph, &inlinable);
assert_eq!(
declined_hops, postfilter_hops,
"the declined conjunct expands like the post-filter form"
);
assert!(pruned_hops < declined_hops);
}
#[test]
fn pruned_route_query_p50_beats_the_postfilter_form() {
let graph = ev_ring_graph();
let pruned = pruned_query("c0", "c5", 7, "600");
let postfilter = postfilter_query("c0", "c5", 7, "600");
let p50 = |query: &str, runs: usize| {
for _ in 0..2 {
exec_cypher(&graph, query);
}
let mut samples: Vec<_> = (0..runs)
.map(|_| {
let start = Instant::now();
exec_cypher(&graph, query);
start.elapsed().as_nanos() as f64
})
.collect();
samples.sort_by(|a, b| a.partial_cmp(b).expect("durations are ordered"));
samples[runs / 2]
};
let runs = 7;
let post_ms = p50(&postfilter, runs) / 1e6;
let pruned_ms = p50(&pruned, runs) / 1e6;
let ratio = post_ms / pruned_ms;
eprintln!("RATIO pruned={pruned_ms:.4}ms post={post_ms:.4}ms ratio={ratio:.1}x");
assert!(
ratio >= 3.0,
"pruned p50 {pruned_ms:.3} ms vs post-filter p50 {post_ms:.3} ms \
(ratio {ratio:.1}x, expected >= 3x)"
);
}
#[test]
fn a_carried_budget_stays_inlinable() {
let graph = ev_ring_graph();
let predicate = "allReduce(acc = 0.0, r IN rels | acc + r.distance_km, acc < budget)";
let pruned = format!(
"WITH 600 AS budget \
MATCH REPEATABLE ELEMENTS p = (a:Geo {{id:'c0'}}) \
(()-[rels:ROAD|CHARGE]-(x:Geo)){{1,6}} (b:Geo {{id:'c5'}}) \
WHERE {predicate} RETURN count(*) AS routes"
);
let postfilter = format!(
"WITH 600 AS budget \
MATCH REPEATABLE ELEMENTS p = (a:Geo {{id:'c0'}}) \
(()-[rels:ROAD|CHARGE]-(x:Geo)){{1,6}} (b:Geo {{id:'c5'}}) \
WITH p, rels WHERE {predicate} RETURN count(*) AS routes"
);
assert_eq!(
exec_cypher(&graph, &pruned).rows,
exec_cypher(&graph, &postfilter).rows
);
assert!(qpp_hops(&graph, &pruned) < qpp_hops(&graph, &postfilter));
}