crates/kleene-gql/tests/schema.rs
//! The set's schema in GQL: `CALL schema.labels()`, `schema.edges()`, `schema.columns()` and
//! `schema.refused()` read the core's schema relations (`Plan::schema`, filled by
//! `Inputs::bind_schema`), the same relations a Datalog program's rules read.
use std::sync::Arc;
use arrow_array::{ArrayRef, UInt64Array};
use kleene_gql::{Catalog, GqlError, lower, parse};
use kleene_plan::{Datum, Engine, Source};
use kleene_sparse::{CpuBackend, CpuDevice};
fn col(v: Vec<u64>) -> ArrayRef {
Arc::new(UInt64Array::from(v))
}
/// Node label `site` (ids 1, 2, 3) with read column `iid` and refused column `raw`, a label
/// `idle` with no nodes, an edge label `calls` with property `n`.
fn catalog() -> Catalog {
let mut c = Catalog::new();
c.label("site", Source::Columns(vec![col(vec![1, 2, 3])]));
c.property(
"site",
"iid",
Source::Columns(vec![col(vec![1, 2, 3]), col(vec![10, 20, 30])]),
);
c.refused_property("site", "raw", "a list column (List(UInt8))");
c.label("idle", Source::Columns(vec![col(vec![])]));
c.edge(
"calls",
Source::Columns(vec![col(vec![1, 2]), col(vec![2, 3])]),
);
c.edge_property(
"calls",
"n",
Source::Columns(vec![col(vec![1, 2]), col(vec![2, 3]), col(vec![5, 6])]),
);
c
}
fn engine() -> Engine<CpuBackend> {
Engine::new(CpuDevice::new(2))
}
fn s(x: &str) -> Datum {
Datum::Str(x.into())
}
async fn rows(q: &str) -> Vec<Vec<Datum>> {
let l = lower(&parse(q).unwrap(), &catalog()).unwrap();
assert!(l.plan.schema_rels().is_some(), "{q}");
l.run_typed(&engine()).await.unwrap().rows
}
#[tokio::test]
async fn call_schema_reads_the_core_schema_relations() {
assert_eq!(
rows("CALL schema.labels() YIELD label RETURN label ORDER BY label").await,
vec![vec![s("idle")], vec![s("site")]]
);
assert_eq!(
rows("CALL schema.edges() YIELD edge RETURN edge").await,
vec![vec![s("calls")]]
);
assert_eq!(
rows(
"CALL schema.columns() YIELD label, column, type \
RETURN label, column, type ORDER BY label"
)
.await,
vec![
vec![s("calls"), s("n"), s("u64")],
vec![s("site"), s("iid"), s("u64")],
]
);
assert_eq!(
rows(
"CALL schema.refused() YIELD label, column, why WHERE label = 'site' \
RETURN column, why"
)
.await,
vec![vec![s("raw"), s("a list column (List(UInt8))")]]
);
// a label the set lacks, asked of the schema: no row
assert!(
rows("CALL schema.labels() YIELD label WHERE label = 'inst' RETURN label")
.await
.is_empty()
);
}
#[test]
fn a_schema_call_takes_no_arguments_and_yields_its_columns() {
let refused = |q: &str| match lower(&parse(q).unwrap(), &catalog()) {
Err(GqlError::Semantic(why)) => why,
r => panic!("{q}: {:?}", r.map(|_| ())),
};
assert!(refused("CALL schema.labels(calls) YIELD label RETURN label").contains("no arguments"));
assert!(refused("CALL schema.edges() YIELD label RETURN label").contains("edge"));
}
/// What GQL does today with a node label the set lacks, kept as it is: refused by name.
#[test]
fn a_missing_label_in_a_match_is_refused_by_name() {
match lower(&parse("MATCH (n:Missing) RETURN n").unwrap(), &catalog()) {
Err(GqlError::Semantic(why)) => assert!(why.contains("no node label `Missing`"), "{why}"),
r => panic!("{:?}", r.map(|_| ())),
}
}