Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

[pivot] Delay construction of data/dimensions click-through metadata (#83055)

Oleksandr Yakushevf4914e41e5e7
diff --git a/src/metabase/pivot/core.cljc b/src/metabase/pivot/core.cljc
index 797b45a8cae..6b46100a06d 100644
--- a/src/metabase/pivot/core.cljc
+++ b/src/metabase/pivot/core.cljc
@@ -183,36 +183,29 @@
(perf/mapv #(nth row %) indexes))
(defn- build-values-by-key
- "Creates a mapping from row and column indexes to the values, as well as
- metadata used for conditional formatting and drill-throughs."
+ "Creates a mapping from row and column indexes to the values needed to render each cell, plus the original `row`
+ itself in order to compute the metadata necessary for the drill-through click. See [[get-normal-cell-values]]."
[rows cols row-indexes col-indexes val-indexes]
(let [col-and-row-indexes (into (vec col-indexes) row-indexes)
- col-names (perf/mapv :name (select-indexes cols val-indexes))
- breakout-col-indexes (vec (keep-indexed (fn [index col]
- (when (= (:source col) "breakout")
- index))
- cols))]
+ col-names (perf/mapv :name (select-indexes cols val-indexes))]
(persistent!
(reduce
(fn [acc row]
(let [value-key (perf/mapv #(ensure-consistent-type (nth row %)) col-and-row-indexes)
- values (perf/mapa #(nth row %) val-indexes)
- data (perf/mapv-indexed (fn [^long index value]
- {:value value
- :colIdx index})
- row)
- dimensions (perf/mapv (fn [index]
- {:value (nth row index)
- :colIdx index})
- breakout-col-indexes)]
+ values (perf/mapa #(nth row %) val-indexes)]
(assoc! acc value-key
{:values values
:valueColNames col-names
- :data data
- :dimensions dimensions})))
+ :row row})))
(transient {})
rows))))
+(defn- build-breakout-col-indexes [cols]
+ (vec (keep-indexed (fn [index col]
+ (when (= (:source col) "breakout")
+ index))
+ cols)))
+
(defn- sort-orders-from-settings
[col-settings indexes]
(->> (map (into [] col-settings) indexes)
@@ -294,10 +287,12 @@
col-sort-orders (sort-orders-from-settings col-settings col-indexes)
sorted-row-tree (sort-tree collapsed-row-tree row-sort-orders)
sorted-col-tree (sort-tree col-tree col-sort-orders)
- values-by-key (build-values-by-key rows cols row-indexes col-indexes val-indexes)]
+ values-by-key (build-values-by-key rows cols row-indexes col-indexes val-indexes)
+ breakout-col-indexes (build-breakout-col-indexes cols)]
{:row-tree (:children sorted-row-tree)
:col-tree (:children sorted-col-tree)
- :values-by-key values-by-key}))
+ :values-by-key values-by-key
+ :breakout-col-indexes breakout-col-indexes}))
(defn- format-values-in-tree
"Walks a tree, formatting values and annotating each value with its color for
@@ -540,18 +535,26 @@
`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)]
- (if data
- (perf/mapv (fn [index value]
- {:value ((nth value-formatters index) value)
- :clicked {:data data
- :dimensions dimensions
- :colIdx (nth val-indexes index)
- :value value}
- :backgroundColor (color-getter value index (nth valueColNames index))})
- (range (alength ^objects values))
- values)
+ [values-by-key index-values value-formatters val-indexes color-getter breakout-col-indexes]
+ (let [{:keys [values valueColNames row]} (get values-by-key index-values)]
+ (if row
+ (let [data (perf/mapv-indexed (fn [^long index value]
+ {:value value
+ :colIdx index})
+ row)
+ dimensions (perf/mapv (fn [index]
+ {:value (nth row index)
+ :colIdx index})
+ breakout-col-indexes)]
+ (perf/mapv (fn [index value]
+ {:value ((nth value-formatters index) value)
+ :clicked {:data data
+ :dimensions dimensions
+ :colIdx (nth val-indexes index)
+ :value value}
+ :backgroundColor (color-getter value index (nth valueColNames index))})
+ (range (alength ^objects values))
+ values))
(format-values values value-formatters))))
(defn- is-subtotal?
@@ -572,16 +575,17 @@
(get-regular-subtotal subtotal-values indexes index-values value-formatters))))
(defn- create-row-section-getter
- "Returns a memoized function that retrieves and formats values for a specific cell
- position in the pivot table."
- [values-by-key subtotal-values value-formatters col-indexes row-indexes val-indexes col-paths row-paths color-getter]
+ "Returns a function that retrieves and formats values for a specific cell position in the pivot table."
+ [values-by-key subtotal-values value-formatters col-indexes row-indexes val-indexes col-paths row-paths color-getter
+ breakout-col-indexes]
(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 val-indexes color-getter))]
+ (get-normal-cell-values values-by-key index-values value-formatters val-indexes color-getter
+ breakout-col-indexes))]
;; Convert to JavaScript object if in ClojureScript context
#?(:cljs (perf/clj->js result)
:clj result))))
@@ -664,7 +668,7 @@
color-getter #?(:cljs (make-color-getter (clj->js primary-rows))
:clj (make-color-getter))
columns (vec columns)
- {:keys [row-tree col-tree values-by-key]}
+ {:keys [row-tree col-tree values-by-key breakout-col-indexes]}
(build-pivot-trees primary-rows columns row-indexes col-indexes val-indexes settings col-settings)
{:keys [row-paths formatted-row-tree-with-totals]}
@@ -679,4 +683,5 @@
: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 val-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
+ breakout-col-indexes)})))
diff --git a/test/metabase/pivot/core_test.cljc b/test/metabase/pivot/core_test.cljc
index fb6f02bfd4d..f2489547a9b 100644
--- a/test/metabase/pivot/core_test.cljc
+++ b/test/metabase/pivot/core_test.cljc
@@ -573,12 +573,7 @@
;; column, and the backend drill therefore can't find the aggregation UUID.
(let [values-by-key {["A" 1] {:values (object-array [10 20])
:valueColNames ["count" "sum"]
- :data [{:value 1 :colIdx 0}
- {:value "A" :colIdx 1}
- {:value 10 :colIdx 2}
- {:value 20 :colIdx 3}]
- :dimensions [{:value 1 :colIdx 0}
- {:value "A" :colIdx 1}]}}
+ :row [1 "A" 10 20]}}
subtotal-values {[1] {[1] [100 200]}}
value-formatters [#(str "$" %) #(str % "%")]
col-indexes [1]
@@ -587,9 +582,12 @@
col-paths [["A"]]
row-paths [[1]]
color-getter (constantly "blue")
+ ;; columns 0 and 1 (breakout values "1" and "A") are the breakout columns
+ breakout-col-indexes [0 1]
getter (#'pivot/create-row-section-getter values-by-key subtotal-values value-formatters
col-indexes row-indexes val-indexes
- col-paths row-paths color-getter)
+ col-paths row-paths color-getter
+ breakout-col-indexes)
result (getter 0 0)]
(is (= [{:value "$10"
:backgroundColor "blue"