From a8e8bd70c6697460f55dd36b2bcb573de24f02c7 Mon Sep 17 00:00:00 2001 From: alosslessdev Date: Thu, 5 Nov 2026 08:22:37 -0500 Subject: [PATCH] dax: normalize numeric relationship joins, scope keyword shadowing and tidy the pivot docs (#2228) --- crates/dax/Cargo.toml | 6 ++++++ crates/dax/src/binder.rs | 15 +++++++++------ crates/dax/src/pivot_table.rs | 10 ++++++---- crates/dax/src/storage.rs | 15 +++++++++++++-- crates/dax/src/value.rs | 10 ++++++++++ crates/dax/tests/pivot_related_star.rs | 5 +++-- docs/dax-functions.md | 6 ++++-- 7 files changed, 51 insertions(+), 16 deletions(-) diff --git a/crates/dax/Cargo.toml b/crates/dax/Cargo.toml index 929f2b212..f5738e2e1 100644 --- a/crates/dax/Cargo.toml +++ b/crates/dax/Cargo.toml @@ -25,5 +25,11 @@ chrono = { workspace = true } # library clock traps there, and `eval/support.rs` would otherwise freeze at 1970. js-sys = "0.3" +[[example]] +name = "parse" +# Cargo does not test examples by default; this one carries assertions on the query-to-expression +# fallback, so it has to run under `cargo test`. +test = true + [dev-dependencies] serde_json = { workspace = true } diff --git a/crates/dax/src/binder.rs b/crates/dax/src/binder.rs index adf35ae4b..ab1ef86a5 100644 --- a/crates/dax/src/binder.rs +++ b/crates/dax/src/binder.rs @@ -312,14 +312,17 @@ impl<'a> Binder<'a> { _ if matches!(expected, Some(ParamKind::Text)) => keyword_name(arg), _ => visual_keyword_name(spec.name, index, arg), } - // A real table or measure name always wins: `RANK(Sales, ORDERBY(...))` uses the - // table in a position whose documented overload would otherwise be `Ties`. A variable - // only shadows a documented text argument (`RANKX(..., Dense)`), because an interval - // or enum keyword is not a name position: a variable called `YEAR` must not turn - // `DATEDIFF(a, b, YEAR)` into a variable reference. + // A name only wins where the position genuinely accepts one — a documented `Text` + // argument such as `RANKX(..., Dense)` or a visual calculation's axis. A fixed enum + // slot (`DATEDIFF`'s interval, `CONVERT`'s type, the order words) is not a name + // position: a table called `Month` must not turn `DATEDIFF(a, b, MONTH)` into a table + // reference, and a variable called `YEAR` must not either. .filter(|keyword| { + if !matches!(expected, Some(ParamKind::Text)) && !is_visual_keyword(spec.name, index, keyword) { + return true; + } let named = self.model.table_index(keyword).is_some() || self.model.measure_index(keyword).is_some(); - let variable = matches!(expected, Some(ParamKind::Text)) && vars.iter().any(|var| var.eq_ignore_ascii_case(keyword)); + let variable = vars.iter().any(|var| var.eq_ignore_ascii_case(keyword)); !named && !variable }); if let Some(keyword) = keyword { diff --git a/crates/dax/src/pivot_table.rs b/crates/dax/src/pivot_table.rs index 44849bb92..5838ee40f 100644 --- a/crates/dax/src/pivot_table.rs +++ b/crates/dax/src/pivot_table.rs @@ -33,8 +33,9 @@ //! * A source row's related value is the related row's column, found through the storage's //! [`RelationshipIndex`](crate::storage::RelationshipIndex) key indexes (or through the model //! rows when the model is not faithfully materialized). A key with no related row groups as -//! BLANK. A key duplicated on the one side resolves to the **lowest row id**, so a source row -//! still reaches exactly one related row and its measure value is counted once. +//! BLANK. A key duplicated on the one side resolves to the **lowest row id** for the group +//! label; the cell filter still passes every related key, so the measure follows the key set +//! (see the duplicate-key note below). //! * The path must be unique. Two active relationships between the same pair of tables, or a //! second chain to the same table (what a cycle in the relationship graph offers), are rejected //! ([`PivotTableError::AmbiguousFieldPath`]) because a pivot field names a table and a column, @@ -1002,8 +1003,9 @@ fn storage_mirrors_model(storage: &ModelStorage, model: &DataModel) -> bool { /// relationship hops through the substrate's key indexes. /// /// A hop whose key has no related row (an orphan key) yields BLANK, like `RELATED`. A key -/// duplicated on the one side resolves to the lowest row id, so a source row reaches at most one -/// related row and its measure value is never counted twice. +/// duplicated on the one side resolves to the lowest row id for the label only: the cell's filter +/// passes every related key, so a duplicated one-side key can put a source row in more than one +/// leaf cell (`integrity` reports it as a duplicate key). fn column_field_value(storage: &ModelStorage, table: usize, row: usize, field: &BoundField) -> Scalar { let mut current_table = table; let mut current_row = row; diff --git a/crates/dax/src/storage.rs b/crates/dax/src/storage.rs index 2bf9e60c4..35af2aedd 100644 --- a/crates/dax/src/storage.rs +++ b/crates/dax/src/storage.rs @@ -1394,8 +1394,16 @@ pub struct RelationshipIndex { impl RelationshipIndex { /// Row ids in the `from` table whose key equals `key`; empty when there is no match. + /// + /// Keys are normalized the same way the index is built (`value::relationship_key_of`), so a + /// caller can probe with the plain `ScalarKey` of a value and still join an `Integer` key to a + /// `Decimal` or `Currency` one. pub fn rows_from(&self, key: &ScalarKey) -> &[usize] { - match self.from_rows.get(key) { + if let Some(rows) = self.from_rows.get(key) { + return rows; + } + // A numeric key still joins its other variants: `Integer (2)` finds a `Decimal (2.0)` row. + match self.from_rows.get(&crate::value::relationship_key_of(key)) { Some(rows) => rows.as_slice(), None => &[], } @@ -1403,7 +1411,10 @@ impl RelationshipIndex { /// Row ids in the `to` table whose key equals `key`; empty when there is no match. pub fn rows_to(&self, key: &ScalarKey) -> &[usize] { - match self.to_rows.get(key) { + if let Some(rows) = self.to_rows.get(key) { + return rows; + } + match self.to_rows.get(&crate::value::relationship_key_of(key)) { Some(rows) => rows.as_slice(), None => &[], } diff --git a/crates/dax/src/value.rs b/crates/dax/src/value.rs index e784fa677..4d644171c 100644 --- a/crates/dax/src/value.rs +++ b/crates/dax/src/value.rs @@ -248,6 +248,16 @@ impl From<&Scalar> for ScalarKey { } } +/// [`relationship_key`] for a key that is already normalized-except-for-variant. +pub(crate) fn relationship_key_of(key: &ScalarKey) -> ScalarKey { + match key { + ScalarKey::Integer(number) if number.unsigned_abs() <= (1 << 53) => ScalarKey::Decimal(order_bits(*number as f64)), + ScalarKey::Currency(number) if number.unsigned_abs() <= (1 << 53) => ScalarKey::Decimal(order_bits(*number as f64 / 10_000.0)), + ScalarKey::DateTime(serial) => ScalarKey::Decimal(*serial), + other => other.clone(), + } +} + /// The same key with text folded to lower case: the rule filter keys and the values they are /// compared with share, so a filter set from `VALUES` matches every spelling of the value. pub(crate) fn folded_key(key: &ScalarKey) -> ScalarKey { diff --git a/crates/dax/tests/pivot_related_star.rs b/crates/dax/tests/pivot_related_star.rs index 6eef75963..df7ca9d65 100644 --- a/crates/dax/tests/pivot_related_star.rs +++ b/crates/dax/tests/pivot_related_star.rs @@ -209,8 +209,9 @@ fn same_table_fields_are_unchanged_by_the_relationships() { } /// A model whose dimension repeats a key. The one side of a relationship must be unique; when it is -/// not, the pivot resolves each fact row to the **lowest** matching dimension row, so no fact row is -/// fanned out and no value is counted twice. +/// not, the pivot resolves each fact row to the **lowest** matching dimension row for its label, +/// while the cell filter passes every related key, so a fact row can land in more than one leaf +/// cell (`integrity` reports the duplicate key). fn duplicate_key_model() -> DataModel { let mut model = DataModel { tables: vec![ diff --git a/docs/dax-functions.md b/docs/dax-functions.md index f15fe9a46..e682f1f73 100644 --- a/docs/dax-functions.md +++ b/docs/dax-functions.md @@ -108,8 +108,10 @@ evaluates them. - `Evaluated` means a dispatch arm exists in one of the family modules under `crates/dax/src/eval/`; table-valued functions are counted when they have a `call_table` path. - Engine-only functions (internal filter clusters, `INFO.*` storage views, visual calculations) - use bounded equivalents of their documented behaviour, described in the module that implements - them; `MS Learn` and dax.guide are the semantic references for everything else. + have no evaluator in this crate: like every other catalogued name outside the scalar core they + report `not implemented` until the family that owns them is linked. Where a family is linked it + documents any bounded equivalent in its own module; `MS Learn` and dax.guide are the semantic + references throughout. - The catalog is the dax.guide aggregation/data-type/statement/operator surface plus every function category in `crates/dax/src/functions/`; see `docs/parity-checklist.md` for the GridCraft command surface.