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
Improve fingerprint performance (#74683)
Nathan Voxland0d4997a4f32a
diff --git a/src/metabase/analyze/fingerprint/fingerprinters.clj b/src/metabase/analyze/fingerprint/fingerprinters.cljindex 6a0d3fc9a64..9d74897b423 100644--- a/src/metabase/analyze/fingerprint/fingerprinters.clj+++ b/src/metabase/analyze/fingerprint/fingerprinters.clj@@ -14,7 +14,7 @@[metabase.util.performance :as perf][redux.core :as redux])(:import- (java.time Instant LocalDate LocalDateTime LocalTime OffsetDateTime OffsetTime ZonedDateTime ZoneOffset)+ (java.time ZoneOffset)(java.time.chrono ChronoLocalDateTime ChronoZonedDateTime)(java.time.temporal Temporal)(org.apache.commons.codec.digest MurmurHash2)@@ -263,8 +263,11 @@(invoke [this x](set! total (inc total))(when-not (nil? x)- (cond (contains? counts x) (set! counts (update counts x inc))- (< (count counts) mode-stats-max-distinct) (set! counts (assoc counts x 1))))+ ;; Key the frequency map on (hash x), not x itself: mode-fraction / top-3-fraction need only+ ;; value frequencies, never the values, and retaining raw values is high memory cost.+ (let [k (hash x)]+ (cond (contains? counts k) (set! counts (update counts k inc))+ (< (count counts) mode-stats-max-distinct) (set! counts (assoc counts k 1)))))this))(defn- mode-stats@@ -276,27 +279,12 @@([tracker] (tracker))([tracker x] (tracker x)))-(defn- ->millis-from-epoch- "Coerce a `java.time.temporal.Temporal` (as produced by `->temporal`) to long epoch millis.- Returns nil for unsupported types; callers typically wrap with `(keep ->millis-from-epoch)`- to strip non-coerceable values."- [t]- (cond (instance? Instant t) (.toEpochMilli ^Instant t)- (instance? OffsetDateTime t) (.toEpochMilli (.toInstant ^OffsetDateTime t))- (instance? ZonedDateTime t) (.toEpochMilli (.toInstant ^ZonedDateTime t))- (instance? LocalDate t) (recur (t/offset-date-time t (t/local-time 0) (t/zone-offset 0)))- (instance? LocalDateTime t) (recur (t/offset-date-time t (t/zone-offset 0)))- (instance? LocalTime t) (recur (t/offset-date-time (t/local-date "1970-01-01") t (t/zone-offset 0)))- (instance? OffsetTime t) (recur (t/offset-date-time (t/local-date "1970-01-01") t (t/zone-offset t)))- :else nil))-(deffingerprinter :type/DateTime((map ->temporal)(redux/post-complete(robust-fuse {:earliest earliest:latest latest- :skewness ((keep ->millis-from-epoch) stats/skewness)- :mode-stats ((keep ->millis-from-epoch) mode-stats)})+ :mode-stats ((filter some?) mode-stats)})(fn [{:keys [mode-stats] :as fused}](-> fused(dissoc :mode-stats)diff --git a/src/metabase/lib/schema/metadata/fingerprint.cljc b/src/metabase/lib/schema/metadata/fingerprint.cljcindex ab7f3a0b303..b7870c3d097 100644--- a/src/metabase/lib/schema/metadata/fingerprint.cljc+++ b/src/metabase/lib/schema/metadata/fingerprint.cljc@@ -53,7 +53,6 @@{:decode/normalize lib.schema.common/normalize-map-no-kebab-case}[:earliest {:optional true} [:maybe :string]][:latest {:optional true} [:maybe :string]]- [:skewness {:optional true} [:maybe number?]][:mode-fraction {:optional true} [:maybe [:ref ::percent]]][:top-3-fraction {:optional true} [:maybe [:ref ::percent]]]])diff --git a/test/metabase/analyze/fingerprint/fingerprinters_test.clj b/test/metabase/analyze/fingerprint/fingerprinters_test.cljindex b09b9659e8f..199064dd535 100644--- a/test/metabase/analyze/fingerprint/fingerprinters_test.clj+++ b/test/metabase/analyze/fingerprint/fingerprinters_test.clj@@ -113,6 +113,40 @@(fingerprinters/fingerprinter field)[#t "2013" #t "2018" #t "2015"])))))))+(deftest ^:parallel temporal-fingerprint-omits-skewness-test+ (testing "temporal fingerprints do not compute skewness (it required a costly per-value epoch-millis+ conversion for a weak, redundant interestingness signal); the other distribution stats remain"+ (let [result (transduce identity+ (fingerprinters/fingerprinter (mi/instance :model/Field {:base_type :type/DateTime}))+ [(java.time.LocalDateTime/of 2013 1 1 20 4 0 0)+ (java.time.LocalDateTime/of 2014 3 2 4 14 0 0)+ (java.time.LocalDateTime/of 2015 6 5 9 24 0 0)+ (java.time.LocalDateTime/of 2016 9 8 14 34 0 0)+ (java.time.LocalDateTime/of 2017 12 11 22 44 0 0)])+ dt (get-in result [:type :type/DateTime])]+ (is (nil? (:skewness dt))+ "skewness must not be present in temporal fingerprints")+ (is (some? (:earliest dt)))+ (is (some? (:latest dt)))+ ;; 5 distinct timestamps, each once: mode-stats counts the raw Temporal values (no epoch-millis+ ;; conversion) and must still produce the right fractions.+ (is (= 0.2 (:mode-fraction dt)) "1/5")+ (is (= 0.6 (:top-3-fraction dt)) "3/5"))))++(deftest ^:parallel mode-stats-large-values-test+ (testing "mode-fraction / top-3-fraction count frequencies correctly for large text values+ (the tracker keys on a hash, so it must not retain the values yet must still count them)"+ (let [big1 (apply str (repeat 2000 \a))+ big2 (apply str (repeat 2000 \b))+ big3 (apply str (repeat 2000 \c))+ ;; big1 x6, big2 x3, big3 x1 -> mode 0.6, top-3 covers all 10+ result (transduce identity+ (fingerprinters/fingerprinter (mi/instance :model/Field {:base_type :type/Text}))+ (concat (repeat 6 big1) (repeat 3 big2) [big3]))+ txt (get-in result [:type :type/Text])]+ (is (= 0.6 (:mode-fraction txt)))+ (is (= 1.0 (:top-3-fraction txt))))))+(deftest ^:parallel fingerprint-numeric-values-test(is (= {:global {:distinct-count 99:nil% 0.0}diff --git a/test/metabase/metabot/tools/field_stats_test.clj b/test/metabase/metabot/tools/field_stats_test.cljindex b73b2b69096..37d2257f9bb 100644--- a/test/metabase/metabot/tools/field_stats_test.clj+++ b/test/metabase/metabot/tools/field_stats_test.clj@@ -29,25 +29,12 @@{:entity-type "table", :entity-id people-id, :field-id state-id, :limit 5}))))(testing "Getting statistics and values for table fields works."(mt/as-admin- ;; Skewness is a derived double whose last few digits vary across JVMs/platforms,- ;; so strip it from the exact-equality check and assert approximate value separately.- (let [birth-date-result (metabot.tools.field-stats/field-values- {:entity-type "table", :entity-id people-id, :field-id birth-date-id, :limit 5})- birth-date-skewness (get-in birth-date-result- [:structured-output :value_metadata :statistics :skewness])]- (is (some? birth-date-skewness))- (is (< (abs (- birth-date-skewness -0.00557870227770)) 1e-6)- "skewness should be approximately -0.00557870227770"))(are [table-id field-id value-metadata](= {:structured-output {:result-type :field-metadata:field_id field-id:value_metadata value-metadata}}- (let [result (metabot.tools.field-stats/field-values- {:entity-type "table", :entity-id table-id, :field-id field-id, :limit 5})]- ;; strip skewness to avoid platform-dependent floating-point mismatch- (cond-> result- (get-in result [:structured-output :value_metadata :statistics :skewness])- (update-in [:structured-output :value_metadata :statistics] dissoc :skewness))))+ (metabot.tools.field-stats/field-values+ {:entity-type "table", :entity-id table-id, :field-id field-id, :limit 5}))people-id birth-date-id {:statistics{:distinct-count 2308:percent-null 0.0