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

nordisk/kleene

crates/kleene-plan/README.md

kleene-plan

The plan between kleene's query clients and its engine. Relations are numbered and rules are values (joins, conditions, negation, aggregates, recursion); plan cuts them into strata and picks a kernel for each: a one-column relation grown along a two-column one runs as a BFS on kleene-graph, a two-column closure as masked sparse products, the rest as column kernels (hash joins, anti-joins, sorted groups). Engine runs the strata semi-naive on Arrow columns, and with Engine::explain it keeps the why of every fact (Output::why).

There is no query language here: kleene-datalog (typed rules in Rust) and kleene-gql (GQL text) lower onto this plan. Part of kleene.

use kleene_plan::{Atom, Engine, Facts, Inputs, Plan, Stratum, Term, plan};
use kleene_sparse::{CpuBackend, CpuDevice};

let mut p = Plan::new();
let entry = p.input("entry", 1);
let calls = p.input("calls", 2);
let reach = p.derived("reach", 1);
let (f, g) = (p.var("F"), p.var("G"));
let atom = |rel, vars: &[_]| Atom { rel, terms: vars.iter().map(|&v| Term::Var(v)).collect() };
// reach(F) :- entry(F).   reach(G) :- reach(F), calls(F, G).
p.rule(atom(reach, &[f]), [atom(entry, &[f])])?;
p.rule(atom(reach, &[g]), [atom(reach, &[f]), atom(calls, &[f, g])])?;
assert!(matches!(plan(&p)?[..], [Stratum::Reach { .. }]));

let mut inputs = Inputs::new();
inputs.facts(entry, Facts::from_rows(1, [[0u32]])?);
inputs.facts(calls, Facts::from_rows(2, [[0u32, 1], [1, 2], [3, 4]])?);
// Threads: the rules' joins and the graph kernels run on the device's own workers
// (`CpuDevice::default()` takes every core), not on tokio. Only table reads through
// DataFusion run on the runtime you bring: give it more than one thread.
let engine = Engine::<CpuBackend>::new(CpuDevice::default());
let rt = tokio::runtime::Builder::new_multi_thread().build()?;
let out = rt.block_on(engine.run(&p, &inputs))?;
let mut reached: Vec<u64> = out.get(reach).unwrap().column(0).iter().collect();
reached.sort_unstable();
assert_eq!(reached, [0, 1, 2]);
# Ok::<(), Box<dyn std::error::Error>>(())

Licence

MIT or Apache-2.0, at your option.