Skip to content

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

[match] Use metabase.util.match macros directly instead of driver-api aliases (#73987)

Oleksandr Yakushevf5d44c019ca3
diff --git a/.clj-kondo/config.edn b/.clj-kondo/config.edn
index 89e61c51aaa..687a11b5207 100644
--- a/.clj-kondo/config.edn
+++ b/.clj-kondo/config.edn
@@ -269,7 +269,6 @@
(metabase.util.match/match-many)
(metabase.util.match/replace-in)
(metabase.util.match/replace)
- (metabase.driver-api.core/match)
(metabase.driver-api.core/match-one)
(metabase.driver-api.core/match-many)
(metabase.driver-api.core/replace)
diff --git a/modules/drivers/druid/src/metabase/driver/druid/query_processor.clj b/modules/drivers/druid/src/metabase/driver/druid/query_processor.clj
index b3fbfb41ef8..28b294c2795 100644
--- a/modules/drivers/druid/src/metabase/driver/druid/query_processor.clj
+++ b/modules/drivers/druid/src/metabase/driver/druid/query_processor.clj
@@ -11,6 +11,7 @@
[metabase.util.i18n :refer [tru]]
[metabase.util.log :as log]
[metabase.util.malli :as mu]
+ [metabase.util.match :as match]
[metabase.util.performance :refer [every? get-in mapv some]]))
(set! *warn-on-reflection* true)
@@ -306,7 +307,7 @@
(defn- parse-filter [filter-clause]
;; strip out all the filters against temporal fields. Those are handled separately, as intervals
- (-> (driver-api/replace filter-clause
+ (-> (match/replace filter-clause
[_ [:field _ {:temporal-unit &truthy}] & _]
nil)
lib/->mbql5
@@ -328,7 +329,7 @@
"Adding `n` `:default` units doesn't make sense. So if an `:absoulte-datetime` has `:default` as its unit, add `n`
milliseconds, because that is the smallest unit Druid supports."
[clause n]
- (driver-api/replace clause
+ (match/replace clause
[:absolute-datetime t :default]
[:absolute-datetime (u.date/add t :millisecond n) :millisecond]
@@ -336,7 +337,7 @@
(add-datetime-units* clause n)))
(defn- ->absolute-timestamp ^java.time.temporal.Temporal [clause]
- (driver-api/match-one clause
+ (match/match-one clause
[:absolute-datetime t :default]
t
@@ -356,7 +357,7 @@
clauses, the methods are skipped entirely."
{:arglists '([filter-clause])}
(fn [filter-clause]
- (when (driver-api/match-one filter-clause [:field _ {:temporal-unit &truthy}] true)
+ (when (match/match-one filter-clause [:field _ {:temporal-unit &truthy}] true)
(driver-api/dispatch-by-clause-name-or-class filter-clause))))
(defmethod filter-clause->intervals :default
@@ -631,7 +632,7 @@
(defn- create-aggregation-clause
[output-name ag-type ag-field args]
(let [output-name-kwd (keyword output-name)]
- (driver-api/match-one [ag-type ag-field]
+ (match/match-one [ag-type ag-field]
;; For 'distinct values' queries (queries with a breakout by no aggregation) just aggregate by count, but name
;; it :___count so it gets discarded automatically
[nil nil] [[(or output-name-kwd :___count)] {:aggregations [(ag:count (or output-name :___count))]}]
@@ -688,7 +689,7 @@
ag-clause :- driver-api/mbql.schema.Aggregation
druid-query]
(let [output-name (*query-unique-name-fn* (driver-api/aggregation-name *query* ag-clause))
- [ag-type ag-field & args] (driver-api/match-one ag-clause
+ [ag-type ag-field & args] (match/match-one ag-clause
[:aggregation-options ag & _] (&recur ag)
_ &match)]
(if-not (isa? query-type ::ag-query)
@@ -708,7 +709,7 @@
(update :query (partial merge-with concat) ag-clauses))))))
(defn- deduplicate-aggregation-options [expression]
- (driver-api/replace expression
+ (match/replace expression
[:aggregation-options [:aggregation-options ag options-1] options-2]
[:aggregation-options ag (merge options-1 options-2)]))
@@ -717,7 +718,7 @@
(defn- add-expression-aggregation-output-names
[expression]
- (driver-api/replace expression
+ (match/replace expression
[:aggregation-options ag options]
(deduplicate-aggregation-options [:aggregation-options (add-expression-aggregation-output-names ag) options])
@@ -735,7 +736,7 @@
(defn- expression-post-aggregation
[[operator & args, :as expression]]
- (driver-api/match-one expression
+ (match/match-one expression
;; If it's a named expression, we want to preserve the included name, so recurse, but merge in the name
[:aggregation-options ag _]
(assoc (expression-post-aggregation ag) :name (driver-api/aggregation-name *query* expression))
@@ -745,7 +746,7 @@
:name (driver-api/aggregation-name *query* expression)
:fn operator
:fields (vec (for [arg args]
- (driver-api/match-one arg
+ (match/match-one arg
(_ :guard number?)
{:type :constant, :name (str &match), :value &match}
@@ -801,7 +802,7 @@
(*query-unique-name-fn* (name projection)))
(reduce
(fn [druid-query aggregation]
- (driver-api/match-one aggregation
+ (match/match-one aggregation
[:aggregation-options [#{:+ :- :/ :*} & _] _]
(handle-expression-aggregation query-type &match druid-query)
@@ -959,7 +960,7 @@
(defn- field-clause->name
[field-clause]
- (driver-api/match-one field-clause
+ (match/match-one field-clause
[:field (id :guard integer?) _]
(:name (driver-api/field (driver-api/metadata-provider) id))
@@ -1007,14 +1008,14 @@
(let [field (->rvalue field)
breakout-field (->rvalue breakout-field)
sort-by-breakout? (= field breakout-field)
- ag-field (driver-api/match-one ag
+ ag-field (match/match-one ag
[:distinct & _] :distinct___count
[:aggregation-options _ {:name name}] name
[:aggregation-options wrapped-ag _] (&recur wrapped-ag)
[(ag-type :guard keyword?) & _] ag-type)]
(when-not sort-by-breakout?
(assert ag-field))
- (assoc-in druid-query [:query :metric] (driver-api/match-one [sort-by-breakout? direction]
+ (assoc-in druid-query [:query :metric] (match/match-one [sort-by-breakout? direction]
[true :asc] {:type :alphaNumeric}
[true :desc] {:type :inverted, :metric {:type :alphaNumeric}}
[false :asc] {:type :inverted, :metric ag-field}
@@ -1032,7 +1033,7 @@
datetime"
[field]
(when field
- (driver-api/match-one field
+ (match/match-one field
[:field _id-or-name {:temporal-unit &truthy}]
true
@@ -1171,12 +1172,12 @@
ts? (boolean
(and
;; Checks whether the query is a timeseries
- (driver-api/match-one (first breakout-fields) [:field _ {:temporal-unit &truthy}] true)
+ (match/match-one (first breakout-fields) [:field _ {:temporal-unit &truthy}] true)
;; (excludes x-of-y type breakouts)
(contains? timeseries-units (:unit (first breakout-fields)))
;; (excludes queries with LIMIT)
(nil? limit)))]
- (driver-api/match-one [breakouts agg? ts?]
+ (match/match-one [breakouts agg? ts?]
[:none false _] ::scan
[:none true _] ::total
[:one _ true] ::grouped-timeseries
diff --git a/modules/drivers/mongo/src/metabase/driver/mongo/query_processor.clj b/modules/drivers/mongo/src/metabase/driver/mongo/query_processor.clj
index 0112b6f6e87..0335b008280 100644
--- a/modules/drivers/mongo/src/metabase/driver/mongo/query_processor.clj
+++ b/modules/drivers/mongo/src/metabase/driver/mongo/query_processor.clj
@@ -29,6 +29,7 @@
[metabase.util.i18n :refer [tru]]
[metabase.util.log :as log]
[metabase.util.malli :as mu]
+ [metabase.util.match :as match]
[metabase.util.performance :as perf :refer [some mapv select-keys empty?]])
(:import
(org.bson BsonBinarySubType)
@@ -1055,7 +1056,7 @@ function(bin) {
"Rename :join-alias properties fields to ::join-local.
See [[find-mapped-field-name]] for an explanation why this is done."
[expr alias]
- (driver-api/replace expr
+ (match/replace expr
[:field _ {:join-alias (a :guard (= a alias))}]
(update &match 2 set/rename-keys {:join-alias ::join-local})))
@@ -1086,7 +1087,7 @@ function(bin) {
source-field-mappings (get-field-mappings source-query projections)
;; Find the fields the join condition refers to that are not coming from the joined query.
;; These have to be bound in the :let property of the $lookup stage, they cannot be referred to directly.
- own-fields (driver-api/match-many condition
+ own-fields (match/match-many condition
[:field _ (opts :guard (not= (:join-alias opts) alias))] &match)
;; Map the own fields to a fresh alias and to its rvalue.
mapping (map (fn [f] (let [alias (-> (format "let_%s_" (->lvalue f))
@@ -1132,7 +1133,7 @@ function(bin) {
:default (->rvalue (:default options))}})
(defn- aggregation->rvalue [ag]
- (driver-api/match-one ag
+ (match/match-one ag
[:aggregation-options ag' _]
(&recur ag')
@@ -1470,7 +1471,7 @@ function(bin) {
(fn [m field-clause]
(assoc-in
m
- (driver-api/match-one field-clause
+ (match/match-one field-clause
[:field (field-id :guard integer?) _]
(str/split (field-alias field-clause) #"\.")
@@ -1679,7 +1680,7 @@ function(bin) {
(defn- query->collection-name
"Return `:collection` from a source query, if it exists."
[query]
- (driver-api/match-one query
+ (match/match-one query
{:collection (collection :guard identity)}
;; ignore source queries inside `:joins` or `:collection` outside of a `:source-query`
(when (and (some #{:source-query} &parents)
@@ -1777,7 +1778,7 @@ function(bin) {
source-alias)
[:field source-alias opts]
[:field id-or-name opts])))]
- (driver-api/replace form
+ (match/replace form
[:field & _]
(update-field-ref &match)
diff --git a/modules/drivers/redshift/src/metabase/driver/redshift.clj b/modules/drivers/redshift/src/metabase/driver/redshift.clj
index 80ede003803..c294ef2dc37 100644
--- a/modules/drivers/redshift/src/metabase/driver/redshift.clj
+++ b/modules/drivers/redshift/src/metabase/driver/redshift.clj
@@ -25,6 +25,7 @@
[metabase.util.honey-sql-2 :as h2x]
[metabase.util.json :as json]
[metabase.util.log :as log]
+ [metabase.util.match :as match]
[metabase.util.performance :as perf])
(:import
(com.amazon.redshift.util RedshiftInterval)
@@ -556,7 +557,7 @@
(keep (fn [param]
(if (contains? param :name)
[(:name param) (:value param)]
- (when-let [field-id (driver-api/match-one param
+ (when-let [field-id (match/match-one param
[:field (field-id :guard integer?) _]
(when (perf/some #{:dimension} &parents)
field-id))]
diff --git a/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj b/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj
index 795e18e6c93..28d2bfebe99 100644
--- a/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj
+++ b/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj
@@ -33,6 +33,7 @@
[metabase.util.json :as json]
[metabase.util.log :as log]
[metabase.util.malli :as mu]
+ [metabase.util.match :as match]
[metabase.util.memoize :as memoize]
[metabase.util.performance :as perf :refer [mapv get-in]]
[next.jdbc :as next.jdbc])
@@ -275,7 +276,7 @@
it casts to `:datetime2`."
[base-expr day-expr]
(if (or (= (:base-type *field-options*) :type/Date)
- (driver-api/match-one base-expr [::h2x/typed _ {:database-type #{:date "date"}}] true))
+ (match/match-one base-expr [::h2x/typed _ {:database-type #{:date "date"}}] true))
day-expr
(h2x/cast :datetime2 day-expr)))
@@ -905,7 +906,7 @@
(and (has-order-by-without-limit? m)
(not (in-join-source-query? path))
(in-source-query? path)))]
- (driver-api/replace inner-query
+ (match/replace inner-query
;; remove order by and then recurse in case we need to do more transformations at another level
(m :guard (remove-order-by? &parents m))
(fix-order-bys (dissoc m :order-by))
diff --git a/src/metabase/driver/sql/query_processor.clj b/src/metabase/driver/sql/query_processor.clj
index 3dc25275917..3efe987dc7c 100644
--- a/src/metabase/driver/sql/query_processor.clj
+++ b/src/metabase/driver/sql/query_processor.clj
@@ -19,6 +19,7 @@
[metabase.util.i18n :refer [tru]]
[metabase.util.log :as log]
[metabase.util.malli :as mu]
+ [metabase.util.match :as match]
[metabase.util.performance :as perf :refer [empty? every? mapv not-empty select-keys some]]
[toucan2.pipeline :as t2.pipeline])
(:import
@@ -1416,7 +1417,7 @@
;; aggregation REFERENCE e.g. the ["aggregation" 0] fields we allow in order-by
(defmethod ->honeysql [:sql :aggregation]
[driver [_ index]]
- (driver-api/match-one (nth (:aggregation *inner-query*) index)
+ (match/match-one (nth (:aggregation *inner-query*) index)
[:aggregation-options ag {driver-api/qp.add.desired-alias desired-alias}]
(->honeysql driver (h2x/identifier :field-alias desired-alias))
@@ -1621,7 +1622,7 @@
([form]
(rewrite-fields-to-force-using-column-aliases form {:is-breakout false}))
([form {is-breakout :is-breakout}]
- (driver-api/replace form
+ (match/replace form
[:field (opts :guard :lib/uuid) id-or-name] ;; mbql5
[:field (force-using-column-alias-opts opts is-breakout) id-or-name]
@@ -1901,7 +1902,7 @@
;; We must not transform the head again else we'll have an infinite loop
;; (and we can't do it at the call-site as then it will be harder to fish out field references)
(let [honeysql-clause (into [op] (map (partial ->honeysql driver)) args)]
- (if-let [field-arg (driver-api/match-one args
+ (if-let [field-arg (match/match-one args
[#{:field :expression} & _] &match)]
[:or
honeysql-clause