Skip to content
Snippets Groups Projects
Unverified Commit a2336e64 authored by Case Nelson's avatar Case Nelson Committed by GitHub
Browse files

[MLv2] Fix join clause dispatches for card and table sources (#32769)

parent 6a23cb55
Branches
Tags
No related merge requests found
......@@ -335,6 +335,20 @@
:stages [mbql-stage]}
lib.options/ensure-uuid))
(defmethod join-clause-method :metadata/card
[card]
(-> {:lib/type :mbql/join
:stages [{:source-card (:id card)
:lib/type :mbql.stage/mbql}]}
lib.options/ensure-uuid))
(defmethod join-clause-method :metadata/table
[table]
(-> {:lib/type :mbql/join
:stages [{:source-table (:id table)
:lib/type :mbql.stage/mbql}]}
lib.options/ensure-uuid))
(defn- with-join-conditions-add-alias-to-rhses
"Add `join-alias` to the RHS of all [[standard-join-condition?]] `conditions` that don't already have a `:join-alias`.
If an RHS already has a `:join-alias`, don't second guess what was already explicitly specified."
......
......@@ -63,7 +63,29 @@
:source-table (meta/id :orders)}]
:lib/options {:lib/uuid string?}
:fields :all}
(lib/join-clause (meta/table-metadata :orders))))))
(lib/join-clause (meta/table-metadata :orders)))))
(testing "source-card"
(let [query {:lib/type :mbql/query
:lib/metadata lib.tu/metadata-provider-with-mock-cards
:database (meta/id)
:stages [{:lib/type :mbql.stage/mbql
:source-card (:id (lib.tu/mock-cards :orders))}]}
product-card (lib.tu/mock-cards :products)
[_ orders-product-id] (lib/join-condition-lhs-columns query product-card nil nil)
[products-id] (lib/join-condition-rhs-columns query product-card orders-product-id nil)]
(is (=? {:stages [{:joins [{:stages [{:source-card (:id product-card)}]}]}]}
(lib/join query (lib/join-clause product-card [(lib/= orders-product-id products-id)]))))))
(testing "source-table"
(let [query {:lib/type :mbql/query
:lib/metadata lib.tu/metadata-provider-with-mock-cards
:database (meta/id)
:stages [{:lib/type :mbql.stage/mbql
:source-card (:id (lib.tu/mock-cards :orders))}]}
product-table (meta/table-metadata :products)
[_ orders-product-id] (lib/join-condition-lhs-columns query product-table nil nil)
[products-id] (lib/join-condition-rhs-columns query product-table orders-product-id nil)]
(is (=? {:stages [{:joins [{:stages [{:source-table (:id product-table)}]}]}]}
(lib/join query (lib/join-clause product-table [(lib/= orders-product-id products-id)])))))))
(deftest ^:parallel join-saved-question-test
(is (=? {:lib/type :mbql/query
......
......@@ -123,7 +123,8 @@
{:arglists '([table-name])}
keyword)
(defmulti ^:private field-metadata-method
(defmulti field-metadata-method
"Metadata for fields"
{:arglists '([table-name field-name])}
(fn [table-name field-name]
[(keyword table-name) (keyword field-name)]))
......
......@@ -261,3 +261,31 @@
meta/metadata-provider
(mock-metadata-provider
{:cards [categories-native-card]})))
(def mock-cards
"Map of mock MBQL query Card against the test tables."
(into {}
(for [[idx table] (m/indexed [:categories
:checkins
:users
:venues
:products
:orders
:people
:reviews])]
[table {:lib/type :metadata/card
:id idx
:name (str "Mock " (name table) " card")
:dataset-query {:database (meta/id)
:type :query
:query {:source-table (meta/id table)}}
:result-metadata (for [[[meta-table meta-col] _] (methods meta/field-metadata-method)
:when (= table meta-table)]
(dissoc (meta/field-metadata table meta-col) :id :table-id))}])))
(def metadata-provider-with-mock-cards
"A metadata provider with all of the [[mock-cards]]. Composed with the normal [[meta/metadata-provider]]."
(lib.metadata.composed-provider/composed-metadata-provider
meta/metadata-provider
(mock-metadata-provider
{:cards (vals mock-cards)})))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment