use relate_core::Value::{self, Bool, Int, Null};
use crate::exec::test_support::{build, exec_autocommit, fan_in, Builder};
use crate::graph::Graph;
use crate::test_support::ints;
use super::fixtures::{
gpm, on_route, parity, plain, run, self_loops, shortcut, sorted, Counts, SELECTORS,
};
use super::with_forced_gpm;
fn expect(rows: &[&[Value]]) -> Vec<Vec<Value>> {
sorted(rows.iter().map(|r| r.to_vec()).collect())
}
#[test]
fn both_endpoints_bound() {
let g = shortcut();
let q = "MATCH (a {id: 0}), (b {id: 3}) \
MATCH p = shortestPath((a)-[*]->(b)) RETURN [n IN nodes(p) | n.id]";
assert_eq!(on_route(&g, q), expect(&[&[ints(&[0, 4, 3])]]));
}
#[test]
fn only_start_bound() {
let g = shortcut();
let q = "MATCH (a {id: 0}) MATCH p = ANY SHORTEST (a)-[*]->(b) RETURN b.id, length(p)";
assert_eq!(
on_route(&g, q),
expect(&[
&[Int(1), Int(1)],
&[Int(2), Int(2)],
&[Int(3), Int(2)],
&[Int(4), Int(1)],
])
);
}
#[test]
fn only_end_bound_reports_start_to_end() {
let g = shortcut();
let q = "MATCH (b {id: 3}) MATCH p = ANY SHORTEST (a)-[*]->(b) \
RETURN a.id, [n IN nodes(p) | n.id]";
assert_eq!(
on_route(&g, q),
expect(&[
&[Int(0), ints(&[0, 4, 3])],
&[Int(1), ints(&[1, 2, 3])],
&[Int(2), ints(&[2, 3])],
&[Int(3), ints(&[3, 1, 2, 3])],
&[Int(4), ints(&[4, 3])],
])
);
}
#[test]
fn both_unbound_with_labels() {
let rels = [(0, 1), (1, 2), (3, 2), (2, 4), (0, 4)].map(|(a, b)| (a, b, "R"));
let g = build(&[&["S"], &[], &["T"], &["S"], &["T"]], &rels).graph;
let q = "MATCH p = shortestPath((a:S)-[*]->(b:T)) RETURN a.id, b.id, length(p)";
let want = expect(&[
&[Int(0), Int(2), Int(2)],
&[Int(0), Int(4), Int(1)],
&[Int(3), Int(2), Int(1)],
&[Int(3), Int(4), Int(2)],
]);
assert_eq!(on_route(&g, q), want);
assert_eq!(gpm(&g, q), want);
}
#[test]
fn endpoint_maps_read_row_and_start_variables() {
let g = shortcut();
for (q, want) in [
(
"MATCH (x {id: 3}) MATCH p = shortestPath((a {id: 0})-[*]->(b {id: x.id})) \
RETURN b.id, length(p)",
expect(&[&[Int(3), Int(2)]]),
),
(
"MATCH p = ANY SHORTEST (a)-[*]->(b {id: a.id}) RETURN a.id, length(p)",
expect(&[&[Int(1), Int(3)], &[Int(2), Int(3)], &[Int(3), Int(3)]]),
),
] {
assert_eq!(on_route(&g, q), want, "{q}");
assert_eq!(gpm(&g, q), want, "{q}");
}
}
#[test]
fn same_variable_directed_two_cycle() {
let g = plain(4, &[(0, 1), (1, 0), (0, 2), (2, 3), (3, 0)]);
let q = "MATCH (a {id: 0}) MATCH p = ANY SHORTEST (a)-[*]->(a) RETURN length(p)";
assert_eq!(on_route(&g, q), expect(&[&[Int(2)]]));
let q = "MATCH p = ANY SHORTEST (a)-[*]->(a) RETURN a.id, length(p)";
let want = expect(&[
&[Int(0), Int(2)],
&[Int(1), Int(2)],
&[Int(2), Int(3)],
&[Int(3), Int(3)],
]);
assert_eq!(on_route(&g, q), want);
assert_eq!(gpm(&g, q), want);
}
#[test]
fn undirected_closed_trail_on_triangle_and_single_edge() {
let triangle = plain(3, &[(0, 1), (1, 2), (2, 0)]);
let q = "MATCH (a {id: 0}) MATCH p = ANY SHORTEST (a)-[*]-(a) RETURN length(p)";
assert_eq!(on_route(&triangle, q), expect(&[&[Int(3)]]));
let all = "MATCH (a {id: 0}) MATCH p = ALL SHORTEST (a)-[*]-(a) \
RETURN [r IN relationships(p) | r.k]";
let want = expect(&[&[ints(&[0, 1, 2])], &[ints(&[2, 1, 0])]]);
assert_eq!(on_route(&triangle, all), want);
assert_eq!(gpm(&triangle, all), want);
let edge = plain(2, &[(0, 1)]);
assert!(
on_route(&edge, q).is_empty(),
"a trail cannot return over its only edge"
);
}
#[test]
fn self_loop_closes_at_length_one() {
let g = plain(2, &[(0, 1), (1, 0), (0, 0)]);
for q in [
"MATCH (a {id: 0}) MATCH p = ANY SHORTEST (a)-[*]->(a) RETURN length(p)",
"MATCH (a {id: 0}) MATCH p = ANY SHORTEST (a)-[*]-(a) RETURN length(p)",
] {
assert_eq!(on_route(&g, q), expect(&[&[Int(1)]]), "{q}");
}
}
#[test]
fn zero_min_hops_on_same_node_is_length_zero() {
let g = plain(2, &[(0, 1), (1, 0)]);
let q = "MATCH (a {id: 0}) MATCH p = ANY SHORTEST (a)-[*0..2]->(a) RETURN length(p)";
let (rows, counts) = run(&g, q);
assert_eq!(rows, vec![vec![Int(0)]]);
assert_eq!(
counts.route_runs, 1,
"the zero-length path skipped the route"
);
assert_eq!(gpm(&g, q), expect(&[&[Int(0)]]));
}
#[test]
fn relationship_variable_binds_path_relationships_in_order() {
let g = plain(4, &[(0, 1), (1, 2), (2, 3)]);
for q in [
"MATCH (a {id: 0}), (b {id: 3}) MATCH p = shortestPath((a)-[r*]->(b)) \
RETURN [x IN r | x.k], r = relationships(p)",
"MATCH (b {id: 3}) MATCH p = shortestPath((a {id: 0})-[r*]->(b)) \
RETURN [x IN r | x.k], r = relationships(p)",
"MATCH (a {id: 0}) MATCH p = allShortestPaths((a)-[r*]->(b {id: 3})) \
RETURN [x IN r | x.k], r = relationships(p)",
] {
assert_eq!(
on_route(&g, q),
expect(&[&[ints(&[0, 1, 2]), Bool(true)]]),
"{q}"
);
}
let q = "MATCH (b {id: 3}) MATCH p = shortestPath((b)<-[r*]-(a {id: 0})) \
RETURN [x IN r | x.k], [n IN nodes(p) | n.id]";
assert_eq!(
on_route(&g, q),
expect(&[&[ints(&[2, 1, 0]), ints(&[3, 2, 1, 0])]])
);
}
#[test]
fn unreachable_pair_gives_no_row_and_optional_gives_null() {
let g = plain(3, &[(0, 1), (2, 1)]);
let q = "MATCH (a {id: 0}), (b {id: 2}) MATCH p = shortestPath((a)-[*]->(b)) RETURN p";
let (rows, counts) = run(&g, q);
assert!(counts.route_runs > 0, "expected the BFS route for {q}");
assert_eq!(counts.expands, 0);
assert!(rows.is_empty());
let q = "MATCH (a {id: 0}), (b {id: 2}) \
OPTIONAL MATCH p = shortestPath((a)-[*]->(b)) RETURN p";
let (rows, counts) = run(&g, q);
assert!(counts.route_runs > 0, "expected the BFS route for {q}");
assert_eq!(rows, vec![vec![Null]]);
}
#[test]
fn degree_weighted_bidir_expands_leaf_instead_of_hub() {
let mut edges: Vec<(usize, usize)> = (1..=100).map(|i| (0, i)).collect();
edges.push((102, 101));
let g = plain(103, &edges);
let q = "MATCH (a {id: 0}), (b {id: 101}) MATCH p = shortestPath((a)-[*1..5]->(b)) RETURN p";
let (rows, counts) = run(&g, q);
assert!(counts.route_runs > 0);
assert!(rows.is_empty());
assert!(
counts.expands < 10,
"expected leaf-side expansion (<10 edges), got {}",
counts.expands
);
}
#[test]
fn one_hop_qpp_with_inner_predicates_routes_and_matches_gpm() {
let g = Graph::default();
exec_autocommit(
&g,
"CREATE (a:User {id: 1, age: 30}),
(b:User {id: 2, age: 15}),
(c:User {id: 3, age: 25}),
(d:User {id: 4, age: 40}),
(e:User {id: 5, age: 10}),
(a)-[:FOLLOWS]->(b)-[:FOLLOWS]->(d),
(a)-[:FOLLOWS]->(c)-[:FOLLOWS]->(d),
(a)-[:FOLLOWS]->(e)-[:FOLLOWS]->(c)",
)
.unwrap();
let queries = [
"MATCH p = SHORTEST 1 (a:User {id: 1}) ((u:User WHERE u.age > 20)-[:FOLLOWS]->(v:User)){1,5} (b:User {id: 4}) \
RETURN [n IN nodes(p) | n.id], [n IN u | n.id], [n IN v | n.id]",
"MATCH p = ALL SHORTEST (a:User {id: 1}) ((u:User WHERE u.age > 20)-[:FOLLOWS]->(v:User)){1,5} (b:User {id: 4}) \
RETURN [n IN nodes(p) | n.id], [n IN u | n.id], [n IN v | n.id]",
"MATCH p = SHORTEST 1 (a:User {id: 2}) ((u:User WHERE u.age > 20)-[:FOLLOWS]->(v:User)){0,3} (b:User {id: 2}) \
RETURN length(p), [n IN u | n.id], [n IN v | n.id]",
"MATCH p = SHORTEST 1 (a:User {id: 2}) ((u:User WHERE u.age > 20)-[:FOLLOWS]->(v:User)){1,3} (b:User {id: 4}) \
RETURN p",
];
for q in queries {
let counts = parity(&g, q);
assert!(counts.route_runs > 0, "expected shortest_route for {q}");
}
}
fn diamond() -> Graph {
plain(4, &[(0, 1), (1, 3), (0, 2), (2, 3)])
}
#[test]
fn all_shortest_paths_on_diamond_gives_both_routes() {
let g = diamond();
let q = "MATCH (a {id: 0}), (b {id: 3}) MATCH p = allShortestPaths((a)-[*]->(b)) \
RETURN [n IN nodes(p) | n.id]";
assert_eq!(
on_route(&g, q),
expect(&[&[ints(&[0, 1, 3])], &[ints(&[0, 2, 3])]])
);
}
#[test]
fn gql_selectors_match_function_forms() {
let g = diamond();
let count = |q: &str| on_route(&g, q).len();
let pair = "MATCH (a {id: 0}), (b {id: 3}) ";
for (endpoints, prefix) in [
("(a)-[*]->(b)", pair),
("(a)-[*]-(b)", pair),
("(a)-[*1..2]-(b)", pair),
] {
let single = count(&format!(
"{prefix}MATCH p = shortestPath({endpoints}) RETURN p"
));
let all = count(&format!(
"{prefix}MATCH p = allShortestPaths({endpoints}) RETURN p"
));
assert!(all > single, "{endpoints}: all={all} single={single}");
for gql in ["ANY SHORTEST", "SHORTEST 1"] {
let q = format!("{prefix}MATCH p = {gql} {endpoints} RETURN p");
assert_eq!(count(&q), single, "{q}");
}
for gql in ["SHORTEST 1 GROUP", "ALL SHORTEST"] {
let q = format!("{prefix}MATCH p = {gql} {endpoints} RETURN p");
assert_eq!(count(&q), all, "{q}");
}
}
}
#[test]
fn unbounded_var_length_is_not_capped() {
let g = chain40();
let q = "MATCH (a {id: 0}), (b {id: 40}) MATCH p = shortestPath((a)-[*]->(b)) \
RETURN length(p)";
assert_eq!(on_route(&g, q), expect(&[&[Int(40)]]));
let q = "MATCH (a {id: 0}) MATCH p = shortestPath((a)-[*]->(b {id: 40})) \
RETURN length(p)";
assert_eq!(on_route(&g, q), expect(&[&[Int(40)]]));
}
#[test]
fn bound_end_runs_one_search_not_one_per_start_candidate() {
let mut labels: Vec<&[&str]> = vec![&["S"]; 50];
labels.extend([&[] as &[&str]; 10]);
labels.push(&["T"]);
let mut rels: Vec<(usize, usize, &str)> = (0..50).map(|i| (i, 50, "R")).collect();
rels.extend((50..60).map(|i| (i, i + 1, "R")));
let g = build(&labels, &rels).graph;
let q = "MATCH (b:T) MATCH p = shortestPath((a:S)-[*]->(b)) RETURN length(p)";
let (rows, counts) = run(&g, q);
assert_eq!(rows.len(), 50);
assert!(rows.iter().all(|r| r[0] == Int(11)));
assert_eq!(counts.route_runs, 1);
assert!(counts.expands > 0);
assert!(
counts.expands < 3 * rels.len() as u64,
"expands={} for {} relationships",
counts.expands,
rels.len()
);
}
#[test]
fn forced_gpm_switch_bypasses_the_route() {
let g = shortcut();
let q = "MATCH p = ALL SHORTEST (a)-[*]->(b) \
RETURN a.id, b.id, [r IN relationships(p) | r.k]";
let route = on_route(&g, q);
let (forced, counts) = with_forced_gpm(|| run(&g, q));
assert_eq!(counts, Counts::default());
assert_eq!(route, sorted(forced));
}
#[test]
fn where_predicate_selects_the_shortest_satisfying_path() {
let g = shortcut();
let q = "MATCH p = shortestPath((a {id: 0})-[*]->(b {id: 3})) \
WHERE NONE(n IN nodes(p) WHERE n.id = 4) \
RETURN [n IN nodes(p) | n.id]";
assert_eq!(on_route(&g, q), expect(&[&[ints(&[0, 1, 2, 3])]]));
}
#[test]
fn where_predicate_filters_each_all_shortest_trail() {
let rels = [(0, 1), (1, 3), (0, 2), (2, 3)].map(|(a, b)| (a, b, "R"));
let g = build(&[&[], &[], &[], &[]], &rels).graph;
let q = "MATCH p = allShortestPaths((a {id: 0})-[*]->(b {id: 3})) \
WHERE NONE(n IN nodes(p) WHERE n.id = 2) \
RETURN [n IN nodes(p) | n.id]";
assert_eq!(on_route(&g, q), expect(&[&[ints(&[0, 1, 3])]]));
}
#[test]
fn where_predicate_that_no_path_satisfies_yields_nothing() {
let g = shortcut();
let q = "MATCH p = shortestPath((a {id: 0})-[*]->(b {id: 3})) \
WHERE length(p) > 10 \
RETURN length(p)";
assert!(on_route(&g, q).is_empty());
}
#[test]
fn where_predicate_on_a_closed_path_keeps_the_zero_length_trail() {
let g = shortcut();
let q = "MATCH p = ANY SHORTEST (a {id: 0})-[*0..3]->(a) \
WHERE length(p) <= 3 RETURN length(p)";
let (rows, counts) = run(&g, q);
assert_eq!(rows, vec![vec![Int(0)]]);
assert_eq!(
counts.route_runs, 1,
"the route declined a decidable filter"
);
assert_eq!(gpm(&g, q), expect(&[&[Int(0)]]));
}
fn chain40() -> Graph {
let rels: Vec<(usize, usize)> = (0..40).map(|i| (i, i + 1)).collect();
plain(41, &rels)
}
#[test]
fn filtered_walk_keeps_an_explicit_bound() {
let g = chain40();
let q = "MATCH p = shortestPath((a {id: 0})-[:R*1..40]->(b {id: 40})) \
WHERE length(p) = 40 \
RETURN length(p)";
assert_eq!(on_route(&g, q), expect(&[&[Int(40)]]));
assert_eq!(gpm(&g, q), expect(&[&[Int(40)]]));
}
#[test]
fn filtered_unbounded_walk_reads_the_gpm_unbounded_cap() {
let g = chain40();
let q = "MATCH p = shortestPath((a {id: 0})-[*]->(b {id: 40})) \
WHERE length(p) = 40 \
RETURN length(p)";
assert_eq!(on_route(&g, q), expect(&[&[Int(40)]]));
assert_eq!(gpm(&g, q), expect(&[&[Int(40)]]));
}
#[test]
fn where_conjunct_reading_a_later_pattern_name_declines_the_route() {
let g = shortcut();
let q = "MATCH p = shortestPath((a {id: 0})-[*]->(b {id: 3})), (x {id: 2}) \
WHERE length(p) = x.id RETURN length(p)";
let (rows, counts) = run(&g, q);
assert_eq!(
counts.route_runs, 0,
"the route chose a trail its predicate could not decide"
);
assert_eq!(rows, vec![vec![Int(2)]], "the enumeration stopped matching");
assert_eq!(sorted(rows), gpm(&g, q));
}
#[test]
fn path_where_over_own_variables_keeps_the_route() {
let g = shortcut();
let q = "MATCH p = ALL SHORTEST ((a {id: 0})-[*]->(b {id: 3}) \
WHERE NONE(n IN nodes(p) WHERE n.id = 4)) \
RETURN [n IN nodes(p) | n.id]";
assert_eq!(
on_route(&g, q),
expect(&[&[ints(&[0, 1, 2, 3])]]),
"the route must choose the shortest trail the predicate accepts"
);
}
#[test]
fn where_predicate_with_subquery_local_variable_still_filters() {
let rels = [(0, 1), (1, 2), (2, 3), (0, 4), (4, 3)].map(|(a, b)| (a, b, "R"));
let g = build(&[&["A"], &[], &[], &[], &[]], &rels).graph;
let q = "MATCH p = shortestPath((a {id: 0})-[*]->(b {id: 3})) \
WHERE EXISTS { MATCH (z:A) WHERE length(p) = 3 } \
RETURN [n IN nodes(p) | n.id]";
assert_eq!(on_route(&g, q), expect(&[&[ints(&[0, 1, 2, 3])]]));
}
#[test]
fn end_node_where_over_the_relationship_list_matches_gpm() {
for s in SELECTORS {
let loops = 3;
let g = self_loops(loops);
let q = format!(
"MATCH p = {}(m:M)-[bs:B]->*(m WHERE size(bs) = 1){} \
RETURN size(bs) AS len",
s.open, s.close
);
let want = |rows: usize| {
let one: &[Value] = &[Int(1)];
expect(&vec![one; rows])
};
assert_eq!(on_route(&g, &q), want(if s.all { loops } else { 1 }), "{q}");
assert_eq!(gpm(&g, &q), want(if s.all { loops } else { 1 }), "{q}");
}
}
#[test]
fn end_node_where_for_a_longer_trail_walks_ascending_lengths() {
for loops in [2, 3] {
let g = self_loops(loops);
let q = format!(
"MATCH p = SHORTEST 1 (m:M)-[bs:B]->*(m WHERE size(bs) = {loops}) \
RETURN size(bs) AS len"
);
assert_eq!(on_route(&g, &q), expect(&[&[Int(loops as i64)]]), "{q}");
assert_eq!(gpm(&g, &q), expect(&[&[Int(loops as i64)]]), "{q}");
}
}
#[test]
fn all_shortest_stops_at_the_shortest_satisfying_length() {
let rels = [(0, 1), (1, 3), (0, 2), (2, 4), (4, 3)].map(|(a, b)| (a, b, "R"));
let g = build(&[&[], &[], &[], &[], &[]], &rels).graph;
let q = "MATCH p = allShortestPaths((a {id: 0})-[*]->(b {id: 3})) \
WHERE NONE(n IN nodes(p) WHERE n.id = 99) \
RETURN [n IN nodes(p) | n.id]";
assert_eq!(on_route(&g, q), expect(&[&[ints(&[0, 1, 3])]]));
}
#[test]
fn both_endpoints_by_property_map_take_the_pair_search() {
let g = shortcut();
let bound = "MATCH (a {id: 0}), (b {id: 3}) \
MATCH p = shortestPath((a)-[*]->(b)) RETURN [n IN nodes(p) | n.id]";
let seek = "MATCH p = shortestPath((a {id: 0})-[*]->(b {id: 3})) \
RETURN [n IN nodes(p) | n.id]";
let (rows, seek_counts) = run(&g, seek);
assert_eq!(sorted(rows), expect(&[&[ints(&[0, 4, 3])]]));
let (_, bound_counts) = run(&g, bound);
assert!(seek_counts.route_runs > 0);
assert_eq!(
seek_counts.expands, bound_counts.expands,
"the property-endpoint form must take the pair search, not the fan"
);
}
#[test]
fn property_map_seeking_several_end_candidates() {
let b = Builder::new();
let nodes: Vec<_> = (0..5)
.map(|i| {
let id = i.to_string();
if i == 2 || i == 4 {
b.prop_node(&id, &[], &[("tag", Value::String("x".into()))])
} else {
b.node(&id, &[])
}
})
.collect();
for (a, z) in [(0, 1), (1, 2), (3, 2), (0, 4), (4, 2)] {
b.rel(nodes[a], nodes[z], "R", &[]);
}
let g = b.done();
let q = "MATCH p = shortestPath((a {id: '0'})-[*..3]->(b {tag: 'x'})) \
RETURN b.id, length(p)";
assert_eq!(
on_route(&g, q),
expect(&[
&[Value::String("2".into()), Int(2)],
&[Value::String("4".into()), Int(1)],
])
);
}
#[test]
fn fan_from_bounded_seekable_end_when_start_is_bare() {
let b = Builder::new();
let t = b.prop_node("t", &[], &[("tag", Value::String("x".into()))]);
for i in 0..3 {
let s = b.node(&format!("s{i}"), &["S"]);
let m = b.node(&format!("m{i}"), &[]);
b.rel(s, m, "R", &[]);
b.rel(m, t, "R", &[]);
for j in 0..20 {
let dead = b.node(&format!("d{i}_{j}"), &[]);
b.rel(s, dead, "R", &[]);
}
}
let g = b.done();
let q = "MATCH p = shortestPath((a:S)-[*]->(b {tag: 'x'})) RETURN a.id, length(p)";
let (rows, counts) = run(&g, q);
assert_eq!(sorted(rows), sorted(gpm(&g, q)));
assert!(counts.route_runs > 0);
assert!(
counts.expands < 20,
"the bounded end must drive a backward fan, not a forward ball per start: {} expansions",
counts.expands
);
}
#[test]
fn property_map_seeking_to_no_candidate() {
let g = shortcut();
let q = "MATCH p = shortestPath((a {id: 0})-[*]->(b {id: 99})) RETURN length(p)";
let (rows, counts) = run(&g, q);
assert!(counts.route_runs > 0);
assert_eq!(sorted(rows), Vec::<Vec<Value>>::new());
}
#[test]
fn exists_probe_answers_from_the_streamed_rows() {
let g = shortcut();
let hit = "RETURN EXISTS { MATCH p = ANY SHORTEST (a {id: 0})-[*]->(b {id: 3}) } AS found";
let (rows, counts) = run(&g, hit);
assert!(counts.route_runs > 0, "the probe must ride the route");
assert_eq!(sorted(rows), expect(&[&[Bool(true)]]));
assert_eq!(on_route(&g, hit), gpm(&g, hit));
let miss = "RETURN EXISTS { MATCH p = ANY SHORTEST (a {id: 0})-[*]->(b {id: 99}) } AS found";
let (rows, counts) = run(&g, miss);
assert!(counts.route_runs > 0, "the probe must ride the route");
assert_eq!(sorted(rows), expect(&[&[Bool(false)]]));
}
#[test]
fn exists_probe_halts_the_filtered_trail_walk() {
let g = shortcut();
let q = "RETURN EXISTS { MATCH p = ALL SHORTEST (a {id: 0})-[*]->(b {id: 3}) \
WHERE length(p) > 0 } AS found";
let (rows, counts) = run(&g, q);
assert!(counts.route_runs > 0, "the probe must ride the route");
assert_eq!(sorted(rows), expect(&[&[Bool(true)]]));
}
#[test]
fn an_exists_probe_stops_the_fan_after_its_first_answering_layer() {
let b = Builder::new();
let root = b.node("0", &["N"]);
for i in 1..=5 {
let mid = b.node(&i.to_string(), &["N"]);
b.rel(root, mid, "R", &[]);
for j in 0..20 {
let leaf = b.node(&format!("{i}_{j}"), &["N"]);
b.rel(mid, leaf, "R", &[]);
}
}
let g = b.done();
let pattern = "p = ANY SHORTEST (a {id: '0'})-[:R*..3]->(b)";
let probe = format!("RETURN EXISTS {{ MATCH {pattern} }} AS found");
let full = format!("MATCH {pattern} RETURN count(*) AS n");
let (rows, probe_counts) = run(&g, &probe);
assert!(probe_counts.route_runs > 0, "the probe must ride the route");
assert_eq!(sorted(rows), vec![vec![Value::Bool(true)]]);
let (_, full_counts) = run(&g, &full);
assert!(
probe_counts.expands < full_counts.expands,
"the probe must not expand the deeper layers: {} vs {}",
probe_counts.expands,
full_counts.expands
);
}
#[test]
fn all_shortest_paths_limit_stops_the_input_row_loop() {
let g = fan_in(20);
let q = "MATCH (a:A) MATCH p = allShortestPaths((a)-[:R*]-(b:B)) \
RETURN p LIMIT 1";
let (rows, counts) = run(&g, q);
assert_eq!(rows.len(), 1, "the LIMIT answer is one row");
assert_eq!(
counts.route_runs, 1,
"the cap must stop the loop after the first row: {} runs",
counts.route_runs
);
let (eager_rows, eager) = run(
&g,
"MATCH (a:A) MATCH p = allShortestPaths((a)-[:R*]-(b:B)) RETURN p",
);
assert_eq!(eager_rows.len(), 20);
assert_eq!(eager.route_runs, 20, "one route run per input row");
}
#[test]
fn distinct_limit_over_a_routed_shortest_path_charges_the_pre_projection_row() {
let g = fan_in(20);
let (rows, counts) = run(
&g,
"MATCH (a:A) MATCH p = allShortestPaths((a)-[:R*]-(b:B)) \
RETURN DISTINCT b.id AS id LIMIT 1",
);
assert_eq!(rows.len(), 1);
assert!(counts.route_runs > 0, "the pattern must ride the route");
assert_eq!(
counts.route_runs, 1,
"the distinct cap fills on the first row: {} runs",
counts.route_runs
);
}
#[test]
fn a_full_cap_stops_the_route_search_mid_row() {
let a: &[&str] = &["A"];
let m: &[&str] = &["M"];
let b: &[&str] = &["B"];
let k = 12usize;
let mut labels = vec![a];
labels.extend(std::iter::repeat_n(m, k));
labels.push(b);
let hub = k + 1;
let mut rels: Vec<(usize, usize, &str)> = Vec::new();
for i in 1..=k {
rels.push((0, i, "R"));
rels.push((i, hub, "R"));
}
let g = build(&labels, &rels).graph;
let q = |limit: &str| {
format!("MATCH (a:A) MATCH p = allShortestPaths((a)-[:R*]-(b:B)) RETURN p{limit}")
};
let (limited, counts) = run(&g, &q(" LIMIT 1"));
let (full, full_counts) = run(&g, &q(""));
assert_eq!(limited.len(), 1);
assert_eq!(full.len(), k);
assert!(counts.route_runs > 0, "the pattern must ride the route");
assert!(
counts.expands < full_counts.expands,
"a full cap must stop the search: {} expands vs eager {}",
counts.expands,
full_counts.expands
);
}