Skip to content
Snippets Groups Projects
Unverified Commit d7962863 authored by metamben's avatar metamben Committed by GitHub
Browse files

Generate unique aliases for operations in aggregations (#30302)

Fixes #30262.
parent da930dfa
No related branches found
No related tags found
No related merge requests found
......@@ -1049,7 +1049,16 @@
(extract-aggregations (first args) parent-name aggregations-seen)
(aggregation-op op)
(let [aggr-name (str parent-name "~" (annotate/aggregation-name aggr-expr))]
(let [aliases-taken (set (vals aggregations-seen))
aggr-name (annotate/aggregation-name aggr-expr)
desired-alias (str parent-name "~" aggr-name)
;; find a free alias by appending increasing integers
;; to the desired alias
aggr-name (some (fn [suffix]
(let [alias (str desired-alias suffix)]
(when-not (aliases-taken alias)
alias)))
(cons "" (iterate inc 1)))]
[(str \$ aggr-name) (assoc aggregations-seen aggr-expr aggr-name)])
:else
......@@ -1061,7 +1070,7 @@
[aggr-expr aggregations-seen])))
(defn- simplify-extracted-aggregations
"Simplifies the extracted aggregation ()for `aggr-name` if the expression
"Simplifies the extracted aggregation for `aggr-name` if the expression
contains only a single top-level aggregation. In this case there is no
need for namespacing and `aggr-name` can be used as the name of the group
introduced for the aggregation.
......
......@@ -143,6 +143,23 @@
{:aggregation [[:+ [:max $price] [:min [:- $price $id]]]]
:breakout [$price]})))))))
(deftest integer-aggregation-division-test
(testing "division of two sum aggregations (#30262)"
(mt/test-drivers (mt/normal-drivers-with-feature :expression-aggregations)
(mt/dataset sample-dataset
(testing "expression parts not selected"
(is (= [[27]]
(mt/formatted-rows [int]
(mt/run-mbql-query orders
{:aggregation [[:/ [:sum $product_id] [:sum $quantity]]]})))))
(testing "expression parts also selected"
(is (= [[1885900 69540 27]]
(mt/formatted-rows [int int int]
(mt/run-mbql-query orders
{:aggregation [[:sum $product_id]
[:sum $quantity]
[:/ [:sum $product_id] [:sum $quantity]]]})))))))))
(deftest aggregation-without-field-test
(mt/test-drivers (mt/normal-drivers-with-feature :expression-aggregations)
(testing "aggregation w/o field"
......
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