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
Defensively support double-encoded search index rows (#74402)
Chris Truterb9d73cfd8e2a
diff --git a/enterprise/backend/src/metabase_enterprise/semantic_search/index.clj b/enterprise/backend/src/metabase_enterprise/semantic_search/index.cljindex 24195268cb6..b0e001ed766 100644--- a/enterprise/backend/src/metabase_enterprise/semantic_search/index.clj+++ b/enterprise/backend/src/metabase_enterprise/semantic_search/index.clj@@ -708,7 +708,12 @@(defn- decode-legacy-input"Decode `row`s `:legacy_input` JSONB PGobject into a Clojure map."[row]- (update row :legacy_input decode-pgobject))+ ;; BOT-1543: some existing rows have legacy_input stored as a JSON string rather than a JSON+ ;; object, so one decode yields a string; decode once more in that case.+ (update row :legacy_input+ (fn [pgo]+ (let [decoded (decode-pgobject pgo)]+ (cond-> decoded (string? decoded) json/decode+kw)))));; Search-models whose `mi/can-read?` is a pure function of `:collection_id`, which is;; already denormalized on the index row. Values are the Toucan model whose `can-read?`diff --git a/enterprise/backend/test/metabase_enterprise/semantic_search/index_test.clj b/enterprise/backend/test/metabase_enterprise/semantic_search/index_test.cljindex 8b38b9c0430..a682d6f951b 100644--- a/enterprise/backend/test/metabase_enterprise/semantic_search/index_test.clj+++ b/enterprise/backend/test/metabase_enterprise/semantic_search/index_test.clj@@ -12,7 +12,11 @@[metabase.collections.models.collection :as collection][metabase.models.interface :as mi][metabase.test :as mt]- [metabase.util :as u]))+ [metabase.util :as u])+ (:import+ (org.postgresql.util PGobject)))++(set! *warn-on-reflection* true)(use-fixtures :once #'semantic.tu/once-fixture)@@ -559,6 +563,17 @@(is (false? (#'semantic.index/to-boolean 0)))(is (true? (#'semantic.index/to-boolean 1))))))+(deftest decode-legacy-input-test+ (letfn [(pgo [s] (doto (PGobject.) (.setType "jsonb") (.setValue s)))]+ (testing "JSONB object value decodes to a map"+ (is (= {:legacy_input {:name "Top 10" :model "card"}}+ (#'semantic.index/decode-legacy-input+ {:legacy_input (pgo "{\"name\":\"Top 10\",\"model\":\"card\"}")}))))+ (testing "BOT-1543: JSONB string value (double-encoded) is decoded again"+ (is (= {:legacy_input {:name "Pro" :model "collection"}}+ (#'semantic.index/decode-legacy-input+ {:legacy_input (pgo "\"{\\\"name\\\":\\\"Pro\\\",\\\"model\\\":\\\"collection\\\"}\"")}))))))+(deftest doc->db-record-boolean-conversion-test(testing "doc->db-record properly converts boolean fields using to-boolean"(let [embedding-vec [0.1 0.2 0.3]