use super::*;
use crate::exec::take_node_reads;
use crate::exec::test_support::{build, exec_autocommit, exec_cypher};
use crate::graph::Graph;
fn person_ring() -> Graph {
let person = &["Person"][..];
let labels: [&[&str]; 6] = [person; 6];
let built = build(
&labels,
&[
(0, 1, "knows"),
(1, 2, "knows"),
(2, 3, "knows"),
(3, 4, "knows"),
(4, 5, "knows"),
(5, 0, "knows"),
],
);
exec_autocommit(&built.graph, "CREATE INDEX FOR (p:Person) ON (p.id)").unwrap();
built.graph
}
const PATTERN_BODIES: [&str; 3] = [
"(x:Person)-[:knows*1..2]-(y)",
"(x:Person)(()-[:knows]-()){1,2}(y)",
"(x:Person)-[:knows]-{1,2}(y)",
];
fn trailed_pattern_bodies() -> Vec<String> {
PATTERN_BODIES
.iter()
.map(|body| (*body).to_owned())
.chain([format!("TRAIL {}", PATTERN_BODIES[0])])
.collect()
}
fn rows_of(g: &Graph, cypher: &str) -> Vec<Vec<Value>> {
let r = exec_cypher(g, cypher);
let mut rows = r.rows;
rows.sort_by(|a, b| format!("{a:?}").cmp(&format!("{b:?}")));
rows
}
#[test]
fn every_spelling_seeks_the_start_point_through_the_index() {
let g = person_ring();
for body in trailed_pattern_bodies() {
let cypher = format!("MATCH p = {body} WHERE x.id = 3 RETURN y.id AS y ORDER BY y");
take_node_reads();
let r = exec_cypher(&g, &cypher);
assert_eq!(r.rows.len(), 4, "{cypher}: two 1-hop and two 2-hop trails");
assert_eq!(
take_node_reads(),
1,
"{cypher}: the start point must be an index seek, not a label scan"
);
}
}
#[test]
fn trail_semantics_spellings_answer_identically() {
let g = person_ring();
let query = |body: &str| {
format!(
"MATCH p = {body} WHERE x.id = 3 \
RETURN y.id AS y, length(p) AS l, [r IN relationships(p) | r.k] AS ks \
ORDER BY y, l, ks"
)
};
let reference = rows_of(&g, &query(PATTERN_BODIES[0]));
assert_eq!(reference.len(), 4, "two 1-hop and two 2-hop trails");
for body in PATTERN_BODIES[1..].iter() {
assert_eq!(
rows_of(&g, &query(body)),
reference,
"the {body} spelling must answer identically"
);
}
assert_eq!(
rows_of(&g, &query(&format!("TRAIL {}", PATTERN_BODIES[0]))),
reference,
"the TRAIL-prefixed spelling must answer identically"
);
}
#[test]
fn walk_mode_is_the_rel_reuse_superset() {
let g = person_ring();
let query = |prefix: &str| {
format!(
"MATCH {prefix}(x:Person)-[:knows*1..2]-(y) WHERE x.id = 3 \
RETURN y.id AS y, length(p) AS l ORDER BY y, l"
)
};
let trail = rows_of(&g, &query("p = "));
let walk = rows_of(&g, &query("REPEATABLE ELEMENTS p = WALK "));
let reuse_rows = walk
.iter()
.filter(|row| row[0] == Value::Int(3) && row[1] == Value::Int(2))
.count();
assert_eq!(reuse_rows, 2, "the two rel-reusing 2-walks back to x");
assert_eq!(walk.len(), trail.len() + reuse_rows);
}
#[test]
fn zero_lower_bound_and_unbounded_spellings_agree() {
let g = person_ring();
let query = |body: &str| {
format!(
"MATCH p = {body} WHERE x.id = 3 \
RETURN y.id AS y, length(p) AS l ORDER BY y, l"
)
};
let zero_hop = rows_of(&g, &query("(x:Person)-[:knows*0..2]-(y)"));
assert_eq!(
zero_hop.len(),
5,
"the zero-hop row plus the four 1..2 trails"
);
assert!(
zero_hop
.iter()
.any(|row| row[0] == Value::Int(3) && row[1] == Value::Int(0)),
"the zero-hop row binds y to x with an empty path"
);
assert_eq!(
rows_of(&g, &query("(x:Person)(()-[:knows]-()){0,2}(y)")),
zero_hop
);
let unbounded = rows_of(&g, &query("(x:Person)-[:knows*]->(y)"));
assert_eq!(
rows_of(&g, &query("(x:Person)(()-[:knows]->()){1,}(y)")),
unbounded,
"the unbounded group spelling must agree with the legacy `*`"
);
let longest = unbounded
.iter()
.map(|row| row[1].as_int().expect("length is an integer"))
.max();
assert_eq!(longest, Some(6), "the full ring tour is the longest trail");
}
#[test]
fn chained_var_length_with_interior_node_agrees() {
let person = &["Person"][..];
let place = &["Place"][..];
let labels: [&[&str]; 5] = [person, person, place, person, person];
let built = build(
&labels,
&[
(0, 1, "knows"),
(1, 2, "knows"),
(2, 3, "knows"),
(3, 4, "knows"),
],
);
let g = &built.graph;
let query = |body: &str| {
format!(
"MATCH p = {body} WHERE a.id = 0 \
RETURN b.id AS b, length(p) AS l ORDER BY b, l"
)
};
let legacy = rows_of(
g,
&query("(a:Person)-[:knows*1..2]->(m:Place)-[:knows*1..2]->(b:Person)"),
);
assert_eq!(legacy.len(), 2);
assert_eq!(
rows_of(
g,
&query("(a:Person)(()-[:knows]->()){1,2}(m:Place)(()-[:knows]->()){1,2}(b:Person)")
),
legacy
);
}
#[test]
fn every_spelling_takes_the_gpm_engine_path() {
for body in PATTERN_BODIES {
let query = relate_front::parse_and_lower(&format!("MATCH p = {body}")).unwrap();
let relate_ir::Clause::Match { patterns, .. } = &query.parts[0].clauses[0] else {
panic!("{body}: expected a MATCH");
};
assert!(
patterns[0].needs_gpm(),
"{body}: must route to the GPM engine"
);
}
}
#[test]
fn repeatable_unbounded_dfs_cap_is_one_rule_across_spellings() {
let g = person_ring();
let bounded = "MATCH REPEATABLE ELEMENTS p = (x:Person)((_)-[:knows]-()){1,8}(y) \
WHERE x.id = 3 RETURN y.id AS y, length(p) AS l";
let unbounded_query = |legacy_rel: bool| {
let mut query = relate_front::parse_and_lower(bounded).unwrap();
let relate_ir::Clause::Match { patterns, .. } = &mut query.parts[0].clauses[0] else {
panic!("{bounded}: expected a MATCH");
};
let elements = &mut patterns[0].elements;
let index = elements
.iter()
.position(|element| matches!(element, relate_ir::PatternElement::Quantified(_)))
.unwrap_or_else(|| panic!("{bounded}: expected a quantified group"));
if legacy_rel {
let (min, rel) = match &elements[index] {
relate_ir::PatternElement::Quantified(qpp) => {
let relate_ir::PatternElement::Relationship(rel) = &qpp.elements[1] else {
panic!("{bounded}: the group's middle element must be the rel");
};
(qpp.min, rel.clone())
}
_ => unreachable!("the position search found a quantified group"),
};
let mut var_length = rel;
var_length.var_length = true;
var_length.min_hops = min;
var_length.max_hops = None;
elements[index] = relate_ir::PatternElement::Relationship(var_length);
} else {
let relate_ir::PatternElement::Quantified(qpp) = &mut elements[index] else {
unreachable!("the position search found a quantified group");
};
qpp.max = None;
}
query
};
let rows = |legacy_rel: bool| {
let mut rows =
crate::exec::test_support::exec_lowered(&g, unbounded_query(legacy_rel), bounded).rows;
rows.sort_by(|a, b| format!("{a:?}").cmp(&format!("{b:?}")));
rows
};
let quantified = rows(false);
let legacy = rows(true);
assert_eq!(
legacy, quantified,
"the two spellings of one unbounded pattern under REPEATABLE ELEMENTS \
must enumerate identically"
);
let longest = |rows: &Vec<Vec<Value>>| {
rows.iter()
.map(|row| row[1].as_int().expect("length(p) is an integer"))
.max()
};
assert_eq!(longest(&quantified), Some(7));
}