Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Ban legacy keys in column metadata (#69504)

Cam Saul7c05a09619bd
diff --git a/src/metabase/driver/sql/query_processor.clj b/src/metabase/driver/sql/query_processor.clj
index dfe4d34ea6e..0f3f00285f8 100644
--- a/src/metabase/driver/sql/query_processor.clj
+++ b/src/metabase/driver/sql/query_processor.clj
@@ -2009,9 +2009,9 @@
(declare apply-clauses)
(defn- apply-source-query
- "Handle a `:source-query` clause by adding a recursive `SELECT` or native query.
- If the source query has ambiguous column names, use a `WITH` statement to rename the source columns.
- At the time of this writing, all source queries are aliased as `source`."
+ "Handle a `:source-query` clause by adding a recursive `SELECT` or native query. If the source query has ambiguous
+ column names, use a `WITH` statement to rename the source columns. At the time of this writing, all source queries
+ are aliased as `source`."
[driver honeysql-form {{:keys [native params] persisted :persisted-info/native :as source-query} :source-query
source-metadata :source-metadata}]
(let [table-alias (->honeysql driver (h2x/identifier :table-alias source-query-alias))
@@ -2024,12 +2024,8 @@
:else
(apply-clauses driver {} source-query))
- ;; TODO: Use MLv2 here to get source and desired-aliases
- alias-info (mapv (fn [{[_ desired-ref-name] :field_ref source-name :name}]
- [source-name desired-ref-name])
- source-metadata)
- source-aliases (mapv first alias-info)
- desired-aliases (mapv second alias-info)
+ source-aliases (mapv :lib/source-column-alias source-metadata)
+ desired-aliases (mapv :lib/desired-column-alias source-metadata)
duplicate-source-aliases? (and (> (count source-aliases) 1)
(not (apply distinct? source-aliases)))
needs-columns? (and (seq desired-aliases)
diff --git a/src/metabase/lib/convert/metadata_to_legacy.cljc b/src/metabase/lib/convert/metadata_to_legacy.cljc
index fc13b592f5e..67686abe1e9 100644
--- a/src/metabase/lib/convert/metadata_to_legacy.cljc
+++ b/src/metabase/lib/convert/metadata_to_legacy.cljc
@@ -9,8 +9,11 @@
(let [f (u.memo/fast-memo
(fn [k]
- (cond-> k
- (simple-keyword? k) u/->snake_case_en)))]
+ (case k
+ :metabase.lib.metadata.result-metadata/field-ref :field_ref
+ :metabase.lib.metadata.result-metadata/source :source
+ #_else (cond-> k
+ (simple-keyword? k) u/->snake_case_en))))]
(defn lib-metadata-column-key->legacy-metadata-column-key
"Convert unnamespaced keys to snake case for traditional reasons; `:lib/` keys and the like can stay in kebab case
because you can't consume them in JS without using bracket notation anyway (and you probably shouldn't be doing it
diff --git a/src/metabase/lib/metadata/result_metadata.cljc b/src/metabase/lib/metadata/result_metadata.cljc
index 803bddfc48b..06ef2e79442 100644
--- a/src/metabase/lib/metadata/result_metadata.cljc
+++ b/src/metabase/lib/metadata/result_metadata.cljc
@@ -27,6 +27,7 @@
[metabase.lib.options :as lib.options]
[metabase.lib.ref :as lib.ref]
[metabase.lib.schema :as lib.schema]
+ [metabase.lib.schema.common :as lib.schema.common]
[metabase.lib.schema.metadata :as lib.schema.metadata]
[metabase.lib.util :as lib.util]
[metabase.lib.util.match :as lib.util.match]
@@ -41,9 +42,14 @@
;; came from) and then have the `annotate` middleware convert them to something else for QP results purposes. Then
;; we can 'ban' stuff like `:source-alias` and `:source` within Lib itself. See #59772 for some experimental work
;; there. (See QUE2-361)
- [:map
- [:source {:optional true} ::lib.schema.metadata/column.legacy-source]
- [:field-ref {:optional true} ::mbql.s/Reference]])
+ [:and
+ [:map
+ [::source {:optional true} ::lib.schema.metadata/column.legacy-source]
+ [::field-ref {:optional true} ::mbql.s/Reference]]
+ (lib.schema.common/disallowed-keys
+ {:source "Use ::source instead of :source"
+ :field-ref "Use ::field-ref instead of :field-ref"
+ :source-alias ":source-alias is deprecated as of #69314"})])
(mr/def ::kebab-cased-map
[:and
@@ -209,12 +215,12 @@
[:merge
::kebab-cased-map
[:map
- [:source ::lib.schema.metadata/column.legacy-source]]]]
+ [::source ::lib.schema.metadata/column.legacy-source]]]]
"Add `:source` to result columns. Needed for legacy FE code. See
https://metaboat.slack.com/archives/C0645JP1W81/p1749064861598669?thread_ts=1748958872.704799&cid=C0645JP1W81"
[cols :- [:sequential ::kebab-cased-map]]
(mapv (fn [col]
- (assoc col :source (legacy-source col)))
+ (assoc col ::source (legacy-source col)))
cols))
(defn- remove-namespaced-options
@@ -291,7 +297,7 @@
{:metabase.lib.join/join-alias previous-join-alias, :lib/source :source/joins})))
lib.ref/ref)]
(cond
- ;; if original ref in the query used an ID then `:field-ref` should as well for historic
+ ;; if original ref in the query used an ID then `::field-ref` should as well for historic
;; reasons.
(and (or (= (:lib/original-ref-style-for-result-metadata-purposes col) :original-ref-style/id)
;; for historic reasons implicit fields should also come back with ID refs...
@@ -320,16 +326,16 @@
;; settings, which use them as keys. Since ambiguous refs have never worked correctly it is ok to return
;; 'modern' refs instead.
(defn- deduplicate-field-refs [cols]
- (let [duplicate-refs (->> (frequencies (map :field-ref cols))
+ (let [duplicate-refs (->> (frequencies (map ::field-ref cols))
(m/filter-vals #(> % 1))
keys
set)]
(cond->> cols
(seq duplicate-refs) (mapv (fn [col]
(cond-> col
- (duplicate-refs (:field-ref col))
- (update :field-ref (fn [[tag _id-or-name opts]]
- [tag (:lib/deduplicated-name col) (assoc opts :base-type (:base-type col))]))))))))
+ (duplicate-refs (::field-ref col))
+ (update ::field-ref (fn [[tag _id-or-name opts]]
+ [tag (:lib/deduplicated-name col) (assoc opts :base-type (:base-type col))]))))))))
(mu/defn- add-legacy-field-refs :- [:sequential ::kebab-cased-map]
"Add legacy `:field_ref` to QP results metadata which is still used in a single place in the FE -- see
@@ -340,7 +346,7 @@
(let [cols (mapv (fn [col]
(let [field-ref (super-broken-legacy-field-ref query col)]
(cond-> col
- field-ref (assoc :field-ref field-ref))))
+ field-ref (assoc ::field-ref field-ref))))
cols)]
(deduplicate-field-refs cols))))
@@ -463,10 +469,10 @@
(apply distinct? (map :name cols))))]
;; QUE-1623
[:fn
- {:error/message "columns should have unique :field-ref(s)"}
+ {:error/message "columns should have unique ::field-ref(s)"}
(fn [cols]
(or (empty? cols)
- (apply distinct? (map :field-ref cols))))]]
+ (apply distinct? (map ::field-ref cols))))]]
"Return metadata for columns returned by a pMBQL `query`.
`initial-cols` are (optionally) the initial minimal metadata columns as returned by the driver (usually just column
diff --git a/src/metabase/lib/schema/metadata.cljc b/src/metabase/lib/schema/metadata.cljc
index 5da116c74f4..0255f9ae136 100644
--- a/src/metabase/lib/schema/metadata.cljc
+++ b/src/metabase/lib/schema/metadata.cljc
@@ -482,13 +482,15 @@
;;
;; Added by [[metabase.lib.metadata.result-metadata]] primarily for legacy/backward-compatibility purposes with
;; legacy viz settings. This should not be used for anything other than that.
- [:field-ref {:optional true} [:maybe #?(:cljs [:or
- [:ref :metabase.legacy-mbql.schema/Reference]
- [:fn {:error/message "JS array"}
- array?]]
- :clj [:ref :metabase.legacy-mbql.schema/Reference])]]
+ [:metabase.lib.metadata.result-metadata/field-ref
+ {:optional true}
+ [:maybe #?(:cljs [:or
+ [:ref :metabase.legacy-mbql.schema/Reference]
+ [:fn {:error/message "JS array"}
+ array?]]
+ :clj [:ref :metabase.legacy-mbql.schema/Reference])]]
;;
- [:source {:optional true} [:maybe [:ref ::column.legacy-source]]]
+ [:metabase.lib.metadata.result-metadata/source {:optional true} [:maybe [:ref ::column.legacy-source]]]
;;
;; these next two keys are derived by looking at `FieldValues` and `Dimension` instances associated with a `Field`;
;; they are used by the Query Processor to add column remappings to query results. To see how this maps to stuff in
@@ -511,9 +513,13 @@
[:ref ::lib.schema.common/kebab-cased-map]
[:ref ::column.validate-for-source]
(lib.schema.common/disallowed-keys
- {:source-alias ":source-alias is deprecated; use :metabase.lib.join/join-alias or :lib/original-join-alias instead"
+ {:binning ":binning is deprecated; use :metabase.lib.field/binning instead"
+ :field-ref ":field-ref is deprecated. For QP result metadata, use :metabase.lib.metadata.result-metadata/field-ref"
:ident ":ident is deprecated and should not be included in column metadata"
- :model/inner-ident ":model/inner_ident (normalized to :model/inner-ident) is deprecated and should not be included in column metadata"})])
+ :model/inner-ident ":model/inner_ident (normalized to :model/inner-ident) is deprecated and should not be included in column metadata"
+ :source ":source is deprecated; use :lib/source instead. For QP result metadata, use :metabase.lib.metadata.result-metadata/source"
+ :source-alias ":source-alias is deprecated; use :metabase.lib.join/join-alias or :lib/original-join-alias instead"
+ :unit ":unit is deprecated; use :metabase.lib.field/temporal-unit instead"})])
(mr/def ::persisted-info.definition
"Definition spec for a cached table."
@@ -616,6 +622,12 @@
(not (:collection-id card))
(assoc :collection-id nil)))
+(mr/def ::card.result-metadata
+ "Schema for the `:result-metadata` for a Card (Saved Question, Model, or v2 Metric)."
+ ;; TODO (Cam 2026-02-11) consider whether we should add additional constraints here like distinct `:name`s... we can
+ ;; fix these with normalization if needed
+ [:sequential [:ref ::lib-or-legacy-column]])
+
(mr/def ::card
"Schema for metadata about a specific Saved Question (which may or may not be a Model). More or less the same as
a [[metabase.queries.models.card]], but with kebab-case keys. Note that the `:dataset-query` is not necessarily
@@ -634,7 +646,7 @@
[:dataset-query {:optional true} ::card.query]
;; vector of column metadata maps; these are ALMOST the correct shape to be [[ColumnMetadata]], but they're
;; probably missing `:lib/type` and probably using `:snake_case` keys.
- [:result-metadata {:optional true} [:maybe [:sequential ::lib-or-legacy-column]]]
+ [:result-metadata {:optional true} [:maybe [:ref ::card.result-metadata]]]
;; what sort of saved query this is, e.g. a normal Saved Question or a Model or a V2 Metric.
[:type {:optional true} [:maybe [:ref ::card.type]]]
;; Table ID is nullable in the application database, because native queries are not necessarily associated with a
diff --git a/src/metabase/lib_be/metadata/jvm.clj b/src/metabase/lib_be/metadata/jvm.clj
index 97695e60d2a..fb816492fbe 100644
--- a/src/metabase/lib_be/metadata/jvm.clj
+++ b/src/metabase/lib_be/metadata/jvm.clj
@@ -44,7 +44,8 @@
(u.memo/fast-memo u/->kebab-case-en))
(def ^:private metadata-type->schema
- {:metadata/card ::lib.schema.metadata/card})
+ {:metadata/card ::lib.schema.metadata/card
+ :metadata/column ::lib.schema.metadata/column})
(mu/defn instance->metadata
"Convert a (presumably) Toucan 2 instance of an application database model with `snake_case` keys to a MLv2 style
diff --git a/src/metabase/query_processor/middleware/add_implicit_joins.clj b/src/metabase/query_processor/middleware/add_implicit_joins.clj
index fecf34642e3..b903e08f41e 100644
--- a/src/metabase/query_processor/middleware/add_implicit_joins.clj
+++ b/src/metabase/query_processor/middleware/add_implicit_joins.clj
@@ -174,34 +174,6 @@
join-alias])))
(visible-joins query path stage)))
-;;; TODO (Cam 7/17/25) -- it seems weird to be updating quite possibly the least important part of stage metadata --
-;;; legacy `:field-ref`, which is generally only provided by the QP as a courtesy for use for legacy purposes as a key
-;;; in viz settings and nothing else. Why aren't we adding `:metabase.lib.join/join-alias` keys or anything like that?
-;;; Why aren't we adding metadata for the fields we spliced in here? It all seems kinda fishy. It might be possible to
-;;; take this out completely without breaking anything.
-(mu/defn- add-implicit-joins-aliases-to-metadata :- ::lib.schema/stage
- "Add `:join-alias`es to legacy field refs for fields containing `:source-field` in `:lib/stage-metadata` of `query`.
- It is required, that `:source-query` has already it's joins resolved. It is valid, when no `:join-alias` could be
- found. For examaple during remaps, metadata contain fields with `:source-field`, that are not used further in their
- `:source-query`."
- [query :- ::lib.schema/query
- path :- ::lib.walk/path
- stage :- ::lib.schema/stage]
- (let [fk-field-info->join-alias (construct-fk-field-info->join-alias query path stage)]
- (letfn [(update-legacy-field-ref [field-ref]
- ;; field ref should be a LEGACY field ref.
- (lib.util.match/replace field-ref
- [:field id-or-name (opts :guard (every-pred :source-field (complement :join-alias)))]
- (let [join-alias (fk-field-info->join-alias (field-opts->fk-field-info query opts))]
- (if (some? join-alias)
- [:field id-or-name (assoc opts :join-alias join-alias)]
- &match))))
- (update-col [col]
- (m/update-existing col :field-ref update-legacy-field-ref))
- (update-cols [cols]
- (mapv update-col cols))]
- (update-in stage [:lib/stage-metadata :columns] update-cols))))
-
(mu/defn- add-join-alias-to-fields-with-source-field :- ::lib.schema/stage
"Add `:field` `:join-alias` to `:field` clauses with `:source-field` in `form`. Ignore `:lib/stage-metadata`."
[query :- ::lib.schema/query
@@ -379,7 +351,7 @@
(mapv (fn [join]
(dissoc join ::original-position))))))
-(mu/defn- resolve-implicit-joins-this-level :- ::lib.schema/stage
+(mu/defn- resolve-implicit-joins :- ::lib.schema/stage
"Add new `:joins` for tables referenced by `:field` forms with a `:source-field`. Add `:join-alias` info to those
`:fields`. Add additional `:fields` to source query if needed to perform the join."
[query :- ::lib.schema/query
@@ -398,16 +370,8 @@
(cond-> (seq required-joins) (update :joins topologically-sort-joins))
(assoc ::reused-join-aliases reused-join-aliases))))
-(mu/defn- resolve-implicit-joins :- ::lib.schema/stage
- [query :- ::lib.schema/query
- path :- ::lib.walk/path
- stage :- ::lib.schema/stage]
- (-> stage
- (->> (resolve-implicit-joins-this-level query path))
- (cond->> (:lib/stage-metadata stage) (add-implicit-joins-aliases-to-metadata query path))))
-
(mu/defn- first-pass :- [:maybe ::lib.schema/stage]
- "The first pass adds all of the new joins ([[resolve-implicit-joins-this-level]]) and updates
+ "The first pass adds all of the new joins ([[resolve-implicit-joins]]) and updates
metadata ([[add-implicit-joins-aliases-to-metadata]])."
[query :- ::lib.schema/query
path :- ::lib.walk/path
diff --git a/src/metabase/query_processor/middleware/add_remaps.clj b/src/metabase/query_processor/middleware/add_remaps.clj
index ddbe7b1a19d..406851e8e18 100644
--- a/src/metabase/query_processor/middleware/add_remaps.clj
+++ b/src/metabase/query_processor/middleware/add_remaps.clj
@@ -622,10 +622,10 @@
(if disable-remaps?
rff
(fn remap-results-rff* [metadata]
- (let [mlv2-cols (map
+ (let [lib-cols (map
#(lib-be/instance->metadata % :metadata/column)
(:cols metadata))
- internal-cols-info (internal-columns-info mlv2-cols)
+ internal-cols-info (internal-columns-info lib-cols)
metadata (add-remapped-to-and-from-metadata metadata external-remaps internal-cols-info)]
(remap-results-xform internal-cols-info (rff metadata))))))
diff --git a/test/metabase/lib/convert_test.cljc b/test/metabase/lib/convert_test.cljc
index 151f72baee4..bc7cc93f10e 100644
--- a/test/metabase/lib/convert_test.cljc
+++ b/test/metabase/lib/convert_test.cljc
@@ -672,7 +672,6 @@
:source-metadata [{:semantic_type :type/Category
:table_id 45
:name "CATEGORY"
- :field_ref [:field 350 {:base-type :type/Text}]
:effective_type :type/Text
:id 350
:display_name "Category"
@@ -721,7 +720,6 @@
:source-metadata [{:semantic_type :type/Category
:table_id 45
:name "CATEGORY"
- :field_ref [:field 350 {:base-type :type/Text, :join-alias "Products"}]
:effective_type :type/Text
:id 350
:display_name "Products → Category"
@@ -735,13 +733,10 @@
{:name "count"
:display_name "Count"
:base_type :type/Integer
- :semantic_type :type/Quantity
- :field_ref [:aggregation 0]}]}
+ :semantic_type :type/Quantity}]}
:source-metadata [{:semantic_type :type/Category
:table_id 45
:name "CATEGORY"
- :field_ref [:field 350 {:base-type :type/Text
- :join-alias "Card 2 - Category"}]
:effective_type :type/Text
:id 350
:display_name "Products → Category"
@@ -755,8 +750,7 @@
{:name "count"
:display_name "Count"
:base_type :type/Integer
- :semantic_type :type/Quantity
- :field_ref [:field "count" {:base-type :type/Integer}]}]}}]]
+ :semantic_type :type/Quantity}]}}]]
(test-round-trip query))))
(deftest ^:parallel round-trip-options-test
@@ -822,8 +816,6 @@
[{:semantic_type :type/PK
:table_id 32598
:name "id"
- :source :fields
- :field_ref [:field 134528 nil]
:effective_type :type/Integer
:id 134528
:visibility_type :normal
diff --git a/test/metabase/lib/metadata/result_metadata_test.cljc b/test/metabase/lib/metadata/result_metadata_test.cljc
index f148ffb5de2..72967b320f9 100644
--- a/test/metabase/lib/metadata/result_metadata_test.cljc
+++ b/test/metabase/lib/metadata/result_metadata_test.cljc
@@ -25,8 +25,8 @@
(deftest ^:parallel col-info-field-ids-test
(testing "make sure columns are comming back the way we'd expect for :field clauses"
(lib.tu.macros/$ids venues
- (is (=? [{:source :fields
- :field-ref $price}]
+ (is (=? [{::result-metadata/source :fields
+ ::result-metadata/field-ref $price}]
(column-info
(lib/query meta/metadata-provider (lib.tu.macros/mbql-query venues {:fields [$price]}))
{:columns [:price], :cols [{}]}))))))
@@ -35,14 +35,14 @@
(lib.tu.macros/$ids venues
(testing (str "when a `:field` with `:source-field` (implicit join) is used, we should add in `:fk-field-id` "
"info about the source Field")
- (is (=? [{:fk-field-id %category-id
- :source :fields
- :field-ref $category-id->categories.name
+ (is (=? [{:fk-field-id %category-id
+ ::result-metadata/source :fields
+ ::result-metadata/field-ref $category-id->categories.name
;; for whatever reason this is what the `annotate` middleware traditionally returns here, for
;; some reason we use the `:long` style inside aggregations and the `:default` style elsewhere
;; who knows why. See notes
;; on [[metabase.query-processor.middleware.result-metadata/col-info-for-aggregation-clause]]
- :display-name "Category → Name"}]
+ :display-name "Category → Name"}]
(column-info
(lib/query meta/metadata-provider {:type :query, :query {:source-table $$venues, :fields [$category-id->categories.name]}})
{:columns [:name]}))))))
@@ -51,9 +51,9 @@
(lib.tu.macros/$ids venues
(testing (str "when a `:field` with `:source-field` (implicit join) is used, we should add in `:fk-field-id` "
"info about the source Field")
- (is (=? [{:source :aggregation
- :field-ref [:aggregation 0]
- :display-name "Distinct values of Category → Name"}]
+ (is (=? [{::result-metadata/source :aggregation
+ ::result-metadata/field-ref [:aggregation 0]
+ :display-name "Distinct values of Category → Name"}]
(column-info
(lib/query
meta/metadata-provider
@@ -66,51 +66,51 @@
(lib.tu.macros/$ids venues
(testing (str "we should get `:fk-field-id` and information where possible when using joins; "
"display-name should include the display name of the FK field (for IMPLICIT JOINS)")
- (is (=? [{:display-name "Category → Name"
- :source :fields
+ (is (=? [{:display-name "Category → Name"
+ ::result-metadata/source :fields
;; sort of contrived since this is not a query we could build create IRL... even tho the join is
;; technically explicit it matches the shape of an implicit one, so we should return field refs that
;; act like the join isn't here yet
- :field-ref [:field (meta/id :categories :name) {:source-field (meta/id :venues :category-id)}]}]
+ ::result-metadata/field-ref [:field (meta/id :categories :name) {:source-field (meta/id :venues :category-id)}]}]
(column-info
(lib/query
meta/metadata-provider
{:type :query
:query {:source-table (meta/id :venues)
- :fields [&Category.categories.name]
+ :fields [&Category.categories.name]
;; This is a hand-rolled implicit join clause.
- :joins [{:alias "Category"
- :source-table $$categories
- :condition [:= $category-id &CATEGORIES__via__CATEGORY_ID.categories.id]
- :strategy :left-join
- :fk-field-id %category-id}]}})
+ :joins [{:alias "Category"
+ :source-table $$categories
+ :condition [:= $category-id &CATEGORIES__via__CATEGORY_ID.categories.id]
+ :strategy :left-join
+ :fk-field-id %category-id}]}})
{:columns [:name]}))))))
(deftest ^:parallel col-info-for-explicit-joins-without-fk-field-id-test
(lib.tu.macros/$ids venues
(testing (str "for EXPLICIT JOINS (which do not include an `:fk-field-id` in the Join info) the returned "
- "`:field-ref` should be have only `:join-alias`, and no `:source-field`")
- (is (=? [{:display-name "Categories → Name"
- :source :fields
- :field-ref &Categories.categories.name}]
+ "`::result-metadata/field-ref` should be have only `:join-alias`, and no `:source-field`")
+ (is (=? [{:display-name "Categories → Name"
+ ::result-metadata/source :fields
+ ::result-metadata/field-ref &Categories.categories.name}]
(column-info
(lib/query
meta/metadata-provider
{:type :query
:query {:source-table (meta/id :venues)
- :fields [&Categories.categories.name]
- :joins [{:alias "Categories"
- :source-table $$categories
- :condition [:= $category-id &Categories.categories.id]
- :strategy :left-join}]}})
+ :fields [&Categories.categories.name]
+ :joins [{:alias "Categories"
+ :source-table $$categories
+ :condition [:= $category-id &Categories.categories.id]
+ :strategy :left-join}]}})
{:columns [:name]}))))))
(deftest ^:parallel col-info-for-field-with-temporal-unit-test
(lib.tu.macros/$ids venues
(testing "when a `:field` with `:temporal-unit` is used, we should add in info about the `:unit`"
- (is (=? [{:unit :month
- :source :fields
- :field-ref !month.price}]
+ (is (=? [{:unit :month
+ ::result-metadata/source :fields
+ ::result-metadata/field-ref !month.price}]
(column-info
(lib/query
meta/metadata-provider
@@ -121,12 +121,12 @@
(deftest ^:parallel col-info-for-field-literal-with-temporal-unit-test
(lib.tu.macros/$ids venues
(testing "datetime unit should work on field literals too"
- (is (=? [{:name "price"
- :base-type :type/Number
- :display-name "Price: Month"
- :unit :month
- :source :fields
- :field-ref !month.*price/Number}]
+ (is (=? [{:name "price"
+ :base-type :type/Number
+ :display-name "Price: Month"
+ :unit :month
+ ::result-metadata/source :fields
+ ::result-metadata/field-ref !month.*price/Number}]
(column-info
(lib/query meta/metadata-provider
{:type :query, :query {:source-table (meta/id :venues)
@@ -135,19 +135,19 @@
(deftest ^:parallel col-info-for-binning-strategy-test
(testing "when binning strategy is used, include `:binning-info`"
- (is (=? [{:name "price"
- :base-type :type/Number
- :display-name "Price: 10 bins: Month"
- :unit :month
- :source :fields
- :binning-info {:num-bins 10, :bin-width 5, :min-value -100, :max-value 100, :binning-strategy :num-bins}
- :field-ref [:field "price" {:base-type :type/Number
- :temporal-unit :month
- :binning {:strategy :num-bins
- :num-bins 10
- :bin-width 5
- :min-value -100
- :max-value 100}}]}]
+ (is (=? [{:name "price"
+ :base-type :type/Number
+ :display-name "Price: 10 bins: Month"
+ :unit :month
+ ::result-metadata/source :fields
+ :binning-info {:num-bins 10, :bin-width 5, :min-value -100, :max-value 100, :binning-strategy :num-bins}
+ ::result-metadata/field-ref [:field "price" {:base-type :type/Number
+ :temporal-unit :month
+ :binning {:strategy :num-bins
+ :num-bins 10
+ :bin-width 5
+ :min-value -100
+ :max-value 100}}]}]
(column-info
(lib/query
meta/metadata-provider
@@ -190,7 +190,7 @@
;; type: the db type is different and we have a way to convert. Othertimes, it doesn't make sense:
;; when the info is inferred. the solution to this might be quite extensive renaming
:name "grandparent.parent"
- :field-ref [:field 2 nil]
+ ::result-metadata/field-ref [:field 2 nil]
:parent-id 1
:visibility-type :normal
:display-name "Grandparent: Parent"
@@ -204,7 +204,7 @@
(lib/with-fields [(lib.metadata/field metadata-provider 3)]))]
(is (=? {:table-id (meta/id :venues)
:name "grandparent.parent.child"
- :field-ref [:field 3 nil]
+ ::result-metadata/field-ref [:field 3 nil]
:parent-id 2
:id 3
:visibility-type :normal
@@ -234,7 +234,7 @@
(is (=? {:name "sum"
:display-name "sum of User ID"
:base-type :type/Integer
- :field-ref [:field "sum" {:base-type :type/Integer}]
+ ::result-metadata/field-ref [:field "sum" {:base-type :type/Integer}]
:semantic-type :type/FK}
(-> (column-info query {:cols [{}]})
first))))))
@@ -251,7 +251,7 @@
(is (=? {:base-type :type/Integer
:name "double-price"
:display-name "double-price"
- :field-ref [:expression "double-price"]}
+ ::result-metadata/field-ref [:expression "double-price"]}
(expression-metadata :venues "double-price" (lib/* (meta/field-metadata :venues :price) 2))))))
(deftest ^:parallel col-info-expressions-test-1b
@@ -287,7 +287,7 @@
:base-type :type/DateTime
:name "last-login-converted"
:display-name "last-login-converted"
- :field-ref [:expression "last-login-converted"]}
+ ::result-metadata/field-ref [:expression "last-login-converted"]}
(expression-metadata :users "last-login-converted" (lib/convert-timezone
(meta/field-metadata :users :last-login)
"Asia/Ho_Chi_Minh"
@@ -299,7 +299,7 @@
:base-type :type/DateTime
:name "last-login-converted"
:display-name "last-login-converted"
- :field-ref [:expression "last-login-converted"]}
+ ::result-metadata/field-ref [:expression "last-login-converted"]}
(expression-metadata :users "last-login-converted" (lib/datetime-add
(lib/convert-timezone
(meta/field-metadata :users :last-login)
@@ -491,14 +491,14 @@
:type :metric
:dataset-query (lib.tu.macros/mbql-query orders
{:aggregation [[:sum $subtotal]]})}]})
- query (lib/query
- metadata-provider
- (lib.tu.macros/mbql-query venues {:aggregation [[:metric 1]]}))]
- (is (=? [{:display-name "Total Events"
- :base-type :type/Float
- :effective-type :type/Float
- :source :aggregation
- :field-ref [:aggregation 0]}]
+ query (lib/query
+ metadata-provider
+ (lib.tu.macros/mbql-query venues {:aggregation [[:metric 1]]}))]
+ (is (=? [{:display-name "Total Events"
+ :base-type :type/Float
+ :effective-type :type/Float
+ ::result-metadata/source :aggregation
+ ::result-metadata/field-ref [:aggregation 0]}]
(column-info
query
{:cols [{:display-name "Total Events", :base-type :type/Float}]}))))))
@@ -538,12 +538,12 @@
(testing "Coalesce"
(testing "Uses the first clause"
(testing "Gets the type information from the field"
- (is (=? {:name "expr"
- :source :fields
- :field-ref [:expression "expr"]
- :effective-type :type/Text
- :display-name "expr"
- :base-type :type/Text}
+ (is (=? {:name "expr"
+ ::result-metadata/source :fields
+ ::result-metadata/field-ref [:expression "expr"]
+ :effective-type :type/Text
+ :display-name "expr"
+ :base-type :type/Text}
(infer [:coalesce [:field (meta/id :venues :name) nil] "bar"])))
(testing "Does not contain a field id in its analysis (#18513)"
(is (false? (contains? (infer [:coalesce [:field (meta/id :venues :name) nil] "bar"])
@@ -553,23 +553,23 @@
(testing "Coalesce"
(testing "Uses the first clause"
(testing "Gets the type information from the literal"
- (is (=? {:base-type :type/Text
- :name "expr"
- :display-name "expr"
- :field-ref [:expression "expr"]
- :source :fields}
+ (is (=? {:base-type :type/Text
+ :name "expr"
+ :display-name "expr"
+ ::result-metadata/field-ref [:expression "expr"]
+ ::result-metadata/source :fields}
(infer [:coalesce "bar" [:field (meta/id :venues :name) nil]])))))))
(deftest ^:parallel computed-columns-inference-2
(testing "Case"
(testing "Uses first available type information"
(testing "From a field"
- (is (=? {:name "expr"
- :source :fields
- :field-ref [:expression "expr"]
- :effective-type :type/Text
- :display-name "expr"
- :base-type :type/Text}
+ (is (=? {:name "expr"
+ ::result-metadata/source :fields
+ ::result-metadata/field-ref [:expression "expr"]
+ :effective-type :type/Text
+ :display-name "expr"
+ :base-type :type/Text}
(infer [:coalesce [:field (meta/id :venues :name) nil] "bar"])))
(testing "does not contain a field id in its analysis (#17512)"
(is (false?
@@ -639,33 +639,33 @@
[:sum $price]
[:count]
[:aggregation-options [:count] {:display-name "count_2"}]]}))]
- (is (=? [{:base-type :type/Number
- :effective-type :type/Number
- :semantic-type :type/Quantity
- :name "count"
- :display-name "count"
- :source :aggregation
- :field-ref [:aggregation 0]}
- {:source :aggregation
- :name "sum"
- :display-name "sum"
- :base-type :type/Number
- :effective-type :type/Number
- :field-ref [:aggregation 1]}
- {:base-type :type/Number
- :effective-type :type/Number
- :semantic-type :type/Quantity
- :name "count_2"
- :display-name "count"
- :source :aggregation
- :field-ref [:aggregation 2]}
- {:base-type :type/Number
- :effective-type :type/Number
- :semantic-type :type/Quantity
- :name "count_3"
- :display-name "count_2"
- :source :aggregation
- :field-ref [:aggregation 3]}]
+ (is (=? [{:base-type :type/Number
+ :effective-type :type/Number
+ :semantic-type :type/Quantity
+ :name "count"
+ :display-name "count"
+ ::result-metadata/source :aggregation
+ ::result-metadata/field-ref [:aggregation 0]}
+ {::result-metadata/source :aggregation
+ :name "sum"
+ :display-name "sum"
+ :base-type :type/Number
+ :effective-type :type/Number
+ ::result-metadata/field-ref [:aggregation 1]}
+ {:base-type :type/Number
+ :effective-type :type/Number
+ :semantic-type :type/Quantity
+ :name "count_2"
+ :display-name "count"
+ ::result-metadata/source :aggregation
+ ::result-metadata/field-ref [:aggregation 2]}
+ {:base-type :type/Number
+ :effective-type :type/Number
+ :semantic-type :type/Quantity
+ :name "count_3"
+ :display-name "count_2"
+ ::result-metadata/source :aggregation
+ ::result-metadata/field-ref [:aggregation 3]}]
(column-info
query
{:cols [{:name "count", :display-name "count", :base-type :type/Number}
@@ -675,11 +675,11 @@
(deftest ^:parallel expressions-keys-test
(testing "make sure expressions come back with the right set of keys (#8854)"
- (is (=? {:name "discount_price"
- :display-name "discount_price"
- :base-type :type/Float
- :source :fields
- :field-ref [:expression "discount_price"]}
+ (is (=? {:name "discount_price"
+ :display-name "discount_price"
+ :base-type :type/Float
+ ::result-metadata/source :fields
+ ::result-metadata/field-ref [:expression "discount_price"]}
(-> (column-info
(lib/query
meta/metadata-provider
@@ -699,16 +699,16 @@
{:aggregation [[:* 0.9 [:avg $price]]
[:* 0.8 [:avg $price]]]
:limit 10}))]
- (is (=? [{:base-type :type/Float
- :name "expression"
- :display-name "0.9 × Average of Price"
- :source :aggregation
- :field-ref [:aggregation 0]}
- {:base-type :type/Float
- :name "expression_2"
- :display-name "0.8 × Average of Price"
- :source :aggregation
- :field-ref [:aggregation 1]}]
+ (is (=? [{:base-type :type/Float
+ :name "expression"
+ :display-name "0.9 × Average of Price"
+ ::result-metadata/source :aggregation
+ ::result-metadata/field-ref [:aggregation 0]}
+ {:base-type :type/Float
+ :name "expression_2"
+ :display-name "0.8 × Average of Price"
+ ::result-metadata/source :aggregation
+ ::result-metadata/field-ref [:aggregation 1]}]
(column-info query {:cols [{} {}]})))))))
(deftest ^:parallel deduplicate-expression-names-test-2
@@ -719,26 +719,26 @@
(lib.tu.macros/mbql-query users
{:expressions {:prev_month [:+ $last-login [:interval -1 :month]]}
:fields [[:expression "prev_month"]], :limit 10}))]
- (is (=? [{:name "prev_month"
- :display-name "prev_month"
- :base-type :type/Temporal
- :source :fields
- :field-ref [:expression "prev_month"]}]
+ (is (=? [{:name "prev_month"
+ :display-name "prev_month"
+ :base-type :type/Temporal
+ ::result-metadata/source :fields
+ ::result-metadata/field-ref [:expression "prev_month"]}]
(column-info query {:cols [{}]})))))))
;;; adapted from [[metabase.query-processor.nested-queries-test/breakout-year-test]]
(deftest ^:parallel breakout-year-test
(testing (str "make sure when doing a nested query we give you metadata that would suggest you should be able to "
"break out a *YEAR*")
- (let [source-query (lib.tu.macros/mbql-query checkins
- {:aggregation [[:count]]
- :breakout [!year.date]})
- metadata-provider (lib.tu/metadata-provider-with-cards-for-queries
- meta/metadata-provider
- [source-query])
+ (let [source-query (lib.tu.macros/mbql-query checkins
+ {:aggregation [[:count]]
+ :breakout [!year.date]})
+ metadata-provider (lib.tu/metadata-provider-with-cards-for-queries
+ meta/metadata-provider
+ [source-query])
[date-col count-col] (for [col (result-metadata/returned-columns (lib/query meta/metadata-provider source-query))]
(as-> col col
- (assoc col :source :fields)
+ (assoc col ::result-metadata/source :fields)
(dissoc col :position)
(m/filter-keys simple-keyword? col)))]
;; since the bucketing is happening in the source query rather than at this level, the field ref should
@@ -746,8 +746,8 @@
;; the `:year` bucketing if you used this query in another subsequent query, so the field ref doesn't
;; include the unit; however `:unit` is still `:year` so the frontend can use the correct formatting to
;; display values of the column.
- (is (=? [(assoc date-col :field-ref [:field (meta/id :checkins :date) nil], :unit :year)
- (assoc count-col :field-ref [:field "count" {:base-type :type/Integer}])]
+ (is (=? [(assoc date-col ::result-metadata/field-ref [:field (meta/id :checkins :date) nil], :unit :year)
+ (assoc count-col ::result-metadata/field-ref [:field "count" {:base-type :type/Integer}])]
(result-metadata/returned-columns
(lib/query metadata-provider (lib.metadata/card metadata-provider 1))))))))
@@ -848,7 +848,7 @@
;; correct.
(is (= [[:field "TITLE" {:base-type :type/Text}]
[:field "sum" {:base-type :type/Float}]]
- (map :field-ref (result-metadata/returned-columns query)))))))
+ (map ::result-metadata/field-ref (result-metadata/returned-columns query)))))))
(deftest ^:parallel mbql-query-type-inference-test
(testing "should add decent base/effective types if driver comes back with `:base-type :type/*`"
@@ -856,20 +856,20 @@
{:name "a", :base-type :type/*}
{:name "a", :base-type :type/*, :effective-type :type/*}
{:name "a", :base-type :type/Integer}]
- :let [expected-base-type (if (= (:base-type initial-metadata) :type/Integer)
+ :let [expected-base-type (if (= (:base-type initial-metadata) :type/Integer)
;; if the initial driver type comes back as something other than `:type/*`, we
;; should use that. Otherwise if it comes back as `:type/*` use the type
;; calculated by Lib.
- :type/Integer
- :type/BigInteger)]]
+ :type/Integer
+ :type/BigInteger)]]
;; should work with and without rows
(testing (lib.util/format "\ninitial-metadata = %s" (pr-str initial-metadata))
- (is (=? [{:name "ID"
- :display-name "ID"
- :base-type expected-base-type
- :effective-type expected-base-type
- :source :fields
- :field-ref [:field (meta/id :venues :id) nil]}]
+ (is (=? [{:name "ID"
+ :display-name "ID"
+ :base-type expected-base-type
+ :effective-type expected-base-type
+ ::result-metadata/source :fields
+ ::result-metadata/field-ref [:field (meta/id :venues :id) nil]}]
(column-info
(lib/query meta/metadata-provider (lib.tu.macros/mbql-query venues
{:fields [$id]}))
@@ -878,21 +878,21 @@
(deftest ^:parallel native-column-info-test
(testing "native column info"
(testing "should disambiguate duplicate names"
- (doseq [rows [[]
- [[1 nil]]]
+ (doseq [rows [[]
+ [[1 nil]]]
query [{:type :native, :native {:query "SELECT *"}}
{:type :query, :query {:source-query {:native "SELECT *"}}}]]
(testing (lib.util/format "\nrows = %s,query = %s" (pr-str rows) (pr-str query))
- (is (=? [{:name "a"
- :display-name "a"
- :base-type :type/Integer
- :source :native
- :field-ref [:field "a" {:base-type :type/Integer}]}
- {:name "a_2"
- :display-name "a"
- :base-type :type/Integer
- :source :native
- :field-ref [:field "a_2" {:base-type :type/Integer}]}]
+ (is (=? [{:name "a"
+ :display-name "a"
+ :base-type :type/Integer
+ ::result-metadata/source :native
+ ::result-metadata/field-ref [:field "a" {:base-type :type/Integer}]}
+ {:name "a_2"
+ :display-name "a"
+ :base-type :type/Integer
+ ::result-metadata/source :native
+ ::result-metadata/field-ref [:field "a_2" {:base-type :type/Integer}]}]
(column-info
(lib/query meta/metadata-provider query)
{:cols [{:name "a" :base_type :type/Integer} {:name "a" :base_type :type/Integer}]}))))))))
@@ -915,13 +915,13 @@
:display-name "Created At: Year"
:effective-type :type/Integer
;; additional keys in field ref are WRONG
- :field-ref [:field "CREATED_AT" (partial = {:base-type :type/DateTimeWithLocalTZ
+ ::result-metadata/field-ref [:field "CREATED_AT" (partial = {:base-type :type/DateTimeWithLocalTZ
:temporal-unit :year})]
:id (meta/id :orders :created-at)
:inherited-temporal-unit :year
:name "CREATED_AT"
:semantic-type :type/CreationTimestamp
- :source :breakout
+ ::result-metadata/source :breakout
:table-id (meta/id :orders)
:unit :year
:lib/deduplicated-name "CREATED_AT"
@@ -1108,24 +1108,24 @@
lib/append-stage
lib/append-stage
(as-> query (lib/remove-field query -1 (first (lib/fieldable-columns query -1)))))]
- (is (=? [{:name "CREATED_AT_2", :display-name "Created At: Month", :field-ref [:field "CREATED_AT_2" {}]}
- {:name "count", :display-name "Count", :field-ref [:field "count" {}]}]
+ (is (=? [{:name "CREATED_AT_2", :display-name "Created At: Month", ::result-metadata/field-ref [:field "CREATED_AT_2" {}]}
+ {:name "count", :display-name "Count", ::result-metadata/field-ref [:field "count" {}]}]
(result-metadata/returned-columns query))))))
(deftest ^:parallel deduplicate-field-refs-test
(testing "Don't return duplicate field refs, force deduplicated-name when they are ambiguous (QUE-1623)"
(let [cols [(-> (meta/field-metadata :venues :id)
(assoc :lib/source :source/previous-stage
- :field-ref [:field (meta/id :venues :id) nil]))
+ ::result-metadata/field-ref [:field (meta/id :venues :id) nil]))
(-> (meta/field-metadata :venues :name)
(assoc :lib/source :source/joins
- :field-ref [:field (meta/id :venues :name) nil]
+ ::result-metadata/field-ref [:field (meta/id :venues :name) nil]
:metabase.lib.join/join-alias "v1"
:lib/source-column-alias "NAME"
:lib/desired-column-alias "v1__NAME"))
(-> (meta/field-metadata :venues :name)
(assoc :lib/source :source/joins
- :field-ref [:field (meta/id :venues :name) nil]
+ ::result-metadata/field-ref [:field (meta/id :venues :name) nil]
:metabase.lib.join/join-alias "v2"
:lib/source-column-alias "NAME"
:lib/desired-column-alias "v2__NAME"))]
@@ -1133,7 +1133,7 @@
(is (=? [[:field (meta/id :venues :id) nil]
[:field "NAME" {}]
[:field "NAME_2" {}]]
- (map :field-ref (#'result-metadata/deduplicate-field-refs cols)))))))
+ (map ::result-metadata/field-ref (#'result-metadata/deduplicate-field-refs cols)))))))
(deftest ^:parallel remove-namespaced-options-test
(are [clause expected] (= expected
diff --git a/test/metabase/lib/query_test.cljc b/test/metabase/lib/query_test.cljc
index a1eb778c338..fb1bc680e53 100644
--- a/test/metabase/lib/query_test.cljc
+++ b/test/metabase/lib/query_test.cljc
@@ -3,7 +3,6 @@
#?@(:cljs ([metabase.test-runner.assert-exprs.approximately-equal]))
[clojure.test :refer [are deftest is testing]]
[clojure.walk :as walk]
- [medley.core :as m]
[metabase.lib.convert :as lib.convert]
[metabase.lib.core :as lib]
[metabase.lib.metadata :as lib.metadata]
@@ -105,31 +104,6 @@
(lib/query meta/metadata-provider converted-query))))))
-(deftest ^:parallel converted-query-leaves-stage-metadata-refs-alone
- (let [query (-> (lib/query meta/metadata-provider (meta/table-metadata :people))
- (lib/expression "BirthMonth" (lib/+ 1 1))
- (as-> $q (lib/breakout $q (m/find-first #(= (:name %) "BirthMonth") (lib/breakoutable-columns $q))))
- (lib/aggregate (lib/count))
- (lib/append-stage)
- (lib/aggregate (lib/count)))]
- (is (=? {:stages [{:lib/stage-metadata {:columns [{:field-ref [:expression "BirthMonth" {:base-type :type/Integer}]} {}]}} {}]}
- (lib/query meta/metadata-provider (assoc-in (lib.convert/->pMBQL (lib.convert/->legacy-MBQL query))
- [:stages 0 :lib/stage-metadata]
- {:columns [{:base-type :type/Float,
- :display-name "BirthMonth",
- :field-ref [:expression
- "BirthMonth"
- {:base-type :type/Integer}],
- :name "BirthMonth",
- :lib/type :metadata/column}
- {:base-type :type/Integer,
- :display-name "Count",
- :field-ref [:aggregation 0],
- :name "count",
- :semantic-type :type/Quantity,
- :lib/type :metadata/column}],
- :lib/type :metadata/results}))))))
-
(deftest ^:parallel stage-count-test
(is (= 1 (lib/stage-count (lib.tu/venues-query))))
(is (= 2 (lib/stage-count (lib/append-stage (lib.tu/venues-query)))))
diff --git a/test/metabase/lib/schema/metadata_test.cljc b/test/metabase/lib/schema/metadata_test.cljc
index 7ffad94f1ec..005f8e03a5c 100644
--- a/test/metabase/lib/schema/metadata_test.cljc
+++ b/test/metabase/lib/schema/metadata_test.cljc
@@ -9,64 +9,68 @@
[metabase.util :as u]))
(deftest ^:parallel normalize-column-metadata-test
- (let [col {"active" true
- "base-type" "type/Text"
- "database-type" "CHARACTER VARYING"
- "display-name" "Category"
- "effective-type" "type/Text"
- "field-ref" ["field" 61339 nil]
- "fingerprint" {"global" {"distinct-count" 4, "nil%" 0.0}
- "type" {"type/Text" {"average-length" 6.375
- "percent-email" 0.0
- "percent-json" 0.0
- "percent-state" 0.0
- "percent-url" 0.0}
- "type/Number" {"q1" 1.459}}}
- "id" 61339
- "lib/breakout?" true
- "lib/deduplicated-name" "CATEGORY"
- "lib/desired-column-alias" "CATEGORY"
- "lib/original-display-name" "Category"
- "lib/original-name" "CATEGORY"
- "lib/source" "source/table-defaults"
- "lib/source-column-alias" "CATEGORY"
- "lib/type" "metadata/column"
- "name" "CATEGORY"
- "position" 3
- "semantic-type" "type/Category"
- "settings" {"is_priceless" true}
- "source" "breakout"
- "table-id" 10808
- "visibility-type" "normal"}]
- (is (= {:active true
- :base-type :type/Text
- :database-type "CHARACTER VARYING"
- :display-name "Category"
- :effective-type :type/Text
- :field-ref [:field 61339 nil]
- :fingerprint {:global {:distinct-count 4, :nil% 0.0}
- :type {:type/Text {:average-length 6.375
- :percent-email 0.0
- :percent-json 0.0
- :percent-state 0.0
- :percent-url 0.0}
- :type/Number {:q1 1.459}}}
- :id 61339
- :name "CATEGORY"
- :position 3
- :semantic-type :type/Category
- :settings {:is_priceless true}
- :source :breakout
- :table-id 10808
- :visibility-type :normal
- :lib/breakout? true
- :lib/deduplicated-name "CATEGORY"
- :lib/desired-column-alias "CATEGORY"
- :lib/original-display-name "Category"
- :lib/original-name "CATEGORY"
- :lib/source :source/table-defaults
- :lib/source-column-alias "CATEGORY"
- :lib/type :metadata/column}
+ (let [col {"active" true
+ "base-type" "type/Text"
+ "database-type" "CHARACTER VARYING"
+ "display-name" "Category"
+ "effective-type" "type/Text"
+ ;; `:field-ref` should get removed, but the result-metadata key should get normalized
+ "field-ref" ["field" 61339 nil]
+ "metabase.lib.metadata.result-metadata/field-ref" ["field" 61339 nil]
+ "fingerprint" {"global" {"distinct-count" 4, "nil%" 0.0}
+ "type" {"type/Text" {"average-length" 6.375
+ "percent-email" 0.0
+ "percent-json" 0.0
+ "percent-state" 0.0
+ "percent-url" 0.0}
+ "type/Number" {"q1" 1.459}}}
+ "id" 61339
+ "lib/breakout?" true
+ "lib/deduplicated-name" "CATEGORY"
+ "lib/desired-column-alias" "CATEGORY"
+ "lib/original-display-name" "Category"
+ "lib/original-name" "CATEGORY"
+ "lib/source" "source/table-defaults"
+ "lib/source-column-alias" "CATEGORY"
+ "lib/type" "metadata/column"
+ "name" "CATEGORY"
+ "position" 3
+ "semantic-type" "type/Category"
+ "settings" {"is_priceless" true}
+ ;; `:source` should get removed, but the result-metadata version should get normalized.
+ "source" "breakout"
+ "metabase.lib.metadata.result-metadata/source" "breakout"
+ "table-id" 10808
+ "visibility-type" "normal"}]
+ (is (= {:active true
+ :base-type :type/Text
+ :database-type "CHARACTER VARYING"
+ :display-name "Category"
+ :effective-type :type/Text
+ :metabase.lib.metadata.result-metadata/field-ref [:field 61339 nil]
+ :fingerprint {:global {:distinct-count 4, :nil% 0.0}
+ :type {:type/Text {:average-length 6.375
+ :percent-email 0.0
+ :percent-json 0.0
+ :percent-state 0.0
+ :percent-url 0.0}
+ :type/Number {:q1 1.459}}}
+ :id 61339
+ :name "CATEGORY"
+ :position 3
+ :semantic-type :type/Category
+ :settings {:is_priceless true}
+ :metabase.lib.metadata.result-metadata/source :breakout
+ :table-id 10808
+ :visibility-type :normal
+ :lib/breakout? true
+ :lib/deduplicated-name "CATEGORY"
+ :lib/desired-column-alias "CATEGORY"
+ :lib/original-display-name "Category"
+ :lib/original-name "CATEGORY"
+ :lib/source :source/table-defaults
+ :lib/source-column-alias "CATEGORY"
+ :lib/type :metadata/column}
(lib/normalize ::lib.schema.metadata/column col)
;; should be able to detect that this is Lib metadata based on the use of `:base-type`
(lib/normalize ::lib.schema.metadata/lib-or-legacy-column (dissoc col "lib/type"))))))
diff --git a/test/metabase/lib_be/metadata/jvm_test.clj b/test/metabase/lib_be/metadata/jvm_test.clj
index cb33023c1f7..06122533e4c 100644
--- a/test/metabase/lib_be/metadata/jvm_test.clj
+++ b/test/metabase/lib_be/metadata/jvm_test.clj
@@ -269,3 +269,30 @@
(lib.metadata/table mp (:id buyer))))
(is (=? {:database-partitioned true}
(lib.metadata/field mp (:id buyer-id)))))))
+
+(deftest ^:parallel instance->metadata-normalize-column-test
+ (testing "instance->metadata should normalize column metadata"
+ (let [legacy-col {:active true
+ :base_type :type/BigInteger
+ :database_type "BIGINT"
+ :display_name "ID"
+ :effective_type :type/BigInteger
+ :field_ref [:field 760 nil]
+ :id 760
+ :lib/deduplicated-name "ID"
+ :lib/desired-column-alias "ID"
+ :lib/original-display-name "ID"
+ :lib/original-name "ID"
+ :lib/source :source/table-defaults
+ :lib/source-column-alias "ID"
+ :metabase.lib.query/transformation-added-base-type true
+ :name "ID"
+ :position 0
+ :semantic_type :type/PK
+ :source :fields
+ :table_id 227
+ :visibility_type :normal}
+ metadata (lib.metadata.jvm/instance->metadata legacy-col :metadata/column)]
+ (is (mr/validate ::lib.schema.metadata/column metadata))
+ (is (not (:field-ref metadata))
+ "Legacy keys like :field_ref/:field-ref should have been removed"))))
diff --git a/test/metabase/models/interface_test.clj b/test/metabase/models/interface_test.clj
index fb2aa394a64..6e3a6b8c5d1 100644
--- a/test/metabase/models/interface_test.clj
+++ b/test/metabase/models/interface_test.clj
@@ -194,7 +194,6 @@
:database-type "CHARACTER VARYING"
:display-name "Category"
:effective-type :type/Text
- :field-ref [:field 61339 nil]
:fingerprint {:global {:distinct-count 4, :nil% 0.0}
:type {:type/Text {:average-length 6.375
:percent-email 0.0
@@ -205,7 +204,6 @@
:name "CATEGORY"
:position 3
:semantic-type :type/Category
- :source :breakout
:table-id 10808
:visibility-type :normal
:lib/breakout? true
diff --git a/test/metabase/query_processor/middleware/add_implicit_joins_test.clj b/test/metabase/query_processor/middleware/add_implicit_joins_test.clj
index 903c339158e..7cfbb808215 100644
--- a/test/metabase/query_processor/middleware/add_implicit_joins_test.clj
+++ b/test/metabase/query_processor/middleware/add_implicit_joins_test.clj
@@ -897,8 +897,7 @@
:source-metadata [{:name "CATEGORY"
:display_name "Category"
:base_type :type/Number
- :id %category
- :field_ref $orders.product-id->category}]})]
+ :id %category}]})]
(is (=? query (add-implicit-joins query)))))))
(deftest ^:parallel metadata-join-alias-test-1b
@@ -925,42 +924,6 @@
:condition [:= $orders.product-id->category &Q2.$reviews.product-id->category]
:strategy :left-join}]})))))
-(deftest ^:parallel metadata-join-alias-test-2
- ;; With remapping, metadata may contain field with `:source-field` which is not used in corresponding query.
- ;; See [[metabase.parameters.custom-values-test/with-mbql-card-test]].
- (testing "`:join-alias` is correctly updated in metadata fields containing `:source-field`"
- (testing "#26631 Case 2: Join query with implicit join into a query with a table as source"
- (is (=? (lib.tu.macros/mbql-query products
- {:source-table $$products
- :joins [{:join-alias "Q2"
- :fields :all
- :condition [:= $category &Q2.$orders.product-id->category]
- :strategy :left-join
- :source-query {:source-table $$orders
- :aggregation [[:count]]
- :breakout [&PRODUCTS__via__PRODUCT_ID.$orders.product-id->category]
- :joins [{:alias "PRODUCTS__via__PRODUCT_ID"
- :fields :none
- :strategy :left-join
- :condition [:= $orders.product-id &PRODUCTS__via__PRODUCT_ID.$id]
- :source-table $$products
- :fk-field-id %orders.product-id}]}
- :source-metadata [{:field_ref &PRODUCTS__via__PRODUCT_ID.$orders.product-id->category}]}]})
- (add-implicit-joins
- (lib.tu.macros/mbql-query products
- {:source-table $$products
- :joins [{:join-alias "Q2"
- :fields :all
- :condition [:= $category &Q2.$orders.product-id->category]
- :strategy :left-join
- :source-query {:source-table $$orders
- :aggregation [[:count]]
- :breakout [$orders.product-id->category]}
- :source-metadata [{:name "CATEGORY"
- :display_name "Category"
- :base_type :type/Number
- :field_ref $orders.product-id->category}]}]})))))))
-
(deftest ^:parallel test-59695
(testing "Resolving an implicit join should not add field refs to incorrect places (#59695)"
(let [mp (lib.tu/mock-metadata-provider
@@ -1093,7 +1056,7 @@
stage (get-in query path)]
(is (=? {:joins [{:alias "CATEGORIES__via__CATEGORY_ID"}
{:alias "CATEGORIES__via__ID"}]}
- (#'qp.add-implicit-joins/resolve-implicit-joins-this-level query path stage))))))
+ (#'qp.add-implicit-joins/resolve-implicit-joins query path stage))))))
(deftest ^:parallel implicit-join-from-much-earlier-stage-test
(testing "if a join in stage 1 is used in stage 2, the field should propagate through stage 1 (#63245)"
diff --git a/test/metabase/query_processor/middleware/fetch_source_query_test.clj b/test/metabase/query_processor/middleware/fetch_source_query_test.clj
index 551e9eda10c..4e9d1ffdff4 100644
--- a/test/metabase/query_processor/middleware/fetch_source_query_test.clj
+++ b/test/metabase/query_processor/middleware/fetch_source_query_test.clj
@@ -43,7 +43,7 @@
(defn- remove-irrelevant-keys [col]
(as-> col col
- (dissoc col :field_ref)
+ (dissoc col :field_ref :source)
(m/filter-keys simple-keyword? col)))
(defn- default-result-with-inner-query
diff --git a/test/metabase/query_processor/middleware/resolve_joins_test.clj b/test/metabase/query_processor/middleware/resolve_joins_test.clj
index 8bcc9c2aafc..e1ec9dac8bb 100644
--- a/test/metabase/query_processor/middleware/resolve_joins_test.clj
+++ b/test/metabase/query_processor/middleware/resolve_joins_test.clj
@@ -180,8 +180,9 @@
(deftest ^:parallel resolve-source-query-with-fields-all-test
(testing "Can we resolve joins using a `:source-query` and `:fields` `:all`?"
- (let [source-metadata (qp.preprocess/query->expected-cols
- (lib/query meta/metadata-provider (lib.tu.macros/mbql-query categories {:limit 1})))
+ (let [source-metadata (for [col (qp.preprocess/query->expected-cols
+ (lib/query meta/metadata-provider (lib.tu.macros/mbql-query categories {:limit 1})))]
+ (dissoc col :source :field_ref))
resolved (resolve-joins
(lib.tu.macros/mbql-query venues
{:joins [{:alias "cat"
@@ -264,7 +265,6 @@
:display_name "User ID"
:base_type :type/Integer
:semantic_type :type/FK
- :field_ref [:field "_USER_ID" {:base-type :type/Integer :join-alias "alias"}]
:fingerprint {:global {:distinct-count 15, :nil% 0.0}}}]]
(is (= (lib.tu.macros/mbql-query users
{:fields [$id
diff --git a/test/metabase/query_processor/native_test.clj b/test/metabase/query_processor/native_test.clj
index 33b19a41c7d..ee2edb0ca07 100644
--- a/test/metabase/query_processor/native_test.clj
+++ b/test/metabase/query_processor/native_test.clj
@@ -59,14 +59,12 @@
:result-metadata [{:base_type :type/BigInteger
:display_name "ID"
:effective_type :type/BigInteger
- :field_ref [:field "ID" {:base-type :type/BigInteger}]
:fingerprint nil
:name "ID"
:semantic_type :type/PK}
{:base_type :type/BigInteger
:display_name "ID"
:effective_type :type/BigInteger
- :field_ref [:field "ID_2" {:base-type :type/BigInteger}]
:fingerprint nil
:name "ID"
:semantic_type :type/PK}]}]})
@@ -77,8 +75,9 @@
[:field "ID_2" {:base-type :type/Integer}]]}
:database (mt/id)
:type :query})]
- (is (=? ["ID" "ID_2"]
- (map :name (mt/cols (qp/process-query query)))))))))
+ (mt/with-native-query-testing-context query
+ (is (=? ["ID" "ID_2"]
+ (map :name (mt/cols (qp/process-query query))))))))))
(deftest ^:parallel native-referring-question-referring-question-test
(testing "Should be able to run native query referring a question referring a question (#25988)"
diff --git a/test/metabase/query_processor/preprocess_test.clj b/test/metabase/query_processor/preprocess_test.clj
index ed530a1dbd9..0eb9555179f 100644
--- a/test/metabase/query_processor/preprocess_test.clj
+++ b/test/metabase/query_processor/preprocess_test.clj
@@ -387,8 +387,12 @@
{:name "count", :display-name "Count"}]
(lib/returned-columns query))))
(testing `lib.metadata.result-metadata/returned-columns
- (is (=? [{:name "CREATED_AT_2", :display-name "Created At: Month", :field-ref [:field "CREATED_AT_2" {}]}
- {:name "count", :display-name "Count", :field-ref [:field "count" {}]}]
+ (is (=? [{:name "CREATED_AT_2"
+ :display-name "Created At: Month"
+ :metabase.lib.metadata.result-metadata/field-ref [:field "CREATED_AT_2" {}]}
+ {:name "count"
+ :display-name "Count"
+ :metabase.lib.metadata.result-metadata/field-ref [:field "count" {}]}]
(lib.metadata.result-metadata/returned-columns query))))
(testing `qp.preprocess/query->expected-cols
;; I think traditionally this field ref would have used a Field ID, and `:field_ref` should aim to preserve