Skip to content

Commit

Permalink
More JDBC driver goodness (#110)
Browse files Browse the repository at this point in the history
* More JDBC driver goodness

* Replace ClickHouseArrayUtil usage

* Additional arrays tests

* Check JAR size on the CI

* Exclude GRPC and CLI shaded dependencies

Co-authored-by: slvrtrn <[email protected]>
  • Loading branch information
enqueue and slvrtrn authored Dec 7, 2022
1 parent 098e6c3 commit 8f55afa
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 12 deletions.
1 change: 1 addition & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ jobs:
run: |
echo "{:deps {metabase/clickhouse {:local/root \"clickhouse\" }}}" > modules/drivers/deps.edn
bin/build-driver.sh clickhouse
ls -lah resources/modules
- name: Archive driver JAR
uses: actions/upload-artifact@v2
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
/.lein-failures
/.lein-plugins
/.lein-repl-history
/.clj-kondo
.eastwood
.calva
.cpcache
Expand Down
4 changes: 3 additions & 1 deletion deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
["src" "resources"]

:deps
{com.clickhouse/clickhouse-jdbc {:mvn/version "0.3.2-patch11"}}}
{com.clickhouse/clickhouse-jdbc$http {:mvn/version "0.3.2-patch11"
:exclusions [com.clickhouse/clickhouse-cli-client$shaded
com.clickhouse/clickhouse-grpc-client$shaded]}}}
22 changes: 16 additions & 6 deletions src/metabase/driver/clickhouse.clj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
[metabase.util.date-2 :as u.date]
[metabase.util.honeysql-extensions :as hx]
[schema.core :as s])
(:import [java.sql
(:import [com.clickhouse.client.data ClickHouseArrayValue]
[java.sql
DatabaseMetaData
ResultSet
ResultSetMetaData
Expand All @@ -29,8 +30,8 @@
OffsetDateTime
OffsetTime
ZonedDateTime]
[java.util TimeZone]
[ru.yandex.clickhouse.util ClickHouseArrayUtil]))
java.lang.Byte
java.util.Arrays))

(driver/register! :clickhouse :parent :sql-jdbc)

Expand Down Expand Up @@ -442,10 +443,19 @@
(.getObject rs i OffsetTime))

(defmethod sql-jdbc.execute/read-column [:clickhouse Types/ARRAY]
[_ calendar resultset _ i]
[_ _ resultset _ i]
(when-let [arr (.getArray resultset i)]
(let [tz (if (nil? calendar) (TimeZone/getDefault) (.getTimeZone calendar))]
(ClickHouseArrayUtil/arrayToString (.getArray arr) tz tz))))
(let [inner (.getArray arr)]
(cond
;; Booleans are returned as just bytes
(bytes? inner)
(str "[" (str/join ", " (map #(if (= 1 %) "true" "false") inner)) "]")
;; All other primitives
(.isPrimitive (.getComponentType (.getClass inner)))
(Arrays/toString inner)
;; Complex types
:else
(.asString (ClickHouseArrayValue/of inner))))))

(def ^:private allowed-table-types
(into-array String
Expand Down
159 changes: 154 additions & 5 deletions test/metabase/driver/clickhouse_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
(:require [cljc.java-time.format.date-time-formatter :as date-time-formatter]
[cljc.java-time.offset-date-time :as offset-date-time]
[cljc.java-time.temporal.chrono-unit :as chrono-unit]
[cljc.java-time.local-date :as local-date]
[clojure.test :refer :all]
[metabase.driver :as driver]
[metabase.driver.clickhouse-test-utils :as ctu]
Expand Down Expand Up @@ -77,7 +78,7 @@
(mt/test-driver
:clickhouse
(is
(= "['foo','bar']"
(= "[foo, bar]"
(-> (data/dataset
(tx/dataset-definition "metabase_tests_array_string"
["test-data-array-string"
Expand All @@ -92,7 +93,7 @@
(mt/test-driver
:clickhouse
(is
(= "[23,42]"
(= "[23, 42]"
(-> (data/dataset
(tx/dataset-definition "metabase_tests_array_uint"
["test-data-array-uint64"
Expand All @@ -118,7 +119,7 @@
[[row1] [row2]]])
(data/run-mbql-query test-data-array-of-arrays {}))
result (ctu/rows-without-index query-result)]
(is (= [["[['foo','bar'],['qaz','qux']]"], ["[]"]] result)))))
(is (= [["[[foo, bar], [qaz, qux]]"], ["[]"]] result)))))

(deftest clickhouse-low-cardinality-array
(mt/test-driver
Expand All @@ -133,7 +134,7 @@
[[row1] [row2]]])
(data/run-mbql-query test-data-low-cardinality-array {}))
result (ctu/rows-without-index query-result)]
(is (= [["['foo','bar']"], ["[]"]] result)))))
(is (= [["[foo, bar]"], ["[]"]] result)))))

(deftest clickhouse-array-of-nullables
(mt/test-driver
Expand All @@ -148,7 +149,155 @@
[[row1] [row2]]])
(data/run-mbql-query test-data-array-of-nullables {}))
result (ctu/rows-without-index query-result)]
(is (= [["['foo',NULL,'bar']"], ["[]"]] result)))))
(is (= [["[foo, null, bar]"], ["[]"]] result)))))

(deftest clickhouse-array-of-booleans
(mt/test-driver
:clickhouse
(let [row1 (into-array (list true false true))
row2 (into-array nil)
query-result (data/dataset
(tx/dataset-definition "metabase_tests_array_of_booleans"
["test-data-array-of-booleans"
[{:field-name "my_array_of_booleans"
:base-type {:native "Array(Boolean)"}}]
[[row1] [row2]]])
(data/run-mbql-query test-data-array-of-booleans {}))
result (ctu/rows-without-index query-result)]
(is (= [["[true, false, true]"], ["[]"]] result)))))

(deftest clickhouse-array-of-floats
(mt/test-driver
:clickhouse
(let [row1 (into-array (list 1.2 3.4))
row2 (into-array nil)
query-result (data/dataset
(tx/dataset-definition "metabase_tests_array_of_floats"
["test-data-array-of-floats"
[{:field-name "my_array_of_floats"
:base-type {:native "Array(Float64)"}}]
[[row1] [row2]]])
(data/run-mbql-query test-data-array-of-floats {}))
result (ctu/rows-without-index query-result)]
(is (= [["[1.2, 3.4]"], ["[]"]] result)))))

(deftest clickhouse-array-of-dates
(mt/test-driver
:clickhouse
(let [row1 (into-array
(list
(local-date/parse "2022-12-06")
(local-date/parse "2021-10-19")))
row2 (into-array nil)
query-result (data/dataset
(tx/dataset-definition "metabase_tests_array_of_dates"
["test-data-array-of-dates"
[{:field-name "my_array_of_dates"
:base-type {:native "Array(Date)"}}]
[[row1] [row2]]])
(data/run-mbql-query test-data-array-of-dates {}))
result (ctu/rows-without-index query-result)]
(is (= [["[2022-12-06, 2021-10-19]"], ["[]"]] result)))))

(deftest clickhouse-array-of-date32
(mt/test-driver
:clickhouse
(let [row1 (into-array
(list
(local-date/parse "2122-12-06")
(local-date/parse "2099-10-19")))
row2 (into-array nil)
query-result (data/dataset
(tx/dataset-definition "metabase_tests_array_of_date32"
["test-data-array-of-date32"
[{:field-name "my_array_of_date32"
:base-type {:native "Array(Date32)"}}]
[[row1] [row2]]])
(data/run-mbql-query test-data-array-of-date32 {}))
result (ctu/rows-without-index query-result)]
(is (= [["[2122-12-06, 2099-10-19]"], ["[]"]] result)))))

(deftest clickhouse-array-of-datetime
(mt/test-driver
:clickhouse
(let [row1 (into-array
(list
(offset-date-time/parse "2022-12-06T18:28:31Z")
(offset-date-time/parse "2021-10-19T13:12:44Z")))
row2 (into-array nil)
query-result (data/dataset
(tx/dataset-definition "metabase_tests_array_of_datetime"
["test-data-array-of-datetime"
[{:field-name "my_array_of_datetime"
:base-type {:native "Array(DateTime)"}}]
[[row1] [row2]]])
(data/run-mbql-query test-data-array-of-datetime {}))
result (ctu/rows-without-index query-result)]
(is (= [["[2022-12-06T18:28:31, 2021-10-19T13:12:44]"], ["[]"]] result)))))

(deftest clickhouse-array-of-datetime64
(mt/test-driver
:clickhouse
(let [row1 (into-array
(list
(offset-date-time/parse "2022-12-06T18:28:31.123Z")
(offset-date-time/parse "2021-10-19T13:12:44.456Z")))
row2 (into-array nil)
query-result (data/dataset
(tx/dataset-definition "metabase_tests_array_of_datetime64"
["test-data-array-of-datetime64"
[{:field-name "my_array_of_datetime64"
:base-type {:native "Array(DateTime64(3))"}}]
[[row1] [row2]]])
(data/run-mbql-query test-data-array-of-datetime64 {}))
result (ctu/rows-without-index query-result)]
(is (= [["[2022-12-06T18:28:31.123, 2021-10-19T13:12:44.456]"], ["[]"]] result)))))

(deftest clickhouse-array-of-decimals
(mt/test-driver
:clickhouse
(let [row1 (into-array (list "12345123.123456789" "78.245"))
row2 nil
query-result (data/dataset
(tx/dataset-definition "metabase_tests_array_of_decimals"
["test-data-array-of-decimals"
[{:field-name "my_array_of_decimals"
:base-type {:native "Array(Decimal(18, 9))"}}]
[[row1] [row2]]])
(data/run-mbql-query test-data-array-of-decimals {}))
result (ctu/rows-without-index query-result)]
(is (= [["[12345123.123456789, 78.245000000]"], ["[]"]] result)))))

(deftest clickhouse-array-of-tuples
(mt/test-driver
:clickhouse
(let [row1 (into-array (list (list "foobar" 1234) (list "qaz" 0)))
row2 nil
query-result (data/dataset
(tx/dataset-definition "metabase_tests_array_of_tuples"
["test-data-array-of-tuples"
[{:field-name "my_array_of_tuples"
:base-type {:native "Array(Tuple(String, UInt32))"}}]
[[row1] [row2]]])
(data/run-mbql-query test-data-array-of-tuples {}))
result (ctu/rows-without-index query-result)]
(is (= [["[[foobar, 1234], [qaz, 0]]"], ["[]"]] result)))))

(deftest clickhouse-array-of-uuids
(mt/test-driver
:clickhouse
(let [row1 (into-array (list "2eac427e-7596-11ed-a1eb-0242ac120002"
"2eac44f4-7596-11ed-a1eb-0242ac120002"))
row2 nil
query-result (data/dataset
(tx/dataset-definition "metabase_tests_array_of_uuids"
["test-data-array-of-uuids"
[{:field-name "my_array_of_uuids"
:base-type {:native "Array(UUID)"}}]
[[row1] [row2]]])
(data/run-mbql-query test-data-array-of-uuids {}))
result (ctu/rows-without-index query-result)]
(is (= [["[2eac427e-7596-11ed-a1eb-0242ac120002, 2eac44f4-7596-11ed-a1eb-0242ac120002]"], ["[]"]] result)))))

(deftest clickhouse-nullable-strings
(mt/test-driver
Expand Down

0 comments on commit 8f55afa

Please sign in to comment.