Skip to content
Snippets Groups Projects
Commit 46104157 authored by Nikita Nefedov's avatar Nikita Nefedov
Browse files

Fix #9563 handling of :default unit in datetimes

parent 720b0dab
No related branches found
No related tags found
No related merge requests found
......@@ -50,7 +50,9 @@
(and (= unit :day) (= amount 0)) "today"
(and (= unit :day) (= amount -1)) "yesterday"
(and (= unit :day) (< amount -1)) (str (- amount) "daysAgo")
:else (du/format-date "yyyy-MM-dd" (du/date-trunc unit (du/relative-date unit amount)))))
:else (du/format-date
"yyyy-MM-dd"
(du/date-trunc unit (du/relative-date unit amount)))))
(defmethod ->rvalue :value [[_ value _]]
value)
......@@ -257,6 +259,18 @@
[(_ :guard #{:< :> :<= :>= :between :=}) [(_ :guard (partial not= :datetime-field)) & _] & _]
nil))
(defn- normalize-unit [unit]
(if (= unit :default) :day unit))
(defn- normalize-datetime-units
"Replace all unsupported datetime units with the default"
[filter-clause]
(mbql.u/replace filter-clause
[:datetime-field field unit] [:datetime-field field (normalize-unit unit)]
[:absolute-datetime timestamp unit] [:absolute-datetime timestamp (normalize-unit unit)]
[:relative-datetime amount unit] [:relative-datetime amount (normalize-unit unit)]))
(defn- add-start-end-dates [filter-clause]
(merge {:start-date earliest-date, :end-date latest-date} filter-clause))
......@@ -265,7 +279,10 @@
`handle-builtin-segment` logic)."
[{filter-clause :filter}]
(or (when filter-clause
(add-start-end-dates (parse-filter:interval (remove-non-datetime-filter-clauses filter-clause))))
(add-start-end-dates
(parse-filter:interval
(normalize-datetime-units
(remove-non-datetime-filter-clauses filter-clause)))))
{:start-date earliest-date, :end-date latest-date}))
......
......@@ -17,8 +17,7 @@
[metabase.test.util :as tu]
[metabase.util.date :as du]
[toucan.db :as db]
[toucan.util.test :as tt]
[clj-time.core :as t]))
[toucan.util.test :as tt]))
;;; +----------------------------------------------------------------------------------------------------------------+
;;; | MBQL->NATIVE (QUERY -> GA QUERY) |
......@@ -88,7 +87,7 @@
(ga-query {:start-date "2016-11-08", :end-date "2016-11-08"})
(mbql->native {:query {:filter [:= (ga-date-field :day) [:absolute-datetime #inst "2016-11-08" :day]]}}))
;; tests day-off correction for gt/lt operators (GA doesn't support exclusive ranges)
;; tests off by one day correction for gt/lt operators (GA doesn't support exclusive ranges)
(expect
(ga-query {:start-date "2016-11-09", :end-date "today"})
(mbql->native {:query {:filter [:> (ga-date-field :day) [:absolute-datetime #inst "2016-11-08" :day]]}}))
......@@ -154,6 +153,7 @@
:end-date "today"})
(mbql->native {:query {:filter [:>= (ga-date-field :day) [:relative-datetime -30 :day]]}}))
;; last year excluding the first day of the range
(expect
(ga-query {:start-date (du/format-date "yyyy-MM-dd" (du/relative-date :day 1 (du/date-trunc :year (du/relative-date :year -1))))
:end-date "today"})
......
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