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

nordisk/kleene

crates/kleene-datalog/tests/entities.rs

//! Whole values from the Datalog side ([`Engine::read_entities`]): the labels and every
//! property of a few nodes, and the edges and their properties between a few pairs, read
//! for those ids alone; a property a node lacks is null, an edge property of two edges
//! between one pair the least. Against the columns given.

use std::collections::BTreeSet;
use std::sync::Arc;

use arrow_array::{ArrayRef, StringArray, UInt64Array};
use kleene_datalog::{Datum, EdgeAsk, Engine, NodeAsk, Source, Type};
use kleene_sparse::{CpuBackend, CpuDevice};

fn u(v: &[u64]) -> ArrayRef {
    Arc::new(UInt64Array::from(v.to_vec()))
}

#[tokio::test]
async fn nodes_and_edges_whole_for_the_ids_asked() {
    // people 0..6; a name for 0..5 (5 has none), an age for the even ones
    let people = Source::Columns(vec![u(&[0, 1, 2, 3, 4, 5])]);
    let names: Vec<String> = (0..5).map(|i| format!("p{i}")).collect();
    let name = Source::Columns(vec![
        u(&[0, 1, 2, 3, 4]),
        Arc::new(StringArray::from(names.clone())),
    ]);
    let age = Source::Columns(vec![u(&[0, 2, 4]), u(&[30, 32, 34])]);
    // pets 10, 11 (a second label over other ids)
    let pets = Source::Columns(vec![u(&[10, 11])]);
    let ask = NodeAsk {
        name: "person".into(),
        members: Some(people),
        props: vec![
            ("name".into(), name, Type::Str),
            ("age".into(), age, Type::U64),
        ],
        ids: BTreeSet::from([1, 2, 5, 10]),
    };
    let pet = NodeAsk {
        name: "pet".into(),
        members: Some(pets),
        props: vec![],
        ids: BTreeSet::from([2, 10]),
    };
    // knows: two edges 1 -> 2 (since 2019 and 2011), one 2 -> 5 (since 2020)
    let knows = EdgeAsk {
        name: "knows".into(),
        source: Source::Columns(vec![u(&[1, 1, 2, 5]), u(&[2, 2, 5, 1])]),
        props: vec![(
            "since".into(),
            Source::Columns(vec![u(&[1, 1, 2]), u(&[2, 2, 5]), u(&[2019, 2011, 2020])]),
            Type::U64,
        )],
        pairs: BTreeSet::from([(1, 2), (2, 5), (2, 1)]),
    };
    let e = Engine::<CpuBackend>::new(CpuDevice::new(2));
    let got = e.read_entities(&[ask, pet], &[knows]).await.unwrap();
    let member: BTreeSet<(usize, u64)> = got.member.iter().copied().collect();
    assert_eq!(member, BTreeSet::from([(0, 1), (0, 2), (0, 5), (1, 10)]));
    assert_eq!(got.value(0, 0, 1), Datum::Str("p1".into()));
    assert_eq!(got.value(0, 0, 5), Datum::Null, "no name");
    assert_eq!(got.value(0, 1, 2), Datum::U64(32));
    assert_eq!(got.value(0, 1, 1), Datum::Null, "no age");
    // only the ids asked are read
    assert!(got.values.keys().all(|k| [1, 2, 5, 10].contains(&k.2)));
    let on: BTreeSet<(usize, u64, u64)> = got.on.iter().copied().collect();
    assert_eq!(on, BTreeSet::from([(0, 1, 2), (0, 2, 5)]));
    assert_eq!(
        got.edge_value(0, 0, 1, 2),
        Datum::U64(2011),
        "the least of two"
    );
    assert_eq!(got.edge_value(0, 0, 2, 5), Datum::U64(2020));
    assert_eq!(got.edge_value(0, 0, 2, 1), Datum::Null);
}