use crate::row::Row;
use relate_front::parse_and_lower;
use relate_ir::{Clause, Expr, MatchMode, PathPattern};
use relate_names::SmallName;
use super::matcher::*;
use super::program::{NodeTarget, Prog};
fn planned(cypher: &str) -> (Vec<PathPattern>, Vec<Expr>) {
let query = parse_and_lower(cypher).unwrap();
let plan = relate_planner::plan(query, &mut |_| false).unwrap();
match &plan.parts[0][0] {
Clause::Match {
patterns,
where_clause: Some(w),
..
} => (
patterns.clone(),
crate::exec::pushdown::flatten_and_owned(w),
),
Clause::Match { patterns, .. } => (patterns.clone(), Vec::new()),
other => panic!("not a MATCH: {other:?}"),
}
}
fn family_of(cypher: &str) -> Option<&'static str> {
let (patterns, conjuncts) = planned(cypher);
match compile(
&patterns,
MatchMode::DifferentRelationships,
&Row::new(),
&conjuncts,
) {
Some(Contract::Chain(_)) => Some("chain"),
None => None,
}
}
fn matched(cypher: &str, want: &str) {
let got = family_of(cypher).unwrap_or_else(|| panic!("{cypher} should match a family"));
assert_eq!(got, want, "{cypher}");
}
fn declined(cypher: &str) {
assert!(family_of(cypher).is_none(), "{cypher} should decline");
}
#[test]
fn matched_plan_shapes() {
matched(
"MATCH (a:Person)-[:knows]->(b:Person)-[:studyAt]->(o:Organisation) RETURN a",
"chain",
);
matched(
"MATCH (a)-[:x]->(b)-[:y]->(c)-[:z]->(d) WHERE b.k > 1 RETURN a",
"chain",
);
matched(
"MATCH (a:Person)-[:knows]->(b)-[:studyAt]->(o:Organisation) \
WHERE o.name = 'MIT' RETURN a",
"chain",
);
matched("MATCH ()-[:x]->()-[:y]->() RETURN 1", "chain");
matched("MATCH (:X)-[:x]->(:Y)-[:y]->(:Z) RETURN 1", "chain");
matched(
"MATCH (a:Person {ID: 7, k: 1})-[:x]->(b)-[:y]->(c) RETURN a",
"chain",
);
matched(
"MATCH (a)-[:x]->(b)-[:y]->(c) WHERE b.k > 1 AND b.j < 2 RETURN a",
"chain",
);
matched(
"MATCH (a)-[:x]->(b)-[:y]->(c) WHERE b.k = 1 OR NOT b.j IS NULL RETURN a",
"chain",
);
matched("MATCH (a)-[:x]->(b) RETURN a", "chain");
matched(
"MATCH (a)-[:x]->(b)-[:y]->(c)-[:z]->(d)-[:w]->(e)-[:v]->(f) RETURN a",
"chain",
);
matched("MATCH (a)-[:x]->(a)-[:y]->(c) RETURN a", "chain");
}
#[test]
fn unmatched_plan_shapes() {
declined("MATCH (c:Comment) WHERE c.k > 1 RETURN c");
declined("MATCH (a)-[:x*40]->(b) RETURN a");
declined("MATCH (a)-[:x*2]->(b)-[:y]->(c) RETURN a");
declined("MATCH (a)((n)-[:x]->(m)){1,2}(b) RETURN a");
declined("MATCH p = (a)-[:x]->(b)-[:y]->(c) RETURN p");
declined("MATCH (a)-[:x {k: 1}]->(b)-[:y]->(c) RETURN a");
declined("MATCH (a)-[r:x]->(b)-[r:y]->(c) RETURN a");
declined("MATCH (a:Person|Place)-[:x]->(b)-[:y]->(c) RETURN a");
declined("MATCH (a)-[:x]->(b)-[:y]->(c) WHERE size(b.k) > 1 RETURN a");
declined("MATCH (c)-[:a]->(t), (c)-[:b]->(l), (c)-[:d]->(m) RETURN c");
declined("MATCH (c)-[:a]->(t), (d)-[:b]->(l) RETURN c");
declined("MATCH (a:Person)-[:x]->(b)-[:y]->(c)-[:z]->(d), (a)-[:w]->(e) RETURN a");
declined("MATCH (c)-[r:a]->(t), (c)-[r:b]->(l) RETURN c");
declined("MATCH (a), (a)-[:x]->(b) RETURN a");
declined("MATCH (a)-[:x]->(b), (a) RETURN a");
declined("MATCH (a), (a) RETURN a");
declined("MATCH ()-[:a]->(), ()-[:b]->() RETURN 1");
}
#[test]
fn row_and_mode_gates_decline() {
let (patterns, conjuncts) = planned("MATCH (a)-[:x]->(b)-[:y]->(c) RETURN a");
let carrying = Row::from_iter(vec![("outer".to_string(), relate_core::Value::Null)]);
assert!(compile(
&patterns,
MatchMode::DifferentRelationships,
&carrying,
&conjuncts
)
.is_none());
assert!(compile(
&patterns,
MatchMode::RepeatableElements,
&Row::new(),
&conjuncts
)
.is_none());
assert!(compile(
&patterns,
MatchMode::DifferentRelationships,
&Row::new(),
&conjuncts
)
.is_some());
}
#[test]
fn chain_hops_carry_seed_and_target_patterns() {
let (patterns, conjuncts) = planned(
"MATCH (a:Person)-[:knows]->(b)-[:studyAt]->(o:Organisation) \
WHERE o.name = 'MIT' RETURN a",
);
let Some(Contract::Chain(chain)) = compile(
&patterns,
MatchMode::DifferentRelationships,
&Row::new(),
&conjuncts,
) else {
panic!("the chain shape compiles");
};
assert_eq!(chain.hops.len(), 2);
let is_org = |target: &NodeTarget<'_>| {
target
.pattern
.label_names()
.any(|label| *label == SmallName::from_name("Organisation"))
};
let org = if is_org(&chain.start) {
&chain.start
} else {
chain
.hops
.iter()
.map(|hop| &hop.target)
.find(|target| is_org(target))
.expect("the Organisation is one of the chain's nodes")
};
assert!(
matches!(org.prog, Prog::Cmp { .. }),
"the folded equality compiled onto the Organisation"
);
let others: Vec<_> = std::iter::once(&chain.start)
.chain(chain.hops.iter().map(|hop| &hop.target))
.filter(|target| !is_org(target))
.collect();
assert_eq!(others.len(), 2);
assert!(others
.iter()
.all(|target| matches!(target.prog, Prog::Bare)));
}
#[test]
fn two_pattern_queries_decline_contracted_kernels() {
let (patterns, conjuncts) = planned(
"MATCH (c:Comment)-[:hasCreator]->(p:Person), \
(c)-[:replyOf]->(post:Post)-[:postHasCreator]->(p) RETURN c",
);
let contract = compile(
&patterns,
MatchMode::DifferentRelationships,
&Row::new(),
&conjuncts,
);
assert!(
contract.is_none(),
"two patterns decline kernel compilation"
);
}
#[test]
fn hub_conjunct_declines_kernel_to_hash_join() {
let (patterns, conjuncts) = planned(
"MATCH (c:Comment)-[:hasTag]->(t:Tag), (c)-[:locatedIn]->(l:Place) \
WHERE c.k > 1 RETURN c",
);
let contract = compile(
&patterns,
MatchMode::DifferentRelationships,
&Row::new(),
&conjuncts,
);
assert!(contract.is_none(), "hub star declines to hash join");
}