mirror of
https://github.com/gorules/zen.git
synced 2026-12-04 00:06:41 +00:00
fix: path-independent policy columnar column order
This commit is contained in:
@@ -51,6 +51,7 @@ pub(super) struct Sheet<'a> {
|
||||
pub(super) inputs: Vec<Column<'a>>,
|
||||
pub(super) entities: Vec<Entity<'a>>,
|
||||
pub(super) refs: Vec<Arc<str>>,
|
||||
order: Vec<usize>,
|
||||
}
|
||||
|
||||
impl<'a> Sheet<'a> {
|
||||
@@ -77,6 +78,7 @@ impl<'a> Sheet<'a> {
|
||||
}
|
||||
Some(Self {
|
||||
rows,
|
||||
order: (0..entries.len()).collect(),
|
||||
entries,
|
||||
inputs,
|
||||
entities: Vec::new(),
|
||||
@@ -167,7 +169,8 @@ impl<'a> Sheet<'a> {
|
||||
|
||||
pub(super) fn object(&self, row: usize) -> Variable {
|
||||
let object = Variable::empty_object();
|
||||
for (index, entry) in self.entries.iter().enumerate() {
|
||||
for &index in &self.order {
|
||||
let entry = &self.entries[index];
|
||||
if entry.dead {
|
||||
continue;
|
||||
}
|
||||
@@ -233,24 +236,39 @@ impl<'a> Sheet<'a> {
|
||||
}
|
||||
_ => (leaf, present),
|
||||
};
|
||||
self.entries.push(Entry {
|
||||
path,
|
||||
leaf,
|
||||
present,
|
||||
input: false,
|
||||
dead: false,
|
||||
});
|
||||
self.push(
|
||||
Entry {
|
||||
path,
|
||||
leaf,
|
||||
present,
|
||||
input: false,
|
||||
dead: false,
|
||||
},
|
||||
previous.filter(|&at| self.entries[at].dead),
|
||||
);
|
||||
}
|
||||
|
||||
fn push(&mut self, entry: Entry<'a>, replaces: Option<usize>) {
|
||||
let index = self.entries.len();
|
||||
self.entries.push(entry);
|
||||
match replaces.and_then(|at| self.order.iter().position(|&slot| slot == at)) {
|
||||
Some(slot) => self.order[slot] = index,
|
||||
None => self.order.push(index),
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn hosted(&mut self, path: Arc<str>, values: Vec<Variable>, present: Vec<u64>) {
|
||||
let full = present.iter().map(|w| w.count_ones() as usize).sum::<usize>() == self.rows;
|
||||
self.entries.push(Entry {
|
||||
path,
|
||||
leaf: Leaf::Any(values.into()),
|
||||
present: (!full).then(|| present.into()),
|
||||
input: false,
|
||||
dead: false,
|
||||
});
|
||||
self.push(
|
||||
Entry {
|
||||
path,
|
||||
leaf: Leaf::Any(values.into()),
|
||||
present: (!full).then(|| present.into()),
|
||||
input: false,
|
||||
dead: false,
|
||||
},
|
||||
None,
|
||||
);
|
||||
}
|
||||
|
||||
pub(super) fn columns(leaves: Vec<Present<'a>>) -> Vec<(Arc<str>, OutputColumn<'a>)> {
|
||||
@@ -276,9 +294,9 @@ impl<'a> Sheet<'a> {
|
||||
});
|
||||
match nested {
|
||||
false => self
|
||||
.entries
|
||||
.order
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|&index| (index, &self.entries[index]))
|
||||
.filter(|(_, entry)| !entry.dead)
|
||||
.map(|(index, entry)| {
|
||||
if let Some(entity) = self.active(index) {
|
||||
|
||||
@@ -649,6 +649,40 @@ fn policy_columnar_shredded_irregular_lists() {
|
||||
assert!(failures.is_empty(), "{} mismatches:\n{}", failures.len(), failures.join("\n"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn policy_column_order_is_path_independent() {
|
||||
let cases = [
|
||||
(
|
||||
r#"{models: {a: {x: "number"}}, blocks: [{expression: "a.ok", value: "a.x - 1"}, {expression: "a.big", value: "a.x * a.x"}]}"#,
|
||||
r#"[{"a": {"x": 5}}, {"a": {"x": 1000000000000000}}]"#,
|
||||
["a.x", "a.big", "a.ok"].as_slice(),
|
||||
),
|
||||
(
|
||||
r#"{models: {a: {x: "number", y: "number"}}, blocks: [{expression: "a.q", value: "a.x / a.y"}, {table: "dt", inputs: ["a.q"], outputs: ["a.band"], rules: [["> 1", "'big'"], ["", "'small'"]]}]}"#,
|
||||
r#"[{"a": {"x": 4, "y": 2}}, {"a": {"x": 4, "y": 0}}]"#,
|
||||
["a.x", "a.y", "a.q", "a.band"].as_slice(),
|
||||
),
|
||||
(
|
||||
r#"{models: {a: {x: "number", y: "number"}}, blocks: [{expression: "a.q", value: "a.x / a.y"}, {match: "a.band", arms: [["a.q > 1", "'big'"], ["", "'small'"]]}]}"#,
|
||||
r#"[{"a": {"x": 4, "y": 2}}, {"a": {"x": 4, "y": 0}}]"#,
|
||||
["a.x", "a.y", "a.q", "a.band"].as_slice(),
|
||||
),
|
||||
];
|
||||
for (policy, rows, order) in cases {
|
||||
let ws = Anchor::workspace_with(policy);
|
||||
let pair: Vec<Value> = serde_json::from_str(rows).expect("rows");
|
||||
let values: Vec<Value> = (0..64).map(|row| pair[row % 2].clone()).collect();
|
||||
let built = Built::new(&values).expect("flat columns");
|
||||
let columns = built.columns();
|
||||
for call in 0..8 {
|
||||
let output = ws.evaluate_columns(&Arc::from("anchor"), &[], &columns).expect("columnar");
|
||||
let got: Vec<&str> = output.columns.iter().map(|(path, _)| path.as_ref()).collect();
|
||||
assert_eq!(got, order, "call {call}: typed {}, laned {}", output.typed, output.laned);
|
||||
assert!((0..output.rows).all(|row| output.errors[row].is_some() == (row % 2 == 1)), "call {call}: errors");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn policy_anchor_is_fully_typed() {
|
||||
if Reference::unavailable("anchor") {
|
||||
|
||||
Reference in New Issue
Block a user