Skip to content
Snippets Groups Projects
Unverified Commit 5bc320bb authored by github-automation-metabase's avatar github-automation-metabase Committed by GitHub
Browse files

Applying the underlying records filter to a query with a join display… (#50802) (#50886)


Applying the underlying records filter to a query with a join displays fields from the join

Co-authored-by: default avatarWilliam <william@metabase.com>
parent 30c04a1d
No related branches found
No related tags found
No related merge requests found
......@@ -34,6 +34,7 @@
[metabase.lib.binning :as lib.binning]
[metabase.lib.drill-thru.common :as lib.drill-thru.common]
[metabase.lib.filter :as lib.filter]
[metabase.lib.join :as lib.join]
[metabase.lib.metadata :as lib.metadata]
[metabase.lib.metadata.calculation :as lib.metadata.calculation]
[metabase.lib.options :as lib.options]
......@@ -186,20 +187,27 @@
;; The column-ref should be an aggregation ref - look up the full aggregation.
aggregation (when-let [agg-uuid (last column-ref)]
(m/find-first #(= (lib.options/uuid %) agg-uuid)
(lib.aggregation/aggregations query -1)))]
;; Apply the filters derived from the aggregation.
(reduce #(lib.filter/filter %1 -1 %2)
filtered
;; If we found an aggregation, check if it implies further filtering.
;; Simple aggregations like :sum don't add more filters; metrics or fancy aggregations like :sum-where do.
(when aggregation
(case (first aggregation)
;; Fancy aggregations that filter the input - the filter is the last part of the aggregation.
(:sum-where :count-where :share)
[(last aggregation)]
;; Default: no filters to add.
nil)))))
(lib.aggregation/aggregations query -1)))
;; Apply the filters derived from the aggregation.
agg-filtered (reduce #(lib.filter/filter %1 -1 %2)
filtered
;; If we found an aggregation, check if it implies further filtering.
;; Simple aggregations like :sum don't add more filters; metrics or fancy aggregations like :sum-where do.
(when aggregation
(case (first aggregation)
;; Fancy aggregations that filter the input - the filter is the last part of the aggregation.
(:sum-where :count-where :share)
[(last aggregation)]
;; Default: no filters to add.
nil)))
;; make all joins include all fields
new-joins (mapv #(lib.join/with-join-fields % :all)
(lib.join/joins agg-filtered))]
;; if we have new joins to add, update query with the new joins
(if (empty? new-joins)
agg-filtered
(lib.util/update-query-stage agg-filtered -1 assoc :joins new-joins))))
(defmethod lib.drill-thru.common/drill-thru-method :drill-thru/underlying-records
[query _stage-number context & _]
......
......@@ -9,6 +9,7 @@
[metabase.lib.drill-thru :as lib.drill-thru]
[metabase.lib.drill-thru.test-util :as lib.drill-thru.tu]
[metabase.lib.drill-thru.test-util.canned :as canned]
[metabase.lib.join :as lib.join]
[metabase.lib.metadata :as lib.metadata]
[metabase.lib.options :as lib.options]
[metabase.lib.test-metadata :as meta]
......@@ -506,3 +507,38 @@
:breakout (symbol "nil #_\"key is not present.\"")
:fields (symbol "nil #_\"key is not present.\"")}]}
(lib/drill-thru query drill))))))
(deftest ^:parallel include-all-joined-columns-test
(testing "underlying records for a query with a join should include all fields from the join (#48032)"
(let [query (-> (lib/query meta/metadata-provider (meta/table-metadata :orders))
(lib/join (meta/table-metadata :people))
(lib/aggregate (lib/count))
(lib/breakout (-> (meta/field-metadata :orders :discount)
(lib/with-binning {:strategy :default}))))
count-col (m/find-first #(= (:name %) "count")
(lib/returned-columns query))
_ (is (some? count-col))
discount-col (m/find-first #(= (:name %) "DISCOUNT")
(lib/returned-columns query))
_ (is (some? discount-col))
context {:column count-col
:column-ref (lib/ref count-col)
:value 16845
:row [{:column discount-col
:column-ref (lib/ref discount-col)
:value nil}
{:column count-col
:column-ref (lib/ref count-col)
:value 16845}]
:dimensions [{:column discount-col
:column-ref (lib/ref discount-col)
:value nil}]}
drill (m/find-first #(= (:type %) :drill-thru/underlying-records)
(lib/available-drill-thrus query context))]
(is (some? drill))
(is (=? :all
(-> query
(lib/drill-thru drill)
lib.join/joins
first
lib.join/join-fields))))))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment