Your browser sucks, JavaScript disabled! Gunnar's console needs JavaScript and WebAssembly. Git over SSH on port 2222 does not.

nordisk/kleene

crates/kleene-gql/tests/next_row.rs

//! `min` (`max`) of a property of a node past (before) a property of another node it is not
//! linked to (`MATCH (a:N), (b:N) WHERE a.x < b.x RETURN a, min(b.x)`): with no proof kept
//! the core reduces the matches where they are found, as the next row by a key (a sort and
//! one search per row); the same rows as a plain loop at every thread count. With proofs
//! kept the query runs as today (the proof of `min` lists every match of its group).

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 min_past_a_property_of_an_unlinked_node_runs_as_the_next_row() {
    let mut r = Lcg(17);
    // nodes 0..400 with a value (repeats), some nodes of another label with values too
    let ns: Vec<u64> = (0..400).collect();
    let nx: Vec<(u64, u64)> = ns.iter().map(|&n| (n, r.below(900))).collect();
    let ms: Vec<u64> = (1000..1050).collect();
    let mx: Vec<(u64, u64)> = ms.iter().map(|&m| (m, r.below(900))).collect();
    let mut c = Catalog::new();
    c.label("N", ones(&ns))
        .label("M", ones(&ms))
        .property("N", "x", twos(&nx))
        .property("M", "x", twos(&mx));
    let vals: BTreeSet<u64> = nx.iter().map(|x| x.1).collect();
    let mut next: BTreeSet<Vec<Value>> = BTreeSet::new();
    let mut prev: BTreeSet<Vec<Value>> = BTreeSet::new();
    for &(n, x) in &nx {
        if let Some(&y) = vals.range(x + 1..).next() {
            next.insert(vec![n, y]);
        }
        if let Some(&y) = vals.range(..x).next_back() {
            prev.insert(vec![n, y]);
        }
    }
    assert!(next.len() > 300 && prev.len() > 300);
    let engine = |threads: usize, on: bool, explain: bool| -> Engine<CpuBackend> {
        Engine::new(CpuDevice::new(threads))
            .next_rows(on)
            .explain(explain)
    };
    for (text, want) in [
        (
            "MATCH (a:N), (b:N) WHERE a.x < b.x RETURN a, min(b.x) AS n",
            &next,
        ),
        (
            "MATCH (a:N), (b:N) WHERE b.x < a.x RETURN a, max(b.x) AS p",
            &prev,
        ),
    ] {
        let l = lower(&parse(text).unwrap(), &c).unwrap();
        let count = |on: bool, explain: bool| {
            let l = &l;
            async move {
                let out = engine(2, on, explain)
                    .run(&l.plan, &l.inputs)
                    .await
                    .unwrap();
                out.report.strata.iter().map(|s| s.next_rows).sum::<usize>()
            }
        };
        for threads in [1, 2, 8] {
            for (on, explain) in [(true, false), (false, false), (true, true)] {
                let t = l.run(&engine(threads, on, explain)).await.unwrap();
                let got: BTreeSet<Vec<Value>> = t.rows.into_iter().collect();
                assert_eq!(
                    &got, want,
                    "{text}: {threads} threads, on {on}, proofs {explain}"
                );
            }
        }
        assert_eq!(count(true, false).await, 1, "{text}: the next row");
        assert_eq!(count(false, false).await, 0, "{text}: off");
        assert_eq!(count(true, true).await, 0, "{text}: with proofs, as today");
    }
}