The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
[QP] Reject fuzzy match of join condition LHS to that join's RHS (#67804)
Braden Shepherdsoncd5bc6bf723f
diff --git a/src/metabase/query_processor/error_type.clj b/src/metabase/query_processor/error_type.cljindex 660665a4643..ba2e4e4192c 100644--- a/src/metabase/query_processor/error_type.clj+++ b/src/metabase/query_processor/error_type.clj@@ -83,6 +83,11 @@:parent invalid-query:show-in-embeds? true)+(deferror dangling-lhs-ref-in-join-condition+ "The query contains a join condition which refers to a field on the LHS of the join which is no longer available."+ :parent invalid-query+ :show-in-embeds? true)+;;;; ### Server-Side Errors(deferror serverdiff --git a/src/metabase/query_processor/util/add_alias_info.clj b/src/metabase/query_processor/util/add_alias_info.cljindex 20c8ba21e37..cb8a73750d6 100644--- a/src/metabase/query_processor/util/add_alias_info.clj+++ b/src/metabase/query_processor/util/add_alias_info.clj@@ -58,6 +58,7 @@[metabase.query-processor.middleware.annotate.legacy-helper-fns :as annotate.legacy-helper-fns]^{:clj-kondo/ignore [:deprecated-namespace]} [metabase.query-processor.store :as qp.store][metabase.util :as u]+ [metabase.util.i18n :refer [tru]][metabase.util.log :as log][metabase.util.malli :as mu][metabase.util.malli.registry :as mr]@@ -399,6 +400,21 @@::source-alias source-alias::source-table source-table)))+(defn- cartesian-join-condition-exception+ "Exception thrown when a join condition contains a field ref to an 'other' field, ie. one whose `:join-alias` is not+ the containing join clause, but which ends up getting resolved to a column from that join anyway. This can happen+ due to the fuzzy matching of malformed field refs, which might (for example) find the `ID` column of the join when+ the original `ID` column it was supposed to refer to has been removed from an upstream question. See #67667.++ Such a join condition compares two columns from the RHS of the join, and is therefore incoherent. It might give an+ empty result set, a full Cartesian join, or anything in between, depending on exactly what RHS column was resolved."+ [field-ref]+ (throw+ (ex-info+ (tru "Join condition refers to a column from outside this join, but it was incorrectly resolved to a column from this join.")+ {:type qp.error-type/dangling-lhs-ref-in-join-condition+ :ref field-ref})))+(mu/defn- add-alias-info-to-join-conditions :- ::lib.schema.join/join"Add alias info to refs in join `:conditions`. Join `:fields` do not need to be updated since they are only used toupdate parent stage `:fields` as appropriate (which will have already been done by now) and otherwise do not directly@@ -409,6 +425,11 @@(let [parent-stage-path (lib.walk/join-parent-stage-path join-path)update-other-ref (fn [field-ref](let [col (resolve-field-ref query parent-stage-path field-ref)]+ ;; Sanity check - reject an "other" ref resolved to also come from this join!+ ;; That creates a broken condition and a Cartesian join.+ (when (and (= (:lib/source col) :source/joins)+ (= (:source-alias col) (:alias join)))+ (throw (cartesian-join-condition-exception field-ref)))(add-source-to-field-ref query parent-stage-path field-ref col)))update-conditions (fn [conditions];; the only kind of ref join conditions can have is a `:field` refdiff --git a/test/metabase/query_processor/explicit_joins_test.clj b/test/metabase/query_processor/explicit_joins_test.cljindex 6ad1bc9e1bc..7ec8ce12ca6 100644--- a/test/metabase/query_processor/explicit_joins_test.clj+++ b/test/metabase/query_processor/explicit_joins_test.clj@@ -10,6 +10,7 @@[metabase.lib.core :as lib][metabase.lib.expression :as lib.expression][metabase.lib.metadata :as lib.metadata]+ [metabase.lib.test-metadata :as meta][metabase.lib.test-util :as lib.tu][metabase.lib.test-util.mocks-31769 :as lib.tu.mocks-31769][metabase.query-processor :as qp]@@ -1571,3 +1572,27 @@2 1 123 110.93 6.1 117.03 nil "2018-05-15T08:04:04.58Z" 32 1 123 110.93 6.1 117.03 nil "2018-05-15T08:04:04.58Z" 3]](mt/rows (qp/process-query q2))))))))))++(deftest ^:parallel dangling-join-condition-lhs-errors-if-fuzzy-matched-to-rhs-test+ (testing (str "When upstream changes leave a dangling ref in a join condition LHS, QP throws if it is fuzzy-matched"+ "to a column from the RHS of the same join (#67667)")+ (let [mp meta/metadata-provider+ ;; Q1(a) is a plain query of Products+ q1a (lib/query mp (lib.metadata/table mp (meta/id :products)))+ mp (lib.tu/mock-metadata-provider+ mp+ {:cards [{:id 1+ :dataset-query q1a}]})+ ;; Q2 joins Q1(a) to Orders on Products.ID = Orders.PRODUCT_ID.+ q2 (-> (lib/query mp (lib.metadata/card mp 1))+ (lib/join (lib/join-clause (lib.metadata/table mp (meta/id :orders)))))+ ;; Q1(b) is an "updated" Q1(a), now an aggregated query, COUNT() by Products.CATEGORY.+ ;; It doesn't really matter what edit is made to Q1(a), just that its `Products.ID` is now gone.+ q1b (-> (lib/query mp (lib.metadata/table mp (meta/id :products)))+ (lib/aggregate (lib/count))+ (lib/breakout (meta/field-metadata :products :category)))+ mp (lib.tu/mock-metadata-provider+ mp {:cards [{:id 1+ :dataset-query q1b}]})]+ (is (thrown-with-msg? Exception #"Join condition refers to a column from outside this join"+ (qp.compile/compile (lib/query mp q2)))))))