Skip to content

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

Normalize horribly broken fingerprints (#63407)

Cam Saul34bddeb931b9
diff --git a/src/metabase/legacy_mbql/normalize.cljc b/src/metabase/legacy_mbql/normalize.cljc
index 33608755bfe..5676eebd9af 100644
--- a/src/metabase/legacy_mbql/normalize.cljc
+++ b/src/metabase/legacy_mbql/normalize.cljc
@@ -29,7 +29,6 @@
Token normalization occurs first, followed by canonicalization, followed by removing empty clauses."
(:require
- #?@(:cljs [[clojure.walk :as walk]])
[clojure.set :as set]
[medley.core :as m]
[metabase.legacy-mbql.predicates :as mbql.preds]
@@ -37,6 +36,7 @@
[metabase.legacy-mbql.util :as mbql.u]
[metabase.lib.normalize :as lib.normalize]
[metabase.lib.schema.expression.temporal :as lib.schema.expression.temporal]
+ [metabase.lib.schema.metadata.fingerprint :as lib.schema.metadata.fingerprint]
[metabase.lib.util.match :as lib.util.match]
[metabase.util :as u]
[metabase.util.i18n :as i18n]
@@ -393,6 +393,10 @@
[clause]
(-> clause normalize-tokens canonicalize-mbql-clauses))
+(mu/defn- normalize-fingerprint :- [:maybe ::lib.schema.metadata.fingerprint/fingerprint]
+ [fingerprint :- [:maybe :map]]
+ (lib.normalize/normalize ::lib.schema.metadata.fingerprint/fingerprint fingerprint))
+
(mu/defn normalize-source-metadata
"Normalize source/results metadata for a single column."
[metadata :- :map]
@@ -414,16 +418,10 @@
:unit
:lib/source) (keyword v)
:field_ref (normalize-field-ref v)
- :fingerprint (#?(:clj perf/keywordize-keys :cljs walk/keywordize-keys) v)
+ :fingerprint (normalize-fingerprint v)
:binning_info (m/update-existing v :binning_strategy keyword)
#_else
v)]
- ;; sanity check
- (when (= k :fingerprint)
- (when-let [base-type (first (keys (:type v)))]
- (assert (isa? base-type :type/*)
- (str "BAD FINGERPRINT! Invalid base-type: " (pr-str base-type) " " (pr-str v)))))
-
[k v]))))
metadata))
diff --git a/src/metabase/lib/schema/common.cljc b/src/metabase/lib/schema/common.cljc
index 862ffaf6aab..c6591f0532e 100644
--- a/src/metabase/lib/schema/common.cljc
+++ b/src/metabase/lib/schema/common.cljc
@@ -1,6 +1,7 @@
(ns metabase.lib.schema.common
(:require
[clojure.string :as str]
+ [medley.core :as m]
[metabase.types.core]
[metabase.util :as u]
[metabase.util.malli :as mu]
@@ -150,10 +151,30 @@
(defn- base-type? [x]
(isa? x :type/*))
+;;; only support fixing really broken types like `:type/creationtime` to `:type/CreationTime` in prod... Malli checks
+;;; will throw in dev. See [[metabase.lib.schema.common-test/normalize-base-type-test]] for more info
+
+(mu/defn- normalize-base-type* :- [:ref ::base-type]
+ [x]
+ (normalize-keyword x))
+
+(defn- normalize-base-type [x]
+ (when-let [k (normalize-base-type* x)]
+ (or (cond
+ (isa? k :type/*)
+ k
+
+ (and (= (namespace k) "type")
+ (= (u/lower-case-en (name k)) (name k)))
+ (m/find-first (fn [base-type]
+ (= (u/lower-case-en (name base-type)) (name k)))
+ (descendants :type/*)))
+ k)))
+
(mr/def ::base-type
[:and
[:keyword
- {:decode/normalize normalize-keyword}]
+ {:decode/normalize #'normalize-base-type}]
[:fn
{:error/message "valid base type"
:error/fn (fn [{:keys [value]} _]
diff --git a/test/metabase/legacy_mbql/normalize_test.cljc b/test/metabase/legacy_mbql/normalize_test.cljc
index 4bbdbb95730..8b5f41e3221 100644
--- a/test/metabase/legacy_mbql/normalize_test.cljc
+++ b/test/metabase/legacy_mbql/normalize_test.cljc
@@ -3,7 +3,8 @@
#?@(:cljs ([metabase.test-runner.assert-exprs.approximately-equal]))
[clojure.set :as set]
[clojure.test :as t]
- [metabase.legacy-mbql.normalize :as mbql.normalize]))
+ [metabase.legacy-mbql.normalize :as mbql.normalize]
+ [metabase.util.malli :as mu]))
#?(:cljs (comment metabase.test-runner.assert-exprs.approximately-equal/keep-me))
@@ -1664,3 +1665,18 @@
(mbql.normalize/normalize-tokens [:datetime "" {:mode "iso"}])))
(t/is (= [:datetime "" {:mode :iso}]
(mbql.normalize/normalize-tokens ["datetime" "" {"mode" "iso"}]))))
+
+(t/deftest ^:parallel normalize-evil-source-metadata-test
+ (t/testing "Fix really messed up fingerprints with lower-cased type names (only in prod) (#63397)"
+ (mu/disable-enforcement
+ (t/is (= {:fingerprint
+ {:global {:distinct-count 418, :nil% 0.0},
+ :type
+ {:type/Text
+ {:percent-json 0.0, :percent-url 0.0, :percent-email 0.0, :percent-state 0.0, :average-length 13.26388888888889}}}}
+ (#'mbql.normalize/normalize-source-metadata
+ {:fingerprint
+ {:global {:distinct-count 418, :nil% 0.0},
+ :type
+ {:type/text
+ {:percent-json 0.0, :percent-url 0.0, :percent-email 0.0, :percent-state 0.0, :average-length 13.26388888888889}}}}))))))
diff --git a/test/metabase/lib/schema/common_test.cljc b/test/metabase/lib/schema/common_test.cljc
index bcecdcc7498..bad7151ac66 100644
--- a/test/metabase/lib/schema/common_test.cljc
+++ b/test/metabase/lib/schema/common_test.cljc
@@ -1,8 +1,10 @@
(ns metabase.lib.schema.common-test
(:require
- [clojure.test :refer [are deftest testing]]
+ [clojure.test :refer [are deftest is testing]]
[malli.error :as me]
+ [metabase.lib.core :as lib]
[metabase.lib.schema.common :as lib.schema.common]
+ [metabase.util.malli :as mu]
[metabase.util.malli.registry :as mr]))
(deftest ^:parallel ip-address-test
@@ -10,3 +12,17 @@
(are [schema] (not (me/humanize (mr/explain schema :type/IPAddress)))
::lib.schema.common/base-type
::lib.schema.common/semantic-type)))
+
+(deftest ^:parallel normalize-base-type-test
+ (testing "Support normalizing really messed up base types like `:type/creationtime` => `:type/CreationTime` (#63397)"
+ (testing (str "Don't do this normalization in dev, because it's a major bug and we shouldn't encourage people to"
+ " write messed up code that needs this to be done to fix it")
+ #?(:clj
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"Invalid output: \[\"Not a valid base type: :type/creationtime, got: :type/creationtime\"\]"
+ (lib/normalize ::lib.schema.common/base-type "type/creationtime")))))
+ (testing "in prod (when Malli enforcement is off) we should be able to fix it"
+ (mu/disable-enforcement
+ (is (= :type/CreationTime
+ (lib/normalize ::lib.schema.common/base-type "type/creationtime")))))))