From 5e1b519955d6f5fa294bcac9fe5ed8c8de12dc0a Mon Sep 17 00:00:00 2001 From: alosslessdev Date: Thu, 5 Nov 2026 05:43:43 -0500 Subject: [PATCH] dax: carry the grouping scope, read integer columns exactly and put IN in the comparison tier (#2228) --- crates/dax/src/ast.rs | 4 ++-- crates/dax/src/eval.rs | 18 ++++++++++++------ crates/dax/src/lib.rs | 1 + crates/dax/src/storage.rs | 9 ++++++++- crates/dax/src/tests.rs | 21 ++++++++++++++------- docs/dax-functions.md | 11 +++++++---- 6 files changed, 44 insertions(+), 20 deletions(-) diff --git a/crates/dax/src/ast.rs b/crates/dax/src/ast.rs index c6ec81ac3..e5cb33daa 100644 --- a/crates/dax/src/ast.rs +++ b/crates/dax/src/ast.rs @@ -218,12 +218,12 @@ impl BinaryOp { /// comparisons, `NOT`, `&&`, `||`. pub const fn precedence(self) -> u8 { match self { - BinaryOp::In => 100, BinaryOp::Pow => 90, BinaryOp::Mul | BinaryOp::Div => 70, BinaryOp::Add | BinaryOp::Sub => 60, BinaryOp::Concat => 50, - BinaryOp::Eq | BinaryOp::StrictEq | BinaryOp::Ne | BinaryOp::Lt | BinaryOp::Gt | BinaryOp::Le | BinaryOp::Ge => 40, + // `IN` is a comparison, at the same tier as `=`/`<` (Microsoft's operator table). + BinaryOp::In | BinaryOp::Eq | BinaryOp::StrictEq | BinaryOp::Ne | BinaryOp::Lt | BinaryOp::Gt | BinaryOp::Le | BinaryOp::Ge => 40, BinaryOp::And => 20, BinaryOp::Or => 10, } diff --git a/crates/dax/src/eval.rs b/crates/dax/src/eval.rs index 4bfd9572f..7bc3ce67b 100644 --- a/crates/dax/src/eval.rs +++ b/crates/dax/src/eval.rs @@ -391,18 +391,18 @@ impl<'a> Evaluator<'a> { } pub fn evaluate_with(&self, expr: &BoundExpr, filters: FilterContext) -> Result { - let mut ctx = Context { filters, rows: Vec::new(), vars: Vec::new() }; + let mut ctx = Context { filters, rows: Vec::new(), vars: Vec::new(), grouping_scope: None }; self.eval(expr, &mut ctx) } pub fn evaluate_table(&self, expr: &BoundExpr) -> Result { - let mut ctx = Context { filters: FilterContext::default(), rows: Vec::new(), vars: Vec::new() }; + let mut ctx = Context { filters: FilterContext::default(), rows: Vec::new(), vars: Vec::new(), grouping_scope: None }; self.eval_table(expr, &mut ctx) } pub fn evaluate_measure(&self, name: &str, filters: FilterContext) -> Result { let index = self.model.measure_index(name).ok_or_else(|| EvalError::new(format!("unknown measure `{name}`")))?; - let mut ctx = Context { filters, rows: Vec::new(), vars: Vec::new() }; + let mut ctx = Context { filters, rows: Vec::new(), vars: Vec::new(), grouping_scope: None }; self.eval_measure(index, &mut ctx) } @@ -469,7 +469,8 @@ impl<'a> Evaluator<'a> { pub(crate) fn eval_measure(&self, index: usize, ctx: &mut Context) -> Result { let expr = self.measures.get(index).ok_or_else(|| EvalError::new(format!("measure index {index} is out of range")))?; let filters = self.transitioned_filters(ctx); - let mut inner = Context { filters, rows: Vec::new(), vars: Vec::new() }; + // A measure keeps the grouping scope it is called from. + let mut inner = Context { filters, rows: Vec::new(), vars: Vec::new(), grouping_scope: ctx.grouping_scope.clone() }; self.eval(expr, &mut inner) } @@ -860,7 +861,7 @@ impl<'a> Evaluator<'a> { self.apply_filter(arg, &mut filters, ctx)?; } } - let mut inner = Context { filters, rows: Vec::new(), vars: ctx.vars.clone() }; + let mut inner = Context { filters, rows: Vec::new(), vars: ctx.vars.clone(), grouping_scope: ctx.grouping_scope.clone() }; return self.eval(&args[0], &mut inner); } "CALCULATETABLE" => return Err(EvalError::new("CALCULATETABLE returns a table and cannot be used as a scalar")), @@ -988,7 +989,7 @@ impl<'a> Evaluator<'a> { self.apply_filter(arg, &mut filters, ctx)?; } } - let mut inner = Context { filters, rows: Vec::new(), vars: ctx.vars.clone() }; + let mut inner = Context { filters, rows: Vec::new(), vars: ctx.vars.clone(), grouping_scope: ctx.grouping_scope.clone() }; self.eval_table(&args[0], &mut inner) } "RELATEDTABLE" => { @@ -1827,6 +1828,11 @@ pub(crate) struct Context { pub(crate) filters: FilterContext, pub(crate) rows: Vec, pub(crate) vars: Vec, + /// The `(table, column)` pairs of the group-by columns still active in this row, or `None` + /// outside any grouping: `SUMMARIZE`/`SUMMARIZECOLUMNS` set it per group, context transition + /// and measure calls carry it along, and a fresh entry point starts with `None`. `ISINSCOPE` + /// and the time-intelligence totals read it. + pub(crate) grouping_scope: Option>, } fn literal_value(literal: &Literal) -> Scalar { diff --git a/crates/dax/src/lib.rs b/crates/dax/src/lib.rs index 7b2478fb1..888304680 100644 --- a/crates/dax/src/lib.rs +++ b/crates/dax/src/lib.rs @@ -60,4 +60,5 @@ pub use validate::{ValidationError, ValidationErrorKind, validate_call, validate pub use value::{EvalError, Scalar, ScalarKey, TableColumn, TableValue}; #[cfg(test)] +#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] mod tests; diff --git a/crates/dax/src/storage.rs b/crates/dax/src/storage.rs index f55be8897..2bf9e60c4 100644 --- a/crates/dax/src/storage.rs +++ b/crates/dax/src/storage.rs @@ -1710,7 +1710,14 @@ fn infer_kind(rows: &[Vec], index: usize) -> ColumnKind { } fn numeric_i64(value: &Scalar) -> i64 { - value.to_number().map_or(0, |number| number as i64) + // The stored variant is read directly: `to_number` would route an `i64` above 2^53 through + // `f64` and collapse distinct keys into one, which a model with large integer keys does not + // survive. + match value { + Scalar::Integer(number) | Scalar::Currency(number) => *number, + Scalar::Decimal(number) | Scalar::DateTime(number) => *number as i64, + other => other.to_number().map_or(0, |number| number as i64), + } } fn numeric_f64(value: &Scalar) -> f64 { diff --git a/crates/dax/src/tests.rs b/crates/dax/src/tests.rs index f6ef221ca..736a51bb4 100644 --- a/crates/dax/src/tests.rs +++ b/crates/dax/src/tests.rs @@ -230,23 +230,30 @@ fn in_operator_with_lists_and_table_expressions() { let (_, _, right) = binary(&expr); assert!(matches!(&right.kind, ExprKind::Call(_))); - // `IN` binds tighter than `^`, comparisons and arithmetic in DAX's table. + // `IN` sits in the comparison tier of Microsoft's operator table, so the arithmetic on its + // left is grouped first and `IN` is the outermost operator. let expr = expression("2 ^ 3 IN { 8 }"); let (op, left, right) = binary(&expr); - assert_eq!(*op, BinaryOp::Pow); - assert!(matches!(&left.kind, ExprKind::Literal(Literal::Integer(n)) if *n == 2)); - assert!(matches!(&right.kind, ExprKind::Binary { op: BinaryOp::In, .. })); + assert_eq!(*op, BinaryOp::In); + assert!(matches!(&left.kind, ExprKind::Binary { op: BinaryOp::Pow, .. })); + assert!(matches!(&right.kind, ExprKind::Table(_))); let expr = expression("1 + 2 IN { 3 }"); let (op, left, right) = binary(&expr); - assert_eq!(*op, BinaryOp::Add); - assert!(matches!(&left.kind, ExprKind::Literal(Literal::Integer(n)) if *n == 1)); - assert!(matches!(&right.kind, ExprKind::Binary { op: BinaryOp::In, .. })); + assert_eq!(*op, BinaryOp::In); + assert!(matches!(&left.kind, ExprKind::Binary { op: BinaryOp::Add, .. })); + assert!(matches!(&right.kind, ExprKind::Table(_))); + // `IN` and `=` share the tier, so they group left to right. let expr = expression("1 IN { 1 } = TRUE"); let (op, left, _) = binary(&expr); assert_eq!(*op, BinaryOp::Eq); assert!(matches!(&left.kind, ExprKind::Binary { op: BinaryOp::In, .. })); + + let expr = expression("1 = 1 IN { TRUE }"); + let (op, left, _) = binary(&expr); + assert_eq!(*op, BinaryOp::In); + assert!(matches!(&left.kind, ExprKind::Binary { op: BinaryOp::Eq, .. })); } #[test] diff --git a/docs/dax-functions.md b/docs/dax-functions.md index f54b1a620..3056bc4b1 100644 --- a/docs/dax-functions.md +++ b/docs/dax-functions.md @@ -33,10 +33,13 @@ The per-category counts below are the catalog's. This crate links no evaluator f ## Evaluated functions -All 484 catalogued functions have an evaluator path. The catalog sections below are the complete -list, grouped by family; each family has an integration suite under `crates/dax/tests/eval_*.rs`, -and `crates/dax/tests/coverage.rs` asserts that every catalogued name binds and reaches an -implementation. +In this crate only the scalar core listed in `crates/dax/README.md` has an evaluator path: the +aggregations and their `*X` iterators, the logical spine, `CALCULATE`/`FILTER`/`ALL*`/`VALUES`/ +`DISTINCT`/`SELECTEDVALUE`, `RELATED*`, `COUNTROWS` and the text/math helpers the core switch +carries. Every other catalogued function reports "not implemented" until its evaluator family is +linked (`eval::FAMILIES` is empty here); each family PR adds its dispatch and its own suite under +`crates/dax/tests/`. The lists below are therefore the **catalog**, not a claim that this build +evaluates them. ## Catalogued (parsed and validated)