use relate_core::Value;
use super::agg_fold::take_path_fold_bindings;
use super::test_support::{exec_cypher, Builder};
use crate::graph::Graph;
fn test_linear_graph() -> Graph {
let b = Builder::new();
for i in 0..10 {
let a = b.node(&format!("a{i}"), &["A"]);
let val_b = if i % 2 == 0 {
Some(i as i64 * 10)
} else {
None
};
let b_node = b.prop_node(
&format!("b{i}"),
&["B"],
&[
("val", val_b.map(Value::Int).unwrap_or(Value::Null)),
("grp", Value::String(if i < 5 { "g1" } else { "g2" }.into())),
],
);
let c = b.prop_node(
&format!("c{i}"),
&["C"],
&[("score", Value::Int((i as i64 + 1) * 5))],
);
let d = b.node(&format!("d{i}"), &["D"]);
let e = b.node(&format!("e{i}"), &["E"]);
let f = b.prop_node(
&format!("f{i}"),
&["F"],
&[("weight", Value::Int(i as i64 * 2))],
);
b.rel(a, b_node, "R", &[]);
b.rel(b_node, c, "R", &[]);
b.rel(c, d, "R", &[]);
b.rel(d, e, "R", &[]);
b.rel(e, f, "R", &[]);
}
b.done()
}
#[test]
fn morsel_agg_ungrouped_1_to_5_hops() {
let g = test_linear_graph();
let _ = take_path_fold_bindings();
let r1 = exec_cypher(
&g,
"MATCH (a:A)-[:R]->(b:B) RETURN count(*) AS c, sum(b.val) AS s, min(b.val) AS mi, max(b.val) AS ma",
);
assert_eq!(
r1.rows,
vec![vec![
Value::Int(10),
Value::Int(200), Value::Int(0),
Value::Int(80),
]]
);
assert!(take_path_fold_bindings() >= 10);
let r2 = exec_cypher(
&g,
"MATCH (a:A)-[:R]->()-[:R]->(c:C) RETURN count(*) AS c, sum(c.score) AS s, min(c.score) AS mi, max(c.score) AS ma",
);
assert_eq!(
r2.rows,
vec![vec![
Value::Int(10),
Value::Int(275), Value::Int(5),
Value::Int(50),
]]
);
let r3 = exec_cypher(
&g,
"MATCH (a:A)-[:R]->()-[:R]->()-[:R]->(d:D) RETURN count(*) AS c",
);
assert_eq!(r3.rows, vec![vec![Value::Int(10)]]);
let r5 = exec_cypher(
&g,
"MATCH (a:A)-[:R]->()-[:R]->()-[:R]->()-[:R]->()-[:R]->(f:F) \
RETURN count(*) AS c, sum(f.weight) AS s, min(f.weight) AS mi, max(f.weight) AS ma",
);
assert_eq!(
r5.rows,
vec![vec![
Value::Int(10),
Value::Int(90), Value::Int(0),
Value::Int(18),
]]
);
}
#[test]
fn morsel_agg_grouped_by_node_id_and_props() {
let g = test_linear_graph();
let r_node = exec_cypher(
&g,
"MATCH (a:A)-[:R]->(b:B) RETURN a, count(*) AS c ORDER BY c DESC LIMIT 3",
);
assert_eq!(r_node.rows.len(), 3);
for row in &r_node.rows {
assert_eq!(row[1], Value::Int(1));
}
let r_prop = exec_cypher(
&g,
"MATCH (a:A)-[:R]->(b:B) RETURN b.grp AS grp, count(*) AS c, sum(b.val) AS s ORDER BY grp",
);
assert_eq!(
r_prop.rows,
vec![
vec![Value::String("g1".into()), Value::Int(5), Value::Int(60)], vec![Value::String("g2".into()), Value::Int(5), Value::Int(140)], ]
);
}
#[test]
fn morsel_agg_distinct_variants() {
let b = Builder::new();
let n1 = b.node("n1", &["User"]);
let n2 = b.node("n2", &["User"]);
let i1 = b.prop_node("i1", &["Item"], &[("score", Value::Int(10))]);
let i2 = b.prop_node("i2", &["Item"], &[("score", Value::Int(10))]);
let i3 = b.prop_node("i3", &["Item"], &[("score", Value::Int(20))]);
b.rel(n1, i1, "BUYS", &[]);
b.rel(n1, i2, "BUYS", &[]);
b.rel(n2, i2, "BUYS", &[]);
b.rel(n2, i3, "BUYS", &[]);
let g = b.done();
let res = exec_cypher(
&g,
"MATCH (u:User)-[:BUYS]->(i:Item) \
RETURN count(i) AS ci, count(DISTINCT i) AS cdi, \
count(i.score) AS cs, count(DISTINCT i.score) AS cds, \
sum(i.score) AS sum_all, sum(DISTINCT i.score) AS sum_dist, \
collect(DISTINCT i.score) AS col_dist",
);
assert_eq!(
res.rows,
vec![vec![
Value::Int(4),
Value::Int(3),
Value::Int(4),
Value::Int(2),
Value::Int(50), Value::Int(30), Value::List(vec![Value::Int(10), Value::Int(20)].into()),
]]
);
}
#[test]
fn morsel_agg_predicate_perturbations() {
let g = test_linear_graph();
let base = exec_cypher(
&g,
"MATCH (a:A)-[:R]->(b:B) WHERE b.val = 20 RETURN count(*) AS c",
);
assert_eq!(base.rows, vec![vec![Value::Int(1)]]);
let or_res = exec_cypher(
&g,
"MATCH (a:A)-[:R]->(b:B) WHERE b.val = 20 OR b.val = 40 RETURN count(*) AS c, sum(b.val) AS s",
);
assert_eq!(or_res.rows, vec![vec![Value::Int(2), Value::Int(60)]]);
let not_res = exec_cypher(
&g,
"MATCH (a:A)-[:R]->(b:B) WHERE NOT (b.grp = \"g1\") RETURN count(*) AS c",
);
assert_eq!(not_res.rows, vec![vec![Value::Int(5)]]);
let rev_res = exec_cypher(
&g,
"MATCH (b:B)<-[:R]-(a:A) WHERE b.grp = \"g1\" RETURN count(*) AS c",
);
assert_eq!(rev_res.rows, vec![vec![Value::Int(5)]]);
}
#[test]
fn morsel_agg_empty_and_null_semantics() {
let g = Graph::default();
let empty_ungrouped = exec_cypher(
&g,
"MATCH (a:A)-[:R]->(b:B) \
RETURN count(*) AS c, count(b.val) AS cb, sum(b.val) AS s, \
avg(b.val) AS a, min(b.val) AS mi, max(b.val) AS ma, collect(b.val) AS col",
);
assert_eq!(
empty_ungrouped.rows,
vec![vec![
Value::Int(0),
Value::Int(0),
Value::Int(0), Value::Null,
Value::Null,
Value::Null,
Value::List(vec![].into()),
]]
);
let empty_grouped = exec_cypher(
&g,
"MATCH (a:A)-[:R]->(b:B) RETURN a.id AS id, count(*) AS c",
);
assert!(empty_grouped.rows.is_empty());
let b = Builder::new();
let a1 = b.node("a1", &["A"]);
let b1 = b.prop_node("b1", &["B"], &[("val", Value::Null)]);
b.rel(a1, b1, "R", &[]);
let g2 = b.done();
let all_null_res = exec_cypher(
&g2,
"MATCH (a:A)-[:R]->(b:B) RETURN count(b.val) AS c, sum(b.val) AS s",
);
assert_eq!(all_null_res.rows, vec![vec![Value::Int(0), Value::Int(0)]]);
}
#[test]
fn morsel_agg_cyclic_patterns() {
let b = Builder::new();
let n0 = b.node("n0", &["N"]);
let n1 = b.node("n1", &["N"]);
let n2 = b.node("n2", &["N"]);
b.rel(n0, n0, "LOOP", &[]); b.rel(n1, n2, "E", &[]);
b.rel(n2, n1, "E", &[]);
let g = b.done();
let self_loop = exec_cypher(&g, "MATCH (x:N)-[:LOOP]->(x) RETURN count(*) AS c");
assert_eq!(self_loop.rows, vec![vec![Value::Int(1)]]);
let cycle = exec_cypher(&g, "MATCH (a:N)-[:E]->(b:N)-[:E]->(a) RETURN count(*) AS c");
assert_eq!(cycle.rows, vec![vec![Value::Int(2)]]);
}
#[test]
fn morsel_agg_comma_patterns_joined() {
let b = Builder::new();
let c1 = b.prop_node("c1", &["Comment"], &[("id", Value::Int(1))]);
let c2 = b.prop_node("c2", &["Comment"], &[("id", Value::Int(2))]);
let t1 = b.prop_node(
"t1",
&["Tag"],
&[("name", Value::String("Copernicus".into()))],
);
let p1 = b.prop_node("p1", &["Place"], &[("name", Value::String("India".into()))]);
b.rel(c1, t1, "HAS_TAG", &[]);
b.rel(c1, p1, "IN_PLACE", &[]);
b.rel(c2, t1, "HAS_TAG", &[]);
b.rel(c2, p1, "IN_PLACE", &[]);
let g = b.done();
let res = exec_cypher(
&g,
"MATCH (c:Comment)-[:HAS_TAG]->(t:Tag), (c)-[:IN_PLACE]->(l:Place) \
WHERE t.name = \"Copernicus\" \
RETURN l.name AS name, count(c.id) AS count \
ORDER BY count DESC",
);
assert_eq!(
res.rows,
vec![vec![Value::String("India".into()), Value::Int(2)]]
);
}
#[test]
fn morsel_agg_comma_patterns_merges_labels_on_shared_node() {
let b = Builder::new();
let a = b.node("a", &["A"]);
let b_node = b.node("b", &["B"]);
let h_both = b.node("h1", &["H1", "H2"]);
let h_only1 = b.node("h2", &["H1"]);
b.rel(a, h_both, "R1", &[]);
b.rel(b_node, h_both, "R2", &[]);
b.rel(a, h_only1, "R1", &[]);
b.rel(b_node, h_only1, "R2", &[]);
let g = b.done();
let res = exec_cypher(
&g,
"MATCH (a:A)-[:R1]->(h:H1), (b:B)-[:R2]->(h:H2) RETURN count(*) AS c",
);
assert_eq!(res.rows, vec![vec![Value::Int(1)]]);
}
#[test]
fn morsel_agg_snw_q1_differential() {
let b = Builder::new();
let p1 = b.prop_node(
"p1",
&["Person"],
&[
("personID", Value::Int(101)),
("name", Value::String("Alice".into())),
],
);
let p2 = b.prop_node(
"p2",
&["Person"],
&[
("personID", Value::Int(102)),
("name", Value::String("Bob".into())),
],
);
let p3 = b.prop_node(
"p3",
&["Person"],
&[
("personID", Value::Int(103)),
("name", Value::String("Charlie".into())),
],
);
let _p4 = b.prop_node(
"p4",
&["Person"],
&[
("personID", Value::Int(104)),
("name", Value::String("Dave".into())),
],
);
let f1 = b.node("f1", &["Person"]);
let f2 = b.node("f2", &["Person"]);
let f3 = b.node("f3", &["Person"]);
let f4 = b.node("f4", &["Person"]);
b.rel(f1, p1, "FOLLOWS", &[]);
b.rel(f2, p1, "FOLLOWS", &[]);
b.rel(f3, p1, "FOLLOWS", &[]);
b.rel(f1, p2, "FOLLOWS", &[]);
b.rel(f4, p2, "FOLLOWS", &[]);
b.rel(f2, p3, "FOLLOWS", &[]);
let g = b.done();
let _ = take_path_fold_bindings();
let query = "MATCH (f:Person)-[:FOLLOWS]->(p:Person) \
RETURN p.personID, p.name, count(f) AS c \
ORDER BY c DESC LIMIT 3";
let res = exec_cypher(&g, query);
assert!(take_path_fold_bindings() > 0, "morsel agg must be engaged");
let oracle_query = "MATCH (f:Person)-[:FOLLOWS]->(p:Person) \
WITH f, p \
RETURN p.personID, p.name, count(f) AS c \
ORDER BY c DESC LIMIT 3";
let oracle = exec_cypher(&g, oracle_query);
assert_eq!(res.rows, oracle.rows);
assert_eq!(
res.rows,
vec![
vec![
Value::Int(101),
Value::String("Alice".into()),
Value::Int(3),
],
vec![Value::Int(102), Value::String("Bob".into()), Value::Int(2),],
vec![
Value::Int(103),
Value::String("Charlie".into()),
Value::Int(1),
],
]
);
}
#[test]
fn morsel_agg_colliding_property_tuples_merge() {
let b = Builder::new();
let p1a = b.prop_node(
"p1a",
&["Person"],
&[
("personID", Value::Int(101)),
("name", Value::String("Alice".into())),
("weight", Value::Int(10)),
("score", Value::Float(2.5)),
],
);
let p1b = b.prop_node(
"p1b",
&["Person"],
&[
("personID", Value::Int(101)),
("name", Value::String("Alice".into())),
("weight", Value::Int(20)),
("score", Value::Float(7.5)),
],
);
let p2a = b.prop_node(
"p2a",
&["Person"],
&[
("personID", Value::Null),
("name", Value::Null),
("weight", Value::Int(5)),
("score", Value::Float(1.0)),
],
);
let p2b = b.prop_node(
"p2b",
&["Person"],
&[
("personID", Value::Null),
("name", Value::Null),
("weight", Value::Null),
("score", Value::Null),
],
);
let p3 = b.prop_node(
"p3",
&["Person"],
&[
("personID", Value::Int(103)),
("name", Value::String("Bob".into())),
("weight", Value::Int(100)),
("score", Value::Float(50.0)),
],
);
let f1 = b.node("f1", &["Person"]);
let f2 = b.node("f2", &["Person"]);
let f3 = b.node("f3", &["Person"]);
let f4 = b.node("f4", &["Person"]);
b.rel(f1, p1a, "FOLLOWS", &[]);
b.rel(f2, p1a, "FOLLOWS", &[]);
b.rel(f3, p1b, "FOLLOWS", &[]);
b.rel(f1, p2a, "FOLLOWS", &[]);
b.rel(f4, p2b, "FOLLOWS", &[]);
b.rel(f2, p3, "FOLLOWS", &[]);
let g = b.done();
let _ = take_path_fold_bindings();
let query = "MATCH (f:Person)-[:FOLLOWS]->(p:Person) \
RETURN p.personID AS pid, p.name AS name, \
count(*) AS c_star, \
count(f) AS c_f, \
sum(p.weight) AS sum_w, \
avg(p.score) AS avg_s, \
min(p.weight) AS min_w, \
max(p.weight) AS max_w, \
collect(p.weight) AS col_w \
ORDER BY c_star DESC, pid, name";
let res = exec_cypher(&g, query);
assert!(take_path_fold_bindings() > 0, "morsel agg must be engaged");
let oracle_query = "MATCH (f:Person)-[:FOLLOWS]->(p:Person) \
WITH f, p \
RETURN p.personID AS pid, p.name AS name, \
count(*) AS c_star, \
count(f) AS c_f, \
sum(p.weight) AS sum_w, \
avg(p.score) AS avg_s, \
min(p.weight) AS min_w, \
max(p.weight) AS max_w, \
collect(p.weight) AS col_w \
ORDER BY c_star DESC, pid, name";
let oracle = exec_cypher(&g, oracle_query);
assert_eq!(res.rows, oracle.rows);
}
#[test]
fn morsel_agg_distinct_with_node_prop_fallback_to_general() {
let b = Builder::new();
let p1 = b.prop_node(
"p1",
&["Person"],
&[("name", Value::String("Shared".into()))],
);
let p2 = b.prop_node(
"p2",
&["Person"],
&[("name", Value::String("Shared".into()))],
);
let f1 = b.node("f1", &["Person"]);
let f2 = b.node("f2", &["Person"]);
b.rel(f1, p1, "FOLLOWS", &[]);
b.rel(f1, p2, "FOLLOWS", &[]);
b.rel(f2, p2, "FOLLOWS", &[]);
let g = b.done();
let query = "MATCH (f:Person)-[:FOLLOWS]->(p:Person) \
RETURN p.name, count(DISTINCT f) AS c";
let res = exec_cypher(&g, query);
let oracle_query = "MATCH (f:Person)-[:FOLLOWS]->(p:Person) \
WITH f, p \
RETURN p.name, count(DISTINCT f) AS c";
let oracle = exec_cypher(&g, oracle_query);
assert_eq!(res.rows, oracle.rows);
assert_eq!(
res.rows,
vec![vec![Value::String("Shared".into()), Value::Int(2)]]
);
}
#[test]
fn morsel_agg_mixed_node_and_node_prop_keys() {
let b = Builder::new();
let p1 = b.prop_node(
"p1",
&["Person"],
&[("name", Value::String("SameName".into()))],
);
let p2 = b.prop_node(
"p2",
&["Person"],
&[("name", Value::String("SameName".into()))],
);
let f1 = b.node("f1", &["Person"]);
let f2 = b.node("f2", &["Person"]);
let f3 = b.node("f3", &["Person"]);
b.rel(f1, p1, "FOLLOWS", &[]);
b.rel(f2, p1, "FOLLOWS", &[]);
b.rel(f3, p2, "FOLLOWS", &[]);
let g = b.done();
let _ = take_path_fold_bindings();
let query = "MATCH (f:Person)-[:FOLLOWS]->(p:Person) \
RETURN p, p.name, count(f) AS c \
ORDER BY c DESC";
let res = exec_cypher(&g, query);
assert!(take_path_fold_bindings() > 0, "morsel agg must be engaged");
let oracle_query = "MATCH (f:Person)-[:FOLLOWS]->(p:Person) \
WITH f, p \
RETURN p, p.name, count(f) AS c \
ORDER BY c DESC";
let oracle = exec_cypher(&g, oracle_query);
assert_eq!(res.rows, oracle.rows);
assert_eq!(res.rows.len(), 2);
assert_eq!(res.rows[0][2], Value::Int(2));
assert_eq!(res.rows[1][2], Value::Int(1));
}