Skip to content

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

[insights] Convert BigDecimals into Doubles early in the pipeline (#65234)

Oleksandr Yakushevcec321e06c5d
diff --git a/src/metabase/analyze/fingerprint/insights.clj b/src/metabase/analyze/fingerprint/insights.clj
index cb122ba83cf..fe0ca4359bd 100644
--- a/src/metabase/analyze/fingerprint/insights.clj
+++ b/src/metabase/analyze/fingerprint/insights.clj
@@ -10,6 +10,7 @@
[metabase.sync.util :as sync-util]
[metabase.util :as u]
[metabase.util.date-2 :as u.date]
+ [metabase.util.performance :refer [mapv-indexed]]
[redux.core :as redux])
(:import
(java.time Instant LocalDate LocalDateTime LocalTime OffsetDateTime OffsetTime ZonedDateTime)
@@ -240,11 +241,16 @@
xfn #(nth % x-position)]
(fingerprinters/with-error-handling
((map (fn [row]
- ;; Convert string datetimes or Instants into into days-from-epoch early.
- (update (vec row) x-position #(some-> %
- fingerprinters/->temporal
- ->millis-from-epoch
- ms->day))))
+ ;; Convert string datetimes or Instants into into days-from-epoch, and BigDecimals into Doubles early.
+ (mapv-indexed (fn [^long i x]
+ (cond (= i x-position)
+ (some-> x
+ fingerprinters/->temporal
+ ->millis-from-epoch
+ ms->day)
+ (decimal? x) (double x)
+ :else x))
+ row)))
(redux/juxt*
(for [number-col numbers]
(redux/post-complete
diff --git a/src/metabase/util/performance.cljc b/src/metabase/util/performance.cljc
index 2e4ffb4485f..2413b21785f 100644
--- a/src/metabase/util/performance.cljc
+++ b/src/metabase/util/performance.cljc
@@ -231,6 +231,23 @@
:cljs
(core/mapv f coll1 coll2 coll3 coll4))))
+#?(:clj
+ (defn mapv-indexed
+ "Like `clojure.core/map-indexed`, but returns a vector and uses Java iterators under the hood and optimized small
+ transient vectors. Requires `f` to be a primitive function of (long, Object) -> Object."
+ [f coll]
+ (if (nil? coll)
+ []
+ (let [n (count coll)
+ it1 (.iterator ^Iterable coll)]
+ (loop [i 0, res (if (<= n 32)
+ (small-transient n identity)
+ (transient []))]
+ (if (.hasNext it1)
+ ;; Use .invokePrim directly to prevent index from autoboxing.
+ (recur (inc i) (conj! res (.invokePrim ^clojure.lang.IFn$LOO f i (.next it1))))
+ (persistent! res)))))))
+
(defn run!
"Drop-in replacement for `clojure.core/run!`.
Iterates collections more efficiently and uses Java iterators under the hood."