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
convert mysql booleans to real booleans during semantic indexing (#62578)
Nicola Mometto7ea8fc3b61fe
diff --git a/enterprise/backend/src/metabase_enterprise/semantic_search/index.clj b/enterprise/backend/src/metabase_enterprise/semantic_search/index.cljindex 34c04f11568..dad354b6030 100644--- a/enterprise/backend/src/metabase_enterprise/semantic_search/index.clj+++ b/enterprise/backend/src/metabase_enterprise/semantic_search/index.clj@@ -103,6 +103,17 @@:else (Instant/ofEpochMilli (inst-ms document-timestamp))))+(defn- to-boolean+ "MySQL booleans are represented as 0/1, so we must ensure we're casting them to+ real booleans when inserting them into our postgres db"+ [b]+ {:pre [(some? b)]}+ (cond+ (boolean? b) b+ (= 0 b) false+ (= 1 b) true+ :else (throw (ex-info "Unexpected boolean value" {:v b}))))+(defn- doc->db-record"Convert a document to a database record with a provided embedding."[embedding-vec {:keys [model id searchable_text created_at creator_id updated_at@@ -117,10 +128,10 @@:name (:name doc):content searchable_text:display_type display_type- :archived archived- :official_collection official_collection- :pinned pinned- :verified verified+ :archived (some-> archived to-boolean)+ :official_collection (some-> official_collection to-boolean)+ :pinned (some-> pinned to-boolean)+ :verified (some-> verified to-boolean):dashboardcard_count dashboardcard_count:view_count view_count:model_created_at (some-> created_at to-instant)diff --git a/enterprise/backend/test/metabase_enterprise/semantic_search/index_test.clj b/enterprise/backend/test/metabase_enterprise/semantic_search/index_test.cljindex 31d07921365..ac473750bb3 100644--- a/enterprise/backend/test/metabase_enterprise/semantic_search/index_test.clj+++ b/enterprise/backend/test/metabase_enterprise/semantic_search/index_test.clj@@ -516,6 +516,56 @@(let [non-existent-docs [{:id "99999:123" :model "indexed-entity" :content "Non-existent"}]](is (= [] (#'semantic.index/filter-can-read-indexed-entity non-existent-docs))))))))))+(deftest to-boolean-test+ (testing "to-boolean function correctly converts various input types to booleans"+ (testing "boolean inputs are returned unchanged"+ (is (true? (#'semantic.index/to-boolean true)))+ (is (false? (#'semantic.index/to-boolean false))))++ (testing "MySQL-style integer booleans are converted correctly"+ (is (false? (#'semantic.index/to-boolean 0)))+ (is (true? (#'semantic.index/to-boolean 1))))))++(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]+ base-doc {:model "card"+ :id "123"+ :searchable_text "test content"+ :creator_id 1}]++ (testing "MySQL-style integer booleans are converted to real booleans"+ (let [doc-with-mysql-booleans (assoc base-doc+ :archived 0+ :official_collection 1+ :pinned 0+ :verified 1)+ result (#'semantic.index/doc->db-record embedding-vec doc-with-mysql-booleans)]+ (is (false? (:archived result)))+ (is (true? (:official_collection result)))+ (is (false? (:pinned result)))+ (is (true? (:verified result)))))++ (testing "real boolean values are preserved"+ (let [doc-with-real-booleans (assoc base-doc+ :archived true+ :official_collection false+ :pinned true+ :verified false)+ result (#'semantic.index/doc->db-record embedding-vec doc-with-real-booleans)]+ (is (true? (:archived result)))+ (is (false? (:official_collection result)))+ (is (true? (:pinned result)))+ (is (false? (:verified result)))))++ (testing "nil boolean fields are handled correctly"+ (let [doc-with-nil-booleans base-doc+ result (#'semantic.index/doc->db-record embedding-vec doc-with-nil-booleans)]+ (is (nil? (:archived result)))+ (is (nil? (:official_collection result)))+ (is (nil? (:pinned result)))+ (is (nil? (:verified result))))))))+(deftest indexed-entity-collapse-id-test(mt/with-premium-features #{:semantic-search}(mt/with-temp [:model/Collection {coll-id :id} {}