use super::*;
fn message_graph() -> Database {
let mut db = Database::new();
for id in 1..=4 {
db.query(&format!("CREATE (:Person {{id: {id}}})")).unwrap();
}
for name in ["X", "Y", "Z"] {
db.query(&format!("CREATE (:Country {{name: '{name}'}})"))
.unwrap();
}
for (source, target) in [(1, 2), (1, 3), (2, 4)] {
db.query(&format!(
"MATCH (a:Person {{id: {source}}}), (b:Person {{id: {target}}}) CREATE (a)-[:KNOWS]->(b)"
))
.unwrap();
}
for (message, creator, date, country) in [
("m1", 2, Some(5), "X"),
("m2", 2, Some(12), "X"),
("m3", 2, Some(15), "Z"),
("m4", 3, Some(19), "Y"),
("m5", 3, Some(25), "Y"),
("m6", 3, None, "X"),
("m7", 4, Some(10), "X"),
("m8", 4, Some(20), "Y"),
("m9", 1, Some(12), "X"),
] {
let label = if message == "m4" { "Comment" } else { "Post" };
let date = date
.map(|date| format!(", creationDate: {date}"))
.unwrap_or_default();
db.query(&format!("CREATE (:{label} {{id: '{message}'{date}}})"))
.unwrap();
db.query(&format!(
"MATCH (m:{label} {{id: '{message}'}}), (p:Person {{id: {creator}}}) CREATE (m)-[:HAS_CREATOR]->(p)"
))
.unwrap();
db.query(&format!(
"MATCH (m:{label} {{id: '{message}'}}), (c:Country {{name: '{country}'}}) CREATE (m)-[:IS_LOCATED_IN]->(c)"
))
.unwrap();
}
db
}
fn rows(db: &mut Database, query: &str, parameters: &BTreeMap<String, Value>) -> Vec<String> {
let output = db.query_with_params(query, parameters).unwrap();
let mut rows = output
.rows
.iter()
.map(|row| {
row.iter()
.map(|(column, value)| format!("{column}={value:?}"))
.collect::<Vec<_>>()
.join(", ")
})
.collect::<Vec<_>>();
rows.sort();
rows
}
fn rows_and_visits(
db: &mut Database,
query: &str,
parameters: &BTreeMap<String, Value>,
) -> (Vec<String>, u64) {
let before = hawdb_storage::store::adjacency_visits_for_test();
let rows = rows(db, query, parameters);
(
rows,
hawdb_storage::store::adjacency_visits_for_test() - before,
)
}
fn expected(rows: &[&str]) -> Vec<String> {
let mut rows = rows.iter().map(ToString::to_string).collect::<Vec<_>>();
rows.sort();
rows
}
fn dates() -> BTreeMap<String, Value> {
BTreeMap::from([
("startDate".to_string(), Value::Int(10)),
("endDate".to_string(), Value::Int(20)),
])
}
fn last_graph_match(db: &Database, query: &str, parameters: &BTreeMap<String, Value>) -> String {
let explain = db.explain_query_with_params(query, parameters).unwrap();
let plan = explain.physical_plan.explain(0);
plan.lines()
.find(|line| line.contains("GraphMatchExec"))
.unwrap_or_else(|| panic!("no GraphMatchExec in:\n{plan}"))
.to_string()
}
const IC3_FRIENDS: &str = "MATCH (countryX:Country {name: 'X'}), (countryY:Country {name: 'Y'}), (person:Person {id: 1}) \
WITH person, countryX, countryY \
MATCH (person)-[:KNOWS*1..2]-(friend:Person) WHERE NOT person = friend \
WITH DISTINCT friend, countryX, countryY ";
fn ic3(filter: &str) -> String {
format!(
"{IC3_FRIENDS}MATCH (friend)<-[:HAS_CREATOR]-(message), (message)-[:IS_LOCATED_IN]->(country) \
WHERE {filter} \
RETURN friend.id AS friend, message.id AS message, country.name AS country"
)
}
#[test]
fn ic3_date_conjunct_runs_before_the_country_expansion() {
let mut db = message_graph();
let dated =
ic3("$endDate > message.creationDate >= $startDate AND country IN [countryX, countryY]");
let line = last_graph_match(&db, &dated, &dates());
assert!(line.contains("predicate_after_steps=[2, 2, 4]"), "{line}");
let explain = db.explain_query_with_params(&dated, &dates()).unwrap();
assert!(
explain.trace.decisions.iter().any(|decision| decision
== "evaluate 2 of 3 GraphMatchExec WHERE conjunct(s) before the last step: after steps [2, 2, 4] of 4"),
"{:?}",
explain.trace.decisions
);
let (dated_rows, dated_visits) = rows_and_visits(&mut db, &dated, &dates());
assert_eq!(
dated_rows,
expected(&[
"friend=Int(2), message=String(\"m2\"), country=String(\"X\")",
"friend=Int(3), message=String(\"m4\"), country=String(\"Y\")",
"friend=Int(4), message=String(\"m7\"), country=String(\"X\")",
])
);
let (_, undated_visits) =
rows_and_visits(&mut db, &ic3("country IN [countryX, countryY]"), &dates());
assert_eq!(undated_visits - dated_visits, 4);
}
#[test]
fn ic3_rows_match_the_end_of_match_filter() {
let mut db = message_graph();
for (filter, rows_expected) in [
(
"message.creationDate >= 15 AND country IN [countryX, countryY]",
vec![
"friend=Int(3), message=String(\"m4\"), country=String(\"Y\")",
"friend=Int(3), message=String(\"m5\"), country=String(\"Y\")",
"friend=Int(4), message=String(\"m8\"), country=String(\"Y\")",
],
),
(
"friend.id = 2 AND message.creationDate < 13",
vec![
"friend=Int(2), message=String(\"m1\"), country=String(\"X\")",
"friend=Int(2), message=String(\"m2\"), country=String(\"X\")",
],
),
(
"NOT message.creationDate < 15 AND country = countryX",
vec![],
),
(
"message.creationDate IS NULL AND country = countryX",
vec!["friend=Int(3), message=String(\"m6\"), country=String(\"X\")"],
),
(
"(message.creationDate = 5 OR country.name = 'Z') AND friend.id <> 3",
vec![
"friend=Int(2), message=String(\"m1\"), country=String(\"X\")",
"friend=Int(2), message=String(\"m3\"), country=String(\"Z\")",
],
),
(
"message.id IN [x IN ['m2', 'm4', 'm9'] WHERE x <> 'm4'] AND country.name = 'X'",
vec!["friend=Int(2), message=String(\"m2\"), country=String(\"X\")"],
),
] {
assert_eq!(
rows(&mut db, &ic3(filter), &dates()),
expected(&rows_expected),
"{filter}"
);
}
}
#[test]
fn optional_match_pads_rows_whose_candidates_all_fail_an_early_conjunct() {
let mut db = message_graph();
let query = "MATCH (p:Person) WITH p WITH p \
OPTIONAL MATCH (p)<-[:HAS_CREATOR]-(m)-[:IS_LOCATED_IN]->(c) \
WHERE m.creationDate >= 15 AND c.name = 'Y' \
RETURN p.id AS person, m.id AS message";
let line = last_graph_match(&db, query, &BTreeMap::new());
assert!(line.contains("optional=true"), "{line}");
assert!(line.contains("predicate_after_steps=[2, 3]"), "{line}");
assert_eq!(
rows(&mut db, query, &BTreeMap::new()),
expected(&[
"person=Int(1), message=Null",
"person=Int(2), message=Null",
"person=Int(3), message=String(\"m4\")",
"person=Int(3), message=String(\"m5\")",
"person=Int(4), message=String(\"m8\")",
])
);
let query = "MATCH (p:Person) WITH p WITH p \
OPTIONAL MATCH (p)<-[:HAS_CREATOR]-(m) WHERE p.id > 3 AND m.creationDate > 0 \
RETURN p.id AS person, m.id AS message";
assert!(last_graph_match(&db, query, &BTreeMap::new()).contains("predicate_after_steps=[0, 2]"));
assert_eq!(
rows(&mut db, query, &BTreeMap::new()),
expected(&[
"person=Int(1), message=Null",
"person=Int(2), message=Null",
"person=Int(3), message=Null",
"person=Int(4), message=String(\"m7\")",
"person=Int(4), message=String(\"m8\")",
])
);
}
#[test]
fn variable_length_and_trail_semantics_are_unchanged_by_early_conjuncts() {
let mut db = message_graph();
let query = "MATCH (p:Person {id: 1}) WITH p WITH p \
MATCH (p)-[:KNOWS*1..2]-(friend)-[k:KNOWS]-(other) \
WHERE friend.id <> 3 AND other.id <> 4 \
RETURN friend.id AS friend, other.id AS other";
let line = last_graph_match(&db, query, &BTreeMap::new());
assert!(line.contains("predicate_after_steps=[2, 3]"), "{line}");
assert_eq!(rows(&mut db, query, &BTreeMap::new()), Vec::<String>::new());
let query = "MATCH (p:Person {id: 1}) WITH p WITH p \
MATCH (p)-[:KNOWS*1..2]-(friend)-[k:KNOWS]-(other) \
WHERE friend.id <> 3 \
RETURN friend.id AS friend, other.id AS other";
assert_eq!(
rows(&mut db, query, &BTreeMap::new()),
expected(&["friend=Int(2), other=Int(4)"])
);
}
#[test]
fn anchored_optional_match_places_conjuncts_after_the_reordered_steps() {
let mut db = Database::new();
for (label, id) in [("Forum", 1), ("Forum", 2), ("Person", 2), ("Person", 3)] {
db.query(&format!("CREATE (:{label} {{id: {id}}})"))
.unwrap();
}
for (id, year) in [(10, 2021), (11, 2018), (12, 2022)] {
db.query(&format!("CREATE (:Post {{id: {id}, year: {year}}})"))
.unwrap();
}
for (forum, post, person) in [(1, 10, 2), (1, 11, 3), (2, 12, 2)] {
db.query(&format!(
"MATCH (f:Forum {{id: {forum}}}), (m:Post {{id: {post}}}) \
CREATE (f)-[:CONTAINER_OF]->(m)"
))
.unwrap();
db.query(&format!(
"MATCH (m:Post {{id: {post}}}), (p:Person {{id: {person}}}) \
CREATE (m)-[:HAS_CREATOR]->(p)"
))
.unwrap();
}
let prefix = "MATCH (f:Forum) WITH f, f.id AS id WITH f ";
let suffix = "WHERE m.year > $minYear AND f.id = 1 AND p.id = 2 \
RETURN f.id AS f, m.id AS m, p.id AS p";
let query = format!(
"{prefix} OPTIONAL MATCH (p:Person)<-[:HAS_CREATOR]-(m:Post:Comment)<-[:CONTAINER_OF]-(f) {suffix}"
);
let reference = format!(
"{prefix} OPTIONAL MATCH (f)-[:CONTAINER_OF]->(m:Post:Comment)-[:HAS_CREATOR]->(p:Person) {suffix}"
);
let parameters = BTreeMap::from([("minYear".into(), Value::Int(2020))]);
let actual = rows(&mut db, &query, ¶meters);
assert_eq!(
actual,
expected(&["f=Int(1), m=Int(10), p=Int(2)", "f=Int(2), m=Null, p=Null"])
);
assert_eq!(actual, rows(&mut db, &reference, ¶meters));
let plan = last_graph_match(&db, &query, ¶meters);
assert!(
plan.contains("steps=[Node(GraphMatchNode { variable: \"f\""),
"{plan}"
);
assert!(plan.contains("predicate_after_steps=[2, 0, 3]"), "{plan}");
assert!(plan.contains("projected_nodes="), "{plan}");
assert!(plan.contains("\"m\": {\"id\", \"year\"}"), "{plan}");
}