Skip to content

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

Fix self join column remapping (#75339)

Riley Thompson498c7e3552b7
diff --git a/src/metabase/query_processor/middleware/add_remaps.clj b/src/metabase/query_processor/middleware/add_remaps.clj
index 575518cd74b..3917ec8054e 100644
--- a/src/metabase/query_processor/middleware/add_remaps.clj
+++ b/src/metabase/query_processor/middleware/add_remaps.clj
@@ -453,17 +453,19 @@
;; if this is a column we're remapping FROM, we need to add information about which column we're remapping
;; TO
(when (= dimension-id original-field-dimension-id)
- {:remapped_to (or (some (fn [{{::keys [new-field-dimension-id]} :options, target-name :name}]
- (when (= new-field-dimension-id dimension-id)
- target-name))
+ {:remapped_to (or (some (fn [{{::keys [new-field-dimension-id]} :options, :as target-col}]
+ (when (and (= new-field-dimension-id dimension-id)
+ (= (:lib/join-alias target-col) (:lib/join-alias column)))
+ (:name target-col)))
columns)
to-name)})
;; if this is a column we're remapping TO, we need to add information about which column we're remapping
;; FROM
(when (= dimension-id new-field-dimension-id)
- {:remapped_from (or (some (fn [{{::keys [original-field-dimension-id]} :options, source-name :name}]
- (when (= original-field-dimension-id dimension-id)
- source-name))
+ {:remapped_from (or (some (fn [{{::keys [original-field-dimension-id]} :options, :as source-col}]
+ (when (and (= original-field-dimension-id dimension-id)
+ (= (:lib/join-alias source-col) (:lib/join-alias column)))
+ (:name source-col)))
columns)
from-name)
:display_name from-display-name}))
diff --git a/test/metabase/query_processor/middleware/add_remaps_test.clj b/test/metabase/query_processor/middleware/add_remaps_test.clj
index 4d05b9aca29..58d221171f3 100644
--- a/test/metabase/query_processor/middleware/add_remaps_test.clj
+++ b/test/metabase/query_processor/middleware/add_remaps_test.clj
@@ -832,3 +832,32 @@
(is (= ["ID"
"Orders → ID"]
(map :display_name (qp.preprocess/query->expected-cols query))))))))
+
+(deftest ^:parallel self-join-with-remapped-columns-uses-remapping-test
+ (let [mp (mt/metadata-provider)
+ orders (lib.metadata/table mp (mt/id :orders))
+ order-id (lib.metadata/field mp (mt/id :orders :id))
+ user-id (lib.metadata/field mp (mt/id :orders :user_id))
+ user-email (lib.metadata/field mp (mt/id :people :email))
+ rm-mp (lib.tu/remap-metadata-provider mp user-id user-email)
+ query (-> (lib/query rm-mp orders)
+ (lib/with-fields [order-id user-id])
+ (lib/join (-> (lib/join-clause orders [(lib/= order-id order-id)])
+ (lib/with-join-fields [order-id user-id])))
+ (lib/limit 3))
+ result (qp/process-query query)]
+ (is (= [[1 1 1 1 "[email protected]" "[email protected]"]
+ (mt/rows result)))
+ (is (= [{:name "ID"}
+ {:name "USER_ID", :remapped_to "EMAIL"}
+ {:name "ID_2"}
+ {:name "USER_ID_2", :remapped_to "EMAIL_2"}
+ {:name "EMAIL", :remapped_from "USER_ID"}
+ {:name "EMAIL_2", :remapped_from "USER_ID_2"}]
+ (->> result
+ :data
+ :results_metadata
+ :columns
+ (mapv #(select-keys % [:name :remapped_to :remapped_from])))))))