Skip to content

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

Fix metric explorer for metrics with joins (#71741)

John Swanson1f90a2172ff3
diff --git a/src/metabase/lib_metric/ast/compile.cljc b/src/metabase/lib_metric/ast/compile.cljc
index 302a95f4c01..891ec442985 100644
--- a/src/metabase/lib_metric/ast/compile.cljc
+++ b/src/metabase/lib_metric/ast/compile.cljc
@@ -168,9 +168,15 @@
(seq (:joins source)))
(defn- compile-join-nodes
- "Compile AST join nodes to pMBQL join clauses."
+ "Compile AST join nodes to pMBQL join clauses.
+ Forces `:fields :all` so that all joined columns are visible in stage 1 of
+ two-stage queries (the only context that calls this function). The dimension
+ system advertises all joined columns regardless of the original `:fields`
+ setting, so the compile phase must match."
[join-nodes]
- (perf/mapv :mbql-join join-nodes))
+ (perf/mapv (fn [join-node]
+ (assoc (:mbql-join join-node) :fields :all))
+ join-nodes))
;;; -------------------- Main Compilation --------------------
diff --git a/test/metabase/lib_metric/ast/compile_test.cljc b/test/metabase/lib_metric/ast/compile_test.cljc
index 53b9cfafd31..3571eee8881 100644
--- a/test/metabase/lib_metric/ast/compile_test.cljc
+++ b/test/metabase/lib_metric/ast/compile_test.cljc
@@ -589,6 +589,25 @@
(is (seq (:joins stage-0)))
(is (nil? (:joins stage-1))))))
+(deftest ^:parallel compile-two-stage-join-fields-test
+ (testing "join without :fields gets :all in stage 0"
+ (let [join-no-fields (dissoc sample-join :fields)
+ ast (assoc-in sample-ast [:source :joins]
+ [{:node/type :ast/join
+ :mbql-join join-no-fields}])
+ result (ast.compile/compile-to-mbql ast)
+ stage-0-join (first (:joins (first (:stages result))))]
+ (is (= :all (:fields stage-0-join)))))
+
+ (testing "join with explicit :fields is overridden to :all (dimension system advertises all joined columns)"
+ (let [join-with-fields (assoc sample-join :fields [[:field {:join-alias "Products"} 20]])
+ ast (assoc-in sample-ast [:source :joins]
+ [{:node/type :ast/join
+ :mbql-join join-with-fields}])
+ result (ast.compile/compile-to-mbql ast)
+ stage-0-join (first (:joins (first (:stages result))))]
+ (is (= :all (:fields stage-0-join))))))
+
(deftest ^:parallel compile-two-stage-filter-separation-test
(let [source-filter {:node/type :filter/mbql
:clause [:= {} [:field {} 30] "active"]}
diff --git a/test/metabase/metrics/api_dataset_test.clj b/test/metabase/metrics/api_dataset_test.clj
index 9536e6b23ce..ece8e1a3681 100644
--- a/test/metabase/metrics/api_dataset_test.clj
+++ b/test/metabase/metrics/api_dataset_test.clj
@@ -1133,6 +1133,68 @@
(is (= 49 (:row_count response))
"should have 49 months of order data")))))))
+(defn- create-orders-products-join-sum-price-query
+ "Create a pMBQL query joining Orders with Products, aggregating SUM(Products.PRICE).
+ This exercises the case where the aggregation references a column from the joined table,
+ which requires the join's fields to be visible across the two-stage query boundary."
+ ([]
+ (create-orders-products-join-sum-price-query nil))
+ ([join-fields]
+ (let [mp (mt/metadata-provider)
+ orders-table (lib.metadata/table mp (mt/id :orders))
+ products-table (lib.metadata/table mp (mt/id :products))
+ orders-product-id (lib.metadata/field mp (mt/id :orders :product_id))
+ products-id (lib.metadata/field mp (mt/id :products :id))
+ products-price (lib.metadata/field mp (mt/id :products :price))]
+ (-> (lib/query mp orders-table)
+ (lib/join (cond-> (lib/join-clause products-table
+ [(lib/= orders-product-id
+ (lib/with-join-alias products-id "Products"))])
+ join-fields (lib/with-join-fields join-fields)))
+ (lib/aggregate (lib/sum (lib/with-join-alias products-price "Products")))))))
+
+(deftest dataset-metric-with-join-aggregation-on-joined-column-test
+ (testing "POST /api/metric/dataset with aggregation on a joined table column"
+ (let [query (create-orders-products-join-sum-price-query)]
+ (mt/with-temp [:model/Card metric {:name "Sum of Product Prices"
+ :type :metric
+ :dataset_query query}]
+ (testing "basic execution without projection"
+ (let [response (dataset-request {:expression [:metric {:lib/uuid "a"} (:id metric)]})]
+ (is (= "completed" (:status response)))
+ (is (= 1 (:row_count response)))
+ (is (pos? (first-result response)))))
+ (testing "with temporal projection on base table column"
+ (let [hydrated (hydrate-metric (:id metric))
+ created-dim (find-dimension-by-name hydrated "CREATED_AT")]
+ (is (some? created-dim) "CREATED_AT dimension should exist")
+ (let [response (dataset-request {:expression [:metric {:lib/uuid "a"} (:id metric)]
+ :projections [{:type :metric :id (:id metric)
+ :projection [[:dimension {:temporal-unit :month} (:id created-dim)]]}]})]
+ (is (= "completed" (:status response)))
+ (is (pos? (:row_count response))))))))))
+
+(deftest dataset-metric-with-join-explicit-fields-projection-test
+ (testing "POST /api/metric/dataset with join that has explicit :fields"
+ (let [mp (mt/metadata-provider)
+ products-price (lib.metadata/field mp (mt/id :products :price))
+ query (create-orders-products-join-sum-price-query
+ [(lib/with-join-alias products-price "Products")])]
+ (mt/with-temp [:model/Card metric {:name "Sum Price (explicit fields)"
+ :type :metric
+ :dataset_query query}]
+ (let [hydrated (hydrate-metric (:id metric))
+ category-dim (find-dimension-by-name hydrated "CATEGORY")]
+ (testing "CATEGORY dimension is advertised even though join only selects PRICE"
+ (is (some? category-dim)))
+ (testing "projecting by CATEGORY succeeds despite explicit :fields on join"
+ (let [response (dataset-request {:expression [:metric {:lib/uuid "a"} (:id metric)]
+ :projections [{:type :metric :id (:id metric)
+ :projection [[:dimension {} (:id category-dim)]]}]})]
+ (is (= "completed" (:status response)))
+ (is (= 4 (:row_count response))
+ "should have 4 rows, one per product category"))))))))
+
;;; +----------------------------------------------------------------------------------------------------------------+
;;; | Category 11: Source-Card (Model) Metrics |
;;; +----------------------------------------------------------------------------------------------------------------+