Compare commits

...

1 Commits

Author SHA1 Message Date
Phil Hagelberg
d9bcd10b4f WIP integer to boolean coercion for SQLite; doesn't work. 2026-06-18 09:33:31 -07:00
4 changed files with 28 additions and 1 deletions

View File

@@ -223,6 +223,9 @@
(ex-info (str "Cannot convert " (pr-str value) " to float.")
{:value value}))))
(defn- integer->boolean [expr]
(h2x/cast :boolean expr))
(defmulti date-dbtype
"Return the name of the date type we convert to in this database."
{:added "0.55.0" :arglists '([driver])}
@@ -802,7 +805,7 @@
(throw (Exception. (tru "No magnitude known for {0}" coercion-type)))))
(defn cast-field-if-needed
"Wrap a `field-identifier` in appropriate HoneySQL expressions if it refers to a UNIX timestamp Field."
"Wrap a `field-identifier` in appropriate HoneySQL expressions if it needs casting."
[driver {:keys [base-type coercion-strategy], :as field} honeysql-form]
(if (some #(str/includes? (name %) "_") (keys field))
(do
@@ -817,6 +820,9 @@
(semantic-type->unix-timestamp-unit coercion-strategy)
honeysql-form)
(and (isa? base-type :type/Number) (isa? coercion-strategy :Coercion/Integer->Boolean))
(integer->boolean honeysql-form)
(and (= base-type :type/Text) (isa? coercion-strategy :Coercion/String->Temporal))
(cast-temporal-string driver coercion-strategy honeysql-form)

View File

@@ -365,6 +365,8 @@
(derive :Coercion/UNIXMicroSeconds->DateTime :Coercion/UNIXTime->Temporal)
(derive :Coercion/UNIXNanoSeconds->DateTime :Coercion/UNIXTime->Temporal)
(derive :Coercion/Integer->Boolean :Coercion/*)
(derive :Coercion/Temporal->Temporal :Coercion/*)
(derive :Coercion/DateTime->Date :Coercion/Temporal->Temporal)
@@ -482,6 +484,8 @@
(coercion-hierarchies/define-types! :Coercion/ISO8601->DateTime :type/Text :type/DateTime)
(coercion-hierarchies/define-types! :Coercion/ISO8601->Time :type/Text :type/Time)
(coercion-hierarchies/define-types! :Coercion/Integer->Boolean :type/Integer :type/Boolean)
(coercion-hierarchies/define-types! :Coercion/YYYYMMDDHHMMSSString->Temporal :type/Text :type/DateTime)
(coercion-hierarchies/define-types! :Coercion/DateTime->Date :type/DateTime :type/Date)

View File

@@ -51,6 +51,21 @@
(is (= res
(biginteger coerced-number))))))))))
(deftest ^:parallel integer-to-boolean-coercion-test
(mt/test-drivers #{:sqlite}
(mt/dataset rounding-nums-db
(let [mp (lib.tu/merged-mock-metadata-provider
(mt/metadata-provider)
{:fields [{:id (mt/id :nums "int_col")
:coercion-strategy :Coercion/Integer->Boolean
:effective-type :type/Boolean}]})
query (-> (lib/query mp (lib.metadata/table mp (mt/id :nums)))
(lib/with-fields [(lib.metadata/field mp (mt/id :nums :int_col))]))]
(let [rows (mt/rows (qp.store/with-metadata-provider mp
(qp/process-query query)))]
(is (false? (ffirst rows)))
(is (true? (fnext rows))))))))
(deftest ^:parallel float-to-integer-coercion-test
(mt/test-drivers
(mt/normal-drivers)

View File

@@ -56,6 +56,7 @@
(deftest ^:parallel coercion-possibilities-test
(is (= {:type/Text #{::Coerce-Int-To-Str}
:type/Boolean #{:Coercion/Integer->Boolean}
:type/Instant #{:Coercion/UNIXNanoSeconds->DateTime
:Coercion/UNIXMicroSeconds->DateTime
:Coercion/UNIXMilliSeconds->DateTime
@@ -68,6 +69,7 @@
(types/coercion-possibilities :type/Decimal)))
(testing "Should work for for subtypes of a the coercion base type(s)"
(is (= {:type/Text #{::Coerce-Int-To-Str}
:type/Boolean #{:Coercion/Integer->Boolean}
:type/Instant #{:Coercion/UNIXNanoSeconds->DateTime
:Coercion/UNIXMicroSeconds->DateTime
:Coercion/UNIXMilliSeconds->DateTime