mirror of
https://github.com/storytold/gridcraft.git
synced 2026-12-02 10:37:05 -06:00
dax: intersect filters and narrow pivot cells under folded equality (#2228)
This commit is contained in:
@@ -62,7 +62,7 @@ use crate::ast::{BinaryOp, Literal, UnaryOp};
|
||||
use crate::binder::{BindError, Binder, BoundExpr};
|
||||
use crate::model::DataModel;
|
||||
use crate::storage::{ColumnKind, ModelStorage, StorageError, TableStorage};
|
||||
use crate::value::{EvalError, Scalar, ScalarKey, TableColumn, TableValue, distinct_key, folded_key, relationship_key_of};
|
||||
use crate::value::{EvalError, Scalar, ScalarKey, TableColumn, TableValue, distinct_key, fold_intersect, folded_key, relationship_key_of};
|
||||
|
||||
use support::*;
|
||||
|
||||
@@ -196,8 +196,12 @@ impl FilterContext {
|
||||
// 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)
|
||||
// Fast path: the exact key is usually in the set; only a miss folds.
|
||||
let exact = ScalarKey::from(value);
|
||||
values.contains(&exact) || {
|
||||
let wanted = folded_key(&exact);
|
||||
values.iter().any(|allowed| folded_key(allowed) == wanted)
|
||||
}
|
||||
}
|
||||
None => true,
|
||||
}
|
||||
@@ -1638,7 +1642,7 @@ impl<'a> Evaluator<'a> {
|
||||
self.apply_filter(inner, &mut narrowed, ctx)?;
|
||||
for (key, keys) in narrowed.filters {
|
||||
let merged = match filters.filters.get(&key) {
|
||||
Some(existing) => existing.intersection(&keys).cloned().collect(),
|
||||
Some(existing) => fold_intersect(existing, &keys),
|
||||
None => keys,
|
||||
};
|
||||
filters.set(key.0, key.1, merged);
|
||||
|
||||
@@ -461,7 +461,7 @@ impl<'a> Plan<'a> {
|
||||
previous_leaf = Some(row_group.values.clone());
|
||||
}
|
||||
for group in &columns {
|
||||
let context = self.filter_context(&row_group.key, &group.key);
|
||||
let context = self.filter_context(&row_group.values, &group.values);
|
||||
for measure in &self.measures {
|
||||
let value = self.evaluator.evaluate_measure(measure, context.clone()).map_err(PivotError::from)?;
|
||||
out.push(value);
|
||||
@@ -543,7 +543,7 @@ impl<'a> Plan<'a> {
|
||||
///
|
||||
/// A related field's group value is set on the related table, so the evaluator's relationship
|
||||
/// propagation restricts the source table exactly as a slicer on that dimension would.
|
||||
fn filter_context(&self, row_key: &[ScalarKey], column_key: &[ScalarKey]) -> FilterContext {
|
||||
fn filter_context(&self, row_values: &[Scalar], column_values: &[Scalar]) -> FilterContext {
|
||||
let mut context = FilterContext::default();
|
||||
// Several filters on one column intersect, the same way `passes` ANDs them for the groups:
|
||||
// the last one must not silently win in the cells while the groups use all of them.
|
||||
@@ -551,7 +551,7 @@ impl<'a> Plan<'a> {
|
||||
for (field, allowed) in &self.filters {
|
||||
let key = (field.field.table, field.field.column);
|
||||
match merged.get_mut(&key) {
|
||||
Some(existing) => *existing = existing.intersection(allowed).cloned().collect(),
|
||||
Some(existing) => *existing = crate::value::fold_intersect(existing, allowed),
|
||||
None => {
|
||||
merged.insert(key, allowed.clone());
|
||||
}
|
||||
@@ -560,11 +560,13 @@ impl<'a> Plan<'a> {
|
||||
for ((table, column), allowed) in merged {
|
||||
context.set(table, column, allowed);
|
||||
}
|
||||
for (field, key) in self.row_fields.iter().zip(row_key) {
|
||||
narrow_to_key(&mut context, field.field.table, field.field.column, key);
|
||||
// The group values keep their own variant and case, so a date group stays a date for
|
||||
// `SELECTEDVALUE`; matching folds both sides inside `narrow_to_key`.
|
||||
for (field, value) in self.row_fields.iter().zip(row_values) {
|
||||
narrow_to_key(&mut context, field.field.table, field.field.column, &ScalarKey::from(value));
|
||||
}
|
||||
for (field, key) in self.column_fields.iter().zip(column_key) {
|
||||
narrow_to_key(&mut context, field.field.table, field.field.column, key);
|
||||
for (field, value) in self.column_fields.iter().zip(column_values) {
|
||||
narrow_to_key(&mut context, field.field.table, field.field.column, &ScalarKey::from(value));
|
||||
}
|
||||
context
|
||||
}
|
||||
@@ -577,19 +579,19 @@ impl<'a> Plan<'a> {
|
||||
for (index, entry) in leaves.iter().enumerate() {
|
||||
let key: &[ScalarKey] = entry.0.as_slice();
|
||||
let values: &[Scalar] = entry.1.as_slice();
|
||||
out.push(ColumnGroup { key: key.to_vec(), values: values.to_vec(), is_total: false });
|
||||
out.push(ColumnGroup { values: values.to_vec(), is_total: false });
|
||||
if !self.pivot.column_subtotals {
|
||||
continue;
|
||||
}
|
||||
let next = leaves.get(index + 1).map(|entry| entry.0.as_slice());
|
||||
for level in (1..levels).rev() {
|
||||
if prefix_closed(key, next, level) {
|
||||
out.push(ColumnGroup { key: key[..level].to_vec(), values: values[..level].to_vec(), is_total: true });
|
||||
out.push(ColumnGroup { values: values[..level].to_vec(), is_total: true });
|
||||
}
|
||||
}
|
||||
}
|
||||
if self.pivot.grand_totals && levels > 0 && !groups.is_empty() {
|
||||
out.push(ColumnGroup { key: Vec::new(), values: Vec::new(), is_total: true });
|
||||
out.push(ColumnGroup { values: Vec::new(), is_total: true });
|
||||
}
|
||||
out
|
||||
}
|
||||
@@ -602,19 +604,19 @@ impl<'a> Plan<'a> {
|
||||
for (index, entry) in leaves.iter().enumerate() {
|
||||
let key: &[ScalarKey] = entry.0.as_slice();
|
||||
let values: &[Scalar] = entry.1.as_slice();
|
||||
out.push(RowGroup { key: key.to_vec(), values: values.to_vec(), kind: RowKind::Leaf });
|
||||
out.push(RowGroup { values: values.to_vec(), kind: RowKind::Leaf });
|
||||
if !self.pivot.row_subtotals || levels < 2 {
|
||||
continue;
|
||||
}
|
||||
let next = leaves.get(index + 1).map(|entry| entry.0.as_slice());
|
||||
for level in (1..levels).rev() {
|
||||
if prefix_closed(key, next, level) {
|
||||
out.push(RowGroup { key: key[..level].to_vec(), values: values[..level].to_vec(), kind: RowKind::Subtotal(level) });
|
||||
out.push(RowGroup { values: values[..level].to_vec(), kind: RowKind::Subtotal(level) });
|
||||
}
|
||||
}
|
||||
}
|
||||
if self.pivot.grand_totals && levels > 0 && !groups.is_empty() {
|
||||
out.push(RowGroup { key: Vec::new(), values: Vec::new(), kind: RowKind::GrandTotal });
|
||||
out.push(RowGroup { values: Vec::new(), kind: RowKind::GrandTotal });
|
||||
}
|
||||
out
|
||||
}
|
||||
@@ -714,10 +716,9 @@ impl<'a> Plan<'a> {
|
||||
|
||||
/// One rendered column group: a leaf column-field combination, or a subtotal/grand-total column.
|
||||
struct ColumnGroup {
|
||||
/// The column-field values constrained by this group; a prefix for subtotals, empty for the
|
||||
/// grand total.
|
||||
key: Vec<ScalarKey>,
|
||||
/// The group's column-field values, parallel to `key`.
|
||||
/// The group's column-field values, crossing the row/column layout; a prefix for a subtotal,
|
||||
/// empty for the grand total. They keep their own variant and case so the cell's filter can
|
||||
/// rebuild the scalar.
|
||||
values: Vec<Scalar>,
|
||||
/// `true` for a subtotal or grand-total group.
|
||||
is_total: bool,
|
||||
@@ -735,10 +736,8 @@ impl ColumnGroup {
|
||||
|
||||
/// One rendered row.
|
||||
struct RowGroup {
|
||||
/// The row-field values constrained by this row; a prefix for subtotals, empty for the grand
|
||||
/// total.
|
||||
key: Vec<ScalarKey>,
|
||||
/// The row's field values, parallel to `key`.
|
||||
/// The row's field values; a prefix for a subtotal, empty for the grand total. They keep their
|
||||
/// own variant and case so the cell's filter can rebuild the scalar.
|
||||
values: Vec<Scalar>,
|
||||
kind: RowKind,
|
||||
}
|
||||
|
||||
@@ -370,6 +370,15 @@ pub(crate) fn relationship_key_of(key: &ScalarKey) -> ScalarKey {
|
||||
}
|
||||
}
|
||||
|
||||
/// Intersects two key sets under [`folded_key`], keeping the left-hand representatives.
|
||||
///
|
||||
/// Two filters that allow `"East"` and `"east"` accept the same rows, so their intersection must
|
||||
/// not be empty; the same holds for `Integer(2)` and `Decimal(2.0)`.
|
||||
pub(crate) fn fold_intersect(left: &BTreeSet<ScalarKey>, right: &BTreeSet<ScalarKey>) -> BTreeSet<ScalarKey> {
|
||||
let right: BTreeSet<ScalarKey> = right.iter().map(folded_key).collect();
|
||||
left.iter().filter(|key| right.contains(&folded_key(key))).cloned().collect()
|
||||
}
|
||||
|
||||
/// Whether a key holds a NaN value, which never joins and is never a relationship key.
|
||||
pub(crate) fn is_nan_key(key: &ScalarKey) -> bool {
|
||||
match key {
|
||||
|
||||
Reference in New Issue
Block a user