Skip to content

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

[match] Implement match-many as a replacement for match (#68659)

Oleksandr Yakushev7beb2e3eb150
diff --git a/.clj-kondo/config.edn b/.clj-kondo/config.edn
index 950ba10deac..a1928f23fa9 100644
--- a/.clj-kondo/config.edn
+++ b/.clj-kondo/config.edn
@@ -250,6 +250,7 @@
(metabase.lib.util.match/replace-in)
(metabase.driver-api.core/match)
(metabase.driver-api.core/match-one)
+ (metabase.driver-api.core/match-lite)
(metabase.driver-api.core/replace)
(metabase.driver-api.core/replace-in)
(metabase.query-processor.middleware.cache-backend.interface/with-cached-results)
diff --git a/.cljfmt.edn b/.cljfmt.edn
index 614be11fd7c..34e207bc122 100644
--- a/.cljfmt.edn
+++ b/.cljfmt.edn
@@ -120,6 +120,7 @@
metabase.dashboards.models.dashboard-test/with-dash-in-collection [[:inner 0]]
metabase.driver-api.core/match [[:inner 0]]
metabase.driver-api.core/match-one [[:inner 0]]
+ metabase.driver-api.core/match-lite [[:inner 0]]
metabase.driver-api.core/replace [[:inner 0]]
metabase.driver-api.core/replace-in [[:inner 0]]
metabase.driver.bigquery-cloud-sdk.query-processor/with-temporal-type [[:default]]
diff --git a/dev/src/dev/debug_qp.clj b/dev/src/dev/debug_qp.clj
index f30fa0b0f77..1ef4789bdcc 100644
--- a/dev/src/dev/debug_qp.clj
+++ b/dev/src/dev/debug_qp.clj
@@ -260,28 +260,27 @@
coll))
(defn- can-symbolize? [x]
- (lib.util.match/match-one x
+ (lib.util.match/match-lite x
(_ :guard string?)
(not (re-find #"\s+" x))
[:field (id :guard pos-int?) nil]
(every? can-symbolize? (field-and-table-name id))
- [:field (field-name :guard string?) (opts :guard #(= (set (keys %)) #{:base-type}))]
+ [:field (field-name :guard string?) {:base-type _}]
(can-symbolize? field-name)
- [:field _ (opts :guard :join-alias)]
- (and (can-symbolize? (:join-alias opts))
- (can-symbolize? (mbql.u/update-field-options &match dissoc :join-alias)))
+ [:field _ {:join-alias join-alias}]
+ (and (can-symbolize? join-alias)
+ (can-symbolize? (mbql.u/update-field-options x dissoc :join-alias)))
- [:field _ (opts :guard :temporal-unit)]
- (and (can-symbolize? (name (:temporal-unit opts)))
- (can-symbolize? (mbql.u/update-field-options &match dissoc :temporal-unit)))
+ [:field _ {:temporal-unit temporal-unit}]
+ (and (can-symbolize? (name temporal-unit))
+ (can-symbolize? (mbql.u/update-field-options x dissoc :temporal-unit)))
- [:field _ (opts :guard :source-field)]
- (let [source-field-id (:source-field opts)]
- (and (can-symbolize? [:field source-field-id nil])
- (can-symbolize? (mbql.u/update-field-options &match dissoc :source-field))))
+ [:field _ {:source-field source-field-id}]
+ (and (can-symbolize? [:field source-field-id nil])
+ (can-symbolize? (mbql.u/update-field-options x dissoc :source-field)))
_
false))
diff --git a/enterprise/backend/src/metabase_enterprise/permission_debug/impl.clj b/enterprise/backend/src/metabase_enterprise/permission_debug/impl.clj
index 0cae8c8fce4..d24129fb456 100644
--- a/enterprise/backend/src/metabase_enterprise/permission_debug/impl.clj
+++ b/enterprise/backend/src/metabase_enterprise/permission_debug/impl.clj
@@ -177,7 +177,7 @@
[user-id card permissions-blocking permissions-granting]
(let [query (-> card :dataset_query qp.preprocess/preprocess)
query-tables (lib/all-source-table-ids query)
- native? (boolean (lib.util.match/match-one query (m :guard (every-pred map? :native))))]
+ native? (boolean (lib.util.match/match-lite query {:native (_ :guard identity)} true))]
(->>
(cond
native?
diff --git a/enterprise/backend/src/metabase_enterprise/sandbox/models/params/field_values.clj b/enterprise/backend/src/metabase_enterprise/sandbox/models/params/field_values.clj
index ca44fabb13c..b66688fd5ee 100644
--- a/enterprise/backend/src/metabase_enterprise/sandbox/models/params/field_values.clj
+++ b/enterprise/backend/src/metabase_enterprise/sandbox/models/params/field_values.clj
@@ -67,7 +67,7 @@
(into {} (for [[k v] attribute_remappings
;; get attribute that map to fields of the same table
:when (contains? field-ids
- (lib.util.match/match-one v
+ (lib.util.match/match-lite v
;; new style with {:stage-number }
[:dimension [:field field-id _] _] field-id
;; old style without stage number
diff --git a/enterprise/backend/src/metabase_enterprise/sandbox/query_processor/middleware/sandboxing.clj b/enterprise/backend/src/metabase_enterprise/sandbox/query_processor/middleware/sandboxing.clj
index 6b07985c55f..9ac82db7e90 100644
--- a/enterprise/backend/src/metabase_enterprise/sandbox/query_processor/middleware/sandboxing.clj
+++ b/enterprise/backend/src/metabase_enterprise/sandbox/query_processor/middleware/sandboxing.clj
@@ -95,7 +95,8 @@
[metadata-providerable :- ::lib.schema.metadata/metadata-providerable
target-field-clause :- ::lib.schema.parameter/target]
;; parameter targets still use legacy field refs for whatever wacko reason
- (when-let [field-id (lib.util.match/match-one target-field-clause [:field (field-id :guard pos-int?) _opts] field-id)]
+ (when-let [field-id (lib.util.match/match-lite target-field-clause
+ [:field (field-id :guard pos-int?) _opts] field-id)]
(:base-type (lib.metadata/field metadata-providerable field-id))))
(defn- attr-value->param-value
@@ -401,8 +402,8 @@
:feature :sandboxes
[{::keys [original-metadata] :as query} rff]
(fn merge-sandboxing-metadata-rff* [metadata]
- (let [metadata (assoc metadata :is_sandboxed (some? (lib.util.match/match-one query
- (m :guard (every-pred map? :query-permissions/sandboxed-table)))))
+ (let [metadata (assoc metadata :is_sandboxed (boolean (lib.util.match/match-lite query
+ {:query-permissions/sandboxed-table sandboxed?} sandboxed?)))
metadata (if original-metadata
(merge-metadata original-metadata metadata)
metadata)]
diff --git a/modules/drivers/bigquery-cloud-sdk/test/metabase/driver/bigquery_cloud_sdk/query_processor_test/reconciliation_test_util.clj b/modules/drivers/bigquery-cloud-sdk/test/metabase/driver/bigquery_cloud_sdk/query_processor_test/reconciliation_test_util.clj
index bb4eb831b78..48f2a71562f 100644
--- a/modules/drivers/bigquery-cloud-sdk/test/metabase/driver/bigquery_cloud_sdk/query_processor_test/reconciliation_test_util.clj
+++ b/modules/drivers/bigquery-cloud-sdk/test/metabase/driver/bigquery_cloud_sdk/query_processor_test/reconciliation_test_util.clj
@@ -171,7 +171,7 @@
(defn- temporal-type-reconciliation-expected-value
[{:keys [field temporal-type expected-value honeysql-filter-fn num-args], :as _test-case}]
- (let [field-literal? (lib.util.match/match-one field [:field (_ :guard string?) _])
+ (let [field-literal? (lib.util.match/match-lite field [:field (_ :guard string?) _] true)
expected-identifier (cond-> (-> (h2x/identifier :field "ABC" (name temporal-type))
(vary-meta assoc ::bigquery.qp/do-not-qualify? true))
(not field-literal?) (h2x/with-database-type-info (name temporal-type)))
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 8a26743ff85..347a050319b 100644
--- a/modules/drivers/druid/src/metabase/driver/druid/query_processor.clj
+++ b/modules/drivers/druid/src/metabase/driver/druid/query_processor.clj
@@ -336,7 +336,7 @@
(add-datetime-units* clause n)))
(defn- ->absolute-timestamp ^java.time.temporal.Temporal [clause]
- (driver-api/match-one clause
+ (driver-api/match-lite clause
[:absolute-datetime t :default]
t
@@ -356,7 +356,7 @@
clauses, the methods are skipped entirely."
{:arglists '([filter-clause])}
(fn [filter-clause]
- (when (driver-api/match-one filter-clause [:field _ (_ :guard :temporal-unit)])
+ (when (driver-api/match-lite filter-clause [:field _ (_ :guard :temporal-unit)] true)
(driver-api/dispatch-by-clause-name-or-class filter-clause))))
(defmethod filter-clause->intervals :default
@@ -683,8 +683,8 @@
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
- [:aggregation-options ag & _] #_:clj-kondo/ignore (recur ag)
+ [ag-type ag-field & args] (driver-api/match-lite ag-clause
+ [:aggregation-options ag & _] (&recur ag)
_ &match)]
(if-not (isa? query-type ::ag-query)
druid-query
@@ -730,25 +730,24 @@
(defn- expression-post-aggregation
[[operator & args, :as expression]]
- (driver-api/match-one expression
+ (driver-api/match-lite expression
;; If it's a named expression, we want to preserve the included name, so recurse, but merge in the name
[:aggregation-options ag _]
- (merge (expression-post-aggregation (second expression))
- {:name (driver-api/aggregation-name *query* expression)})
+ (assoc (expression-post-aggregation ag) :name (driver-api/aggregation-name *query* expression))
_
{:type :arithmetic
:name (driver-api/aggregation-name *query* expression)
:fn operator
:fields (vec (for [arg args]
- (driver-api/match-one arg
- number?
+ (driver-api/match-lite arg
+ (_ :guard number?)
{:type :constant, :name (str &match), :value &match}
- [:aggregation-options ag (options :guard :name)]
- {:type (post-aggregator-type ag), :fieldName (:name options)}
+ [:aggregation-options ag {:name name}]
+ {:type (post-aggregator-type ag), :fieldName name}
- #{:+ :- :/ :*}
+ [#{:+ :- :/ :*} & _]
(expression-post-aggregation &match)
;; we should never get here unless our code is B U S T E D
@@ -797,15 +796,15 @@
(*query-unique-name-fn* (name projection)))
(reduce
(fn [druid-query aggregation]
- (driver-api/match-one aggregation
- [:aggregation-options [(_ :guard #{:+ :- :/ :*}) & _] _]
+ (driver-api/match-lite aggregation
+ [:aggregation-options [#{:+ :- :/ :*} & _] _]
(handle-expression-aggregation query-type &match druid-query)
- #{:+ :- :/ :*}
+ [#{:+ :- :/ :*} & _]
(handle-expression-aggregation query-type &match druid-query)
_
- (handle-aggregation query-type &match druid-query)))
+ (handle-aggregation query-type aggregation druid-query)))
druid-query
aggregations)))
@@ -955,7 +954,7 @@
(defn- field-clause->name
[field-clause]
- (driver-api/match-one field-clause
+ (driver-api/match-lite field-clause
[:field (id :guard integer?) _]
(:name (driver-api/field (driver-api/metadata-provider) id))
@@ -1003,18 +1002,11 @@
(let [field (->rvalue field)
breakout-field (->rvalue breakout-field)
sort-by-breakout? (= field breakout-field)
- ag-field (driver-api/match-one ag
- :distinct
- :distinct___count
-
- [:aggregation-options _ (options :guard :name)]
- (:name options)
-
- [:aggregation-options wrapped-ag _]
- #_:clj-kondo/ignore (recur wrapped-ag)
-
- [(ag-type :guard keyword?) & _]
- ag-type)]
+ ag-field (driver-api/match-lite 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] (match [sort-by-breakout? direction]
@@ -1035,7 +1027,7 @@
datetime"
[field]
(when field
- (driver-api/match-one field
+ (driver-api/match-lite field
[:field _id-or-name (_opts :guard :temporal-unit)]
true
@@ -1174,7 +1166,7 @@
ts? (boolean
(and
;; Checks whether the query is a timeseries
- (driver-api/match-one (first breakout-fields) [:field _ (_ :guard :temporal-unit)])
+ (driver-api/match-lite (first breakout-fields) [:field _ (_ :guard :temporal-unit)] true)
;; (excludes x-of-y type breakouts)
(contains? timeseries-units (:unit (first breakout-fields)))
;; (excludes queries with LIMIT)
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 d54f8628820..8455c58154f 100644
--- a/modules/drivers/mongo/src/metabase/driver/mongo/query_processor.clj
+++ b/modules/drivers/mongo/src/metabase/driver/mongo/query_processor.clj
@@ -1122,9 +1122,9 @@ function(bin) {
:default (->rvalue (:default options))}})
(defn- aggregation->rvalue [ag]
- (driver-api/match-one ag
+ (driver-api/match-lite ag
[:aggregation-options ag' _]
- (recur ag')
+ (&recur ag')
[:count]
{$sum 1}
@@ -1136,7 +1136,7 @@ function(bin) {
;; these aggregation types can all be used in expressions as well so their implementations live above in the
;; general [[->rvalue]] implementations
- #{:avg :stddev :sum :min :max}
+ [#{:avg :stddev :sum :min :max} & _]
(->rvalue &match)
[:distinct arg]
@@ -1148,9 +1148,9 @@ function(bin) {
:else 0}}}
[:count-where pred]
- (recur [:sum-where [:value 1] pred])
+ (&recur [:sum-where [:value 1] pred])
- :else
+ _
(throw
(ex-info (tru "Don''t know how to handle aggregation {0}" ag)
{:type :invalid-query, :clause ag}))))
@@ -1462,7 +1462,7 @@ function(bin) {
(fn [m field-clause]
(assoc-in
m
- (driver-api/match-one field-clause
+ (driver-api/match-lite field-clause
[:field (field-id :guard integer?) _]
(str/split (field-alias field-clause) #"\.")
diff --git a/modules/drivers/mongo/test/metabase/driver/mongo_test.clj b/modules/drivers/mongo/test/metabase/driver/mongo_test.clj
index 10207002992..65456c87308 100644
--- a/modules/drivers/mongo/test/metabase/driver/mongo_test.clj
+++ b/modules/drivers/mongo/test/metabase/driver/mongo_test.clj
@@ -671,11 +671,12 @@
(deftest xrays-test
(mt/test-driver :mongo
(testing "make sure x-rays don't use features that the driver doesn't support"
- (is (empty?
- (lib.util.match/match-one (->> (magic/automagic-analysis (t2/select-one :model/Field :id (mt/id :venues :price)) {})
- :dashcards
- (mapcat (comp :breakout :query :dataset_query :card)))
- [:field _ (_ :guard :binning)]))))))
+ (is (nil?
+ (lib.util.match/match-lite
+ (->> (magic/automagic-analysis (t2/select-one :model/Field :id (mt/id :venues :price)) {})
+ :dashcards
+ (mapcat (comp :breakout :query :dataset_query :card)))
+ [:field _ (_ :guard :binning)] true))))))
(deftest no-values-test
(mt/test-driver :mongo
diff --git a/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj b/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj
index 46f0299d962..e4fbc104555 100644
--- a/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj
+++ b/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj
@@ -246,7 +246,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 (_ :guard #{:date "date"})}]))
+ (driver-api/match-lite base-expr [::h2x/typed _ {:database-type #{:date "date"}}] true))
day-expr
(h2x/cast :datetime2 day-expr)))
diff --git a/src/metabase/driver/sql/query_processor.clj b/src/metabase/driver/sql/query_processor.clj
index 0f3f00285f8..017a8acbc01 100644
--- a/src/metabase/driver/sql/query_processor.clj
+++ b/src/metabase/driver/sql/query_processor.clj
@@ -1301,30 +1301,29 @@
;; 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)
- [:aggregation-options ag (options :guard driver-api/qp.add.desired-alias)]
- (->honeysql driver (h2x/identifier :field-alias (driver-api/qp.add.desired-alias options)))
+ (driver-api/match-lite (nth (:aggregation *inner-query*) index)
+ [:aggregation-options ag {driver-api/qp.add.desired-alias desired-alias}]
+ (->honeysql driver (h2x/identifier :field-alias desired-alias))
- [:aggregation-options ag (options :guard driver-api/qp.add.source-alias)]
- (->honeysql driver (h2x/identifier :field-alias (driver-api/qp.add.source-alias options)))
+ [:aggregation-options ag {driver-api/qp.add.source-alias source-alias}]
+ (->honeysql driver (h2x/identifier :field-alias source-alias))
- [:aggregation-options ag (options :guard :name)]
- (->honeysql driver (h2x/identifier :field-alias (:name options)))
+ [:aggregation-options ag {:name name}]
+ (->honeysql driver (h2x/identifier :field-alias name))
[:aggregation-options ag options]
- #_{:clj-kondo/ignore [:invalid-arity]}
- (recur ag)
+ (&recur ag)
;; For some arcane reason we name the results of a distinct aggregation "count", everything else is named the
;; same as the aggregation
- :distinct
+ [:distinct & _]
(->honeysql driver (h2x/identifier :field-alias :count))
- #{:+ :- :* :/}
+ [#{:+ :- :* :/} & _]
(->honeysql driver &match)
- [:offset (options :guard :name) _expr _n]
- (->honeysql driver (h2x/identifier :field-alias (:name options)))
+ [:offset {:name name} _expr _n]
+ (->honeysql driver (h2x/identifier :field-alias name))
;; for everything else just use the name of the aggregation as an identifier, e.g. `:sum`
;;
@@ -1748,9 +1747,8 @@
;; 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
- :field &match
- :expression &match)]
+ (if-let [field-arg (driver-api/match-lite args
+ [#{:field :expression} & _] &match)]
[:or
honeysql-clause
[:= (->honeysql driver field-arg) nil]]
diff --git a/src/metabase/driver_api/core.clj b/src/metabase/driver_api/core.clj
index 24f960362db..6b6fc7b6040 100644
--- a/src/metabase/driver_api/core.clj
+++ b/src/metabase/driver_api/core.clj
@@ -107,6 +107,7 @@
lib.types.isa/temporal?
lib.util.match/match
lib.util.match/match-one
+ lib.util.match/match-lite
lib.util.match/replace
lib/truncate-alias
lib/->legacy-MBQL
diff --git a/src/metabase/lib/fe_util.cljc b/src/metabase/lib/fe_util.cljc
index e8fb3e28b77..ef77c540c23 100644
--- a/src/metabase/lib/fe_util.cljc
+++ b/src/metabase/lib/fe_util.cljc
@@ -734,7 +734,7 @@
(mu/defn join-condition-parts :- [:maybe JoinConditionParts]
"Destructures a join condition created by [[join-condition-clause]]."
[join-condition :- ::lib.schema.join/condition]
- (lib.util.match/match-one join-condition
+ (lib.util.match/match-lite join-condition
[(op :guard lib.schema.join/condition-operators) _ lhs rhs]
{:operator op, :lhs-expression lhs, :rhs-expression rhs}
diff --git a/src/metabase/lib/filter/desugar.cljc b/src/metabase/lib/filter/desugar.cljc
index d2b2be064a0..6be0baed66d 100644
--- a/src/metabase/lib/filter/desugar.cljc
+++ b/src/metabase/lib/filter/desugar.cljc
@@ -247,7 +247,7 @@
(lib.util.match/replace expr
[tag opts field & (args :guard (fn [args]
(some (fn [arg]
- (lib.util.match/match-one arg [:relative-datetime _opts :current]))
+ (lib.util.match/match-lite arg [:relative-datetime _opts :current] true))
args)))]
(let [temporal-unit (or (lib.util.match/match-lite field
[:field {:temporal-unit temporal-unit} _]
diff --git a/src/metabase/lib/metadata/result_metadata.cljc b/src/metabase/lib/metadata/result_metadata.cljc
index 06ef2e79442..d8651710de1 100644
--- a/src/metabase/lib/metadata/result_metadata.cljc
+++ b/src/metabase/lib/metadata/result_metadata.cljc
@@ -170,10 +170,9 @@
(log/error e "Column metadata has invalid :lib/expression-name (this was probably incorrectly propagated from a previous stage) (QUE-1342)")
(log/debugf "In query:\n%s" (u/pprint-to-str query))
nil))]
- (lib.util.match/match-one expr
- :convert-timezone
- (let [[_convert-timezone _opts _expr source-tz] &match]
- source-tz)))))]
+ (lib.util.match/match-lite expr
+ [:convert-timezone _opts _expr source-tz & _]
+ source-tz))))]
(cond-> col
converted-timezone (assoc :converted-timezone converted-timezone))))
cols))
diff --git a/src/metabase/lib/util/match.clj b/src/metabase/lib/util/match.clj
index 57775d434ae..f3230dac3ba 100644
--- a/src/metabase/lib/util/match.clj
+++ b/src/metabase/lib/util/match.clj
@@ -247,7 +247,7 @@
{:type :map
:map pattern}
- (or (number? pattern) (keyword? pattern) (boolean? pattern) (nil? pattern))
+ (or (number? pattern) (keyword? pattern) (string? pattern) (boolean? pattern) (nil? pattern))
{:type :equality
:value pattern}
@@ -269,17 +269,21 @@
cnt (count parts)]
(vswap! bindings conj (with-meta [s `(metabase.lib.util.match.impl/vector! ~value)]
{:vector-check true}))
- (dorun (map-indexed #(process-pattern %2 (list `nth s %1 nil) bindings conditions return) parts))
+ (dorun (map-indexed #(process-pattern %2 (with-meta (list `nth s %1 nil) (meta s))
+ bindings conditions return) parts))
(if rest-part
(process-pattern rest-part (list `drop cnt s) bindings conditions false)
(vswap! conditions conj (with-meta (list `metabase.lib.util.match.impl/count= s cnt)
{:depends-on s}))))
:map (let [s (if (symbol? value) value (gensym "map"))]
(vswap! bindings conj [s `(metabase.lib.util.match.impl/map! ~value)])
- (run! (fn [[k v]] (process-pattern v (list `get s k) bindings conditions false)) (:map parsed)))
+ (run! (fn [[k v]]
+ (vswap! conditions conj (with-meta (list `contains? s k) {:depends-on s}))
+ (process-pattern v (list `get s k) bindings conditions false))
+ (:map parsed)))
:or (let [or-clauses (:clauses parsed)
new-body (process-clauses (mapv vector (:clauses parsed) (repeat (count or-clauses) @return)) value nil)]
- (vreset! return new-body))
+ (vreset! return (with-meta new-body {:nil-wrapped true})))
:guard (let [s (:symbol parsed)
s (if (= s '_) (gensym "_") s)
;; Treat symbol, keyword, or set predicates as functions to be called, and thus transform them
@@ -301,7 +305,7 @@
(when (:length parsed)
(vswap! conditions conj (with-meta (list `metabase.lib.util.match.impl/count= s (:length parsed))
{:depends-on s}))))
- :equality (vswap! conditions conj (list `= value (:value parsed)))
+ :equality (vswap! conditions conj (with-meta (list `= value (:value parsed)) (meta value)))
:set (vswap! conditions conj (list (:value parsed) value)))))
(defn- process-clause [[pattern ret] value-sym]
@@ -314,6 +318,11 @@
(defn- seq-contains? [coll item]
(some #(= % item) coll))
+(defn- strip-meta [expr]
+ (if (instance? clojure.lang.IObj expr)
+ (vary-meta expr dissoc :depends-on :vector-check)
+ expr))
+
(defn- collect-common [bindings conditions]
(let [common-bindings (filter (fn [bind] (every? #(seq-contains? % bind) bindings))
(first bindings))
@@ -322,17 +331,16 @@
(seq-contains? (map first common-bindings)
(:depends-on (meta condition)))))
(first conditions))]
- {:common-bindings common-bindings
- :common-conditions (->> common-conditions
- (mapv #(vary-meta % dissoc :depends-on)))
+ {:common-bindings (mapv strip-meta common-bindings)
+ :common-conditions (mapv strip-meta common-conditions)
:all-bindings (mapv (fn [bindings]
- (remove #(seq-contains? common-bindings %) bindings))
+ (strip-meta (remove #(seq-contains? common-bindings %) bindings)))
bindings)
:all-conditions (->> conditions
(mapv (fn [conditions]
(->> conditions
(remove #(seq-contains? common-conditions %))
- (mapv #(vary-meta % dissoc :depends-on))))))}))
+ (mapv strip-meta)))))}))
(defn- expand-conditions [combiner conditions body & [bool?]]
(case (count conditions)
@@ -348,11 +356,16 @@
(if (empty? bindings) body
`(let ~(vec (mapcat identity bindings)) ~body)))
+(defn- maybe-wrap-nil [expr]
+ (if (:nil-wrapped (meta expr))
+ (vary-meta expr dissoc :nil-wrapped)
+ `(metabase.lib.util.match.impl/wrap-nil ~expr)))
+
(defn- expand-or-some [args]
(case (count args)
0 nil
1 (first args)
- `(if-some [a# ~(first args)] a# ~(expand-or-some (rest args)))))
+ `(let [a# ~(first args)] (if (some? a#) a# ~(expand-or-some (rest args))))))
(defn- emit-clause [{:keys [common-bindings common-conditions all-bindings all-conditions]}
returns value-sym value-binding]
@@ -360,8 +373,8 @@
;; Only allow extracting same result if there are no individual bindings in branches.
(every? empty? all-bindings))
;; If all clauses check for vector, hoist the check into the let binding.
- common-vector-check? (some #(:vector-check (meta %)) common-bindings)
- common-bindings (remove #(:vector-check (meta %)) common-bindings)
+ common-vector-check? (some #(and (= % value-sym) (:vector-check (meta %))) common-bindings)
+ common-bindings (remove #(and (= % value-sym) (:vector-check (meta %))) common-bindings)
value-binding (if common-vector-check?
`(metabase.lib.util.match.impl/vector! ~(or value-binding value-sym))
value-binding)]
@@ -374,10 +387,10 @@
`(when (or ~@(mapv (fn [bindings conditions]
(expand-bindings bindings (expand-conditions `and conditions true true)))
all-bindings all-conditions))
- ~(first returns))
+ ~(maybe-wrap-nil (first returns)))
(expand-or-some
(mapv (fn [bindings conditions return-expr]
- (expand-bindings bindings (expand-conditions `and conditions return-expr)))
+ (expand-bindings bindings (expand-conditions `and conditions (maybe-wrap-nil return-expr))))
all-bindings all-conditions returns)))))))
(defn- process-clauses [clauses value-sym value-binding]
@@ -395,22 +408,31 @@
[pairs nil])
;; match-lite is always recursive unless there is a default clause
recursive? (not has-default?)
+ contains-&recur? (volatile! false)
+ ;; Search for &recur usage in clauses.
+ _ (perf/postwalk #(when (= % '&recur) (vreset! contains-&recur? true)) (map second pairs))
;; Wrap explicit nil values.
value (if (nil? value) `(identity nil) value)
- [value-sym value-binding] (cond recursive? [(gensym "value") nil]
- (symbol? value) [value nil]
- :else [(gensym "value") value])
+ [value-sym value-binding] (if (or recursive? @contains-&recur?)
+ ['&match nil]
+ ['&match value])
body (process-clauses pairs value-sym value-binding)]
(if recursive?
- (let [f (gensym "f")]
- `((fn ~f [~value-sym]
- ~(expand-or-some
- [body
- `(metabase.lib.util.match.impl/match-lite-in-collection ~f ~value-sym)]))
- ~value))
- (expand-or-some
- (cond-> [body]
- (some? default) (conj default))))))
+ `((fn ~'&recur [~value-sym]
+ (metabase.lib.util.match.impl/unwrap-nil
+ ~(expand-or-some
+ [body
+ `(metabase.lib.util.match.impl/match-lite-in-collection ~'&recur ~value-sym)])))
+ ~value)
+ (let [body `(metabase.lib.util.match.impl/unwrap-nil
+ ~(expand-or-some
+ (cond-> [body]
+ (some? default) (conj default))))]
+ (if @contains-&recur?
+ `((fn ~'&recur [~value-sym]
+ ~body)
+ ~value)
+ body)))))
(defmacro match-lite
"Pattern matching macro, simplified version of [[clojure.core.match]].
@@ -426,7 +448,7 @@
Patterns can be:
- symbol - binds the entire value
- - keyword - must match exactly
+ - keyword or string - must match exactly
- set - must be one of the set items
- (sym :guard pred :len size) - bind with predicate check. The predicate should either be a symbol denoting a function, keyword, set, or an invocation snippet (but not a lambda). Can optionally check for collection length.
- vector - binds positional values inside a sequence against other patterns. Can have & to bind remaining elements.
diff --git a/src/metabase/lib/util/match/impl.cljc b/src/metabase/lib/util/match/impl.cljc
index 236ba20cd7b..8c3d5ab59d3 100644
--- a/src/metabase/lib/util/match/impl.cljc
+++ b/src/metabase/lib/util/match/impl.cljc
@@ -85,6 +85,16 @@
[coll cnt]
(= (count coll) cnt))
+(defn wrap-nil
+ "If `value` is nil, return `::wrapped-nil`, otherwise return `value`."
+ [value]
+ (if (nil? value) ::wrapped-nil value))
+
+(defn unwrap-nil
+ "If `value` is `::wrapped-nil`, return nil, otherwise return `value`."
+ [value]
+ (when-not (= value ::wrapped-nil) value))
+
(defn match-lite-in-collection
"Internal impl for `match-lite`. If `form` is a collection, call `match-fn` to recursively look for matches in it."
[match-fn form]
@@ -92,12 +102,12 @@
(cond
(map? form)
(reduce-kv (fn [_ _ v]
- (when-let [match (match-fn v)]
+ (when-some [match (match-fn v)]
(reduced match)))
nil form)
(sequential? form)
(reduce (fn [_ v]
- (when-let [match (match-fn v)]
+ (when-some [match (match-fn v)]
(reduced match)))
nil form)))
diff --git a/src/metabase/query_processor/card.clj b/src/metabase/query_processor/card.clj
index 94d8940f853..4bbcd1e4165 100644
--- a/src/metabase/query_processor/card.clj
+++ b/src/metabase/query_processor/card.clj
@@ -205,7 +205,7 @@
[{parameter-name :name, :keys [target]}]
(or
parameter-name
- (lib.util.match/match-one target
+ (lib.util.match/match-lite target
[:template-tag tag-name]
(name tag-name))))
diff --git a/src/metabase/query_processor/middleware/add_implicit_joins.clj b/src/metabase/query_processor/middleware/add_implicit_joins.clj
index b903e08f41e..b292eff4f7a 100644
--- a/src/metabase/query_processor/middleware/add_implicit_joins.clj
+++ b/src/metabase/query_processor/middleware/add_implicit_joins.clj
@@ -119,8 +119,8 @@
(-> join
(assoc :alias new-alias)
(update :conditions lib.walk/walk-clauses* (fn [clause]
- (lib.util.match/match-one clause
- [:field (_opts :guard #(= (:join-alias %) join-alias)) _id-or-name]
+ (lib.util.match/match-lite clause
+ [:field {:join-alias (ja :guard (= ja join-alias))} _id-or-name]
(lib/update-options &match assoc :join-alias new-alias))))))
(mu/defn- implicitly-joined-fields->joins :- [:sequential ::join]
@@ -129,8 +129,8 @@
field-clauses-with-source-field :- [:sequential :mbql.clause/field]]
(let [fk-field-infos (->> field-clauses-with-source-field
(keep (fn [clause]
- (lib.util.match/match-one clause
- [:field (opts :guard (every-pred :source-field (complement :join-alias))) (id :guard integer?)]
+ (lib.util.match/match-lite clause
+ [:field (opts :guard (and (:source-field opts) (not (:join-alias opts)))) (id :guard integer?)]
(field-opts->fk-field-info metadata-providerable opts))))
distinct
not-empty)
@@ -377,8 +377,8 @@
path :- ::lib.walk/path
stage :- ::lib.schema/stage]
(when (and (= (:lib/type stage) :mbql.stage/mbql)
- (lib.util.match/match-one stage
- [:field (_opts :guard (every-pred :source-field (complement :join-alias))) _id-or-name]))
+ (lib.util.match/match-lite stage
+ [:field (opts :guard (and (:source-field opts) (not (:join-alias opts)))) _id-or-name] true))
(when (and driver/*driver*
(not (driver.u/supports? driver/*driver* :left-join (lib.metadata/database query))))
(throw (ex-info (tru "{0} driver does not support left join." driver/*driver*)
diff --git a/src/metabase/query_processor/middleware/auto_bucket_datetimes.clj b/src/metabase/query_processor/middleware/auto_bucket_datetimes.clj
index 4c639720af3..dd810375063 100644
--- a/src/metabase/query_processor/middleware/auto_bucket_datetimes.clj
+++ b/src/metabase/query_processor/middleware/auto_bucket_datetimes.clj
@@ -116,11 +116,12 @@
:do-not-bucket-reason/not-all-values-are-auto-bucketable)
;; * do not autobucket clauses that are updating the time interval
- (lib.util.match/match-one x
+ (lib.util.match/match-lite x
[(_tag :guard #{:+ :-})
_
- [(_ :guard #{:expression :field}) _ _]
- [:interval _ _n (unit :guard #{:minute :hour :second})]])
+ [#{:expression :field} _ _]
+ [:interval _ _n (unit :guard #{:minute :hour :second})]]
+ true)
:do-not-bucket-reason/bucket-between-relative-starting-from
;; do not auto-bucket clauses inside a `:time-interval` filter: it already supplies its own unit
diff --git a/src/metabase/query_processor/middleware/optimize_temporal_filters.clj b/src/metabase/query_processor/middleware/optimize_temporal_filters.clj
index 5c1ec0042e1..b10acedfcf1 100644
--- a/src/metabase/query_processor/middleware/optimize_temporal_filters.clj
+++ b/src/metabase/query_processor/middleware/optimize_temporal_filters.clj
@@ -30,8 +30,8 @@
(isa? expr-type :type/Temporal))))))
(defn- optimizable-expr? [expr]
- (lib.util.match/match-one expr
- #{:field :expression}
+ (lib.util.match/match-lite expr
+ [#{:field :expression} & _]
(and (temporal-ref? &match)
(let [unit (or (lib/raw-temporal-bucket &match) :default)]
(or (= unit :default)
@@ -44,7 +44,7 @@
(defn- optimizable-temporal-value?
"Can `temporal-value` clause can be optimized?"
[temporal-value]
- (lib.util.match/match-one temporal-value
+ (lib.util.match/match-lite temporal-value
[:relative-datetime _opts (_n :guard #{0 :current})]
true
@@ -57,7 +57,7 @@
"Do datetime `field` clause and `temporal-value` clause have 'compatible' units that mean we'll be able to optimize
the filter clause they're in?"
[field temporal-value]
- (lib.util.match/match-one temporal-value
+ (lib.util.match/match-lite temporal-value
[:relative-datetime _opts (_n :guard #{0 :current})]
true
@@ -71,7 +71,7 @@
(defmethod can-optimize-filter? :default
[filter-clause]
- (lib.util.match/match-one filter-clause
+ (lib.util.match/match-lite filter-clause
[_tag
_opts
(field :guard optimizable-expr?)
@@ -90,34 +90,33 @@
(defmethod can-optimize-filter? :>=
[filter-clause]
- (lib.util.match/match-one
+ (lib.util.match/match-lite
filter-clause
[_tag
_opts
- ;; Don't optimize >= with column that has default temporal bucket
- (field :guard (every-pred not-default-bucket-clause? optimizable-expr?))
+ ;; Don't optimize >= with column that has default temporal bucket
+ (field :guard (and (not-default-bucket-clause? field) (optimizable-expr? field)))
(temporal-value :guard optimizable-temporal-value?)]
(field-and-temporal-value-have-compatible-units? field temporal-value)))
(defmethod can-optimize-filter? :<
[filter-clause]
- (lib.util.match/match-one
- filter-clause
+ (lib.util.match/match-lite filter-clause
[_tag
_opts
;; Don't optimize < with column that has default temporal bucket
- (field :guard (every-pred not-default-bucket-clause? optimizable-expr?))
+ (field :guard (and (not-default-bucket-clause? field) (optimizable-expr? field)))
(temporal-value :guard optimizable-temporal-value?)]
(field-and-temporal-value-have-compatible-units? field temporal-value)))
(defmethod can-optimize-filter? :between
[filter-clause]
- (lib.util.match/match-one filter-clause
+ (lib.util.match/match-lite filter-clause
[:between
_opts
[(_offset :guard #{:+ :-})
_plus_minus_opts
- (field :guard (every-pred (comp #{:field :expression} first) optimizable-expr?))
+ (field :guard (and (vector? field) (#{:field :expression} (first field)) (optimizable-expr? field)))
[:interval _interval_opts _n _unit]]
(temporal-value-1 :guard optimizable-temporal-value?)
(temporal-value-2 :guard optimizable-temporal-value?)]
@@ -126,7 +125,7 @@
[:between
_opts
- (field :guard (every-pred (comp #{:field :expression} first) optimizable-expr?))
+ (field :guard (and (vector? field) (#{:field :expression} (first field)) (optimizable-expr? field)))
(temporal-value-1 :guard optimizable-temporal-value?)
(temporal-value-2 :guard optimizable-temporal-value?)]
(and (field-and-temporal-value-have-compatible-units? field temporal-value-1)
diff --git a/src/metabase/query_processor/middleware/parameters/mbql.clj b/src/metabase/query_processor/middleware/parameters/mbql.clj
index 96451db2685..62817436381 100644
--- a/src/metabase/query_processor/middleware/parameters/mbql.clj
+++ b/src/metabase/query_processor/middleware/parameters/mbql.clj
@@ -75,8 +75,8 @@
[query :- ::lib.schema/query
stage-path :- ::lib.walk/path
{param-type :type, param-value :value, target :target, :as param} :- ::lib.schema.parameter/parameter]
- (let [a-ref (lib.util.match/match-one target
- #{:field :expression}
+ (let [a-ref (lib.util.match/match-lite target
+ [#{:field :expression} & _]
(lib/->pMBQL &match))]
(cond
(params.ops/operator? param-type)
@@ -152,7 +152,7 @@
;; ignore `:template-tag` parameters... these may be lying around if we had a source card that was native and
;; then replaced it with an MBQL source card.
- (lib.util.match/match-one target :template-tag &match)
+ (lib.util.match/match-lite target [:template-tag & _] &match)
(do
(log/warnf "Ignoring :template-tag parameter %s because this is an MBQL stage (path = %s)"
(pr-str target)
diff --git a/src/metabase/query_processor/middleware/persistence.clj b/src/metabase/query_processor/middleware/persistence.clj
index 2c932d88582..140c8449ab9 100644
--- a/src/metabase/query_processor/middleware/persistence.clj
+++ b/src/metabase/query_processor/middleware/persistence.clj
@@ -14,7 +14,7 @@
work, but for now we skip the cache in these cases."
[query]
(if (and api/*current-user-id*
- (or (some? (lib.util.match/match-one query (m :guard (every-pred map? :query-permissions/sandboxed-table))))
+ (or (lib.util.match/match-lite query {:query-permissions/sandboxed-table (_ :guard identity)} true)
(perms/impersonation-enforced-for-db? (:database query))))
(lib.util.match/replace query
(x :guard (every-pred map? :persisted-info/native))
diff --git a/src/metabase/query_processor/middleware/wrap_value_literals.clj b/src/metabase/query_processor/middleware/wrap_value_literals.clj
index 1d28f8cd9eb..355eb399215 100644
--- a/src/metabase/query_processor/middleware/wrap_value_literals.clj
+++ b/src/metabase/query_processor/middleware/wrap_value_literals.clj
@@ -283,9 +283,9 @@
(defn unwrap-value-literal
"Extract value literal from `:value` form or returns form as is if not a `:value` form."
[maybe-value-form]
- (lib.util.match/match-one maybe-value-form
+ (lib.util.match/match-lite maybe-value-form
[:value x & _] x
- _ &match))
+ _ maybe-value-form))
(defn- type-info-no-query
"This is like [[type-info*]] but specifically for supporting the legacy/deprecated [[wrap-value-literals-in-mbql]]
diff --git a/src/metabase/query_processor/parameters/operators.clj b/src/metabase/query_processor/parameters/operators.clj
index 64a294611b6..68d8c114818 100644
--- a/src/metabase/query_processor/parameters/operators.clj
+++ b/src/metabase/query_processor/parameters/operators.clj
@@ -80,8 +80,8 @@
`:type qp.error-type/invalid-parameter` if arity is incorrect."
[param]
(let [{param-type :type, [a b :as param-value] :value, target :target, options :options} (normalize-param param)
- field-ref (or (lib.util.match/match-one target
- #{:field :expression}
+ field-ref (or (lib.util.match/match-lite target
+ [#{:field :expression} & _]
(lib/->pMBQL &match))
(throw (ex-info (format "Invalid target: expected :field ref, got: %s" (pr-str target))
{:target target, :type qp.error-type/invalid-parameter})))
diff --git a/src/metabase/query_processor/util/nest_query.clj b/src/metabase/query_processor/util/nest_query.clj
index 7f8869b4d61..0ad7de7ff35 100644
--- a/src/metabase/query_processor/util/nest_query.clj
+++ b/src/metabase/query_processor/util/nest_query.clj
@@ -22,7 +22,7 @@
;; fail, but it might still make sense. For example, #48721 would have been avoided by unconditional marking.
(defn- contains-expression-refs? [location]
- (lib.util.match/match-one location :expression))
+ (lib.util.match/match-lite location [:expression & _] true))
(defn- should-nest-expressions? [query path]
(and (lib.walk/apply-f-for-stage-at-path lib/mbql-stage? query path)
diff --git a/src/metabase/xrays/automagic_dashboards/util.clj b/src/metabase/xrays/automagic_dashboards/util.clj
index 4aa6d6d1cd5..80e8febe168 100644
--- a/src/metabase/xrays/automagic_dashboards/util.clj
+++ b/src/metabase/xrays/automagic_dashboards/util.clj
@@ -61,7 +61,7 @@
(mu/defn field-reference->id :- [:maybe [:or ms/NonBlankString ::lib.schema.id/field]]
"Extract field ID from a given field reference form."
[clause :- :mbql.clause/field]
- (lib.util.match/match-one clause [:field _opts id] id))
+ (lib.util.match/match-lite clause [:field _opts id] id))
(mu/defn collect-field-references :- [:maybe [:sequential :mbql.clause/field]]
"Collect all `:field` references from a given form."
diff --git a/src/metabase/xrays/transforms/core.clj b/src/metabase/xrays/transforms/core.clj
index af9bb7a3a46..e4667d20207 100644
--- a/src/metabase/xrays/transforms/core.clj
+++ b/src/metabase/xrays/transforms/core.clj
@@ -30,7 +30,7 @@
(defn- mbql-reference->col-name
[field-clause]
- (lib.util.match/match-one field-clause
+ (lib.util.match/match-lite field-clause
[:field (field-name :guard string?) _]
field-name
diff --git a/test/metabase/channel/render/card_test.clj b/test/metabase/channel/render/card_test.clj
index 2d95b9a39da..4240a8a9e37 100644
--- a/test/metabase/channel/render/card_test.clj
+++ b/test/metabase/channel/render/card_test.clj
@@ -39,9 +39,9 @@
:display :line
:visualization_settings {:graph.dimensions ["CREATED_AT"]
:graph.metrics ["count"]}}]
- (is (some? (lib.util.match/match-one
+ (is (some? (lib.util.match/match-lite
(render-pulse-card card)
- [:img _])))))))
+ [:img _] true)))))))
(deftest ^:parallel render-error-test
(testing "gives us a proper error if we have erroring card"
@@ -293,8 +293,9 @@
nil
(qp/process-query (:dataset_query card))
{:channel.render/include-title? true}))]
- (is (some? (lib.util.match/match-one rendered-card-content
- [:a (_ :guard #(= (format "https://mb.com/question/%d" (:id card)) (:href %))) "A Card"]))))))))
+ (is (lib.util.match/match-lite rendered-card-content
+ [:a {:href (href :guard (= href (format "https://mb.com/question/%d" (:id card))))} "A Card"]
+ true)))))))
(deftest href-includes-scroll
(testing "the title and body hrefs for cards in dashboards should be of the form '.../dashboard/<DASHBOARD_ID>#scrollTo=<DASHBOARD_CARD_ID>'"
diff --git a/test/metabase/lib/util/match_test.cljc b/test/metabase/lib/util/match_test.cljc
index 1105d83b035..18100157655 100644
--- a/test/metabase/lib/util/match_test.cljc
+++ b/test/metabase/lib/util/match_test.cljc
@@ -400,3 +400,15 @@
#?(:clj (t/is (thrown? clojure.lang.Compiler$CompilerException
(eval '(lib.util.match/match-lite [1]
[(a :guard (fn [x] (odd? x)))] a))))))
+
+(t/deftest ^:parallel return-nil-matches-test
+ (t/testing "a clause can return nil and that is still considered a match"
+ (t/is (= nil (lib.util.match/match-lite [:a nil]
+ [:a x] x
+ _ :default)))))
+
+(t/deftest ^:parallel recur-test
+ (t/testing "clause can choose to &recur the match with a different value"
+ (t/is (= :inside (lib.util.match/match-lite [[[[[[:inside]]]]]]
+ [in] (&recur in)
+ _ &match)))))
diff --git a/test/metabase/warehouse_schema/metadata_queries_test.clj b/test/metabase/warehouse_schema/metadata_queries_test.clj
index 9e7a9a496ac..1bba4d2655e 100644
--- a/test/metabase/warehouse_schema/metadata_queries_test.clj
+++ b/test/metabase/warehouse_schema/metadata_queries_test.clj
@@ -9,7 +9,7 @@
[metabase.warehouse-schema.metadata-queries :as metadata-queries]))
(defn- sort-by-field-id [clauses]
- (sort-by #(lib.util.match/match-one %
+ (sort-by #(lib.util.match/match-lite %
[:field _opts (id :guard pos-int?)]
id)
clauses))