The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Preserve cell information in pivots (#79266)
metambenfe731bdb2ebb
diff --git a/frontend/src/metabase/visualizations/visualizations/PivotTable/PivotTable.unit.spec.tsx b/frontend/src/metabase/visualizations/visualizations/PivotTable/PivotTable.unit.spec.tsxindex d730b581307..37ae8368ac4 100644--- a/frontend/src/metabase/visualizations/visualizations/PivotTable/PivotTable.unit.spec.tsx+++ b/frontend/src/metabase/visualizations/visualizations/PivotTable/PivotTable.unit.spec.tsx@@ -270,6 +270,56 @@ describe("Visualizations > PivotTable > PivotTable", () => {});});+ describe("body cell click (#79023)", () => {+ // The click payload must identify the aggregation column so drills like+ // "See these X" can lift the aggregation's inner filter (e.g. CountIf's+ // `[Source] = "Invite"`). Without `column` set, and without each `data`+ // entry carrying its resolved `col`, the underlying-records drill can't+ // find the aggregation ref and drops the inner filter.+ it("resolves the aggregation column and enriches the data/dimensions arrays", async () => {+ const onVisualizationClick = jest.fn();+ setupPivotTable({ onVisualizationClick });++ // The value 111 is the aggregation-1 cell for the first breakout row (foo1/bar1/baz1).+ await userEvent.click(await screen.findByText("111"));++ expect(onVisualizationClick).toHaveBeenCalledTimes(1);+ const clicked = onVisualizationClick.mock.calls[0][0];++ expect(clicked.column).toMatchObject({+ name: "aggregation-1",+ source: "aggregation",+ });+ expect(clicked.value).toBe(111);++ // Each `data` entry must have `col` resolved from its `colIdx` — the split+ // if/else in getCellClickHandler is what enables this for value-cell clicks+ // that carry both a top-level `colIdx` and a pre-built `data` array.+ expect(clicked.data).toEqual(+ expect.arrayContaining([+ expect.objectContaining({+ col: expect.objectContaining({ name: "aggregation-1" }),+ value: 111,+ }),+ expect.objectContaining({+ col: expect.objectContaining({ name: "field-123" }),+ value: "foo1",+ }),+ ]),+ );++ // Breakout dimensions must carry column metadata so the drill can filter by them.+ expect(clicked.dimensions).toEqual(+ expect.arrayContaining([+ expect.objectContaining({+ column: expect.objectContaining({ name: "field-123" }),+ value: "foo1",+ }),+ ]),+ );+ });+ });+describe("scrollbar alignment", () => {it("should reduce top header width by scrollbar size when body has vertical scrollbar", () => {const scrollBarSize = 15;diff --git a/frontend/src/metabase/visualizations/visualizations/PivotTable/PivotTableInner.tsx b/frontend/src/metabase/visualizations/visualizations/PivotTable/PivotTableInner.tsxindex 43a19980390..7ad060f6351 100644--- a/frontend/src/metabase/visualizations/visualizations/PivotTable/PivotTableInner.tsx+++ b/frontend/src/metabase/visualizations/visualizations/PivotTable/PivotTableInner.tsx@@ -348,23 +348,27 @@ const PivotTableInner = forwardRef<HTMLDivElement, VisualizationProps>(}// The CLJS code adds `colIdx` to the objects used for click handling instead of the entire column- // to avoid duplicate column metadata conversions from CLJS data structures to JS objects+ // to avoid duplicate column metadata conversions from CLJS data structures to JS objects.+ // A value-cell click carries a top-level `colIdx` (identifying the aggregation column) AND+ // a pre-built `data` array, so column-setting and data-enrichment must be independent (#79023).const { colIdx, ...updatedClicked } = clicked;if (typeof colIdx === "number") {updatedClicked.column = columnsWithoutPivotGroup[colIdx];- updatedClicked.data ??= [- {- value: updatedClicked.value,- col: columnsWithoutPivotGroup[colIdx] || null,- },- ];- } else if (updatedClicked.data) {+ }+ if (updatedClicked.data) {updatedClicked.data = updatedClicked.data.map(({ colIdx, ...item }) => ({...item,col: colIdx !== undefined ? columnsWithoutPivotGroup[colIdx] : null,}),);+ } else if (typeof colIdx === "number") {+ updatedClicked.data = [+ {+ value: updatedClicked.value,+ col: columnsWithoutPivotGroup[colIdx] || null,+ },+ ];}if (updatedClicked.dimensions) {diff --git a/src/metabase/pivot/core.cljc b/src/metabase/pivot/core.cljcindex c44f65be6a3..b25ddc69eaa 100644--- a/src/metabase/pivot/core.cljc+++ b/src/metabase/pivot/core.cljc@@ -540,8 +540,14 @@(get-subtotals subtotal-values indexes index-values {} value-formatters))(defn- get-normal-cell-values- "Processes and formats values for normal data cells (non-subtotal)."- [values-by-key index-values value-formatters color-getter]+ "Processes and formats values for normal data cells (non-subtotal).++ Each cell's `:clicked` payload carries `:colIdx` — the position of the aggregation column in `cols` — and the raw+ `:value` for that column, so the FE can enrich the click with the aggregation column metadata. Drill-thrus like+ `underlying-records` rely on this to identify the clicked aggregation and lift its inner filter (e.g. `count-where`,+ `sum-where`, `share`). Without them, clicking a pivot cell for `CountIf([X] = Y)` drills without the `X = Y` filter+ (#79023)."+ [values-by-key index-values value-formatters val-indexes color-getter](let [{:keys [values valueColNames data dimensions]} (get values-by-key index-values)formatted-values (format-values values value-formatters)](if-not data@@ -550,7 +556,9 @@(fn [index value](assoc value:clicked {:data data- :dimensions dimensions}+ :dimensions dimensions+ :colIdx (nth val-indexes index)+ :value (nth values index)}:backgroundColor (color-getter(nth values index)index@@ -577,14 +585,14 @@(defn- create-row-section-getter"Returns a memoized function that retrieves and formats values for a specific cellposition in the pivot table."- [values-by-key subtotal-values value-formatters col-indexes row-indexes col-paths row-paths color-getter]+ [values-by-key subtotal-values value-formatters col-indexes row-indexes val-indexes col-paths row-paths color-getter](fn [col-index row-index](let [col-values (nth col-paths col-index [])row-values (nth row-paths row-index [])index-values (concat col-values row-values)result (if (is-subtotal? row-values col-values row-indexes col-indexes)(handle-subtotal-cell subtotal-values row-values col-values row-indexes col-indexes value-formatters)- (get-normal-cell-values values-by-key index-values value-formatters color-getter))]+ (get-normal-cell-values values-by-key index-values value-formatters val-indexes color-getter))];; Convert to JavaScript object if in ClojureScript context#?(:cljs (perf/clj->js result):clj result))))@@ -683,4 +691,4 @@:rowIndex row-paths:leftHeaderItems (tree-to-array formatted-row-tree-with-totals):topHeaderItems (tree-to-array formatted-col-tree)- :getRowSection (create-row-section-getter values-by-key subtotal-values value-formatters col-indexes row-indexes col-paths row-paths color-getter)})))+ :getRowSection (create-row-section-getter values-by-key subtotal-values value-formatters col-indexes row-indexes val-indexes col-paths row-paths color-getter)})))diff --git a/test/metabase/lib/drill_thru/underlying_records_test.cljc b/test/metabase/lib/drill_thru/underlying_records_test.cljcindex f1111cf6dcf..d0fbc97bff4 100644--- a/test/metabase/lib/drill_thru/underlying_records_test.cljc+++ b/test/metabase/lib/drill_thru/underlying_records_test.cljc@@ -207,6 +207,36 @@(lib.options/with-options {}))"Doohickey"]]))))+(deftest ^:parallel underlying-records-apply-test-count-where-79023+ ;; Direct reproduction of #79023: a pivot table summarized with a+ ;; `CountIf([Source] = "Invite")` aggregation and broken out by a dimension.+ ;; Clicking a body cell and drilling to "See these X" must apply BOTH the+ ;; breakout equality filter AND the count-where's inner filter, matching how+ ;; the same aggregation drills from a line chart. `underlying-state` feeds a+ ;; well-formed cell-click context (aggregation column + column-ref + value ++ ;; breakout dimensions) — the shape the pivot layer produces post-fix.+ (testing "count_where(products.category = \"Doohickey\") over time (#79023)"+ (underlying-state (-> (lib/query meta/metadata-provider (meta/table-metadata :orders))+ (lib/aggregate (lib/count-where+ (lib/= (meta/field-metadata :products :category)+ "Doohickey")))+ (lib/breakout (lib/with-temporal-bucket+ (meta/field-metadata :orders :created-at)+ :month)))+ 0 #_agg-index+ 42+ [last-month]+ (fn [_agg-dim [breakout-dim]]+ [[:between {}+ (-> (:column-ref breakout-dim)+ (lib.options/with-options {:temporal-unit :month}))+ last-month-start+ last-month-end]+ [:= {} (-> (meta/field-metadata :products :category)+ lib/ref+ (lib.options/with-options {}))+ "Doohickey"]]))))+(deftest ^:parallel underlying-records-apply-test-3(testing "metric over time"(let [metric-id 101diff --git a/test/metabase/pivot/core_test.cljc b/test/metabase/pivot/core_test.cljcindex fdca74f9250..74bfd34672a 100644--- a/test/metabase/pivot/core_test.cljc+++ b/test/metabase/pivot/core_test.cljc@@ -565,8 +565,14 @@(deftest create-row-section-getter-test(testing "Returns a function that correctly retrieves cell values"+ ;; Each body cell's `:clicked` payload must carry `:colIdx` and raw `:value`+ ;; identifying the aggregation column the cell represents (issue #79023).+ ;; Without them, drills like "See these X" on a pivot cell for a `CountIf` /+ ;; `SumIf` / `Share` aggregation cannot lift the aggregation's inner filter,+ ;; because the frontend has no way to enrich the click with the aggregation+ ;; column, and the backend drill therefore can't find the aggregation UUID.(let [values-by-key {["A" 1] {:values [10 20]- :valueColumns [{:name "count"} {:name "sum"}]+ :valueColNames ["count" "sum"]:data [{:value 1 :colIdx 0}{:value "A" :colIdx 1}{:value 10 :colIdx 2}@@ -577,11 +583,13 @@value-formatters [#(str "$" %) #(str % "%")]col-indexes [1]row-indexes [0]+ val-indexes [2 3]col-paths [["A"]]row-paths [[1]]color-getter (constantly "blue")getter (#'pivot/create-row-section-getter values-by-key subtotal-values value-formatters- col-indexes row-indexes col-paths row-paths color-getter)+ col-indexes row-indexes val-indexes+ col-paths row-paths color-getter)result (getter 0 0)](is (= [{:value "$10":backgroundColor "blue"@@ -590,7 +598,9 @@{:value 10 :colIdx 2}{:value 20 :colIdx 3}]:dimensions [{:value 1 :colIdx 0}- {:value "A" :colIdx 1}]}}+ {:value "A" :colIdx 1}]+ :colIdx 2+ :value 10}}{:value "20%":backgroundColor "blue":clicked {:data [{:value 1 :colIdx 0}@@ -598,7 +608,9 @@{:value 10 :colIdx 2}{:value 20 :colIdx 3}]:dimensions [{:value 1 :colIdx 0}- {:value "A" :colIdx 1}]}}]+ {:value "A" :colIdx 1}]+ :colIdx 3+ :value 20}}]#?(:cljs (js->clj result :keywordize-keys true):clj result))))))