diff --git a/crates/dax/src/eval/support.rs b/crates/dax/src/eval/support.rs index 5595fee48..e97526432 100644 --- a/crates/dax/src/eval/support.rs +++ b/crates/dax/src/eval/support.rs @@ -335,12 +335,17 @@ pub(crate) fn utc_today_serial() -> f64 { fn excel_precision(value: f64) -> f64 { // An exact whole number under 2^53 has no binary noise to remove, and snapping it would drop // digits: `ROUND ( 1234567890123457, 0 )` must stay that number. - if value.fract() == 0.0 && value.abs() < 2f64.powi(53) { + if is_exact_whole(value) { return value; } format!("{value:.14e}").parse().unwrap_or(value) } +/// Whether `value` is a whole number held exactly in an `f64`. +fn is_exact_whole(value: f64) -> bool { + value.fract() == 0.0 && value.abs() < 2f64.powi(53) +} + pub(crate) fn round_to(value: f64, digits: i32) -> f64 { // The scale factor overflows or underflows outside f64's exponent range: past +308 digits the // value cannot change, past -308 every digit is rounded away. @@ -350,6 +355,11 @@ pub(crate) fn round_to(value: f64, digits: i32) -> f64 { if digits < -308 { return 0.0; } + // An exact whole number is already rounded: scaling it past 2^53 would only make the snap + // lose digits, so `ROUND ( 1234567890123457, 1 )` stays that number. + if digits >= 0 && is_exact_whole(value) { + return value; + } let factor = 10f64.powi(digits); let scaled = excel_precision(value * factor); if !scaled.is_finite() { @@ -370,6 +380,9 @@ pub(crate) fn round_direction(value: f64, digits: i32, up: bool) -> f64 { if digits < -308 { return if up && value != 0.0 { f64::INFINITY.copysign(value) } else { 0.0 }; } + if digits >= 0 && is_exact_whole(value) { + return value; + } let factor = 10f64.powi(digits); let scaled = excel_precision(value * factor); if !scaled.is_finite() { @@ -722,6 +735,9 @@ mod tests { assert_eq!(round_direction(-2.1, 0, true), -3.0); assert_eq!(round_to(2.5, 0), 3.0); assert_eq!(round_to(2.345, 2), 2.35); + // An exact whole input is already the answer: scaling it past 2^53 must not snap digits. + assert_eq!(round_to(1_234_567_890_123_457.0, 1), 1_234_567_890_123_457.0); + assert_eq!(round_direction(1_234_567_890_123_457.0, 2, true), 1_234_567_890_123_457.0); } #[test] diff --git a/crates/dax/src/pivot.rs b/crates/dax/src/pivot.rs index aa30599f5..2499cb76f 100644 --- a/crates/dax/src/pivot.rs +++ b/crates/dax/src/pivot.rs @@ -49,10 +49,10 @@ pub fn pivot_cross(model: &DataModel, measure: &str, row_fields: &[PivotField], let mut column_groups: BTreeMap, Vec> = BTreeMap::new(); for row in &source.rows { let row_values = group_values(row, row_fields, table_index)?; - row_groups.entry(row_values.iter().map(ScalarKey::from).collect()).or_insert(row_values); + row_groups.entry(row_values.iter().map(crate::value::distinct_key).collect()).or_insert(row_values); if !column_fields.is_empty() { let column_values = group_values(row, column_fields, table_index)?; - column_groups.entry(column_values.iter().map(ScalarKey::from).collect()).or_insert(column_values); + column_groups.entry(column_values.iter().map(crate::value::distinct_key).collect()).or_insert(column_values); } } if column_fields.is_empty() { diff --git a/crates/dax/src/pivot_table.rs b/crates/dax/src/pivot_table.rs index 0084e8819..4dac42cef 100644 --- a/crates/dax/src/pivot_table.rs +++ b/crates/dax/src/pivot_table.rs @@ -14,7 +14,8 @@ //! value is a column read, so the model rows are neither walked nor cloned //! ([`PivotTable::render_with_stats`] reports the counters). //! * Filters are applied first: a row must match every filter. A filter's `allowed` list is a set -//! of exact scalar values (case-sensitive for strings); an empty `allowed` list keeps no rows. +//! of scalar values matched with the model collation (text ignores case, like `=`); an empty +//! `allowed` list keeps no rows. //! * Row- and column-field values are grouped ascending in [`ScalarKey`] order, so BLANK sorts //! first and is a group of its own (`(Blank)` in labels). //! * Cells are evaluated through the normal evaluator with a [`FilterContext`] holding the filters @@ -137,7 +138,8 @@ pub enum PivotLayout { pub struct PivotFilter { /// A field reference in `Table[Column]` form, resolved with [`crate::pivot::pivot_fields`]. pub field: String, - /// The allowed values. Membership is exact scalar equality. + /// The allowed values, matched with the model collation: text compares case-insensitively, + /// everything else exactly. #[serde(default)] pub allowed: Vec, /// Keep every value (`(All)` in Excel): the filter is shown but restricts nothing. `allowed` @@ -411,20 +413,22 @@ impl<'a> Plan<'a> { SourceRows::Columns { storage, rows } => { for row in rows { let values = self.column_group_values(storage, row, &self.row_fields); - row_groups.entry(values.iter().map(ScalarKey::from).collect()).or_insert(values); + // The group key folds text exactly like the cell filter, so `East` and + // `east` are one leaf; the first spelling seen is the one displayed. + row_groups.entry(values.iter().map(crate::value::distinct_key).collect()).or_insert(values); if !self.column_fields.is_empty() { let values = self.column_group_values(storage, row, &self.column_fields); - column_groups.entry(values.iter().map(ScalarKey::from).collect()).or_insert(values); + column_groups.entry(values.iter().map(crate::value::distinct_key).collect()).or_insert(values); } } } SourceRows::Rows(rows) => { for row in rows.iter() { let values = self.model_group_values(row, &self.row_fields); - row_groups.entry(values.iter().map(ScalarKey::from).collect()).or_insert(values); + row_groups.entry(values.iter().map(crate::value::distinct_key).collect()).or_insert(values); if !self.column_fields.is_empty() { let values = self.model_group_values(row, &self.column_fields); - column_groups.entry(values.iter().map(ScalarKey::from).collect()).or_insert(values); + column_groups.entry(values.iter().map(crate::value::distinct_key).collect()).or_insert(values); } } } diff --git a/crates/dax/src/value.rs b/crates/dax/src/value.rs index 14c8746f7..c0e887694 100644 --- a/crates/dax/src/value.rs +++ b/crates/dax/src/value.rs @@ -202,7 +202,10 @@ impl Scalar { match (self, other) { (Scalar::Blank, Scalar::Blank) => true, (Scalar::String(a), Scalar::String(b)) => a == b, - (a, b) if a.is_numeric() && b.is_numeric() => a.to_number() == b.to_number(), + (a, b) if a.is_numeric() && b.is_numeric() => match (exact_number(a), exact_number(b)) { + (Some(x), Some(y)) => x == y, + _ => false, + }, _ => self == other, } } @@ -260,6 +263,21 @@ pub(crate) fn distinct_key(value: &Scalar) -> ScalarKey { } } +/// The numeric value of a variant that an `f64` holds exactly. +/// +/// `Integer` and `Currency` are compared through their exact representation only up to 2^53: +/// beyond it `9007199254740992` and `9007199254740993` share an `f64`, and a relationship must not +/// treat two distinct keys as the same one. +fn exact_number(value: &Scalar) -> Option { + const LIMIT: i64 = 1 << 53; + match value { + Scalar::Integer(number) if number.unsigned_abs() <= LIMIT as u64 => Some(*number as f64), + Scalar::Currency(number) if number.unsigned_abs() <= LIMIT as u64 => Some(*number as f64 / 10_000.0), + Scalar::Decimal(number) | Scalar::DateTime(number) => Some(*number), + _ => None, + } +} + /// Maps an `f64` to a `u64` whose unsigned order matches numeric order. `-0.0` and `0.0` map to /// the same key, so `Ord` on [`ScalarKey`] sorts pivot groups numerically. fn order_bits(value: f64) -> u64 { diff --git a/crates/dax/tests/eval_audit_semantics.rs b/crates/dax/tests/eval_audit_semantics.rs index ccbbfbf3a..85a71640c 100644 --- a/crates/dax/tests/eval_audit_semantics.rs +++ b/crates/dax/tests/eval_audit_semantics.rs @@ -179,6 +179,8 @@ fn strict_equality_matches_a_decimal_column_against_an_integer_literal() { assert!(gridcraft_dax::Scalar::Integer(2).strict_eq(&Scalar::Decimal(2.0))); assert!(!gridcraft_dax::Scalar::Integer(2).strict_eq(&Scalar::Decimal(2.5))); assert!(!Scalar::String("a".into()).strict_eq(&Scalar::String("A".into()))); + // Beyond 2^53 two integers share an `f64`, and they must stay distinct keys. + assert!(!Scalar::Integer(9_007_199_254_740_992).strict_eq(&Scalar::Integer(9_007_199_254_740_993))); } #[test]