crates/kleene-gql/tests/zox.rs
//! The same reach question through both clients on one plan engine, on a zox-M set
//! (`KLEENE_GAR_DIR` names it; skipped without): Datalog's `reach(F) :- entry(F).
//! reach(G) :- reach(F), calls(F, G).` and GQL's `MATCH (e:Entry)-[:CALLS]->*(f) RETURN
//! DISTINCT f` give EQUAL rows, and both equal a plain walk.
use std::collections::VecDeque;
use arrow_array::cast::AsArray;
use arrow_array::types::UInt32Type;
use kleene_datalog::{Inputs, Program};
use kleene_gar::GarGraph;
use kleene_gql::{Catalog, lower, parse};
use kleene_plan::{Source, Stratum, Way};
use kleene_sparse::{CpuBackend, CpuDevice, Csr, Pattern};
const EDGE: &str = "inst_icall_inst";
/// The edge type as a structural matrix and its transpose, shared with the mapped files.
fn shared(g: &GarGraph) -> (Csr<u8>, Csr<u8>) {
let et = g.edge_type(EDGE).expect("the edge type").clone();
let adj = g.adjacency(&et).unwrap();
let out = Csr::<u8>::with_pattern(
Pattern::trusted(
adj.sources,
adj.destinations,
adj.out_offsets.clone(),
adj.out_neighbours.clone(),
"by source",
)
.unwrap(),
None,
)
.unwrap();
let inn = Csr::<u8>::with_pattern(
Pattern::trusted(
adj.destinations,
adj.sources,
adj.in_offsets.clone(),
adj.in_neighbours.clone(),
"by destination",
)
.unwrap(),
None,
)
.unwrap();
(out, inn)
}
#[tokio::test]
async fn datalog_and_gql_reach_are_equal_on_zox_m() {
let Ok(dir) = std::env::var("KLEENE_GAR_DIR") else {
eprintln!("KLEENE_GAR_DIR not set: skipped");
return;
};
let g = GarGraph::open(&dir).unwrap();
let (out, inn) = shared(&g);
let entries = g.vertex_column("entry", "iid").unwrap();
// Client 1: the Datalog rule builder.
let mut p = Program::new();
let entry = p.input::<1>("entry");
let calls = p.input::<2>("calls");
let reach = p.derived::<1>("reach");
let (f, gv) = (p.var("F"), p.var("G"));
p.rule(reach.at([f]), [entry.at([f])]).unwrap();
p.rule(reach.at([gv]), [reach.at([f]), calls.at([f, gv])])
.unwrap();
let mut inputs = Inputs::new();
inputs
.graph(&calls, out.clone(), Some(inn.clone()))
.columns(&entry, [entries.clone()]);
let dl = kleene_datalog::Engine::<CpuBackend>::new(CpuDevice::new(4));
let o = dl.run(&p, &inputs).await.unwrap();
let datalog: Vec<Vec<u64>> = o.get(&reach).unwrap().sorted_rows();
// Client 2: GQL text.
let mut c = Catalog::new();
c.label("Entry", Source::Columns(vec![entries.clone()]))
.edge(
"CALLS",
Source::Graph {
out: out.clone(),
inn: Some(inn.clone()),
},
);
let lowered = lower(
&parse("MATCH (e:Entry)-[:CALLS]->*(f) RETURN DISTINCT f").unwrap(),
&c,
)
.unwrap();
let strata = lowered.strata().unwrap();
assert!(
strata.iter().any(|s| matches!(
s,
Stratum::Reach { rel, way: Way::Forward, .. } if lowered.paths.contains(rel)
)),
"{strata:?}"
);
let engine = kleene_plan::Engine::<CpuBackend>::new(CpuDevice::new(4));
let gql = lowered.run(&engine).await.unwrap();
assert_eq!(gql.columns, ["f"]);
// A plain walk over the mapped files.
let seeds: Vec<u32> = entries
.as_primitive::<UInt32Type>()
.values()
.iter()
.copied()
.collect();
let mut seen = vec![false; out.nrows()];
let mut q: VecDeque<u32> = VecDeque::new();
for &s in &seeds {
if !seen[s as usize] {
seen[s as usize] = true;
q.push_back(s);
}
}
while let Some(v) = q.pop_front() {
for &w in out.row(v as usize) {
if !seen[w as usize] {
seen[w as usize] = true;
q.push_back(w);
}
}
}
let plain: Vec<Vec<u64>> = (0..seen.len())
.filter(|&v| seen[v])
.map(|v| vec![v as u64])
.collect();
eprintln!(
"zox-M reach: datalog {} rows, gql {} rows, plain {}",
datalog.len(),
gql.rows.len(),
plain.len()
);
assert!(!datalog.is_empty());
assert_eq!(gql.rows, datalog, "GQL and Datalog rows differ");
assert_eq!(datalog, plain);
}