Skip to content

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

Fix postgres bytea truncation (#65569)

Riley Thompson1a827eb9a0cf
diff --git a/src/metabase/driver/postgres.clj b/src/metabase/driver/postgres.clj
index 86e738364c8..bfffe074f01 100644
--- a/src/metabase/driver/postgres.clj
+++ b/src/metabase/driver/postgres.clj
@@ -43,6 +43,7 @@
ResultSetMetaData
Types)
(java.time LocalDateTime OffsetDateTime OffsetTime)
+ (org.apache.commons.codec.binary Hex)
(org.postgresql.copy CopyManager)
(org.postgresql.jdbc PgConnection)))
@@ -997,6 +998,13 @@
[_driver ^ResultSet rs ^ResultSetMetaData _rsmeta ^Integer i]
(fn [] (.getString rs i)))
+;; Handle bytea columns to avoid truncation (#30671)
+(defmethod sql-jdbc.execute/read-column-thunk [:postgres Types/BINARY]
+ [_driver ^ResultSet rs ^ResultSetMetaData _rsmeta ^Integer i]
+ (fn []
+ (when-let [bytes (.getBytes rs i)]
+ (str "\\x" (String. (Hex/encodeHex bytes))))))
+
;; de-CLOB any CLOB values that come back
(defmethod sql-jdbc.execute/read-column-thunk :postgres
[_ ^ResultSet rs _ ^Integer i]
diff --git a/test/metabase/driver/postgres_test.clj b/test/metabase/driver/postgres_test.clj
index f2a83e2e2ff..a7a450b40f8 100644
--- a/test/metabase/driver/postgres_test.clj
+++ b/test/metabase/driver/postgres_test.clj
@@ -1928,8 +1928,7 @@
"(-89.0,-179.0)"
"<(1.0,1.0),50.0>"
"'1':4 'content':1 'row':3"
- ;; match on type
- bytes?
+ "\\xc4ca4238a0b923820dcc509a6f75849b"
"[1,101)"
;; match on type
decimal?
@@ -1937,3 +1936,27 @@
;; doesnt error then it worked
string?]
(-> results :data :rows deref first)))))))
+
+(defn- hex->bytes [hex]
+ (->> (partition 2 hex)
+ (map #(apply str %))
+ (map #(Integer/parseInt % 16))
+ (map unchecked-byte)
+ byte-array))
+
+(deftest bytea-column-not-truncated-test
+ (testing "fix for #30671"
+ (mt/test-driver :postgres
+ (mt/dataset (mt/dataset-definition
+ "bytea_dataset"
+ [["bytea_table"
+ [{:field-name "bytea_col", :base-type {:native "bytea"}, :effective-type :type/*}]
+ [[(hex->bytes "900000000000810074123E9DB008AB5C")]
+ [(hex->bytes "900000000000475000DE4EC0C0A920AB")]]]])
+ (let [mp (mt/metadata-provider)]
+ (is (= [[1 "\\x900000000000810074123e9db008ab5c"]
+ [2 "\\x900000000000475000de4ec0c0a920ab"]]
+ (->> (lib.metadata/table mp (mt/id :bytea_table))
+ (lib/query mp)
+ (qp/process-query)
+ (mt/rows)))))))))