The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Remove-replace cleans up empty first stages when reasonable (#65565)
Williamc1e548ca9e3d
diff --git a/src/metabase/lib/remove_replace.cljc b/src/metabase/lib/remove_replace.cljcindex 142cbeea516..afb418d81b2 100644--- a/src/metabase/lib/remove_replace.cljc+++ b/src/metabase/lib/remove_replace.cljc@@ -229,6 +229,28 @@possible-location))))(stage-paths query stage-number))))+(defn- maybe-merge-first-stage [query]+ ;; Note that this is a fairly narrowly-scoped change to fix #45041+ ;; Merging more stages would be plausible, but this approach is enough to fix the current bug+ ;; and I'm trying to minimize the chances of breaking anything else.+ (let [first-stage (lib.util/query-stage query 0)+ stage-keys (-> first-stage keys set)+ next-stage (when-not (lib.util/last-stage? query 0)+ (lib.util/query-stage query 1))]+ ;; if the first stage is empty and there are more stages after it, merge the first stage into the next stage+ (if (and (or (= stage-keys+ #{:source-table :lib/type})+ (= stage-keys+ #{:source-card :lib/type}))+ next-stage+ (lib.util/mbql-stage? first-stage)+ (lib.util/mbql-stage? next-stage))+ (let [additional-stages (drop 2 (:stages query))+ new-stages (vec (cons (merge next-stage first-stage)+ additional-stages))]+ (assoc query :stages new-stages))+ query)))+(mu/defn- remove-replace* :- :map[query :- :mapstage-number :- :int@@ -283,7 +305,8 @@;; clean this up -- why don't we do that?(lib.schema.util/distinct-mbql-clauses? (:breakout new-stage)));; any change to something other than breakouts is always considered valid- true)]+ true)+ new-query (maybe-merge-first-stage new-query)](if valid-change?new-queryquery))))diff --git a/test/metabase/lib/remove_replace_test.cljc b/test/metabase/lib/remove_replace_test.cljcindex 1222cae4eeb..59dcc71aa4e 100644--- a/test/metabase/lib/remove_replace_test.cljc+++ b/test/metabase/lib/remove_replace_test.cljc@@ -775,6 +775,21 @@(is (= (get-in q3 [:stages 0 :breakout 0 1 :metabase.lib.field/original-temporal-unit])(get-in q3 [:stages 0 :order-by 0 2 1 :metabase.lib.field/original-temporal-unit])))))))+(deftest ^:parallel empty-first-stage-gets-merged-test+ (testing "issue #45041"+ (testing "Emptying a stage via remove-replace should merge that stage into the next stage"+ (let [query (lib/query meta/metadata-provider (meta/table-metadata :products))+ q2 (-> query+ (lib/aggregate (lib/count))+ lib/append-stage+ (lib/aggregate (lib/count)))+ to-remove (-> (lib/query-stage q2 0)+ :aggregation+ first)+ q3 (lib/remove-clause q2 0 to-remove)]+ (is (=? {:aggregation [[:count {}]]}+ (lib/query-stage q3 0)))))))+(deftest ^:parallel rename-join-test(let [joined-column (-> (meta/field-metadata :venues :id)(lib/with-join-alias "alias"))