Skip to content

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

Treat postgres bit strings as text (#66618)

Riley Thompsona173a45e8076
diff --git a/src/metabase/driver/postgres.clj b/src/metabase/driver/postgres.clj
index 3a89def0666..c6affc6f288 100644
--- a/src/metabase/driver/postgres.clj
+++ b/src/metabase/driver/postgres.clj
@@ -587,6 +587,7 @@
(let [[_ raw-value {base-type :base_type, database-type :database_type}] value]
(when (some? raw-value)
(condp #(isa? %2 %1) base-type
+ :type/PostgresBitString (h2x/cast :varbit raw-value)
:type/IPAddress (h2x/cast :inet raw-value)
:type/PostgresEnum (if (quoted? database-type)
(h2x/cast database-type raw-value)
@@ -807,7 +808,7 @@
{:array :type/*
:bigint :type/BigInteger
:bigserial :type/BigInteger
- :bit :type/*
+ :bit :type/PostgresBitString
:bool :type/Boolean
:boolean :type/Boolean
:box :type/*
@@ -856,10 +857,10 @@
:tsvector :type/*
:txid_snapshot :type/*
:uuid :type/UUID
- :varbit :type/*
+ :varbit :type/PostgresBitString
:varchar :type/Text
:xml :type/Structured
- (keyword "bit varying") :type/*
+ (keyword "bit varying") :type/PostgresBitString
(keyword "character varying") :type/Text
(keyword "double precision") :type/Float
(keyword "time with time zone") :type/Time
@@ -1007,6 +1008,15 @@
(when-let [bytes (.getBytes rs i)]
(str "\\x" (String. (Hex/encodeHex bytes))))))
+(defmethod sql-jdbc.execute/read-column-thunk [:postgres Types/BIT]
+ [_driver ^ResultSet rs ^ResultSetMetaData rsmeta ^Integer i]
+ ;; convert bit strings to strings, leave booleans as objects
+ (if (= "bit" (.getColumnTypeName rsmeta i))
+ (fn []
+ (.getString rs i))
+ (fn []
+ (.getObject rs i))))
+
;; de-CLOB any CLOB values that come back
(defmethod sql-jdbc.execute/read-column-thunk :postgres
[_ ^ResultSet rs _ ^Integer i]
diff --git a/src/metabase/types/core.cljc b/src/metabase/types/core.cljc
index ce23d22205f..84793f11f98 100644
--- a/src/metabase/types/core.cljc
+++ b/src/metabase/types/core.cljc
@@ -183,6 +183,7 @@
(derive :type/Comment :type/Text)
(derive :type/PostgresEnum :type/TextLike)
+(derive :type/PostgresBitString :type/TextLike)
(derive :type/OracleCLOB :type/Text)
(derive :type/OracleCLOB :type/Large)
diff --git a/test/metabase/driver/postgres_test.clj b/test/metabase/driver/postgres_test.clj
index 05680bd33a5..d8644636109 100644
--- a/test/metabase/driver/postgres_test.clj
+++ b/test/metabase/driver/postgres_test.clj
@@ -1968,21 +1968,22 @@
(qp/process-query)
(mt/rows)))))))))
+(defn- pg-obj [type val]
+ (doto (PGobject.) (.setType type) (.setValue val)))
+
(deftest pgobject-freeze-thaw-test
- (letfn [(make-pgobject [type value]
- (doto (PGobject.) (.setType type) (.setValue value)))
- (test-pgobject-caching [obj]
+ (letfn [(test-pgobject-caching [obj]
(is (= obj (-> obj nippy/freeze nippy/thaw))))]
(testing "Simple PGobject instances can be frozen and thawed"
- (test-pgobject-caching (make-pgobject "foo_type" "abc_val")))
+ (test-pgobject-caching (pg-obj "foo_type" "abc_val")))
(testing "PGobjects in an array can be frozen and thawed"
- (let [pg-obj-1 (make-pgobject "foo_type" "abc_val")
- pg-obj-2 (make-pgobject "foo_type" "xyz_val")
+ (let [pg-obj-1 (pg-obj "foo_type" "abc_val")
+ pg-obj-2 (pg-obj "foo_type" "xyz_val")
pg-obj-arr [pg-obj-1 pg-obj-2]]
(test-pgobject-caching pg-obj-arr)))
(testing "PGobjects in a map can be frozen and thawed"
- (let [pg-obj (make-pgobject "foo_type" "abc_val")
- pg-obj-map {:data [pg-obj] :metadata {:type "test"}}]
+ (let [pg-object (pg-obj "foo_type" "abc_val")
+ pg-obj-map {:data [pg-object] :metadata {:type "test"}}]
(test-pgobject-caching pg-obj-map)))))
(deftest canceled-query-no-stacktrace-test
@@ -2037,3 +2038,70 @@
(is (= "ERROR: canceling statement due to user request" (:error response)))
(let [bad-messages (into [] (cancel-messages) (messages))]
(is (empty? bad-messages)))))))))
+
+(deftest bit-strings-can-be-filtered
+ (mt/test-driver :postgres
+ (mt/dataset (mt/dataset-definition
+ "bit_string_dataset"
+ [["bit_string_table"
+ [{:field-name "single_bit", :base-type {:native "BIT"}}
+ {:field-name "fixed_bit", :base-type {:native "BIT(8)"}}
+ {:field-name "var_bit", :base-type {:native "VARBIT(16)"}}
+ {:field-name "bit_varying", :base-type {:native "BIT VARYING(32)"}}]
+ [[(pg-obj "BIT" "1") (pg-obj "BIT" "10101010") (pg-obj "BIT" "1101") (pg-obj "BIT" "111000111")]
+ [(pg-obj "BIT" "0") (pg-obj "BIT" "00001111") (pg-obj "BIT" "10101") (pg-obj "BIT" "1001001")]
+ [nil nil nil nil]]]])
+ (let [mp (mt/metadata-provider)
+ base-query (lib/query mp (lib.metadata/table mp (mt/id :bit_string_table)))]
+ (testing "can query all rows"
+ (is (= [[1 "1" "10101010" "1101" "111000111"]
+ [2 "0" "00001111" "10101" "1001001"]
+ [3 nil nil nil nil]]
+ (mt/rows (qp/process-query base-query)))))
+ (testing "can filter by single_bit column"
+ (let [single-bit-col (mt/id :bit_string_table :single_bit)
+ query (-> base-query
+ (lib/filter (lib/= (lib.metadata/field mp single-bit-col)
+ "1")))]
+ (is (= [[1 "1" "10101010" "1101" "111000111"]]
+ (mt/rows (qp/process-query query))))))
+ (testing "can filter by fixed_bit column"
+ (let [fixed-bit-col (mt/id :bit_string_table :fixed_bit)
+ query (-> base-query
+ (lib/filter (lib/= (lib.metadata/field mp fixed-bit-col)
+ "00001111")))]
+ (is (= [[2 "0" "00001111" "10101" "1001001"]]
+ (mt/rows (qp/process-query query))))))
+ (testing "can filter by var_bit column"
+ (let [var-bit-col (mt/id :bit_string_table :var_bit)
+ query (-> base-query
+ (lib/filter (lib/= (lib.metadata/field mp var-bit-col)
+ "1101")))]
+ (is (= [[1 "1" "10101010" "1101" "111000111"]]
+ (mt/rows (qp/process-query query))))))
+ (testing "can filter by bit_varying column"
+ (let [bit-varying-col (mt/id :bit_string_table :bit_varying)
+ query (-> base-query
+ (lib/filter (lib/= (lib.metadata/field mp bit-varying-col)
+ "1001001")))]
+ (is (= [[2 "0" "00001111" "10101" "1001001"]]
+ (mt/rows (qp/process-query query))))))
+ (let [fixed-bit-col (mt/id :bit_string_table :fixed_bit)]
+ (testing "can filter with not equals"
+ (let [query (-> base-query
+ (lib/filter (lib/!= (lib.metadata/field mp fixed-bit-col)
+ "00001111")))]
+ (is (= [[1 "1" "10101010" "1101" "111000111"]
+ [3 nil nil nil nil]]
+ (mt/rows (qp/process-query query))))))
+ (testing "can filter with is empty"
+ (let [query (-> base-query
+ (lib/filter (lib/is-empty (lib.metadata/field mp fixed-bit-col))))]
+ (is (= [[3 nil nil nil nil]]
+ (mt/rows (qp/process-query query))))))
+ (testing "can filter with is not empty"
+ (let [query (-> base-query
+ (lib/filter (lib/not-empty (lib.metadata/field mp fixed-bit-col))))]
+ (is (= [[1 "1" "10101010" "1101" "111000111"]
+ [2 "0" "00001111" "10101" "1001001"]]
+ (mt/rows (qp/process-query query)))))))))))