Skip to content

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

Preserve column selection on joined tables in drill through (#75617)

Riley Thompsona989608ef63b
diff --git a/src/metabase/lib/drill_thru/underlying_records.cljc b/src/metabase/lib/drill_thru/underlying_records.cljc
index c96ed9bf386..3decd201a59 100644
--- a/src/metabase/lib/drill_thru/underlying_records.cljc
+++ b/src/metabase/lib/drill_thru/underlying_records.cljc
@@ -48,6 +48,7 @@
[metabase.lib.types.isa :as lib.types.isa]
[metabase.lib.underlying :as lib.underlying]
[metabase.lib.util :as lib.util]
+ [metabase.util :as u]
[metabase.util.malli :as mu]
[metabase.util.performance :refer [mapv empty? #?(:clj for)]]))
@@ -203,8 +204,9 @@
;; Default: no filters to add.
nil)))
- ;; make all joins include all fields
- new-joins (mapv #(lib.join/with-join-fields % :all)
+ ;; A join with no `:fields` contributes no columns, so default it to `:all` (#48032).
+ ;; Joins with explicit `:fields` keep the user's column selection (#69105).
+ new-joins (mapv #(u/assoc-default % :fields :all)
(lib.join/joins agg-filtered))]
;; if we have new joins to add, update query with the new joins
(if (empty? new-joins)
diff --git a/test/metabase/lib/drill_thru/underlying_records_test.cljc b/test/metabase/lib/drill_thru/underlying_records_test.cljc
index a3cdcd7ba8d..b3e96f38503 100644
--- a/test/metabase/lib/drill_thru/underlying_records_test.cljc
+++ b/test/metabase/lib/drill_thru/underlying_records_test.cljc
@@ -576,6 +576,43 @@
first
lib.join/join-fields))))))
+(deftest ^:parallel preserve-column-selection-after-drill-thru-test
+ (testing "underlying records should keep the join's column selection (#69105)"
+ (let [query (-> (lib/query meta/metadata-provider (meta/table-metadata :orders))
+ (lib/join (-> (lib/join-clause (meta/table-metadata :products))
+ (lib/with-join-fields [(meta/field-metadata :products :id)
+ (meta/field-metadata :products :category)])))
+ (lib/aggregate (lib/count)))
+ query (lib/breakout query (m/find-first #(= (:name %) "CATEGORY")
+ (lib/breakoutable-columns query)))
+ count-col (m/find-first #(= (:name %) "count")
+ (lib/returned-columns query))
+ category-col (m/find-first #(= (:name %) "CATEGORY")
+ (lib/returned-columns query))
+ context {:column count-col
+ :column-ref (lib/ref count-col)
+ :value 4939
+ :row [{:column category-col
+ :column-ref (lib/ref category-col)
+ :value "Gadget"}
+ {:column count-col
+ :column-ref (lib/ref count-col)
+ :value 4939}]
+ :dimensions [{:column category-col
+ :column-ref (lib/ref category-col)
+ :value "Gadget"}]}
+ drill (m/find-first #(= (:type %) :drill-thru/underlying-records)
+ (lib/available-drill-thrus query context))
+ drilled (lib/drill-thru query drill)]
+ (is (=? [[:field {} (meta/id :products :id)]
+ [:field {} (meta/id :products :category)]]
+ (-> drilled lib.join/joins first lib.join/join-fields)))
+ (is (= #{(meta/id :products :id) (meta/id :products :category)}
+ (into #{}
+ (comp (filter #(= (:table-id %) (meta/id :products)))
+ (map :id))
+ (lib/returned-columns drilled)))))))
+
(deftest ^:parallel breakout-by-expression-test
(testing "underlying records for a query with a breakout on an expression should produce a correct ref (#59005)"
(let [base (-> (lib/query meta/metadata-provider (meta/table-metadata :orders))