Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Make returned-columns ignore :fields when summary exists (#84197)

metambena5d7261ae8d4
diff --git a/src/metabase/lib/stage.cljc b/src/metabase/lib/stage.cljc
index dffeb49ef91..1fd3195822d 100644
--- a/src/metabase/lib/stage.cljc
+++ b/src/metabase/lib/stage.cljc
@@ -308,8 +308,11 @@
field-cols (fields-columns query stage-number options)
;; ... then calculate metadata for this stage
cols (cond
+ ;; An aggregating stage doesn't actually return its `:fields` columns -- SQL would
+ ;; project them alongside a GROUP BY / aggregations, which the preprocessing middleware
+ ;; `drop-fields-in-summaries` strips before compilation (#62539).
+ summary-cols
summary-cols
- (concat summary-cols field-cols)
field-cols
(reduce
diff --git a/src/metabase/lib/util.cljc b/src/metabase/lib/util.cljc
index 5b7372e0717..94c022cb0da 100644
--- a/src/metabase/lib/util.cljc
+++ b/src/metabase/lib/util.cljc
@@ -468,8 +468,8 @@
str/trim))
(mu/defn add-summary-clause :- ::lib.schema/query
- "If the given stage has no summary, it will drop :fields, :order-by, and :join :fields from it,
- as well as dropping any subsequent stages."
+ "If the given stage has no summary, it will drop :order-by from it and drop any subsequent stages.
+ :fields is preserved so that the user's field selection survives toggling an aggregation on and off (#62539)."
[query :- ::lib.schema/query
stage-number :- :int
location :- [:enum :breakout :aggregation]
diff --git a/test/metabase/lib/remove_replace_test.cljc b/test/metabase/lib/remove_replace_test.cljc
index 3663a8b1e72..b7f5f083b5b 100644
--- a/test/metabase/lib/remove_replace_test.cljc
+++ b/test/metabase/lib/remove_replace_test.cljc
@@ -619,13 +619,18 @@
(is (not= expressions replaced-expressions))
(is (= 2 (count replaced-expressions)))
(is (= expr-b repl-expr-b))
- (testing "replacing with dependent should cascade"
+ (testing "replacing with an aggregation dependent should drop the aggregation"
(is (=? {:stages [{:aggregation (symbol "nil #_\"key is not present.\"")
:expressions [[:field {:lib/expression-name "a"} (meta/id :venues :price)]
+ expr-b]}]}
+ (-> query
+ (as-> <> (lib/aggregate <> (lib/sum (lib/expression-ref <> "a"))))
+ (lib/replace-clause 0 expr-a (meta/field-metadata :venues :price))))))
+ (testing "replacing with a later-stage dependent should drop the later-stage reference"
+ (is (=? {:stages [{:expressions [[:field {:lib/expression-name "a"} (meta/id :venues :price)]
expr-b]}
(complement :filters)]}
(-> query
- (as-> <> (lib/aggregate <> (lib/sum (lib/expression-ref <> "a"))))
(as-> <> (lib/with-fields <> [(lib/expression-ref <> "a")]))
(lib/append-stage)
(lib/filter (lib/= [:field {:lib/uuid (str (random-uuid)) :base-type :type/Integer} "a"] 1))
diff --git a/test/metabase/lib/stage_test.cljc b/test/metabase/lib/stage_test.cljc
index d31cabb08f0..7c1498002b3 100644
--- a/test/metabase/lib/stage_test.cljc
+++ b/test/metabase/lib/stage_test.cljc
@@ -807,8 +807,30 @@
"Q1 → Count"]
(map :display-name (lib/returned-columns query))))))))
+(deftest ^:parallel aggregating-stage-returned-columns-ignore-fields-test
+ (testing "a stage with :aggregation/:breakout should not advertise :fields columns as returned columns. (#62539)"
+ (are [stage expected]
+ (= expected
+ (mapv :display-name
+ (lib/returned-columns
+ (lib/query meta/metadata-provider
+ (lib.tu.macros/mbql-query orders stage)))))
+
+ {:aggregation [[:count]]
+ :fields [$orders.id $orders.total]}
+ ["Count"]
+
+ {:breakout [$orders.user-id]
+ :fields [$orders.id $orders.total]}
+ ["User ID"]
+
+ {:aggregation [[:count]]
+ :breakout [$orders.user-id]
+ :fields [$orders.id $orders.total]}
+ ["User ID" "Count"])))
+
(deftest ^:parallel expressions-with-aggregations-and-breakouts-returned-columns-test
- (testing "expressions in :fields should be returned AFTER breakouts and aggregations"
+ (testing "expression refs in :fields on an aggregating stage are stripped from returned columns (#62539)"
(let [query (lib/query
meta/metadata-provider
(lib.tu.macros/mbql-query orders
@@ -818,12 +840,11 @@
$orders.product-id->products.category]
:expressions {:test-expr [:ltrim "wheeee"]}
:fields [[:expression "test-expr"]]}))]
- (is (= ["User → State" ; from breakouts
- "User → Source" ; from breakouts
- "Product → Category" ; from breakouts
- "Count" ; from aggregations
- "Sum of Quantity" ; from aggregations
- "test-expr"] ; from expressions/fields ???
+ (is (= ["User → State"
+ "User → Source"
+ "Product → Category"
+ "Count"
+ "Sum of Quantity"]
(binding [lib.metadata.calculation/*display-name-style* :long]
(mapv :display-name (lib/returned-columns query))))))))