Skip to content

Commit e1376ea

Browse files
committed
Merge pull request metabase#2586 from metabase/default-to-day-bucketing-except-for-sorting
Default to `:day` bucketing everywhere except when sorting. 😓
2 parents a4e685a + 0d140ca commit e1376ea

File tree

5 files changed

+37
-25
lines changed

5 files changed

+37
-25
lines changed

src/metabase/query_processor/expand.clj

+26-16
Original file line numberDiff line numberDiff line change
@@ -306,27 +306,37 @@
306306

307307
;;; ## order-by
308308

309-
(s/defn ^:private ^:always-validate maybe-parse-order-by-subclause :- i/OrderBy [subclause]
310-
(cond
311-
(map? subclause) subclause
312-
(vector? subclause) (let [[f direction] subclause]
313-
(log/warn (u/format-color 'yellow (str "The syntax for order-by has changed in MBQL '98. [<field> :ascending/:descending] is deprecated. "
314-
"Prefer [:asc/:desc <field>] instead.")))
315-
{:field (field f), :direction (normalize-token direction)})))
316-
317-
(s/defn ^:ql ^:always-validate asc :- i/OrderBy
318-
"order-by subclause. Specify that results should be returned in ascending order for Field or AgRef F.
309+
(s/defn ^:private ^:always-validate order-by-subclause :- i/OrderBy
310+
[direction :- i/OrderByDirection, f]
311+
;; it's not particularly useful to sort datetime fields with the default `:day` bucketing,
312+
;; so specifiy `:default` bucketing to prevent the default of `:day` from being set during resolution.
313+
;; This won't affect fields that aren't `DateTimeFields`.
314+
{:direction direction
315+
:field (let [f (field f)]
316+
(if-not (instance? FieldPlaceholder f)
317+
f
318+
(update f :datetime-unit (fn [unit]
319+
(core/or unit :default)))))})
320+
321+
(def ^:ql ^{:arglists '([field])} asc
322+
"`order-by` subclause. Specify that results should be returned in ascending order for Field or AgRef F.
319323
320324
(order-by {} (asc 100))"
321-
[f]
322-
{:field (field f), :direction :ascending})
325+
(partial order-by-subclause :ascending))
323326

324-
(s/defn ^:ql ^:always-validate desc :- i/OrderBy
325-
"order-by subclause. Specify that results should be returned in ascending order for Field or AgRef F.
327+
(def ^:ql ^{:arglists '([field])} desc
328+
"`order-by` subclause. Specify that results should be returned in ascending order for Field or AgRef F.
326329
327330
(order-by {} (desc 100))"
328-
[f]
329-
{:field (field f), :direction :descending})
331+
(partial order-by-subclause :descending))
332+
333+
(s/defn ^:private ^:always-validate maybe-parse-order-by-subclause :- i/OrderBy
334+
[subclause]
335+
(cond
336+
(map? subclause) subclause ; already parsed by `asc` or `desc`
337+
(vector? subclause) (let [[f direction] subclause]
338+
(log/warn (u/format-color 'yellow "The syntax for order-by has changed in MBQL '98. [<field> :ascending/:descending] is deprecated. Prefer [:asc/:desc <field>] instead."))
339+
(order-by-subclause (normalize-token direction) f))))
330340

331341
(defn ^:ql order-by
332342
"Specify how ordering should be done for this query.

src/metabase/query_processor/interface.clj

+4-1
Original file line numberDiff line numberDiff line change
@@ -300,11 +300,14 @@
300300
(s/named (s/cond-pre SimpleFilterClause NotFilter CompoundFilter)
301301
"Valid filter clause"))
302302

303+
(def OrderByDirection
304+
"Schema for the direction in an `OrderBy` subclause."
305+
(s/named (s/enum :ascending :descending) "Valid order-by direction"))
303306

304307
(def OrderBy
305308
"Schema for top-level `order-by` clause in an MBQL query."
306309
(s/named {:field AnyField
307-
:direction (s/named (s/enum :ascending :descending) "Valid order-by direction")}
310+
:direction OrderByDirection}
308311
"Valid order-by subclause"))
309312

310313

src/metabase/query_processor/resolve.clj

+2-3
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,12 @@
9797
(if-let [{:keys [base-type special-type], :as field} (some-> (field-id->fields field-id)
9898
map->Field)]
9999
;; try to resolve the Field with the ones available in field-id->fields
100-
(let [datetime-field? (or datetime-unit
101-
(contains? #{:DateField :DateTimeField} base-type)
100+
(let [datetime-field? (or (contains? #{:DateField :DateTimeField} base-type)
102101
(contains? #{:timestamp_seconds :timestamp_milliseconds} special-type))]
103102
(if-not datetime-field?
104103
field
105104
(map->DateTimeField {:field field
106-
:unit (or datetime-unit :default)})))
105+
:unit (or datetime-unit :day)}))) ; default to `:day` if a unit wasn't specified
107106
;; If that fails just return ourselves as-is
108107
this))
109108

test/metabase/query_processor_test.clj

+4-4
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@
485485
:cols [(aggregate-col :count)]}
486486
(format-rows-by [int] (run-query checkins
487487
(ql/aggregation (ql/count))
488-
(ql/filter (ql/between (ql/datetime-field $date :day) "2015-04-01" "2015-05-01")))))
488+
(ql/filter (ql/between $date "2015-04-01" "2015-05-01")))))
489489

490490
;; ### FILTER -- "OR", "<=", "="
491491
(expect-with-non-timeseries-dbs
@@ -859,8 +859,8 @@
859859
9)
860860
(count (rows (dataset sad-toucan-incidents
861861
(run-query incidents
862-
(ql/filter (ql/and (ql/> (ql/datetime-field $timestamp :day) "2015-06-01")
863-
(ql/< (ql/datetime-field $timestamp :day) "2015-06-03")))
862+
(ql/filter (ql/and (ql/> $timestamp "2015-06-01")
863+
(ql/< $timestamp "2015-06-03")))
864864
(ql/order-by (ql/asc $timestamp)))))))
865865

866866
(expect-with-non-timeseries-dbs
@@ -905,7 +905,7 @@
905905
(->> (dataset sad-toucan-incidents
906906
(run-query incidents
907907
(ql/aggregation (ql/count))
908-
(ql/breakout (ql/datetime-field $timestamp :day))
908+
(ql/breakout $timestamp)
909909
(ql/limit 10)))
910910
rows (format-rows-by [identity int])))
911911

test/metabase/timeseries_query_processor_test.clj

+1-1
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@
390390
["2013-01-23+0000" 1]]}
391391
(data (data/run-query checkins
392392
(ql/aggregation (ql/count))
393-
(ql/breakout (ql/datetime-field $timestamp :day))
393+
(ql/breakout $timestamp)
394394
(ql/limit 5))))
395395

396396
;;; date bucketing - minute

0 commit comments

Comments
 (0)