Skip to content

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

Custom test on connection checkout for clickhouse (#68808)

Riley Thompson798fc4aff61a
diff --git a/enterprise/backend/test/metabase_enterprise/impersonation/driver_test.clj b/enterprise/backend/test/metabase_enterprise/impersonation/driver_test.clj
index 4a1e9faf4a6..5b4dc741210 100644
--- a/enterprise/backend/test/metabase_enterprise/impersonation/driver_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/impersonation/driver_test.clj
@@ -836,3 +836,37 @@
java.lang.Exception
(mt/run-mbql-query checkins
{:aggregation [[:count]]}))))))))))))
+
+;; TODO(rileythomp, 2026-01-28): Verify this test can be removed after upgrading ClickHouse JDBC driver past 0.8.4
+(deftest clickhouse-double-hyphen-role-doesnt-break-all-queries-test
+ (testing "using an unknown role on clickhouse won't break queries for other users (68674)"
+ (mt/test-driver :clickhouse
+ (mt/with-premium-features #{:advanced-permissions}
+ (let [venues-table (sql.tx/qualify-and-quote driver/*driver* "test-data" "venues")
+ role-a "role--with--double--hyphens"]
+ (tx/with-temp-roles! driver/*driver*
+ (impersonation-granting-details driver/*driver* (mt/db))
+ {role-a {venues-table {}}}
+ (impersonation-default-user driver/*driver*)
+ (impersonation-default-role driver/*driver*)
+ (mt/with-temp [:model/Database database {:engine driver/*driver*,
+ :details (impersonation-details driver/*driver* (mt/db))}]
+ (mt/with-db database
+ (when (driver/database-supports? driver/*driver* :connection-impersonation-requires-role nil)
+ (t2/update! :model/Database :id (mt/id) (assoc-in (mt/db) [:details :role] (impersonation-default-role driver/*driver*))))
+ (sync/sync-database! database {:scan :schema})
+ (impersonation.util-test/with-impersonations! {:impersonations [{:db-id (mt/id) :attribute "impersonation_attr"}]
+ :attributes {"impersonation_attr" role-a}}
+ (testing "using double hyphen role causes an error"
+ (is (thrown?
+ java.lang.Exception
+ (mt/run-mbql-query venues
+ {:aggregation [[:count]]}))))
+ (testing "admin should still be able to query"
+ (request/as-admin
+ (is (= [100]
+ (map
+ long
+ (mt/first-row
+ (mt/run-mbql-query venues
+ {:aggregation [[:count]]}))))))))))))))))
diff --git a/modules/drivers/clickhouse/src/metabase/driver/clickhouse.clj b/modules/drivers/clickhouse/src/metabase/driver/clickhouse.clj
index d9e8203f063..d8d5d4a51f2 100644
--- a/modules/drivers/clickhouse/src/metabase/driver/clickhouse.clj
+++ b/modules/drivers/clickhouse/src/metabase/driver/clickhouse.clj
@@ -351,3 +351,11 @@
(defmethod sql-jdbc.execute/set-parameter [:clickhouse LocalDate]
[_ ^PreparedStatement prepared-statement i object]
(.setObject prepared-statement i object))
+
+(defmethod sql-jdbc.conn/data-warehouse-connection-pool-properties :clickhouse
+ [driver database]
+ (merge
+ ((get-method sql-jdbc.conn/data-warehouse-connection-pool-properties :sql-jdbc) driver database)
+ ;; TODO(rileythomp, 2026-01-29): Remove this once we upgrade past 0.8.4
+ ;; This is to work around 68674 where connections are being poisoned with bad roles
+ {"preferredTestQuery" "SELECT 1"}))