Skip to content

Commit f158f2b

Browse files
authored
Merge pull request #275 from ClickHouse/0.51.x
0.51.x
2 parents 7788eb2 + 642ab62 commit f158f2b

File tree

9 files changed

+55
-45
lines changed

9 files changed

+55
-45
lines changed

.github/workflows/check.yml

+5-13
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ on:
1010
pull_request:
1111

1212
env:
13-
METABASE_VERSION: v0.50.6
13+
METABASE_VERSION: v1.51.1.2
1414

1515
jobs:
1616
check-local-current-version:
@@ -23,19 +23,11 @@ jobs:
2323
ref: ${{ env.METABASE_VERSION }}
2424

2525
- name: Remove incompatible tests
26-
# dataset-definition-test tests test data definition,
27-
# and is currently failing for an unknown reason.
28-
# metabase.query-processor.middleware.permissions-test does not look like it is related to the driver at all,
29-
# but it is failing on the CI only.
30-
#
31-
# FIXME: metabase.models.card-test is failing as of 0.50.0;
32-
# it is unrelated to the driver, likely will be fixed in the future Metabase versions.
33-
# FIXME: metabase.models.dashboard-card-test disabled because it imports from metabase.models.card-test
26+
# metabase.events.view-log-test -> minor precision mismatch, still causes the assertion to fail
27+
# hooks.clojure.test-test -> :clickhouse is not in the set of the bundled drivers
3428
run: |
35-
echo "(ns metabase.test.data.dataset-definition-test)" > test/metabase/test/data/dataset_definition_test.clj
36-
echo "(ns metabase.query-processor.middleware.permissions-test)" > test/metabase/query_processor/middleware/permissions_test.clj
37-
echo "(ns metabase.models.card-test)" > test/metabase/models/card_test.clj
38-
echo "(ns metabase.models.dashboard-card-test)" > test/metabase/models/dashboard_card_test.clj
29+
echo "(ns metabase.events.view-log-test)" > test/metabase/events/view_log_test.clj
30+
echo "(ns hooks.clojure.test-test)" > .clj-kondo/test/hooks/clojure/test_test.clj
3931
4032
- name: Checkout Driver Repo
4133
uses: actions/checkout@v4

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 1.51.0
2+
3+
Adds Metabase 0.51.x support.
4+
15
# 1.50.7
26

37
### Improvements

docker-compose.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ services:
115115
##########################################################################################################
116116

117117
metabase:
118-
image: metabase/metabase-enterprise:v1.50.3
118+
image: metabase/metabase-enterprise:v1.51.1.2
119119
container_name: metabase-with-clickhouse-driver
120120
hostname: metabase
121121
environment:

resources/metabase-plugin.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
info:
22
name: Metabase ClickHouse Driver
3-
version: 1.50.7
3+
version: 1.51.0
44
description: Allows Metabase to connect to ClickHouse databases.
55
contact-info:
66
name: ClickHouse

src/metabase/driver/clickhouse.clj

+14-15
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#_{:clj-kondo/ignore [:unsorted-required-namespaces]}
44
(:require [clojure.core.memoize :as memoize]
55
[clojure.string :as str]
6-
[honey.sql :as sql]
76
[metabase.config :as config]
87
[metabase.driver :as driver]
98
[metabase.driver.clickhouse-introspection]
@@ -12,10 +11,10 @@
1211
[metabase.driver.clickhouse-version :as clickhouse-version]
1312
[metabase.driver.ddl.interface :as ddl.i]
1413
[metabase.driver.sql :as driver.sql]
14+
[metabase.driver.sql-jdbc :as sql-jdbc]
1515
[metabase.driver.sql-jdbc.common :as sql-jdbc.common]
1616
[metabase.driver.sql-jdbc.connection :as sql-jdbc.conn]
1717
[metabase.driver.sql-jdbc.execute :as sql-jdbc.execute]
18-
[metabase.driver.sql.query-processor :as sql.qp]
1918
[metabase.driver.sql.util :as sql.u]
2019
[metabase.lib.metadata :as lib.metadata]
2120
[metabase.query-processor.store :as qp.store]
@@ -29,22 +28,26 @@
2928
(driver/register! :clickhouse :parent #{:sql-jdbc})
3029

3130
(defmethod driver/display-name :clickhouse [_] "ClickHouse")
32-
(def ^:private product-name "metabase/1.50.7")
31+
(def ^:private product-name "metabase/1.51.0")
3332

3433
(defmethod driver/prettify-native-form :clickhouse
3534
[_ native-form]
3635
(sql.u/format-sql-and-fix-params :mysql native-form))
3736

3837
(doseq [[feature supported?] {:standard-deviation-aggregations true
39-
:foreign-keys (not config/is-test?)
4038
:now true
4139
:set-timezone true
4240
:convert-timezone true
4341
:test/jvm-timezone-setting false
42+
:test/time-type false
4443
:schemas true
4544
:datetime-diff true
4645
:upload-with-auto-pk false
47-
:window-functions/offset false}]
46+
:window-functions/offset false
47+
:window-functions/cumulative (not config/is-test?)
48+
:left-join (not config/is-test?)
49+
:describe-fks false
50+
:metadata/key-constraints false}]
4851
(defmethod driver/database-supports? [:clickhouse feature] [_driver _feature _db] supported?))
4952

5053
(def ^:private default-connection-details
@@ -190,7 +193,8 @@
190193

191194
(defmethod ddl.i/format-name :clickhouse
192195
[_ table-or-field-name]
193-
(str/replace table-or-field-name #"-" "_"))
196+
(when table-or-field-name
197+
(str/replace table-or-field-name #"-" "_")))
194198

195199
;;; ------------------------------------------ Connection Impersonation ------------------------------------------
196200

@@ -219,14 +223,9 @@
219223
(defn- create-table!-sql
220224
"Creates a ClickHouse table with the given name and column definitions. It assumes the engine is MergeTree,
221225
so it only works with Clickhouse Cloud and single node on-premise deployments at the moment."
222-
[driver table-name column-definitions & {:keys [primary-key]}]
226+
[_driver table-name column-definitions & {:keys [primary-key] :as opts}]
223227
(str/join "\n"
224-
[(first (sql/format {:create-table (keyword table-name)
225-
:with-columns (mapv (fn [[name type-spec]]
226-
(vec (cons name [[:raw type-spec]])))
227-
column-definitions)}
228-
:quoted true
229-
:dialect (sql.qp/quote-style driver)))
228+
[(#'sql-jdbc/create-table!-sql :sql-jdbc table-name column-definitions opts)
230229
"ENGINE = MergeTree"
231230
(format "ORDER BY (%s)" (str/join ", " (map quote-name primary-key)))
232231
;; disable insert idempotency to allow duplicate inserts
@@ -261,6 +260,7 @@
261260
(when (seq row)
262261
(doseq [[idx v] (map-indexed (fn [x y] [(inc x) y]) row)]
263262
(condp isa? (type v)
263+
nil (.setString ps idx nil)
264264
java.lang.String (.setString ps idx v)
265265
java.lang.Boolean (.setBoolean ps idx v)
266266
java.lang.Long (.setLong ps idx v)
@@ -293,8 +293,7 @@
293293
quoted-role (->> (clojure.string/split role #",")
294294
(map quote-if-needed)
295295
(clojure.string/join ","))]
296-
(format "SET ROLE %s;" quoted-role))
297-
)
296+
(format "SET ROLE %s;" quoted-role)))
298297

299298
(defmethod driver.sql/default-database-role :clickhouse
300299
[_ _]

src/metabase/driver/clickhouse_qp.clj

+2-4
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
[metabase.query-processor.timezone :as qp.timezone]
1515
[metabase.util :as u]
1616
[metabase.util.date-2 :as u.date]
17-
[metabase.util.honey-sql-2 :as h2x]
18-
[metabase.util.log :as log])
17+
[metabase.util.honey-sql-2 :as h2x])
1918
(:import [com.clickhouse.data.value ClickHouseArrayValue]
2019
[java.sql ResultSet ResultSetMetaData Types]
2120
[java.time
@@ -134,8 +133,7 @@
134133

135134
(defn- date-trunc
136135
[ch-fn expr]
137-
(-> [ch-fn (in-report-timezone expr)]
138-
(h2x/with-database-type-info (h2x/database-type expr))))
136+
[ch-fn (in-report-timezone expr)])
139137

140138
(defn- to-start-of-week
141139
[expr]

test/metabase/driver/clickhouse_impersonation_test.clj

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131
(is true))
3232
(testing "does not throw with a role containing hyphens"
3333
(sql-jdbc.execute/do-with-connection-with-options
34-
:clickhouse spec nil
35-
(fn [^java.sql.Connection conn]
36-
(driver/set-role! :clickhouse conn "metabase-test-role")))
34+
:clickhouse spec nil
35+
(fn [^java.sql.Connection conn]
36+
(driver/set-role! :clickhouse conn "metabase-test-role")))
3737
(is true))
3838
(testing "does not throw with the default role"
3939
(sql-jdbc.execute/do-with-connection-with-options

test/metabase/driver/clickhouse_test.clj

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@
150150
(mt/test-driver
151151
:clickhouse
152152
(let [query (data/mbql-query venues {:fields [$id] :order-by [[:asc $id]] :limit 5})
153-
{compiled :query} (qp.compile/compile-and-splice-parameters query)
153+
{compiled :query} (qp.compile/compile-with-inline-parameters query)
154154
pretty (driver/prettify-native-form :clickhouse compiled)]
155155
(testing "compiled"
156156
(is (= "SELECT `test_data`.`venues`.`id` AS `id` FROM `test_data`.`venues` ORDER BY `test_data`.`venues`.`id` ASC LIMIT 5" compiled)))

test/metabase/test/data/clickhouse.clj

+24-7
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
:ssl false
3232
:use_no_proxy false
3333
:use_server_time_zone_for_dates true
34-
:product_name "metabase/1.50.7"
34+
:product_name "metabase/1.51.0"
3535
:databaseTerm "schema"
3636
:remember_last_set_roles true
3737
:http_connection_provider "HTTP_URL_CONNECTION"
@@ -125,17 +125,15 @@
125125
(defmethod execute/execute-sql! :clickhouse [& args]
126126
(apply execute/sequentially-execute-sql! args))
127127

128-
(defmethod load-data/load-data! :clickhouse [& args]
129-
(apply load-data/load-data-maybe-add-ids-chunked! args))
128+
(defmethod load-data/row-xform :clickhouse [_driver _dbdef tabledef]
129+
(load-data/maybe-add-ids-xform tabledef))
130130

131131
(defmethod sql.tx/pk-sql-type :clickhouse [_] "Int32")
132132

133133
(defmethod sql.tx/add-fk-sql :clickhouse [& _] nil)
134134

135135
(defmethod sql.tx/session-schema :clickhouse [_] "default")
136136

137-
(defmethod tx/supports-time-type? :clickhouse [_driver] false)
138-
139137
(defn rows-without-index
140138
"Remove the Metabase index which is the first column in the result set"
141139
[query-result]
@@ -147,7 +145,7 @@
147145
[f]
148146
(when (not @test-db-initialized?)
149147
(let [details (tx/dbdef->connection-details :clickhouse :db {:database-name "metabase_test"})]
150-
(println "Executing create-test-db! with details:" details)
148+
;; (println "Executing create-test-db! with details:" details)
151149
(jdbc/with-db-connection
152150
[spec (sql-jdbc.conn/connection-details->spec :clickhouse (merge {:engine :clickhouse} details))]
153151
(let [statements (as-> (slurp "modules/drivers/clickhouse/test/metabase/test/data/datasets.sql") s
@@ -169,7 +167,7 @@
169167
{:write? true}
170168
(fn [^java.sql.Connection conn]
171169
(doseq [statement statements]
172-
(println "Executing:" statement)
170+
;; (println "Executing:" statement)
173171
(with-open [jdbcStmt (.createStatement conn)]
174172
(let [^ClickHouseStatementImpl clickhouseStmt (.unwrap jdbcStmt ClickHouseStatementImpl)
175173
request (.getRequest clickhouseStmt)]
@@ -188,3 +186,22 @@
188186
:details (tx/dbdef->connection-details :clickhouse :db {:database-name "metabase_test"})}]
189187
(sync-metadata/sync-db-metadata! database)
190188
(f database)))
189+
190+
(defmethod tx/dataset-already-loaded? :clickhouse
191+
[driver dbdef]
192+
(let [tabledef (first (:table-definitions dbdef))
193+
db-name (ddl.i/format-name :clickhouse (:database-name dbdef))
194+
table-name (ddl.i/format-name :clickhouse (:table-name tabledef))
195+
details (tx/dbdef->connection-details :clickhouse :db {:database-name db-name})]
196+
(sql-jdbc.execute/do-with-connection-with-options
197+
driver
198+
(sql-jdbc.conn/connection-details->spec driver details)
199+
{:write? false}
200+
(fn [^java.sql.Connection conn]
201+
(with-open [rset (.getTables (.getMetaData conn)
202+
#_catalog nil
203+
#_schema-pattern db-name
204+
#_table-pattern table-name
205+
#_types (into-array String ["TABLE"]))]
206+
;; if the ResultSet returns anything we know the table is already loaded.
207+
(.next rset))))))

0 commit comments

Comments
 (0)