crates/kleene-gar/tests/catalog.rs
//! A set's schema as kleene-plan's catalog ([`GarGraph::catalog`]): vertex types as labels
//! and id blocks of the one id space, edge types by table and bare edge name, vertex columns
//! and edge properties by their Arrow types, a column no reader takes refused by name.
use std::path::{Path, PathBuf};
use std::sync::Arc;
use arrow_array::{Array, ArrayRef, Decimal128Array, Int64Array, RecordBatch, StringArray};
use arrow_schema::{Field, Schema};
use graphar::mapped::{IntColumn, IntEdges, IntGraphWriter, write_ipc};
use kleene_gar::GarGraph;
use kleene_plan::{PlanError, Source, Type};
struct TempDir(PathBuf);
impl Drop for TempDir {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
/// Column `name` of vertex type `t` written as one chunk the way another writer would.
fn put(dir: &Path, t: &str, name: &str, values: ArrayRef) {
let nullable = values.null_count() > 0;
let schema = Schema::new(vec![Field::new(name, values.data_type().clone(), nullable)]);
let batch = RecordBatch::try_new(Arc::new(schema), vec![values]).unwrap();
write_ipc(dir.join("vertex").join(t).join(name).join("chunk0"), &batch).unwrap();
}
fn set() -> TempDir {
let dir = std::env::temp_dir().join(format!("kleene-gar-catalog-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let mut w = IntGraphWriter::new(&dir, "X").unwrap();
w.vertices(
"a",
3,
vec![("n".to_string(), IntColumn::U32(vec![5, 6, 7]))],
)
.unwrap();
w.vertices("b", 2, vec![]).unwrap();
w.edges(
IntEdges::from_pairs(
("a", "to", "b"),
(3, 2),
&[0, 2],
&[1, 0],
vec![("w".to_string(), IntColumn::U32(vec![9, 8]))],
)
.unwrap(),
)
.unwrap();
w.edges(IntEdges::from_pairs(("a", "link", "a"), (3, 3), &[0], &[1], vec![]).unwrap())
.unwrap();
w.edges(IntEdges::from_pairs(("b", "link", "a"), (2, 3), &[1], &[2], vec![]).unwrap())
.unwrap();
w.finish().unwrap();
put(
&dir,
"a",
"t",
Arc::new(Int64Array::from(vec![Some(-1), None, Some(4)])),
);
put(
&dir,
"a",
"s",
Arc::new(StringArray::from(vec!["x", "y", "z"])),
);
let d = Decimal128Array::from(vec![1, 2])
.with_precision_and_scale(10, 2)
.unwrap();
put(&dir, "b", "d", Arc::new(d));
TempDir(dir)
}
#[test]
fn a_sets_schema_is_a_catalog() {
let dir = set();
let g = GarGraph::open(&dir.0).unwrap();
let c = g.catalog().unwrap();
let space = g.id_space().unwrap();
let types = |n: &str| c.relation(n).map(|x| x.1);
// labels: the types' ids, each a block of the one id space
for t in ["a", "b"] {
assert_eq!(types(t).unwrap(), [Type::U64]);
assert_eq!(c.block(t), space.of(t));
}
let Source::Columns(ids) = c.relation("b").unwrap().0 else {
panic!("b's ids as a column")
};
let b = space.base("b").unwrap();
assert_eq!(ids[0].len(), 2);
assert_eq!(
ids[0]
.as_any()
.downcast_ref::<arrow_array::UInt64Array>()
.unwrap()
.values()
.to_vec(),
[b, b + 1]
);
// properties by their Arrow types
assert_eq!(types("a.n").unwrap(), [Type::U64, Type::U64]);
assert_eq!(types("a.t").unwrap(), [Type::U64, Type::I64]);
assert_eq!(types("a.s").unwrap(), [Type::U64, Type::Str]);
assert!(matches!(c.relation("b.d"), Err(PlanError::Input(w)) if w.contains("Decimal128")));
// edges: by table name, by bare name when one type alone has it
assert_eq!(types("a_to_b").unwrap(), [Type::U64, Type::U64]);
assert_eq!(types("to").unwrap(), [Type::U64, Type::U64]);
assert_eq!(c.alias_of("to"), Some("a_to_b"));
assert!(types("link").is_err(), "two types have `link`");
assert!(types("a_link_a").is_ok() && types("b_link_a").is_ok());
assert!(matches!(
c.relation("a_to_b").unwrap().0,
Source::GraphAt { bases, .. } if bases == (space.base("a").unwrap(), b)
));
assert_eq!(types("to.w").unwrap(), [Type::U64, Type::U64, Type::U64]);
assert_eq!(
types("a_to_b.w").unwrap(),
[Type::U64, Type::U64, Type::U64]
);
// no index is the schema's
assert!(c.prop_idx("a", "n").is_none() && c.reach_idx("to").is_none());
}