Skip to content

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

[match-lite] Make match-lite recursive by default (#68658)

Oleksandr Yakushev0ec522227b59
diff --git a/.clj-kondo/config.edn b/.clj-kondo/config.edn
index 4fee2430bd9..f1208dcde82 100644
--- a/.clj-kondo/config.edn
+++ b/.clj-kondo/config.edn
@@ -240,7 +240,6 @@
(metabase.lib.util.match/match)
(metabase.lib.util.match/match-one)
(metabase.lib.util.match/match-lite)
- (metabase.lib.util.match/match-lite-recursive)
(metabase.lib.util.match/replace)
(metabase.lib.util.match/replace-in)
(metabase.driver-api.core/match)
diff --git a/.cljfmt.edn b/.cljfmt.edn
index 2bbcda977ee..bc562ca772b 100644
--- a/.cljfmt.edn
+++ b/.cljfmt.edn
@@ -154,7 +154,6 @@
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-lite-recursive [[: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.cljc
index c3492a45f8d..cc6959b3222 100644
--- a/src/metabase/legacy_mbql/util.cljc
+++ b/src/metabase/legacy_mbql/util.cljc
@@ -85,10 +85,8 @@
{:deprecated "0.57.0"}
[compound-type subclauses]
(mapcat #(lib.util.match/match-lite %
- [(t :guard (= t compound-type)) & args]
- args
- _
- [%])
+ [#{compound-type} & args] args
+ _ [%])
subclauses))
(declare simplify-compound-filter)
@@ -388,7 +386,7 @@
#_{:clj-kondo/ignore [:deprecated-var]}
(lib.util.match/replace m
[clause field & (args :guard (partial some (partial = [:relative-datetime :current])))]
- (let [temporal-unit (or (lib.util.match/match-lite-recursive field
+ (let [temporal-unit (or (lib.util.match/match-lite field
[:field _ {:temporal-unit temporal-unit}] temporal-unit)
:default)]
(into [clause field] (lib.util.match/replace args
diff --git a/src/metabase/lib/expression.cljc b/src/metabase/lib/expression.cljc
index 9dd7c9d669c..c3a9e86fe2e 100644
--- a/src/metabase/lib/expression.cljc
+++ b/src/metabase/lib/expression.cljc
@@ -698,11 +698,11 @@
(-> nested name u/->camelCaseEn u/capitalize-first-char)))
:friendly true})
(when (and (= expression-mode :expression)
- (lib.util.match/match-lite-recursive expr :offset true))
+ (lib.util.match/match-lite expr :offset true))
{:message (i18n/tru "OFFSET is not supported in custom columns")
:friendly true})
(when (and (= expression-mode :filter)
- (lib.util.match/match-lite-recursive expr :offset true))
+ (lib.util.match/match-lite expr :offset true))
{:message (i18n/tru "OFFSET is not supported in custom filters")
:friendly true})
(when (and (lib.schema.common/is-clause? :value expr)
diff --git a/src/metabase/lib/fe_util.cljc b/src/metabase/lib/fe_util.cljc
index 9edd307fff7..4b25feb7678 100644
--- a/src/metabase/lib/fe_util.cljc
+++ b/src/metabase/lib/fe_util.cljc
@@ -330,7 +330,10 @@
;; multiple arguments with options
[(op :guard #{:contains :does-not-contain :starts-with :ends-with}) opts (col-ref :guard string-col?) & (args :guard (every? string? args))]
- (result op col-ref args {:case-sensitive (:case-sensitive opts true)}))))
+ (result op col-ref args {:case-sensitive (:case-sensitive opts true)})
+
+ ;; do not match inner clauses
+ _ nil)))
(def ^:private NumberFilterValue
[:or number? [:fn u.number/bigint?]])
@@ -348,7 +351,9 @@
value
[:value (x :guard (= (:base-type x) :type/BigInteger)) (value :guard string?)]
- (u.number/parse-bigint value)))
+ (u.number/parse-bigint value)
+
+ _ nil))
(def ^:private NumberFilterParts
[:map
@@ -391,7 +396,10 @@
[(op :guard #{:between}) _ (col-ref :guard number-col?) & (args :len 2 :guard (every? number-arg? args))])
{:operator ({:in :=, :not-in :!=} op op)
:column (ref->col col-ref)
- :values (mapv expression-arg->number args)})))
+ :values (mapv expression-arg->number args)}
+
+ ;; do not match inner clauses
+ _ nil)))
(def ^:private CoordinateFilterParts
[:map
@@ -427,7 +435,6 @@
:column (ref->col col-ref)
:values (mapv expression-arg->number args)}
lon-col-ref (assoc :longitude-column (ref->col lon-col-ref))))]
- ;; Separated into two match calls to allow `match-lite` macro to better group things.
(lib.util.match/match-lite filter-clause
(:or
;; multiple arguments, `:=`
@@ -446,7 +453,10 @@
(lat-col-ref :guard coordinate-col?)
(lon-col-ref :guard coordinate-col?)
& (args :len 4 :guard (every? number-arg? args))]
- (result op lat-col-ref lon-col-ref args))))
+ (result op lat-col-ref lon-col-ref args)
+
+ ;; do not match inner clauses
+ _ nil)))
(def ^:private BooleanFilterParts
[:map
@@ -476,7 +486,10 @@
[(op :guard #{:is-null :not-null}) _ (col-ref :guard boolean-col?) & (args :len 0 :guard (every? boolean? args))]
;; exactly 1 argument
[(op :guard #{:=}) _ (col-ref :guard boolean-col?) & (args :len 1 :guard (every? boolean? args))])
- {:operator op, :column (ref->col col-ref), :values (vec args)})))
+ {:operator op, :column (ref->col col-ref), :values (vec args)}
+
+ ;; do not match inner clauses
+ _ nil)))
(def ^:private SpecificDateFilterParts
[:map
@@ -517,7 +530,10 @@
;; exactly 2 arguments
[(op :guard #{:between}) _ (col-ref :guard date-col?) & (args :len 2 :guard (every? string? args))])
- (result op col-ref args))))
+ (result op col-ref args)
+
+ ;; do not match inner clauses
+ _ nil)))
(def ^:private RelativeDateFilterParts
[:map
@@ -573,7 +589,10 @@
:unit unit
:offset-value offset-value
:offset-unit offset-unit
- :options {}})))
+ :options {}}
+
+ ;; do not match inner clauses
+ _ nil)))
(def ^:private ExcludeDateFilterParts
[:map
@@ -628,7 +647,10 @@
;; with `:mode`
[(_ :guard #{:!= :not-in}) _ [:get-day-of-week _ (col-ref :guard date-col?) :iso] & (args :guard (every? int? args))]
- {:operator :!=, :column (ref->col col-ref), :unit :day-of-week, :values args})))
+ {:operator :!=, :column (ref->col col-ref), :unit :day-of-week, :values args}
+
+ ;; do not match inner clauses
+ _ nil)))
(def ^:private TimeFilterParts
[:map
@@ -663,7 +685,10 @@
[(op :guard #{:between}) _ (col-ref :guard time-col?) & (args :len 2 :guard (every? string? args))])
(let [values (mapv u.time/coerce-to-time args)]
(when (every? u.time/valid? values)
- {:operator op, :column (ref->col col-ref), :values values})))))
+ {:operator op, :column (ref->col col-ref), :values values}))
+
+ ;; do not match inner clauses
+ _ nil)))
(def ^:private DefaultFilterParts
[:map
@@ -690,7 +715,9 @@
(not (lib.util/original-isa? % :type/TextLike)))]
(lib.util.match/match-lite filter-clause
[(op :guard #{:is-null :not-null}) _ (col-ref :guard supported-col?)]
- {:operator op, :column (ref->col col-ref)})))
+ {:operator op, :column (ref->col col-ref)}
+
+ _ nil)))
;; ::lib.schema.expression/expression
(def ^:private JoinConditionParts
@@ -714,8 +741,7 @@
{:operator op, :lhs-expression lhs, :rhs-expression rhs}
;; do not match inner clauses
- _
- nil))
+ _ nil))
(mu/defn join-condition-lhs-or-rhs-literal? :- :boolean
"Whether this LHS or RHS expression is a `:value` clause."
diff --git a/src/metabase/lib/filter.cljc b/src/metabase/lib/filter.cljc
index b9cbfe4dbb6..e1d04115e3a 100644
--- a/src/metabase/lib/filter.cljc
+++ b/src/metabase/lib/filter.cljc
@@ -188,7 +188,10 @@
(if (clojure.core/= (count args) 1)
(let [y (first args)]
(i18n/tru "{0} does not contain {1}" (->display-name x) (if (string? y) y (->display-name y))))
- (i18n/tru "{0} does not contain {1} selections" (->display-name x) (count args))))))
+ (i18n/tru "{0} does not contain {1} selections" (->display-name x) (count args)))
+
+ ;; do not match inner clauses
+ _ nil)))
(defmethod lib.metadata.calculation/display-name-method ::binary
[query stage-number expr style]
@@ -212,7 +215,10 @@
(i18n/tru "{0} is greater than {1}" (->display-name x) (->display-name y))
[:>= _ x y]
- (i18n/tru "{0} is greater than or equal to {1}" (->display-name x) (->display-name y)))))
+ (i18n/tru "{0} is greater than or equal to {1}" (->display-name x) (->display-name y))
+
+ ;; do not match inner clauses
+ _ nil)))
(defmethod lib.metadata.calculation/display-name-method :between
[query stage-number expr style]
@@ -248,7 +254,10 @@
(i18n/tru "{0} is between {1} and {2}"
(->display-name x)
(->display-name y)
- (->display-name z)))))
+ (->display-name z))
+
+ ;; do not match inner clauses
+ _ nil)))
(defmethod lib.metadata.calculation/display-name-method :during
[query stage-number [_tag _opts expr value unit] style]
diff --git a/src/metabase/lib/filter/desugar.cljc b/src/metabase/lib/filter/desugar.cljc
index b5b0a39ddf4..d275944e8d5 100644
--- a/src/metabase/lib/filter/desugar.cljc
+++ b/src/metabase/lib/filter/desugar.cljc
@@ -243,7 +243,7 @@
(some (fn [arg]
(lib.util.match/match-one arg [:relative-datetime _opts :current]))
args)))]
- (let [temporal-unit (or (lib.util.match/match-lite-recursive field
+ (let [temporal-unit (or (lib.util.match/match-lite field
[:field {:temporal-unit temporal-unit} _]
temporal-unit)
:default)]
diff --git a/src/metabase/lib/filter/update.cljc b/src/metabase/lib/filter/update.cljc
index 2d2bf5f196c..8897b22d5be 100644
--- a/src/metabase/lib/filter/update.cljc
+++ b/src/metabase/lib/filter/update.cljc
@@ -39,7 +39,7 @@
(defn- contains-ref-for-column? [expr column]
(letfn [(ref-for-column? [expr]
(is-ref-for-column? expr column))]
- (lib.util.match/match-lite-recursive expr
+ (lib.util.match/match-lite expr
(x :guard ref-for-column?) true)))
(mu/defn- remove-existing-filters-against-column* :- ::lib.schema/query
diff --git a/src/metabase/lib/remove_replace.cljc b/src/metabase/lib/remove_replace.cljc
index 50ad40f026c..680de9e70f8 100644
--- a/src/metabase/lib/remove_replace.cljc
+++ b/src/metabase/lib/remove_replace.cljc
@@ -178,7 +178,7 @@
(when-let [clauses (get-in stage location)]
(->> clauses
(keep (fn [clause]
- (lib.util.match/match-lite-recursive clause
+ (lib.util.match/match-lite clause
[(op :guard (= op target-op))
(opts :guard (or (empty? target-opts)
(set/subset? (set target-opts) (set opts))))
diff --git a/src/metabase/lib/schema.cljc b/src/metabase/lib/schema.cljc
index 81f48a8fca6..c9936bec5d5 100644
--- a/src/metabase/lib/schema.cljc
+++ b/src/metabase/lib/schema.cljc
@@ -404,7 +404,7 @@
(let [visible-join-alias? (some-fn visible-join-alias? (visible-join-alias?-fn stage))]
(or
(when (map? stage)
- (lib.util.match/match-lite-recursive (dissoc stage :joins :lib/stage-metadata)
+ (lib.util.match/match-lite (dissoc stage :joins :lib/stage-metadata)
[:field {:join-alias (join-alias :guard (and (some? join-alias)
(not (visible-join-alias? join-alias))))} _id-or-name]
(str "Invalid :field reference in stage " i ": no join named " (pr-str join-alias))))
diff --git a/src/metabase/lib/stage.cljc b/src/metabase/lib/stage.cljc
index a7b8facb351..ccb6eb8275c 100644
--- a/src/metabase/lib/stage.cljc
+++ b/src/metabase/lib/stage.cljc
@@ -167,7 +167,7 @@
;; Only include "late" expressions when required.
;; "Late" expressions those like :offset which can't be used within the same query stage, like aggregations.
:when (or include-late-exprs?
- (not (lib.util.match/match-lite-recursive clause :offset clause)))]
+ (not (lib.util.match/match-lite clause :offset clause)))]
(-> col
(assoc :lib/source :source/expressions, :lib/source-column-alias (:name col))
(u/assoc-default :effective-type (or (:base-type col) :type/*))))))
diff --git a/src/metabase/lib/util/match.clj b/src/metabase/lib/util/match.clj
index 52220b55f65..57775d434ae 100644
--- a/src/metabase/lib/util/match.clj
+++ b/src/metabase/lib/util/match.clj
@@ -385,13 +385,16 @@
collected (collect-common (map :bindings processed) (map :conditions processed))]
(emit-clause collected (mapv :return processed) value-sym value-binding)))
-(defn- match-lite* [value clauses recursive?]
+(defn- match-lite* [value clauses]
(when (odd? (count clauses))
(throw (ex-info "match-lite requires even number of clauses" {})))
(let [pairs (partition 2 clauses)
- [pairs default] (if (= (first (last pairs)) '_)
+ 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?)
;; Wrap explicit nil values.
value (if (nil? value) `(identity nil) value)
[value-sym value-binding] (cond recursive? [(gensym "value") nil]
@@ -431,13 +434,7 @@
- (:or clause1 clause2 ...) - special syntax for grouping several alternative conditions that share the same returned value."
{:style/indent :defn}
[value & clauses]
- (match-lite* value clauses false))
-
-(defmacro match-lite-recursive
- "Like [[match-lite]], but tries to match children recursively if the top-level match failed."
- {:style/indent :defn}
- [value & clauses]
- (match-lite* value clauses true))
+ (match-lite* value clauses))
(defmacro replace*
"Internal implementation for `replace`. Generate a pattern-matching function with `core.match`, and use it to replace
diff --git a/src/metabase/lib/walk/util.cljc b/src/metabase/lib/walk/util.cljc
index f8db7234920..bca7ae92925 100644
--- a/src/metabase/lib/walk/util.cljc
+++ b/src/metabase/lib/walk/util.cljc
@@ -73,9 +73,10 @@
(fn [_query _path-type _stage-or-join-path clause]
(lib.util.match/match-lite clause
[:metric _opts (id :guard pos-int?)]
- (do
- (vswap! metric-ids conj! id)
- nil))))
+ (vswap! metric-ids conj! id)
+
+ _ nil)
+ nil))
(not-empty (persistent! @metric-ids))))
(mu/defn all-segment-ids :- [:maybe [:set {:min 1} ::lib.schema.id/segment]]
@@ -87,9 +88,10 @@
(fn [_query _path-type _stage-or-join-path clause]
(lib.util.match/match-lite clause
[:segment _opts (id :guard pos-int?)]
- (do
- (vswap! segment-ids conj! id)
- nil))))
+ (vswap! segment-ids conj! id)
+
+ _ nil)
+ nil))
(not-empty (persistent! @segment-ids))))
(mu/defn all-measure-ids :- [:maybe [:set {:min 1} ::lib.schema.id/measure]]
@@ -101,9 +103,10 @@
(fn [_query _path-type _stage-or-join-path clause]
(lib.util.match/match-lite clause
[:measure _opts (id :guard pos-int?)]
- (do
- (vswap! measure-ids conj! id)
- nil))))
+ (vswap! measure-ids conj! id)
+
+ _ nil)
+ nil))
(not-empty (persistent! @measure-ids))))
(defn- all-template-tag-card-ids [query]
@@ -164,7 +167,9 @@
walk-clause (fn [clause]
(lib.util.match/match-lite clause
[:field _opts (id :guard pos-int?)]
- (vswap! field-ids conj! id))
+ (vswap! field-ids conj! id)
+
+ _ nil)
nil)]
(if (map? query-or-clause)
(lib.walk/walk-clauses query-or-clause (fn [_query _path-type _stage-or-join-path clause]
@@ -180,7 +185,9 @@
walk-clause (fn [clause]
(lib.util.match/match-lite clause
[:field (opts :guard implicit-join-field-opt?) id]
- (vswap! joined-field-ids conj! id))
+ (vswap! joined-field-ids conj! id)
+
+ _ nil)
nil)]
(if (map? query-or-clause)
(lib.walk/walk-clauses query-or-clause (fn [_query _path-type _path clause]
diff --git a/src/metabase/query_processor/middleware/auto_parse_filter_values.clj b/src/metabase/query_processor/middleware/auto_parse_filter_values.clj
index f156bc48df5..6d8c786bcce 100644
--- a/src/metabase/query_processor/middleware/auto_parse_filter_values.clj
+++ b/src/metabase/query_processor/middleware/auto_parse_filter_values.clj
@@ -46,7 +46,9 @@
(and effective-type
(not (isa? effective-type :type/Text)))))
(v :guard string?)]
- [:value opts (parse-value-for-base-type v (:effective-type opts))]))
+ [:value opts (parse-value-for-base-type v (:effective-type opts))]
+
+ _ nil))
(mu/defn auto-parse-filter-values :- ::lib.schema/query
"Automatically parse String filter clause values to the appropriate type."
diff --git a/src/metabase/query_processor/middleware/binning.clj b/src/metabase/query_processor/middleware/binning.clj
index e4fe4d3a121..d47300e08fa 100644
--- a/src/metabase/query_processor/middleware/binning.clj
+++ b/src/metabase/query_processor/middleware/binning.clj
@@ -107,4 +107,6 @@
;; then do another pass and update `:lib/original-binning` options
[:field (_opts :guard :lib/original-binning) _id-or-name]
- (propagate-original-binning query path clause)))))))
+ (propagate-original-binning query path clause)
+
+ _ nil))))))
diff --git a/src/metabase/query_processor/middleware/cumulative_aggregations.clj b/src/metabase/query_processor/middleware/cumulative_aggregations.clj
index d410a65df5e..a747a939d55 100644
--- a/src/metabase/query_processor/middleware/cumulative_aggregations.clj
+++ b/src/metabase/query_processor/middleware/cumulative_aggregations.clj
@@ -44,7 +44,8 @@
;; cumulative count doesn't necessarily have a field-id arg
[:cum-count opts] [:count opts]
[:cum-count opts field] [:count opts field]
- [:cum-sum opts field] [:sum opts field]))
+ [:cum-sum opts field] [:sum opts field]
+ _ nil))
(defn- update-aggregations [aggregations]
(lib.walk/walk-clauses* aggregations update-clause))
diff --git a/src/metabase/query_processor/middleware/validate_temporal_bucketing.clj b/src/metabase/query_processor/middleware/validate_temporal_bucketing.clj
index 16135494bf8..99835f532fb 100644
--- a/src/metabase/query_processor/middleware/validate_temporal_bucketing.clj
+++ b/src/metabase/query_processor/middleware/validate_temporal_bucketing.clj
@@ -30,4 +30,6 @@
:field clause
:effective-type effective-type
:unit temporal-unit
- :valid-units valid-units})))))))))
+ :valid-units valid-units}))))
+
+ _ nil)))))
diff --git a/src/metabase/query_processor/middleware/wrap_value_literals.clj b/src/metabase/query_processor/middleware/wrap_value_literals.clj
index 5faf6a191b5..d53518d85cd 100644
--- a/src/metabase/query_processor/middleware/wrap_value_literals.clj
+++ b/src/metabase/query_processor/middleware/wrap_value_literals.clj
@@ -256,7 +256,10 @@
[(tag :guard #{:starts-with :ends-with :contains}) opts field (s :guard string?) & more]
(let [s (add-type-info s (*type-info* query path field), :parse-datetime-strings? false)]
- (into [tag opts field s] more))))
+ (into [tag opts field s] more))
+
+ ;; do not match inner clauses
+ _ nil))
(mu/defn wrap-value-literals :- ::lib.schema/query
"Middleware that wraps ran value literals in `:value` (for integers, strings, etc.) or `:absolute-datetime` (for
diff --git a/test/metabase/lib/walk_test.cljc b/test/metabase/lib/walk_test.cljc
index 14f32b9f5d7..9ac421a7cad 100644
--- a/test/metabase/lib/walk_test.cljc
+++ b/test/metabase/lib/walk_test.cljc
@@ -287,7 +287,9 @@
(lib.util.match/match-lite clause
[:field opts id]
(let [col (lib.metadata/field query id)]
- [:field (merge (select-keys col [:base-type]) opts) (:name col)]))))))))
+ [:field (merge (select-keys col [:base-type]) opts) (:name col)])
+
+ _ nil)))))))
(deftest ^:parallel walk-clauses-identity-test
(testing "If we don't update any clauses then we should return the original query"