dax: compare integers exactly across variants and keep DATATABLE types keywords (#2228)

This commit is contained in:
alosslessdev
2026-11-05 09:35:46 -05:00
parent b4c8238594
commit 88717cd5ac
5 changed files with 63 additions and 8 deletions
+5 -1
View File
@@ -318,7 +318,11 @@ impl<'a> Binder<'a> {
// position: a table called `Month` must not turn `DATEDIFF(a, b, MONTH)` into a table
// reference, and a variable called `YEAR` must not either.
.filter(|keyword| {
if !matches!(expected, Some(ParamKind::Text)) && !is_visual_keyword(spec.name, index, keyword) {
// `DATATABLE`'s type words are types even though the catalog marks them `Text`,
// so a table named `INTEGER` must not capture them either.
let fixed_slot =
spec.name == "DATATABLE" || (!matches!(expected, Some(ParamKind::Text)) && !is_visual_keyword(spec.name, index, keyword));
if fixed_slot {
return true;
}
let named = self.model.table_index(keyword).is_some() || self.model.measure_index(keyword).is_some();
+14 -6
View File
@@ -1390,6 +1390,12 @@ pub struct RelationshipIndex {
pub from_rows: BTreeMap<ScalarKey, Vec<usize>>,
/// Key value to row ids in the `to` table.
pub to_rows: BTreeMap<ScalarKey, Vec<usize>>,
/// `from_rows` under the numeric key rule (`value::relationship_key_of`), so a probe with the
/// other variant still joins. Kept beside the exact map because integrity reporting needs the
/// model's own variant.
from_rows_normalized: BTreeMap<ScalarKey, Vec<usize>>,
/// `to_rows` under the same rule.
to_rows_normalized: BTreeMap<ScalarKey, Vec<usize>>,
}
impl RelationshipIndex {
@@ -1403,7 +1409,7 @@ impl RelationshipIndex {
return rows;
}
// A numeric key still joins its other variants: `Integer (2)` finds a `Decimal (2.0)` row.
match self.from_rows.get(&crate::value::relationship_key_of(key)) {
match self.from_rows_normalized.get(&crate::value::relationship_key_of(key)) {
Some(rows) => rows.as_slice(),
None => &[],
}
@@ -1414,7 +1420,7 @@ impl RelationshipIndex {
if let Some(rows) = self.to_rows.get(key) {
return rows;
}
match self.to_rows.get(&crate::value::relationship_key_of(key)) {
match self.to_rows_normalized.get(&crate::value::relationship_key_of(key)) {
Some(rows) => rows.as_slice(),
None => &[],
}
@@ -1490,8 +1496,10 @@ impl ModelStorage {
to_column,
bidirectional: relationship.bidirectional,
inactive: relationship.inactive,
from_rows: build_key_index(from_storage, from_column),
to_rows: build_key_index(to_storage, to_column),
from_rows: build_key_index(from_storage, from_column, |value| ScalarKey::from(value)),
to_rows: build_key_index(to_storage, to_column, |value| ScalarKey::from(value)),
from_rows_normalized: build_key_index(from_storage, from_column, crate::value::relationship_key_of_value),
to_rows_normalized: build_key_index(to_storage, to_column, crate::value::relationship_key_of_value),
});
}
@@ -1635,11 +1643,11 @@ fn storage_round_trips(storage: &TableStorage, table: &crate::model::ModelTable)
})
}
fn build_key_index(table: &TableStorage, column: usize) -> BTreeMap<ScalarKey, Vec<usize>> {
fn build_key_index(table: &TableStorage, column: usize, key: fn(&crate::value::Scalar) -> ScalarKey) -> BTreeMap<ScalarKey, Vec<usize>> {
let mut index: BTreeMap<ScalarKey, Vec<usize>> = BTreeMap::new();
for row in 0..table.row_count() {
if let Some(value) = table.value(column, row) {
index.entry(ScalarKey::from(&value)).or_default().push(row);
index.entry(key(&value)).or_default().push(row);
}
}
index
+27 -1
View File
@@ -195,7 +195,7 @@ impl Scalar {
// would report distinct values as equal, and `IN`/`=` must not.
(Scalar::Integer(a), Scalar::Integer(b)) => Some(a.cmp(b)),
(Scalar::Currency(a), Scalar::Currency(b)) => Some(a.cmp(b)),
_ => self.to_number()?.partial_cmp(&other.to_number()?),
_ => numeric_compare(self, other),
}
}
@@ -252,6 +252,32 @@ impl From<&Scalar> for ScalarKey {
}
}
/// Compares two numeric scalars.
///
/// An `Integer` against a `Decimal`/`DateTime` is compared through `i128` when the decimal is
/// integral, so a value above 2^53 is not rounded into its neighbour; every other pair uses the
/// `f64` coercion DAX applies.
fn numeric_compare(left: &Scalar, right: &Scalar) -> Option<Ordering> {
fn exact(value: &Scalar) -> Option<i128> {
match value {
Scalar::Integer(number) => Some(i128::from(*number)),
Scalar::Decimal(number) | Scalar::DateTime(number) if number.fract() == 0.0 && number.abs() < 9.2e18 => Some(*number as i128),
_ => None,
}
}
let integer_involved = matches!(left, Scalar::Integer(_) | Scalar::Currency(_)) || matches!(right, Scalar::Integer(_) | Scalar::Currency(_));
if integer_involved && let (Some(a), Some(b)) = (exact(left), exact(right)) {
return Some(a.cmp(&b));
}
left.to_number()?.partial_cmp(&right.to_number()?)
}
/// [`relationship_key_of`] for a value: the key a relationship index stores for it under the
/// numeric rule.
pub(crate) fn relationship_key_of_value(value: &Scalar) -> ScalarKey {
relationship_key_of(&ScalarKey::from(value))
}
/// [`relationship_key`] for a key that is already normalized-except-for-variant.
pub(crate) fn relationship_key_of(key: &ScalarKey) -> ScalarKey {
match key {
+15
View File
@@ -301,3 +301,18 @@ fn concatenate_x_binds_a_bare_order_word_in_every_pair() {
let expr = parse_expression("CONCATENATEX ( Sales, Sales[Region], \"|\", Sales[Region], SIDEWAYS )").expect("parse");
assert!(Binder::new(&model).bind_expression(&expr).is_err());
}
#[test]
fn a_datatable_type_word_is_never_a_table_name() {
let mut model = sample_model();
// A table called `INTEGER` must not capture DATATABLE's type word.
model.tables.push(gridcraft_dax::model::ModelTable {
name: "INTEGER".into(),
columns: vec![gridcraft_dax::model::ModelColumn { name: "Id".into(), data_type: Some("Int64".into()) }],
rows: Vec::new(),
});
match bind(&model, "DATATABLE ( \"Id\", INTEGER, { { 1 } } )") {
BoundExpr::Call { args, .. } => assert!(matches!(&args[1], BoundExpr::Keyword(word) if word == "INTEGER"), "{:?}", args[1]),
other => panic!("expected a call, got {other:?}"),
}
}
+2
View File
@@ -187,6 +187,8 @@ fn strict_equality_matches_a_decimal_column_against_an_integer_literal() {
// `compare` keeps the same exactness, so `IN` and `=` do not conflate large integers either.
assert_eq!(Scalar::Integer(9_007_199_254_740_992).compare(&Scalar::Integer(9_007_199_254_740_993)), Some(std::cmp::Ordering::Less));
assert!(Scalar::Integer(9_007_199_254_740_993).compare(&Scalar::Integer(9_007_199_254_740_993)) == Some(std::cmp::Ordering::Equal));
// A large integer against an integral decimal stays exact instead of rounding into it.
assert_eq!(Scalar::Integer(9_007_199_254_740_993).compare(&Scalar::Decimal(9_007_199_254_740_992.0)), Some(std::cmp::Ordering::Greater));
}
#[test]