use super::agg_fold::take_path_fold_bindings;
use super::kernels::with_kernels_disabled;
use super::test_support::{
assert_expression_cap, build, error_of, exec_autocommit, exec_cypher, person_named, same_rows,
try_exec_cypher,
};
use super::*;
use crate::graph::Graph;
use relate_core::NodeId;
#[test]
fn path_fold_counts_distinct_comments_in_a_place() {
let g = Graph::default();
g.begin_write().unwrap();
let berlin = g
.create_node(
vec!["Place".into()],
BTreeMap::from([("name".into(), Value::String("Berlin".into()))]),
)
.unwrap();
let paris = g
.create_node(
vec!["Place".into()],
BTreeMap::from([("name".into(), Value::String("Paris".into()))]),
)
.unwrap();
let local = person_named(&g, 10, "Ada", "Local");
let away = person_named(&g, 11, "Bea", "Away");
g.create_rel(local, berlin, "personIsLocatedIn".into(), BTreeMap::new())
.unwrap();
g.create_rel(away, paris, "personIsLocatedIn".into(), BTreeMap::new())
.unwrap();
for (author, id, content) in [
(local, 1, Some("one")),
(local, 2, Some("two")),
(local, 3, None),
(away, 4, Some("away")),
] {
let mut props = BTreeMap::from([("ID".into(), Value::Int(id))]);
if let Some(content) = content {
props.insert("content".into(), Value::String(content.into()));
}
let comment = g.create_node(vec!["Comment".into()], props).unwrap();
g.create_rel(comment, author, "commentHasCreator".into(), BTreeMap::new())
.unwrap();
}
g.commit_write();
let _ = take_path_fold_bindings();
let result = exec_cypher(
&g,
"MATCH (c:Comment)-[:commentHasCreator]->(p:Person)-[:personIsLocatedIn]->(l:Place) \
WHERE c.content IS NOT NULL AND l.name = \"Berlin\" \
RETURN COUNT(DISTINCT c.ID) AS num_comments",
);
assert_eq!(result.rows, vec![vec![Value::Int(2)]]);
assert_eq!(take_path_fold_bindings(), 2);
}
#[test]
fn path_fold_counts_distinct_likers() {
let g = Graph::default();
g.begin_write().unwrap();
let rafael = person_named(&g, 1, "Rafael", "Alonso");
let other = person_named(&g, 2, "Other", "Author");
let ada = person_named(&g, 3, "Ada", "Liker");
let bea = person_named(&g, 4, "Bea", "Liker");
let cam = person_named(&g, 5, "Cam", "Liker");
let comment = |id: i64, author: NodeId| {
let comment = g
.create_node(
vec!["Comment".into()],
BTreeMap::from([("ID".into(), Value::Int(id))]),
)
.unwrap();
g.create_rel(comment, author, "commentHasCreator".into(), BTreeMap::new())
.unwrap();
comment
};
let first = comment(10, rafael);
let second = comment(11, rafael);
let foreign = comment(12, other);
g.create_rel(ada, first, "likeComment".into(), BTreeMap::new())
.unwrap();
g.create_rel(ada, second, "likeComment".into(), BTreeMap::new())
.unwrap();
g.create_rel(bea, first, "likeComment".into(), BTreeMap::new())
.unwrap();
g.create_rel(cam, foreign, "likeComment".into(), BTreeMap::new())
.unwrap();
g.commit_write();
let _ = take_path_fold_bindings();
let result = exec_cypher(
&g,
"MATCH (p:Person)<-[:commentHasCreator]-(c:Comment)<-[:likeComment]-(p2:Person) \
WHERE p.firstName = \"Rafael\" AND p.lastName = \"Alonso\" \
RETURN COUNT(DISTINCT p2.ID) AS num_persons",
);
assert_eq!(result.rows, vec![vec![Value::Int(2)]]);
assert!(take_path_fold_bindings() >= 3);
}
#[test]
fn path_fold_groups_interests_of_students() {
let g = Graph::default();
g.begin_write().unwrap();
let school = g
.create_node(
vec!["Organisation".into()],
BTreeMap::from([("name".into(), Value::String("IISc".into()))]),
)
.unwrap();
let other = g
.create_node(
vec!["Organisation".into()],
BTreeMap::from([("name".into(), Value::String("Other".into()))]),
)
.unwrap();
let rust = g
.create_node(
vec!["Tag".into()],
BTreeMap::from([("name".into(), Value::String("Rust".into()))]),
)
.unwrap();
let go = g
.create_node(
vec!["Tag".into()],
BTreeMap::from([("name".into(), Value::String("Go".into()))]),
)
.unwrap();
let mut next_id = 1;
let mut student = |org: NodeId, tags: &[NodeId]| {
let person = person_named(&g, next_id, "Stu", "Dent");
next_id += 1;
g.create_rel(person, org, "studyAt".into(), BTreeMap::new())
.unwrap();
for tag in tags {
g.create_rel(person, *tag, "hasInterest".into(), BTreeMap::new())
.unwrap();
}
};
student(school, &[rust, go]);
student(school, &[rust]);
student(other, &[rust]);
g.commit_write();
let _ = take_path_fold_bindings();
let result = exec_cypher(
&g,
"MATCH (p:Person)-[:studyAt]->(o:Organisation), (p)-[:hasInterest]->(t:Tag) \
WHERE o.name = \"IISc\" \
RETURN t.name AS name, COUNT(*) AS tag_count \
ORDER BY tag_count DESC LIMIT 1",
);
assert_eq!(
result.rows,
vec![vec![Value::String("Rust".into()), Value::Int(2)]]
);
assert!(take_path_fold_bindings() >= 2);
}
#[test]
fn path_fold_requires_both_comma_patterns() {
let g = Graph::default();
g.begin_write().unwrap();
let school = g
.create_node(
vec!["Organisation".into()],
BTreeMap::from([("name".into(), Value::String("Oxford".into()))]),
)
.unwrap();
let tag = g
.create_node(
vec!["Tag".into()],
BTreeMap::from([("name".into(), Value::String("Shakespeare".into()))]),
)
.unwrap();
let enroll = |id: i64, links: &[(&str, NodeId)]| {
let person = g
.create_node(
vec!["Person".into()],
BTreeMap::from([("ID".into(), Value::Int(id))]),
)
.unwrap();
for (kind, target) in links {
g.create_rel(person, *target, (*kind).into(), BTreeMap::new())
.unwrap();
}
};
enroll(1, &[("studyAt", school), ("hasInterest", tag)]);
enroll(2, &[("studyAt", school), ("hasInterest", tag)]);
enroll(3, &[("studyAt", school)]);
enroll(4, &[("hasInterest", tag)]);
g.commit_write();
let _ = take_path_fold_bindings();
let result = exec_cypher(
&g,
"MATCH (p:Person)-[:studyAt]->(o:Organisation), (p)-[:hasInterest]->(t:Tag) \
WHERE o.name = \"Oxford\" AND t.name = \"Shakespeare\" \
RETURN COUNT(DISTINCT p.ID) AS num_p",
);
assert_eq!(result.rows, vec![vec![Value::Int(2)]]);
assert_eq!(take_path_fold_bindings(), 2);
}
#[test]
fn path_fold_counts_comments_by_place() {
let g = Graph::default();
g.begin_write().unwrap();
let tag = g
.create_node(
vec!["Tag".into()],
BTreeMap::from([("name".into(), Value::String("Copernicus".into()))]),
)
.unwrap();
let other = g
.create_node(
vec!["Tag".into()],
BTreeMap::from([("name".into(), Value::String("Other".into()))]),
)
.unwrap();
let india = g
.create_node(
vec!["Place".into()],
BTreeMap::from([("name".into(), Value::String("India".into()))]),
)
.unwrap();
let spain = g
.create_node(
vec!["Place".into()],
BTreeMap::from([("name".into(), Value::String("Spain".into()))]),
)
.unwrap();
let comment = |id: i64, tag: NodeId, place: NodeId| {
let comment = g
.create_node(
vec!["Comment".into()],
BTreeMap::from([("ID".into(), Value::Int(id))]),
)
.unwrap();
g.create_rel(comment, tag, "commentHasTag".into(), BTreeMap::new())
.unwrap();
g.create_rel(comment, place, "commentIsLocatedIn".into(), BTreeMap::new())
.unwrap();
};
comment(1, tag, india);
comment(2, tag, india);
comment(3, tag, spain);
comment(4, other, india);
g.commit_write();
let _ = take_path_fold_bindings();
let result = exec_cypher(
&g,
"MATCH (c:Comment)-[:commentHasTag]->(t:Tag), (c)-[:commentIsLocatedIn]->(l:Place) \
WHERE t.name CONTAINS \"Copernicus\" \
RETURN l.name AS name, COUNT(c.ID) AS comment_count \
ORDER BY comment_count DESC",
);
assert_eq!(
result.rows,
vec![
vec![Value::String("India".into()), Value::Int(2)],
vec![Value::String("Spain".into()), Value::Int(1)],
]
);
assert_eq!(take_path_fold_bindings(), 3);
}
#[test]
fn variable_length_and_relationship_sum_stay_materialized() {
let g = Graph::default();
g.begin_write().unwrap();
let a = g
.create_node(
vec!["A".into()],
BTreeMap::from([("id".into(), Value::Int(1))]),
)
.unwrap();
let mid = g.create_node(vec!["M".into()], BTreeMap::new()).unwrap();
let b = g
.create_node(
vec!["B".into()],
BTreeMap::from([("id".into(), Value::Int(2)), ("n".into(), Value::Int(10))]),
)
.unwrap();
let c = g
.create_node(
vec!["C".into()],
BTreeMap::from([("id".into(), Value::Int(3))]),
)
.unwrap();
g.create_rel(a, b, "R".into(), BTreeMap::new()).unwrap();
g.create_rel(a, mid, "R".into(), BTreeMap::new()).unwrap();
g.create_rel(mid, b, "R".into(), BTreeMap::new()).unwrap();
g.create_rel(
a,
b,
"E".into(),
BTreeMap::from([("w".into(), Value::Int(10))]),
)
.unwrap();
g.create_rel(b, c, "E".into(), BTreeMap::new()).unwrap();
g.commit_write();
let _ = take_path_fold_bindings();
let varied = exec_cypher(&g, "MATCH (a:A)-[:R*1..2]->(b:B) RETURN count(*) AS n");
assert_eq!(varied.rows, vec![vec![Value::Int(2)]]);
assert_eq!(take_path_fold_bindings(), 0);
let summed = exec_cypher(
&g,
"MATCH (a:A)-[e:E]->(b:B)-[:E]->(c:C) RETURN sum(e.w) AS s",
);
assert_eq!(summed.rows, vec![vec![Value::Int(10)]]);
assert_eq!(take_path_fold_bindings(), 0);
}
#[test]
fn repeated_relationship_variable_stays_on_the_matcher() {
let g = Graph::default();
g.begin_write().unwrap();
let a = g.create_node(vec!["A".into()], BTreeMap::new()).unwrap();
let b = g.create_node(vec!["B".into()], BTreeMap::new()).unwrap();
let c = g.create_node(vec!["C".into()], BTreeMap::new()).unwrap();
g.create_rel(a, b, "T".into(), BTreeMap::new()).unwrap();
g.create_rel(b, c, "T".into(), BTreeMap::new()).unwrap();
g.commit_write();
let _ = take_path_fold_bindings();
let implicit = exec_cypher(&g, "MATCH ()-[r]-()-[r]-() RETURN count(*) AS count");
assert_eq!(implicit.rows, vec![vec![Value::Int(0)]]);
assert_eq!(take_path_fold_bindings(), 0);
let repeatable = exec_cypher(
&g,
"MATCH REPEATABLE ELEMENTS ()-[r]-()-[r]-() RETURN count(*) AS count",
);
assert_eq!(repeatable.rows, vec![vec![Value::Int(4)]]);
assert_eq!(take_path_fold_bindings(), 0);
let directed = exec_cypher(
&g,
"MATCH (:A)-[e:T]->(:B)-[e]->() RETURN count(*) AS count",
);
assert_eq!(directed.rows, vec![vec![Value::Int(0)]]);
assert_eq!(take_path_fold_bindings(), 0);
}
#[test]
fn path_fold_declines_a_user_alias_in_the_internal_namespace() {
let _ = take_path_fold_bindings();
let built = build(&[&["A"], &["B"]], &[(0, 1, "R")]);
let r = exec_cypher(
&built.graph,
"MATCH (a:A)-[:R]->(b:B) RETURN count(b) AS __agg_base",
);
assert_eq!(r.columns, vec!["__agg_base".to_string()]);
assert_eq!(r.rows, vec![vec![Value::Int(1)]]);
assert_eq!(take_path_fold_bindings(), 0);
}
#[test]
fn path_fold_declines_the_zero_argument_backtick_count() {
let built = build(&[&["A"], &["B"], &["C"]], &[(0, 1, "R"), (1, 2, "R")]);
let _ = take_path_fold_bindings();
let one_hop = error_of(try_exec_cypher(
&built.graph,
"MATCH (:A)-[:R]->(:B) RETURN `count`()",
));
let two_hop = error_of(try_exec_cypher(
&built.graph,
"MATCH (:A)-[:R]->()-[:R]->(:C) RETURN `count`()",
));
assert_eq!(one_hop, two_hop);
assert!(two_hop.contains("InvalidNumberOfArguments"), "{two_hop}");
assert_eq!(take_path_fold_bindings(), 0);
}
#[test]
fn path_fold_agrees_with_the_general_path_across_a_detach_delete() {
let g = Graph::default();
g.begin_write().unwrap();
let a = g.create_node(vec!["N".into()], BTreeMap::new()).unwrap();
let b = g.create_node(vec!["M".into()], BTreeMap::new()).unwrap();
g.create_rel(a, b, "R".into(), BTreeMap::new()).unwrap();
g.commit_write();
let deleted = exec_autocommit(
&g,
"MATCH (m) DETACH DELETE m WITH m MATCH (n)-[:R]->(m) \
RETURN count(m), count(DISTINCT m)",
)
.unwrap();
assert_eq!(deleted.rows, vec![vec![Value::Int(0), Value::Int(0)]]);
let g = Graph::default();
g.begin_write().unwrap();
let n1 = g.create_node(vec!["N".into()], BTreeMap::new()).unwrap();
let m1 = g.create_node(vec!["M".into()], BTreeMap::new()).unwrap();
let n2 = g.create_node(vec!["Dead".into()], BTreeMap::new()).unwrap();
let m2 = g.create_node(vec!["Dead".into()], BTreeMap::new()).unwrap();
g.create_rel(n1, m1, "R".into(), BTreeMap::new()).unwrap();
g.create_rel(n2, m2, "R".into(), BTreeMap::new()).unwrap();
g.commit_write();
let deleted = exec_autocommit(
&g,
"MATCH (m:Dead) DETACH DELETE m WITH m MATCH (n)-[:R]->(m) \
RETURN count(m), count(DISTINCT m)",
)
.unwrap();
assert_eq!(deleted.rows, vec![vec![Value::Int(0), Value::Int(0)]]);
let _ = take_path_fold_bindings();
let folded = exec_cypher(&g, "MATCH (n)-[:R]->(m) RETURN count(m), count(DISTINCT m)");
assert_eq!(folded.rows, vec![vec![Value::Int(1), Value::Int(1)]]);
assert_eq!(take_path_fold_bindings(), 1);
let general = exec_cypher(
&g,
"MATCH (n)-[:R]->(m) WITH n, m RETURN count(m), count(DISTINCT m)",
);
assert_eq!(general.rows, folded.rows);
}
fn repeated_variable_graph() -> Graph {
build(&[&["X"], &["X"], &["X"]], &[(0, 1, "R"), (2, 2, "R")]).graph
}
#[test]
fn path_fold_counts_a_repeated_node_variable_as_a_self_loop() {
let g = repeated_variable_graph();
let _ = take_path_fold_bindings();
let star = exec_cypher(&g, "MATCH (x:X)-[:R]->(x:X) RETURN count(*) AS n");
assert_eq!(star.rows, vec![vec![Value::Int(1)]]);
assert!(
take_path_fold_bindings() > 0,
"the star count takes the fold"
);
let whole = exec_cypher(&g, "MATCH (x:X)-[:R]->(x:X) RETURN count(x) AS n");
assert_eq!(whole.rows, vec![vec![Value::Int(1)]]);
assert!(
take_path_fold_bindings() > 0,
"the whole count takes the fold"
);
let summed = exec_cypher(&g, "MATCH (x:X)-[:R]->(x:X) RETURN sum(x.id) AS s");
assert_eq!(summed.rows, vec![vec![Value::Int(2)]]);
let value = exec_cypher(&g, "MATCH (x:X)-[:R]->(x:X) RETURN x.id AS id");
assert_eq!(value.rows, vec![vec![Value::Int(2)]]);
let collected = exec_cypher(&g, "MATCH (x:X)-[:R]->(x:X) RETURN collect(x.id) AS ids");
assert_eq!(
collected.rows,
vec![vec![Value::List(vec![Value::Int(2)].into())]]
);
}
#[test]
fn path_fold_repeated_node_variable_stays_self_loop_when_prebound() {
let g = repeated_variable_graph();
let _ = take_path_fold_bindings();
let pair = exec_cypher(
&g,
"MATCH (x:X {id: 0}) MATCH (x)-[:R]->(x) RETURN count(*) AS n",
);
assert_eq!(pair.rows, vec![vec![Value::Int(0)]]);
let _ = take_path_fold_bindings();
let self_loop = exec_cypher(
&g,
"MATCH (x:X {id: 2}) MATCH (x)-[:R]->(x) RETURN count(*) AS n",
);
assert_eq!(self_loop.rows, vec![vec![Value::Int(1)]]);
assert!(
take_path_fold_bindings() > 0,
"the prebound count takes the fold"
);
}
#[test]
fn exists_probe_repeated_node_variable_needs_a_self_loop() {
let g = repeated_variable_graph();
let pair = exec_cypher(
&g,
"MATCH (x:X {id: 0}) RETURN EXISTS { (x:X)-[:R]->(x:X) } AS e",
);
assert_eq!(pair.rows, vec![vec![Value::Bool(false)]]);
let self_loop = exec_cypher(
&g,
"MATCH (x:X {id: 2}) RETURN EXISTS { (x:X)-[:R]->(x:X) } AS e",
);
assert_eq!(self_loop.rows, vec![vec![Value::Bool(true)]]);
}
fn two_hop_score_graph() -> Graph {
let g = Graph::default();
g.begin_write().unwrap();
let user = |name: &str| {
g.create_node(
vec!["User".into()],
BTreeMap::from([("name".into(), Value::String(name.into()))]),
)
.unwrap()
};
let u1 = user("u1");
let u2 = user("u2");
let x1 = g.create_node(vec!["Mid".into()], BTreeMap::new()).unwrap();
let x2 = g.create_node(vec!["Mid".into()], BTreeMap::new()).unwrap();
let item = |score: Option<i64>| {
let props = match score {
Some(score) => BTreeMap::from([("score".into(), Value::Int(score))]),
None => BTreeMap::new(),
};
g.create_node(vec!["Item".into()], props).unwrap()
};
let m1 = item(Some(10));
let m2 = item(Some(20));
let m3 = item(Some(30));
let m4 = item(None);
let rel = |a: NodeId, b: NodeId| g.create_rel(a, b, "R".into(), BTreeMap::new()).unwrap();
rel(u1, x1);
rel(u2, x2);
rel(x1, m1);
rel(x1, m2);
rel(x2, m2);
rel(x2, m3);
rel(x2, m4);
g.commit_write();
g
}
#[test]
fn path_fold_streams_count_over_a_bare_two_hop_endpoint() {
let g = two_hop_score_graph();
let _ = take_path_fold_bindings();
let result =
with_kernels_disabled(|| exec_cypher(&g, "MATCH (n)-[:R]->()-[:R]->(m) RETURN count(m)"));
assert_eq!(result.rows, vec![vec![Value::Int(5)]]);
assert_eq!(take_path_fold_bindings(), 5);
}
#[test]
fn path_fold_streams_count_distinct_over_a_bare_two_hop_endpoint() {
let g = two_hop_score_graph();
let _ = take_path_fold_bindings();
let result = exec_cypher(&g, "MATCH (n)-[:R]->()-[:R]->(m) RETURN count(DISTINCT m)");
assert_eq!(result.rows, vec![vec![Value::Int(4)]]);
assert_eq!(take_path_fold_bindings(), 5);
}
#[test]
fn path_fold_streams_a_grouped_bare_endpoint_count() {
let g = two_hop_score_graph();
let _ = take_path_fold_bindings();
let result = exec_cypher(
&g,
"MATCH (n:User)-[:R]->()-[:R]->(m:Item) \
RETURN n.name AS name, count(m) AS c, count(DISTINCT m) AS dc \
ORDER BY name",
);
assert_eq!(
result.rows,
vec![
vec![Value::String("u1".into()), Value::Int(2), Value::Int(2)],
vec![Value::String("u2".into()), Value::Int(3), Value::Int(3)],
]
);
assert_eq!(take_path_fold_bindings(), 5);
}
#[test]
fn two_hop_min_max_avg_sum_answer_over_the_shape() {
let g = two_hop_score_graph();
let keyless = exec_cypher(
&g,
"MATCH (n:User)-[:R]->()-[:R]->(m:Item) \
RETURN min(m.score), max(m.score), avg(m.score), sum(m.score)",
);
assert_eq!(
keyless.rows,
vec![vec![
Value::Int(10),
Value::Int(30),
Value::Float(20.0),
Value::Int(80),
]]
);
let keyless_distinct = exec_cypher(
&g,
"MATCH (n:User)-[:R]->()-[:R]->(m:Item) \
RETURN min(DISTINCT m.score), max(DISTINCT m.score), \
avg(DISTINCT m.score), sum(DISTINCT m.score)",
);
assert_eq!(
keyless_distinct.rows,
vec![vec![
Value::Int(10),
Value::Int(30),
Value::Float(20.0),
Value::Int(60),
]]
);
let grouped = exec_cypher(
&g,
"MATCH (n:User)-[:R]->()-[:R]->(m:Item) \
RETURN n.name AS name, min(m.score), max(m.score), avg(m.score), sum(m.score) \
ORDER BY name",
);
assert_eq!(
grouped.rows,
vec![
vec![
Value::String("u1".into()),
Value::Int(10),
Value::Int(20),
Value::Float(15.0),
Value::Int(30),
],
vec![
Value::String("u2".into()),
Value::Int(20),
Value::Int(30),
Value::Float(25.0),
Value::Int(50),
],
]
);
let grouped_distinct = exec_cypher(
&g,
"MATCH (n:User)-[:R]->()-[:R]->(m:Item) \
RETURN n.name AS name, sum(DISTINCT m.score), count(DISTINCT m.score) \
ORDER BY name",
);
assert_eq!(
grouped_distinct.rows,
vec![
vec![Value::String("u1".into()), Value::Int(30), Value::Int(2)],
vec![Value::String("u2".into()), Value::Int(50), Value::Int(2)],
]
);
}
fn row_bound_rel_graph() -> Graph {
build(
&[&["A"], &["B"], &["E"], &["C"], &["D"]],
&[(0, 1, "R"), (1, 2, "R"), (0, 3, "R"), (0, 4, "R")],
)
.graph
}
#[test]
fn path_fold_declines_a_row_bound_relationship_variable() {
let g = row_bound_rel_graph();
let _ = take_path_fold_bindings();
let folded = exec_cypher(
&g,
"MATCH ()-[r:R]->(:B) WITH r MATCH (x:A)-[r:R]->(z) RETURN count(*) AS n",
);
assert_eq!(folded.rows, vec![vec![Value::Int(1)]]);
assert_eq!(
take_path_fold_bindings(),
0,
"a row-bound relationship variable must keep the path fold off the clause"
);
let general = exec_cypher(
&g,
"MATCH ()-[r:R]->(:B) WITH r MATCH (x:A)-[r:R]->(z) WITH z RETURN count(*) AS n",
);
assert_eq!(
general.rows, folded.rows,
"the spelling must not change the answer"
);
let null_bound = exec_cypher(
&g,
"MATCH (x:A) OPTIONAL MATCH (x)-[r:R]->(:Nope) \
MATCH (x)-[r:R]->(z) RETURN count(*) AS n",
);
assert_eq!(null_bound.rows, vec![vec![Value::Int(0)]]);
}
#[test]
fn path_fold_still_takes_free_and_node_bound_patterns() {
let g = row_bound_rel_graph();
let _ = take_path_fold_bindings();
let free = exec_cypher(&g, "MATCH (x:A)-[:R]->(y) RETURN count(*) AS n");
assert_eq!(free.rows, vec![vec![Value::Int(3)]]);
assert!(
take_path_fold_bindings() > 0,
"an unpinned pattern still folds"
);
let _ = take_path_fold_bindings();
let node_bound = exec_cypher(
&g,
"MATCH (m:B) WITH m MATCH (x)-[:R]->(m)-[:R]->(z) RETURN count(*) AS n",
);
assert_eq!(node_bound.rows, vec![vec![Value::Int(1)]]);
assert!(
take_path_fold_bindings() > 0,
"a row-bound node still folds"
);
}
fn followers_graph() -> Graph {
let g = Graph::default();
g.begin_write().unwrap();
let person = |name: Option<&str>| {
let mut props = BTreeMap::new();
if let Some(name) = name {
props.insert("name".into(), Value::String(name.into()));
}
g.create_node(vec!["P".into()], props).unwrap()
};
let follower = || g.create_node(vec!["F".into()], BTreeMap::new()).unwrap();
let (a, b, c) = (person(Some("a")), person(Some("x")), person(Some("x")));
let (n1, n2) = (person(None), person(None));
for (to, times) in [(a, 2), (b, 1), (c, 2), (n1, 1), (n2, 2)] {
for _ in 0..times {
let from = follower();
g.create_rel(from, to, "R".into(), BTreeMap::new()).unwrap();
}
}
g.commit_write();
g
}
fn assert_folds_like_general(g: &Graph, folded: &str, general: &str) -> QueryResult {
let _ = take_path_fold_bindings();
let fast = exec_cypher(g, folded);
assert_eq!(take_path_fold_bindings(), 8, "the path fold took {folded}");
let slow = exec_cypher(g, general);
assert!(
same_rows(&fast, &slow),
"{folded}: {:?} vs {:?}",
fast.rows,
slow.rows
);
fast
}
#[test]
fn the_node_counted_fold_answers_what_the_general_path_answers() {
let g = followers_graph();
for (folded, general) in [
(
"MATCH (f:F)-[:R]->(p:P) RETURN p.name AS n, count(f) AS c",
"MATCH (f:F)-[:R]->(p:P) WITH p.name AS n, count(f) AS c RETURN n, c",
),
(
"MATCH (f:F)-[:R]->(p:P) RETURN p.name AS n, count(*) AS s, count(f) AS w, count(p) AS e",
"MATCH (f:F)-[:R]->(p:P) WITH p.name AS n, count(*) AS s, count(f) AS w, count(p) AS e RETURN n, s, w, e",
),
(
"MATCH (f:F)-[:R]->(p:P) RETURN p.name AS n, p.name AS again, count(f) AS c",
"MATCH (f:F)-[:R]->(p:P) WITH p.name AS n, p.name AS again, count(f) AS c RETURN n, again, c",
),
] {
assert_folds_like_general(&g, folded, general);
}
let mut rows = exec_cypher(
&g,
"MATCH (f:F)-[:R]->(p:P) RETURN p.name AS n, count(f) AS c",
)
.rows;
rows.sort_by_key(|row| format!("{:?}", row[0]));
assert_eq!(
rows,
vec![
vec![Value::Null, Value::Int(3)],
vec![Value::String("a".into()), Value::Int(2)],
vec![Value::String("x".into()), Value::Int(3)],
]
);
}
#[test]
fn a_distinct_or_property_count_keeps_the_per_binding_table() {
let g = followers_graph();
for (folded, general) in [
(
"MATCH (f:F)-[:R]->(p:P) RETURN p.name AS n, count(DISTINCT f) AS c",
"MATCH (f:F)-[:R]->(p:P) WITH p.name AS n, count(DISTINCT f) AS c RETURN n, c",
),
(
"MATCH (f:F)-[:R]->(p:P) RETURN p.name AS n, count(p.name) AS c",
"MATCH (f:F)-[:R]->(p:P) WITH p.name AS n, count(p.name) AS c RETURN n, c",
),
] {
assert_folds_like_general(&g, folded, general);
}
}
#[test]
fn the_node_counts_are_charged_to_the_expression_budget() {
let mut g = followers_graph();
g.configure_expression_memory_limit(Some(64));
let error = try_exec_cypher(
&g,
"MATCH (f:F)-[:R]->(p:P) RETURN p.name AS n, count(f) AS c",
)
.err()
.expect("five key nodes cross a 64-byte cap");
assert_expression_cap(&error, 64);
}