diff --git a/crates/dax/README.md b/crates/dax/README.md index b896914bd..9db3384f4 100644 --- a/crates/dax/README.md +++ b/crates/dax/README.md @@ -217,9 +217,10 @@ operator was written. `IN` is a comparison at the same level as `=`, so `1 + 2 I `(1 + 2) IN { 3 }`, and it takes a list (`expr IN { v1, v2 }`) or a table expression (`expr IN VALUES(T[Col])`). -Excel evaluates `2^3^2` as `512` (`2^(3^2)`), so `^` is right-associative even though the generic -DAX Guide note says operators of equal precedence go left to right. Unary minus binds looser than -`^`: `-2^2` parses as `-(2^2)`. +Microsoft's operator reference states that operators of equal precedence are ordered from left to +right, and `^` shares no tier with another operator, so a chain groups left: `2^3^2` is `(2^3)^2` = +64. (Excel's worksheet engine answers 512 for the same text; this crate follows DAX.) Unary minus +binds looser than `^`: `-2^2` parses as `-(2^2)`. ## Data types and static inference diff --git a/crates/dax/src/ast.rs b/crates/dax/src/ast.rs index fa9377eb2..7c7bd9ae4 100644 --- a/crates/dax/src/ast.rs +++ b/crates/dax/src/ast.rs @@ -229,14 +229,16 @@ impl BinaryOp { } } - /// DAX binary operators associate to the left, except `^`, which is right-associative - /// (verified against Excel: `2^3^2` is 512). + /// DAX binary operators all associate to the left. Microsoft's operator reference puts `^` in + /// its own precedence tier and states that operators of equal precedence are ordered from left + /// to right, so `2^3^2` is `(2^3)^2` = 64. (Excel's worksheet engine groups the chain the other + /// way and answers 512; this crate implements DAX.) pub const fn is_left_associative(self) -> bool { - !matches!(self, BinaryOp::Pow) + true } pub const fn is_right_associative(self) -> bool { - matches!(self, BinaryOp::Pow) + false } pub const fn is_comparison(self) -> bool { diff --git a/crates/dax/src/parser.rs b/crates/dax/src/parser.rs index 3b386aede..9947b8cc1 100644 --- a/crates/dax/src/parser.rs +++ b/crates/dax/src/parser.rs @@ -2,8 +2,8 @@ //! //! Binding powers encode the operator table (tighter binds first): parentheses and function //! calls; `^`; unary `+`/`-`; `*`/`/`; binary `+`/`-`; `&`; comparisons, which include `IN`; `NOT`; -//! `&&`; `||`. All operators are left-associative except `^`, which Excel evaluates -//! right-associatively (`2^3^2` is 512). +//! `&&`; `||`. Every operator is left-associative, including `^`: Microsoft's operator reference +//! orders equal-precedence operators from left to right, so `2^3^2` is `(2^3)^2`. use crate::ast::{ Axis, AxisGroup, AxisKind, AxisOrderItem, BinaryOp, Call, ColumnDef, Define, Definition, Evaluate, Expr, ExprKind, FunctionDef, FunctionParam, @@ -678,7 +678,7 @@ impl Parser { break; } self.next(); - // `^` is right-associative; every other binary operator is left-associative. + // Every binary operator is left-associative (DAX orders equal precedence left to right). let next_min = if op.is_right_associative() { bp } else { bp + 1 }; let rhs = self.expr(next_min)?; // Grouped expressions return their inner tree without a parenthesis node, so the diff --git a/crates/dax/src/tests.rs b/crates/dax/src/tests.rs index 736a51bb4..2876603bc 100644 --- a/crates/dax/src/tests.rs +++ b/crates/dax/src/tests.rs @@ -78,8 +78,8 @@ fn arithmetic_precedence_parentheses_and_associativity() { ("2 * 3 ^ 4", "2 * 3 ^ 4"), ("2 ^ 3 + 4", "2 ^ 3 + 4"), ("2 ^ 3 ^ 2", "2 ^ 3 ^ 2"), - ("2 ^ (3 ^ 2)", "2 ^ 3 ^ 2"), - ("(2 ^ 3) ^ 2", "(2 ^ 3) ^ 2"), + ("(2 ^ 3) ^ 2", "2 ^ 3 ^ 2"), + ("2 ^ (3 ^ 2)", "2 ^ (3 ^ 2)"), ("-2 ^ 2", "-2 ^ 2"), ("(-2) ^ 2", "(-2) ^ 2"), ("2 ^ -3", "2 ^ (-3)"), @@ -107,18 +107,19 @@ fn arithmetic_precedence_parentheses_and_associativity() { } #[test] -fn binary_operators_associate_left_except_exponentiation() { +fn binary_operators_associate_left() { let expr = expression("1 - 2 - 3"); let (op, left, right) = binary(&expr); assert_eq!(*op, BinaryOp::Sub); assert!(matches!(&left.kind, ExprKind::Binary { op: BinaryOp::Sub, .. })); assert!(matches!(&right.kind, ExprKind::Literal(Literal::Integer(n)) if *n == 3)); + // DAX orders equal precedence left to right, so a `^` chain groups left: `(2^3)^2`. let expr = expression("2 ^ 3 ^ 2"); 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::Pow, .. })); + assert!(matches!(&left.kind, ExprKind::Binary { op: BinaryOp::Pow, .. })); + assert!(matches!(&right.kind, ExprKind::Literal(Literal::Integer(n)) if *n == 2)); let expr = expression("1 / 2 / 3"); let (op, left, _) = binary(&expr);