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
Track whether column is `:lib/from-model?` (#70128)
Cam Saul5e35a33837d9
diff --git a/src/metabase/lib/card.cljc b/src/metabase/lib/card.cljcindex 4597cbc4e68..31cee932f05 100644--- a/src/metabase/lib/card.cljc+++ b/src/metabase/lib/card.cljc@@ -218,8 +218,9 @@(cond-> [:description :display-name :semantic-type :fk-target-field-id :settings :visibility-type :lib/source-display-name]native-model? (conj :id)))-;;; TODO (Cam 6/13/25) -- duplicated/overlapping responsibility with [[metabase.lib.field/previous-stage-metadata]] as-;;; well as [[metabase.lib.metadata.result-metadata/merge-model-metadata]] -- find a way to deduplicate these+;;; TODO (Cam 6/13/25) -- duplicated/overlapping responsibility+;;; with [[metabase.lib.field.resolution/previous-stage-metadata]] as well+;;; as [[metabase.lib.metadata.result-metadata/merge-model-metadata]] -- find a way to deduplicate these(mu/defn merge-model-metadata :- [:sequential ::lib.schema.metadata/column]"Merge metadata from source model metadata into result cols.@@ -241,7 +242,7 @@(empty? result-cols)(not-empty model-cols)- :else ;; both not empty+ :else ; both not empty(let [name->model-col (m/index-by :name model-cols)](mapv (fn [result-col](merge@@ -251,7 +252,8 @@(when (or own-model-query?(not= (:lib/source result-col) :source/aggregations))(when-let [model-col (get name->model-col (:name result-col))]- (let [model-col (u/select-non-nil-keys model-col (model-preserved-keys native-model?))]+ (let [model-col (-> (u/select-non-nil-keys model-col (model-preserved-keys native-model?))+ (assoc :lib/from-model? true))];; For the model's own query, preserve the user-customized display name as-is.;; For outer queries using the model as source, append temporal/binning suffixes;; because the outer query may apply its own bucketing on top of the model columns.diff --git a/src/metabase/lib/field.cljc b/src/metabase/lib/field.cljcindex a43dac82b37..8114d2638bc 100644--- a/src/metabase/lib/field.cljc+++ b/src/metabase/lib/field.cljc@@ -116,13 +116,22 @@simple-display-name :lib/simple-display-nameoriginal-display-name :lib/original-display-nameref-display-name :lib/ref-display-name+ from-model? :lib/from-model?source :lib/sourcesource-uuid :lib/source-uuid:as col}style](let [humanized-name (u.humanization/name->human-readable-name :simple field-name)field-display-name (or ref-display-name- original-display-name+ ;; TODO (Cam 2026-02-27) HACK for Models, `:original-display-name` gets incorrectly+ ;; overriden at some point with a recalculated display name for the column, so user+ ;; edits to `:display-name` itself get stomped on. Since we want to preserve the user+ ;; edits in `:display-name` we should ignore `:original-display-name` which is+ ;; incorrect here. FIXME -- we should make sure `:original-display-name` is set right+ ;; away for metadata coming from models and we can remove this hack.+ (when (or (not from-model?)+ (= source :source/joins))+ original-display-name)field-display-name)fk-field-id (or fk-field-id original-fk-field-id)](or simple-display-name@@ -167,6 +176,8 @@fk-field-id :fk-field-idoriginal-fk-field-id :lib/original-fk-field-idtable-id :table-id+ from-model? :lib/from-model?+ source :lib/source:as _col}styledisplay-name]@@ -174,9 +185,17 @@original-join-alias)fk-field-id (or fk-field-id original-fk-field-id)join-display-name (when (and (= style :long)- ;; don't prepend a join display name if `:display-name` already contains one! Legacy- ;; result metadata might include it for joined Fields, don't want to add it twice.- ;; Otherwise we'll end up with display names like+ ;; don't append join aliases to columns that were introduced (directly in the+ ;; main previous stage/source card lineage) by models, since their+ ;; `:display-name` should already include the join alias and if it doesn't it's+ ;; because the user edited the display name to something else. Still ok to+ ;; prepend join aliases when we JOIN a model, since that should be added on top+ ;; of whatever is already there.+ (or (not from-model?)+ (= source :source/joins))+ ;; don't prepend a join display name if `:display-name` already contains one!+ ;; Legacy result metadata might include it for joined Fields, don't want to add+ ;; it twice. Otherwise we'll end up with display names like;;;; Products → Products → Category(not (str/includes? display-name " → ")))diff --git a/src/metabase/lib/field/resolution.cljc b/src/metabase/lib/field/resolution.cljcindex d01677df6c1..9cbec0bf9b9 100644--- a/src/metabase/lib/field/resolution.cljc+++ b/src/metabase/lib/field/resolution.cljc@@ -226,33 +226,32 @@[k v])))opts-metadata-fns))-(def ^:private model-propagated-keys- #{:lib/card-id- :lib/original-display-name- :lib/original-expression-name- :lib/original-fk-field-id- :lib/original-fk-field-name- :lib/original-fk-join-alias- :lib/original-join-alias- :lib/original-name- :lib/type- :active- :base-type- :converted-timezone- :description- :display-name- :fingerprint- :id- :semantic-type- :table-id- :visibility-type})-(def ^:private regular-card-propagated-keys#{:lib/card-id+ :lib/from-model?:active:fingerprint:visibility-type})+(def ^:private model-propagated-keys+ (set/union+ regular-card-propagated-keys+ #{:lib/original-display-name+ :lib/original-expression-name+ :lib/original-fk-field-id+ :lib/original-fk-field-name+ :lib/original-fk-join-alias+ :lib/original-join-alias+ :lib/original-name+ :lib/type+ :base-type+ :converted-timezone+ :description+ :display-name+ :id+ :semantic-type+ :table-id}))+(declare resolve-in-previous-stage-returned-columns-and-update-keys)(mu/defn- additional-metadata-from-source-card :- [:maybe :map]@@ -273,8 +272,11 @@;; unique/unambiguous if multiple versions of the column (e.g. with different bucketing units) are;; returned(when-some [col (resolve-in-previous-stage-returned-columns-and-update-keys query card-cols (:lib/source-column-alias col))]- (let [col (assoc col :lib/source :source/card, :lib/card-id card-id)- propagated-keys (if (= (:type card) :model)+ (let [col (assoc col :lib/source :source/card, :lib/card-id card-id)+ model? (= (:type card) :model)+ col (cond-> col+ model? (assoc :lib/from-model? true))+ propagated-keys (if model?model-propagated-keysregular-card-propagated-keys)](select-keys col propagated-keys))))))diff --git a/src/metabase/lib/schema/metadata.cljc b/src/metabase/lib/schema/metadata.cljcindex 5765c9d8dfc..2b4bcab8894 100644--- a/src/metabase/lib/schema/metadata.cljc+++ b/src/metabase/lib/schema/metadata.cljc@@ -416,10 +416,18 @@;;;; this SHOULD NOT get propagated to subsequent stages![:lib/breakout? {:optional true} [:maybe :boolean]]-- ;; ID of the Card this came from, if this came from Card results metadata. Mostly used for creating column groups.+ ;;+ ;; ID of the Card this came from, if this column originally came from a Card (Saved Question or Model). Mostly+ ;; used for creating column groups. AFAIK this should get propagated indefinitely -- Cam[:lib/card-id {:optional true} [:maybe ::lib.schema.id/card]];;+ ;; Whether this column originally was introduced by a Model. Model metadata has special rules, for example because+ ;; `:display-name` can be user-edited we should be careful not to recalculate it unnecessarily. See for+ ;; example [[metabase.query-processor.model-test/preserve-model-display-names-test]].+ ;;+ ;; This key should get propagated indefinitely.+ [:lib/from-model? {:optional true} [:maybe :boolean]]+ ;;;; this stuff is adapted from [[metabase.query-processor.util.add-alias-info]]. It is included in;; the [[metabase.lib.metadata.calculation/metadata]];;diff --git a/src/metabase/query_processor/middleware/fetch_source_query.clj b/src/metabase/query_processor/middleware/fetch_source_query.cljindex 3bbd82449cc..de49a5ff079 100644--- a/src/metabase/query_processor/middleware/fetch_source_query.clj+++ b/src/metabase/query_processor/middleware/fetch_source_query.clj@@ -59,7 +59,9 @@;; This is for detecting circular refs below, and is later used as part of;; permissions enforcement(assoc stage :qp/stage-is-from-source-card card-id))- card-metadata (into [] (remove :remapped-from)+ ;; TODO (Cam 2026-02-25) Check if attaching the metadata is even necessary anymore+ card-metadata (into []+ (remove :remapped-from)(lib.card/card-returned-columns metadata-providerable card))last-stage (cond-> (last stages)(seq card-metadata) (assoc :lib/stage-metadata {:lib/type :metadata/results, :columns card-metadata})diff --git a/test/metabase/query_processor/model_test.clj b/test/metabase/query_processor/model_test.cljindex 534ba2c3303..7a1885a521c 100644--- a/test/metabase/query_processor/model_test.clj+++ b/test/metabase/query_processor/model_test.clj@@ -5,10 +5,40 @@[metabase.lib.core :as lib][metabase.lib.metadata :as lib.metadata][metabase.lib.metadata.calculation :as lib.metadata.calculation]+ [metabase.lib.metadata.result-metadata :as lib.metadata.result-metadata][metabase.lib.test-util :as lib.tu]- [metabase.query-processor :as qp]+ [metabase.query-processor.preprocess :as qp.preprocess][metabase.test :as mt]))+(deftest ^:parallel join-in-model-display-name-test+ (let [mp (mt/metadata-provider)+ mp (lib.tu/mock-metadata-provider+ mp+ {:cards [{:id 1+ :dataset-query (-> (lib/query mp (lib.metadata/table mp (mt/id :products)))+ ;; I guess this join is named `Reviews`+ (lib/join (-> (lib/join-clause (lib.metadata/table mp (mt/id :reviews))+ [(lib/=+ (lib.metadata/field mp (mt/id :products :id))+ (lib.metadata/field mp (mt/id :reviews :product_id)))])+ (lib/with-join-fields :all))))+ :database-id (mt/id)+ :name "Products+Reviews"+ :type :model}]})+ question (binding [lib.metadata.calculation/*display-name-style* :long]+ (as-> (lib/query mp (lib.metadata/card mp 1)) $q+ (lib/breakout $q (-> (m/find-first (comp #{"Reviews → Created At"} :display-name)+ (lib/breakoutable-columns $q))+ (lib/with-temporal-bucket :month)))+ (lib/aggregate $q (lib/avg (->> $q+ lib/available-aggregation-operators+ (m/find-first (comp #{:avg} :short))+ :columns+ (m/find-first (comp #{"Rating"} :display-name)))))))]+ (is (= ["Reviews → Created At: Month"+ "Average of Rating"]+ (mapv :display_name (qp.preprocess/query->expected-cols question))))))+;;; see also [[metabase.lib.field-test/model-self-join-test-display-name-test]](deftest ^:parallel model-self-join-test(testing "Field references from model joined a second time can be resolved (#48639)"@@ -75,6 +105,43 @@#_"Products+Reviews Summary - Reviews → Created At: Month → Created At""Products+Reviews Summary - Reviews → Created At: Month → Created At: Month""Products+Reviews Summary - Reviews → Created At: Month → Sum of Price"]- (->> (qp/process-query question)- mt/cols- (mapv :display_name)))))))+ (mapv :display_name (qp.preprocess/query->expected-cols question)))))))++(deftest ^:parallel preserve-model-display-names-test+ (testing "Edited display names for columns in Models should get preserved (#65532)"+ ;; 1. Create a Model of ORDERS joining PRODUCTS+ ;;+ ;; 2. Edit display name for "Products → ID", change to "[RENAMED]"+ ;;+ ;; 3. Create a new Saved Question using the first Model as its source+ ;;+ ;; 4. Run the Saved Question; results metadata incorrectly returns "Products → [RENAMED]"+ (let [mp (as-> (mt/metadata-provider) $mp+ (lib.tu/mock-metadata-provider+ $mp+ {:cards [(let [query (-> (lib/query $mp (lib.metadata/table $mp (mt/id :orders)))+ (lib/join (lib.metadata/table $mp (mt/id :products))))]+ {:id 1+ :type :model+ :dataset-query query+ :result-metadata (for [col (lib.metadata.result-metadata/returned-columns query)]+ (cond-> col+ (= (:display-name col) "Products → ID")+ (assoc :display-name "[RENAMED]")))})]})+ (lib.tu/mock-metadata-provider+ $mp+ {:cards [(let [query (lib/query $mp (lib.metadata/card $mp 1))]+ {:id 2+ :dataset-query query+ :source-card-id 1})]}))+ query (lib/query mp (lib.metadata/card mp 2))+ returned-display-name (fn [query]+ (-> (lib.metadata.result-metadata/returned-columns query)+ (->> (m/find-first #(= (:lib/desired-column-alias %) "Products__ID")))+ :display-name))]+ (is (= "[RENAMED]"+ (returned-display-name query)))+ ;; the bug only seems to trigger if we preprocess the query first+ (testing "preprocessed query should have the same returned display name"+ (is (= "[RENAMED]"+ (returned-display-name (qp.preprocess/preprocess query))))))))