Skip to content
Snippets Groups Projects
Unverified Commit 3ccf4d41 authored by Braden Shepherdson's avatar Braden Shepherdson Committed by GitHub
Browse files

[QP] Fix breakouts on a nested model (#44418)

Fixes #43993 for real.

The earlier fix #44182 was needlessly restricting the
"nominal refs" check to fields which were using numeric IDs,
when really any match on nominal refs is valid.
parent 891260cb
No related branches found
No related tags found
No related merge requests found
......@@ -66,12 +66,11 @@
used-fields (into #{} (map keep-source+alias-props) usages)
nominal-fields (into #{} (keep ->nominal-ref) usages)
nfc-roots (into #{} (keep nfc-root) used-fields)]
(letfn [(used? [[_tag id-or-name {::add/keys [source-table]}, :as field]]
(letfn [(used? [[_tag _id-or-name {::add/keys [source-table]}, :as field]]
(or (contains? used-fields (keep-source+alias-props field))
;; We should also consider a Field to be used if we're referring to it with a nominal field literal
;; ref in the next stage -- that's actually how you're supposed to be doing it anyway.
(and (integer? id-or-name)
(= source-table ::add/source)
(and (= source-table ::add/source)
(contains? nominal-fields (->nominal-ref field)))
(contains? nfc-roots (field-id-props field))))
(used?* [field]
......
......@@ -313,6 +313,43 @@
(assoc :info {:visualization-settings viz-settings})
qp.pivot/run-pivot-query))))))))))
(deftest nested-models-with-expressions-pivot-breakout-names-test
(testing "#43993 again - breakouts on an expression from the inner model should pass"
(mt/with-temp [Card model1 {:type :model
:dataset_query
(mt/mbql-query products
{:source-table $$products
:expressions {"Rating Bucket" [:floor $products.rating]}})}
Card model2 {:type :model
:dataset_query
(mt/mbql-query orders
{:source-table $$orders
:joins [{:source-table (str "card__" (u/the-id model1))
:alias "model A - Product"
:fields :all
:condition [:= $orders.product_id
[:field %products.id
{:join-alias "model A - Product"}]]}]})}]
(testing "Column aliasing works when joining an expression in an inner model"
(let [query (mt/mbql-query
orders {:source-table (str "card__" (u/the-id model2))
:aggregation [[:sum [:field "SUBTOTAL" {:base-type :type/Number}]]]
:breakout [[:field "Rating Bucket" {:base-type :type/Number
:join-alias "model A - Product"}]]})
viz-settings (mt/$ids orders {:pivot_table.column_split
{:columns [[:field "Rating Bucket"
{:base-type :type/Number
:join-alias "model A - Product"}]]}})]
(testing "for a regular query"
(is (=? {:status :completed}
(qp/process-query query))))
(testing "and a pivot query"
(is (=? {:status :completed
:row_count 6}
(-> query
(assoc :info {:visualization-settings viz-settings})
qp.pivot/run-pivot-query)))))))))
(deftest ^:parallel dont-return-too-many-rows-test
(testing "Make sure pivot queries don't return too many rows (#14329)"
(let [results (qp.pivot/run-pivot-query (test-query))
......
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