Skip to content

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

[pivot] Optimize m.pivot.postprocess/build-full-pivot (#79635)

Oleksandr Yakushevc22064ab7fdf
diff --git a/src/metabase/pivot/core.cljc b/src/metabase/pivot/core.cljc
index f5bb2d0522f..797b45a8cae 100644
--- a/src/metabase/pivot/core.cljc
+++ b/src/metabase/pivot/core.cljc
@@ -543,14 +543,15 @@
[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
- (map-indexed (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))})
- values)
+ (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?
diff --git a/src/metabase/pivot/postprocess.clj b/src/metabase/pivot/postprocess.clj
index c20d19a3bfd..3330797976b 100644
--- a/src/metabase/pivot/postprocess.clj
+++ b/src/metabase/pivot/postprocess.clj
@@ -11,7 +11,7 @@
[metabase.pivot.core :as pivot]
[metabase.util.malli :as mu]
[metabase.util.malli.registry :as mr]
- [metabase.util.performance :as perf :refer [mapv empty?]]))
+ [metabase.util.performance :refer [mapv empty?]]))
(set! *warn-on-reflection* true)
@@ -159,20 +159,15 @@
[get-row-section left-headers top-headers measure-count]
(let [row-count (count left-headers)
left-width (count (first left-headers))
- col-count (- (count (first top-headers)) left-width)
- result (perf/concat
- top-headers
- ;; For each row in left-headers...
- (for [row-idx (range (max row-count 1))]
- (let [left-row (nth left-headers row-idx [])
- ;; ...get cell values for this row
- cell-values (mapcat (fn [col-idx]
- (let [values (get-row-section col-idx row-idx)]
- (map :value values)))
- (range (/ col-count (max measure-count 1))))]
- ;; Combine left headers with cell values
- (into left-row cell-values))))]
- (vec result)))
+ col-count (- (count (first top-headers)) left-width)]
+ (reduce (fn [acc row-idx]
+ ;; For each row in left-headers, get its cell values and combine left headers with cell values.
+ (let [left-row (nth left-headers row-idx [])]
+ (conj acc (into left-row
+ (comp (mapcat (fn [col-idx] (get-row-section col-idx row-idx)))
+ (map :value))
+ (range (/ col-count (max measure-count 1)))))))
+ (vec top-headers) (range (max row-count 1)))))
(defn build-pivot-output
"Processes pivot data into the final pivot structure for exports. Calls into metabase.pivot.core, which is the
diff --git a/test/metabase/pivot/core_test.cljc b/test/metabase/pivot/core_test.cljc
index 31bb4233643..fb6f02bfd4d 100644
--- a/test/metabase/pivot/core_test.cljc
+++ b/test/metabase/pivot/core_test.cljc
@@ -571,7 +571,7 @@
;; `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]
+ (let [values-by-key {["A" 1] {:values (object-array [10 20])
:valueColNames ["count" "sum"]
:data [{:value 1 :colIdx 0}
{:value "A" :colIdx 1}