The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Merge remaining match -> match-many rewrites (#69686)
Oleksandr Yakushevd4702e10e03c
diff --git a/.clj-kondo/config.edn b/.clj-kondo/config.ednindex a1928f23fa9..1e497cde5fe 100644--- a/.clj-kondo/config.edn+++ b/.clj-kondo/config.edn@@ -227,7 +227,8 @@;;:invalid-arity {:skip-args [metabase.lib.util.match/match- metabase.lib.util.match/match-lite]} ; TODO (cam): can we fix these?+ metabase.lib.util.match/match-lite+ metabase.lib.util.match/match-many]} ; TODO (cam): can we fix these?:refer-all {:level :warning, :exclude [clojure.test]}:unused-referred-var {:exclude {compojure.core [GET DELETE POST PUT]}}@@ -246,6 +247,7 @@(metabase.lib.util.match/match)(metabase.lib.util.match/match-one)(metabase.lib.util.match/match-lite)+ (metabase.lib.util.match/match-many)(metabase.lib.util.match/replace)(metabase.lib.util.match/replace-in)(metabase.driver-api.core/match)diff --git a/.cljfmt.edn b/.cljfmt.ednindex 34e207bc122..f8499bfc4a4 100644--- a/.cljfmt.edn+++ b/.cljfmt.edn@@ -157,6 +157,7 @@metabase.lib.util.match/match [[:inner 0]]metabase.lib.util.match/match-one [[:inner 0]]metabase.lib.util.match/match-lite [[:inner 0]]+ metabase.lib.util.match/match-many [[:inner 0]]metabase.lib.util.match/replace [[:inner 0]]metabase.lib.util.match/replace-in [[:inner 0]]metabase.models.interface/define-batched-hydration-method [[:inner 0]]diff --git a/src/metabase/legacy_mbql/util.cljc b/src/metabase/legacy_mbql/util.cljcindex cc6959b3222..5a2330e3939 100644--- a/src/metabase/legacy_mbql/util.cljc+++ b/src/metabase/legacy_mbql/util.cljc@@ -722,7 +722,7 @@(not-empty(into #{}(comp cat (filter some?))- (lib.util.match/match coll+ (lib.util.match/match-many coll[:field (id :guard integer?) opts][id (:source-field opts)]))))diff --git a/src/metabase/lib/expression.cljc b/src/metabase/lib/expression.cljcindex d06c1b89304..d0602cf54e9 100644--- a/src/metabase/lib/expression.cljc+++ b/src/metabase/lib/expression.cljc@@ -579,9 +579,7 @@(defn- referred-expressions[expr]- (into #{}- (map #(get % 2))- (lib.util.match/match expr :expression)))+ (set (lib.util.match/match-many expr [:expression _opts x & _] x)))(defn- aggregation->name[query stage-number aggregation]@@ -589,9 +587,7 @@(defn- referred-aggregations[agg]- (into #{}- (map #(get % 2))- (lib.util.match/match agg :aggregation)))+ (set (lib.util.match/match agg [:aggregation _opts x & _] x)))(defn- cyclic-definition([node->children]diff --git a/src/metabase/lib/remove_replace.cljc b/src/metabase/lib/remove_replace.cljcindex 680de9e70f8..ec5f1e403e8 100644--- a/src/metabase/lib/remove_replace.cljc+++ b/src/metabase/lib/remove_replace.cljc@@ -374,9 +374,9 @@[query next-stage-number [col replaced-col]](let [target-ref-id (:lib/desired-column-alias col)replaced-ref (lib.ref/ref (assoc replaced-col :lib/source :source/previous-stage))]- (map (fn [target-ref] [target-ref (fresh-ref replaced-ref)])- (lib.util.match/match (lib.util/query-stage query next-stage-number)- [:field _ target-ref-id] &match))))+ (mapv (fn [target-ref] [target-ref (fresh-ref replaced-ref)])+ (lib.util.match/match-many (lib.util/query-stage query next-stage-number)+ [:field _ (id :guard (= id target-ref-id))] &match))))(defn- typed-expression[query stage-number expression]diff --git a/src/metabase/lib/util/match.clj b/src/metabase/lib/util/match.cljindex f3230dac3ba..0d0b4612e1e 100644--- a/src/metabase/lib/util/match.clj+++ b/src/metabase/lib/util/match.clj@@ -437,27 +437,125 @@(defmacro match-lite"Pattern matching macro, simplified version of [[clojure.core.match]].- TODO (Cam 9/16/25) -- what exactly is the difference between this and [[match]]? It doesn't recurse? Someone please- write an explanation here.+ TODO (Sashko 2026-02-01): this macro should eventually supersede all cases of `match`.++ Return a single thing that matches one of the match `clauses` inside `value`. If none of the clauses matched, return+ `nil`. Recurses through maps and sequences. A clause is a pair of a match pattern and a return expression. Usage:- Usage:(match-lite valuepattern1 result1pattern2 result2...)- Patterns can be:+ A pattern can be one of several things:+- symbol - binds the entire value- - keyword or string - must match exactly+ - keyword, string, number, boolean, `nil` - 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.+ - (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.- map - binds associative values inside a map against other patterns.- - (:or clause1 clause2 ...) - special syntax for grouping several alternative conditions that share the same returned value."- {:style/indent :defn}+ - (:or clause1 clause2 ...) - special syntax for grouping several alternative conditions that share the same+ return expression.+ - `_` - matches anything. One usecase for this is to curtail recursive search, thus making the match non-recursive+ (because `_` will always match and its return expression will be returned).++ Examples:++ ;; keyword pattern+ (match-lite {:fields [[:field 10 nil]]} :field) ; -> [:field 10 nil]++ ;; set of keywords+ (match-lite some-query #{:field :expression}) ; -> [:field 10 nil] or [:expression \"wow\"]++ ;; match any `:field` clause with two args (which should be all of them)+ (match-lite some-query [:field _ _])++ ;; match-lite any `:field` clause with integer ID > 100+ (match-lite some-query [:field (num :guard (and (integer? num) (> num 100)))]) ; -> [:field 200 nil]++ ;; symbol naming a predicate function+ ;; match anything that satisfies that predicate+ (match-lite some-query integer?)++ ;; match anything with `_`+ (match-lite 100 `_` :anything) ; -> :anything++ The return expresion can use any of the bindings established in the pattern to compute what should be returned.+ `nil` is a legal return value and prevents other clauses from being checked and matched. The following special forms+ can be used within return expression:++ - `&match` - is bound to the current value being matched.+ - `(&recur <other-value>)` - can be used to re-run just the matching macro on an arbitrarily computed value.++ Examples:++ ;; find vector with exactly 3 items and multiply them+ (match-lite [[[[[10 20 30]]]]] [_ _ _] (reduce * &match)) ; -> 6000++ ;; find innermost :div+ (match-lite [:div [:div [:div \"hello\"]]]+ [:div (nested :guard vector?)] (&recur nested)+ [:div & _] &match)+ ; -> [:div \"hello\"]"[value & clauses](match-lite* value clauses))+(defn- match-many* [value clauses]+ (when (odd? (count clauses))+ (throw (ex-info "match-many requires even number of clauses" {})))+ (let [pairs (partition 2 clauses)+ has-default? (= (first (last pairs)) '_)+ [pairs default] (if has-default?+ [(butlast pairs) (second (last pairs))]+ [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] (if (or recursive? @contains-&recur?)+ ['&match nil]+ ['&match value])+ body (process-clauses pairs value-sym value-binding)]+ `(let [acc# (volatile! [])]+ ((fn ~'&recur [~value-sym]+ (if-some [result# ~(expand-or-some+ (cond-> [body]+ (some? default) (conj default)))]+ ;; Important: a clause may return `nil` to stop further recursive search, yet we don't want that nil to+ ;; wind up in results.+ (do (some->> (metabase.lib.util.match.impl/unwrap-nil result#) (vswap! acc# conj))+ nil)+ (metabase.lib.util.match.impl/match-lite-in-collection ~'&recur ~value-sym)))+ ~value)+ (perf/not-empty @acc#))))++(defmacro match-many+ "Pattern matching macro, returns multiple things that match one of the `clauses` inside `value. See `match-lite` for+ pattern and return expression syntax.++ There are several important characteristics that make `match-many` behavior semantically different from `match-lite`:++ 1. If one or more clauses matched, return a vector of the matched return values. If none of the clauses were+ matched, `nil` is returned instead of `[]`.++ 2. If a return expression returns `nil`, it will not appear in the return list. This can be used to filter results:++ (match some-query [:field (id :guard integer?) _]+ (when (even? id)+ id))+ ;; -> [2 4 6 8]++ Note that returning `nil` still prevents other clauses from being checked, and causes recursion to stop."+ {:style/indent :defn}+ [value & clauses]+ (match-many* value clauses))+(defmacro replace*"Internal implementation for `replace`. Generate a pattern-matching function with `core.match`, and use it to replacematching values in `form`."diff --git a/src/metabase/query_permissions/impl.clj b/src/metabase/query_permissions/impl.cljindex bbde114c0c0..1ee49aeb564 100644--- a/src/metabase/query_permissions/impl.clj+++ b/src/metabase/query_permissions/impl.clj@@ -126,25 +126,25 @@(recur (lib/->legacy-MBQL query) parent-source-card-id in-sandbox?);; already legacy MBQL(apply merge-with merge-source-ids- (lib.util.match/match query- (m :guard (every-pred map? :qp/stage-is-from-source-card))+ (lib.util.match/match-many query+ (m :guard (and (map? m) (:qp/stage-is-from-source-card m)))(merge-with merge-source-ids(when-not parent-source-card-id{:card-ids #{(:qp/stage-is-from-source-card m)}})(query->source-ids (dissoc m :qp/stage-is-from-source-card) (:qp/stage-is-from-source-card m) in-sandbox?))- (m :guard (every-pred map? :query-permissions/sandboxed-table))+ (m :guard (and (map? m) (:query-permissions/sandboxed-table m)))(merge-with merge-source-ids{:table-ids #{(:query-permissions/sandboxed-table m)}}(when-not (or parent-source-card-id in-sandbox?){:table-query-ids #{(:query-permissions/sandboxed-table m)}})(query->source-ids (dissoc m :query-permissions/sandboxed-table :native) parent-source-card-id true))- (m :guard (every-pred map? :native))+ {:native (_ :guard identity)}(when-not parent-source-card-id{:native? true})- (m :guard (every-pred map? #(pos-int? (:source-table %))))+ (m :guard (and (map? m) (pos-int? (:source-table m))))(merge-with merge-source-ids{:table-ids #{(:source-table m)}}(when-not (or parent-source-card-id in-sandbox?)diff --git a/src/metabase/query_processor/middleware/add_implicit_clauses.clj b/src/metabase/query_processor/middleware/add_implicit_clauses.cljindex 73938f86a67..34d73298872 100644--- a/src/metabase/query_processor/middleware/add_implicit_clauses.clj+++ b/src/metabase/query_processor/middleware/add_implicit_clauses.clj@@ -61,8 +61,8 @@(lib.walk/apply-f-for-stage-at-path updated-stage (assoc-in query path stage) path))))))(defn- has-window-function-aggregations? [stage]- (or (lib.util.match/match (mapcat stage [:aggregation :expressions])- #{:cum-sum :cum-count :offset}+ (or (lib.util.match/match-lite (mapcat stage [:aggregation :expressions])+ [#{:cum-sum :cum-count :offset} & _]true);; FIXME(when-let [source-query (:source-query stage)]diff --git a/src/metabase/query_processor/middleware/add_implicit_joins.clj b/src/metabase/query_processor/middleware/add_implicit_joins.cljindex b292eff4f7a..3d9078682ea 100644--- a/src/metabase/query_processor/middleware/add_implicit_joins.clj+++ b/src/metabase/query_processor/middleware/add_implicit_joins.clj@@ -34,8 +34,8 @@[x](into [](distinct)- (lib.util.match/match (dissoc x :lib/stage-metadata)- [:field (_opts :guard (every-pred :source-field (complement :join-alias))) _id-or-oname]+ (lib.util.match/match-many (dissoc x :lib/stage-metadata)+ [:field (opts :guard (and (:source-field opts) (not (:join-alias opts)))) _id-or-oname]&match)))(defn- join-alias [dest-table-name source-fk-field-name source-fk-join-alias]@@ -259,8 +259,8 @@(let [next-stage (get-in query next-path)](when-let [reused-join-aliases (not-empty (::reused-join-aliases next-stage))](when-let [referenced-fields (not-empty- (set (lib.util.match/match (dissoc next-stage :joins :lib/stage-metadata)- :field+ (set (lib.util.match/match-many (dissoc next-stage :joins :lib/stage-metadata)+ [:field & _](when (contains? reused-join-aliases (lib/current-join-alias &match))&match))))](log/debugf "Adding referenced fields from next stage: %s" (pr-str referenced-fields))@@ -306,12 +306,9 @@"Get a set of join aliases that `join` has an immediate dependency on."[join :- ::lib.schema.join/join](set- (lib.util.match/match (:conditions join)- [:field (opts :guard :join-alias) _id-or-name]- (let [join-alias (:join-alias opts)]- (when (and join-alias- (not= join-alias (:alias join)))- join-alias)))))+ (lib.util.match/match-many (:conditions join)+ [:field {:join-alias (join-alias :guard (and join-alias (not= join-alias (:alias join))))} _id-or-name]+ join-alias)))(mu/defn- topologically-sort-joins :- ::lib.schema.join/joins"Sort `joins` by topological dependency order: joins that are referenced by the `:condition` of another will be sorteddiff --git a/src/metabase/query_processor/middleware/auto_bucket_datetimes.clj b/src/metabase/query_processor/middleware/auto_bucket_datetimes.cljindex dd810375063..d279c6314d0 100644--- a/src/metabase/query_processor/middleware/auto_bucket_datetimes.clj+++ b/src/metabase/query_processor/middleware/auto_bucket_datetimes.clj@@ -189,10 +189,10 @@{breakouts :breakout, :keys [filters], :as stage} :- ::lib.schema/stage];; find any breakouts or filters in the query that are just plain `[:field-id ...]` clauses (unwrapped by any other;; clause)- (if-let [unbucketed-clauses (lib.util.match/match (cons filters breakouts)- (_clause :guard (partial should-not-be-autobucketed? query stage-path)) nil- :expression &match- :field &match)]+ (if-let [unbucketed-clauses (lib.util.match/match-many (cons filters breakouts)+ (clause :guard (should-not-be-autobucketed? query stage-path clause)) nil+ [:expression & _] &match+ [:field & _] &match)];; if we found some unbucketed breakouts/filters, fetch the Fields & type info that are referred to by those;; breakouts/filters...(let [unbucketed-fields (filter (comp (partial = :field) first) unbucketed-clauses)diff --git a/src/metabase/query_processor/middleware/binning.clj b/src/metabase/query_processor/middleware/binning.cljindex d47300e08fa..ef9220882bf 100644--- a/src/metabase/query_processor/middleware/binning.clj+++ b/src/metabase/query_processor/middleware/binning.clj@@ -28,8 +28,8 @@(reduce(partial merge-with concat){}- (for [subclause (lib.util.match/match filters #{:between :< :<= :> :>=})- field-id-or-name (lib.util.match/match subclause [:field _opts field-id-or-name] field-id-or-name)]+ (for [subclause (lib.util.match/match-many filters [#{:between :< :<= :> :>=} & _] &match)+ field-id-or-name (lib.util.match/match-many subclause [:field _opts field-id-or-name] field-id-or-name)]{field-id-or-name [subclause]})))(mu/defn- extract-bounds :- [:map [:min-value number?] [:max-value number?]]@@ -42,11 +42,10 @@(let [{global-min :min, global-max :max} (get-in fingerprint [:type :type/Number])filter-clauses (get field-id-or-name->filters field-id-or-name);; [:between <field> <min> <max>] or [:< <field> <x>]- user-maxes (lib.util.match/match filter-clauses- [(_tag :guard #{:< :<= :between}) _opts & args] (last args))- user-mins (lib.util.match/match filter-clauses- [(_tag :guard #{:> :>= :between}) _opts _field min-val & _]- min-val)+ user-maxes (lib.util.match/match-many filter-clauses+ [#{:< :<= :between} _opts & args] (last args))+ user-mins (lib.util.match/match-many filter-clauses+ [#{:> :>= :between} _opts _field min-val & _] min-val)min-value (or (when (seq user-mins)(apply max user-mins))global-min)diff --git a/src/metabase/query_processor/middleware/expand_macros.clj b/src/metabase/query_processor/middleware/expand_macros.cljindex 9c1cbfad49d..20ac17d79e7 100644--- a/src/metabase/query_processor/middleware/expand_macros.clj+++ b/src/metabase/query_processor/middleware/expand_macros.clj@@ -43,8 +43,8 @@(lib.walk/walk-stagesquery(fn [_query _path stage]- (lib.util.match/match stage- [macro-type _opts (id :guard pos-int?)]+ (lib.util.match/match-many stage+ [#{macro-type} _opts (id :guard pos-int?)](conj! ids id))nil))(not-empty (persistent! ids))))diff --git a/src/metabase/query_processor/middleware/metrics.clj b/src/metabase/query_processor/middleware/metrics.cljindex 49b366896c7..0a81afbba5a 100644--- a/src/metabase/query_processor/middleware/metrics.clj+++ b/src/metabase/query_processor/middleware/metrics.clj@@ -160,7 +160,7 @@(defn- find-metric-ids[x]- (lib.util.match/match x+ (lib.util.match/match-many x[:metric _ (id :guard pos-int?)]id))diff --git a/src/metabase/query_processor/util/transformations/nest_breakouts.clj b/src/metabase/query_processor/util/transformations/nest_breakouts.cljindex 20aa373c50c..26c9e502d7b 100644--- a/src/metabase/query_processor/util/transformations/nest_breakouts.clj+++ b/src/metabase/query_processor/util/transformations/nest_breakouts.clj@@ -17,8 +17,8 @@[metabase.util.performance :refer [mapv select-keys some not-empty]]))(defn- stage-has-window-aggregation? [stage]- (lib.util.match/match (:aggregation stage)- #{:cum-sum :cum-count :offset}))+ (lib.util.match/match-lite (:aggregation stage)+ [#{:cum-sum :cum-count :offset} & _] true))(defn- stage-has-breakout? [stage](seq (:breakout stage)))@@ -33,8 +33,8 @@[tag(select-keys opts [:join-alias :temporal-unit :bucketing])id-or-name]))- (lib.util.match/match (concat (:breakout stage) (:aggregation stage) (:expressions stage))- #{:field :expression})))+ (lib.util.match/match-many (concat (:breakout stage) (:aggregation stage) (:expressions stage))+ [#{:field :expression} & _] &match)))(mu/defn- new-first-stage :- ::lib.schema/stage"Remove breakouts, aggregations, order bys, and limit. Add `:fields` to return the things needed by the second stage."diff --git a/src/metabase/warehouses_rest/api.clj b/src/metabase/warehouses_rest/api.cljindex 3f8f5acc572..390efddaa12 100644--- a/src/metabase/warehouses_rest/api.clj+++ b/src/metabase/warehouses_rest/api.clj@@ -153,7 +153,7 @@we can filter it out in Clojure-land."[{query :dataset_query, :as _card} :- [:map[:dataset_query ::queries.schema/query]]]- (lib.util.match/match (lib/aggregations query) #{:cum-count :cum-sum}))+ (lib.util.match/match-lite (lib/aggregations query) [#{:cum-count :cum-sum} & _] true))(defn card-can-be-used-as-source-query?"Does `card`'s query meet the conditions required for it to be used as a source query for another query?"diff --git a/src/metabase/xrays/automagic_dashboards/util.clj b/src/metabase/xrays/automagic_dashboards/util.cljindex 80e8febe168..40668e9be0b 100644--- a/src/metabase/xrays/automagic_dashboards/util.clj+++ b/src/metabase/xrays/automagic_dashboards/util.clj@@ -66,7 +66,7 @@(mu/defn collect-field-references :- [:maybe [:sequential :mbql.clause/field]]"Collect all `:field` references from a given form."[form]- (lib.util.match/match form :field &match))+ (lib.util.match/match-many form [:field & _] &match))(mu/defn ->field :- [:maybe [:and(ms/InstanceOf :model/Field)diff --git a/src/metabase/xrays/domain_entities/core.clj b/src/metabase/xrays/domain_entities/core.cljindex aef4470ec67..9e3007bbcf5 100644--- a/src/metabase/xrays/domain_entities/core.clj+++ b/src/metabase/xrays/domain_entities/core.clj@@ -101,7 +101,7 @@(into (empty entities) ; this way we don't care if we're dealing with a map or a vec(for [entity entities:when (every? (get-in bindings [source :dimensions])- (lib.util.match/match entity [:dimension dimension] dimension))]+ (lib.util.match/match-many entity [:dimension dimension] dimension))](resolve-dimension-clauses bindings source entity))))(mu/defn- instantiate-domain-entity :- ::domain-entities.specs/instantiated-domain-entitydiff --git a/src/metabase/xrays/transforms/specs.clj b/src/metabase/xrays/transforms/specs.cljindex 185f3ec63d0..4338111bbee 100644--- a/src/metabase/xrays/transforms/specs.clj+++ b/src/metabase/xrays/transforms/specs.clj@@ -40,7 +40,8 @@(defn- extract-dimensions[mbql]- (lib.util.match/match (domain-entities.specs/normalize-mbql-clause mbql) [:dimension dimension & _] dimension))+ (lib.util.match/match-many (domain-entities.specs/normalize-mbql-clause mbql)+ [:dimension dimension & _] dimension))(def ^:private ^{:arglists '([m])} stringify-keys(partial m/map-keys name))diff --git a/test/metabase/lib/util/match_test.cljc b/test/metabase/lib/util/match_test.cljcindex 18100157655..c59e145ec39 100644--- a/test/metabase/lib/util/match_test.cljc+++ b/test/metabase/lib/util/match_test.cljc@@ -360,7 +360,7 @@(t/is (= 10 (lib.util.match/match-lite [1 2 3](:or [a] [a b] [a b c]) (* a 10))))(t/is (= nil (lib.util.match/match-lite [1 2 3]- (:or [(a :guard even?) b] [a (b :guard odd?)]) [* a b])))+ (:or [(a :guard even?) b] [a (b :guard odd?)]) (* a b))))(t/testing ":or can mix with other patterns"(t/is (= 20 (lib.util.match/match-lite [1 2 3][a b c d] 10@@ -412,3 +412,21 @@(t/is (= :inside (lib.util.match/match-lite [[[[[[:inside]]]]]][in] (&recur in)_ &match)))))++(t/deftest ^:parallel match-many-test+ (t/is (= [6 15] (lib.util.match/match-many [[1 2 3] [4 5 6]]+ [a b c] (+ a b c))))+ (t/is (= [1 2 3 4 5 6] (lib.util.match/match-many [[1 2 3] [4 5 6]]+ (_ :guard number?) &match)))+ (t/is (= [100] (lib.util.match/match-many [[1 2 3] [4 5 6]]+ (_ :guard keyword?) &match+ _ 100)))++ (t/testing "absent of matches returns nil"+ (t/is (= nil (lib.util.match/match-many [[1 2 3] [4 5 6]]+ (_ :guard keyword?) &match))))++ (t/testing "nils aren't recorded into the result"+ (t/is (= [15] (lib.util.match/match-many [[1 2 3] [4 5 6]]+ [a b c] (when (> a 1)+ (+ a b c)))))))