diff --git a/src/metabase/driver/common/parameters/dates.clj b/src/metabase/driver/common/parameters/dates.clj index 16f0c440b9ee9fbe03026b6ea4fe5e646bf68f1b..de6da786418163a448ffa13c203675300843b197 100644 --- a/src/metabase/driver/common/parameters/dates.clj +++ b/src/metabase/driver/common/parameters/dates.clj @@ -69,7 +69,11 @@ (defn- year-range [start end] (comparison-range start end :year)) -(defn- quarter-range +(defn- relative-quarter-range + [start end] + (comparison-range start end :quarter)) + +(defn- absolute-quarter-range [quarter year] (let [year-quarter (t/year-quarter year (case quarter "Q1" 1 @@ -80,20 +84,22 @@ :end (.atEndOfQuarter year-quarter)})) (def ^:private operations-by-date-unit - {"second" {:unit-range second-range - :to-period t/seconds} - "minute" {:unit-range minute-range - :to-period t/minutes} - "hour" {:unit-range hour-range - :to-period t/hours} - "day" {:unit-range day-range - :to-period t/days} - "week" {:unit-range week-range - :to-period t/weeks} - "month" {:unit-range month-range - :to-period t/months} - "year" {:unit-range year-range - :to-period t/years}}) + {"second" {:unit-range second-range + :to-period t/seconds} + "minute" {:unit-range minute-range + :to-period t/minutes} + "hour" {:unit-range hour-range + :to-period t/hours} + "day" {:unit-range day-range + :to-period t/days} + "week" {:unit-range week-range + :to-period t/weeks} + "month" {:unit-range month-range + :to-period t/months} + "quarter" {:unit-range relative-quarter-range + :to-period (comp t/months (partial * 3))} + "year" {:unit-range year-range + :to-period t/years}}) (defn- maybe-reduce-resolution [unit dt] (if @@ -269,9 +275,9 @@ ;; quarter year {:parser (regex->parser #"(Q[1-4]{1})-([0-9]{4})" [:quarter :year]) :range (fn [{:keys [quarter year]} _] - (quarter-range quarter (Integer/parseInt year))) + (absolute-quarter-range quarter (Integer/parseInt year))) :filter (fn [{:keys [quarter year]} field-clause] - (range->filter (quarter-range quarter (Integer/parseInt year)) + (range->filter (absolute-quarter-range quarter (Integer/parseInt year)) field-clause))} ;; single day {:parser (regex->parser #"([0-9-T:]+)" [:date]) diff --git a/test/metabase/driver/common/parameters/dates_test.clj b/test/metabase/driver/common/parameters/dates_test.clj index d63defb18b587cf383a455b5f958bcb29c375b3e..ab3b6412cb3d7153079f7fdb3e0bdc9f581e1c38 100644 --- a/test/metabase/driver/common/parameters/dates_test.clj +++ b/test/metabase/driver/common/parameters/dates_test.clj @@ -133,45 +133,51 @@ "2016-04-18~2016-04-23" {:end "2016-04-23", :start "2016-04-18"} "2016-04-18~" {:start "2016-04-18"} "~2016-04-18" {:end "2016-04-18"}} - "relative (past)" {"past30seconds" {:end "2016-06-07T12:13:54", :start "2016-06-07T12:13:25"} - "past5minutes~" {:end "2016-06-07T12:13:00", :start "2016-06-07T12:08:00"} - "past3hours" {:end "2016-06-07T11:00:00", :start "2016-06-07T09:00:00"} - "past3days" {:end "2016-06-06", :start "2016-06-04"} - "past3days~" {:end "2016-06-07", :start "2016-06-04"} - "past7days" {:end "2016-06-06", :start "2016-05-31"} - "past30days" {:end "2016-06-06", :start "2016-05-08"} - "past2months" {:end "2016-05-31", :start "2016-04-01"} - "past2months~" {:end "2016-06-30", :start "2016-04-01"} - "past13months" {:end "2016-05-31", :start "2015-05-01"} - "past1years" {:end "2015-12-31", :start "2015-01-01"} - "past1years~" {:end "2016-12-31", :start "2015-01-01"} - "past16years" {:end "2015-12-31", :start "2000-01-01"}} - "relative (next)" {"next45seconds" {:end "2016-06-07T12:14:40", :start "2016-06-07T12:13:56"} - "next20minutes" {:end "2016-06-07T12:33:00", :start "2016-06-07T12:14:00"} - "next6hours" {:end "2016-06-07T18:00:00", :start "2016-06-07T13:00:00"} - "next3days" {:end "2016-06-10", :start "2016-06-08"} - "next3days~" {:end "2016-06-10", :start "2016-06-07"} - "next7days" {:end "2016-06-14", :start "2016-06-08"} - "next30days" {:end "2016-07-07", :start "2016-06-08"} - "next2months" {:end "2016-08-31", :start "2016-07-01"} - "next2months~" {:end "2016-08-31", :start "2016-06-01"} - "next13months" {:end "2017-07-31", :start "2016-07-01"} - "next1years" {:end "2017-12-31", :start "2017-01-01"} - "next1years~" {:end "2017-12-31", :start "2016-01-01"} - "next16years" {:end "2032-12-31", :start "2017-01-01"}} - "relative (this)" {"thissecond" {:end "2016-06-07T12:13:55", :start "2016-06-07T12:13:55"} - "thisminute" {:end "2016-06-07T12:13:00", :start "2016-06-07T12:13:00"} - "thishour" {:end "2016-06-07T12:00:00", :start "2016-06-07T12:00:00"} - "thisday" {:end "2016-06-07", :start "2016-06-07"} - "thisweek" {:end "2016-06-11", :start "2016-06-05"} - "thismonth" {:end "2016-06-30", :start "2016-06-01"} - "thisyear" {:end "2016-12-31", :start "2016-01-01"}} - "relative (last)" {"lastsecond" {:end "2016-06-07T12:13:54", :start "2016-06-07T12:13:54"} - "lastminute" {:end "2016-06-07T12:12:00", :start "2016-06-07T12:12:00"} - "lasthour" {:end "2016-06-07T11:00:00", :start "2016-06-07T11:00:00"} - "lastweek" {:end "2016-06-04", :start "2016-05-29"} - "lastmonth" {:end "2016-05-31", :start "2016-05-01"} - "lastyear" {:end "2015-12-31", :start "2015-01-01"}} + "relative (past)" {"past30seconds" {:end "2016-06-07T12:13:54", :start "2016-06-07T12:13:25"} + "past5minutes~" {:end "2016-06-07T12:13:00", :start "2016-06-07T12:08:00"} + "past3hours" {:end "2016-06-07T11:00:00", :start "2016-06-07T09:00:00"} + "past3days" {:end "2016-06-06", :start "2016-06-04"} + "past3days~" {:end "2016-06-07", :start "2016-06-04"} + "past7days" {:end "2016-06-06", :start "2016-05-31"} + "past30days" {:end "2016-06-06", :start "2016-05-08"} + "past2months" {:end "2016-05-31", :start "2016-04-01"} + "past2months~" {:end "2016-06-30", :start "2016-04-01"} + "past13months" {:end "2016-05-31", :start "2015-05-01"} + "past2quarters" {:end "2016-03-31", :start "2015-10-01"} + "past2quarters~" {:end "2016-06-30", :start "2015-10-01"} + "past1years" {:end "2015-12-31", :start "2015-01-01"} + "past1years~" {:end "2016-12-31", :start "2015-01-01"} + "past16years" {:end "2015-12-31", :start "2000-01-01"}} + "relative (next)" {"next45seconds" {:end "2016-06-07T12:14:40", :start "2016-06-07T12:13:56"} + "next20minutes" {:end "2016-06-07T12:33:00", :start "2016-06-07T12:14:00"} + "next6hours" {:end "2016-06-07T18:00:00", :start "2016-06-07T13:00:00"} + "next3days" {:end "2016-06-10", :start "2016-06-08"} + "next3days~" {:end "2016-06-10", :start "2016-06-07"} + "next7days" {:end "2016-06-14", :start "2016-06-08"} + "next30days" {:end "2016-07-07", :start "2016-06-08"} + "next2months" {:end "2016-08-31", :start "2016-07-01"} + "next2months~" {:end "2016-08-31", :start "2016-06-01"} + "next2quarters" {:end "2016-12-31", :start "2016-07-01"} + "next2quarters~" {:end "2016-12-31", :start "2016-04-01"} + "next13months" {:end "2017-07-31", :start "2016-07-01"} + "next1years" {:end "2017-12-31", :start "2017-01-01"} + "next1years~" {:end "2017-12-31", :start "2016-01-01"} + "next16years" {:end "2032-12-31", :start "2017-01-01"}} + "relative (this)" {"thissecond" {:end "2016-06-07T12:13:55", :start "2016-06-07T12:13:55"} + "thisminute" {:end "2016-06-07T12:13:00", :start "2016-06-07T12:13:00"} + "thishour" {:end "2016-06-07T12:00:00", :start "2016-06-07T12:00:00"} + "thisday" {:end "2016-06-07", :start "2016-06-07"} + "thisweek" {:end "2016-06-11", :start "2016-06-05"} + "thismonth" {:end "2016-06-30", :start "2016-06-01"} + "thisquarter" {:end "2016-06-30", :start "2016-04-01"} + "thisyear" {:end "2016-12-31", :start "2016-01-01"}} + "relative (last)" {"lastsecond" {:end "2016-06-07T12:13:54", :start "2016-06-07T12:13:54"} + "lastminute" {:end "2016-06-07T12:12:00", :start "2016-06-07T12:12:00"} + "lasthour" {:end "2016-06-07T11:00:00", :start "2016-06-07T11:00:00"} + "lastweek" {:end "2016-06-04", :start "2016-05-29"} + "lastmonth" {:end "2016-05-31", :start "2016-05-01"} + "lastquarter" {:end "2016-03-31", :start "2016-01-01"} + "lastyear" {:end "2015-12-31", :start "2015-01-01"}} "relative (today/yesterday)" {"yesterday" {:end "2016-06-06", :start "2016-06-06"} "today" {:end "2016-06-07", :start "2016-06-07"}} "relative (past) with starting from" {"past3days-from-3years" {:end "2013-06-07", :start "2013-06-04"}}