Skip to content

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

Don't add remaps from join columns that aren't in join `:fields` (#63193)

Cam Saulcd9ebca53ab4
diff --git a/src/metabase/lib/core.cljc b/src/metabase/lib/core.cljc
index 0cfe1e44097..552d85565ea 100644
--- a/src/metabase/lib/core.cljc
+++ b/src/metabase/lib/core.cljc
@@ -290,6 +290,7 @@
join-condition-update-temporal-bucketing
join-conditions
join-fields
+ join-fields-to-add-to-parent-stage
join-lhs-display-name
join-strategy
joinable-columns
diff --git a/src/metabase/lib/join.cljc b/src/metabase/lib/join.cljc
index fb2e3718c10..da16d38da16 100644
--- a/src/metabase/lib/join.cljc
+++ b/src/metabase/lib/join.cljc
@@ -306,8 +306,10 @@
{:keys [stages], :as _join} :- ::lib.schema.join/join
options :- [:maybe ::lib.metadata.calculation/returned-columns.options]]
(let [join-query (assoc query :stages stages)]
- (lib.metadata.calculation/returned-columns join-query -1 (lib.util/query-stage join-query -1) options)))
+ (lib.metadata.calculation/returned-columns join-query -1 -1 options)))
+;;; TODO (Cam 9/5/25) -- rename this to `join-last-stage-returned-columns-relative-to-parent-stage` to make it's clear
+;;; it's talking about the columns returned by the join's last stage and has nothing to do with the join's `:fields`
(mu/defn join-returned-columns-relative-to-parent-stage :- ::lib.metadata.calculation/visible-columns
"All columns made 'visible' by the join (regardless of `:fields`) -- i.e., the columns returned by the last stage of
the join -- updated so their metadata is relative to the parent stage.
diff --git a/src/metabase/query_processor/middleware/add_remaps.clj b/src/metabase/query_processor/middleware/add_remaps.clj
index d33958f34ab..ec15a8a64bb 100644
--- a/src/metabase/query_processor/middleware/add_remaps.clj
+++ b/src/metabase/query_processor/middleware/add_remaps.clj
@@ -273,13 +273,31 @@
(mu/defn- add-fk-remaps-to-join :- [:maybe ::lib.schema.join/join]
"Update Join `:fields` to add entries for remapped columns. Update the join's last stage "
- [query :- ::lib.schema/query
+ [original-query :- ::lib.schema/query
+ updated-query :- ::lib.schema/query
path :- ::lib.walk/path
{:keys [fields], :as join} :- ::lib.schema.join/join]
- (when (and (sequential? fields) ; `:fields :all` should have already been resolved by this point.
+ (when (and (sequential? fields) ; `:fields :all` should have already been resolved by this point.
(seq fields))
- (let [join-last-stage-path (into (vec path) [:stages (dec (count (:stages join)))])]
- (when-let [last-stage-infos (remap-column-infos query join-last-stage-path)]
+ (let [original-join-field-ids (into #{}
+ (keep :id)
+ (lib.walk/apply-f-for-stage-at-path
+ lib/join-fields-to-add-to-parent-stage
+ original-query path join {:include-remaps? false}))
+ original-join-fields-includes-source? (fn [remap-info]
+ (some (fn [path]
+ (contains? original-join-field-ids (get-in remap-info path)))
+ [[:dimension :field-id]
+ ;; (not sure this is really something we want to support,
+ ;; but [[metabase.query-processor-test.remapping-test/remapped-columns-in-joined-source-queries-test]]
+ ;; alleges that you can include just the remapped column in
+ ;; join `:fields` and it's supposed to work)
+ [:dimension :human-readable-field-id]]))
+ join-last-stage-path (into (vec path) [:stages (dec (count (:stages join)))])]
+ (if-let [last-stage-infos (->> (remap-column-infos updated-query join-last-stage-path)
+ (filter original-join-fields-includes-source?)
+ not-empty)]
+ ;; we have remaps to add, add them to join `:fields`
(let [infos (for [info last-stage-infos]
(-> info
(update :original-field-clause lib/with-join-alias (:alias join))
@@ -297,18 +315,21 @@
simplify-ref-options
(lib/update-options dissoc :source-field))))
(add-fk-remaps-to-fields infos fields))]
- (assoc join :fields new-fields))))))
+ (assoc join :fields new-fields))
+ ;; there are no remaps to add, discard any changes that happen inside of the join (such as adding additional
+ ;; joins to power remaps that we're not using)
+ (get-in original-query path)))))
(mu/defn- add-fk-remaps :- ::query-and-remaps
"Add any Fields needed for `:external` remappings to the `:fields` clause of the query, and update `:order-by` and
`breakout` clauses as needed. Returns a map with `:query` (the updated query) and `:remaps` (a sequence
of [[:sequential ::external-remapping]] information maps)."
- [query :- ::lib.schema/query]
- (let [query' (lib.walk/walk query
+ [original-query :- ::lib.schema/query]
+ (let [query' (lib.walk/walk original-query
(fn [query path-type path stage-or-join]
(case path-type
:lib.walk/stage (add-fk-remaps-to-stage query path stage-or-join)
- :lib.walk/join (add-fk-remaps-to-join query path stage-or-join))))
+ :lib.walk/join (add-fk-remaps-to-join original-query query path stage-or-join))))
remaps (::remaps (lib/query-stage query' -1))]
{:query (lib.walk/walk-stages
query'
diff --git a/test/metabase/lib/join_test.cljc b/test/metabase/lib/join_test.cljc
index acf60afc319..1a403bc7ab8 100644
--- a/test/metabase/lib/join_test.cljc
+++ b/test/metabase/lib/join_test.cljc
@@ -1693,7 +1693,7 @@
{:name "NAME"}] ; remap of VENUES.CATEGORY_ID => CATEGORIES.NAME
exp-join2 [{:name "CATEGORY"}]
cols (fn [query]
- (lib/returned-columns query -1 (lib.util/query-stage query -1) {:include-remaps? true}))]
+ (lib/returned-columns query -1 -1 {:include-remaps? true}))]
(is (=? (concat exp-main exp-join1 exp-join2)
(-> base
(lib/join join1)
diff --git a/test/metabase/lib/metadata/calculation_test.cljc b/test/metabase/lib/metadata/calculation_test.cljc
index 0a9a0b57f97..55b85a2c8e3 100644
--- a/test/metabase/lib/metadata/calculation_test.cljc
+++ b/test/metabase/lib/metadata/calculation_test.cljc
@@ -13,7 +13,6 @@
[metabase.lib.test-metadata :as meta]
[metabase.lib.test-util :as lib.tu]
[metabase.lib.test-util.macros :as lib.tu.macros]
- [metabase.lib.util :as lib.util]
[metabase.lib.walk :as lib.walk]
[metabase.util :as u]
[metabase.util.malli :as mu]))
@@ -432,7 +431,7 @@
{:name "price10"}
{:name "NAME"}
{:name "SUBTOTAL"}]
- (lib/returned-columns query -1 (lib.util/query-stage query -1) {:include-remaps? true}))))))
+ (lib/returned-columns query -1 -1 {:include-remaps? true}))))))
(deftest ^:parallel remapped-columns-test-2-remapping-in-joins
(testing "explicitly joined columns with remaps are added after their join"
@@ -459,7 +458,7 @@
{:name "NAME"}]
exp-join2 [{:name "CATEGORY"}]
cols (fn [query]
- (lib/returned-columns query -1 (lib.util/query-stage query -1) {:include-remaps? true}))]
+ (lib/returned-columns query -1 -1 {:include-remaps? true}))]
(is (=? (concat exp-main exp-join1 exp-join2)
(-> base
(lib/join join1)
diff --git a/test/metabase/lib/stage_test.cljc b/test/metabase/lib/stage_test.cljc
index 9c7c2c045e3..4144de926fb 100644
--- a/test/metabase/lib/stage_test.cljc
+++ b/test/metabase/lib/stage_test.cljc
@@ -564,7 +564,7 @@
(testing (str "Stage number = " stage-number)
(is (=? [{:name "Total_number_of_people_from_each_state_separated_by_state_and_then_we_do_a_count"}
{:name "coun"}]
- (lib/returned-columns query stage-number (lib.util/query-stage query stage-number) {}))))))))
+ (lib/returned-columns query stage-number stage-number {}))))))))
(deftest ^:parallel remapped-columns-in-joined-source-queries-test
(testing "Remapped columns in joined source queries should work (#15578)"
@@ -616,7 +616,7 @@
;; this is already here, DO NOT add a duplicate of it.
&CATEGORIES__via__CATEGORY_ID.categories.name]}))]
(is (= ["ID" "NAME" "CATEGORY_ID" "LATITUDE" "LONGITUDE" "PRICE" "CATEGORIES__via__CATEGORY_ID__NAME"]
- (map :lib/desired-column-alias (lib/returned-columns query -1 (lib.util/query-stage query -1) {:include-remaps? true})))))))
+ (map :lib/desired-column-alias (lib/returned-columns query -1 -1 {:include-remaps? true})))))))
(deftest ^:parallel propagate-binning-info-test
(testing "binning info from previous stages should get propagated"
diff --git a/test/metabase/query_processor/middleware/add_remaps_test.clj b/test/metabase/query_processor/middleware/add_remaps_test.clj
index ef76d0d01f6..746af0035eb 100644
--- a/test/metabase/query_processor/middleware/add_remaps_test.clj
+++ b/test/metabase/query_processor/middleware/add_remaps_test.clj
@@ -3,6 +3,7 @@
[clojure.test :refer :all]
[metabase.lib.core :as lib]
[metabase.lib.metadata :as lib.metadata]
+ [metabase.lib.metadata.calculation :as lib.metadata.calculation]
[metabase.lib.test-metadata :as meta]
[metabase.lib.test-util :as lib.tu]
[metabase.lib.test-util.macros :as lib.tu.macros]
@@ -785,3 +786,42 @@
(-> query
qp.preprocess/preprocess
lib/->legacy-MBQL))))))
+
+(deftest ^:parallel do-not-include-remaps-in-joins-for-columns-that-are-not-in-fields-e2e-test
+ (testing "Do not include remaps in joins for columns that are not in :fields (#63165)"
+ (let [mp (-> meta/metadata-provider
+ (lib.tu/remap-metadata-provider (meta/id :orders :product-id) (meta/id :products :title)))
+ query (-> (lib/query mp (meta/table-metadata :people))
+ (lib/with-fields [(meta/field-metadata :people :id)])
+ (lib/join (-> (lib/join-clause (meta/table-metadata :orders))
+ (lib/with-join-fields [(meta/field-metadata :orders :id)]))))]
+ (testing `lib/returned-columns
+ (binding [lib.metadata.calculation/*display-name-style* :long]
+ (testing "remapping disabled"
+ (is (= ["ID"
+ "Orders → ID"]
+ (map :display-name (lib/returned-columns query -1 -1)))))
+ (testing "remapping enabled: should return the same columns"
+ (is (= ["ID"
+ "Orders → ID"]
+ (map :display-name (lib/returned-columns query -1 -1 {:include-remaps? true})))))))
+ (testing `qp.preprocess/preprocess
+ (is (=? {:fields [[:field {} (meta/id :people :id)]
+ [:field {:join-alias "Orders"} (meta/id :orders :id)]]
+ :joins [{:alias "Orders"
+ :fields [[:field {:join-alias "Orders"} (meta/id :orders :id)]]
+ :stages [{:joins (symbol "nil #_\"key is not present.\"")
+ :fields [[:field {} (meta/id :orders :id)]
+ [:field {} (meta/id :orders :user-id)]
+ [:field {} (meta/id :orders :product-id)]
+ [:field {} (meta/id :orders :subtotal)]
+ [:field {} (meta/id :orders :tax)]
+ [:field {} (meta/id :orders :total)]
+ [:field {} (meta/id :orders :discount)]
+ [:field {} (meta/id :orders :created-at)]
+ [:field {} (meta/id :orders :quantity)]]}]}]}
+ (-> (qp.preprocess/preprocess query) :stages first))))
+ (testing `qp.preprocess/query->expected-cols
+ (is (= ["ID"
+ "Orders → ID"]
+ (map :display_name (qp.preprocess/query->expected-cols query))))))))
diff --git a/test/metabase/query_processor_test/remapping_test.clj b/test/metabase/query_processor_test/remapping_test.clj
index 5b13dc8ac33..70cc684bbad 100644
--- a/test/metabase/query_processor_test/remapping_test.clj
+++ b/test/metabase/query_processor_test/remapping_test.clj
@@ -6,7 +6,6 @@
[metabase.lib.core :as lib]
[metabase.lib.metadata :as lib.metadata]
[metabase.lib.test-util :as lib.tu]
- [metabase.lib.util :as lib.util]
[metabase.query-processor :as qp]
[metabase.query-processor.middleware.add-remaps :as qp.add-remaps]
[metabase.query-processor.pivot :as qp.pivot]
@@ -335,7 +334,7 @@
:aggregation [[:sum $orders.quantity]]}
:alias "Orders"
:condition [:= $id &Orders.orders.product_id]
- ;; we can get title since product_id is remapped to title
+ ;; we can get products.title since orders.product_id is remapped to title
:fields [&Orders.title
&Orders.*sum/Integer]}]
:fields [$title $category]
@@ -551,7 +550,7 @@
(map :lib/desired-column-alias
(condp = f
#'lib/returned-columns
- (lib/returned-columns query -1 (lib.util/query-stage query -1) {:include-remaps? true})
+ (lib/returned-columns query -1 -1 {:include-remaps? true})
#'qp.preprocess/query->expected-cols
(qp.preprocess/query->expected-cols query)))))))