use std::collections::BTreeMap;
use relate_core::Value;
use relate_ir::{MatchMode, NodePattern, PatternElement, RelPattern};
use super::super::graph_stats::GraphStats;
use super::super::test_support::Builder;
use super::frontier_match::{compile_frontier, BoundaryPair, Consumer};
const DISTINCT: Consumer = Consumer::DistinctEndpoints {
pair: BoundaryPair::Unconstrained,
};
use super::nfa::Pool;
use crate::exec::test_support::{
complete_graph, decide, declines_with, line_graph, named_node, one_hop_rel, path_pattern,
quantified, var_length_rel,
};
use crate::frontier_decline::FrontierDecline;
use crate::graph::Graph;
use crate::row::Row;
fn corpus_graph() -> Graph {
let b = Builder::new();
let persons: Vec<_> = (0..5)
.map(|i| b.node(&format!("p{i}"), &["Person"]))
.collect();
b.rel(persons[0], persons[1], "KNOWS", &[]);
b.rel(persons[1], persons[2], "KNOWS", &[]);
b.rel(persons[2], persons[0], "KNOWS", &[]);
b.rel(persons[3], persons[4], "KNOWS", &[]);
let comment = b.node("c0", &["Comment"]);
let org = b.node("o0", &["Organisation"]);
b.rel(persons[0], comment, "HAS_CREATOR", &[]);
b.rel(persons[1], comment, "LIKE", &[]);
b.rel(persons[0], org, "STUDY_AT", &[]);
b.done()
}
fn engages(cypher: &str, graph: &Graph) -> bool {
decide(cypher, graph, false, &Row::new(), |decision| {
decision.is_ok()
})
}
#[test]
fn engages_the_timeout_classes() {
let graph = corpus_graph();
decide(
"MATCH p = (x:Person)-[:KNOWS*1..]-(y) WHERE x.ID = 1 RETURN p LIMIT 100",
&graph,
false,
&Row::new(),
|decision| {
let contract = decision.unwrap_or_else(|e| panic!("q9 engages: {e:?}"));
assert_eq!(contract.consumer, Consumer::Capped { want: 100 });
assert_eq!((contract.nfa.min_hops, contract.nfa.max_hops), (1, None));
assert_eq!(
contract.pool,
Pool::Default,
"the default pool is trail semantics"
);
},
);
decide(
"MATCH p = (x:Person)((a)-[:LIKE]-(b)-[:HAS_CREATOR]-(c)){1,}(y) \
WHERE x.ID = 1 RETURN p LIMIT 100",
&graph,
false,
&Row::new(),
|decision| {
let contract = decision.unwrap_or_else(|e| panic!("q37 engages: {e:?}"));
assert_eq!(contract.consumer, Consumer::Capped { want: 100 });
assert_eq!(contract.nfa.states, 2);
assert_eq!(contract.nfa.max_hops, None);
},
);
for cypher in [
"MATCH p = (x:Person)-[:KNOWS*1..]-()-[:STUDY_AT]-(y) WHERE x.ID = 1 \
RETURN p LIMIT 100",
"MATCH p = (x:Person)-[:KNOWS]-()-[:KNOWS*1..]-(y) WHERE x.ID = 1 \
RETURN p LIMIT 100",
] {
decide(cypher, &graph, false, &Row::new(), |decision| {
assert!(decision.is_ok(), "{cypher} engages");
});
}
assert!(engages(
"MATCH p = (x:Person)-[:KNOWS|STUDY_AT*1..]-(y) WHERE x.ID = 1 \
RETURN p LIMIT 100",
&graph
));
}
#[test]
fn engages_distinct_endpoint_answers() {
let graph = corpus_graph();
for cypher in [
"MATCH (x:Person)-[:KNOWS*1..]-(y:Person) RETURN DISTINCT y",
"MATCH (x:Person)-[:KNOWS*1..]-(y:Person) RETURN count(DISTINCT y)",
"MATCH (x:Person)-[:KNOWS*1..]-(y:Person) RETURN collect(DISTINCT y)",
"MATCH (x:Person)-[:KNOWS*1..]-(y:Person) RETURN min(y.k)",
"MATCH (x:Person)-[:KNOWS*1..]-(y:Person) RETURN max(y.k)",
"MATCH (x:Person)-[:KNOWS*1..]-(y:Person) RETURN x, count(DISTINCT y)",
"MATCH (x:Person)-[:KNOWS*1..]-(y:Person) RETURN DISTINCT y LIMIT 5",
] {
decide(cypher, &graph, false, &Row::new(), |decision| {
let contract = decision.unwrap_or_else(|e| panic!("{cypher} engages: {e:?}"));
assert_eq!(
contract.consumer,
Consumer::DistinctEndpoints {
pair: BoundaryPair::Unconstrained
},
"{cypher}"
);
});
}
}
#[test]
fn engages_the_exists_probe_arm() {
let graph = corpus_graph();
decide(
"MATCH (x:Person)-[:KNOWS*0..]-(y:Person) RETURN y",
&graph,
true,
&Row::new(),
|decision| {
let contract = decision.unwrap_or_else(|e| panic!("the probe arm engages: {e:?}"));
assert_eq!(contract.consumer, Consumer::Exists);
assert_eq!(
contract.nfa.min_hops, 0,
"q12 knows* is the zero-length arm"
);
},
);
}
#[test]
fn the_cost_gate_prices_the_dedup_ratio() {
declines_with(
"MATCH (x:Person)-[:KNOWS*1..2]-(y:Person) RETURN DISTINCT y",
&line_graph(),
FrontierDecline::Cost,
);
decide(
"MATCH (x:Person)-[:KNOWS*1..3]-(y:Person) RETURN DISTINCT y",
&complete_graph(),
false,
&Row::new(),
|decision| {
let contract = decision.unwrap_or_else(|e| panic!("the bounded ratio engages: {e:?}"));
assert_eq!(contract.consumer, DISTINCT);
assert_eq!(
contract.nfa.max_hops,
Some(3),
"the bounded shape, rule (b)"
);
},
);
}
fn layered_graph() -> Graph {
let b = Builder::new();
let nodes: Vec<_> = (0..28)
.map(|i| b.node(&format!("n{i}"), &["Person"]))
.collect();
for layer in 0..6 {
for a in 0..4 {
for c in 0..4 {
b.rel(
nodes[layer * 4 + a],
nodes[(layer + 1) * 4 + c],
"KNOWS",
&[],
);
}
}
}
b.done()
}
#[test]
fn the_cost_gate_engages_a_branching_recursion_onto_an_unlabeled_target() {
let graph = layered_graph();
for cypher in [
"MATCH (x:Person)-[:KNOWS*1..6]->(y) RETURN count(DISTINCT y)",
"MATCH (x:Person)-[:KNOWS*1..6]->(y) RETURN collect(DISTINCT y)",
"MATCH (x:Person)-[:KNOWS*1..6]->(y:Person) RETURN count(DISTINCT y)",
] {
decide(cypher, &graph, false, &Row::new(), |decision| {
let contract = decision.unwrap_or_else(|e| panic!("{cypher} engages: {e:?}"));
assert_eq!(
contract.consumer,
Consumer::DistinctEndpoints {
pair: BoundaryPair::Unconstrained
},
"{cypher}"
);
});
}
}
#[test]
fn bounded_walk_mode_engages_with_walk_semantics() {
let graph = complete_graph();
decide(
"MATCH REPEATABLE ELEMENTS p = (x:Person)-[:KNOWS*1..3]-(y:Person) \
RETURN p LIMIT 5",
&graph,
false,
&Row::new(),
|decision| {
let contract = decision.unwrap_or_else(|e| panic!("bounded WALK engages: {e:?}"));
assert_eq!(
contract.pool,
Pool::Walk,
"the REPEATABLE pool is walk semantics"
);
assert_eq!(contract.consumer, Consumer::Capped { want: 5 });
},
);
decide(
"MATCH p = (x:Person)-[:KNOWS*1..3]-(y:Person) RETURN p LIMIT 5",
&graph,
false,
&Row::new(),
|decision| {
let contract = decision.unwrap_or_else(|e| panic!("the default pool: {e:?}"));
assert_eq!(contract.pool, Pool::Default);
},
);
decide(
"MATCH p = TRAIL (x:Person)-[:KNOWS*1..3]-(y:Person) RETURN p LIMIT 5",
&graph,
false,
&Row::new(),
|decision| {
let contract = decision.unwrap_or_else(|e| panic!("capped TRAIL engages: {e:?}"));
assert_eq!(
contract.pool,
Pool::Default,
"the default pool checks trails"
);
},
);
for mode in ["ACYCLIC", "SIMPLE"] {
decide(
&format!("MATCH p = {mode} (x:Person)-[:KNOWS*1..3]-(y:Person) RETURN p LIMIT 5"),
&graph,
false,
&Row::new(),
|decision| {
let contract = decision.unwrap_or_else(|e| panic!("capped {mode} engages: {e:?}"));
assert_eq!(
contract.pool,
Pool::Default,
"{mode} answers in the default pool"
);
},
);
}
for mode in ["TRAIL", "ACYCLIC", "SIMPLE"] {
declines_with(
&format!("MATCH p = {mode} (x:Person)-[:KNOWS*1..3]-(y:Person) RETURN DISTINCT y"),
&graph,
FrontierDecline::Mode,
);
}
{
let mut pattern = path_pattern(vec![
PatternElement::Node(named_node("x")),
PatternElement::Quantified(quantified(
vec![
PatternElement::Node(NodePattern::anonymous()),
PatternElement::Relationship(one_hop_rel(&["KNOWS"])),
PatternElement::Node(NodePattern::anonymous()),
],
1,
Some(3),
)),
PatternElement::Node(named_node("y")),
]);
pattern.mode = Some(relate_ir::PathMode::Acyclic);
let params: BTreeMap<String, Value> = BTreeMap::new();
let stats = GraphStats::new(&graph, ¶ms);
let patterns = [pattern];
let decision = compile_frontier(
&graph,
&patterns,
MatchMode::RepeatableElements,
&Row::new(),
&[],
Consumer::Capped { want: 5 },
&stats,
);
assert_eq!(decision.err(), Some(FrontierDecline::Mode));
}
declines_with(
"MATCH REPEATABLE ELEMENTS p = (x:Person)-[:KNOWS*1..3]-(y) RETURN p",
&graph,
FrontierDecline::FullEnumeration,
);
}
#[test]
fn declines_a_nested_quantified_spine() {
let graph = corpus_graph();
let nested = |inner: relate_ir::PatternElement| {
path_pattern(vec![
PatternElement::Node(named_node("x")),
PatternElement::Quantified(quantified(
vec![
PatternElement::Node(NodePattern::anonymous()),
inner,
PatternElement::Node(NodePattern::anonymous()),
],
1,
None,
)),
PatternElement::Node(named_node("y")),
])
};
let params: BTreeMap<String, Value> = BTreeMap::new();
let stats = GraphStats::new(&graph, ¶ms);
let var_length = nested(PatternElement::Relationship(var_length_rel(
&["KNOWS"],
1,
Some(2),
)));
let group = nested(PatternElement::Quantified(quantified(
vec![
PatternElement::Node(NodePattern::anonymous()),
PatternElement::Relationship(one_hop_rel(&["KNOWS"])),
PatternElement::Node(NodePattern::anonymous()),
],
1,
Some(2),
)));
for pattern in [var_length, group] {
let patterns = [pattern];
let decision = compile_frontier(
&graph,
&patterns,
MatchMode::DifferentRelationships,
&Row::new(),
&[],
Consumer::Exists,
&stats,
);
assert_eq!(decision.err(), Some(FrontierDecline::NestedSpine));
}
}
#[test]
fn the_capped_want_counts_skip() {
let graph = corpus_graph();
decide(
"MATCH p = (x:Person)-[:KNOWS*1..]-(y) WHERE x.ID = 1 \
RETURN p SKIP 10 LIMIT 5",
&graph,
false,
&Row::new(),
|decision| {
let contract = decision.unwrap_or_else(|e| panic!("SKIP+LIMIT engages: {e:?}"));
assert_eq!(contract.consumer, Consumer::Capped { want: 15 });
},
);
}
#[test]
fn declines_the_nfa_compiler_shapes() {
let graph = corpus_graph();
declines_with(
"MATCH p = (x)((a)-[r:KNOWS WHERE r.since < 2010]-(b)){1,}(y) RETURN p LIMIT 100",
&graph,
FrontierDecline::NfaShape,
);
let mut ceiling = String::from("MATCH p = (x)((a)");
for _ in 0..65 {
ceiling.push_str("-[:KNOWS]-()");
}
ceiling.push_str("){1,}(y) RETURN p LIMIT 100");
declines_with(&ceiling, &graph, FrontierDecline::NfaShape);
declines_with(
"MATCH p = (x:Person)-[:KNOWS*1..]-(m)-[:STUDY_AT*1..]-(y) \
RETURN p LIMIT 100",
&graph,
FrontierDecline::NfaShape,
);
declines_with(
"MATCH p = ((x:Person)-[:KNOWS*1..]-(y) WHERE x.ID > 1) RETURN p LIMIT 100",
&graph,
FrontierDecline::NfaShape,
);
declines_with(
"MATCH p = (x)((a:Person)-[:KNOWS]-(b)){1,}(y) RETURN p LIMIT 100",
&graph,
FrontierDecline::NfaShape,
);
declines_with(
"MATCH p = (x:Person)-[:KNOWS]-(m:Person)-[:KNOWS*1..]-(y) RETURN p LIMIT 100",
&graph,
FrontierDecline::NfaShape,
);
declines_with(
"MATCH p = ANY SHORTEST (x:Person)-[:KNOWS*1..]-(y) RETURN p LIMIT 100",
&graph,
FrontierDecline::NfaShape,
);
}
#[test]
fn declines_full_enumeration_consumers() {
let graph = corpus_graph();
let base = "MATCH p = (x:Person)-[:KNOWS*1..]-(y:Person)";
declines_with(
&format!("{base} RETURN p"),
&graph,
FrontierDecline::FullEnumeration,
);
declines_with(
&format!("{base} RETURN y"),
&graph,
FrontierDecline::FullEnumeration,
);
declines_with(
&format!("{base} WITH x, y RETURN x"),
&graph,
FrontierDecline::FullEnumeration,
);
declines_with(
&format!("{base} WITH x WHERE x.k > 1 RETURN x"),
&graph,
FrontierDecline::FullEnumeration,
);
declines_with(
&format!("{base} RETURN y GROUP BY y"),
&graph,
FrontierDecline::FullEnumeration,
);
declines_with(
&format!("{base} CREATE (z:Z)"),
&graph,
FrontierDecline::FullEnumeration,
);
declines_with(
&format!("{base} RETURN p LIMIT 0"),
&graph,
FrontierDecline::FullEnumeration,
);
}
#[test]
fn declines_ordered_caps_and_path_counts() {
let graph = corpus_graph();
let base = "MATCH p = (x:Person)-[:KNOWS*1..]-(y:Person)";
declines_with(
&format!("{base} RETURN p ORDER BY length(p) LIMIT 10"),
&graph,
FrontierDecline::OrderedCap,
);
declines_with(
&format!("{base} RETURN p ORDER BY y.k LIMIT 10"),
&graph,
FrontierDecline::OrderedCap,
);
for suffix in [
"RETURN count(p)",
"RETURN count(*)",
"RETURN count(y)",
"RETURN collect(y)",
"RETURN sum(y.k)",
"RETURN avg(y.k)",
] {
declines_with(
&format!("{base} {suffix}"),
&graph,
FrontierDecline::PathCount,
);
}
declines_with(
&format!("{base} RETURN DISTINCT p"),
&graph,
FrontierDecline::PathConsumer,
);
declines_with(
&format!("{base} RETURN min(length(p))"),
&graph,
FrontierDecline::PathConsumer,
);
declines_with(
"MATCH p = (x:Person)((a)-[rs:KNOWS]-(b)){1,}(y:Person) \
RETURN DISTINCT rs",
&graph,
FrontierDecline::PathConsumer,
);
}
#[test]
fn declines_the_context_gates() {
let graph = corpus_graph();
declines_with(
"MATCH p = (x:Person)-[:KNOWS*1..]-(y:Person), (z:Organisation) \
RETURN p LIMIT 10",
&graph,
FrontierDecline::MultiplePatterns,
);
declines_with(
"MATCH (x:Person)-[:KNOWS]-(m)-[:KNOWS*1..]-(y:Person) \
WHERE size(m.k) > 1 RETURN DISTINCT y",
&graph,
FrontierDecline::Conjunct,
);
declines_with(
"MATCH (x:Person)-[:KNOWS*1..]-(y:Person) WHERE y.k > 1 AND y.j < 2 \
RETURN DISTINCT y",
&graph,
FrontierDecline::Conjunct,
);
declines_with(
"MATCH (x:Person)-[:KNOWS*1..]-(y:Person) WHERE size(x.k) > 1 \
RETURN DISTINCT y",
&graph,
FrontierDecline::Conjunct,
);
declines_with(
"MATCH p = (x:Person)-[:KNOWS*1..]-(x) RETURN p LIMIT 10",
&graph,
FrontierDecline::PinnedBinding,
);
declines_with(
"MATCH p = (x:Person)-[r:KNOWS]-()-[r:STUDY_AT]-()-[:KNOWS*1..]-(y) \
RETURN p LIMIT 10",
&graph,
FrontierDecline::PinnedBinding,
);
}
#[test]
fn declines_an_inner_node_rel_name_collision() {
let graph = corpus_graph();
let rel_a = RelPattern {
variable: Some("a".into()),
..one_hop_rel(&["KNOWS"])
};
let pattern = path_pattern(vec![
PatternElement::Node(named_node("x")),
PatternElement::Quantified(quantified(
vec![
PatternElement::Node(named_node("a")),
PatternElement::Relationship(rel_a),
PatternElement::Node(named_node("b")),
],
1,
None,
)),
PatternElement::Node(named_node("y")),
]);
let params: BTreeMap<String, Value> = BTreeMap::new();
let stats = GraphStats::new(&graph, ¶ms);
let patterns = [pattern];
let decision = compile_frontier(
&graph,
&patterns,
MatchMode::DifferentRelationships,
&Row::new(),
&[],
Consumer::Exists,
&stats,
);
assert_eq!(decision.err(), Some(FrontierDecline::PinnedBinding));
}
#[test]
fn the_row_gate_binds_only_the_seed() {
let graph = corpus_graph();
let cypher = "MATCH (x:Person)-[:KNOWS*1..]-(y:Person) RETURN DISTINCT y";
let carrying = Row::from_iter(vec![("outer".to_string(), Value::Null)]);
decide(cypher, &graph, false, &carrying, |decision| {
assert_eq!(decision.err(), Some(FrontierDecline::CarryingRow));
});
let pinned = Row::from_iter(vec![("rs".to_string(), Value::Null)]);
decide(
"MATCH p = (x:Person)((a)-[rs:KNOWS]-(b)){1,}(y:Person) RETURN DISTINCT y",
&graph,
false,
&pinned,
|decision| {
assert_eq!(decision.err(), Some(FrontierDecline::CarryingRow));
},
);
let seeded = Row::from_iter(vec![("x".to_string(), Value::Null)]);
decide(cypher, &graph, false, &seeded, |decision| {
assert!(
decision.is_ok(),
"the seed binding rides: {:?}",
decision.err()
);
});
}
#[test]
fn declines_the_allreduce_inlined_group() {
let b = Builder::new();
let a = b.node("a", &["Geo"]);
let c = b.node("c", &["Geo"]);
let d = b.node("d", &["Geo"]);
b.rel(a, c, "KNOWS", &[("k", Value::Float(1.0))]);
b.rel(c, d, "KNOWS", &[("k", Value::Float(2.0))]);
let graph = b.done();
declines_with(
"MATCH p = (a:Geo)((n)-[rels:KNOWS]-(m)){1,3}(b:Geo) \
WHERE allReduce(acc = 0.0, r IN rels | acc + r.k, acc < 10) \
RETURN p LIMIT 10",
&graph,
FrontierDecline::AllReduceGroup,
);
assert!(engages(
"MATCH p = (a:Geo)((n)-[rels:KNOWS]-(m)){1,}(b:Geo) RETURN p LIMIT 10",
&graph,
));
}