Skip to content
Snippets Groups Projects
Unverified Commit b0a9b11c authored by Cam Saul's avatar Cam Saul Committed by GitHub
Browse files

Fix cumulative aggregations in queries with filters and breakouts (#41792)

parent e89012f3
No related branches found
No related tags found
No related merge requests found
......@@ -62,10 +62,11 @@
first-stage-cols :- [:sequential ::lib.schema.metadata/column]]
(lib.util.match/replace stage
#{:field :expression}
(let [col (lib.equality/find-matching-column &match first-stage-cols)]
(if-let [col (lib.equality/find-matching-column &match first-stage-cols)]
(-> col
update-metadata-from-previous-stage-to-produce-correct-ref-in-current-stage
lib/ref))))
lib/ref)
(lib.util/fresh-uuids &match))))
(mu/defn ^:private new-second-stage :- ::lib.schema/stage
"All references need to be updated to be prior-stage references using the desired alias from the previous stage.
......@@ -77,7 +78,7 @@
(let [query (assoc-in query path first-stage)
first-stage-cols (lib.walk/apply-f-for-stage-at-path lib/returned-columns query path)]
(-> stage
(dissoc :expressions :joins :source-table :source-card :sources :lib/stage-metadata)
(dissoc :expressions :joins :source-table :source-card :sources :lib/stage-metadata :filters)
(update-second-stage-refs first-stage-cols))))
(mu/defn ^:private nest-breakouts-in-stage :- [:maybe [:sequential {:min 2, :max 2} ::lib.schema/stage]]
......
......@@ -51,3 +51,24 @@
[:cum-count {}]]]
:limit 3}]}
(nest-breakouts/nest-breakouts-in-stages-with-cumulative-aggregation query)))))
(deftest ^:parallel cumulative-aggregation-with-filter-and-temporal-bucketed-breakout-test
(testing "Query with a filter and a temporally bucketed breakout should work (#41791)"
(let [orders (meta/table-metadata :orders)
orders-quantity (meta/field-metadata :orders :quantity)
orders-created-at (meta/field-metadata :orders :created-at)
orders-id (meta/field-metadata :orders :id)
query (-> (lib/query meta/metadata-provider orders)
(lib/filter (lib/> orders-id 0))
(lib/aggregate (lib/cum-count))
(lib/breakout (lib/with-temporal-bucket orders-created-at :month))
(lib/breakout orders-quantity)
(lib/limit 5))]
(is (=? {:stages [{:filters [[:> {} [:field {} (meta/id :orders :id)] 0]]
:fields [[:field {:temporal-unit :month} (meta/id :orders :created-at)]
[:field {} (meta/id :orders :quantity)]]}
{:breakout [[:field {:temporal-unit :default} "CREATED_AT"]
[:field {} "QUANTITY"]]
:aggregation [[:cum-count {}]]
:limit 5}]}
(nest-breakouts/nest-breakouts-in-stages-with-cumulative-aggregation query))))))
......@@ -344,3 +344,27 @@
[#t "2016-06-01" 57 3391.41 2072.92]]
(mt/formatted-rows [->local-date int 2.0 2.0]
(qp/process-query query))))))))
(deftest ^:parallel cumulative-aggregation-with-filter-and-temporal-bucketed-breakout-test
(testing "Query with a filter and a temporally bucketed breakout should work (#41791)"
(let [metadata-provider (lib.metadata.jvm/application-database-metadata-provider (mt/id))
orders (lib.metadata/table metadata-provider (mt/id :orders))
orders-quantity (lib.metadata/field metadata-provider (mt/id :orders :quantity))
orders-created-at (lib.metadata/field metadata-provider (mt/id :orders :created_at))
orders-id (lib.metadata/field metadata-provider (mt/id :orders :id))
query (-> (lib/query metadata-provider orders)
(lib/filter (lib/> orders-id 5000))
(lib/aggregate (lib/cum-count))
(lib/breakout (lib/with-temporal-bucket orders-created-at :month))
(lib/breakout orders-quantity)
(lib/limit 5)
(assoc-in [:middleware :format-rows?] false))]
(mt/with-native-query-testing-context query
(is (= [[#t "2016-04-01" 2 1]
[#t "2016-05-01" 2 1]
[#t "2016-05-01" 3 4]
[#t "2016-05-01" 4 10]
[#t "2016-05-01" 5 17]]
(mt/formatted-rows
[->local-date int int]
(qp/process-query query))))))))
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