Skip to content

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

Simpler fix than first attempt. (#67300)

Timothy S. Dean6f00f47c479b
diff --git a/src/metabase/lib/drill_thru/pivot.cljc b/src/metabase/lib/drill_thru/pivot.cljc
index 906dc499305..816195f39bc 100644
--- a/src/metabase/lib/drill_thru/pivot.cljc
+++ b/src/metabase/lib/drill_thru/pivot.cljc
@@ -173,9 +173,11 @@
(get-in drill-thru [:pivots pivot-type]))
(defn- breakouts->filters [query stage-number {:keys [column value] :as _dimension}]
- (let [resolved-column (lib.drill-thru.common/breakout->resolved-column query stage-number column)]
+ (let [col-for-stage (or (lib.underlying/top-level-column query column)
+ column)
+ resolved-column (lib.drill-thru.common/breakout->resolved-column query stage-number col-for-stage)]
(-> query
- (lib.breakout/remove-existing-breakouts-for-column stage-number column)
+ (lib.breakout/remove-existing-breakouts-for-column stage-number col-for-stage)
(lib.filter/filter stage-number (lib.filter/= resolved-column value)))))
;; Pivot drills are in play when clicking an aggregation cell. Pivoting is applied by:
diff --git a/test/metabase/lib/drill_thru/pivot_test.cljc b/test/metabase/lib/drill_thru/pivot_test.cljc
index 612f31c4c47..d9518ed7b2f 100644
--- a/test/metabase/lib/drill_thru/pivot_test.cljc
+++ b/test/metabase/lib/drill_thru/pivot_test.cljc
@@ -293,7 +293,6 @@
(lib.drill-thru.tu/append-filter-stage "count"))
:expected-query (-> base-case
:expected-query
- (assoc-in [:stages 0 :filters 0 2 2] "CREATED_AT")
(lib.drill-thru.tu/append-filter-stage-to-test-expectation "count"))})))
(deftest ^:parallel returns-pivot-drill-boolean-column-test
diff --git a/test/metabase/query_processor/drill_thru_e2e_test.clj b/test/metabase/query_processor/drill_thru_e2e_test.clj
index 69fdda8812c..e88f7a559a0 100644
--- a/test/metabase/query_processor/drill_thru_e2e_test.clj
+++ b/test/metabase/query_processor/drill_thru_e2e_test.clj
@@ -84,3 +84,61 @@
(mt/formatted-rows
[double long]
(qp/process-query query')))))))))
+
+(deftest ^:parallel pivot-drill-on-implicit-join-with-filter-after-aggregation-test
+ (testing "Pivot drill on implicitly joined column with filter-after-aggregation should work (#67228)"
+ ;; Reproduces a bug where applying a pivot drill-through on a query with:
+ ;; 1. Breakout by an implicitly joined column (e.g., Products.Category via Orders.product_id)
+ ;; 2. A filter stage after the aggregation (e.g., Count > 1)
+ ;; Would produce an invalid query with a filter in stage 0 referencing a column by its
+ ;; output alias (e.g., "PRODUCTS__via__PRODUCT_ID__CATEGORY") instead of by field ID.
+ (mt/dataset test-data
+ (qp.store/with-metadata-provider (mt/id)
+ (let [orders (lib.metadata/table (qp.store/metadata-provider) (mt/id :orders))
+ initial-query (-> (lib/query (qp.store/metadata-provider) orders)
+ (lib/aggregate (lib/count)))
+ products-cat (m/find-first #(and (= (:name %) "CATEGORY")
+ (= (:table-id %) (mt/id :products)))
+ (lib/breakoutable-columns initial-query))
+ base-query (lib/breakout initial-query products-cat)
+ query (-> base-query
+ lib/append-stage
+ (as-> q
+ (let [count-col (m/find-first #(= (:name %) "count")
+ (lib/filterable-columns q))]
+ (lib/filter q (lib/> count-col 1)))))
+ returned-cols (lib/returned-columns query)
+ count-col (m/find-first #(= (:name %) "count") returned-cols)
+ category-col (m/find-first #(= (:name %) "CATEGORY") returned-cols)
+ drill-context {:column count-col
+ :column-ref (lib.ref/ref count-col)
+ :value 42
+ :row [{:column category-col
+ :column-ref (lib.ref/ref category-col)
+ :value "Doohickey"}
+ {:column count-col
+ :column-ref (lib.ref/ref count-col)
+ :value 42}]
+ :dimensions [{:column category-col
+ :column-ref (lib.ref/ref category-col)
+ :value "Doohickey"}]}
+ pivot-drill (m/find-first #(= (:type %) :drill-thru/pivot)
+ (lib/available-drill-thrus query drill-context))
+ _ (is (some? pivot-drill))
+ people-source (m/find-first #(= (:name %) "SOURCE")
+ (lib/pivot-columns-for-type pivot-drill :category))
+ query' (lib/drill-thru query -1 nil pivot-drill people-source)]
+ ;; Overspecification?
+ (is (=? {:stages [{:source-table (mt/id :orders)
+ :aggregation [[:count {}]]
+ :breakout [[:field {} (mt/id :people :source)]]
+ :filters [[:= {} [:field {} (mt/id :products :category)] "Doohickey"]]}
+ {:filters [[:> {} [:field {} "count"] 1]]}]}
+ query'))
+ (mt/with-native-query-testing-context query'
+ (is (= [["Affiliate" 783]
+ ["Facebook" 816]
+ ["Google" 844]
+ ["Organic" 738]
+ ["Twitter" 795]]
+ (mt/formatted-rows [str int] (qp/process-query query'))))))))))