use super::qpp_hops;
use crate::exec::kernels::with_frontier_disabled;
use crate::exec::test_support::{column, exec_cypher, fan_in, path_value, Builder};
use crate::graph::Graph;
fn ring_graph() -> Graph {
let b = Builder::new();
let n: Vec<_> = (0..6).map(|i| b.node(&format!("n{i}"), &["Geo"])).collect();
for i in 0..6 {
let j = (i + 1) % 6;
b.rel(n[i], n[j], "LINK", &[]);
b.rel(n[j], n[i], "LINK", &[]);
}
b.done()
}
fn enumerates_enough(rows: &[Vec<relate_core::Value>]) {
assert!(
rows.len() > 10,
"the fixture enumerates enough paths to make the halt visible: {rows:?}"
);
}
#[test]
fn limit_rides_the_walk_and_answers_the_truncated_enumeration() {
let graph = ring_graph();
with_frontier_disabled(|| {
let body = |tail: &str| {
format!(
"MATCH (x:Geo {{id:'n0'}})-[:LINK*1..3]-(y:Geo) \
RETURN x.id AS x, y.id AS y {tail}"
)
};
let full = exec_cypher(&graph, &body("")).rows;
enumerates_enough(&full);
let limited = exec_cypher(&graph, &body("LIMIT 3")).rows;
assert_eq!(
limited,
full[..3.min(full.len())].to_vec(),
"LIMIT keeps exactly the full enumeration's first rows"
);
let full_hops = qpp_hops(&graph, &body(""));
let limited_hops = qpp_hops(&graph, &body("LIMIT 3"));
eprintln!("HOPS full={full_hops} limit3={limited_hops}");
assert!(
limited_hops < full_hops,
"LIMIT 3 took {limited_hops} hops vs the full enumeration's {full_hops}"
);
let paged = exec_cypher(&graph, &body("SKIP 4 LIMIT 3")).rows;
assert_eq!(
paged,
full[4..7.min(full.len())].to_vec(),
"SKIP+LIMIT keeps the enumeration's corresponding window"
);
});
}
#[test]
fn distinct_limit_rides_the_walk() {
let graph = ring_graph();
with_frontier_disabled(|| {
let full = exec_cypher(
&graph,
"MATCH (x:Geo {id:'n0'})-[:LINK*1..3]-(y:Geo) RETURN DISTINCT y.id AS y",
)
.rows;
let limited = exec_cypher(
&graph,
"MATCH (x:Geo {id:'n0'})-[:LINK*1..3]-(y:Geo) RETURN DISTINCT y.id AS y LIMIT 2",
)
.rows;
assert_eq!(
limited,
full[..2].to_vec(),
"DISTINCT LIMIT keeps the distinct enumeration's first rows"
);
let full_hops = qpp_hops(
&graph,
"MATCH (x:Geo {id:'n0'})-[:LINK*1..3]-(y:Geo) RETURN DISTINCT y.id AS y",
);
let limited_hops = qpp_hops(
&graph,
"MATCH (x:Geo {id:'n0'})-[:LINK*1..3]-(y:Geo) RETURN DISTINCT y.id AS y LIMIT 2",
);
assert!(
limited_hops < full_hops,
"the distinct cap halts the walk: {limited_hops} vs {full_hops} hops"
);
});
}
fn assert_names_bound(result: &super::super::QueryResult) {
let wholes = column(result, "whole", path_value);
let subs = column(result, "sub", path_value);
assert_eq!(wholes, subs, "both names carry the same path value");
}
#[test]
fn capped_rows_carry_every_path_name_binding() {
let graph = ring_graph();
let body = |tail: &str| {
format!(
"MATCH p = (q = (x:Geo {{id:'n0'}})-[:LINK*1..3]-(y:Geo)) \
RETURN x.id AS x, y.id AS y, p AS whole, q AS sub {tail}"
)
};
let full = exec_cypher(&graph, &body(""));
enumerates_enough(&full.rows);
assert_names_bound(&full);
let limited = exec_cypher(&graph, &body("LIMIT 3"));
assert_eq!(
limited.rows.len(),
3,
"the cap truncates: {:?}",
limited.rows
);
assert_names_bound(&limited);
for row in &limited.rows {
assert!(
full.rows.contains(row),
"every capped row is a row of the uncapped enumeration: {row:?}"
);
}
}
#[test]
fn selector_probe_answers_through_the_residual() {
let graph = ring_graph();
let fold = |predicate: &str| {
format!(
"MATCH p = ANY (q = (x:Geo {{id:'n0'}})-[:LINK*1..3]-(y:Geo)) \
WHERE {predicate} \
RETURN count(DISTINCT x.id) > 0 AS hit"
)
};
assert_eq!(
exec_cypher(&graph, &fold("y.id = 'n3'")).rows,
vec![vec![relate_core::Value::Bool(true)]]
);
assert_eq!(
exec_cypher(&graph, &fold("y.id = 'nope'")).rows,
vec![vec![relate_core::Value::Bool(false)]],
"the selector probe must not answer on WHERE-rejected rows"
);
}
#[test]
fn selector_probe_applies_the_deferred_element_predicates() {
let graph = ring_graph();
let fold = |body: &str| {
format!(
"MATCH p = ANY (q = (x:Geo {{id:'n0'}} WHERE EXISTS {{ {body} }}) \
-[:LINK*1..3]-(y:Geo)) \
RETURN count(DISTINCT x.id) > 0 AS hit"
)
};
assert_eq!(
exec_cypher(&graph, &fold("MATCH (x)-[:LINK]-(z:Geo {id:'n1'})")).rows,
vec![vec![relate_core::Value::Bool(true)]]
);
assert_eq!(
exec_cypher(&graph, &fold("MATCH (x)-[:LINK]-(z:Geo {id:'nope'})")).rows,
vec![vec![relate_core::Value::Bool(false)]],
"the probe must not answer on rows the deferred element predicate rejects"
);
}
#[test]
fn exists_probe_short_circuits_the_frontier() {
let graph = ring_graph();
let exists = "RETURN EXISTS { MATCH (x:Geo {id:'n0'})-[:LINK*1..3]-(y:Geo {id:'n3'}) } AS hit";
let rows = exec_cypher(&graph, exists).rows;
assert_eq!(rows, vec![vec![relate_core::Value::Bool(true)]]);
let full_hops = qpp_hops(
&graph,
"MATCH (x:Geo {id:'n0'})-[:LINK*1..3]-(y:Geo {id:'n3'}) RETURN count(*) AS n",
);
let probe_hops = qpp_hops(&graph, exists);
eprintln!("HOPS exists={probe_hops} full={full_hops}");
assert!(full_hops > 0, "the pattern enumerates hops");
assert!(
probe_hops < full_hops,
"the probe took {probe_hops} hops vs the full enumeration's {full_hops}"
);
let miss = "RETURN EXISTS { MATCH (x:Geo {id:'n0'})-[:LINK*1..3]-(y:Nope) } AS hit";
assert_eq!(
exec_cypher(&graph, miss).rows,
vec![vec![relate_core::Value::Bool(false)]]
);
}
#[test]
fn exists_probe_answers_what_the_engine_answers() {
let graph = ring_graph();
for k in 0..6 {
let exists = format!(
"RETURN EXISTS {{ MATCH (x:Geo {{id:'n0'}})-[:LINK*1..2]-(y:Geo {{id:'n{k}'}}) }} AS hit"
);
let count = format!(
"MATCH (x:Geo {{id:'n0'}})-[:LINK*1..2]-(y:Geo {{id:'n{k}'}}) \
RETURN count(*) > 0 AS hit"
);
assert_eq!(
exec_cypher(&graph, &exists).rows,
exec_cypher(&graph, &count).rows,
"EXISTS vs the engine's count on n{k}"
);
}
}
#[test]
fn selector_limit_stops_the_input_row_loop_on_the_gpm_path() {
let graph = fan_in(20);
let q =
|limit: &str| format!("MATCH (a:A) MATCH p = SHORTEST 2 (a)-[:R*]-(b:B) RETURN p{limit}");
let _ = crate::ctx::take_node_reads();
let limited = exec_cypher(&graph, &q(" LIMIT 1"));
let limited_reads = crate::ctx::take_node_reads();
let _ = crate::ctx::take_node_reads();
let eager = exec_cypher(&graph, &q(""));
let eager_reads = crate::ctx::take_node_reads();
assert_eq!(limited.rows.len(), 1);
assert_eq!(eager.rows.len(), 20);
assert_eq!(
eager_reads - limited_reads,
19,
"the cap must stop the loop after the first row: \
{limited_reads} reads vs eager {eager_reads}"
);
}