Skip to content

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

Handle clickhouse dbname when it has spaces in it (#70822)

Riley Thompsonbfdd8a0b236e
diff --git a/modules/drivers/clickhouse/src/metabase/driver/clickhouse.clj b/modules/drivers/clickhouse/src/metabase/driver/clickhouse.clj
index 272b54e2160..65fbf3605ec 100644
--- a/modules/drivers/clickhouse/src/metabase/driver/clickhouse.clj
+++ b/modules/drivers/clickhouse/src/metabase/driver/clickhouse.clj
@@ -1,5 +1,6 @@
(ns metabase.driver.clickhouse
"Driver for ClickHouse databases"
+ (:refer-clojure :exclude [not-empty])
(:require
[clojure.java.jdbc :as jdbc]
[clojure.string :as str]
@@ -19,7 +20,8 @@
[metabase.driver.sql.util :as sql.u]
[metabase.driver.util :as driver.u]
[metabase.util :as u]
- [metabase.util.log :as log])
+ [metabase.util.log :as log]
+ [metabase.util.performance :refer [not-empty]])
(:import
(com.clickhouse.client.api.query QuerySettings)
(java.sql Connection SQLException Statement PreparedStatement)
@@ -107,6 +109,11 @@
default-connection-details
details)
{:keys [user password dbname host port ssl clickhouse-settings max-open-connections]} details
+ ;; Handling legacy `dbname` values here. `dbname` used to be a space-separated string of
+ ;; the database names. These `dbname` values weren't migrated so they still need to be handled
+ ;; here. This is the original version that takes the first db. This value is just the default
+ ;; db, we hit other dbs by including the db in the queries (#70798).
+ dbname (first (str/split (str/trim dbname) #" "))
host (cond ; JDBCv1 used to accept schema in the `host` configuration option
(str/starts-with? host "http://") (subs host 7)
(str/starts-with? host "https://") (subs host 8)
@@ -141,7 +148,8 @@
;; Default SELECT 1 is not enough for Metabase test suite,
;; as it works slightly differently than expected there
(let [spec (sql-jdbc.conn/connection-details->spec driver details)
- db (ddl.i/format-name driver (or (:dbname details) (:db details) "default"))]
+ dbname (first (str/split (str/trim (or (not-empty (:dbname details)) (:db details) "default")) #" "))
+ db (ddl.i/format-name driver dbname)]
(sql-jdbc.execute/do-with-connection-with-options
driver spec nil
(fn [^java.sql.Connection conn]
diff --git a/modules/drivers/clickhouse/test/metabase/driver/clickhouse_test.clj b/modules/drivers/clickhouse/test/metabase/driver/clickhouse_test.clj
index c85a873bb82..953936eed26 100644
--- a/modules/drivers/clickhouse/test/metabase/driver/clickhouse_test.clj
+++ b/modules/drivers/clickhouse/test/metabase/driver/clickhouse_test.clj
@@ -487,6 +487,18 @@
(qp/process-query)
(mt/rows))))))))))
+(deftest ^:parallel handle-db-names-with-spaces-test
+ (mt/test-driver :clickhouse
+ (are [dbname exp-name] (let [details (assoc (:details (mt/db)) :dbname dbname)
+ spec (sql-jdbc.conn/connection-details->spec :clickhouse details)]
+ (is (true? (driver/can-connect? :clickhouse details)))
+ (is (= (format "//localhost:8123/%s" exp-name)
+ (:subname spec))))
+ "test_data default fake_db" "test_data"
+ "test_data" "test_data"
+ "" ""
+ nil "default")))
+
;; TODO (lbrdnk 2026-01-23): Excplicit exceptions from [[metabase.driver.util/parsed-query]] are shutdown
;; at the moment to avoid potential log flooding. We should revisit this during further
;; parsing work.