crates/kleene-iceberg/tests/kept.rs
//! What is built per (table, snapshot id) is kept: asking again reads no data file, in the
//! same set (memory) and in another set on the same store (kleene-index's `Store`, keyed by
//! the snapshot id); a table at a new snapshot is read again, the others not.
use std::sync::Arc;
use arrow_array::{ArrayRef, Int64Array, RecordBatch};
use arrow_schema::{Field, Schema};
use kleene_iceberg::{IcebergLake, IcebergSet, MappingSpec, Version};
use kleene_plan::Source;
const MAPPING: &str = r#"(version: 1, tables: [
(table: "g.node", pipeline: [(vertex: (label: "node", id_column: "id", properties: ["w", "tag", "f", "ok"]))]),
(table: "g.link", pipeline: [
(edge: (type: "link", src: (label: "node", column: "a"), dst: (label: "node", column: "b"))),
]),
])"#;
fn batch(cols: Vec<(&str, Vec<i64>)>) -> RecordBatch {
let fields: Vec<Field> = cols
.iter()
.map(|(n, _)| Field::new(*n, arrow_schema::DataType::Int64, false))
.collect();
let arrays: Vec<ArrayRef> = cols
.into_iter()
.map(|(_, v)| Arc::new(Int64Array::from(v)) as ArrayRef)
.collect();
RecordBatch::try_new(Arc::new(Schema::new(fields)), arrays).unwrap()
}
/// Nodes 0..4 with a weight, a tag (one null), a float and a flag.
fn nodes() -> RecordBatch {
let b = batch(vec![("id", (0..4).collect()), ("w", vec![7, 8, 9, 10])]);
let mut fields: Vec<Field> = b.schema().fields().iter().map(|f| (**f).clone()).collect();
fields.extend([
Field::new("tag", arrow_schema::DataType::Utf8, true),
Field::new("f", arrow_schema::DataType::Float64, false),
Field::new("ok", arrow_schema::DataType::Boolean, false),
]);
let mut cols = b.columns().to_vec();
cols.extend([
Arc::new(arrow_array::StringArray::from(vec![
Some("a"),
None,
Some("c"),
Some("d"),
])) as ArrayRef,
Arc::new(arrow_array::Float64Array::from(vec![0.5, 1.5, -2.0, 3.25])) as ArrayRef,
Arc::new(arrow_array::BooleanArray::from(vec![
true, false, true, true,
])) as ArrayRef,
]);
RecordBatch::try_new(Arc::new(Schema::new(fields)), cols).unwrap()
}
/// Each property column of `node` as the catalog holds it.
fn props(s: &kleene_iceberg::Snapshot) -> Vec<ArrayRef> {
["node.tag", "node.f", "node.ok"]
.iter()
.map(|n| match s.catalog().relation(n).unwrap().0 {
Source::Columns(c) => c[1].clone(),
_ => panic!("columns"),
})
.collect()
}
fn links(s: &kleene_iceberg::Snapshot) -> usize {
match s.catalog().relation("link").unwrap().0 {
Source::Graph { out, .. } => out.nnz(),
_ => panic!("a graph"),
}
}
fn weights(s: &kleene_iceberg::Snapshot) -> Vec<i64> {
let Source::Columns(c) = s.catalog().relation("node.w").unwrap().0 else {
panic!("columns")
};
let a = c[1].as_any().downcast_ref::<Int64Array>().unwrap();
a.values().to_vec()
}
#[tokio::test]
async fn a_table_at_a_snapshot_read_once_is_never_read_again() {
let tmp = tempfile::tempdir().unwrap();
let (lake, keep) = (tmp.path().join("lake"), tmp.path().join("kept"));
let write = kleene_iceberg::write::append;
write(&lake, "g.node", &[nodes()]).await.unwrap();
write(
&lake,
"g.link",
&[batch(vec![("a", vec![0, 1, 2]), ("b", vec![1, 2, 3])])],
)
.await
.unwrap();
let spec = MappingSpec::from_ron(MAPPING).unwrap();
let open = || {
IcebergSet::new(&spec, IcebergLake::Warehouse(lake.clone()))
.unwrap()
.keep_in(&keep)
};
let set = open();
let first = set.snapshot(&Version::new()).await.unwrap();
assert_eq!(set.files_read(), 2);
let again = set.snapshot(&Version::new()).await.unwrap();
assert_eq!(set.files_read(), 2, "kept in memory: no data file read");
assert_eq!((links(&first), links(&again)), (3, 3));
// another set on the same store: the rows come from the store, typed as before
let other = open();
let kept = other.snapshot(&Version::new()).await.unwrap();
assert_eq!(
other.files_read(),
0,
"kept in the store: no data file read"
);
assert_eq!(links(&kept), 3);
assert_eq!(weights(&kept), [7, 8, 9, 10]);
let (a, b) = (props(&first), props(&kept));
for (x, y) in a.iter().zip(&b) {
assert_eq!(x.to_data(), y.to_data(), "{:?}", x.data_type());
}
assert_eq!(b[0].null_count(), 1);
assert_eq!(kept.version(), first.version());
// one more link: only that table is read again (both its files, from the start)
write(
&lake,
"g.link",
&[batch(vec![("a", vec![3]), ("b", vec![0])])],
)
.await
.unwrap();
let now = other.snapshot(&Version::new()).await.unwrap();
assert_eq!(other.files_read(), 2, "the node table not read again");
assert_eq!(links(&now), 4);
// the old snapshot of `link` is still kept in memory
let then = other.snapshot(first.version()).await.unwrap();
assert_eq!((other.files_read(), links(&then)), (2, 3));
}