mirror of
https://github.com/storytold/gridcraft.git
synced 2026-12-01 18:26:05 -06:00
dax: carry the grouping scope, read integer columns exactly and put IN in the comparison tier (#2228)
This commit is contained in:
@@ -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,
|
||||
}
|
||||
|
||||
+12
-6
@@ -391,18 +391,18 @@ impl<'a> Evaluator<'a> {
|
||||
}
|
||||
|
||||
pub fn evaluate_with(&self, expr: &BoundExpr, filters: FilterContext) -> Result<Scalar, EvalError> {
|
||||
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<TableValue, EvalError> {
|
||||
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<Scalar, EvalError> {
|
||||
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<Scalar, EvalError> {
|
||||
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<RowFrame>,
|
||||
pub(crate) vars: Vec<Scalar>,
|
||||
/// 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<Vec<(usize, usize)>>,
|
||||
}
|
||||
|
||||
fn literal_value(literal: &Literal) -> Scalar {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -1710,7 +1710,14 @@ fn infer_kind(rows: &[Vec<Scalar>], 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 {
|
||||
|
||||
+14
-7
@@ -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]
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user