Skip to content
Snippets Groups Projects
Commit 0f7202a7 authored by Ryan Senior's avatar Ryan Senior
Browse files

Fix date bucketing with Druid grouped timeseries queries [ci drivers]

The timestamp field was getting added after the results were returned
and not getting added to the list of expected projections. This commit
adds that which causes the bucketed fields to be returned correctly.

Fixes #5933
parent 2f8073b9
Branches
Tags
No related merge requests found
......@@ -720,14 +720,31 @@
(defmulti ^:private post-process query-type-dispatch-fn)
(defmethod post-process ::select [_ results] (->> results first :result :events (map :event)))
(defmethod post-process ::total [_ results] (map :result results))
(defmethod post-process ::topN [_ results] (-> results first :result))
(defmethod post-process ::groupBy [_ results] (map :event results))
(defn- post-process-map [projections results]
{:projections projections
:results results})
(defmethod post-process ::timeseries [_ results]
(for [event results]
(conj {:timestamp (:timestamp event)} (:result event))))
(defmethod post-process ::select [_ projections results]
(->> results
first
:result
:events
(map :event)
(post-process-map projections)))
(defmethod post-process ::total [_ projections results]
(post-process-map projections (map :result results)))
(defmethod post-process ::topN [_ projections results]
(post-process-map projections (-> results first :result)))
(defmethod post-process ::groupBy [_ projections results]
(post-process-map projections (map :event results)))
(defmethod post-process ::timeseries [_ projections results]
(post-process-map (conj projections :timestamp)
(for [event results]
(conj {:timestamp (:timestamp event)} (:result event)))))
(defn post-process-native
"Post-process the results of a *native* Druid query.
......@@ -778,24 +795,25 @@
"Execute a query for a Druid DB."
[do-query {database :database, {:keys [query query-type mbql? projections]} :native}]
{:pre [database query]}
(let [details (:details database)
query (if (string? query)
(json/parse-string query keyword)
query)
query-type (or query-type (keyword "metabase.driver.druid.query-processor" (name (:queryType query))))
results (->> query
(do-query details)
(post-process query-type))
columns (if mbql?
(->> projections
remove-bonus-keys
vec)
(keys (first results)))
getters (columns->getter-fns columns)]
(let [details (:details database)
query (if (string? query)
(json/parse-string query keyword)
query)
query-type (or query-type (keyword "metabase.driver.druid.query-processor" (name (:queryType query))))
post-proc-map (->> query
(do-query details)
(post-process query-type projections))
columns (if mbql?
(->> post-proc-map
:projections
remove-bonus-keys
vec)
(-> post-proc-map :results first keys))
getters (columns->getter-fns columns)]
;; rename any occurances of `:timestamp___int` to `:timestamp` in the results so the user doesn't know about our behind-the-scenes conversion
;; and apply any other post-processing on the value such as parsing some units to int and rounding up approximate cardinality values.
{:columns (vec (replace {:timestamp___int :timestamp :distinct___count :count} columns))
:rows (for [row results]
:rows (for [row (:results post-proc-map)]
(for [getter getters]
(getter row)))
:annotate? mbql?}))
......@@ -595,6 +595,23 @@
(ql/breakout (ql/datetime-field $timestamp :month))
(ql/limit 5))))
;; This test is similar to the above query but doesn't use a limit
;; clause which causes the query to be a grouped timeseries query
;; rather than a topN query. The dates below are formatted incorrectly
;; due to https://github.com/metabase/metabase/issues/5969.
(expect-with-timeseries-dbs
{:columns ["timestamp" "count"]
:rows [["2013-01-01T00:00:00.000Z" 8]
["2013-02-01T00:00:00.000Z" 11]
["2013-03-01T00:00:00.000Z" 21]
["2013-04-01T00:00:00.000Z" 26]
["2013-05-01T00:00:00.000Z" 23]]}
(-> (data/run-query checkins
(ql/aggregation (ql/count))
(ql/breakout (ql/datetime-field $timestamp :month)))
data
(update :rows #(take 5 %))))
;;; date bucketing - month-of-year
(expect-with-timeseries-dbs
{:columns ["timestamp" "count"]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment