Skip to content

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

[pivot] Compute and store some data rows as arrays (#83056)

Oleksandr Yakushevc0754b952385
diff --git a/src/metabase/pivot/core.cljc b/src/metabase/pivot/core.cljc
index 6dcb7102ce5..f5bb2d0522f 100644
--- a/src/metabase/pivot/core.cljc
+++ b/src/metabase/pivot/core.cljc
@@ -100,7 +100,7 @@
(reduce
(fn [acc row]
(let [grouping-key (perf/mapv #(ensure-consistent-type (nth row %)) column-indexes)
- values (perf/mapv #(nth row %) val-indexes)]
+ values (perf/mapa #(nth row %) val-indexes)]
(assoc! acc grouping-key values)))
(transient {})
rows))]
@@ -196,7 +196,7 @@
(reduce
(fn [acc row]
(let [value-key (perf/mapv #(ensure-consistent-type (nth row %)) col-and-row-indexes)
- values (select-indexes row val-indexes)
+ values (perf/mapa #(nth row %) val-indexes)
data (perf/mapv-indexed (fn [^long index value]
{:value value
:colIdx index})
diff --git a/src/metabase/util/performance.cljc b/src/metabase/util/performance.cljc
index c91fa15f380..ee3fc84c09e 100644
--- a/src/metabase/util/performance.cljc
+++ b/src/metabase/util/performance.cljc
@@ -689,3 +689,14 @@
[m key value]
#?(:clj (do (.put ^HashMap m key value) m)
:cljs (do (.set m key value) m)))
+
+;;;; Cross-platform array-processing functions
+
+(defn mapa
+ "Like [[mapv]], but returns an array. Requires `coll` to be countable and have O(1) access."
+ [f coll]
+ (let [n (count coll)
+ res (object-array n)]
+ (dotimes [i n]
+ (aset res i (f (nth coll i))))
+ res))
diff --git a/test/metabase/pivot/core_test.cljc b/test/metabase/pivot/core_test.cljc
index 34c182ff512..31bb4233643 100644
--- a/test/metabase/pivot/core_test.cljc
+++ b/test/metabase/pivot/core_test.cljc
@@ -178,7 +178,7 @@
result (#'pivot/get-subtotal-values pivot-data val-indexes nil)]
(is (= {[0 1 2] {[1 "A" "Y"] [10]
[1 "B" "Z"] [20]}}
- result)))))
+ (update-vals result #(update-vals % vec)))))))
(deftest get-subtotal-values-primary-rows-key-test
(testing "Excludes the primary rows if passed a primary rows key"
@@ -270,7 +270,7 @@
(is (= [[["Y" 1 "A"] [10]]
[["Z" 2 "B"] [20]]]
(map
- (fn [[k v]] [k (:values v)])
+ (fn [[k v]] [k (vec (:values v))])
(:values-by-key result)))
"values-by-key should identify each value by its concatenated column and row paths"))))
diff --git a/test/metabase/util/performance_test.cljc b/test/metabase/util/performance_test.cljc
index f6160a4d2bb..ba76caa1e69 100644
--- a/test/metabase/util/performance_test.cljc
+++ b/test/metabase/util/performance_test.cljc
@@ -258,6 +258,9 @@
(for [_ (range 10)]
(throw (ex-info "you should not be here" {})))))))))
+(deftest ^:parallel mapa-test
+ (is (= [1 2 3] (vec (perf/mapa inc (range 3))))))
+
#?(:clj
(defspec mapv-single-coll-equivalence 100
(prop/for-all [coll (mg/generator [:sequential :int])]