use std::collections::BTreeMap;
use relate_core::Value;
use super::test_support::{build, exec_cypher, profiled, profiled_params};
use crate::graph::Graph;
#[test]
fn expand_into_directed_forward_and_backward_parity() {
let built = build(&[&["A"], &["B"]], &[(0, 1, "KNOWS")]);
let g = &built.graph;
let res = exec_cypher(
g,
"MATCH (a:A), (b:B) WHERE a.id = 0 AND b.id = 1 MATCH (a)-[r:KNOWS]->(b) RETURN count(r) AS n",
);
assert_eq!(res.rows, vec![vec![Value::Int(1)]]);
let res = exec_cypher(
g,
"MATCH (a:A), (b:B) WHERE a.id = 0 AND b.id = 1 MATCH (b)<-[r:KNOWS]-(a) RETURN count(r) AS n",
);
assert_eq!(res.rows, vec![vec![Value::Int(1)]]);
let res = exec_cypher(
g,
"MATCH (a:A), (b:B) WHERE a.id = 0 AND b.id = 1 MATCH (a)<-[r:KNOWS]-(b) RETURN count(r) AS n",
);
assert_eq!(res.rows, vec![vec![Value::Int(0)]]);
}
#[test]
fn expand_into_undirected_parity() {
let built = build(&[&["A"], &["B"]], &[(0, 1, "KNOWS")]);
let g = &built.graph;
let res = exec_cypher(
g,
"MATCH (a:A), (b:B) WHERE a.id = 0 AND b.id = 1 MATCH (a)-[r:KNOWS]-(b) RETURN count(r) AS n",
);
assert_eq!(res.rows, vec![vec![Value::Int(1)]]);
let res = exec_cypher(
g,
"MATCH (a:A), (b:B) WHERE a.id = 0 AND b.id = 1 MATCH (b)-[r:KNOWS]-(a) RETURN count(r) AS n",
);
assert_eq!(res.rows, vec![vec![Value::Int(1)]]);
}
#[test]
fn expand_into_self_loops_directed_and_undirected() {
let g = Graph::default();
g.begin_write().unwrap();
let a = g
.create_node(
vec!["Node".into()],
BTreeMap::from([("id".into(), Value::Int(1))]),
)
.unwrap();
g.create_rel(a, a, "LOOP".into(), BTreeMap::new()).unwrap();
g.commit_write();
let res = exec_cypher(
&g,
"MATCH (a:Node), (b:Node) WHERE a.id = 1 AND b.id = 1 MATCH (a)-[r:LOOP]->(b) RETURN count(r) AS n",
);
assert_eq!(
res.rows,
vec![vec![Value::Int(1)]],
"directed self loop exactly 1 row"
);
let res = exec_cypher(
&g,
"MATCH (a:Node), (b:Node) WHERE a.id = 1 AND b.id = 1 MATCH (a)-[r:LOOP]-(b) RETURN count(r) AS n",
);
assert_eq!(
res.rows,
vec![vec![Value::Int(1)]],
"undirected self loop exactly 1 row"
);
}
#[test]
fn expand_into_parallel_multi_edges() {
let g = Graph::default();
g.begin_write().unwrap();
let a = g
.create_node(
vec!["N".into()],
BTreeMap::from([("id".into(), Value::Int(1))]),
)
.unwrap();
let b = g
.create_node(
vec!["N".into()],
BTreeMap::from([("id".into(), Value::Int(2))]),
)
.unwrap();
g.create_rel(
a,
b,
"R".into(),
BTreeMap::from([("k".into(), Value::Int(10))]),
)
.unwrap();
g.create_rel(
a,
b,
"R".into(),
BTreeMap::from([("k".into(), Value::Int(20))]),
)
.unwrap();
g.commit_write();
let res = exec_cypher(
&g,
"MATCH (a:N {id: 1}), (b:N {id: 2}) MATCH (a)-[r:R]->(b) RETURN r.k AS k ORDER BY k",
);
assert_eq!(res.rows, vec![vec![Value::Int(10)], vec![Value::Int(20)]]);
let res = exec_cypher(
&g,
"MATCH (a:N {id: 1}), (b:N {id: 2}) MATCH (a)-[r:R]-(b) RETURN r.k AS k ORDER BY k",
);
assert_eq!(res.rows, vec![vec![Value::Int(10)], vec![Value::Int(20)]]);
}
#[test]
fn expand_into_prebound_relationship_variable() {
let built = build(&[&["N"], &["N"]], &[(0, 1, "R")]);
let g = &built.graph;
let res = exec_cypher(
g,
"MATCH (a:N {id: 0})-[r:R]->(b:N {id: 1}) MATCH (a)-[r]->(b) RETURN count(*) AS n",
);
assert_eq!(res.rows, vec![vec![Value::Int(1)]]);
let res = exec_cypher(
g,
"MATCH (a:N {id: 0})-[r:R]->(b:N {id: 1}) MATCH (a)<-[r]-(b) RETURN count(*) AS n",
);
assert_eq!(res.rows, vec![vec![Value::Int(0)]]);
}
#[test]
fn expand_into_property_and_inline_where_filters() {
let g = Graph::default();
g.begin_write().unwrap();
let a = g
.create_node(
vec!["N".into()],
BTreeMap::from([("id".into(), Value::Int(1))]),
)
.unwrap();
let b = g
.create_node(
vec!["N".into()],
BTreeMap::from([("id".into(), Value::Int(2))]),
)
.unwrap();
g.create_rel(
a,
b,
"R".into(),
BTreeMap::from([
("weight".into(), Value::Int(15)),
("active".into(), Value::Bool(true)),
]),
)
.unwrap();
g.commit_write();
let res = exec_cypher(
&g,
"MATCH (a:N {id: 1}), (b:N {id: 2}) MATCH (a)-[r:R {active: true}]->(b) RETURN count(*) AS n",
);
assert_eq!(res.rows, vec![vec![Value::Int(1)]]);
let res = exec_cypher(
&g,
"MATCH (a:N {id: 1}), (b:N {id: 2}) MATCH (a)-[r:R {active: false}]->(b) RETURN count(*) AS n",
);
assert_eq!(res.rows, vec![vec![Value::Int(0)]]);
let res = exec_cypher(
&g,
"MATCH (a:N {id: 1}), (b:N {id: 2}) MATCH (a)-[r:R]->(b) WHERE r.weight > 10 RETURN count(*) AS n",
);
assert_eq!(res.rows, vec![vec![Value::Int(1)]]);
let res = exec_cypher(
&g,
"MATCH (a:N {id: 1}), (b:N {id: 2}) MATCH (a)-[r:R]->(b) WHERE r.weight > 20 RETURN count(*) AS n",
);
assert_eq!(res.rows, vec![vec![Value::Int(0)]]);
}
#[test]
fn expand_into_optional_match() {
let built = build(&[&["A"], &["B"]], &[(0, 1, "E")]);
let g = &built.graph;
let res = exec_cypher(
g,
"MATCH (a:A), (b:B) WHERE a.id = 0 AND b.id = 1 OPTIONAL MATCH (a)-[r:OTHER]->(b) RETURN a.id, b.id, r",
);
assert_eq!(
res.rows,
vec![vec![Value::Int(0), Value::Int(1), Value::Null]]
);
}
#[test]
fn expand_into_with_piped_join() {
let built = build(&[&["User"], &["User"]], &[(0, 1, "KNOWS")]);
let g = &built.graph;
let res = exec_cypher(
g,
"MATCH (u1:User {id: 0}) MATCH (u2:User {id: 1}) WITH u1, u2 MATCH (u1)-[r:KNOWS]->(u2) RETURN count(r) AS n",
);
assert_eq!(res.rows, vec![vec![Value::Int(1)]]);
}
#[test]
fn expand_into_triangles() {
let built = build(
&[&["N"], &["N"], &["N"]],
&[(0, 1, "E"), (1, 2, "E"), (0, 2, "E")],
);
let g = &built.graph;
let res = exec_cypher(
g,
"MATCH (a:N {id: 0})-[:E]->(b:N), (b)-[:E]->(c:N) MATCH (a)-[:E]->(c) RETURN b.id, c.id",
);
assert_eq!(res.rows, vec![vec![Value::Int(1), Value::Int(2)]]);
}
#[test]
fn expand_into_common_friends() {
let built = build(
&[&["User"], &["User"], &["User"], &["User"]],
&[(0, 2, "KNOWS"), (1, 2, "KNOWS"), (0, 3, "KNOWS")],
);
let g = &built.graph;
let res = exec_cypher(
g,
"MATCH (u1:User {id: 0})-[:KNOWS]->(f:User) MATCH (u2:User {id: 1})-[:KNOWS]->(f) RETURN f.id",
);
assert_eq!(res.rows, vec![vec![Value::Int(2)]]);
}
fn skewed_hub_leaf_graph() -> Graph {
let g = Graph::default();
g.begin_write().unwrap();
let hub = g
.create_node(
vec!["Hub".into()],
BTreeMap::from([("id".into(), Value::Int(0))]),
)
.unwrap();
let leaf = g
.create_node(
vec!["Leaf".into()],
BTreeMap::from([("id".into(), Value::Int(1))]),
)
.unwrap();
let leaf2 = g
.create_node(
vec!["Other".into()],
BTreeMap::from([("id".into(), Value::Int(2))]),
)
.unwrap();
g.create_rel(hub, leaf, "CONNECTED".into(), BTreeMap::new())
.unwrap();
g.create_rel(leaf, leaf2, "OTHER".into(), BTreeMap::new())
.unwrap();
for i in 3..202 {
let dummy = g
.create_node(
vec!["Dummy".into()],
BTreeMap::from([("id".into(), Value::Int(i))]),
)
.unwrap();
g.create_rel(hub, dummy, "CONNECTED".into(), BTreeMap::new())
.unwrap();
}
g.commit_write();
g
}
fn assert_expand_into_plan(
records: &[crate::metrics::ClauseMetricsRecord],
expected_reversed: bool,
) {
let rec = records
.iter()
.find(|r| r.expand_into.probes > 0)
.expect("clause with expand_into probes");
assert!(rec.expand_into.probes > 0, "ExpandIntoProbes > 0");
if expected_reversed {
assert!(rec.expand_into.reversed > 0, "ExpandIntoReversed > 0");
} else {
assert_eq!(rec.expand_into.reversed, 0, "ExpandIntoReversed == 0");
}
assert!(
rec.reads.total() <= 5,
"DbHits must be O(leaf degree), got {}",
rec.reads.total()
);
}
#[test]
fn profile_expand_into_hub_leaf_smaller_degree_probe_and_db_hits() {
let g = skewed_hub_leaf_graph();
let cases = [
(
"MATCH (h:Hub), (l:Leaf) WHERE h.id = 0 AND l.id = 1 MATCH (h)-[:CONNECTED]->(l) RETURN h.id, l.id",
true,
),
(
"MATCH (h:Hub), (l:Leaf) WHERE h.id = 0 AND l.id = 1 MATCH (h)-[:CONNECTED]->(l) WHERE h.id = 0 RETURN h.id, l.id",
true,
),
(
"MATCH (h:Hub), (l:Leaf) WHERE h.id = 0 AND l.id = 1 MATCH (h)-[:CONNECTED]->(l) WHERE NOT (h.id = 99 OR l.id = 99) RETURN h.id, l.id",
true,
),
(
"MATCH (h:Hub), (l:Leaf) WHERE h.id = 0 AND l.id = 1 MATCH (l)<-[:CONNECTED]-(h) RETURN h.id, l.id",
false,
),
];
for (query, expected_reversed) in cases {
let records = profiled(&g, query);
assert_expand_into_plan(&records, expected_reversed);
}
let params = BTreeMap::from([
("hid".to_string(), Value::Int(0)),
("lid".to_string(), Value::Int(1)),
]);
let param_records = profiled_params(
&g,
"MATCH (h:Hub), (l:Leaf) WHERE h.id = $hid AND l.id = $lid MATCH (h)-[:CONNECTED]->(l) RETURN h.id, l.id",
¶ms,
);
assert_expand_into_plan(¶m_records, true);
}