Skip to content

Commit

Permalink
Use ClickHouse startsWith/endsWith (#174)
Browse files Browse the repository at this point in the history
* Use ClickHouse startsWith/endsWith

* Update CHANGELOG
  • Loading branch information
slvrtrn authored Jun 20, 2023
1 parent a219f27 commit 79a8f1d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* JDBC driver upgrade (v0.4.1 -> [v0.4.6](https://github.com/ClickHouse/clickhouse-java/releases/tag/v0.4.6))
* Support DateTime64 by [@lucas-tubi](https://github.com/lucas-tubi) ([#165](https://github.com/ClickHouse/metabase-clickhouse-driver/pull/165))
* Use native `startsWith`/`endsWith` instead of `LIKE str%`/`LIKE %str`

# 1.1.6

Expand Down
26 changes: 14 additions & 12 deletions src/metabase/driver/clickhouse.clj
Original file line number Diff line number Diff line change
Expand Up @@ -472,26 +472,28 @@
[value :- (s/constrained mbql.s/value #(string? (second %)) "string value") f]
(update value 1 f))

(defmethod sql.qp/->honeysql [:clickhouse :starts-with]
[driver [_ field value options]]
(ch-like-clause driver
(sql.qp/->honeysql driver field)
(update-string-value value #(str % \%))
options))

(defmethod sql.qp/->honeysql [:clickhouse :contains]
[driver [_ field value options]]
(ch-like-clause driver
(sql.qp/->honeysql driver field)
(update-string-value value #(str \% % \%))
options))

(defn- clickhouse-string-fn
[fn-name field value options]
(let [field (sql.qp/->honeysql :clickhouse field)
value (sql.qp/->honeysql :clickhouse value)]
(if (get options :case-sensitive true)
(hsql/call fn-name field value)
(hsql/call fn-name (hsql/call :lowerUTF8 field) (str/lower-case value)))))

(defmethod sql.qp/->honeysql [:clickhouse :starts-with]
[_ [_ field value options]]
(clickhouse-string-fn :startsWith field value options))

(defmethod sql.qp/->honeysql [:clickhouse :ends-with]
[driver [_ field value options]]
(ch-like-clause driver
(sql.qp/->honeysql driver field)
(update-string-value value #(str \% %))
options))
[_ [_ field value options]]
(clickhouse-string-fn :endsWith field value options))

;; We do not have Time data types, so we cheat a little bit
(defmethod sql.qp/cast-temporal-string [:clickhouse :Coercion/ISO8601->Time]
Expand Down

0 comments on commit 79a8f1d

Please sign in to comment.