crates/kleene-gql/tests/count_without_rows.rs
//! A count over a match runs without a table of the matches: `count(*)` and `count(e)` over
//! one edge pattern (with and without labels on its ends, grouped or not) read the edges
//! with their parallel-edge counts in the aggregate itself; no stratum derives the match or
//! the counted matches. Each answer against a plain loop over the edge list, at 1 and 8
//! threads.
use std::collections::BTreeMap;
use std::sync::Arc;
use arrow_array::{ArrayRef, UInt64Array};
use kleene_gql::{Catalog, Params, lower_rows, parse};
use kleene_plan::{Datum, Engine, Facts, Rows, Source};
use kleene_sparse::{CpuBackend, CpuDevice};
/// Edges of label E, repeats are parallel edges: 0->1 three times, 1->2, 2->2, 2->3
/// twice, 3->4, 4->3, 4->5, 1->3.
const EDGES: [(u64, u64); 11] = [
(0, 1),
(0, 1),
(0, 1),
(1, 2),
(2, 2),
(2, 3),
(2, 3),
(3, 4),
(4, 3),
(4, 5),
(1, 3),
];
/// The nodes of label M (all six have label N).
const M: [u64; 4] = [0, 1, 2, 3];
fn catalog() -> Catalog {
let mut c = Catalog::new();
let ids = |xs: &[u64]| -> ArrayRef { Arc::new(UInt64Array::from_iter_values(xs.to_vec())) };
let mut pairs: Vec<[u64; 2]> = EDGES.iter().map(|&(a, b)| [a, b]).collect();
pairs.sort_unstable();
pairs.dedup();
let mut counts: BTreeMap<(u64, u64), u64> = BTreeMap::new();
for &e in &EDGES {
*counts.entry(e).or_default() += 1;
}
c.label("N", Source::Columns(vec![ids(&[0, 1, 2, 3, 4, 5])]))
.label("M", Source::Columns(vec![ids(&M)]))
.edge("E", Source::Facts(Facts::from_rows(2, pairs).unwrap()))
.edge_counts(
"E",
Source::Facts(
Facts::from_rows(3, counts.iter().map(|(&(a, b), &n)| [a, b, n])).unwrap(),
),
);
c
}
/// The rows of `q` at `threads`, sorted, and the relations of the strata that ran with
/// rows or facts.
async fn run(q: &str, threads: usize) -> (Vec<Vec<Datum>>, Vec<String>) {
let c = catalog();
let l = lower_rows(&parse(q).unwrap(), &c, &Params::new(), Rows::Bag)
.unwrap_or_else(|e| panic!("{q}: {e}"));
let e: Engine<CpuBackend> = Engine::new(CpuDevice::new(threads));
let mut rows = l
.run_typed(&e)
.await
.unwrap_or_else(|e| panic!("{q}: {e}"))
.rows;
rows.sort_by_key(|r| format!("{r:?}"));
let o = e.run(&l.plan, &l.inputs).await.unwrap();
let ran = o
.report
.strata
.iter()
.filter(|s| s.peak_rows > 0 || s.facts > 0)
.flat_map(|s| s.relations.clone())
.collect();
(rows, ran)
}
fn one(n: usize) -> Vec<Vec<Datum>> {
vec![vec![Datum::U64(n as u64)]]
}
fn in_m(x: u64) -> bool {
M.contains(&x)
}
/// Per start in M: its edges to M, parallel ones each.
fn grouped() -> Vec<Vec<Datum>> {
let mut by: BTreeMap<u64, u64> = BTreeMap::new();
for &(a, b) in &EDGES {
if in_m(a) && in_m(b) {
*by.entry(a).or_default() += 1;
}
}
let mut rows: Vec<Vec<Datum>> = by
.into_iter()
.map(|(a, n)| vec![Datum::U64(a), Datum::U64(n)])
.collect();
rows.sort_by_key(|r| format!("{r:?}"));
rows
}
#[tokio::test]
async fn a_count_over_one_edge_builds_no_match_rows() {
let all = EDGES.len();
let m_to_m = EDGES.iter().filter(|e| in_m(e.0) && in_m(e.1)).count();
let from_m = EDGES.iter().filter(|e| in_m(e.0)).count();
let cases: [(&str, Vec<Vec<Datum>>); 6] = [
("MATCH (a)-[:E]->(b) RETURN count(*)", one(all)),
("MATCH (a)-[e:E]->(b) RETURN count(e)", one(all)),
("MATCH (a:N)-[:E]->(b:N) RETURN count(*)", one(all)),
("MATCH (a:M)-[:E]->(b:M) RETURN count(*)", one(m_to_m)),
("MATCH (a:M)-[:E]->(b) RETURN count(*)", one(from_m)),
("MATCH (a:M)-[e:E]->(b:M) RETURN a, count(e)", grouped()),
];
for (q, want) in &cases {
for threads in [1, 8] {
let (rows, ran) = run(q, threads).await;
assert_eq!(&rows, want, "{q} at {threads}");
assert!(
!ran.iter().any(|r| r == "match" || r == "matches"),
"{q}: {ran:?}"
);
}
}
}
/// A count over two steps: each pair of edges (parallel ones each) one match.
#[tokio::test]
async fn a_count_over_two_steps_counts_each_pair_of_edges() {
let walks = EDGES
.iter()
.flat_map(|x| EDGES.iter().filter(move |y| y.0 == x.1))
.count();
for threads in [1, 8] {
let (rows, _) = run("MATCH (a)-[:E]->(b)-[:E]->(c) RETURN count(*)", threads).await;
assert_eq!(rows, one(walks));
}
}