mirror of
https://github.com/storytold/gridcraft.git
synced 2026-12-01 18:26:05 -06:00
dax: keep filter keys in their own variant and fold at comparison (#2228)
This commit is contained in:
+10
-4
@@ -180,8 +180,11 @@ impl FilterContext {
|
||||
self.filters.clear();
|
||||
}
|
||||
|
||||
/// Sets a column filter. The keys keep their own variant so `column_value` can rebuild the
|
||||
/// original scalar (`SELECTEDVALUE` must answer a date for a date filter); matching folds both
|
||||
/// sides through [`folded_key`] instead.
|
||||
pub fn set(&mut self, table: usize, column: usize, values: impl IntoIterator<Item = ScalarKey>) {
|
||||
self.filters.insert((table, column), values.into_iter().map(|key| folded_key(&key)).collect());
|
||||
self.filters.insert((table, column), values.into_iter().collect());
|
||||
}
|
||||
|
||||
pub fn remove_table(&mut self, table: usize) {
|
||||
@@ -190,9 +193,12 @@ impl FilterContext {
|
||||
|
||||
fn matches(&self, table: usize, column: usize, value: &Scalar) -> bool {
|
||||
match self.filters.get(&(table, column)) {
|
||||
// Filter keys and values fold text the same way, so `VALUES`/`DISTINCTCOUNT` and the
|
||||
// filters they feed agree on `"A"` and `"a"`.
|
||||
Some(values) => values.contains(&distinct_key(value)),
|
||||
// Both sides fold through the same rule, so text ignores case and a numeric key
|
||||
// matches its other variants (a caller may insert keys into the public map directly).
|
||||
Some(values) => {
|
||||
let wanted = distinct_key(value);
|
||||
values.iter().any(|allowed| folded_key(allowed) == wanted)
|
||||
}
|
||||
None => true,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -393,7 +393,7 @@ impl<'a> Plan<'a> {
|
||||
let field =
|
||||
resolved.into_iter().next().ok_or_else(|| PivotTableError::UnknownField { pivot: name.clone(), field: filter.field.clone() })?;
|
||||
let bound = bind_field(model, table, &table_name, field)?;
|
||||
let allowed = filter.allowed.iter().map(|value| crate::value::folded_key(&ScalarKey::from(value))).collect();
|
||||
let allowed = filter.allowed.iter().map(ScalarKey::from).collect();
|
||||
filters.push((bound, allowed));
|
||||
}
|
||||
|
||||
@@ -525,7 +525,7 @@ impl<'a> Plan<'a> {
|
||||
fn passes(&self, row: &[Scalar]) -> bool {
|
||||
self.filters.iter().all(|(field, allowed)| {
|
||||
let value = row.get(field.field.column).cloned().unwrap_or(Scalar::Blank);
|
||||
allowed.contains(&crate::value::distinct_key(&value))
|
||||
allowed.iter().any(|allowed| crate::value::folded_key(allowed) == crate::value::distinct_key(&value))
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1095,12 +1095,12 @@ fn hop_key_lookups<'a>(model: &DataModel, fields: impl IntoIterator<Item = &'a B
|
||||
/// off-diagonal combination leaves the column's filter empty, and an empty filter matches no row,
|
||||
/// so the cell is BLANK rather than the other axis's total.
|
||||
pub(crate) fn narrow_to_key(context: &mut FilterContext, table: usize, column: usize, key: &ScalarKey) {
|
||||
// Filter keys fold text, so the group key is folded before it is compared with them.
|
||||
let key = crate::value::folded_key(key);
|
||||
// The stored keys keep their variants; the comparison folds both sides.
|
||||
let folded = crate::value::folded_key(key);
|
||||
match context.filters.get(&(table, column)) {
|
||||
// The intersection with `{key}` is empty: no row can satisfy both axes.
|
||||
Some(allowed) if !allowed.contains(&key) => context.set(table, column, std::iter::empty()),
|
||||
_ => context.set(table, column, [key]),
|
||||
Some(allowed) if !allowed.iter().any(|allowed| crate::value::folded_key(allowed) == folded) => context.set(table, column, std::iter::empty()),
|
||||
_ => context.set(table, column, [key.clone()]),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user