Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MLv2: Simplify MetadataProvider protocols (16 methods/3 protocols => 7 methods/2 protocols) #42070

Merged
merged 18 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
[metabase.driver.common.parameters.values :as params.values]
[metabase.driver.mongo.query-processor :as mongo.qp]
[metabase.legacy-mbql.util :as mbql.u]
[metabase.lib.metadata :as lib.metadata]
[metabase.lib.schema.metadata :as lib.schema.metadata]
[metabase.query-processor.error-type :as qp.error-type]
[metabase.query-processor.middleware.wrap-value-literals :as qp.wrap-value-literals]
[metabase.util :as u]
Expand Down Expand Up @@ -69,7 +69,7 @@
([field]
(field->name field true))

([field :- lib.metadata/ColumnMetadata
([field :- ::lib.schema.metadata/column
pr?]
;; for native parameters we serialize and don't need the extra pr
(cond-> (mongo.qp/field->name field ".")
Expand Down Expand Up @@ -111,7 +111,7 @@

(mu/defn ^:private substitute-field-filter
[{field :field, {:keys [value]} :value, :as field-filter} :- [:map
[:field lib.metadata/ColumnMetadata]
[:field ::lib.schema.metadata/column]
[:value [:map [:value :any]]]]]
(if (sequential? value)
(format "{%s: %s}" (field->name field) (param-value->str field value))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
[metabase.legacy-mbql.util :as mbql.u]
[metabase.lib.metadata :as lib.metadata]
[metabase.lib.schema.common :as lib.schema.common]
[metabase.lib.schema.metadata :as lib.schema.metadata]
[metabase.lib.util.match :as lib.util.match]
[metabase.public-settings :as public-settings]
[metabase.query-processor.error-type :as qp.error-type]
Expand Down Expand Up @@ -173,12 +174,12 @@
([field]
(field->name field \.))

([field :- lib.metadata/ColumnMetadata
([field :- ::lib.schema.metadata/column
separator :- [:or :string char?]]
(str/join separator (field-name-components field))))

(mu/defmethod add/field-reference-mlv2 :mongo
[_driver field-inst :- lib.metadata/ColumnMetadata]
[_driver field-inst :- ::lib.schema.metadata/column]
(field->name field-inst))

(defmacro ^:private mongo-let
Expand Down
6 changes: 3 additions & 3 deletions src/metabase/driver/sql/parameters/substitution.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
[metabase.driver.sql.query-processor :as sql.qp]
[metabase.legacy-mbql.schema :as mbql.s]
[metabase.legacy-mbql.util :as mbql.u]
[metabase.lib.metadata :as lib.metadata]
[metabase.lib.schema.common :as lib.schema.common]
[metabase.lib.schema.metadata :as lib.schema.metadata]
[metabase.lib.schema.parameter :as lib.schema.parameter]
[metabase.query-processor.error-type :as qp.error-type]
[metabase.query-processor.middleware.wrap-value-literals :as qp.wrap-value-literals]
Expand Down Expand Up @@ -254,7 +254,7 @@

(mu/defn ^:private field->clause :- mbql.s/field
[driver :- :keyword
field :- lib.metadata/ColumnMetadata
field :- ::lib.schema.metadata/column
param-type :- ::lib.schema.parameter/type
value]
;; The [[metabase.query-processor.middleware.parameters/substitute-parameters]] QP middleware actually happens before
Expand Down Expand Up @@ -323,7 +323,7 @@
(mu/defmethod ->replacement-snippet-info [:sql FieldFilter]
[driver :- :keyword
{:keys [value], :as field-filter} :- [:map
[:field lib.metadata/ColumnMetadata]
[:field ::lib.schema.metadata/column]
[:value :any]]]
(cond
;; otherwise if the value isn't present just put in something that will always be true, such as `1` (e.g. `WHERE 1
Expand Down
12 changes: 6 additions & 6 deletions src/metabase/lib/aggregation.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

(mu/defn column-metadata->aggregation-ref :- :mbql.clause/aggregation
"Given `:metadata/column` column metadata for an aggregation, construct an `:aggregation` reference."
[metadata :- lib.metadata/ColumnMetadata]
[metadata :- ::lib.schema.metadata/column]
(let [options {:lib/uuid (str (random-uuid))
:effective-type ((some-fn :effective-type :base-type) metadata)
:lib/source-name (:name metadata)}
Expand Down Expand Up @@ -245,7 +245,7 @@
[:or
::lib.schema.aggregation/aggregation
::lib.schema.common/external-op
lib.metadata/LegacyMetricMetadata])
::lib.schema.metadata/legacy-metric])

(mu/defn aggregate :- ::lib.schema/query
"Adds an aggregation to query."
Expand All @@ -269,7 +269,7 @@
stage-number :- :int]
(not-empty (:aggregation (lib.util/query-stage query stage-number)))))

(mu/defn aggregations-metadata :- [:maybe [:sequential lib.metadata/ColumnMetadata]]
(mu/defn aggregations-metadata :- [:maybe [:sequential ::lib.schema.metadata/column]]
"Get metadata about the aggregations in a given stage of a query."
([query]
(aggregations-metadata query -1))
Expand All @@ -288,7 +288,7 @@
[:merge
::lib.schema.aggregation/operator
[:map
[:columns {:optional true} [:sequential lib.metadata/ColumnMetadata]]]])
[:columns {:optional true} [:sequential ::lib.schema.metadata/column]]]])

(defmethod lib.metadata.calculation/display-name-method :operator/aggregation
[_query _stage-number {:keys [display-info]} _display-name-style]
Expand All @@ -301,7 +301,7 @@
:requires-column requires-column?)
(some? selected?) (assoc :selected selected?)))

(mu/defn aggregation-operator-columns :- [:maybe [:sequential lib.metadata/ColumnMetadata]]
(mu/defn aggregation-operator-columns :- [:maybe [:sequential ::lib.schema.metadata/column]]
"Returns the columns for which `aggregation-operator` is applicable."
[aggregation-operator :- OperatorWithColumns]
(:columns aggregation-operator))
Expand Down Expand Up @@ -359,7 +359,7 @@
[:merge
::lib.schema.aggregation/operator
[:map
[:columns {:optional true} [:sequential lib.metadata/ColumnMetadata]]
[:columns {:optional true} [:sequential ::lib.schema.metadata/column]]
[:selected? {:optional true} :boolean]]])

(mu/defn selected-aggregation-operators :- [:maybe [:sequential SelectedOperatorWithColumns]]
Expand Down
8 changes: 4 additions & 4 deletions src/metabase/lib/card.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
(fallback-display-name source-card)))))

(mu/defn ^:private infer-returned-columns :- [:maybe [:sequential ::lib.schema.metadata/column]]
[metadata-providerable :- lib.metadata/MetadataProviderable
[metadata-providerable :- ::lib.schema.metadata/metadata-providerable
card-query :- :map]
(when (some? card-query)
(lib.metadata.calculation/returned-columns (lib.query/query metadata-providerable card-query))))
Expand All @@ -68,7 +68,7 @@
([metadata-providerable col]
(->card-metadata-column metadata-providerable nil col))

([metadata-providerable :- lib.metadata/MetadataProviderable
([metadata-providerable :- ::lib.schema.metadata/metadata-providerable
card-or-id :- [:maybe [:or ::lib.schema.id/card ::lib.schema.metadata/card]]
col :- :map]
(let [col (-> col
Expand Down Expand Up @@ -119,7 +119,7 @@

(mu/defn card-metadata-columns :- CardColumns
"Get a normalized version of the saved metadata associated with Card metadata."
[metadata-providerable :- lib.metadata/MetadataProviderable
[metadata-providerable :- ::lib.schema.metadata/metadata-providerable
card :- Card]
(when-not (contains? *card-metadata-columns-card-ids* (:id card))
(binding [*card-metadata-columns-card-ids* (conj *card-metadata-columns-card-ids* (:id card))]
Expand All @@ -136,7 +136,7 @@

(mu/defn saved-question-metadata :- CardColumns
"Metadata associated with a Saved Question with `card-id`."
[metadata-providerable :- lib.metadata/MetadataProviderable
[metadata-providerable :- ::lib.schema.metadata/metadata-providerable
card-id :- ::lib.schema.id/card]
;; it seems like in some cases (unit tests) the FE is renaming `:result-metadata` to `:fields`, not 100% sure why
;; but handle that case anyway. (#29739)
Expand Down
9 changes: 5 additions & 4 deletions src/metabase/lib/column_group.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
[metabase.lib.metadata.calculation :as lib.metadata.calculation]
[metabase.lib.schema.common :as lib.schema.common]
[metabase.lib.schema.id :as lib.schema.id]
[metabase.lib.schema.metadata :as lib.schema.metadata]
[metabase.lib.util :as lib.util]
[metabase.util.malli :as mu]))

Expand All @@ -31,7 +32,7 @@
[:map
[:lib/type [:= :metadata/column-group]]
[::group-type GroupType]
[::columns [:sequential lib.metadata/ColumnMetadata]]]
[::columns [:sequential [:ref ::lib.schema.metadata/column]]]]
[:multi
{:dispatch ::group-type}
[:group-type/main
Expand Down Expand Up @@ -153,7 +154,7 @@

(mu/defn ^:private column-group-info :- [:map [::group-type GroupType]]
"The value we should use to `group-by` inside [[group-columns]]."
[column-metadata :- lib.metadata/ColumnMetadata]
[column-metadata :- ::lib.schema.metadata/column]
(column-group-info-method column-metadata))

(defn- column-group-ordering
Expand Down Expand Up @@ -191,7 +192,7 @@
Ordered to put own columns first, then explicit joins alphabetically by join alias, then implicit joins alphabetically
by FK join alias + FK field name (which is used as the table name). So if the same FK is available multiple times,
they are ordered: own first, then alphabetically by the join alias for that FK."
[column-metadatas :- [:sequential lib.metadata/ColumnMetadata]]
[column-metadatas :- [:sequential ::lib.schema.metadata/column]]
(let [fk-field-names (into {} (comp (filter :id)
(map (juxt :id :name)))
column-metadatas)]
Expand All @@ -203,7 +204,7 @@
(sort-by (partial column-group-ordering fk-field-names))
vec)))

(mu/defn columns-group-columns :- [:sequential lib.metadata/ColumnMetadata]
(mu/defn columns-group-columns :- [:sequential ::lib.schema.metadata/column]
"Get the columns associated with a column group"
[column-group :- ColumnGroup]
(::columns column-group))
Expand Down
9 changes: 4 additions & 5 deletions src/metabase/lib/drill_thru/pivot.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,26 @@
- `pivotTypes` function that return available column types for the drill - \"category\" | \"location\" | \"time\"

- `pivotColumnsForType` returns the list of available columns for the drill and the selected type"

(:require
[metabase.lib.aggregation :as lib.aggregation]
[metabase.lib.breakout :as lib.breakout]
[metabase.lib.drill-thru.common :as lib.drill-thru.common]
[metabase.lib.filter :as lib.filter]
[metabase.lib.metadata :as lib.metadata]
[metabase.lib.metadata.calculation :as lib.metadata.calculation]
[metabase.lib.schema :as lib.schema]
[metabase.lib.schema.drill-thru :as lib.schema.drill-thru]
[metabase.lib.schema.metadata :as lib.schema.metadata]
[metabase.lib.types.isa :as lib.types.isa]
[metabase.util.malli :as mu]))

(mu/defn ^:private pivot-drill-pred :- [:sequential lib.metadata/ColumnMetadata]
(mu/defn ^:private pivot-drill-pred :- [:sequential ::lib.schema.metadata/column]
"Implementation for pivoting on various kinds of fields.

Don't call this directly; call [[pivot-drill]]."
[query :- ::lib.schema/query
stage-number :- :int
{:keys [column value]} :- ::lib.schema.drill-thru/context
field-pred :- [:=> [:cat lib.metadata/ColumnMetadata] boolean?]]
field-pred :- [:=> [:cat [:schema ::lib.schema.metadata/column]] boolean?]]
(when (and (lib.drill-thru.common/mbql-stage? query stage-number)
column
(some? value)
Expand Down Expand Up @@ -154,7 +153,7 @@
[:map [:type [:= :drill-thru/pivot]]]]]
(-> drill-thru :pivots keys sort))

(mu/defn pivot-columns-for-type :- [:sequential lib.metadata/ColumnMetadata]
(mu/defn pivot-columns-for-type :- [:sequential ::lib.schema.metadata/column]
"A helper for the FE. Returns all the columns of the given type which can be used to pivot the query."
[drill-thru :- [:and ::lib.schema.drill-thru/drill-thru
[:map [:type [:= :drill-thru/pivot]]]]
Expand Down
4 changes: 2 additions & 2 deletions src/metabase/lib/drill_thru/quick_filter.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,21 @@
[metabase.lib.drill-thru.column-filter :as lib.drill-thru.column-filter]
[metabase.lib.drill-thru.common :as lib.drill-thru.common]
[metabase.lib.filter :as lib.filter]
[metabase.lib.metadata :as lib.metadata]
[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.drill-thru :as lib.schema.drill-thru]
[metabase.lib.schema.expression :as lib.schema.expression]
[metabase.lib.schema.metadata :as lib.schema.metadata]
[metabase.lib.types.isa :as lib.types.isa]
[metabase.util.malli :as mu]))

(defn- operator [op & args]
(lib.options/ensure-uuid (into [op {}] args)))

(mu/defn ^:private operators-for :- [:sequential ::lib.schema.drill-thru/drill-thru.quick-filter.operator]
[column :- lib.metadata/ColumnMetadata
[column :- ::lib.schema.metadata/column
value]
(let [field-ref (lib.ref/ref column)]
(cond
Expand Down
7 changes: 3 additions & 4 deletions src/metabase/lib/drill_thru/zoom_in_timeseries.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,12 @@
[metabase.lib.drill-thru.common :as lib.drill-thru.common]
[metabase.lib.equality :as lib.equality]
[metabase.lib.filter :as lib.filter]
[metabase.lib.metadata :as lib.metadata]
[metabase.lib.remove-replace :as lib.remove-replace]
[metabase.lib.schema :as lib.schema]
[metabase.lib.schema.common :as lib.schema.common]
[metabase.lib.schema.drill-thru :as lib.schema.drill-thru]
[metabase.lib.schema.temporal-bucketing
:as lib.schema.temporal-bucketing]
[metabase.lib.schema.metadata :as lib.schema.metadata]
[metabase.lib.schema.temporal-bucketing :as lib.schema.temporal-bucketing]
[metabase.lib.temporal-bucket :as lib.temporal-bucket]
[metabase.lib.util :as lib.util]
[metabase.shared.util.i18n :as i18n]
Expand Down Expand Up @@ -71,7 +70,7 @@
(assoc dimension :column-ref breakout))))

(mu/defn ^:private next-breakout-unit :- [:maybe ::lib.schema.temporal-bucketing/unit.date-time.truncate]
[column :- lib.metadata/ColumnMetadata]
[column :- ::lib.schema.metadata/column]
(when-let [current-unit (lib.temporal-bucket/raw-temporal-bucket column)]
(when (contains? (set valid-current-units) current-unit)
(unit->next-unit current-unit))))
Expand Down
14 changes: 6 additions & 8 deletions src/metabase/lib/expression.cljc
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
(ns metabase.lib.expression
(:refer-clojure
:exclude
[+ - * / case coalesce abs time concat replace])
(:refer-clojure :exclude [+ - * / case coalesce abs time concat replace])
(:require
[clojure.string :as str]
[malli.core :as mc]
[malli.error :as me]
[medley.core :as m]
[metabase.lib.common :as lib.common]
[metabase.lib.hierarchy :as lib.hierarchy]
[metabase.lib.metadata :as lib.metadata]
[metabase.lib.metadata.calculation :as lib.metadata.calculation]
[metabase.lib.options :as lib.options]
[metabase.lib.ref :as lib.ref]
[metabase.lib.schema :as lib.schema]
[metabase.lib.schema.aggregation :as lib.schema.aggregation]
[metabase.lib.schema.common :as lib.schema.common]
[metabase.lib.schema.expression :as lib.schema.expression]
[metabase.lib.schema.metadata :as lib.schema.metadata]
[metabase.lib.schema.temporal-bucketing :as lib.schema.temporal-bucketing]
[metabase.lib.temporal-bucket :as lib.temporal-bucket]
[metabase.lib.util :as lib.util]
Expand All @@ -28,7 +26,7 @@

(mu/defn column-metadata->expression-ref :- :mbql.clause/expression
"Given `:metadata/column` column metadata for an expression, construct an `:expression` reference."
[metadata :- lib.metadata/ColumnMetadata]
[metadata :- ::lib.schema.metadata/column]
(let [options {:lib/uuid (str (random-uuid))
:base-type (:base-type metadata)
:effective-type ((some-fn :effective-type :base-type) metadata)}]
Expand Down Expand Up @@ -293,7 +291,7 @@
(lib.common/defop day-name [n])
(lib.common/defop offset [x n])

(mu/defn ^:private expression-metadata :- lib.metadata/ColumnMetadata
(mu/defn ^:private expression-metadata :- ::lib.schema.metadata/column
[query :- ::lib.schema/query
stage-number :- :int
expression-definition :- ::lib.schema.expression/expression]
Expand All @@ -303,7 +301,7 @@
:name expression-name
:display-name expression-name))))

(mu/defn expressions-metadata :- [:maybe [:sequential lib.metadata/ColumnMetadata]]
(mu/defn expressions-metadata :- [:maybe [:sequential ::lib.schema.metadata/column]]
"Get metadata about the expressions in a given stage of a `query`."
([query]
(expressions-metadata query -1))
Expand All @@ -326,7 +324,7 @@
[expression-clause]
expression-clause)

(mu/defn expressionable-columns :- [:sequential lib.metadata/ColumnMetadata]
(mu/defn expressionable-columns :- [:sequential ::lib.schema.metadata/column]
"Get column metadata for all the columns that can be used expressions in
the stage number `stage-number` of the query `query` and in expression index `expression-position`
If `stage-number` is omitted, the last stage is used.
Expand Down
4 changes: 2 additions & 2 deletions src/metabase/lib/filter/operator.cljc
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
(ns metabase.lib.filter.operator
(:require
[metabase.lib.metadata :as lib.metadata]
[metabase.lib.metadata.calculation :as lib.metadata.calculation]
[metabase.lib.schema.common :as lib.schema.common]
[metabase.lib.schema.filter :as lib.schema.filter]
[metabase.lib.schema.metadata :as lib.schema.metadata]
[metabase.lib.types.isa :as lib.types.isa]
[metabase.shared.util.i18n :as i18n]
[metabase.util :as u]
Expand Down Expand Up @@ -121,7 +121,7 @@
"The list of available filter operators.
The order of operators is relevant for the front end.
There are slight differences between names and ordering for the different base types."
[column :- lib.metadata/ColumnMetadata]
[column :- ::lib.schema.metadata/column]
;; The order of these clauses is important since we want to match the most relevant type
;; the order is different than `lib.types.isa/field-type` as filters need to operate
;; on the effective-type rather than the semantic-type, eg boolean and number cannot become
Expand Down
4 changes: 2 additions & 2 deletions src/metabase/lib/join.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
(= (lib.dispatch/dispatch-value x) :mbql/join))

(def ^:private Joinable
[:or lib.metadata/TableMetadata ::lib.schema.metadata/card])
[:or ::lib.schema.metadata/table ::lib.schema.metadata/card])

(def ^:private JoinOrJoinable
[:or
Expand Down Expand Up @@ -585,7 +585,7 @@

(mu/defn joined-thing :- [:maybe Joinable]
"Return metadata about the origin of `a-join` using `metadata-providerable` as the source of information."
[metadata-providerable :- lib.metadata/MetadataProviderable
[metadata-providerable :- ::lib.schema.metadata/metadata-providerable
a-join :- lib.join.util/PartialJoin]
(let [origin (-> a-join :stages first)]
(cond
Expand Down