Skip to content

Commit 3655ca3

Browse files
authored
Merge pull request #3 from swisscom-bigdata/dev/date-add_method
overriding date-add with matching amounts
2 parents fe6699a + 171d06d commit 3655ca3

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/metabase/driver/teradata.clj

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,21 @@
155155
(defmethod sql.qp/date [:teradata :quarter-of-year] [_ _ expr] (hx// (hx/+ (sql.qp/date :month-of-year (sql.qp/date :quarter expr)) 2) 3))
156156
(defmethod sql.qp/date [:teradata :year] [_ _ expr] (extract-integer :year expr))
157157

158+
(defn- num-to-interval [unit amount]
159+
(hsql/raw (format "INTERVAL '%d' %s" (int (Math/abs amount)) (name unit))))
160+
161+
(defmethod driver/date-add :teradata [_ dt amount unit]
162+
(let [op (if (>= amount 0) hx/+ hx/-)]
163+
(op (hx/->timestamp dt) (case unit
164+
:second (num-to-interval :second amount)
165+
:minute (num-to-interval :minute amount)
166+
:hour (num-to-interval :hour amount)
167+
:day (num-to-interval :day amount)
168+
:week (num-to-interval :day (hx/* amount (hsql/raw 7)))
169+
:month (num-to-interval :month amount)
170+
:quarter (num-to-interval :month (hx/* amount (hsql/raw 3)))
171+
:year (num-to-interval :year amount)))))
172+
158173
(defmethod sql.qp/unix-timestamp->timestamp [:teradata :seconds] [_ _ field-or-value]
159174
(hsql/call :to_timestamp field-or-value))
160175

@@ -235,8 +250,3 @@
235250

236251
; TODO check if overriding apply-top-level-clause could make nested queries work
237252
(defmethod driver/supports? [:teradata :nested-queries] [_ _] false)
238-
239-
(defmethod driver/date-add :teradata [_ dt amount unit]
240-
(if (>= amount 0)
241-
(hx/+ (hx/->timestamp dt) (hsql/raw (format "INTERVAL '%d' %s" (int amount) (name unit))))
242-
(hx/- (hx/->timestamp dt) (hsql/raw (format "INTERVAL '%d' %s" (Math/abs (int amount)) (name unit))))))

0 commit comments

Comments
 (0)