Skip to content

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

[Lib] make `visible-columns` only work on stages (#61943)

Cam Saulc6674c01859f
diff --git a/src/metabase/lib/aggregation.cljc b/src/metabase/lib/aggregation.cljc
index 1cd23f27312..dfab8ca17b4 100644
--- a/src/metabase/lib/aggregation.cljc
+++ b/src/metabase/lib/aggregation.cljc
@@ -336,8 +336,7 @@
([query :- ::lib.schema/query
stage-number :- :int]
- (let [stage (lib.util/query-stage query stage-number)
- columns (lib.metadata.calculation/visible-columns query stage-number stage)
+ (let [columns (lib.metadata.calculation/visible-columns query stage-number)
with-columns (fn [{:keys [requires-column? supported-field] :as operator}]
(cond
(not requires-column?)
@@ -449,9 +448,8 @@
([query :- ::lib.schema/query
stage-number :- :int
aggregation-position :- [:maybe ::lib.schema.common/int-greater-than-or-equal-to-zero]]
- (let [stage (lib.util/query-stage query stage-number)
- agg-cols (aggregations-metadata query stage-number)
- columns (into (vec (lib.metadata.calculation/visible-columns query stage-number stage))
+ (let [agg-cols (aggregations-metadata query stage-number)
+ columns (into (vec (lib.metadata.calculation/visible-columns query stage-number))
(cond->> agg-cols
aggregation-position (keep-indexed (fn [i a]
(when (not= i aggregation-position)
diff --git a/src/metabase/lib/breakout.cljc b/src/metabase/lib/breakout.cljc
index 84e140c91f1..e048fda9d6c 100644
--- a/src/metabase/lib/breakout.cljc
+++ b/src/metabase/lib/breakout.cljc
@@ -75,9 +75,8 @@
([query :- ::lib.schema/query
stage-number :- :int]
- (let [columns (let [stage (lib.util/query-stage query stage-number)
- options {:include-implicitly-joinable-for-source-card? false}]
- (lib.metadata.calculation/visible-columns query stage-number stage options))]
+ (let [columns (let [options {:include-implicitly-joinable-for-source-card? false}]
+ (lib.metadata.calculation/visible-columns query stage-number options))]
(when (seq columns)
(let [existing-breakouts (breakouts query stage-number)
column->breakout-positions (group-by
diff --git a/src/metabase/lib/card.cljc b/src/metabase/lib/card.cljc
index 4947c553471..8250f320939 100644
--- a/src/metabase/lib/card.cljc
+++ b/src/metabase/lib/card.cljc
@@ -39,16 +39,6 @@
(= (:type card-metadata) :metric) (assoc :metric? true)
(= (lib.util/source-card-id query) (:id card-metadata)) (assoc :is-source-card true)))
-(mu/defmethod lib.metadata.calculation/visible-columns-method :metadata/card :- ::lib.metadata.calculation/visible-columns
- [query :- ::lib.schema/query
- stage-number :- :int
- {:keys [fields result-metadata] :as card-metadata} :- ::lib.schema.metadata/card
- {:keys [include-implicitly-joinable?] :as options} :- [:maybe ::lib.metadata.calculation/visible-columns.options]]
- (concat
- (lib.metadata.calculation/returned-columns query stage-number card-metadata options)
- (when include-implicitly-joinable?
- (lib.metadata.calculation/implicitly-joinable-columns query stage-number (concat fields result-metadata)))))
-
(mu/defn fallback-display-name :- ::lib.schema.common/non-blank-string
"If for some reason the metadata is unavailable. This is better than returning nothing I guess."
[card-id :- ::lib.schema.id/card]
diff --git a/src/metabase/lib/column_group.cljc b/src/metabase/lib/column_group.cljc
index 5b1272e238d..93d7cf3ab53 100644
--- a/src/metabase/lib/column_group.cljc
+++ b/src/metabase/lib/column_group.cljc
@@ -108,8 +108,7 @@
(when-let [;; TODO: This is clumsy and expensive; there is likely a neater way to find the full FK column.
;; Note that using `lib.metadata/field` is out - we need to respect metadata overrides etc. in models, and
;; `lib.metadata/field` uses the field's original status.
- fk-column (->> (lib.util/query-stage query stage-number)
- (lib.metadata.calculation/visible-columns query stage-number)
+ fk-column (->> (lib.metadata.calculation/visible-columns query stage-number)
(m/find-first #(and (= (:id %) fk-field-id)
(= (lib.field.util/inherited-column-name %) fk-field-name)
(= (lib.join.util/current-join-alias %) fk-join-alias)
diff --git a/src/metabase/lib/drill_thru/common.cljc b/src/metabase/lib/drill_thru/common.cljc
index 593b3a9c42e..c07cd496e21 100644
--- a/src/metabase/lib/drill_thru/common.cljc
+++ b/src/metabase/lib/drill_thru/common.cljc
@@ -59,7 +59,6 @@
(->> (lib.metadata.calculation/visible-columns
query
stage-number
- (lib.util/query-stage query stage-number)
{:include-joined? false
:include-expressions? false
:include-implicitly-joinable? false
diff --git a/src/metabase/lib/drill_thru/zoom_in_geographic.cljc b/src/metabase/lib/drill_thru/zoom_in_geographic.cljc
index 6d636a78ef0..4d624471bfc 100644
--- a/src/metabase/lib/drill_thru/zoom_in_geographic.cljc
+++ b/src/metabase/lib/drill_thru/zoom_in_geographic.cljc
@@ -88,7 +88,6 @@
[metabase.lib.schema.metadata :as lib.schema.metadata]
[metabase.lib.types.isa :as lib.types.isa]
[metabase.lib.underlying :as lib.underlying]
- [metabase.lib.util :as lib.util]
[metabase.util.malli :as mu]))
(def ^:private ContextWithLatLon
@@ -104,15 +103,14 @@
[query :- ::lib.schema/query
stage-number :- :int
{:keys [row], :as context} :- ::lib.schema.drill-thru/context]
- (let [stage (lib.util/query-stage query stage-number)
- ;; First check returned columns in case we breakout by lat/lon so we maintain the binning, othwerwise check visible.
+ (let [;; First check returned columns in case we breakout by lat/lon so we maintain the binning, othwerwise check visible.
[lat-column lon-column] (some
(fn [columns]
(when-let [lat-column (m/find-first lib.types.isa/latitude? columns)]
(when-let [lon-column (m/find-first lib.types.isa/longitude? columns)]
[lat-column lon-column])))
- [(lib.metadata.calculation/returned-columns query stage-number stage)
- (lib.metadata.calculation/visible-columns query stage-number stage)])]
+ [(lib.metadata.calculation/returned-columns query stage-number)
+ (lib.metadata.calculation/visible-columns query stage-number)])]
(when (and lat-column lon-column)
(letfn [(same-column? [col-x col-y]
(if (:id col-x)
diff --git a/src/metabase/lib/expression.cljc b/src/metabase/lib/expression.cljc
index a253ea86373..f7ec67e02f8 100644
--- a/src/metabase/lib/expression.cljc
+++ b/src/metabase/lib/expression.cljc
@@ -266,8 +266,7 @@
temporal-unit)
#_(defn- conflicting-name? [query stage-number expression-name]
- (let [stage (lib.util/query-stage query stage-number)
- cols (lib.metadata.calculation/visible-columns query stage-number stage)
+ (let [cols (lib.metadata.calculation/visible-columns query stage-number)
expr-name (u/lower-case-en expression-name)]
(some #(-> % :name u/lower-case-en (= expr-name)) cols)))
@@ -477,12 +476,11 @@
;; Changing the legacy/wire format is probably the right way to go, but that's a bigger
;; endeavor.
expression-position :- [:maybe ::lib.schema.common/int-greater-than-or-equal-to-zero]]
- (let [stage (lib.util/query-stage query stage-number)
- expr-name (when expression-position
+ (let [expr-name (when expression-position
(some-> (expressions query stage-number)
(nth expression-position nil)
lib.util/expression-name))
- columns (cond->> (lib.metadata.calculation/visible-columns query stage-number stage)
+ columns (cond->> (lib.metadata.calculation/visible-columns query stage-number)
expr-name (into [] (remove #(and (= (:lib/source %) :source/expressions)
(= (:name %) expr-name)))))]
(not-empty columns))))
diff --git a/src/metabase/lib/field.cljc b/src/metabase/lib/field.cljc
index 5c4c899c8ca..f2420db106d 100644
--- a/src/metabase/lib/field.cljc
+++ b/src/metabase/lib/field.cljc
@@ -452,7 +452,6 @@
(lib.metadata.calculation/visible-columns
query
stage-number
- (lib.util/query-stage query stage-number)
{:include-joined? false
:include-expressions? true
:include-implicitly-joinable? false})))
@@ -505,7 +504,6 @@
stage-number :- :int]
(let [visible-columns (lib.metadata.calculation/visible-columns query
stage-number
- (lib.util/query-stage query stage-number)
{:include-joined? false
:include-expressions? false
:include-implicitly-joinable? false})
@@ -696,13 +694,12 @@
([query :- ::lib.schema/query
stage-number :- :int
field-ref :- some?]
- (let [stage (lib.util/query-stage query stage-number)
- ;; not 100% sure why, but [[lib.metadata.calculation/visible-columns]] doesn't seem to return aggregations,
+ (let [;; not 100% sure why, but [[lib.metadata.calculation/visible-columns]] doesn't seem to return aggregations,
;; so we have to use [[lib.metadata.calculation/returned-columns]] instead.
columns ((if (= (lib.dispatch/dispatch-value field-ref) :aggregation)
lib.metadata.calculation/returned-columns
lib.metadata.calculation/visible-columns)
- query stage-number stage)]
+ query stage-number)]
(lib.equality/find-matching-column query stage-number field-ref columns))))
(defn json-field?
diff --git a/src/metabase/lib/filter.cljc b/src/metabase/lib/filter.cljc
index e1dafe55a6e..322f98325eb 100644
--- a/src/metabase/lib/filter.cljc
+++ b/src/metabase/lib/filter.cljc
@@ -468,11 +468,10 @@
([query :- ::lib.schema/query
stage-number :- :int]
- (let [stage (lib.util/query-stage query stage-number)
- columns (sequence
+ (let [columns (sequence
(comp (map add-column-operators)
(clojure.core/filter :operators))
- (lib.metadata.calculation/visible-columns query stage-number stage))
+ (lib.metadata.calculation/visible-columns query stage-number))
existing-filters (filters query stage-number)]
(cond
(empty? columns)
@@ -515,8 +514,7 @@
stage-number :- :int
a-filter-clause :- ::lib.schema.expression/boolean]
(let [[op _ first-arg] a-filter-clause
- stage (lib.util/query-stage query stage-number)
- columns (lib.metadata.calculation/visible-columns query stage-number stage)
+ columns (lib.metadata.calculation/visible-columns query stage-number)
col (lib.equality/find-matching-column query stage-number first-arg columns)]
(clojure.core/or (m/find-first #(clojure.core/= (:short %) op)
(lib.filter.operator/filter-operators col))
@@ -577,8 +575,7 @@
stage-number :- :int
a-filter-clause :- ::lib.schema.expression/boolean]
(let [[op options first-arg & rest-args] a-filter-clause
- stage (lib.util/query-stage query stage-number)
- columns (lib.metadata.calculation/visible-columns query stage-number stage)
+ columns (lib.metadata.calculation/visible-columns query stage-number)
col (lib.equality/find-matching-column query stage-number first-arg columns)]
{:lib/type :mbql/filter-parts
:operator (clojure.core/or (m/find-first #(clojure.core/= (:short %) op)
diff --git a/src/metabase/lib/join.cljc b/src/metabase/lib/join.cljc
index c65a743d779..a22fde974ce 100644
--- a/src/metabase/lib/join.cljc
+++ b/src/metabase/lib/join.cljc
@@ -381,17 +381,17 @@
[query :- ::lib.schema/query
stage-number :- :int
{join-alias :alias, :as join} :- ::lib.schema.join/join
- options :- ::lib.metadata.calculation/visible-columns.options]
+ options :- [:maybe ::lib.metadata.calculation/returned-columns.options]]
(into []
(comp (map lib.field.util/update-keys-for-col-from-previous-stage)
(map #(column-from-join query stage-number % join-alias)))
- (lib.metadata.calculation/visible-columns query stage-number join options)))
+ (lib.metadata.calculation/returned-columns query stage-number join options)))
(mu/defn all-joins-visible-columns-relative-to-parent-stage :- ::lib.metadata.calculation/visible-columns
- "Convenience for calling [[lib.metadata.calculation/visible-columns]] on all of the joins in a query stage."
+ "Convenience for calling [[join-visible-columns-relative-to-parent-stage]] on all of the joins in a query stage."
[query :- ::lib.schema/query
stage-number :- :int
- options :- ::lib.metadata.calculation/visible-columns.options]
+ options :- [:maybe ::lib.metadata.calculation/returned-columns.options]]
(into []
(mapcat (fn [join]
(join-visible-columns-relative-to-parent-stage query stage-number join options)))
@@ -592,9 +592,10 @@
(defn default-alias
"Generate a default `:alias` for a join clause. home-cols should be visible columns for the stage"
([query stage-number a-join]
- (let [stage (lib.util/query-stage query stage-number)
- home-cols (lib.metadata.calculation/visible-columns query stage-number stage)]
+ (let [stage (lib.util/query-stage query stage-number)
+ home-cols (lib.metadata.calculation/visible-columns query stage-number)]
(default-alias query stage-number a-join stage home-cols)))
+
([query _stage-number a-join stage home-cols]
(let [home-cols home-cols
cond-fields (into []
@@ -625,7 +626,7 @@
(dissoc % :alias)
%)
joins))))
- home-cols (lib.metadata.calculation/visible-columns query stage-number stage)
+ home-cols (lib.metadata.calculation/visible-columns query stage-number)
join-alias (default-alias query stage-number a-join stage home-cols)
join-cols (lib.metadata.calculation/returned-columns
(lib.query/query-with-stages query (:stages a-join)))]
@@ -848,9 +849,7 @@
(comp (map lib.join.util/current-join-alias)
(drop-while #(not= % existing-join-alias)))
(joins query stage-number))]
- (->> (lib.metadata.calculation/visible-columns query stage-number
- (lib.util/query-stage query stage-number)
- {:include-implicitly-joinable? false})
+ (->> (lib.metadata.calculation/visible-columns query stage-number {:include-implicitly-joinable? false})
(remove (fn [col]
(when-let [col-join-alias (lib.join.util/current-join-alias col)]
(contains? join-aliases-to-ignore col-join-alias))))
@@ -895,7 +894,7 @@
(cond-> rhs-expression-or-nil
;; Drop the :join-alias from the RHS if the joinable doesn't have one either.
(not join-alias) (lib.options/update-options dissoc :join-alias)))]
- (->> (lib.metadata.calculation/visible-columns query stage-number joinable {:include-implicitly-joinable? false})
+ (->> (lib.metadata.calculation/returned-columns query stage-number joinable)
(map (fn [col]
(if join-alias
(column-from-join query stage-number col join-alias)
@@ -924,16 +923,26 @@
::lib.schema.metadata/column
[:map
[::target ::lib.schema.metadata/column]]]]]
- "Find FK columns in `source` pointing at a column in `target`. Includes the target column under the `::target` key."
+ "Find FK columns in `source` pointing at a column in `target`. Includes the target column under the `::target` key.
+
+ `source` and `target` are `::current-stage` and a [[Joinable]], in either order; `::current-stage` means use the
+ stage in `query` at `stage-number`."
[query :- ::lib.schema/query
stage-number :- :int
- source
- target]
- (let [target-columns (delay
- (lib.metadata.calculation/visible-columns
- query stage-number target
- {:include-implicitly-joinable? false
- :include-implicitly-joinable-for-source-card? false}))]
+ source :- [:or
+ [:= ::current-stage]
+ Joinable]
+ target :- [:or
+ [:= ::current-stage]
+ Joinable]]
+ (let [current-stage-cols (fn []
+ (let [opts {:include-implicitly-joinable? false
+ :include-implicitly-joinable-for-source-card? false}]
+ (lib.metadata.calculation/visible-columns query stage-number opts)))
+ target-columns (delay
+ (if (= target ::current-stage)
+ (current-stage-cols)
+ (lib.metadata.calculation/returned-columns query stage-number target)))]
(not-empty
(into []
(keep (fn [{:keys [fk-target-field-id], :as col}]
@@ -944,9 +953,9 @@
(:id target-column)))
@target-columns)]
(assoc col ::target target-column)))))
- (lib.metadata.calculation/visible-columns query stage-number source
- {:include-implicitly-joinable? false
- :include-implicitly-joinable-for-source-card? false})))))
+ (if (= source ::current-stage)
+ (current-stage-cols)
+ (lib.metadata.calculation/returned-columns query stage-number source))))))
(mu/defn suggested-join-conditions :- [:maybe [:sequential {:min 1} ::lib.schema.expression/boolean]] ; i.e., a filter clause
"Return suggested default join conditions when constructing a join against `joinable`, e.g. a Table, Saved
@@ -972,8 +981,7 @@
(lib.util/update-query-stage query stage-number
u/assoc-dissoc :joins new-joins))
;; If this is a new joinable, use the entire current query.
- query)
- stage (lib.util/query-stage unjoined stage-number)]
+ query)]
(letfn [;; only keep one FK to each target column e.g. for
;;
;; messages (sender_id REFERENCES user(id), recipient_id REFERENCES user(id))
@@ -988,13 +996,13 @@
(lib.filter/filter-clause (lib.filter.operator/operator-def :=) x y))]
(or
;; find cases where we have FK(s) pointing to joinable. Our column goes on the LHS.
- (when-let [fks (fks stage joinable)]
+ (when-let [fks (fks ::current-stage joinable)]
(mapv (fn [fk]
(filter-clause fk (::target fk)))
fks))
;; find cases where the `joinable` has FK(s) pointing to us. Note our column is the target this time around --
;; keep in on the LHS.
- (when-let [fks (fks joinable stage)]
+ (when-let [fks (fks joinable ::current-stage)]
(mapv (fn [fk]
(filter-clause (::target fk) fk))
fks)))))))
diff --git a/src/metabase/lib/js.cljs b/src/metabase/lib/js.cljs
index 8cbd36d80f9..4fd3be1a62c 100644
--- a/src/metabase/lib/js.cljs
+++ b/src/metabase/lib/js.cljs
@@ -1429,9 +1429,8 @@
(defn- visible-columns*
"Inner implementation for [[visible-columns]], which wraps this with caching."
[a-query stage-number]
- (let [stage (lib.util/query-stage a-query stage-number)
- vis-columns (lib.metadata.calculation/visible-columns a-query stage-number stage)
- ret-columns (lib.metadata.calculation/returned-columns a-query stage-number stage)]
+ (let [vis-columns (lib.metadata.calculation/visible-columns a-query stage-number)
+ ret-columns (lib.metadata.calculation/returned-columns a-query stage-number)]
(to-array (lib.equality/mark-selected-columns a-query stage-number vis-columns ret-columns))))
(defn ^:export visible-columns
diff --git a/src/metabase/lib/metadata/calculation.cljc b/src/metabase/lib/metadata/calculation.cljc
index d3a3d84ade4..23caf940dca 100644
--- a/src/metabase/lib/metadata/calculation.cljc
+++ b/src/metabase/lib/metadata/calculation.cljc
@@ -483,7 +483,17 @@
[:include-remaps? {:optional true, :default false} :boolean]]
[:fn
{:error/message "unique-name-fn is no longer allowed as an option."}
- (complement :unique-name-fn)]])
+ (complement :unique-name-fn)]
+ ;; historically the third arg to [[visible-columns]] was something like a stage; catch code that has not been
+ ;; updated yet.
+ [:fn
+ {:error/message "Expected an options map, got something with :lib/type"
+ :error/fn (fn [{:keys [value]} _]
+ (str "Expected an options map, got a " (pr-str (:lib/type value))))}
+ (complement :lib/type)]])
+
+(def ^:private returned-columns-options-keys
+ (mu/map-schema-keys ::returned-columns.options))
(defmulti returned-columns-method
"Impl for [[returned-columns]]."
@@ -534,8 +544,12 @@
x
options :- [:maybe ::returned-columns.options]]
(binding [*propagate-binning-and-bucketing* true]
- (lib.metadata.cache/with-cached-value query (cache-key ::returned-columns query stage-number x options)
- (returned-columns-method query stage-number x options)))))
+ ;; minor optimization for caching purposes: only keep the options keys that are actually relevant for
+ ;; `returned-columns` purposes. As a bonus, this means undocumented options keys that aren't part of the schema
+ ;; will effectively be ignored, which sorta forces people to actually go document them.
+ (let [options (select-keys options returned-columns-options-keys)]
+ (lib.metadata.cache/with-cached-value query (cache-key ::returned-columns query stage-number x options)
+ (returned-columns-method query stage-number x options))))))
(mr/def ::visible-column
"Schema for a column that should be returned by [[visible-columns]]. A visible column is a column metadata that is
@@ -551,21 +565,22 @@
[:lib/source-column-alias ::lib.schema.metadata/source-column-alias]]])
(mr/def ::visible-columns
- "Schema for column metadata that should be returned by [[visible-columns]] and implementations
- of [[visible-columns-method]]."
+ "Schema for column metadata that should be returned by [[visible-columns]]."
[:sequential ::visible-column])
(mr/def ::visible-columns.options
- "Schema for options passed to [[visible-columns]] and [[visible-columns-method]]."
+ "Schema for options passed to [[visible-columns]]."
[:merge
[:ref ::returned-columns.options]
[:map
- ;; these all default to true
[:include-joined? {:optional true, :default true} :boolean]
[:include-expressions? {:optional true, :default true} :boolean]
[:include-implicitly-joinable? {:optional true, :default true} :boolean]
[:include-implicitly-joinable-for-source-card? {:optional true, :default true} :boolean]]])
+(def ^:private visible-columns-options-keys
+ (mu/map-schema-keys ::visible-columns.options))
+
(mu/defn- default-visible-columns-options :- ::visible-columns.options
[]
{:include-joined? true
@@ -573,30 +588,12 @@
:include-implicitly-joinable? true
:include-implicitly-joinable-for-source-card? true})
-(defmulti visible-columns-method
- "Impl for [[visible-columns]]."
- {:arglists '([query stage-number x options])}
- (fn [_query _stage-number x _options]
- (lib.dispatch/dispatch-value x))
- :hierarchy lib.hierarchy/hierarchy)
-
-(defmethod visible-columns-method :dispatch-type/nil
- [_query _stage-number _x _options]
- [])
-
-;;; default impl is just the impl for [[returned-columns-method]]
-(defmethod visible-columns-method :default
- [query stage-number x options]
- (returned-columns-method query stage-number x options))
-
-;;; if you pass in an integer assume it's a stage number; use the method for the query stage itself.
-(defmethod visible-columns-method :dispatch-type/integer
- [query _stage-number stage-number options]
- (visible-columns-method query stage-number (lib.util/query-stage query stage-number) options))
-
-;; TODO (Cam 8/7/25) -- Braden and I are in agreement that `visible-columns` fundamentally does not make sense for
-;; anything other than a specific query stage, and we should probably remove it entirely in the near future and leave
-;; just the stage implementation. See https://github.com/metabase/metabase/pull/61767#discussion_r2260558780
+;;; TODO (Cam 8/7/25) -- historically `visible-columns` worked on a bunch of stuff besides just a stage, but Braden
+;;; pointed out that it really only makes any sense at all for a stage here
+;;; https://metaboat.slack.com/archives/C0645JP1W81/p1754587431585559 . For historic reasons the main entrypoint to
+;;; the function still lives here, but it's now just a shim that calls [[metabase.lib.stage/visible-columns*]] -- we
+;;; should combine these so there's no more indirection. It should probably live in [[metabase.lib.stage]] going
+;;; forward, but we'd need to update a lot of code that calls this version of [[visible-columns]].
(mu/defn visible-columns :- ::visible-columns
"Return a sequence of columns that should be visible *within* a given stage of something, e.g. a query stage or a
join query. This includes not just the columns that get returned (ones present in [[metadata]], but other columns
@@ -610,23 +607,31 @@
and would thus be returned by this function.
Columns from joins, expressions, and implicitly joinable columns are included automatically by default;
- see [[VisibleColumnsOptions]] for the options for disabling these columns."
+ see `::visible-columns.options` for the options for disabling these columns."
([query]
- (visible-columns query (lib.util/query-stage query -1)))
+ (visible-columns query -1))
- ([query x]
- (visible-columns query -1 x))
+ ([query stage-number]
+ ;; apparently the FE sometimes accidentally calls this with a `nil` stage number -- see #31366 -- in that case just
+ ;; return an empty vector. The FE only has access to this arity, so we don't need to do the check below.
+ ;; See [[metabase.lib.metadata.calculation-test/visible-columns-test-2]]
+ (if (and query stage-number)
+ (visible-columns query stage-number nil)
+ []))
- ([query stage-number x]
- (visible-columns query stage-number x nil))
-
- ([query :- ::lib.schema/query
+ ([query :- [:maybe ::lib.schema/query]
stage-number :- :int
- x
options :- [:maybe ::visible-columns.options]]
- (let [options (merge (default-visible-columns-options) options)]
- (lib.metadata.cache/with-cached-value query (cache-key ::visible-columns query stage-number x options)
- (visible-columns-method query stage-number x options)))))
+ (let [options (-> (merge (default-visible-columns-options) options)
+ ;; minor caching optimization: only keep the keys that are relevant to `visible-columns` --
+ ;; ignore any other ones. As a bonus, this means undocumented options keys that aren't part of
+ ;; the schema will effectively be ignored.
+ (select-keys visible-columns-options-keys))]
+ (lib.metadata.cache/with-cached-value query (cache-key ::visible-columns query stage-number nil options)
+ ((#?(:clj requiring-resolve :cljs resolve) 'metabase.lib.stage/-visible-columns)
+ query
+ stage-number
+ options)))))
(mu/defn remapped-columns :- [:maybe ::visible-columns]
"Given a seq of columns, return metadata for any remapped columns, if the `:include-remaps?` option is set."
@@ -657,7 +662,7 @@
(lib.metadata/fields query table-id)
(returned-columns query))))
-(defn implicitly-joinable-columns
+(mu/defn implicitly-joinable-columns :- ::visible-columns
"Columns that are implicitly joinable from some other columns in `column-metadatas`. To be joinable, the column has to
have (1) appropriate FK metadata, i.e. have an `:fk-target-field-id` pointing to another Field, and (2) have a numeric
`:id`, i.e. be a real database column that can be used in a JOIN condition. (I think we only include this information
@@ -666,12 +671,14 @@
Does not include columns from any Tables that are already explicitly joined.
Does not include columns that would be implicitly joinable via multiple hops."
- [query stage-number column-metadatas]
- (let [remap-target-ids (into #{} (keep (comp :field-id :lib/external-remap)) column-metadatas)
+ [query :- ::lib.schema/query
+ stage-number :- :int
+ cols :- [:sequential ::lib.schema.metadata/column]]
+ (let [remap-target-ids (into #{} (keep (comp :field-id :lib/external-remap)) cols)
existing-table-ids (into #{} (comp (remove (comp remap-target-ids :id))
(map :table-id))
- column-metadatas)
- fk-fields (into [] (filter (every-pred :fk-target-field-id (comp number? :id))) column-metadatas)
+ cols)
+ fk-fields (into [] (filter (every-pred :fk-target-field-id (comp number? :id))) cols)
id->target-fields (m/index-by :id (lib.metadata/bulk-metadata
query :metadata/column (into #{} (map :fk-target-field-id) fk-fields)))
target-fields (into []
@@ -688,15 +695,8 @@
query :metadata/table (into #{} (map :table-id) target-fields)))]
(into []
(mapcat (fn [{:keys [table-id], ::keys [fk-field-id fk-field-name fk-join-alias]}]
- (let [table-metadata (id->table table-id)
- ;; Shouldn't we be forwarding the rest of the `options` as well? -- Cam
- ;;
- ;; I wouldn't say so. This is deliberately minimal, including only the core/default columns
- ;; for an implicit join. In practice since implicit joins are only to tables (or cards, in
- ;; the future) the other options (including joins, expressions, etc.) are not relevant. It's
- ;; always the table's columns, or the returned-columns of the card. -- Braden
- options {:include-implicitly-joinable? false}]
- (for [field (visible-columns-method query stage-number table-metadata options)]
+ (let [table (id->table table-id)]
+ (for [field (returned-columns query stage-number table)]
(m/assoc-some field
:fk-field-id fk-field-id
:fk-field-name fk-field-name
diff --git a/src/metabase/lib/order_by.cljc b/src/metabase/lib/order_by.cljc
index 4666baa9f64..7de0bd0ecf4 100644
--- a/src/metabase/lib/order_by.cljc
+++ b/src/metabase/lib/order_by.cljc
@@ -152,9 +152,8 @@
aggregations (not-empty (lib.aggregation/aggregations-metadata query stage-number))
columns (if (or breakouts aggregations)
(concat breakouts aggregations)
- (let [stage (lib.util/query-stage query stage-number)
- options {:include-implicitly-joinable-for-source-card? false}]
- (lib.metadata.calculation/visible-columns query stage-number stage options)))
+ (let [options {:include-implicitly-joinable-for-source-card? false}]
+ (lib.metadata.calculation/visible-columns query stage-number options)))
columns (filter orderable-column? columns)
existing-order-bys (->> (order-bys query stage-number)
(map (fn [[_tag _opts expr]]
diff --git a/src/metabase/lib/remove_replace.cljc b/src/metabase/lib/remove_replace.cljc
index c80ae660058..b1141f924e8 100644
--- a/src/metabase/lib/remove_replace.cljc
+++ b/src/metabase/lib/remove_replace.cljc
@@ -205,8 +205,7 @@
(defn- remove-stage-references
[query previous-stage-number unmodified-query-for-stage target-uuid]
(if-let [stage-number (lib.util/next-stage-number unmodified-query-for-stage previous-stage-number)]
- (let [stage (lib.util/query-stage unmodified-query-for-stage stage-number)
- target-ref-id (->> (lib.metadata.calculation/visible-columns unmodified-query-for-stage stage-number stage)
+ (let [target-ref-id (->> (lib.metadata.calculation/visible-columns unmodified-query-for-stage stage-number)
(some (fn [{:keys [lib/source lib/source-uuid] :as column}]
(when (and (= :source/previous-stage source) (= target-uuid source-uuid))
(:lib/source-column-alias column)))))]
@@ -564,8 +563,8 @@
(defn- remove-matching-missing-columns
[query-after query-before stage-number match-spec]
(let [removed-cols (set/difference
- (set (lib.metadata.calculation/visible-columns query-before stage-number (lib.util/query-stage query-before stage-number)))
- (set (lib.metadata.calculation/visible-columns query-after stage-number (lib.util/query-stage query-after stage-number))))]
+ (set (lib.metadata.calculation/visible-columns query-before stage-number))
+ (set (lib.metadata.calculation/visible-columns query-after stage-number)))]
(reduce
#(apply remove-local-references %1 stage-number query-after (match-spec %2))
query-after
diff --git a/src/metabase/lib/stage.cljc b/src/metabase/lib/stage.cljc
index ae94f00349d..eadbef5f6d1 100644
--- a/src/metabase/lib/stage.cljc
+++ b/src/metabase/lib/stage.cljc
@@ -112,15 +112,15 @@
(lib.util/query-stage query previous-stage-number)
options)))))
-(mu/defn- saved-question-visible-columns :- [:maybe ::lib.metadata.calculation/visible-columns]
+(mu/defn- saved-question-returned-columns :- [:maybe ::lib.metadata.calculation/returned-columns]
"Metadata associated with a Saved Question, e.g. if we have a `:source-card`"
[query :- ::lib.schema/query
stage-number :- :int
card-id :- [:maybe ::lib.schema.id/card]
- options :- ::lib.metadata.calculation/visible-columns.options]
+ options :- ::lib.metadata.calculation/returned-columns.options]
(when card-id
(when-let [card (lib.metadata/card query card-id)]
- (not-empty (lib.metadata.calculation/visible-columns query stage-number card options)))))
+ (not-empty (lib.metadata.calculation/returned-columns query stage-number card options)))))
;;; TODO (Cam 8/6/25) -- this should probably live in [[metabase.lib.metric]]
(mu/defn- metric-visible-columns :- [:maybe ::lib.metadata.calculation/visible-columns]
@@ -133,7 +133,6 @@
(not-empty (lib.metadata.calculation/visible-columns
(assoc metric-query :lib/metadata (:lib/metadata query))
-1
- (lib.util/query-stage metric-query -1)
options))))
(mu/defn- expressions-metadata :- [:maybe ::lib.metadata.calculation/visible-columns]
@@ -189,15 +188,15 @@
;; 1b: default visible Fields for the source Table
(when source-table
(assert (integer? source-table))
- (let [table-metadata (lib.metadata/table query source-table)]
- (lib.metadata.calculation/visible-columns query stage-number table-metadata options)))
+ (let [table (lib.metadata/table query source-table)]
+ (lib.metadata.calculation/returned-columns query stage-number table options)))
;; 1e. Metadata associated with a Metric
(when metric-based?
(metric-visible-columns query stage-number card options))
;; 1c. Metadata associated with a saved Question
(when source-card
- (when-let [cols (not-empty (saved-question-visible-columns query stage-number source-card
- (assoc options :include-implicitly-joinable? false)))]
+ (when-let [cols (not-empty (saved-question-returned-columns query stage-number source-card
+ (assoc options :include-implicitly-joinable? false)))]
(into []
(comp (map lib.field.util/update-keys-for-col-from-previous-stage)
(map (fn [col]
@@ -229,10 +228,17 @@
(when include-joined?
(lib.join/all-joins-visible-columns-relative-to-parent-stage query stage-number options)))))
-(mu/defmethod lib.metadata.calculation/visible-columns-method ::stage :- ::lib.metadata.calculation/visible-columns
+;;; TODO (Cam 8/7/25) -- we should probably just move all of `visible-columns` to here since this is the only
+;;; implementation, then we could avoid the indirection. A problem for another day tho. See TODO notes
+;;; on [[metabase.lib.metadata.calculation/visible-columns]]
+
+#_{:clj-kondo/ignore [:unused-private-var]} ; this is actually used
+(mu/defn- -visible-columns :- ::lib.metadata.calculation/visible-columns
+ "Implementation of [[metabase.lib.calculation/visible-columns]], which as of 8/7/25 only works on stages. Use that
+ instead of using this function directly, since it includes caching, merges default options, and does other nice
+ things for us."
[query :- ::lib.schema/query
stage-number :- :int
- _stage :- ::lib.schema/stage
{:keys [include-implicitly-joinable?], :as options} :- ::lib.metadata.calculation/visible-columns.options]
(let [existing-columns (existing-visible-columns query stage-number options)]
(into (vec existing-columns)
diff --git a/src/metabase/query_processor/middleware/add_implicit_joins.clj b/src/metabase/query_processor/middleware/add_implicit_joins.clj
index 18fb70c726f..50bd211ebdc 100644
--- a/src/metabase/query_processor/middleware/add_implicit_joins.clj
+++ b/src/metabase/query_processor/middleware/add_implicit_joins.clj
@@ -244,7 +244,6 @@
(lib/visible-columns
query
stage-number
- (lib/query-stage query stage-number)
{:include-joined? true
:include-expressions? false
:include-implicitly-joinable? false
diff --git a/src/metabase/query_processor/middleware/annotate.clj b/src/metabase/query_processor/middleware/annotate.clj
index 93b215c4c08..681d6f01e91 100644
--- a/src/metabase/query_processor/middleware/annotate.clj
+++ b/src/metabase/query_processor/middleware/annotate.clj
@@ -8,7 +8,6 @@
[metabase.lib.metadata.result-metadata :as lib.metadata.result-metadata]
[metabase.lib.schema :as lib.schema]
[metabase.lib.schema.metadata :as lib.schema.metadata]
- [metabase.lib.util :as lib.util]
[metabase.query-processor.debug :as qp.debug]
[metabase.query-processor.middleware.annotate.legacy-helper-fns]
[metabase.query-processor.reducible :as qp.reducible]
@@ -116,7 +115,7 @@
(mu/defn- needs-type-inference?
[query :- ::lib.schema/query
{initial-cols :cols, :as _initial-metadata} :- ::metadata]
- (and (= (:lib/type (lib.util/query-stage query 0)) :mbql.stage/native)
+ (and (= (:lib/type (lib/query-stage query 0)) :mbql.stage/native)
(or (empty? initial-cols)
(every? (fn [col]
(let [base-type ((some-fn :base-type :base_type) col)]
diff --git a/src/metabase/query_processor/middleware/expand_macros.clj b/src/metabase/query_processor/middleware/expand_macros.clj
index 9a0673d7dfc..084fb80a15f 100644
--- a/src/metabase/query_processor/middleware/expand_macros.clj
+++ b/src/metabase/query_processor/middleware/expand_macros.clj
@@ -60,7 +60,7 @@
definition)
:database (u/the-id (lib.metadata/database metadata-providerable))}
(lib/->query metadata-providerable)
- (lib.util/query-stage -1))
+ (lib/query-stage -1))
(log/tracef "to pMBQL\n%s" (u/pprint-to-str <>))))
(mu/defn- legacy-macro-filters :- [:maybe [:sequential ::lib.schema.expression/boolean]]
diff --git a/src/metabase/query_processor/middleware/metrics.clj b/src/metabase/query_processor/middleware/metrics.clj
index 2a48eb2d3cc..02f3ab90bfe 100644
--- a/src/metabase/query_processor/middleware/metrics.clj
+++ b/src/metabase/query_processor/middleware/metrics.clj
@@ -263,7 +263,7 @@
(if (and (= (lib.util/source-table-id query) (lib.util/source-table-id metric-query))
(or (= (lib/stage-count metric-query) 1)
(= (:qp/stage-had-source-card (last (:stages metric-query)))
- (:qp/stage-had-source-card (lib.util/query-stage query agg-stage-index)))))
+ (:qp/stage-had-source-card (lib/query-stage query agg-stage-index)))))
(let [metric-query (update-metric-query-expression-names metric-query unique-name-fn)
lookup (-> lookup
(assoc-in [metric-id :query] metric-query)
@@ -324,14 +324,14 @@
(dissoc stage :breakout :order-by :aggregation :fields :lib/stage-metadata)))
;; Needed for field references to resolve further in the pipeline
stage-query (lib/with-fields stage-query last-metric-stage-number (lib/fieldable-columns stage-query last-metric-stage-number))
- new-metric-stage (lib.util/query-stage stage-query last-metric-stage-number)
+ new-metric-stage (lib/query-stage stage-query last-metric-stage-number)
lookup {(:id metric-metadata)
{:name metric-name :aggregation metric-aggregation}}
stage-query (replace-metric-aggregation-refs
stage-query
(inc last-metric-stage-number)
lookup)
- new-following-stage (lib.util/query-stage stage-query (inc last-metric-stage-number))
+ new-following-stage (lib/query-stage stage-query (inc last-metric-stage-number))
combined-stages (vec (remove nil? (concat pre-transition-stages
[new-metric-stage new-following-stage]
following-stages)))]
diff --git a/src/metabase/query_processor/parameters/values.clj b/src/metabase/query_processor/parameters/values.clj
index 789e345d0cb..3f3b655854d 100644
--- a/src/metabase/query_processor/parameters/values.clj
+++ b/src/metabase/query_processor/parameters/values.clj
@@ -18,7 +18,6 @@
[metabase.lib.schema.metadata :as lib.schema.metadata]
[metabase.lib.schema.parameter :as lib.schema.parameter]
[metabase.lib.schema.template-tag :as lib.schema.template-tag]
- [metabase.lib.util :as lib.util]
[metabase.query-processor.compile :as qp.compile]
[metabase.query-processor.error-type :as qp.error-type]
[metabase.query-processor.middleware.limit :as limit]
@@ -443,7 +442,7 @@
([query :- ::lib.schema/query
stage-number :- :int]
- (let [{tags :template-tags, params :parameters} (lib.util/query-stage query stage-number)]
+ (let [{tags :template-tags, params :parameters} (lib/query-stage query stage-number)]
(log/tracef "Building params map out of tags\n%s\nand params\n%s\n" (u/pprint-to-str tags) (u/pprint-to-str params))
(try
(into {} (for [[k tag] tags
diff --git a/src/metabase/util/malli.cljc b/src/metabase/util/malli.cljc
index 9b75fef2205..dcee4b4e802 100644
--- a/src/metabase/util/malli.cljc
+++ b/src/metabase/util/malli.cljc
@@ -173,3 +173,24 @@
[(u/->snake_case_en k) p s]) (mc/children schema)))
schema)))))
+
+(core/defn map-schema-keys
+ "Return a set of keys specified in a map `schema`. Resolves refs in the registry and handles maps wrapped in `:and`
+ or combined with `:merge`.
+
+ (map-schema-keys :metabase.lib.metadata.calculation/visible-columns.options)
+ ;; => #{:include-joined?
+ :include-expressions?
+ :include-implicitly-joinable-for-source-card?
+ :include-implicitly-joinable?
+ :include-remaps?}"
+ [schema]
+ ;; TODO (Cam 8/7/25) -- there's probably a better way to do this but I don't know what it is.
+ (let [schema (mr/resolve-schema schema)]
+ (case (mc/type schema)
+ :map (into
+ #{}
+ (map first)
+ (mc/children schema))
+ :and (some map-schema-keys (mc/children schema))
+ nil)))
diff --git a/test/metabase/lib/card_test.cljc b/test/metabase/lib/card_test.cljc
index 583714826c5..b12b55a6abb 100644
--- a/test/metabase/lib/card_test.cljc
+++ b/test/metabase/lib/card_test.cljc
@@ -511,14 +511,11 @@
:database-id (meta/id)
:fields q1-cols}]})
card (lib.metadata/card mp 1)
- q2 (lib/query mp card)]
- (doseq [f [#'lib/returned-columns
- #'lib/visible-columns]
- :let [double-price (lib.tu.notebook/find-col-with-spec q2 (f q2 card) {} {:display-name "double-price"})]]
- (testing f
- (testing "metadata should not include :lib/expression-name"
- (is (=? {:lib/expression-name (symbol "nil #_\"key is not present.\"")}
- double-price))))))))
+ q2 (lib/query mp card)
+ double-price (lib.tu.notebook/find-col-with-spec q2 (lib/returned-columns q2 card) {} {:display-name "double-price"})]
+ (testing "metadata should not include :lib/expression-name"
+ (is (=? {:lib/expression-name (symbol "nil #_\"key is not present.\"")}
+ double-price))))))
(deftest ^:parallel card-with-join-visible-columns-test
(let [q1 (lib.tu.macros/mbql-query reviews
@@ -533,24 +530,23 @@
{:cards [{:id 1, :dataset-query q1}]})
card (lib.metadata/card mp 1)
q2 (lib/query mp card)]
- (doseq [f [#'lib/returned-columns
- #'lib/visible-columns]]
- (testing (str f " for a card should NEVER return `:metabase.lib.join/join-alias`, because the join happened within the Card itself.")
- (is (=? [{:name "CREATED_AT"
- :display-name "Created At: Month"
- :lib/card-id 1
- :lib/source :source/card
- :lib/original-join-alias "Products"
- :metabase.lib.join/join-alias (symbol "nil #_\"key is not present.\"")
- :metabase.lib.field/temporal-unit (symbol "nil #_\"key is not present.\"")
- :inherited-temporal-unit :month}
- {:name "count"
- :display-name "Distinct values of ID"
- :lib/card-id 1
- :lib/source :source/card
- :lib/original-join-alias (symbol "nil #_\"key is not present.\"")
- :metabase.lib.join/join-alias (symbol "nil #_\"key is not present.\"")}]
- (f q2 card)))))))
+ (testing (str "returned-columns for a card should NEVER return `:metabase.lib.join/join-alias`, because the join"
+ " happened within the Card itself.")
+ (is (=? [{:name "CREATED_AT"
+ :display-name "Created At: Month"
+ :lib/card-id 1
+ :lib/source :source/card
+ :lib/original-join-alias "Products"
+ :metabase.lib.join/join-alias (symbol "nil #_\"key is not present.\"")
+ :metabase.lib.field/temporal-unit (symbol "nil #_\"key is not present.\"")
+ :inherited-temporal-unit :month}
+ {:name "count"
+ :display-name "Distinct values of ID"
+ :lib/card-id 1
+ :lib/source :source/card
+ :lib/original-join-alias (symbol "nil #_\"key is not present.\"")
+ :metabase.lib.join/join-alias (symbol "nil #_\"key is not present.\"")}]
+ (lib/returned-columns q2 card))))))
(deftest ^:parallel do-not-propagate-breakout?-test
(is (=? [{:name "USER_ID"
diff --git a/test/metabase/lib/field_ref_repro_test.cljc b/test/metabase/lib/field_ref_repro_test.cljc
index 0f748fddb63..9e59cad4e32 100644
--- a/test/metabase/lib/field_ref_repro_test.cljc
+++ b/test/metabase/lib/field_ref_repro_test.cljc
@@ -9,8 +9,7 @@
[metabase.lib.test-metadata :as meta]
[metabase.lib.test-util :as lib.tu]
[metabase.lib.test-util.macros :as lib.tu.macros]
- [metabase.lib.test-util.metadata-providers.mock :as providers.mock]
- [metabase.lib.util :as lib.util]))
+ [metabase.lib.test-util.metadata-providers.mock :as providers.mock]))
(deftest ^:parallel mark-selected-columns-with-duplicate-names-test
(testing "Should be able to distinguish columns with the same name and differen join alias (#39033)"
@@ -29,9 +28,8 @@
(m/find-first (comp #{"ID"} :name)
(:result-metadata card2)))])))
stage-number -1
- stage (lib.util/query-stage query stage-number)
- vis-columns (lib.metadata.calculation/visible-columns query stage-number stage)
- ret-columns (lib.metadata.calculation/returned-columns query stage-number stage)
+ vis-columns (lib.metadata.calculation/visible-columns query stage-number)
+ ret-columns (lib.metadata.calculation/returned-columns query stage-number)
marked-columns (lib.equality/mark-selected-columns query stage-number vis-columns ret-columns)]
(is (= ["ID"
"Total"
diff --git a/test/metabase/lib/field_test.cljc b/test/metabase/lib/field_test.cljc
index 8aed15c77de..9825c5e64e3 100644
--- a/test/metabase/lib/field_test.cljc
+++ b/test/metabase/lib/field_test.cljc
@@ -1202,7 +1202,7 @@
(lib/breakout -1 (lib/with-temporal-bucket (meta/field-metadata :orders :created-at) :month))
lib/append-stage)
stage1 (lib.util/query-stage query 1)
- [created-at sum] (lib/visible-columns query 1 stage1)]
+ [created-at sum] (lib/visible-columns query 1)]
(testing "populating :fields"
(is (nil? (:fields stage1)))
(is (=? [[:field {} "CREATED_AT"]
@@ -1242,8 +1242,7 @@
(lib/remove-field native-query -1 (first native-columns)))))
(testing "with MBQL stage"
(let [query (lib/append-stage native-query)
- stage1 (lib.util/query-stage query 1)
- columns (lib/visible-columns query 1 stage1)]
+ columns (lib/visible-columns query 1)]
(testing "removing each field"
(is (=? [[:field {} "sum"]]
(-> query
diff --git a/test/metabase/lib/metadata/calculation_test.cljc b/test/metabase/lib/metadata/calculation_test.cljc
index 30f57f3fd4f..bb00fa904d3 100644
--- a/test/metabase/lib/metadata/calculation_test.cljc
+++ b/test/metabase/lib/metadata/calculation_test.cljc
@@ -116,9 +116,9 @@
visible-columns-with-desired-aliases))))))
(deftest ^:parallel visible-columns-test-2
- (testing "nil has no visible columns (#31366)"
- (is (empty? (-> (lib.tu/venues-query)
- (lib/visible-columns nil))))))
+ (testing "a nil stage-number has no visible columns (#31366)"
+ (is (= []
+ (lib/visible-columns (lib.tu/venues-query) nil)))))
(deftest ^:parallel visible-columns-test-3
(testing "Include multiple implicitly joinable columns pointing to the same table and field (##33451)"
@@ -245,7 +245,7 @@
:lib/source :source/joins})]
(testing "just own columns"
(is (=? (concat orders-cols joined-cols)
- (lib/visible-columns query -1 (lib.util/query-stage query -1) {:include-implicitly-joinable? false}))))
+ (lib/visible-columns query -1 {:include-implicitly-joinable? false}))))
(testing "with implicit joins"
(is (=? (concat orders-cols
joined-cols
@@ -255,7 +255,7 @@
;; Second set of implicit joins
(implicitly-joined :people)
(implicitly-joined :products))
- (lib/visible-columns query -1 (lib.util/query-stage query -1)))))))
+ (lib/visible-columns query -1))))))
(deftest ^:parallel implicitly-joinable-requires-numeric-id-test
(letfn [(query-with-user-id-tweaks [tweaks]
@@ -499,7 +499,7 @@
([query]
(cols query {:include-remaps? true}))
([query opts]
- (lib/visible-columns query -1 (lib.util/query-stage query -1) opts)))
+ (lib/visible-columns query -1 opts)))
orders-id (meta/id :orders)
products-id (meta/id :products)
people-id (meta/id :people)
@@ -935,8 +935,8 @@
:fields [$id
&Cat.categories.name]}
{;; THIS REF IS WRONG -- it should not be using `Cat` because the join is in the source
- ;; query rather than in the current stage. However, we should be smart enough to try to
- ;; figure out what they meant.
+ ;; query rather than in the current stage. However, we should be smart enough to try to
+ ;; figure out what they meant.
:breakout [&Cat.categories.name]}]}))]
(testing `lib/returned-columns
(testing "stage 1 of 2"
@@ -985,10 +985,10 @@
;; should not be returned by `visible-columns` since it needs to be recalculated in the context of
;; everything that gets returned.
:lib/desired-column-alias (symbol "nil #_\"key is not present.\"")}]
- (lib/visible-columns query -1 (lib/query-stage query -1) {:include-joined? false
- :include-expressions? false
- :include-implicitly-joinable? false
- :include-implicitly-joinable-for-source-card? false})))))))
+ (lib/visible-columns query -1 {:include-joined? false
+ :include-expressions? false
+ :include-implicitly-joinable? false
+ :include-implicitly-joinable-for-source-card? false})))))))
(deftest ^:parallel join-source-query-join-test
(testing "lib/returned-columns calculates incorrect :source-column-aliases for columns coming from nested joins (QUE-1373)"
diff --git a/test/metabase/lib/metric_test.cljc b/test/metabase/lib/metric_test.cljc
index 62bfee40dbf..6d6da002533 100644
--- a/test/metabase/lib/metric_test.cljc
+++ b/test/metabase/lib/metric_test.cljc
@@ -8,8 +8,7 @@
[metabase.lib.metric :as lib.metric]
[metabase.lib.test-metadata :as meta]
[metabase.lib.test-util :as lib.tu]
- [metabase.lib.test-util.metadata-providers.mock :as providers.mock]
- [metabase.lib.util :as lib.util]))
+ [metabase.lib.test-util.metadata-providers.mock :as providers.mock]))
#?(:cljs (comment metabase.test-runner.assert-exprs.approximately-equal/keep-me))
@@ -199,8 +198,8 @@
:aggregation [[:metric {} 1]]}]}
metric-based-query)))
(testing "The columns of the query underlying the metric are visible in the metric-based query"
- (is (= (lib/visible-columns metric-card-query 0 (lib.util/query-stage metric-card-query 0))
- (lib/visible-columns metric-based-query 0 (lib.util/query-stage metric-based-query 0)))))))
+ (is (= (lib/visible-columns metric-card-query 0)
+ (lib/visible-columns metric-based-query 0))))))
(deftest ^:parallel available-metrics-test
(let [expected-metric-metadata {:lib/type :metadata/metric
diff --git a/test/metabase/query_processor_test/field_ref_repro_test.clj b/test/metabase/query_processor_test/field_ref_repro_test.clj
index bc68ca18ab4..e70fba29332 100644
--- a/test/metabase/query_processor_test/field_ref_repro_test.clj
+++ b/test/metabase/query_processor_test/field_ref_repro_test.clj
@@ -11,7 +11,6 @@
[metabase.lib.metadata :as lib.metadata]
[metabase.lib.metadata.calculation :as lib.metadata.calculation]
[metabase.lib.test-util :as lib.tu]
- [metabase.lib.util :as lib.util]
[metabase.query-processor :as qp]
[metabase.query-processor.preprocess :as qp.preprocess]
[metabase.query-processor.store :as qp.store]
@@ -79,9 +78,8 @@
(lib/join (lib/join-clause card-meta
[(lib/= (lib.metadata/field mp (mt/id :reviews :id))
id-col)])))
- stage (lib.util/query-stage query -1)
- visible (lib.metadata.calculation/visible-columns query -1 stage)
- returned (lib.metadata.calculation/returned-columns query -1 stage)
+ visible (lib.metadata.calculation/visible-columns query -1)
+ returned (lib.metadata.calculation/returned-columns query -1)
marked (lib.equality/mark-selected-columns query -1 visible returned)]
(is (=? [{:name "ID", :display-name "ID"}
{:name "PRODUCT_ID", :display-name "Product ID"}
@@ -143,9 +141,8 @@
(lib/join (lib/join-clause card-meta
[(lib/= (lib.metadata/field mp (mt/id :orders :id))
count-col)])))
- stage (lib.util/query-stage query -1)
- visible (lib.metadata.calculation/visible-columns query -1 stage)
- returned (lib.metadata.calculation/returned-columns query -1 stage)
+ visible (lib.metadata.calculation/visible-columns query -1)
+ returned (lib.metadata.calculation/returned-columns query -1)
marked (lib.equality/mark-selected-columns query -1 visible returned)]
(is (=? [{:name "ID", :display-name "ID"}
{:name "USER_ID", :display-name "User ID"}
@@ -277,9 +274,8 @@
:fields :all}]})])
card-meta (lib.metadata/card mp 1)
query (lib/query mp card-meta)
- stage (lib.util/query-stage query -1)
- visible (lib.metadata.calculation/visible-columns query -1 stage)
- returned (lib.metadata.calculation/returned-columns query -1 stage)
+ visible (lib.metadata.calculation/visible-columns query -1)
+ returned (lib.metadata.calculation/returned-columns query -1)
marked (lib.equality/mark-selected-columns query -1 visible returned)]
(is (=? [{:name "ID", :display-name "ID", :selected? true}
{:name "USER_ID", :display-name "User ID", :selected? true}