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/side_by_side.rs

//! A node's properties read in one scan: GQL lowers one atom per property read, and the
//! core reads the atoms over columns side by side (one id array) as one scan
//! (`Engine::side_by_side`), with the same rows as the joins.

use std::sync::Arc;

use arrow_array::{ArrayRef, UInt64Array};
use kleene_gql::{Catalog, Params, lower_with, parse};
use kleene_plan::{Engine, Source};
use kleene_sparse::{CpuBackend, CpuDevice};

const N: u64 = 50;

fn col(f: impl Fn(u64) -> u64) -> ArrayRef {
    Arc::new(UInt64Array::from((0..N).map(f).collect::<Vec<_>>()))
}

fn catalog() -> Catalog {
    let ids = col(|r| r);
    let mut c = Catalog::new();
    c.label("t", Source::Columns(vec![ids.clone()]));
    c.property(
        "t",
        "a",
        Source::Columns(vec![ids.clone(), col(|r| r * 7 % 10)]),
    );
    c.property(
        "t",
        "b",
        Source::Columns(vec![ids.clone(), col(|r| r * 3 % 11)]),
    );
    c.property("t", "c", Source::Columns(vec![ids, col(|r| r + 100)]));
    c
}

#[tokio::test]
async fn a_nodes_properties_read_in_one_scan() {
    let text = "MATCH (n:t) WHERE n.a = 7 RETURN n.b, n.c";
    let l = lower_with(&parse(text).unwrap(), &catalog(), &Params::new()).unwrap();
    for threads in [1, 2, 4] {
        let on: Engine<CpuBackend> = Engine::new(CpuDevice::new(threads));
        let off: Engine<CpuBackend> = Engine::new(CpuDevice::new(threads)).side_by_side(false);
        let mut got = l.run(&on).await.unwrap().rows;
        got.sort_unstable();
        let mut want = l.run(&off).await.unwrap().rows;
        want.sort_unstable();
        assert_eq!(got, want, "{threads} threads");
        let mut plain: Vec<Vec<u64>> = (0..N)
            .filter(|r| r * 7 % 10 == 7)
            .map(|r| vec![r * 3 % 11, r + 100])
            .collect();
        plain.sort_unstable();
        assert_eq!(got, plain);
        // the lowered plan's property atoms ran as one scan
        let out = on.run(&l.plan, &l.inputs).await.unwrap();
        assert!(out.report.side_by_side >= 3, "{}", out.report.side_by_side);
        let out = off.run(&l.plan, &l.inputs).await.unwrap();
        assert_eq!(out.report.side_by_side, 0);
    }
}

/// `min` of a property past another node's property, with a second property of the reduced
/// node read too: with no proof kept the core fuses the aggregate with its match and runs it
/// as the next row by a key, and the two columns of `b` merge inside the fused rule. Both
/// knobs (`side_by_side`, `next_rows`) on and off give the rows of a plain loop.
#[tokio::test]
async fn columns_side_by_side_inside_a_fused_aggregate() {
    let text = "MATCH (a:t), (b:t) WHERE a.a < b.b AND b.c > 120 RETURN a, min(b.b) AS n";
    let l = lower_with(&parse(text).unwrap(), &catalog(), &Params::new()).unwrap();
    let mut plain: Vec<Vec<u64>> = (0..N)
        .filter_map(|i| {
            (0..N)
                .filter(|&s| s + 100 > 120 && i * 7 % 10 < s * 3 % 11)
                .map(|s| s * 3 % 11)
                .min()
                .map(|y| vec![i, y])
        })
        .collect();
    plain.sort_unstable();
    assert!(plain.len() > N as usize / 2);
    for threads in [1, 2, 4] {
        for side in [true, false] {
            for next in [true, false] {
                let e: Engine<CpuBackend> = Engine::new(CpuDevice::new(threads))
                    .side_by_side(side)
                    .next_rows(next);
                let at = format!("{threads} threads, side {side}, next {next}");
                let mut got = l.run(&e).await.unwrap().rows;
                got.sort_unstable();
                assert_eq!(got, plain, "{at}");
                let out = e.run(&l.plan, &l.inputs).await.unwrap();
                let ran: usize = out.report.strata.iter().map(|s| s.next_rows).sum();
                assert_eq!(ran, usize::from(next), "{at}");
                match side {
                    true => assert!(out.report.side_by_side >= 2, "{at}"),
                    false => assert_eq!(out.report.side_by_side, 0, "{at}"),
                }
            }
        }
    }
}