diff --git a/crates/dax/src/eval.rs b/crates/dax/src/eval.rs index d3117bad2..823321cfc 100644 --- a/crates/dax/src/eval.rs +++ b/crates/dax/src/eval.rs @@ -1547,7 +1547,8 @@ impl<'a> Evaluator<'a> { BoundExpr::Binary { op: BinaryOp::In, left, right } => { // `CALCULATE ( expr, Table[Column] IN { ... } )`: the list (or table expression) // is the membership set, and the filter keeps the rows whose column value is a - // member — the same per-row truth the other comparison operators apply. + // member — the same per-row truth `IN` and the other comparison operators apply, + // so a number matches across variants and text ignores case. let BoundExpr::Column { table, column } = &**left else { return Err(EvalError::new("CALCULATE filter must compare a column")); }; @@ -1559,7 +1560,7 @@ impl<'a> Evaluator<'a> { .table(*table, &FilterContext::default()) .column(*column) .into_iter() - .filter(|candidate| members.rows.iter().any(|row| row.first().is_some_and(|member| member.strict_eq(candidate)))) + .filter(|candidate| members.rows.iter().any(|row| row.first().is_some_and(|member| in_equals(member, candidate)))) .map(|candidate| ScalarKey::from(&candidate)) .collect(); filters.set(*table, *column, keys); @@ -1730,6 +1731,11 @@ impl<'a> Evaluator<'a> { )); } }; + if relationship.inactive && flow != RelationshipFlow::NONE { + // `CROSSFILTER` only redirects filtering between tables that already filter; turning an + // inactive relationship on is `USERELATIONSHIP`'s job. + return Err(EvalError::new(format!("{name} needs an active relationship; use USERELATIONSHIP to activate it"))); + } filters.relationships.insert(index, flow); Ok(()) } @@ -2005,7 +2011,12 @@ fn binary(op: BinaryOp, left: Scalar, right: Scalar) -> Result` answers TRUE + // while `=`, `==` and the ordering operators answer FALSE. Reporting FALSE for + // both would make two contradictory comparisons agree. + return Ok(Scalar::Boolean(op == BinaryOp::Ne)); + }; let result = match op { BinaryOp::Eq | BinaryOp::StrictEq => ordering == Ordering::Equal, BinaryOp::Ne => ordering != Ordering::Equal, diff --git a/crates/dax/src/functions/relationships.rs b/crates/dax/src/functions/relationships.rs index 0c13fd00f..9014157e1 100644 --- a/crates/dax/src/functions/relationships.rs +++ b/crates/dax/src/functions/relationships.rs @@ -37,15 +37,16 @@ pub static FUNCTIONS: &[FunctionSpec] = &[ RELATIONSHIP_FLAGS, "Overrides the cross-filter direction of an existing relationship for the duration of \ the filter context it modifies. The two column arguments identify the relationship \ - endpoints; the third argument is 0/1/2/3/4 or the words NONE, ONE, ONEWAY, BOTH, \ - TWOWAY, ONEWAY_LEFTFILTERSRIGHT or ONEWAY_RIGHTFILTERSLEFT (the last two flow from the \ - first column argument's table to the second's, or back). It changes filter propagation \ - in both directions, never which relationship is active, so it is used as a filter \ - modifier inside CALCULATE and similar functions and returns no value.", + endpoints; the third argument is one of the words NONE, ONE, ONEWAY, BOTH, TWOWAY, \ + ONEWAY_LEFTFILTERSRIGHT or ONEWAY_RIGHTFILTERSLEFT (the last two flow from the first \ + column argument's table to the second's, or back). It changes filter propagation between \ + the two tables, never which relationship is active — activating an inactive relationship \ + is USERELATIONSHIP's job — so it is used as a filter modifier inside CALCULATE and \ + similar functions and returns no value.", &[ "CALCULATE ( [Sales Amount], CROSSFILTER ( Sales[CustomerKey], Customer[CustomerKey], BOTH ) )", "CALCULATE ( COUNTROWS ( Customer ), CROSSFILTER ( Sales[ProductKey], 'Product'[ProductKey], NONE ) )", - "CALCULATE ( [Sales Amount], CROSSFILTER ( Sales[CustomerKey], Customer[CustomerKey], 1 ) )", + "CALCULATE ( [Sales Amount], CROSSFILTER ( Sales[CustomerKey], Customer[CustomerKey], TWOWAY ) )", ], ), spec( diff --git a/crates/dax/tests/eval_audit_semantics.rs b/crates/dax/tests/eval_audit_semantics.rs index da32212d5..9ee74bb3b 100644 --- a/crates/dax/tests/eval_audit_semantics.rs +++ b/crates/dax/tests/eval_audit_semantics.rs @@ -135,6 +135,8 @@ fn calculate_filter_accepts_an_in_list() { eval_in_sales(r#"CALCULATE(SUM(Sales[Amount]), Sales[Region] IN FILTER(VALUES(Sales[Region]), Sales[Region] <> "East"))"#), Scalar::Decimal(110.0) ); + // The filter form follows DAX equality too: the Decimal column matches an Integer literal. + assert_eq!(eval_in_sales(r#"CALCULATE(SUM(Sales[Amount]), Sales[Amount] IN { 10 })"#), Scalar::Decimal(10.0)); // The scalar IN operator keeps working. assert_eq!(eval("1 IN { 1, 2 }"), Scalar::Boolean(true)); assert_eq!(eval("3 IN { 1, 2 }"), Scalar::Boolean(false)); diff --git a/crates/dax/tests/evaluator.rs b/crates/dax/tests/evaluator.rs index d99fb5f36..64de980a6 100644 --- a/crates/dax/tests/evaluator.rs +++ b/crates/dax/tests/evaluator.rs @@ -429,6 +429,11 @@ fn userelationship_selects_the_matching_relationship_by_column() { // USERELATIONSHIP picks ShipDate even though OrderDate is the first relationship in the model. assert_eq!(evaluate("CALCULATE([Total], USERELATIONSHIP(Sales[ShipDate], Date[Date]), Date[Date] = DATE(2024,1,2))"), Scalar::Decimal(10.0)); assert_eq!(evaluate("CALCULATE([Total], USERELATIONSHIP(Sales[ShipDate], Date[Date]), Date[Date] = DATE(2024,1,1))"), Scalar::Decimal(20.0)); + // CROSSFILTER cannot activate ShipDate: only USERELATIONSHIP may do that. + let crossfilter = parse_expression("CALCULATE([Total], CROSSFILTER(Sales[ShipDate], Date[Date], BOTH))").expect("parse"); + let bound = Binder::new(&model).bind_expression(&crossfilter).expect("bind"); + let error = evaluator.evaluate(&bound).expect_err("CROSSFILTER must not activate an inactive relationship"); + assert!(error.message.contains("USERELATIONSHIP"), "{error}"); // CROSSFILTER accepts both documented spellings and leaves the active relationship in place. assert_eq!(evaluate("CALCULATE([Total], CROSSFILTER(Sales[OrderDate], Date[Date], OneWay))"), Scalar::Decimal(30.0)); assert_eq!(evaluate("CALCULATE([Total], CROSSFILTER(Sales[OrderDate], Date[Date], BOTH))"), Scalar::Decimal(30.0)); @@ -544,6 +549,10 @@ fn pivot_cross_puts_column_groups_in_columns() { fn text_is_not_compared_with_numbers() { // DAX refuses to compare text with a number, so `=` answers FALSE instead of coercing "3". assert_eq!(eval("\"3\" = 3"), Scalar::Boolean(false)); + // The negation of an incomparable pair is TRUE: two contradictory comparisons cannot both be + // FALSE, and the evaluator maps "no ordering" to inequality. + assert_eq!(eval("\"3\" <> 3"), Scalar::Boolean(true)); + assert_eq!(eval("\"3\" < 3"), Scalar::Boolean(false)); assert_eq!(eval("\"3\" = \"3\""), Scalar::Boolean(true)); // `IN` is DAX equality rather than representation equality: numbers compare by value across // variants and text ignores case, while BLANK still matches only BLANK. diff --git a/crates/dax/tests/functions_relationships.rs b/crates/dax/tests/functions_relationships.rs index 21c4bf447..a4472574c 100644 --- a/crates/dax/tests/functions_relationships.rs +++ b/crates/dax/tests/functions_relationships.rs @@ -250,7 +250,8 @@ fn relatedtable_requires_a_table_expression() { #[test] fn crossfilter_accepts_every_documented_direction_argument() { - for direction in ["NONE", "ONEWAY", "BOTH", "ONEWAY_RIGHTFILTERSLEFT", "ONEWAY_LEFTFILTERSRIGHT", "0", "1", "2", "3", "4"] { + // Directions are the documented words; a numeric argument is not one of them. + for direction in ["NONE", "ONE", "ONEWAY", "BOTH", "TWOWAY", "ONEWAY_RIGHTFILTERSLEFT", "ONEWAY_LEFTFILTERSRIGHT"] { let src = format!("CROSSFILTER ( Sales[CustomerKey], Customer[CustomerKey], {direction} )"); assert_valid(&src); }