Skip to content
Snippets Groups Projects
Unverified Commit ecf2647c authored by Cal Herries's avatar Cal Herries Committed by GitHub
Browse files

datetime-diff function for H2 (#27064)

* Add datetime-diff for h2

* Set supports? to true

* Refactor h2 datetime-diff

* Formatting
parent ce0046c1
No related branches found
No related tags found
No related merge requests found
......@@ -39,6 +39,7 @@
:percentile-aggregations false
:actions true
:actions/custom true
:datetime-diff true
:now true}]
(defmethod driver/database-supports? [:h2 feature]
[_driver _feature _database]
......@@ -255,6 +256,39 @@
[driver [_ field]]
(hsql/call :log10 (sql.qp/->honeysql driver field)))
(defn- datediff
"Like H2's `datediff` function but accounts for timestamps with time zones."
[unit x y]
(hsql/call :datediff (hsql/raw (name unit)) (hx/->timestamp x) (hx/->timestamp y)))
(defn- time-zoned-extract
"Like H2's extract but accounts for timestamps with time zones."
[unit x]
(extract unit (hx/->timestamp x)))
(defmethod sql.qp/datetime-diff [:h2 :year] [driver _unit x y] (hx// (sql.qp/datetime-diff driver :month x y) 12))
(defmethod sql.qp/datetime-diff [:h2 :quarter] [driver _unit x y] (hx// (sql.qp/datetime-diff driver :month x y) 3))
(defmethod sql.qp/datetime-diff [:h2 :month]
[_driver _unit x y]
(hx/+ (datediff :month x y)
;; datediff counts month boundaries not whole months, so we need to adjust
;; if x<y but x>y in the month calendar then subtract one month
;; if x>y but x<y in the month calendar then add one month
(hsql/call
:case
(hsql/call :and (hsql/call :< x y) (hsql/call :> (time-zoned-extract :day x) (time-zoned-extract :day y)))
-1
(hsql/call :and (hsql/call :> x y) (hsql/call :< (time-zoned-extract :day x) (time-zoned-extract :day y)))
1
:else 0)))
(defmethod sql.qp/datetime-diff [:h2 :week] [_driver _unit x y] (hx// (datediff :day x y) 7))
(defmethod sql.qp/datetime-diff [:h2 :day] [_driver _unit x y] (datediff :day x y))
(defmethod sql.qp/datetime-diff [:h2 :hour] [_driver _unit x y] (hx// (datediff :millisecond x y) 3600000))
(defmethod sql.qp/datetime-diff [:h2 :minute] [_driver _unit x y] (datediff :minute x y))
(defmethod sql.qp/datetime-diff [:h2 :second] [_driver _unit x y] (datediff :second x y))
;;; +----------------------------------------------------------------------------------------------------------------+
;;; | metabase.driver.sql-jdbc impls |
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment