dax: make exponentiation left-associative like every DAX operator (#2228)

This commit is contained in:
alosslessdev
2026-11-05 12:14:28 -05:00
parent 68389e865b
commit 581b085e9b
4 changed files with 19 additions and 15 deletions
+4 -3
View File
@@ -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
+6 -4
View File
@@ -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 {
+3 -3
View File
@@ -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
+6 -5
View File
@@ -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);