From f912df3b6b81e9c0118da9df37a1edb119a4ad81 Mon Sep 17 00:00:00 2001 From: "metabase-bot[bot]" <109303359+metabase-bot[bot]@users.noreply.github.com> Date: Tue, 26 Mar 2024 12:34:43 -0400 Subject: [PATCH] backport "[MLv2] Ensure `:name` is unique on returned columns in the FE (#39118)" (#39122) Fixes #37517. Co-authored-by: Braden Shepherdson <braden@metabase.com> --- src/metabase/lib/js.cljs | 10 ++++++++-- test/metabase/lib/js_test.cljs | 13 +++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/metabase/lib/js.cljs b/src/metabase/lib/js.cljs index f3b733358e0..5448251fbef 100644 --- a/src/metabase/lib/js.cljs +++ b/src/metabase/lib/js.cljs @@ -659,9 +659,15 @@ (defn- returned-columns* "Inner implementation for [[returned-columns]], which wraps this with caching." [a-query stage-number] - (let [stage (lib.util/query-stage a-query stage-number)] + (let [stage (lib.util/query-stage a-query stage-number) + unique-name-fn (lib.util/unique-name-generator)] (->> (lib.metadata.calculation/returned-columns a-query stage-number stage) - (map #(assoc % :selected? true)) + (map #(-> % + (assoc :selected? true) + ;; Unique names are required by the FE for compatibility. + ;; This applies only for JS; Clojure usage should prefer `:lib/desired-column-alias` to `:name`, and + ;; that's already unique by construction. + (update :name unique-name-fn))) to-array))) (defn ^:export returned-columns diff --git a/test/metabase/lib/js_test.cljs b/test/metabase/lib/js_test.cljs index e0f2c34abe9..e2322ea770f 100644 --- a/test/metabase/lib/js_test.cljs +++ b/test/metabase/lib/js_test.cljs @@ -398,3 +398,16 @@ :day] (lib.js/expression-clause "time-interval" [(meta/field-metadata :products :created-at) (lib.js/expression-clause "interval" [10 "day"] nil) "day"] nil))))) +(deftest ^:parallel returned-columns-unique-names-test + (testing "returned-columns should ensure the :name fields are unique (#37517)" + (let [query (-> (lib/query meta/metadata-provider (meta/table-metadata :orders)) + (lib/join (lib/join-clause (meta/table-metadata :orders) + [(lib/= (meta/field-metadata :orders :id) + (lib/with-join-alias (meta/field-metadata :orders :id) + "Orders"))])))] + (is (= #{1} + (->> (lib.js/returned-columns query -1) + (map :name) + frequencies + vals + set)))))) -- GitLab