Skip to content

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

Add &parents to match-lite and rewrite remaining match/match-one usages (#69259)

Oleksandr Yakushev795fa21b9d18
diff --git a/.clj-kondo/config.edn b/.clj-kondo/config.edn
index 1e497cde5fe..40cdcbee38c 100644
--- a/.clj-kondo/config.edn
+++ b/.clj-kondo/config.edn
@@ -253,6 +253,7 @@
(metabase.driver-api.core/match)
(metabase.driver-api.core/match-one)
(metabase.driver-api.core/match-lite)
+ (metabase.driver-api.core/match-many)
(metabase.driver-api.core/replace)
(metabase.driver-api.core/replace-in)
(metabase.query-processor.middleware.cache-backend.interface/with-cached-results)
@@ -306,6 +307,8 @@
metabase.lib.convert/->legacy-MBQL {:message "Conversion to legacy MBQL is discouraged, please use Lib to manipulate MBQL queries"}
metabase.lib.core/->legacy-MBQL {:message "Conversion to legacy MBQL is discouraged, please use Lib to manipulate MBQL queries"}
metabase.lib.equality/find-column-indexes-for-refs {:message "Use lib.equality/closest-matches-in-metadata or lib.equality/find-closest-matches-for-refs instead of lib.equality/find-column-indexes-for-refs"}
+ metabase.lib.util.match/match {:message "Use lib.util.match/match-many instead of lib.util.match/match."}
+ metabase.lib.util.match/match-one {:message "Use lib.util.match/match-lite instead of lib.util.match/match-one."}
metabase-enterprise.transforms.interface/execute! {:message "Use metabase-enterprise.transforms.execute/execute! instead of metabase-enterprise.transforms.interface/execute!"}
next.jdbc/active-tx? {:message "Use metabase.app-db.core/in-transaction? to see if we are in a transaction"}
toucan2.core/debug {:message "Don't commit code using t2/debug"}
diff --git a/.cljfmt.edn b/.cljfmt.edn
index f8499bfc4a4..884ec5ceedf 100644
--- a/.cljfmt.edn
+++ b/.cljfmt.edn
@@ -121,6 +121,7 @@
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/match-many [[: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/modules/drivers/mongo/src/metabase/driver/mongo/query_processor.clj b/modules/drivers/mongo/src/metabase/driver/mongo/query_processor.clj
index 8455c58154f..6fadd5dbf42 100644
--- a/modules/drivers/mongo/src/metabase/driver/mongo/query_processor.clj
+++ b/modules/drivers/mongo/src/metabase/driver/mongo/query_processor.clj
@@ -1076,8 +1076,8 @@ 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 condition
- [:field _ (_ :guard #(not= (:join-alias %) alias))])
+ own-fields (driver-api/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))
;; ~ in let aliases provokes a parse error in Mongo. For correct function,
@@ -1663,13 +1663,12 @@ function(bin) {
(defn- query->collection-name
"Return `:collection` from a source query, if it exists."
[query]
- (driver-api/match-one query
- (_ :guard (every-pred map? :collection))
+ (driver-api/match-lite query
+ {:collection (collection :guard identity)}
;; ignore source queries inside `:joins` or `:collection` outside of a `:source-query`
- (when (let [parents (set &parents)]
- (and (contains? parents :source-query)
- (not (contains? parents :joins))))
- (:collection &match))))
+ (when (and (some #{:source-query} &parents)
+ (not (some #{:joins} &parents)))
+ collection)))
(defn- log-aggregation-pipeline [form]
(when-not driver-api/*disable-qp-logging*
diff --git a/modules/drivers/redshift/src/metabase/driver/redshift.clj b/modules/drivers/redshift/src/metabase/driver/redshift.clj
index d50103a0f84..76325406788 100644
--- a/modules/drivers/redshift/src/metabase/driver/redshift.clj
+++ b/modules/drivers/redshift/src/metabase/driver/redshift.clj
@@ -550,9 +550,9 @@
(if (contains? param :name)
[(:name param) (:value param)]
- (when-let [field-id (driver-api/match-one param
+ (when-let [field-id (driver-api/match-lite param
[:field (field-id :guard integer?) _]
- (when (contains? (set &parents) :dimension)
+ (when (perf/some #{:dimension} &parents)
field-id))]
[(:name (driver-api/field (driver-api/metadata-provider) field-id))
(:value param)]))))
diff --git a/src/metabase/driver_api/core.clj b/src/metabase/driver_api/core.clj
index 6b6fc7b6040..38b286540c3 100644
--- a/src/metabase/driver_api/core.clj
+++ b/src/metabase/driver_api/core.clj
@@ -108,6 +108,7 @@
lib.util.match/match
lib.util.match/match-one
lib.util.match/match-lite
+ lib.util.match/match-many
lib.util.match/replace
lib/truncate-alias
lib/->legacy-MBQL
diff --git a/src/metabase/lib/expression.cljc b/src/metabase/lib/expression.cljc
index d0602cf54e9..b761d621300 100644
--- a/src/metabase/lib/expression.cljc
+++ b/src/metabase/lib/expression.cljc
@@ -587,7 +587,7 @@
(defn- referred-aggregations
[agg]
- (set (lib.util.match/match agg [:aggregation _opts x & _] x)))
+ (set (lib.util.match/match-many agg [:aggregation _opts x & _] x)))
(defn- cyclic-definition
([node->children]
diff --git a/src/metabase/lib/query.cljc b/src/metabase/lib/query.cljc
index 41128ce9561..5f5b9c87236 100644
--- a/src/metabase/lib/query.cljc
+++ b/src/metabase/lib/query.cljc
@@ -127,10 +127,10 @@
"Add `:base-type` and `:effective-type` to options of fields in `x` using `metadata-provider`. Works on pmbql fields.
`:effective-type` is required for coerced fields to pass schema checks."
[x metadata-provider :- ::lib.schema.metadata/metadata-provider]
- (if-let [field-ids (lib.util.match/match x
+ (if-let [field-ids (lib.util.match/match-many x
[:field
- (_options :guard (every-pred map? (complement (every-pred :base-type :effective-type))))
- (id :guard integer? pos?)]
+ (_opts :guard (and (map? _opts) (not (and (:base-type _opts) (:effective-type _opts)))))
+ (id :guard (and (integer? id) (pos? id)))]
(when-not (some #{:mbql/stage-metadata} &parents)
id))]
;; "pre-warm" the metadata provider
diff --git a/src/metabase/lib/util/match.clj b/src/metabase/lib/util/match.clj
index 0d0b4612e1e..6da283f245f 100644
--- a/src/metabase/lib/util/match.clj
+++ b/src/metabase/lib/util/match.clj
@@ -221,7 +221,7 @@
:symbol pattern}
;; Guard pattern
- (and (list? pattern) (symbol? (first pattern)) (>= (count pattern) 3))
+ (and (seq? pattern) (symbol? (first pattern)) (>= (count pattern) 3))
(let [preds (apply hash-map (rest pattern))]
(cond-> {:type :guard
:symbol (nth pattern 0)
@@ -230,7 +230,7 @@
(:len preds) (assoc :length (:len preds))))
;; (:or ...) pattern
- (and (list? pattern) (= (first pattern) :or) (>= (count pattern) 2))
+ (and (seq? pattern) (= (first pattern) :or) (>= (count pattern) 2))
{:type :or
:clauses (rest pattern)}
@@ -398,6 +398,21 @@
collected (collect-common (map :bindings processed) (map :conditions processed))]
(emit-clause collected (mapv :return processed) value-sym value-binding)))
+(defn- contains-symbol? [form sym]
+ (let [found (volatile! false)]
+ (perf/postwalk #(when (= % sym) (vreset! found true)) form)
+ @found))
+
+(defn- rewrite-&recur
+ "Replace any `&recur` forms with ones that include the implicit `&parents` arg."
+ [form]
+ (perf/postwalk
+ (fn [form]
+ (if (and (seq? form) (= '&recur (first form)))
+ (list '&recur (second form) '&parents)
+ form))
+ form))
+
(defn- match-lite* [value clauses]
(when (odd? (count clauses))
(throw (ex-info "match-lite requires even number of clauses" {})))
@@ -408,37 +423,34 @@
[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))
+ ;; Search for &recur and &parents usage in clauses.
+ contains-&recur? (contains-symbol? pairs '&recur)
+ contains-&parents? (contains-symbol? pairs '&parents)
+ pairs (rewrite-&recur 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)]
- (if recursive?
- `((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)))))
+ value-binding (when-not (or recursive? contains-&recur?) value)
+ body `(metabase.lib.util.match.impl/unwrap-nil
+ ~(expand-or-some
+ (cond-> [(process-clauses pairs '&match value-binding)]
+ (some? default) (conj default)
+ recursive? (conj `(metabase.lib.util.match.impl/match-lite-in-collection ~'&recur ~'&match ~'&parents)))))]
+ (if (or recursive? contains-&recur?)
+ `((fn ~'&recur [~'&match ~'&parents]
+ ~body)
+ ~value
+ ;; Only set &parents to [] if user code uses &parents, otherwise keep it `nil` to avoid unnecessary updates.
+ ~(when contains-&parents? []))
+ body)))
(defmacro match-lite
"Pattern matching macro, simplified version of [[clojure.core.match]].
TODO (Sashko 2026-02-01): this macro should eventually supersede all cases of `match`.
+ NB: unlike previously used `lib.util.match/match`, this version doesn't support using `:tag` to match `[:tag & _]`.
+ You should write out the vector explicitly.
+
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:
@@ -488,6 +500,7 @@
can be used within return expression:
- `&match` - is bound to the current value being matched.
+ - `&parents` - is bound to a vector of keywords that describe the path of the current form in the data structure.
- `(&recur <other-value>)` - can be used to re-run just the matching macro on an arbitrarily computed value.
Examples:
@@ -495,6 +508,10 @@
;; find vector with exactly 3 items and multiply them
(match-lite [[[[[10 20 30]]]]] [_ _ _] (reduce * &match)) ; -> 6000
+ ;; returns [:div :a :href]
+ (match-lite [:div [:a {:href \"hello\"}]]
+ string? &parents)
+
;; find innermost :div
(match-lite [:div [:div [:div \"hello\"]]]
[:div (nested :guard vector?)] (&recur nested)
@@ -513,17 +530,16 @@
[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))
+ ;; Search for &recur and &parents usage in clauses.
+ contains-&recur? (contains-symbol? pairs '&recur)
+ contains-&parents? (contains-symbol? pairs '&parents)
+ pairs (rewrite-&recur 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)]
+ value-binding (when-not (or recursive? contains-&recur?) value)
+ body (process-clauses pairs '&match value-binding)]
`(let [acc# (volatile! [])]
- ((fn ~'&recur [~value-sym]
+ ((fn ~'&recur [~'&match ~'&parents]
(if-some [result# ~(expand-or-some
(cond-> [body]
(some? default) (conj default)))]
@@ -531,8 +547,9 @@
;; 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)
+ (metabase.lib.util.match.impl/match-lite-in-collection ~'&recur ~'&match ~'&parents)))
+ ~value
+ ~(when contains-&parents? []))
(perf/not-empty @acc#))))
(defmacro match-many
diff --git a/src/metabase/lib/util/match/impl.cljc b/src/metabase/lib/util/match/impl.cljc
index 8c3d5ab59d3..a2e51617175 100644
--- a/src/metabase/lib/util/match/impl.cljc
+++ b/src/metabase/lib/util/match/impl.cljc
@@ -95,19 +95,26 @@
[value]
(when-not (= value ::wrapped-nil) value))
+(defn- maybe-add-to-parents [clause-parents k]
+ (if (and clause-parents k)
+ (conj clause-parents k)
+ clause-parents))
+
(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]
+ "Internal impl for `match-lite`. If `form` is a collection, call `match-fn` to recursively look for matches in it.
+ `clause-parents` is a sequence of keywords naming the parent top-level keys and clauses of the match."
+ [match-fn form clause-parents]
{:pre [(fn? match-fn)]}
(cond
(map? form)
- (reduce-kv (fn [_ _ v]
- (when-some [match (match-fn v)]
+ (reduce-kv (fn [_ k v]
+ (when-some [match (match-fn v (maybe-add-to-parents clause-parents k))]
(reduced match)))
nil form)
(sequential? form)
(reduce (fn [_ v]
- (when-some [match (match-fn v)]
- (reduced match)))
+ (let [fst (first form)]
+ (when-some [match (match-fn v (maybe-add-to-parents clause-parents (when (keyword? fst) fst)))]
+ (reduced match))))
nil form)))
diff --git a/test/metabase/channel/render/card_test.clj b/test/metabase/channel/render/card_test.clj
index 4240a8a9e37..619b478e20f 100644
--- a/test/metabase/channel/render/card_test.clj
+++ b/test/metabase/channel/render/card_test.clj
@@ -311,7 +311,7 @@
(qp/process-query (:dataset_query card))
{:channel.render/include-title? true}))
expected-href (format "https://mb.com/dashboard/%d#scrollTo=%d" (:dashboard_id dc1) (:id dc1))]
- (is (every? true? (map #(= (:href %) expected-href) (lib.util.match/match rendered-card-content {:href _}))))))))
+ (is (every? #(= % expected-href) (lib.util.match/match-many rendered-card-content {:href href} href)))))))
(testing "the title and body hrefs for visualizer cards should be of the form '.../dashboard/<DASHBOARD_ID>#scrollTo=<DASHBOARD_CARD_ID>'"
(mt/with-temp [:model/Card card {:name "A Card"
:dataset_query (mt/mbql-query venues {:limit 1})}
@@ -325,4 +325,4 @@
(qp/process-query (:dataset_query card))
{:channel.render/include-title? true}))
expected-href (format "https://mb.com/dashboard/%d#scrollTo=%d" (:dashboard_id dc1) (:id dc1))]
- (is (every? true? (map #(= (:href %) expected-href) (lib.util.match/match rendered-card-content {:href _})))))))))
+ (is (every? #(= % expected-href) (lib.util.match/match-many rendered-card-content {:href href} href))))))))
diff --git a/test/metabase/lib/util/match_test.cljc b/test/metabase/lib/util/match_test.cljc
index c59e145ec39..2b313b7e579 100644
--- a/test/metabase/lib/util/match_test.cljc
+++ b/test/metabase/lib/util/match_test.cljc
@@ -1,4 +1,7 @@
(ns metabase.lib.util.match-test
+ {:clj-kondo/config '{:linters
+ {:discouraged-var {metabase.lib.util.match/match {:level :off}
+ metabase.lib.util.match/match-one {:level :off}}}}}
(:require
[clojure.test :as t]
[metabase.lib.util.match :as lib.util.match]))
@@ -430,3 +433,12 @@
(t/is (= [15] (lib.util.match/match-many [[1 2 3] [4 5 6]]
[a b c] (when (> a 1)
(+ a b c)))))))
+
+(t/deftest ^:parallel parents-tests
+ (t/is (= [:foo :bar :baz :qux]
+ (lib.util.match/match-lite [:foo [:bar [:baz {:qux 42}]]]
+ 42 &parents)))
+ (t/is (= [[:foo :bar :baz :qux]
+ [:foo :bar :baz :corge]]
+ (lib.util.match/match-many [:foo [:bar [:baz {:qux 42 :corge 42}]]]
+ 42 &parents))))
diff --git a/test/metabase/query_processor/util/add_alias_info_test.clj b/test/metabase/query_processor/util/add_alias_info_test.clj
index b33ad773612..939790c164f 100644
--- a/test/metabase/query_processor/util/add_alias_info_test.clj
+++ b/test/metabase/query_processor/util/add_alias_info_test.clj
@@ -1098,10 +1098,10 @@
:condition [:= $product-id &PRODUCTS__via__PRODUCT_ID.products.id]}]}))]
(is (=? [[:expression {::add/source-table ::add/none, ::add/desired-alias "pivot-grouping"} "pivot-grouping"]
[:expression {::add/source-table ::add/none, ::add/desired-alias "pivot-grouping"} "pivot-grouping"]]
- (lib.util.match/match (-> query
- add/add-alias-info
- qp.preprocess/preprocess)
- :expression))))))
+ (lib.util.match/match-many (-> query
+ add/add-alias-info
+ qp.preprocess/preprocess)
+ [:expression & _] &match))))))
(deftest ^:parallel remapped-columns-in-joined-source-queries-test
(testing "Make sure remapped columns are given correct aliases and escaped correctly for drivers like Oracle"
diff --git a/test/metabase/xrays/automagic_dashboards/core_test.clj b/test/metabase/xrays/automagic_dashboards/core_test.clj
index c129b9a16c9..69d0664f710 100644
--- a/test/metabase/xrays/automagic_dashboards/core_test.clj
+++ b/test/metabase/xrays/automagic_dashboards/core_test.clj
@@ -623,8 +623,8 @@
:let [query (get-in dashcard [:card :dataset_query])]
:when query
:let [breakouts (lib/breakouts query)]
- id (lib.util.match/match breakouts
- [:field (_opts :guard :binning) (id :guard pos-int?)]
+ id (lib.util.match/match-many breakouts
+ [:field {:binning _} (id :guard pos-int?)]
id)]
id)))))))))))
@@ -657,8 +657,8 @@
:let [query (get-in dashcard [:card :dataset_query])]
:when query
:let [breakouts (lib/breakouts query)]
- id (lib.util.match/match breakouts
- [:field (_opts :guard :temporal-unit) (id :guard pos-int?)]
+ id (lib.util.match/match-many breakouts
+ [:field {:temporal-unit _} (id :guard pos-int?)]
id)]
id)]
(ensure-single-table-sourced (mt/id :products) dashboard)