crates/kleene-gql/tests/range_join.rs
//! A `WHERE` that holds a value of one node between two values of another node it is not
//! linked to (`k.a >= p.lo AND k.a < p.hi`) runs as a sort and a search in the core: the
//! same rows as a plain loop, at every thread count, and the same proof of every match as
//! the run with the sort and search off.
use std::collections::BTreeSet;
use kleene_gql::{Catalog, lower, parse};
use kleene_plan::{Engine, Facts, Source, Value};
use kleene_sparse::{CpuBackend, CpuDevice};
/// Deterministic pseudo-random numbers (a 64-bit LCG), no dependency.
struct Lcg(u64);
impl Lcg {
fn below(&mut self, m: u64) -> u64 {
self.0 = self
.0
.wrapping_mul(6364136223846793005)
.wrapping_add(1442695040888963407);
(self.0 >> 11) % m
}
}
fn ones(xs: &[u64]) -> Source {
Source::Facts(Facts::from_rows(1, xs.iter().map(|&x| [x])).unwrap())
}
fn twos(xs: &[(u64, u64)]) -> Source {
Source::Facts(Facts::from_rows(2, xs.iter().map(|&(a, b)| [a, b])).unwrap())
}
#[tokio::test]
async fn a_value_between_two_properties_of_an_unlinked_node_joins_by_a_sort_and_a_search() {
let mut r = Lcg(5);
// sites 0..300 with an address; pieces 1000.. with overlapping and nested [lo, hi)
let ks: Vec<u64> = (0..300).collect();
let ka: Vec<(u64, u64)> = ks.iter().map(|&k| (k, r.below(1000))).collect();
let ps: Vec<u64> = (1000..1120).collect();
let mut plo = Vec::new();
let mut phi = Vec::new();
for &p in &ps {
let lo = r.below(1000);
let hi = lo + if p % 5 == 0 { 400 } else { r.below(50) };
plo.push((p, lo));
phi.push((p, hi));
}
let mut c = Catalog::new();
c.label("K", ones(&ks))
.label("P", ones(&ps))
.property("K", "a", twos(&ka))
.property("P", "lo", twos(&plo))
.property("P", "hi", twos(&phi));
let mut want: BTreeSet<Vec<Value>> = BTreeSet::new();
for &(k, a) in &ka {
for ((p, lo), (_, hi)) in plo.iter().zip(&phi) {
if a >= *lo && a < *hi {
want.insert(vec![k, *p]);
}
}
}
assert!(want.len() > 100, "many matches");
let text = "MATCH (k:K), (p:P) WHERE k.a >= p.lo AND k.a < p.hi RETURN k, p";
let l = lower(&parse(text).unwrap(), &c).unwrap();
let engine = |threads: usize, on: bool| -> Engine<CpuBackend> {
Engine::new(CpuDevice::new(threads))
.explain(true)
.range_joins(on)
};
// the facts and proofs of the match relation, and the join steps run by a band
let run = |threads: usize, on: bool| {
let l = &l;
async move {
let out = engine(threads, on).run(&l.plan, &l.inputs).await.unwrap();
let f: Vec<Vec<Value>> = out.get(l.result).expect("matches").rows().collect();
let proofs: Vec<Option<String>> = f
.iter()
.map(|t| out.why(&l.plan, l.result, t).map(|q| q.render(&l.plan)))
.collect();
let n: usize = out.report.strata.iter().map(|s| s.range_joins).sum();
(f, proofs, n)
}
};
let (off, off_proofs, n_off) = run(1, false).await;
assert_eq!(n_off, 0);
assert!(
off_proofs.iter().all(Option::is_some),
"a proof of every match"
);
for threads in [1, 2, 8] {
let t = l.run(&engine(threads, true)).await.unwrap();
let got: BTreeSet<Vec<Value>> = t.rows.into_iter().collect();
assert_eq!(got, want, "{threads} threads");
let (f, proofs, n) = run(threads, true).await;
assert_eq!(f, off, "{threads} threads");
assert_eq!(proofs, off_proofs, "{threads} threads");
assert!(
n >= 1,
"the WHERE joined by a sort and a search ({threads} threads)"
);
}
}