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
Use source alias for breakout with implicit join (#67480)
Riley Thompson63ce826faa48
diff --git a/src/metabase/query_processor/util/add_alias_info.clj b/src/metabase/query_processor/util/add_alias_info.cljindex c940484eea5..20c8ba21e37 100644--- a/src/metabase/query_processor/util/add_alias_info.clj+++ b/src/metabase/query_processor/util/add_alias_info.clj@@ -207,11 +207,21 @@:source/native) ::none))(defn- add-source-to-field-ref [query path field-ref col]- (lib/update-options- field-ref #(-> %- (assoc ::source-table (source-table query path col)- ::source-alias (escaped-source-alias query path (:metabase.lib.join/join-alias col) (:lib/source-column-alias col)))- (m/assoc-some ::nfc-path (not-empty (:nfc-path col))))))+ (let [join-alias (:metabase.lib.join/join-alias col)+ ;; For implicit joins, use the original column name since that's what exists+ ;; in the actual database table (#67002).+ implicit-join? (when join-alias+ (when-let [join (m/find-first #(= (:alias %) join-alias)+ (:joins (get-in query path)))]+ (:qp/is-implicit-join join)))+ source-col-alias (if implicit-join?+ ((some-fn :lib/original-name :name) col)+ (:lib/source-column-alias col))]+ (lib/update-options+ field-ref #(-> %+ (assoc ::source-table (source-table query path col)+ ::source-alias (escaped-source-alias query path join-alias source-col-alias))+ (m/assoc-some ::nfc-path (not-empty (:nfc-path col)))))))(defn- fix-field-ref-if-it-should-actually-be-an-expression-ref"I feel evil about doing this, since generally this namespace otherwise just ADDs info and does not in any other waydiff --git a/test/metabase/query_processor/implicit_joins_test.clj b/test/metabase/query_processor/implicit_joins_test.cljindex 5acee0486f1..e2a4b37fa47 100644--- a/test/metabase/query_processor/implicit_joins_test.clj+++ b/test/metabase/query_processor/implicit_joins_test.clj@@ -1,15 +1,20 @@(ns ^:mb/driver-tests metabase.query-processor.implicit-joins-test"Tests for joins that are created automatically when an `:fk->` column is present."(:require+ [clojure.string :as str][clojure.test :refer :all][java-time.api :as t]+ [medley.core :as m][metabase.driver :as driver][metabase.lib.core :as lib]+ [metabase.lib.metadata :as lib.metadata][metabase.lib.test-util :as lib.tu][metabase.query-processor :as qp]^{:clj-kondo/ignore [:deprecated-namespace]} [metabase.query-processor.store :as qp.store][metabase.test :as mt]- [metabase.util.date-2 :as u.date]))+ [metabase.util :as u]+ [metabase.util.date-2 :as u.date]+ [toucan2.core :as t2]))(set! *warn-on-reflection* true)@@ -225,3 +230,59 @@{:source-field (mt/id :orders :product_id), :base-type :type/Integer}]]:aggregation [[:count]]:limit 1})))))))))))++(mt/defdataset long-col-name-dataset+ [["long_col_name" [{:field-name "fk"+ :base-type :type/Integer}+ {:field-name "abcdefg_abcdefg_abcdefg_abcdefg_abcdefg_abcdefg_abcdefg_abcdefg_"+ :base-type :type/Text}]+ [[1 "a1"]+ [2 "a2"]+ [2 "a3"]]]+ ["long_col_name_2" [{:field-name "foo"+ :base-type :type/Integer}+ {:field-name "abcdefg_abcdefg_abcdefg_abcdefg_abcdefg_abcdefg_abcdefg_abcdefg_"+ :base-type :type/Text}]+ [[1 "b1"]+ [2 "b2"]+ [3 "b3"]]]])++(deftest long-col-name-repro-test+ (mt/test-drivers (mt/normal-drivers-with-feature :left-join)+ (mt/dataset long-col-name-dataset+ (let [mp (mt/metadata-provider)+ table (lib.metadata/table mp (mt/id :long_col_name))+ query (lib/query mp table)+ fk-field (mt/id :long_col_name :fk)+ id-2-field (mt/id :long_col_name_2 :foo)]+ (t2/update! :model/Field fk-field {:semantic_type :type/FK+ :fk_target_field_id id-2-field})+ (testing "Implicit join with long column name should use actual DB column name as source alias (#67002)"+ (let [breakoutable-cols (lib/breakoutable-columns query)+ ;; Use case-insensitive matching because H2 stores column names in uppercase+ fk-col (m/find-first (fn [col]+ (and (:fk-field-id col)+ (str/starts-with? (u/lower-case-en (:name col)) "abcdefg")))+ breakoutable-cols)+ query (-> query+ (lib/breakout fk-col))]+ (is (= [["b1"] ["b2"]]+ (mt/rows (qp/process-query query))))))+ (testing "Explicit join with long column name should use desired alias as source alias"+ (let [table-2 (lib.metadata/table mp (mt/id :long_col_name_2))+ join-cols (lib/joinable-columns query -1 table-2)+ fk-col (lib.metadata/field mp (mt/id :long_col_name :fk))+ id-col (m/find-first #(= (u/lower-case-en (:name %)) "foo") join-cols)+ join-clause (-> (lib/join-clause table-2)+ (lib/with-join-conditions [(lib/= fk-col id-col)])+ (lib/with-join-fields :all))+ query (lib/join query join-clause)+ breakoutable-cols (lib/breakoutable-columns query)+ big-col (m/find-first (fn [col]+ (and (:metabase.lib.join/join-alias col)+ (str/starts-with? (u/lower-case-en (:name col)) "abcdefg")))+ breakoutable-cols)+ query (-> query+ (lib/breakout big-col))]+ (is (= [["b1"] ["b2"]]+ (mt/rows (qp/process-query query))))))))))