Skip to content

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

Fix: Cannot add filters on boolean MySQL columns when the data source is a model (#67123)

github-actions[bot]276d6532d80e
diff --git a/docs/developers-guide/driver-changelog.md b/docs/developers-guide/driver-changelog.md
index 54976a09ef1..28ca2e6c46e 100644
--- a/docs/developers-guide/driver-changelog.md
+++ b/docs/developers-guide/driver-changelog.md
@@ -4,6 +4,10 @@ title: Driver interface changelog
# Driver Interface Changelog
+## Metabase 0.59.0
+
+- Added `sql-jdbc.execute/db-type-name` multimethod. Override this if something more than the default is needed in your sql-jdbc-based driver. See the `:mysql` implementation as an example.
+
## Metabase 0.58.0
- Added a `:collate` feature for drivers that support collation settings on text fields
diff --git a/src/metabase/driver/mysql.clj b/src/metabase/driver/mysql.clj
index aea99da33ee..1af86f60d1d 100644
--- a/src/metabase/driver/mysql.clj
+++ b/src/metabase/driver/mysql.clj
@@ -1170,3 +1170,22 @@
(defmethod driver.sql/default-schema :mysql
[_]
nil)
+
+;; Override db-type-name to handle tinyint(1) as boolean
+;; During sync, tinyint(1) is mapped to BIT (boolean) unless tinyInt1isBit=false is set
+;; We need to do the same during query execution to ensure type consistency
+(defmethod sql-jdbc.execute/db-type-name :mysql
+ [_driver ^ResultSetMetaData rsmeta column-index]
+ (let [db (try
+ (driver-api/database (driver-api/metadata-provider))
+ (catch Throwable _ nil))
+ tiny-int-1-is-bit? (not (some-> db :details :additional-options (str/includes? "tinyInt1isBit=false")))
+ db-type-name (.getColumnTypeName rsmeta column-index)
+ precision (try
+ (.getPrecision rsmeta column-index)
+ (catch Throwable _ nil))]
+ (if (and (= "TINYINT" db-type-name)
+ (= precision 1)
+ tiny-int-1-is-bit?)
+ "BIT"
+ db-type-name)))
diff --git a/src/metabase/driver/sql_jdbc/execute.clj b/src/metabase/driver/sql_jdbc/execute.clj
index 5499a322d86..4d2ceb56299 100644
--- a/src/metabase/driver/sql_jdbc/execute.clj
+++ b/src/metabase/driver/sql_jdbc/execute.clj
@@ -174,6 +174,12 @@
driver/dispatch-on-initialized-driver
:hierarchy #'driver/hierarchy)
+(defmulti db-type-name
+ "Return the type name of a column given the rsmeta and the column-index. The default implementation fetches this information from rsmeta."
+ {:added "0.59.0" :arglists '([driver ^java.sql.ResultSetMetaData rsmeta column-index])}
+ driver/dispatch-on-initialized-driver
+ :hierarchy #'driver/hierarchy)
+
(defmulti column-metadata
"Return a sequence of maps containing information about the corresponding columns in query results. The default
implementation fetches this information via the result set metadata. It is unlikely you will need to override this."
@@ -687,12 +693,16 @@
metadatas))
metadatas))
+(defmethod db-type-name :sql-jdbc
+ [_driver ^ResultSetMetaData rsmeta column-index]
+ (.getColumnTypeName rsmeta column-index))
+
(defmethod column-metadata :sql-jdbc
[driver ^ResultSetMetaData rsmeta]
(->> (mapv
(fn [^Long i]
(let [col-name (.getColumnLabel rsmeta i)
- db-type-name (.getColumnTypeName rsmeta i)
+ db-type-name (db-type-name driver rsmeta i)
base-type (sql-jdbc.sync.interface/database-type->base-type driver (keyword db-type-name))]
(log/tracef "Column %d '%s' is a %s which is mapped to base type %s for driver %s\n"
i col-name db-type-name base-type driver)
diff --git a/test/metabase/driver/mysql_test.clj b/test/metabase/driver/mysql_test.clj
index 6a16ab7d4d4..c4341b5c3f3 100644
--- a/test/metabase/driver/mysql_test.clj
+++ b/test/metabase/driver/mysql_test.clj
@@ -5,6 +5,7 @@
[clojure.string :as str]
[clojure.test :refer :all]
[honey.sql :as sql]
+ [medley.core :as m]
[metabase.actions.error :as actions.error]
[metabase.actions.models :as action]
[metabase.driver :as driver]
@@ -20,8 +21,10 @@
[metabase.driver.sql-jdbc.sync.describe-database :as sql-jdbc.describe-database]
[metabase.driver.sql-jdbc.sync.interface :as sql-jdbc.sync.interface]
[metabase.driver.sql.query-processor :as sql.qp]
+ [metabase.lib.card :as lib.card]
[metabase.lib.core :as lib]
[metabase.lib.metadata :as lib.metadata]
+ [metabase.lib.test-util :as lib.tu]
[metabase.query-processor :as qp]
[metabase.query-processor.compile :as qp.compile]
^{:clj-kondo/ignore [:deprecated-namespace]} [metabase.query-processor.store :as qp.store]
@@ -164,6 +167,31 @@
{:name "thing", :base_type :type/Text, :semantic_type :type/Category}}
(db->fields db))))))))
+(deftest tinyint-1-model-filter-test
+ (mt/test-driver :mysql
+ (mt/dataset tiny-int-ones
+ ;; trigger a full sync on this database so fields are categorized correctly
+ (sync/sync-database! (mt/db))
+ (testing "Can filter on TINYINT(1) boolean columns in models (metabase#65826)"
+ (let [mp (as-> (mt/metadata-provider) $mp
+ (lib.tu/mock-metadata-provider $mp {:cards
+ [{:id 1
+ :name "Test Model"
+ :database-id (mt/id)
+ :type :model
+ :dataset-query (lib/query $mp (lib.metadata/table $mp (mt/id :number-of-cans)))}]}))
+ field-metadata (lib.card/saved-question-metadata mp 1)
+ boolean-col (m/find-first #(= (:name %) "number-of-cans")
+ field-metadata)]
+ (testing "Model has boolean metadata"
+ (is (= :type/Boolean (:base-type boolean-col))))
+
+ (testing "Can query model with boolean filter"
+ (let [query (as-> (lib/query mp (lib.metadata/card mp 1)) $q
+ (lib/filter $q (lib/= (m/find-first #(= (:name %) "number-of-cans") (lib/fieldable-columns $q))
+ true)))]
+ (is (some? (mt/rows (qp/process-query query)))))))))))
+
(tx/defdataset year-db
[["years"
[{:field-name "year_column", :base-type {:native "YEAR"}, :effective-type :type/Date}]