use std::collections::BTreeMap;
use relate_core::Value;
use super::{take_hash_join_engagements, with_hash_join_disabled, with_hash_join_forced};
use crate::exec::test_support::{exec_cypher, profiled, same_rows, try_exec_cypher_params};
use crate::graph::Graph;
fn build_varlen_geography_graph(persons: usize) -> Graph {
let g = Graph::default();
g.begin_write().unwrap();
let us = g
.create_node(
vec!["Country".into()],
BTreeMap::from([("country".into(), Value::String("US".into()))]),
)
.unwrap();
let de = g
.create_node(
vec!["Country".into()],
BTreeMap::from([("country".into(), Value::String("DE".into()))]),
)
.unwrap();
let wa = g
.create_node(
vec!["State".into()],
BTreeMap::from([("name".into(), Value::String("WA".into()))]),
)
.unwrap();
let by = g
.create_node(
vec!["State".into()],
BTreeMap::from([("name".into(), Value::String("BY".into()))]),
)
.unwrap();
g.create_rel(wa, us, "STATE_IN".into(), BTreeMap::new())
.unwrap();
g.create_rel(by, de, "STATE_IN".into(), BTreeMap::new())
.unwrap();
let sea = g
.create_node(
vec!["City".into()],
BTreeMap::from([("name".into(), Value::String("Seattle".into()))]),
)
.unwrap();
let muc = g
.create_node(
vec!["City".into()],
BTreeMap::from([("name".into(), Value::String("Munich".into()))]),
)
.unwrap();
g.create_rel(sea, wa, "CITY_IN".into(), BTreeMap::new())
.unwrap();
g.create_rel(sea, wa, "CITY_IN".into(), BTreeMap::new())
.unwrap();
g.create_rel(muc, by, "CITY_IN".into(), BTreeMap::new())
.unwrap();
g.create_rel(muc, de, "CITY_IN".into(), BTreeMap::new())
.unwrap();
for i in 0..persons {
let p_sea = g
.create_node(
vec!["Person".into()],
BTreeMap::from([
("age".into(), Value::Int(i as i64)),
("name".into(), Value::String(format!("sea_p_{i}").into())),
]),
)
.unwrap();
g.create_rel(p_sea, sea, "LIVES_IN".into(), BTreeMap::new())
.unwrap();
let p_muc = g
.create_node(
vec!["Person".into()],
BTreeMap::from([
("age".into(), Value::Int(i as i64)),
("name".into(), Value::String(format!("muc_p_{i}").into())),
]),
)
.unwrap();
g.create_rel(p_muc, muc, "LIVES_IN".into(), BTreeMap::new())
.unwrap();
}
g.commit_write();
g
}
#[test]
fn junction_split_hash_join_parity() {
let g = build_varlen_geography_graph(80);
let query = "MATCH (p:Person)-[:LIVES_IN]->(c:City)-[:CITY_IN|STATE_IN*1..2]->(co:Country) \
WHERE co.country = 'US' \
RETURN p.age AS age, c.name AS city, co.country AS country \
ORDER BY age, city, country";
with_hash_join_forced(|| {
let _ = take_hash_join_engagements();
let joined = exec_cypher(&g, query);
assert!(
take_hash_join_engagements() >= 1,
"junction split hash join engaged"
);
let nested = with_hash_join_disabled(|| exec_cypher(&g, query));
assert!(same_rows(&joined, &nested), "junction split matches walker");
assert_eq!(joined.rows.len(), 160);
});
}
#[test]
fn junction_split_multiplicity_mode() {
let g = build_varlen_geography_graph(80);
let query = "MATCH (p:Person)-[:LIVES_IN]->(c:City)-[:CITY_IN|STATE_IN*1..2]->(co:Country) \
WHERE co.country = 'US' \
RETURN count(p) AS total";
with_hash_join_forced(|| {
let _ = take_hash_join_engagements();
let joined = exec_cypher(&g, query);
assert!(
take_hash_join_engagements() >= 1,
"junction split multiplicity mode engaged"
);
let nested = with_hash_join_disabled(|| exec_cypher(&g, query));
assert!(
same_rows(&joined, &nested),
"multiplicity mode matches nested loop"
);
assert_eq!(joined.rows, vec![vec![Value::Int(160)]]);
});
}
#[test]
fn junction_split_with_limit_early_halt() {
let g = build_varlen_geography_graph(80);
let query = "MATCH (p:Person)-[:LIVES_IN]->(c:City)-[:CITY_IN|STATE_IN*1..2]->(co:Country) \
WHERE co.country = 'US' \
RETURN p.age AS age LIMIT 15";
with_hash_join_forced(|| {
let _ = take_hash_join_engagements();
let joined = exec_cypher(&g, query);
assert!(
take_hash_join_engagements() >= 1,
"junction split with LIMIT engaged"
);
let nested = with_hash_join_disabled(|| exec_cypher(&g, query));
assert_eq!(joined.rows.len(), 15);
assert_eq!(joined.rows.len(), nested.rows.len());
});
}
#[test]
fn junction_split_profile_metrics_and_perturbations() {
let g = build_varlen_geography_graph(80);
let base = "MATCH (p:Person)-[:LIVES_IN]->(c:City)-[:CITY_IN|STATE_IN*1..2]->(co:Country) \
WHERE co.country = 'DE' \
RETURN count(p) AS total";
let inline = "MATCH (p:Person)-[:LIVES_IN]->(c:City)-[:CITY_IN|STATE_IN*1..2]->(co:Country {country: 'DE'}) \
RETURN count(p) AS total";
let or_not = "MATCH (p:Person)-[:LIVES_IN]->(c:City)-[:CITY_IN|STATE_IN*1..2]->(co:Country) \
WHERE co.country = 'DE' OR NOT co.country = 'US' \
RETURN count(p) AS total";
for (desc, q) in [("base", base), ("inline", inline), ("or_not", or_not)] {
with_hash_join_forced(|| {
let _ = take_hash_join_engagements();
let records = profiled(&g, q);
assert!(
take_hash_join_engagements() >= 1,
"HashJoin engaged on perturbation: {desc}"
);
let hj_rec = records.iter().find(|r| r.hash_join.build_rows > 0);
assert!(
hj_rec.is_some(),
"PROFILE records contain hash_join metrics for {desc}"
);
let hj = hj_rec.unwrap().hash_join;
assert!(hj.build_rows > 0, "build_rows tracked for {desc}");
assert!(hj.probe_rows > 0, "probe_rows tracked for {desc}");
});
}
let param_q = "MATCH (p:Person)-[:LIVES_IN]->(c:City)-[:CITY_IN|STATE_IN*1..2]->(co:Country) \
WHERE co.country = $c \
RETURN count(p) AS total";
let mut params = BTreeMap::new();
params.insert("c".into(), Value::String("DE".into()));
with_hash_join_forced(|| {
let res_lit = exec_cypher(&g, base);
let res_param = try_exec_cypher_params(&g, param_q, ¶ms).unwrap();
assert!(
same_rows(&res_lit, &res_param),
"parameterized query matches literal"
);
});
}
#[test]
fn junction_split_overlapping_rel_types_different_relationships_parity() {
let g = Graph::default();
g.begin_write().unwrap();
let a = g
.create_node(
vec!["Node".into()],
BTreeMap::from([("id".into(), Value::Int(1))]),
)
.unwrap();
let b = g
.create_node(
vec!["Node".into()],
BTreeMap::from([("id".into(), Value::Int(2))]),
)
.unwrap();
let c = g
.create_node(
vec!["Node".into()],
BTreeMap::from([("id".into(), Value::Int(3))]),
)
.unwrap();
g.create_rel(a, b, "R".into(), BTreeMap::new()).unwrap();
g.create_rel(b, c, "R".into(), BTreeMap::new()).unwrap();
g.create_rel(c, a, "R".into(), BTreeMap::new()).unwrap();
g.commit_write();
let query = "UNWIND [1, 2, 3] AS id MATCH (n:Node {id: id}) WITH n \
MATCH (n)-[:R]->(m:Node)-[:R*1..2]->(end:Node) \
RETURN n.id AS nid, end.id AS end_id ORDER BY nid, end_id";
with_hash_join_forced(|| {
let joined = exec_cypher(&g, query);
let nested = with_hash_join_disabled(|| exec_cypher(&g, query));
assert!(
same_rows(&joined, &nested),
"rows mode with overlapping rels preserves relationship uniqueness"
);
});
}
#[test]
fn junction_split_shared_non_junction_variable_declines_to_walker() {
let g = Graph::default();
g.begin_write().unwrap();
let a = g
.create_node(
vec!["Node".into()],
BTreeMap::from([("id".into(), Value::Int(1))]),
)
.unwrap();
let c = g
.create_node(
vec!["Node".into()],
BTreeMap::from([("id".into(), Value::Int(2))]),
)
.unwrap();
g.create_rel(a, c, "R".into(), BTreeMap::new()).unwrap();
g.create_rel(c, a, "R".into(), BTreeMap::new()).unwrap();
g.commit_write();
let query = "MATCH (a:Node)-[:R]->(c:Node)-[:R*1..2]->(a) RETURN a.id AS aid ORDER BY aid";
let res = exec_cypher(&g, query);
let nested = with_hash_join_disabled(|| exec_cypher(&g, query));
assert!(same_rows(&res, &nested));
assert_eq!(res.rows, vec![vec![Value::Int(1)], vec![Value::Int(2)]]);
}