mirror of
https://github.com/gorules/zen.git
synced 2026-11-29 00:07:37 +00:00
fix(engine): typed index-then-field reads and struct-column policy references (#582)
* fix(engine): typed index-then-field reads and struct-column policy references Refuse typed lowering of `list[n].field` when the list carries a map projection, so `map(items, #.code)[0].price` reads null like version 1 instead of the source item's field. Take the shared row path when a column sits above or below a policy reference field, so references inside struct columns are checked against their pool like version 1. * test(engine): build the struct-column reference test from plain string arrays
This commit is contained in:
@@ -72,6 +72,12 @@ impl PolicyPlan {
|
||||
flat && fields.iter().all(|field| free(field) && safe(field))
|
||||
}
|
||||
|
||||
fn nested(columns: &Columns, path: &str) -> bool {
|
||||
columns.columns.iter().any(|(at, _)| {
|
||||
Sheet::related(at, path).is_some_and(|order| order != std::cmp::Ordering::Equal)
|
||||
})
|
||||
}
|
||||
|
||||
fn pool_items<'a>(columns: &Columns<'a>, target: &str) -> Option<Option<Items<'a>>> {
|
||||
let Some(pool) = columns.find(target).map(|at| columns.columns[at].1) else {
|
||||
return Some(None);
|
||||
@@ -243,6 +249,9 @@ impl PolicyPlan {
|
||||
let mut leaves: Vec<(Arc<str>, Leaf<'a>, Option<Rc<[u64]>>)> = Vec::new();
|
||||
let mut lists: Vec<Listed<'a>> = Vec::new();
|
||||
for field in fields {
|
||||
if Self::nested(columns, &field.path) {
|
||||
return refused("reference beside a composite column");
|
||||
}
|
||||
let Some(entry) = columns.find(&field.path) else {
|
||||
continue;
|
||||
};
|
||||
|
||||
@@ -2594,3 +2594,57 @@ fn member_blocks_reading_root_dates_in_any_form_match_version_one() {
|
||||
.collect();
|
||||
columns_match_version_one(&[(PATH, LIFTED_DATES)], rows);
|
||||
}
|
||||
|
||||
const REFERENCED_FEE: &str = r#"{
|
||||
models: {order: {shipTo: "&jurisdiction?", fee: "number"}, jurisdiction: {id: "string", stateRate: "number"}},
|
||||
blocks: [
|
||||
{expression: "order.shipped", value: "order.shipTo != null"},
|
||||
{expression: "order.tax", value: "order.fee * (order.shipTo.stateRate ?? 0)"},
|
||||
],
|
||||
}"#;
|
||||
|
||||
#[test]
|
||||
fn references_inside_a_struct_column_are_checked_like_version_one() {
|
||||
let old = workspace(&[(PATH, REFERENCED_FEE)], EngineVersion::V1);
|
||||
let new = workspace(&[(PATH, REFERENCED_FEE)], EngineVersion::V2);
|
||||
let ship_to = ["unknown", "NY", "", "CA"];
|
||||
let ship_to_valid = [0b1011u64];
|
||||
let fee = [1i64, 2, 3, 4];
|
||||
let ids = ["NY", "CA", "NY", "CA", "NY", "CA", "NY", "CA"];
|
||||
let rates = [4i64, 7, 4, 7, 4, 7, 4, 7];
|
||||
let two_per_row = [0i32, 2, 4, 6, 8];
|
||||
let order_fields = [
|
||||
(
|
||||
"shipTo",
|
||||
Column::with_validity(Values::Strs(&ship_to), &ship_to_valid, 0),
|
||||
),
|
||||
("fee", Column::new(Values::I64(&fee))),
|
||||
];
|
||||
let order = Column::new(Values::Struct {
|
||||
fields: &order_fields,
|
||||
len: 4,
|
||||
});
|
||||
let pool_fields = [
|
||||
("id", Column::new(Values::Strs(&ids))),
|
||||
("stateRate", Column::new(Values::I64(&rates))),
|
||||
];
|
||||
let pool_item = Column::new(Values::Struct {
|
||||
fields: &pool_fields,
|
||||
len: ids.len(),
|
||||
});
|
||||
let pool = Column::new(Values::List {
|
||||
offsets: &two_per_row,
|
||||
child: Dictionary::Column(&pool_item),
|
||||
});
|
||||
let structured = Columns::new(4)
|
||||
.column("order", order)
|
||||
.column("jurisdiction", pool);
|
||||
given_columns_match(&old, &new, &structured);
|
||||
|
||||
let ship_to_ids = ["unknown", "NY", "CA", "NY"];
|
||||
let nested = Columns::new(4)
|
||||
.column("order.shipTo.id", Column::new(Values::Strs(&ship_to_ids)))
|
||||
.column("order.fee", Column::new(Values::I64(&fee)))
|
||||
.column("jurisdiction", pool);
|
||||
given_columns_match(&old, &new, &nested);
|
||||
}
|
||||
|
||||
@@ -2315,3 +2315,32 @@ fn membership_in_another_list_of_the_row_matches_version_one() {
|
||||
let paths = columns_match_version_one(expression("count(n, # in m)"), clean);
|
||||
assert!(paths.typed > 0 && paths.laned == 0, "{paths:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_field_of_an_indexed_mapped_list_reads_the_mapped_value() {
|
||||
let rows: Vec<Value> = (0..24)
|
||||
.map(|i| match i % 4 {
|
||||
0 => json!({"items": []}),
|
||||
1 => json!({"items": null}),
|
||||
_ => json!({"items": [
|
||||
{"code": format!("C{}", i % 5), "price": i % 7, "tags": ["a"]},
|
||||
{"code": "Z", "price": 1, "tags": []}
|
||||
]}),
|
||||
})
|
||||
.collect();
|
||||
for (mapped, read) in [
|
||||
("map(items, #.code)", "$.mapped[0].price"),
|
||||
("map(items, #.code)", "$.mapped[1].code"),
|
||||
("map(items, #.price)", "$.mapped[0].price"),
|
||||
("map(items ?? [], #.code)", "$.mapped[0].price"),
|
||||
("filter(map(items, #.code), # != 'Z')", "$.mapped[0].price"),
|
||||
("map(items, #.code)", "$.mapped[0]"),
|
||||
] {
|
||||
let node = json!({"id": "node", "name": "node", "type": "expressionNode", "content": {"expressions": [
|
||||
{"id": "x0", "key": "mapped", "value": mapped},
|
||||
{"id": "x1", "key": "read", "value": read},
|
||||
{"id": "x2", "key": "total", "value": "sum(map(items ?? [], #.price))"}
|
||||
]}});
|
||||
columns_match_version_one(graph(node), rows.clone());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -501,8 +501,8 @@ impl Lowering {
|
||||
};
|
||||
let mark = self.mark();
|
||||
let seq = match self.seq(list, ctx, r) {
|
||||
Ok(seq) => Self::pure(seq)?,
|
||||
Err(_) => {
|
||||
Ok(seq) if path.is_empty() || seq.value.is_none() => Self::pure(seq)?,
|
||||
_ => {
|
||||
self.rollback(mark);
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user