Skip to content

Commit

Permalink
Emulate Time data type (ClickHouse#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
enqueue authored Apr 11, 2019
1 parent 292d15b commit 62ebd20
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
12 changes: 9 additions & 3 deletions src/metabase/driver/clickhouse.clj
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
[date :as du]
[honeysql-extensions :as hx]]
[schema.core :as sc])
(:import [java.sql DatabaseMetaData Time]
java.util.Date))
(:import [java.sql DatabaseMetaData ResultSet Time Types]
[java.util Calendar Date]))

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

Expand Down Expand Up @@ -200,7 +200,7 @@
;; See above. We are removing the artificial alias suffix
(defmethod driver/execute-query :clickhouse [driver query]
(update-in
(sql-jdbc.execute/execute-query :sql-jdbc query)
(sql-jdbc.execute/execute-query driver query)
[:columns]
(fn [columns]
(mapv (fn [column]
Expand All @@ -209,6 +209,12 @@
column))
columns))))

(defmethod sql-jdbc.execute/read-column [:clickhouse Types/TIMESTAMP] [driver calendar resultset meta i]
(when-let [timestamp (.getTimestamp resultset i)]
(if (string/starts-with? (.toString timestamp) "1970-01-01")
(Time. (.getTime timestamp))
((get-method sql-jdbc.execute/read-column [:sql-jdbc Types/TIMESTAMP]) driver calendar resultset meta i))))

(defn- get-tables
"Fetch a JDBC Metadata ResultSet of tables in the DB, optionally limited to ones belonging to a given schema."
[^DatabaseMetaData metadata, ^String schema-or-nil, ^String db-name-or-nil]
Expand Down
5 changes: 1 addition & 4 deletions test/metabase/test/data/clickhouse.clj
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@
(defmethod sql.tx/field-base-type->sql-type [:clickhouse :type/Text] [_ _] "String")
(defmethod sql.tx/field-base-type->sql-type [:clickhouse :type/UUID] [_ _] "UUID")

;; If someone tries to run Time column tests with ClickHouse give them a heads up that
;; ClickHouse does not support it
(defmethod sql.tx/field-base-type->sql-type [:clickhouse :type/Time] [_ _]
(throw (UnsupportedOperationException. "ClickHouse does not have a TIME data type.")))
(defmethod sql.tx/field-base-type->sql-type [:clickhouse :type/Time] [_ _] "DateTime")

(defmethod tx/dbdef->connection-details :clickhouse [_ context {:keys [database-name]}]
(merge
Expand Down

0 comments on commit 62ebd20

Please sign in to comment.