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
[match] Implement replace-lite and use it instead of replace (#69270)
Oleksandr Yakushev3f898be45a5b
diff --git a/.clj-kondo/config.edn b/.clj-kondo/config.ednindex 40cdcbee38c..121ab6d0dec 100644--- a/.clj-kondo/config.edn+++ b/.clj-kondo/config.edn@@ -228,7 +228,8 @@:invalid-arity {:skip-args [metabase.lib.util.match/matchmetabase.lib.util.match/match-lite- metabase.lib.util.match/match-many]} ; TODO (cam): can we fix these?+ metabase.lib.util.match/match-many+ metabase.lib.util.match/replace-lite]} ; TODO (cam): can we fix these?:refer-all {:level :warning, :exclude [clojure.test]}:unused-referred-var {:exclude {compojure.core [GET DELETE POST PUT]}}@@ -250,12 +251,14 @@(metabase.lib.util.match/match-many)(metabase.lib.util.match/replace)(metabase.lib.util.match/replace-in)+ (metabase.lib.util.match/replace-lite)(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.driver-api.core/replace-lite)(metabase.query-processor.middleware.cache-backend.interface/with-cached-results)(metabase.util.regex/rx [opt])(metabase.util/prog1 [<>])@@ -309,6 +312,7 @@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.lib.util.match/replace {:message "Use lib.util.match/replace-lite instead of lib.util.match/replace."}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.ednindex 884ec5ceedf..917375aefbc 100644--- a/.cljfmt.edn+++ b/.cljfmt.edn@@ -123,6 +123,7 @@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-lite [[:inner 0]]metabase.driver-api.core/replace-in [[:inner 0]]metabase.driver.bigquery-cloud-sdk.query-processor/with-temporal-type [[:default]]metabase.driver.druid.client/DELETE [[:default]]@@ -160,6 +161,7 @@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-lite [[:inner 0]]metabase.lib.util.match/replace-in [[:inner 0]]metabase.models.interface/define-batched-hydration-method [[:inner 0]]metabase.models.interface/define-simple-hydration-method [[:inner 0]]diff --git a/deps.edn b/deps.ednindex 3fd8bdf8bfa..64d2522efd3 100644--- a/deps.edn+++ b/deps.edn@@ -617,6 +617,9 @@:implicit-dependencies;; too many false positives for now:unused-ret-vals+ ;; hard to clear on for lib.util.match macros, and disabling the linter just there+ ;; doesn't seem to work+ :unused-meta-on-macro;; Kondo lints this for us anyway, and this isn't as easy to configure.:wrong-arity;; Kondo lints this for us anyway, and this isn't as easy to configure.diff --git a/dev/src/dev/debug_qp.clj b/dev/src/dev/debug_qp.cljindex 1ef4789bdcc..31bd9f3c4b2 100644--- a/dev/src/dev/debug_qp.clj+++ b/dev/src/dev/debug_qp.clj@@ -7,6 +7,8 @@;; run a query with debugging enabled(binding [metabase.query-processor.debug/*debug* true](metabase.query-processor/process-query query))"+ {:clj-kondo/config '{:linters+ {:discouraged-var {metabase.lib.util.match/replace {:level :off}}}}}(:require[clojure.java.io :as io][clojure.string :as str]@@ -123,25 +125,25 @@(symbol (format "#_\"%s.%s\"" field-name table-name)))))(field-id->name-form [field-id](list 'do (add-name-to-field-id field-id) field-id))]- (lib.util.match/replace form+ (lib.util.match/replace-lite form[:field (id :guard pos-int?) opts][:field id (add-name-to-field-id id) (cond-> opts(pos-int? (:source-field opts))(update :source-field field-id->name-form))]- (m :guard (every-pred map? (comp pos-int? :source-table)))+ (m :guard (and (map? m) (pos-int? (:source-table m))))(add-names* (update m :source-table add-table-id-name))- (m :guard (every-pred map? (comp pos-int? :metabase.query-processor.util.add-alias-info/source-table)))+ (m :guard (and (map? m) (pos-int? (:metabase.query-processor.util.add-alias-info/source-table m))))(add-names* (update m :metabase.query-processor.util.add-alias-info/source-table add-table-id-name))- (m :guard (every-pred map? (comp pos-int? :fk-field-id)))+ (m :guard (and (map? m) (pos-int? (:fk-field-id m))))(-> m(update :fk-field-id field-id->name-form)add-names*);; don't recursively replace the `do` lists above, other we'll get vectors.- (_ :guard (every-pred list? #(= (first %) 'do)))+ (l :guard (and (seq? l) (= (first l) 'do)))&match)))x)->sorted-mbql-query-map))@@ -343,10 +345,10 @@e)))))(defn- no-$ [x]- (lib.util.match/replace x [::$ & args] (into [::no-$] args)))+ (lib.util.match/replace-lite x [::$ & args] (into [::no-$] args)))(defn- symbolize [form]- (lib.util.match/replace form+ (lib.util.match/replace-lite form[::-> x y](symbol (format "%s->%s" (symbolize x) (str/replace (symbolize y) #"^\$" "")))diff --git a/enterprise/backend/test/metabase_enterprise/advanced_permissions/query_processor/middleware/permissions_test.clj b/enterprise/backend/test/metabase_enterprise/advanced_permissions/query_processor/middleware/permissions_test.cljindex 547da8625f0..6a6268325f9 100644--- a/enterprise/backend/test/metabase_enterprise/advanced_permissions/query_processor/middleware/permissions_test.clj+++ b/enterprise/backend/test/metabase_enterprise/advanced_permissions/query_processor/middleware/permissions_test.clj@@ -33,8 +33,8 @@(f)))))(defn- remove-metadata [m]- (lib.util.match/replace m- (_ :guard (every-pred map? :source-metadata))+ (lib.util.match/replace-lite m+ {:source-metadata _}(remove-metadata (dissoc &match :source-metadata))))(mu/defn- apply-row-level-permissions [query :- ::lib.schema/query]diff --git a/enterprise/backend/test/metabase_enterprise/sandbox/query_processor/middleware/sandboxing_test.clj b/enterprise/backend/test/metabase_enterprise/sandbox/query_processor/middleware/sandboxing_test.cljindex 831cfd2d38c..83b815ba33e 100644--- a/enterprise/backend/test/metabase_enterprise/sandbox/query_processor/middleware/sandboxing_test.clj+++ b/enterprise/backend/test/metabase_enterprise/sandbox/query_processor/middleware/sandboxing_test.clj@@ -162,8 +162,8 @@;; TODO -- #19754 adds [[mt/remove-source-metadata]] that can be used here (once it gets merged)(defn- remove-metadata [m]- (lib.util.match/replace m- (_ :guard (every-pred map? :source-metadata))+ (lib.util.match/replace-lite m+ {:source-metadata _}(remove-metadata (dissoc &match :source-metadata))))(defn- apply-row-level-permissions [query]diff --git a/modules/drivers/druid/src/metabase/driver/druid/query_processor.clj b/modules/drivers/druid/src/metabase/driver/druid/query_processor.cljindex 347a050319b..e7b98f131aa 100644--- a/modules/drivers/druid/src/metabase/driver/druid/query_processor.clj+++ b/modules/drivers/druid/src/metabase/driver/druid/query_processor.clj@@ -306,8 +306,8 @@(defn- parse-filter [filter-clause];; strip out all the filters against temporal fields. Those are handled separately, as intervals- (-> (driver-api/replace filter-clause- [_ [:field _ (_ :guard :temporal-unit)] & _]+ (-> (driver-api/replace-lite filter-clause+ [_ [:field _ {:temporal-unit (_ :guard identity)}] & _]nil);; TODO (Cam 8/18/25) -- I am 90% sure this is serving no useful purpose.#_{:clj-kondo/ignore [:deprecated-var]}@@ -328,7 +328,7 @@"Adding `n` `:default` units doesn't make sense. So if an `:absoulte-datetime` has `:default` as its unit, add `n`milliseconds, because that is the smallest unit Druid supports."[clause n]- (driver-api/replace clause+ (driver-api/replace-lite clause[:absolute-datetime t :default][:absolute-datetime (u.date/add t :millisecond n) :millisecond]@@ -703,7 +703,7 @@(update :query (partial merge-with concat) ag-clauses))))))(defn- deduplicate-aggregation-options [expression]- (driver-api/replace expression+ (driver-api/replace-lite expression[:aggregation-options [:aggregation-options ag options-1] options-2][:aggregation-options ag (merge options-1 options-2)]))@@ -712,7 +712,7 @@(defn- add-expression-aggregation-output-names[expression]- (driver-api/replace expression+ (driver-api/replace-lite expression[:aggregation-options ag options](deduplicate-aggregation-options [:aggregation-options (add-expression-aggregation-output-names ag) options])diff --git a/modules/drivers/mongo/src/metabase/driver/mongo/query_processor.clj b/modules/drivers/mongo/src/metabase/driver/mongo/query_processor.cljindex 6fadd5dbf42..d154adfccac 100644--- a/modules/drivers/mongo/src/metabase/driver/mongo/query_processor.clj+++ b/modules/drivers/mongo/src/metabase/driver/mongo/query_processor.clj@@ -1045,8 +1045,8 @@ function(bin) {"Rename :join-alias properties fields to ::join-local.See [[find-mapped-field-name]] for an explanation why this is done."[expr alias]- (driver-api/replace expr- [:field _ {:join-alias alias}]+ (driver-api/replace-lite expr+ [:field _ {:join-alias (a :guard (= a alias))}](update &match 2 set/rename-keys {:join-alias ::join-local})))(defn- get-field-mappings [source-query projections]@@ -1761,14 +1761,14 @@ function(bin) {source-alias)[:field source-alias opts][:field id-or-name opts])))]- (driver-api/replace form- :field+ (driver-api/replace-lite form+ [:field & _](update-field-ref &match)- (join :guard (every-pred map?- driver-api/qp.add.alias- #(not= (driver-api/qp.add.alias %) (:alias %))))- (recur (assoc join :alias (driver-api/qp.add.alias join))))))+ (join :guard (and (map? join)+ (driver-api/qp.add.alias join)+ (not= (driver-api/qp.add.alias join) (:alias join))))+ (&recur (assoc join :alias (driver-api/qp.add.alias join))))))(defn- preprocess[inner-query]diff --git a/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj b/modules/drivers/sqlserver/src/metabase/driver/sqlserver.cljindex ec8c4734ea0..e9a0b947f85 100644--- a/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj+++ b/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj@@ -863,12 +863,12 @@(and (has-order-by-without-limit? m)(not (in-join-source-query? path))(in-source-query? path)))]- (driver-api/replace inner-query+ (driver-api/replace-lite inner-query;; remove order by and then recurse in case we need to do more transformations at another level- (m :guard (partial remove-order-by? &parents))+ (m :guard (remove-order-by? &parents m))(fix-order-bys (dissoc m :order-by))- (m :guard (partial add-limit? &parents))+ (m :guard (add-limit? &parents m))(fix-order-bys (assoc m :limit driver-api/absolute-max-results)))))(defmethod sql.qp/preprocess :sqlserverdiff --git a/src/metabase/driver/sql/query_processor.clj b/src/metabase/driver/sql/query_processor.cljindex 017a8acbc01..3705775aba7 100644--- a/src/metabase/driver/sql/query_processor.clj+++ b/src/metabase/driver/sql/query_processor.clj@@ -1479,7 +1479,7 @@([form](rewrite-fields-to-force-using-column-aliases form {:is-breakout false}))([form {is-breakout :is-breakout}]- (driver-api/replace+ (driver-api/replace-liteform[:field id-or-name opts][:field id-or-name (cond-> optsdiff --git a/src/metabase/driver_api/core.clj b/src/metabase/driver_api/core.cljindex 38b286540c3..317797ddc24 100644--- a/src/metabase/driver_api/core.clj+++ b/src/metabase/driver_api/core.clj@@ -110,6 +110,7 @@lib.util.match/match-litelib.util.match/match-manylib.util.match/replace+ lib.util.match/replace-litelib/truncate-aliaslib/->legacy-MBQLlib/->metadata-providerdiff --git a/src/metabase/legacy_mbql/jvm_util.clj b/src/metabase/legacy_mbql/jvm_util.cljindex cd4660f9d32..f59ce63f5d3 100644--- a/src/metabase/legacy_mbql/jvm_util.clj+++ b/src/metabase/legacy_mbql/jvm_util.clj@@ -10,13 +10,13 @@"Unwrap host and domain."{:deprecated "0.57.0"}[expression]- (lib.util.match/replace+ (lib.util.match/replace-liteexpression[:host column]- (recur [:regex-match-first column (str lib.filter.desugar.jvm/host-regex)])+ (&recur [:regex-match-first column (str lib.filter.desugar.jvm/host-regex)])[:domain column]- (recur [:regex-match-first column (str lib.filter.desugar.jvm/domain-regex)])+ (&recur [:regex-match-first column (str lib.filter.desugar.jvm/domain-regex)])[:subdomain column]- (recur [:regex-match-first column (str lib.filter.desugar.jvm/subdomain-regex)])+ (&recur [:regex-match-first column (str lib.filter.desugar.jvm/subdomain-regex)])[:path column]- (recur [:regex-match-first column (str lib.filter.desugar.jvm/path-regex)])))+ (&recur [:regex-match-first column (str lib.filter.desugar.jvm/path-regex)])))diff --git a/src/metabase/legacy_mbql/schema.cljc b/src/metabase/legacy_mbql/schema.cljcindex 55178b6b26d..33d3cd2ba56 100644--- a/src/metabase/legacy_mbql/schema.cljc+++ b/src/metabase/legacy_mbql/schema.cljc@@ -944,15 +944,15 @@(defn- replace-exclude-date-filters"Replaces legacy exclude date filter clauses that rely on temporal bucketing with `:temporal-extract` function calls."[filter-clause]- (lib.util.match/replace filter-clause+ (lib.util.match/replace-lite filter-clause[:!=- [:field id-or-name (opts :guard #(= (:temporal-unit %) :hour-of-day))]- & (args :guard #(every? number? %))]+ [:field id-or-name (opts :guard (= (:temporal-unit opts) :hour-of-day))]+ & (args :guard (every? number? args))](into [:!= [:get-hour [:field id-or-name (not-empty (dissoc opts :temporal-unit))]]] args)[:!=- [:field id-or-name (opts :guard #(#{:day-of-week :month-of-year :quarter-of-year} (:temporal-unit %)))]- & (args :guard #(every? u.time/timestamp-coercible? %))]+ [:field id-or-name (opts :guard (#{:day-of-week :month-of-year :quarter-of-year} (:temporal-unit opts)))]+ & (args :guard (every? u.time/timestamp-coercible? args))](let [args (mapv u.time/coerce-to-timestamp args)](if (every? u.time/valid? args)(let [unit (:temporal-unit opts)@@ -1009,7 +1009,7 @@expression and convert it to a `:relative-time-interval` call, honoring the original user intent. See #46211 and#46438 for details."[clause]- (lib.util.match/replace clause+ (lib.util.match/replace-lite clause[:between[:+fielddiff --git a/src/metabase/legacy_mbql/util.cljc b/src/metabase/legacy_mbql/util.cljcindex 5a2330e3939..7fc8690981e 100644--- a/src/metabase/legacy_mbql/util.cljc+++ b/src/metabase/legacy_mbql/util.cljc@@ -160,7 +160,7 @@"Rewrite `:inside` filter clauses as a pair of `:between` clauses."{:deprecated "0.57.0"}[m]- (lib.util.match/replace m+ (lib.util.match/replace-lite m[:inside lat-field lon-field lat-max lon-min lat-min lon-max][:and[:between lat-field lat-min lat-max]@@ -170,7 +170,7 @@"Rewrite `:is-null` and `:not-null` filter clauses as simpler `:=` and `:!=`, respectively."{:deprecated "0.57.0"}[m]- (lib.util.match/replace m+ (lib.util.match/replace-lite m[:is-null field] [:= field nil][:not-null field] [:!= field nil]))@@ -194,7 +194,7 @@{:deprecated "0.57.0"}[m]#_{:clj-kondo/ignore [:deprecated-var]}- (lib.util.match/replace m+ (lib.util.match/replace-lite m[:is-empty clause](if (emptyable? clause)[:or [:= clause nil] [:= clause ""]]@@ -210,11 +210,11 @@{:deprecated "0.57.0"}[m unit]#_{:clj-kondo/ignore [:deprecated-var]}- (lib.util.match/replace m+ (lib.util.match/replace-lite m[:field id-or-name opts][:field id-or-name (assoc opts :temporal-unit unit)]- :expression+ [:expression & _](let [[_expression expression-name opts] &match][:expression expression-name (assoc opts :temporal-unit unit)])))@@ -223,21 +223,21 @@{:deprecated "0.57.0"}[m]#_{:clj-kondo/ignore [:deprecated-var]}- (lib.util.match/replace m- [:time-interval field-or-expression n unit] (recur [:time-interval field-or-expression n unit nil])+ (lib.util.match/replace-lite m+ [:time-interval field-or-expression n unit] (&recur [:time-interval field-or-expression n unit nil]);; replace current/last/next with corresponding value of n and recur- [:time-interval field-or-expression :current unit options] (recur [:time-interval field-or-expression 0 unit options])- [:time-interval field-or-expression :last unit options] (recur [:time-interval field-or-expression -1 unit options])- [:time-interval field-or-expression :next unit options] (recur [:time-interval field-or-expression 1 unit options])+ [:time-interval field-or-expression :current unit options] (&recur [:time-interval field-or-expression 0 unit options])+ [:time-interval field-or-expression :last unit options] (&recur [:time-interval field-or-expression -1 unit options])+ [:time-interval field-or-expression :next unit options] (&recur [:time-interval field-or-expression 1 unit options])- [:time-interval field-or-expression (n :guard #{-1}) unit (_ :guard :include-current)]+ [:time-interval field-or-expression (n :guard #{-1}) unit {:include-current identity}][:between(replace-field-or-expression field-or-expression unit)[:relative-datetime n unit][:relative-datetime 0 unit]]- [:time-interval field-or-expression (n :guard #{1}) unit (_ :guard :include-current)]+ [:time-interval field-or-expression (n :guard #{1}) unit {:include-current identity}][:between(replace-field-or-expression field-or-expression unit)[:relative-datetime 0 unit]@@ -246,7 +246,7 @@[:time-interval field-or-expression (n :guard #{-1 0 1}) unit _][:= (replace-field-or-expression field-or-expression unit) [:relative-datetime n unit]]- [:time-interval field-or-expression (n :guard neg?) unit (_ :guard :include-current)]+ [:time-interval field-or-expression (n :guard neg?) unit {:include-current identity}][:between(replace-field-or-expression field-or-expression unit)[:relative-datetime n unit]@@ -258,7 +258,7 @@[:relative-datetime n unit][:relative-datetime -1 unit]]- [:time-interval field-or-expression n unit (_ :guard :include-current)]+ [:time-interval field-or-expression n unit {:include-current identity}][:between(replace-field-or-expression field-or-expression unit)[:relative-datetime 0 unit]@@ -275,7 +275,7 @@{:deprecated "0.57.0"}[m]#_{:clj-kondo/ignore [:deprecated-var]}- (lib.util.match/replace+ (lib.util.match/replace-litem[:relative-time-interval col value bucket offset-value offset-bucket](let [col-default-bucket (cond-> col (and (vector? col) (= 3 (count col)))@@ -298,7 +298,7 @@{:deprecated "0.57.0"}[m]#_{:clj-kondo/ignore [:deprecated-var]}- (lib.util.match/replace+ (lib.util.match/replace-litem[:during col value unit](let [col-default-bucket (cond-> col@@ -315,8 +315,7 @@{:deprecated "0.57.0"}[m]#_{:clj-kondo/ignore [:deprecated-var]}- (lib.util.match/replace- m+ (lib.util.match/replace-lite m[:if & args](into [:case] args)))@@ -325,7 +324,7 @@{:deprecated "0.57.0"}[m]#_{:clj-kondo/ignore [:deprecated-var]}- (lib.util.match/replace m+ (lib.util.match/replace-lite m[:in & args](into [:=] args)@@ -341,7 +340,7 @@{:deprecated "0.57.0"}[m]#_{:clj-kondo/ignore [:deprecated-var]}- (lib.util.match/replace m+ (lib.util.match/replace-lite m[:does-not-contain & args][:not (into [:contains] args)]))@@ -360,7 +359,7 @@{:deprecated "0.57.0"}[m]#_{:clj-kondo/ignore [:deprecated-var]}- (lib.util.match/replace m+ (lib.util.match/replace-lite m[:= field x y & more](apply vector :or (for [x (concat [x y] more)][:= field x]))@@ -384,12 +383,12 @@{:deprecated "0.57.0"}[m]#_{:clj-kondo/ignore [:deprecated-var]}- (lib.util.match/replace m- [clause field & (args :guard (partial some (partial = [:relative-datetime :current])))]+ (lib.util.match/replace-lite m+ [clause field & (args :guard (some (partial = [:relative-datetime :current]) args))](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+ (into [clause field] (lib.util.match/replace-lite args[:relative-datetime :current][:relative-datetime 0 temporal-unit])))))@@ -421,16 +420,16 @@{:deprecated "0.57.0"}[m]#_{:clj-kondo/ignore [:deprecated-var]}- (lib.util.match/replace m+ (lib.util.match/replace-lite m[(op :guard temporal-extract-ops) field & args][:temporal-extract field (temporal-extract-ops->unit [op (first args)])]))(defn- desugar-divide-with-extra-args{:deprecated "0.57.0"}[expression]- (lib.util.match/replace expression+ (lib.util.match/replace-lite expression[:/ x y z & more]- (recur (into [:/ [:/ x y]] (cons z more)))))+ (&recur (into [:/ [:/ x y]] (cons z more)))))(defn- temporal-case-expression"Creates a `:case` expression with a condition for each value of the given unit."@@ -452,13 +451,13 @@{:deprecated "0.57.0"}[expression]#_{:clj-kondo/ignore [:deprecated-var]}- (lib.util.match/replace expression+ (lib.util.match/replace-lite expression[:month-name column]- (recur (temporal-case-expression column :month-of-year 12))+ (&recur (temporal-case-expression column :month-of-year 12))[:quarter-name column]- (recur (temporal-case-expression column :quarter-of-year 4))+ (&recur (temporal-case-expression column :quarter-of-year 4))[:day-name column]- (recur (temporal-case-expression column :day-of-week 7))))+ (&recur (temporal-case-expression column :day-of-week 7))))(mu/defn desugar-expression :- ::mbql.s/FieldOrExpressionDef"Rewrite various 'syntactic sugar' expressions like `:/` with more than two args into something simpler for driversdiff --git a/src/metabase/lib/filter/desugar.cljc b/src/metabase/lib/filter/desugar.cljcindex 6be0baed66d..bb9102c6e71 100644--- a/src/metabase/lib/filter/desugar.cljc+++ b/src/metabase/lib/filter/desugar.cljc@@ -30,7 +30,7 @@(mu/defn- desugar-inside :- ::clause"Rewrite `:inside` filter clauses as a pair of `:between` clauses."[expr :- ::clause]- (lib.util.match/replace expr+ (lib.util.match/replace-lite expr[:inside opts lat-field lon-field lat-max lon-min lat-min lon-max](-> (lib.filter/and(lib.filter/between lat-field lat-min lat-max)@@ -40,7 +40,7 @@(mu/defn- desugar-is-null-and-not-null :- ::clause"Rewrite `:is-null` and `:not-null` filter clauses as simpler `:=` and `:!=`, respectively."[expr :- ::clause]- (lib.util.match/replace expr+ (lib.util.match/replace-lite expr[:is-null opts x] [:= opts x nil][:not-null opts x] [:!= opts x nil]))@@ -54,7 +54,7 @@If `:not-empty` is called on `::lib.schema.expression/emptyable` type, expand check for empty string. Fornon-`emptyable` types act as `:is-null`. If field has nil base type it is considered not emptyable expansion wise."[expr :- ::clause]- (lib.util.match/replace expr+ (lib.util.match/replace-lite expr[:is-empty opts arg](-> (if (emptyable? arg)(lib.filter/or@@ -75,51 +75,51 @@"Replace a field or expression inside :time-interval"[expr :- ::clauseunit :- ::lib.schema.temporal-bucketing/unit]- (lib.util.match/replace expr- #{:field :expression}+ (lib.util.match/replace-lite expr+ [#{:field :expression} & _](lib.options/update-options &match assoc :temporal-unit unit)))(mu/defn- desugar-time-interval :- ::clause"Rewrite `:time-interval` filter clauses as simpler ones like `:=` or `:between`."[expr :- ::clause]- (lib.util.match/replace expr+ (lib.util.match/replace-lite expr;; replace current/last/next with corresponding value of n and recur- [:time-interval opts field-or-expression :current unit] (recur [:time-interval opts field-or-expression 0 unit])- [:time-interval opts field-or-expression :last unit] (recur [:time-interval opts field-or-expression -1 unit])- [:time-interval opts field-or-expression :next unit] (recur [:time-interval opts field-or-expression 1 unit])-- [:time-interval (opts :guard :include-current) field-or-expression (n :guard neg?) unit]- (-> (lib.filter/between- (update-temporal-unit-in-refs field-or-expression unit)- (lib.expression/relative-datetime n unit)- (lib.expression/relative-datetime 0 unit))- (merge-options opts))-- [:time-interval (opts :guard :include-current) field-or-expression n unit]- (-> (lib.filter/between- (update-temporal-unit-in-refs field-or-expression unit)- (lib.expression/relative-datetime 0 unit)- (lib.expression/relative-datetime n unit))- (merge-options opts))-- [:time-interval opts field-or-expression (n :guard #{-1 0 1}) unit]- (-> (lib.filter/= (update-temporal-unit-in-refs field-or-expression unit)- (lib.expression/relative-datetime n unit))- (merge-options opts))-- [:time-interval opts field-or-expression (n :guard neg?) unit]- (-> (lib.filter/between- (update-temporal-unit-in-refs field-or-expression unit)- (lib.expression/relative-datetime n unit)- (lib.expression/relative-datetime -1 unit))- (merge-options opts))+ [:time-interval opts field-or-expression :current unit] (&recur [:time-interval opts field-or-expression 0 unit])+ [:time-interval opts field-or-expression :last unit] (&recur [:time-interval opts field-or-expression -1 unit])+ [:time-interval opts field-or-expression :next unit] (&recur [:time-interval opts field-or-expression 1 unit])++ [:time-interval (opts :guard (:include-current opts)) field-or-expression n unit]+ (if (neg? n)+ (-> (lib.filter/between+ (update-temporal-unit-in-refs field-or-expression unit)+ (lib.expression/relative-datetime n unit)+ (lib.expression/relative-datetime 0 unit))+ (merge-options opts))+ (-> (lib.filter/between+ (update-temporal-unit-in-refs field-or-expression unit)+ (lib.expression/relative-datetime 0 unit)+ (lib.expression/relative-datetime n unit))+ (merge-options opts)))[:time-interval opts field-or-expression n unit]- (-> (update-temporal-unit-in-refs field-or-expression unit)- (lib.filter/between- (lib.expression/relative-datetime 1 unit)- (lib.expression/relative-datetime n unit))- (merge-options opts))))+ (cond (#{-1 0 1} n)+ (-> (lib.filter/= (update-temporal-unit-in-refs field-or-expression unit)+ (lib.expression/relative-datetime n unit))+ (merge-options opts))++ (neg? n)+ (-> (lib.filter/between+ (update-temporal-unit-in-refs field-or-expression unit)+ (lib.expression/relative-datetime n unit)+ (lib.expression/relative-datetime -1 unit))+ (merge-options opts))++ :else+ (-> (update-temporal-unit-in-refs field-or-expression unit)+ (lib.filter/between+ (lib.expression/relative-datetime 1 unit)+ (lib.expression/relative-datetime n unit))+ (merge-options opts)))))(defn- with-default-temporal-bucket-in-refs [arg](lib.util/fresh-uuids (update-temporal-unit-in-refs arg :default)))@@ -127,7 +127,7 @@(mu/defn- desugar-relative-time-interval :- ::clause"Transform `:relative-time-interval` to `:and` expression."[expr :- ::clause]- (lib.util.match/replace expr+ (lib.util.match/replace-lite expr[:relative-time-interval opts arg (value :guard int?) bucket offset-value offset-bucket](let [bucket #(lib.util/fresh-uuids bucket)offset-value #(lib.util/fresh-uuids offset-value)@@ -149,7 +149,7 @@(mu/defn- desugar-during :- ::clause"Transform a `:during` expression to an `:and` expression."[expr :- ::clause]- (lib.util.match/replace expr+ (lib.util.match/replace-lite expr[:during opts arg value unit](let [lower-bound (u.time/truncate value unit)upper-bound (u.time/add lower-bound unit 1)]@@ -166,7 +166,7 @@(lib.util/clause? fragment) (desugar-if fragment)(vector? fragment) (mapv process-fragment fragment):else fragment))]- (lib.util.match/replace expr+ (lib.util.match/replace-lite expr[:if opts & args](-> (map process-fragment args)(->> (apply lib.expression/case))@@ -175,7 +175,7 @@(mu/defn- desugar-in :- ::clause"Transform `:in` and `:not-in` expressions to `:=` and `:!=` expressions."[expr :- ::clause]- (lib.util.match/replace expr+ (lib.util.match/replace-lite expr[:in opts & args](-> (apply lib.filter/= args)(merge-options opts))@@ -191,7 +191,7 @@several `[:and [:does-not-contain ...] [:does-not-contain ...] ...]` clauses, which then get rewritten here into`[:and [:not [:contains ...]] [:not [:contains ...]]]`."[expr :- ::clause]- (lib.util.match/replace expr+ (lib.util.match/replace-lite expr[:does-not-contain opts whole & parts](-> (lib.filter/not (-> (apply lib.filter/contains whole parts);; need to preserve stuff like `:case-sensitive`. Prefer values in each clause over `opts`@@ -212,7 +212,7 @@Multi-argument forms use pMBQL style with the options at index 1, **even if there are no options**:`[:contains {} field x y z]`."[expr :- ::clause]- (lib.util.match/replace expr+ (lib.util.match/replace-lite expr[:= opts field a b & more](-> (apply lib.filter/or (map (fn [expr](lib.filter/= (lib.util/fresh-uuids field) expr))@@ -244,18 +244,17 @@"Replace `relative-datetime` clauses like `[:relative-datetime :current]` with `[:relative-datetime 0 <unit>]`.`<unit>` is inferred from the `:field` the clause is being compared to (if any), otherwise falls back to `default.`"[expr :- ::clause]- (lib.util.match/replace expr- [tag opts field & (args :guard (fn [args]- (some (fn [arg]- (lib.util.match/match-lite arg [:relative-datetime _opts :current] true))- args)))]+ (lib.util.match/replace-lite expr+ [tag opts field & (args :guard (some (fn [arg]+ (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} _]temporal-unit):default)](into [tag opts field](map (fn [arg]- (lib.util.match/replace arg+ (lib.util.match/replace-lite arg[:relative-datetime relative-datetime-opts :current](-> (lib.expression/relative-datetime 0 temporal-unit)(merge-options relative-datetime-opts)))))@@ -286,16 +285,16 @@(mu/defn- desugar-temporal-extract :- ::clause"Replace datetime extractions clauses like `[:get-year field]` with `[:temporal-extract field :year]`."[expr :- ::clause]- (lib.util.match/replace expr+ (lib.util.match/replace-lite expr[(tag :guard temporal-extract-ops) opts field & args][:temporal-extract opts field (temporal-extract-ops->unit [tag (first args)])]))(mu/defn- desugar-divide-with-extra-args :- ::clause[expression :- ::clause]- (lib.util.match/replace expression+ (lib.util.match/replace-lite expression[:/ opts x y z & more]- (recur (-> (apply lib.expression// (lib.expression// x y) z more)- (merge-options opts)))))+ (&recur (-> (apply lib.expression// (lib.expression// x y) z more)+ (merge-options opts)))))(mu/defn- temporal-case-expression :- :mbql.clause/case"Creates a `:case` expression with a condition for each value of the given unit."@@ -319,10 +318,10 @@Uses the user's locale rather than the site locale, so the results will depend on the runner of the query, not justthe query itself. Filtering should be done based on the number, rather than the name."[expression :- ::clause]- (lib.util.match/replace expression- [:month-name opts expr] (recur (temporal-case-expression expr opts :month-of-year 12))- [:quarter-name opts expr] (recur (temporal-case-expression expr opts :quarter-of-year 4))- [:day-name opts expr] (recur (temporal-case-expression expr opts :day-of-week 7))))+ (lib.util.match/replace-lite expression+ [:month-name opts expr] (&recur (temporal-case-expression expr opts :month-of-year 12))+ [:quarter-name opts expr] (&recur (temporal-case-expression expr opts :quarter-of-year 4))+ [:day-name opts expr] (&recur (temporal-case-expression expr opts :day-of-week 7))))(mu/defn- desugar-expression :- ::clause"Rewrite various 'syntactic sugar' expressions like `:/` with more than two args into something simpler for driversdiff --git a/src/metabase/lib/filter/desugar/jvm.clj b/src/metabase/lib/filter/desugar/jvm.cljindex c4fe43427cb..51bbd954870 100644--- a/src/metabase/lib/filter/desugar/jvm.clj+++ b/src/metabase/lib/filter/desugar/jvm.clj@@ -141,16 +141,16 @@(mu/defn desugar-host-and-domain :- ::lib.schema.mbql-clause/clause"Unwrap host and domain."[expression :- ::lib.schema.mbql-clause/clause]- (lib.util.match/replace expression+ (lib.util.match/replace-lite expression[:host opts expr];; TODO (Cam 8/18/25) -- seems weird that we don't support Regex literals in the regex clauses and have to call (str ...)- (recur [:regex-match-first opts expr (str host-regex)])+ (&recur [:regex-match-first opts expr (str host-regex)])[:domain opts expr]- (recur [:regex-match-first opts expr (str domain-regex)])+ (&recur [:regex-match-first opts expr (str domain-regex)])[:subdomain opts expr]- (recur [:regex-match-first opts expr (str subdomain-regex)])+ (&recur [:regex-match-first opts expr (str subdomain-regex)])[:path opts expr]- (recur [:regex-match-first opts expr (str path-regex)])))+ (&recur [:regex-match-first opts expr (str path-regex)])))diff --git a/src/metabase/lib/filter/simplify_compound.cljc b/src/metabase/lib/filter/simplify_compound.cljcindex a22e42ed848..206d7ee8e7e 100644--- a/src/metabase/lib/filter/simplify_compound.cljc+++ b/src/metabase/lib/filter/simplify_compound.cljc@@ -68,10 +68,10 @@also fixes theoretically disallowed compound filters like `:and` with only a single subclause, and eliminates `nils`and duplicate subclauses from the clauses."[x]- (lib.util.match/replace x+ (lib.util.match/replace-lite x;; double negation, eliminate both[:not opts [:not arg-opts arg-arg]]- (recur+ (&recur;; preserve options from the collapsed clauses.(lib.options/update-options arg-arg merge arg-opts opts))diff --git a/src/metabase/lib/join.cljc b/src/metabase/lib/join.cljcindex 3ddaa755a6c..326b1c23319 100644--- a/src/metabase/lib/join.cljc+++ b/src/metabase/lib/join.cljc@@ -136,7 +136,7 @@(mapv (fn [condition](standard-join-condition-update-rhs condition(fn [rhs]- (lib.util.match/replace rhs+ (lib.util.match/replace-lite rhs(field :guard lib.util/field-clause?)(with-join-alias field new-alias)))))conditions)))))@@ -482,8 +482,8 @@conditions(mapv (fn [condition](standard-join-condition-update-rhs condition (fn [rhs]- (lib.util.match/replace rhs- (field :guard #(and (lib.util/field-clause? %) (not (lib.join.util/current-join-alias %))))+ (lib.util.match/replace-lite rhs+ (field :guard (and (lib.util/field-clause? field) (not (lib.join.util/current-join-alias field))))(with-join-alias field join-alias)))))conditions)))@@ -565,10 +565,9 @@join-alias))(defn- add-alias-to-join-refs [query stage-number form join-alias join-cols]- (lib.util.match/replace form- (field :guard (fn [field-clause]- (and (lib.util/field-clause? field-clause)- (boolean (lib.equality/find-matching-column query stage-number field-clause join-cols)))))+ (lib.util.match/replace-lite form+ (field :guard (and (lib.util/field-clause? field)+ (boolean (lib.equality/find-matching-column query stage-number field join-cols))))(with-join-alias field join-alias)))(defn- add-alias-to-condition@@ -579,7 +578,7 @@;; these cases, but only for conditions that look like the ones generated;; generated by the FE. These have the form home-field op join-field,;; so we break ties by looking at the position of the field reference.- (lib.util.match/replace condition+ (lib.util.match/match-lite condition[op op-opts (lhs :guard lib.util/field-clause?) (rhs :guard lib.util/field-clause?)](let [lhs-alias (lib.join.util/current-join-alias lhs)rhs-alias (lib.join.util/current-join-alias rhs)]@@ -603,7 +602,7 @@;; we leave alone the condition otherwise:else &match))- ;; do not replace inner references as there can be a custom join expression+ ;; do not walk inner references as there can be a custom join expression_condition)))diff --git a/src/metabase/lib/metadata/result_metadata.cljc b/src/metabase/lib/metadata/result_metadata.cljcindex 7a554c19e11..a2984786ffd 100644--- a/src/metabase/lib/metadata/result_metadata.cljc+++ b/src/metabase/lib/metadata/result_metadata.cljc@@ -242,7 +242,7 @@[col :- ::kebab-cased-mapa-ref :- ::mbql.s/Reference](let [a-ref (remove-namespaced-options a-ref)]- (lib.util.match/replace a-ref+ (lib.util.match/replace-lite a-ref[:field (id :guard pos-int?) opts][:field id (not-empty (cond-> (dissoc opts :effective-type :inherited-temporal-unit)(:source-field opts) (dissoc :join-alias)@@ -251,13 +251,13 @@[:field (field-name :guard string?) opts][:field field-name (not-empty (dissoc opts :inherited-temporal-unit))]- [:expression expression-name (opts :guard (some-fn :base-type :effective-type))]+ [:expression expression-name (opts :guard (or (:base-type opts) (:effective-type opts)))](let [fe-friendly-opts (dissoc opts :base-type :effective-type)](if (seq fe-friendly-opts)[:expression expression-name fe-friendly-opts][:expression expression-name]))- [:aggregation aggregation-index (opts :guard (some-fn :base-type :effective-type))]+ [:aggregation aggregation-index (opts :guard (or (:base-type opts) (:effective-type opts)))](let [fe-friendly-opts (dissoc opts :base-type :effective-type)](if (seq fe-friendly-opts)[:aggregation aggregation-index fe-friendly-opts]diff --git a/src/metabase/lib/order_by.cljc b/src/metabase/lib/order_by.cljcindex 3afc4e53305..bb164a33167 100644--- a/src/metabase/lib/order_by.cljc+++ b/src/metabase/lib/order_by.cljc@@ -189,8 +189,8 @@([query :- ::lib.schema/querycurrent-order-by :- ::lib.schema.order-by/order-by](let [lib-uuid (lib.options/uuid current-order-by)]- (lib.util.match/replace query- [direction (_ :guard #(= (:lib/uuid %) lib-uuid)) _]+ (lib.util.match/replace-lite query+ [direction {:lib/uuid (uuid :guard (= uuid lib-uuid))} _](assoc &match 0 (opposite-direction direction))))))(mu/defn remove-all-order-bys :- ::lib.schema/querydiff --git a/src/metabase/lib/query.cljc b/src/metabase/lib/query.cljcindex 5f5b9c87236..6dcddaab708 100644--- a/src/metabase/lib/query.cljc+++ b/src/metabase/lib/query.cljc@@ -135,10 +135,10 @@id))];; "pre-warm" the metadata provider(do (lib.metadata/bulk-metadata metadata-provider :metadata/column field-ids)- (lib.util.match/replace- x+ (lib.util.match/replace-lite x[:field- (options :guard (every-pred map? (complement (every-pred :base-type :effective-type))))+ (options :guard (and (map? options) (not (and (:base-type options)+ (:effective-type options)))))(id :guard pos-int?)](if (some #{:mbql/stage-metadata} &parents)&match@@ -229,9 +229,10 @@(mapv (fn [[stage-number stage]](-> stage(add-types-to-fields metadata-provider)- (lib.util.match/replace+ (lib.util.match/replace-lite[:expression- (opts :guard (every-pred map? (complement (every-pred :base-type :effective-type))))+ (opts :guard (and (map? opts) (not (and (:base-type opts)+ (:effective-type opts)))))expression-name](let [found-ref (try(m/remove-valsdiff --git a/src/metabase/lib/remove_replace.cljc b/src/metabase/lib/remove_replace.cljcindex ec5f1e403e8..70f27964a1e 100644--- a/src/metabase/lib/remove_replace.cljc+++ b/src/metabase/lib/remove_replace.cljc@@ -90,9 +90,9 @@source-uuid->new-column (m/index-by :lib/source-uuid new-columns)](lib.util/update-query-stagequery-modified stage-number- #(lib.util.match/replace+ #(lib.util.match/replace-lite%- :field+ [:field & _](let [old-matching-column (lib.equality/find-matching-column &match old-columns)](if-let [new-column (some-> old-matching-column :lib/source-uuid source-uuid->new-column)](assoc &match 2 ((some-fn :lib/source-column-alias :name) new-column))@@ -332,8 +332,8 @@(defn- local-replace-expression-references [stage target-ref-id replacement-ref](let [replace-embedded-refs (fn replace-refs [stage]- (lib.util.match/replace stage- [:expression _ target-ref-id]+ (lib.util.match/replace-lite stage+ [:expression _ (id :guard (= id target-ref-id))](-> replacement-reffresh-ref)))](replace-embedded-refs stage)))@@ -454,17 +454,15 @@"Checks if two sets of join conditions are the same. We ignore the current join-aliases as those may be changing,and we ignore effective-type since `tweak-expression` above may have already added it, and in this case it will be irrelevant."[new-join-alias new-join-conditions join-alias-b join-conditions-b]- (let [a-conds (lib.util.match/replace- new-join-conditions- (_ :guard (every-pred map? (comp #{new-join-alias} :join-alias)))+ (let [a-conds (lib.util.match/replace-lite new-join-conditions+ {:join-alias (ja :guard (= ja new-join-alias))}(dissoc &match :join-alias :effective-type)- (_ :guard (every-pred map? :effective-type))+ {:effective-type (_ :guard identity)}(dissoc &match :effective-type))- b-conds (lib.util.match/replace- join-conditions-b- (_ :guard (every-pred map? (comp #{join-alias-b} :join-alias)))+ b-conds (lib.util.match/replace-lite join-conditions-b+ {:join-alias (ja :guard (= ja join-alias-b))}(dissoc &match :join-alias :effective-type)- (_ :guard (every-pred map? :effective-type))+ {:effective-type (_ :guard identity)}(dissoc &match :effective-type))](not (lib.equality/= a-conds b-conds))))@@ -557,8 +555,8 @@(defn- replace-join-alias[a-join old-name new-name]- (lib.util.match/replace a-join- (field :guard #(field-clause-with-join-alias? % old-name))+ (lib.util.match/replace-lite a-join+ (field :guard (field-clause-with-join-alias? field old-name))(lib.join/with-join-alias field new-name)))(defn- rename-join-in-stagediff --git a/src/metabase/lib/util.cljc b/src/metabase/lib/util.cljcindex 65fc5ac7375..7f7e69a405b 100644--- a/src/metabase/lib/util.cljc+++ b/src/metabase/lib/util.cljc@@ -523,7 +523,7 @@query (fresh-uuids query (fn [old-uuid new-uuid](vswap! remapping assoc! old-uuid new-uuid)))remapping (persistent! @remapping)]- (lib.util.match/replace query+ (lib.util.match/replace-lite query[:aggregation opts old-uuid][:aggregation opts (or (remapping old-uuid)(throw (ex-info "Could not convert old :aggregation ref to new UUIDs"diff --git a/src/metabase/lib/util/match.clj b/src/metabase/lib/util/match.cljindex 6da283f245f..f696e9ae161 100644--- a/src/metabase/lib/util/match.clj+++ b/src/metabase/lib/util/match.clj@@ -269,12 +269,16 @@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 (with-meta (list `nth s %1 nil) (meta s))+ (dorun (map-indexed #(process-pattern %2 (with-meta (list `nth s %1 nil) (or (meta s)+ {:depends-on 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}))))+ (when (pos? cnt)+ (vswap! conditions conj (with-meta (list (if rest-part+ `metabase.lib.util.match.impl/count>=+ `metabase.lib.util.match.impl/count=) s cnt)+ {:depends-on s})))+ (when rest-part+ (process-pattern rest-part (list `drop cnt s) bindings conditions false))):map (let [s (if (symbol? value) value (gensym "map"))](vswap! bindings conj [s `(metabase.lib.util.match.impl/map! ~value)])(run! (fn [[k v]]@@ -330,17 +334,18 @@;; Ensure that variable for common condition is already bound.(seq-contains? (map first common-bindings)(:depends-on (meta condition)))))- (first conditions))]- {:common-bindings (mapv strip-meta common-bindings)+ (first conditions))+ clear-bindings-meta #(strip-meta (mapv (fn [[s v]] [s (strip-meta v)]) %))]+ {:common-bindings (clear-bindings-meta common-bindings):common-conditions (mapv strip-meta common-conditions):all-bindings (mapv (fn [bindings]- (strip-meta (remove #(seq-contains? common-bindings %) bindings)))+ (clear-bindings-meta (remove #(seq-contains? common-bindings %) bindings)))bindings):all-conditions (->> conditions(mapv (fn [conditions](->> conditions(remove #(seq-contains? common-conditions %))- (mapv strip-meta)))))}))+ strip-meta))))}))(defn- expand-conditions [combiner conditions body & [bool?]](case (count conditions)@@ -378,7 +383,7 @@value-binding (if common-vector-check?`(metabase.lib.util.match.impl/vector! ~(or value-binding value-sym))value-binding)]- `(let [~@(when (some? value-binding)+ `(let [~@(when (and (some? value-binding) (not= value-sym value-binding))[value-sym value-binding])~@(mapcat identity common-bindings)]~(expand-conditions@@ -603,3 +608,18 @@`(metabase.lib.util.match.impl/update-in-unless-empty ~x ~ks (fn [x#] (replace* x# ~patterns-and-results))));; TODO - it would be useful to have something like a `replace-all` function as well++(defmacro replace-lite+ "Walk `form` recursively and replace all patterns matched with `match-lite` by the respective return expressions. The+ same pattern options are supported, and `&parents` and `&match` anaphors are available in the same way."+ [form & clauses]+ (let [replace-fn-symb (gensym "replace-")+ contains-&parents? (contains-symbol? clauses '&parents)]+ `((fn ~replace-fn-symb [~'&match ~'&parents]+ (match-lite ~'&match+ ~@clauses+ ~'_ (metabase.lib.util.match.impl/replace-lite-in-collection+ ~replace-fn-symb ~'&match ~(when contains-&parents?+ '&parents))))+ ~form+ ~(when contains-&parents? []))))diff --git a/src/metabase/lib/util/match/impl.cljc b/src/metabase/lib/util/match/impl.cljcindex a2e51617175..7f6a7427103 100644--- a/src/metabase/lib/util/match/impl.cljc+++ b/src/metabase/lib/util/match/impl.cljc@@ -85,6 +85,11 @@[coll cnt](= (count coll) cnt))+(defn count>=+ "Return true if collection `coll` has `cnt` elements or more."+ [coll cnt]+ (>= (count coll) cnt))+(defn wrap-nil"If `value` is nil, return `::wrapped-nil`, otherwise return `value`."[value]@@ -118,3 +123,21 @@(when-some [match (match-fn v (maybe-add-to-parents clause-parents (when (keyword? fst) fst)))](reduced match))))nil form)))++(defn replace-lite-in-collection+ "Internal impl for `replace-lite`. Recursively replace values in a collection using a `replace-fn`."+ [replace-fn form clause-parents]+ (cond+ (map? form)+ (reduce-kv (fn [form k v]+ (let [repl (replace-fn v (maybe-add-to-parents clause-parents k))]+ (cond-> form+ (not (identical? v form)) (assoc k repl))))+ form form)++ (sequential? form)+ (mapv #(let [fst (first form)]+ (replace-fn % (maybe-add-to-parents clause-parents (when (keyword? fst) fst))))+ form)++ :else form))diff --git a/src/metabase/models/serialization.clj b/src/metabase/models/serialization.cljindex 6b4d4b7c167..5b94654db34 100644--- a/src/metabase/models/serialization.clj+++ b/src/metabase/models/serialization.clj@@ -1120,19 +1120,16 @@[mbql](-> mbqlnormalize- (lib.util.match/replace+ (lib.util.match/replace-lite;; `integer?` guard is here to make the operation idempotent[:field (id :guard integer?) opts][:field (*export-field-fk* id) (mbql-id->fully-qualified-name opts)];; `integer?` guard is here to make the operation idempotent- [:field (id :guard integer?)]- [:field (*export-field-fk* id)]-;; field-id is still used within parameter mapping dimensions;; example relevant clause - [:dimension [:fk-> [:field-id 1] [:field-id 2]]]- [:field-id (id :guard integer?)]- [:field-id (*export-field-fk* id)]+ [(tag :guard #{:field :field-id}) (id :guard integer?)]+ [tag (*export-field-fk* id)]{:source-table (id :guard integer?)}(assoc &match :source-table (*export-table-fk* id))@@ -1145,14 +1142,11 @@[:dimension (dim :guard vector?)][:dimension (mbql-id->fully-qualified-name dim)]- [:metric (id :guard integer?)]- [:metric (*export-fk* id 'Card)]-- [:segment (id :guard integer?)]- [:segment (*export-fk* id 'Segment)]-- [:measure (id :guard integer?)]- [:measure (*export-fk* id 'Measure)])))+ [(tag :guard #{:metric :segment :measure}) (id :guard integer?)]+ [tag (*export-fk* id (case tag+ :metric 'Card+ :segment 'Segment+ :measure 'Measure))])))(defn- export-source-table[source-table]@@ -1167,14 +1161,14 @@(defn- ids->fully-qualified-names[entity]- (lib.util.match/replace entity- mbql-entity-reference?+ (lib.util.match/replace-lite entity+ (_ :guard mbql-entity-reference?)(mbql-id->fully-qualified-name &match)- sequential?+ (_ :guard sequential?)(mapv ids->fully-qualified-names &match)- map?+ (_ :guard map?)(reduce-kv(fn [entity k _v](let [f (case k@@ -1210,16 +1204,16 @@(defn- mbql-fully-qualified-names->ids*[entity]- (lib.util.match/replace entity+ (lib.util.match/replace-lite entity;; handle legacy `:field-id` forms encoded prior to 0.39.0;; and also *current* expression forms used in parameter mapping dimensions;; example relevant clause - [:dimension [:fk-> [:field-id 1] [:field-id 2]]]- [(:or :field-id "field-id") fully-qualified-name]+ [#{:field-id "field-id"} fully-qualified-name](mbql-fully-qualified-names->ids* [:field fully-qualified-name])- [(:or :field "field") (fully-qualified-name :guard vector?) opts]+ [#{:field "field"} (fully-qualified-name :guard vector?) opts][:field (*import-field-fk* fully-qualified-name) (mbql-fully-qualified-names->ids* opts)]- [(:or :field "field") (fully-qualified-name :guard vector?)]+ [#{:field "field"} (fully-qualified-name :guard vector?)][:field (*import-field-fk* fully-qualified-name)];; source-field is also used within parameter mapping dimensions@@ -1239,38 +1233,38 @@(assoc :card-id (*import-fk* entity-id 'Card))mbql-fully-qualified-names->ids*) ; Process other keys- [(:or :metric "metric") (entity-id :guard portable-id?)]+ [#{:metric "metric"} (entity-id :guard portable-id?)][:metric (*import-fk* entity-id 'Card)]- [(:or :segment "segment") (fully-qualified-name :guard portable-id?)]+ [#{:segment "segment"} (fully-qualified-name :guard portable-id?)][:segment (*import-fk* fully-qualified-name 'Segment)]- [(:or :measure "measure") (fully-qualified-name :guard portable-id?)]+ [#{:measure "measure"} (fully-qualified-name :guard portable-id?)][:measure (*import-fk* fully-qualified-name 'Measure)]- (_ :guard (every-pred map? #(vector? (:source-table %))))+ {:source-table (_ :guard vector?)}(-> &match- (assoc :source-table (*import-table-fk* (:source-table &match)))+ (update :source-table *import-table-fk*)mbql-fully-qualified-names->ids*)- (_ :guard (every-pred map? #(vector? (:source_table %))))+ {:source_table (_ :guard vector?)}(-> &match- (assoc :source_table (*import-table-fk* (:source_table &match)))+ (update :source_table *import-table-fk*)mbql-fully-qualified-names->ids*)- (_ :guard (every-pred map? (comp portable-id? :source-table)))+ {:source-table (id :guard portable-id?)}(-> &match- (assoc :source-table (str "card__" (*import-fk* (:source-table &match) 'Card)))+ (assoc :source-table (str "card__" (*import-fk* id 'Card)))mbql-fully-qualified-names->ids*)- (_ :guard (every-pred map? (comp portable-id? :source_table)))+ {:source_table (id :guard portable-id?)}(-> &match- (assoc :source_table (str "card__" (*import-fk* (:source_table &match) 'Card)))+ (assoc :source_table (str "card__" (*import-fk* id 'Card)))mbql-fully-qualified-names->ids*) ;; process other keys- (_ :guard (every-pred map? (comp portable-id? :snippet-id)))+ {:snippet-id (id :guard portable-id?)}(-> &match- (assoc :snippet-id (*import-fk* (:snippet-id &match) 'NativeQuerySnippet))+ (assoc :snippet-id (*import-fk* id 'NativeQuerySnippet))mbql-fully-qualified-names->ids*)))(defn- mbql-fully-qualified-names->ids@@ -1504,8 +1498,7 @@(m/update-existing-in [:pivot_table.column_split :columns] mbql-fully-qualified-names->ids)))(defn- export-visualizations [entity]- (lib.util.match/replace- entity+ (lib.util.match/replace-lite entity["field-id" (id :guard number?)] ["field-id" (*export-field-fk* id)][:field-id (id :guard number?)] [:field-id (*export-field-fk* id)]["field-id" (id :guard number?) tail] ["field-id" (*export-field-fk* id) (export-visualizations tail)]@@ -1589,16 +1582,15 @@(*import-fk* id (link-card-model->toucan-model model)))}))))(defn- import-visualizations [entity]- (lib.util.match/replace- entity- [(:or :field-id "field-id") (fully-qualified-name :guard vector?) tail]+ (lib.util.match/replace-lite entity+ [#{:field-id "field-id"} (fully-qualified-name :guard vector?) tail][:field-id (*import-field-fk* fully-qualified-name) (import-visualizations tail)]- [(:or :field-id "field-id") (fully-qualified-name :guard vector?)]+ [#{:field-id "field-id"} (fully-qualified-name :guard vector?)][:field-id (*import-field-fk* fully-qualified-name)]- [(:or :field "field") (fully-qualified-name :guard vector?) tail]+ [#{:field "field"} (fully-qualified-name :guard vector?) tail][:field (*import-field-fk* fully-qualified-name) (import-visualizations tail)]- [(:or :field "field") (fully-qualified-name :guard vector?)]+ [#{:field "field"} (fully-qualified-name :guard vector?)][:field (*import-field-fk* fully-qualified-name)](_ :guard map?)diff --git a/src/metabase/query_processor/middleware/add_implicit_joins.clj b/src/metabase/query_processor/middleware/add_implicit_joins.cljindex 3d9078682ea..87dc29664f5 100644--- a/src/metabase/query_processor/middleware/add_implicit_joins.clj+++ b/src/metabase/query_processor/middleware/add_implicit_joins.clj@@ -180,8 +180,8 @@path :- ::lib.walk/pathstage :- ::lib.schema/stage](or (when-let [fk-field-info->join-alias (not-empty (construct-fk-field-info->join-alias query path stage))]- (let [stage' (lib.util.match/replace stage- [:field (opts :guard (every-pred :source-field (complement :join-alias))) id-or-name]+ (let [stage' (lib.util.match/replace-lite stage+ [:field (opts :guard (and (:source-field opts) (not (:join-alias opts)))) id-or-name](if-not (some #{:lib/stage-metadata} &parents)(let [join-alias (or (fk-field-info->join-alias (field-opts->fk-field-info query opts))(throw (ex-info (tru "Cannot find matching FK Table ID for FK Field {0}"diff --git a/src/metabase/query_processor/middleware/auto_bucket_datetimes.clj b/src/metabase/query_processor/middleware/auto_bucket_datetimes.cljindex d279c6314d0..df280dd517c 100644--- a/src/metabase/query_processor/middleware/auto_bucket_datetimes.clj+++ b/src/metabase/query_processor/middleware/auto_bucket_datetimes.clj@@ -165,9 +165,9 @@{:base-type base-type:effective-type (or effective-type base-type)})(wrap-clauses [x]- (lib.util.match/replace x+ (lib.util.match/replace-lite x;; don't replace anything that's already bucketed or otherwise is not subject to autobucketing- (_ :guard (partial should-not-be-autobucketed? query stage-path))+ (x :guard (should-not-be-autobucketed? query stage-path x))&match;; if it's a `:field` clause and `field-id->type-info` tells us it's a `:type/Temporal` (but not@@ -175,7 +175,7 @@[:field _opts (_id-or-name :guard datetime-but-not-time?)](lib/with-temporal-bucket &match :day)- [:expression (_opts :guard (comp date-or-datetime-clause? expression-opts->type-info)) _name]+ [:expression (opts :guard (date-or-datetime-clause? (expression-opts->type-info opts))) _name](lib/with-temporal-bucket &match :day)))(rewrite-clause [stage clause-to-rewrite](m/update-existing stage clause-to-rewrite wrap-clauses))]diff --git a/src/metabase/query_processor/middleware/desugar.clj b/src/metabase/query_processor/middleware/desugar.cljindex 9b2a3f62384..7dd2a46c9f6 100644--- a/src/metabase/query_processor/middleware/desugar.clj+++ b/src/metabase/query_processor/middleware/desugar.clj@@ -12,7 +12,7 @@(defn- desugar*[stage-or-join](letfn [(desugar** [x]- (lib.util.match/replace x+ (lib.util.match/replace-lite x(clause :guard lib.util/clause?)(lib/desugar-filter-clause clause)))](mergediff --git a/src/metabase/query_processor/middleware/expand_macros.clj b/src/metabase/query_processor/middleware/expand_macros.cljindex 20ac17d79e7..afe4eb0b3b4 100644--- a/src/metabase/query_processor/middleware/expand_macros.clj+++ b/src/metabase/query_processor/middleware/expand_macros.clj@@ -95,7 +95,7 @@[_macro-type :- [:= :segment]stage :- ::lib.schema/stageid->legacy-segment :- ::id->legacy-macro]- (-> (lib.util.match/replace stage+ (-> (lib.util.match/replace-lite stage[:segment _opts (id :guard pos-int?)](let [legacy-segment (get id->legacy-segment id)filter-clauses (legacy-macro-filters legacy-segment)]diff --git a/src/metabase/query_processor/middleware/fix_bad_field_id_refs.clj b/src/metabase/query_processor/middleware/fix_bad_field_id_refs.cljindex 147e5508d24..9d2aecc2c1f 100644--- a/src/metabase/query_processor/middleware/fix_bad_field_id_refs.clj+++ b/src/metabase/query_processor/middleware/fix_bad_field_id_refs.clj@@ -18,12 +18,12 @@(let [first-stage-path (conj (pop (vec path)) 0)source-table (:source-table (get-in query first-stage-path))update-fields (fn update-fields [form]- (lib.util.match/replace form+ (lib.util.match/replace-lite form;; don't recurse into joins. But should we update conditions tho.- (join :guard (every-pred map? #(= (:lib/type %) :mbql/join)))- (update join :conditions update-fields)+ {:lib/type :mbql/join}+ (update &match :conditions update-fields)- [:field (_opts :guard (complement :join-alias)) (id :guard pos-int?)]+ [:field (opts :guard (not (:join-alias opts))) (id :guard pos-int?)](or (when-let [col (lib.metadata/field query id)](when-not (= (:table-id col) source-table)(when-let [resolved (lib.walk/apply-f-for-stage-at-pathdiff --git a/src/metabase/query_processor/middleware/measures.clj b/src/metabase/query_processor/middleware/measures.cljindex f73e97e4008..8dbde180d08 100644--- a/src/metabase/query_processor/middleware/measures.clj+++ b/src/metabase/query_processor/middleware/measures.clj@@ -79,7 +79,7 @@"Replace :measure clauses in a stage with their actual aggregation expressions."[stage :- ::lib.schema/stageid->measure :- [:map-of pos-int? :map]]- (lib.util.match/replace stage+ (lib.util.match/replace-lite stage[:measure opts (id :guard pos-int?)](b/cond:let [measure (get id->measure id)]diff --git a/src/metabase/query_processor/middleware/metrics.clj b/src/metabase/query_processor/middleware/metrics.cljindex 0a81afbba5a..de3943b5c10 100644--- a/src/metabase/query_processor/middleware/metrics.clj+++ b/src/metabase/query_processor/middleware/metrics.clj@@ -137,13 +137,13 @@(if-let [aggregations (lib/aggregations query stage-number)](let [columns (lib/visible-columns query stage-number)](assoc-in query [:stages stage-number :aggregation]- (lib.util.match/replace aggregations+ (lib.util.match/replace-lite aggregations[:metric _ metric-id](if-let [{replacement :aggregation} (get lookup metric-id)];; We have to replace references from the source-metric with references appropriate for;; this stage (expression/aggregation -> field, field-id to string)- (let [replacement (lib.util.match/replace replacement- [(tag :guard #{:expression :field :aggregation}) _ _]+ (let [replacement (lib.util.match/replace-lite replacement+ [#{:expression :field :aggregation} _ _](if-let [col (lib/find-matching-column &match columns)](lib/ref col);; This is probably due to a field-id where it shouldn't be@@ -228,8 +228,8 @@(defn- add-join-aliases[x source-field->join-alias]- (lib.util.match/replace x- [:field (opts :guard (every-pred (comp source-field->join-alias :source-field) (complement :join-alias))) _]+ (lib.util.match/replace-lite x+ [:field (opts :guard (and (source-field->join-alias (:source-field opts)) (not (:join-alias opts)))) _](assoc-in &match [1 :join-alias] (-> opts :source-field source-field->join-alias))))(defn- include-implicit-joinsdiff --git a/src/metabase/query_processor/middleware/optimize_temporal_filters.clj b/src/metabase/query_processor/middleware/optimize_temporal_filters.cljindex b10acedfcf1..dea76185d95 100644--- a/src/metabase/query_processor/middleware/optimize_temporal_filters.clj+++ b/src/metabase/query_processor/middleware/optimize_temporal_filters.clj@@ -145,8 +145,8 @@(:end (u.date/range t unit)))(defn- change-temporal-unit-to-default [field]- (lib.util.match/replace field- [(_tag :guard #{:field :expression}) (_opts :guard (comp optimizable-units :temporal-unit)) _id-or-name]+ (lib.util.match/replace-lite field+ [#{:field :expression} {:temporal-unit (_ :guard optimizable-units)} _id-or-name](lib/update-options &match assoc :temporal-unit :default)[:absolute-datetime _opts t _unit]diff --git a/src/metabase/query_processor/middleware/parameters/mbql.clj b/src/metabase/query_processor/middleware/parameters/mbql.cljindex fa011fc8ae7..ae751c3010c 100644--- a/src/metabase/query_processor/middleware/parameters/mbql.clj+++ b/src/metabase/query_processor/middleware/parameters/mbql.clj@@ -114,10 +114,10 @@target-column :- [:or ::lib.schema.id/field :string]temporal-unit :- ::lib.schema.temporal-bucketing/unitnew-unit :- ::lib.schema.temporal-bucketing/unit]- (lib.util.match/replace stage- [(tag :guard #{:field :expression})- (opts :guard #(= temporal-unit (:temporal-unit %)))- (_id-or-name :guard #(= target-column %))]+ (lib.util.match/replace-lite stage+ [#{:field :expression}+ (opts :guard (= (:temporal-unit opts) temporal-unit))+ (id-or-name :guard (= id-or-name target-column))](lib/with-temporal-bucket &match new-unit)))(mu/defn- update-breakout-unit :- ::lib.schema/stagediff --git a/src/metabase/query_processor/middleware/persistence.clj b/src/metabase/query_processor/middleware/persistence.cljindex 140c8449ab9..05e8fa0887c 100644--- a/src/metabase/query_processor/middleware/persistence.clj+++ b/src/metabase/query_processor/middleware/persistence.clj@@ -16,7 +16,7 @@(if (and api/*current-user-id*(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))- (dissoc x :persisted-info/native))+ (lib.util.match/replace-lite query+ {:persisted-info/native (_ :guard identity)}+ (dissoc &match :persisted-info/native))query))diff --git a/src/metabase/query_processor/util/add_alias_info.clj b/src/metabase/query_processor/util/add_alias_info.cljindex 37d1135e845..e4c8b0ebe70 100644--- a/src/metabase/query_processor/util/add_alias_info.clj+++ b/src/metabase/query_processor/util/add_alias_info.clj@@ -227,22 +227,19 @@[query :- ::lib.schema/querypath :- ::lib.walk/pathstage :- ::lib.schema/stage.mbql]- (lib.util.match/replace stage+ (lib.util.match/replace-lite stage;; don't recurse into the metadata or joins -- [[lib.walk]] will take care of that recursion for us.- (_ :guard (constantly (some (set &parents) [:lib/stage-metadata :joins])))+ (_ :guard (some #{:lib/stage-metadata :joins} &parents))&match- :field+ [:field & _](let [col (resolve-field-ref query path &match)](-> (add-source-to-field-ref query path &match col)(fix-field-ref-if-it-should-actually-be-an-expression-ref col);; record the column we resolved it to, so we can use this when we add desired aliases in the next pass.(lib/update-options assoc ::resolved col)))- :expression- (lib/update-options &match assoc ::source-table ::none)-- :aggregation+ [#{:expression :aggregation} & _](lib/update-options &match assoc ::source-table ::none)))(mu/defn- add-desired-aliases-to-aggregations :- ::lib.schema/stage.mbql@@ -281,9 +278,9 @@by-source-alias :- [:map-of :string [:sequential ::lib.schema.metadata/column]]by-expression-name :- [:map-of :string ::lib.schema.metadata/column]stage :- ::lib.schema/stage.mbql]- (lib.util.match/replace stage+ (lib.util.match/replace-lite stage;; don't recurse into the metadata or joins -- [[lib.walk]] will take care of that recursion for us.- (_ :guard (constantly (some (set &parents) [:lib/stage-metadata :joins ::resolved])))+ (_ :guard (some #{:lib/stage-metadata :joins ::resolved} &parents))&match[:field opts _id-or-name]@@ -423,16 +420,15 @@(add-source-to-field-ref query parent-stage-path field-ref col)))update-conditions (fn [conditions];; the only kind of ref join conditions can have is a `:field` ref- (lib.util.match/replace conditions- ;; a field ref that comes from THIS join needs to get the desired alias returned- ;; by the last stage of the join to use as its source alias- [:field (_opts :guard #(= (:join-alias %) (:alias join))) _id-or-name]- (update-ref-from-this-join query join-path join &match)-- ;; a field ref that DOES NOT come from this join should get resolved relative to- ;; the parent stage.- [:field (_opts :guard #(not= (:join-alias %) (:alias join))) _id-or-name]- (update-other-ref &match)))]+ (lib.util.match/replace-lite conditions+ [:field opts _id-or-name]+ (if (= (:join-alias opts) (:alias join))+ ;; a field ref that comes from THIS join needs to get the desired alias returned+ ;; by the last stage of the join to use as its source alias+ (update-ref-from-this-join query join-path join &match)+ ;; a field ref that DOES NOT come from this join should get resolved relative to+ ;; the parent stage.+ (update-other-ref &match))))](try(update join :conditions update-conditions)(catch Throwable ediff --git a/src/metabase/query_processor/util/transformations/nest_breakouts.clj b/src/metabase/query_processor/util/transformations/nest_breakouts.cljindex 26c9e502d7b..cd5b01cade5 100644--- a/src/metabase/query_processor/util/transformations/nest_breakouts.clj+++ b/src/metabase/query_processor/util/transformations/nest_breakouts.clj@@ -58,8 +58,8 @@(mu/defn- update-second-stage-refs :- ::lib.schema/stage[stage :- ::lib.schema/stagefirst-stage-cols :- [:sequential ::lib.schema.metadata/column]]- (lib.util.match/replace stage- #{:field :expression}+ (lib.util.match/replace-lite stage+ [#{:field :expression} & _](if-let [col (when-not (some #{:expressions} &parents)(lib.equality/find-matching-column &match first-stage-cols))](-> coldiff --git a/src/metabase/xrays/domain_entities/core.clj b/src/metabase/xrays/domain_entities/core.cljindex 9e3007bbcf5..6ee633df9e3 100644--- a/src/metabase/xrays/domain_entities/core.clj+++ b/src/metabase/xrays/domain_entities/core.clj@@ -61,7 +61,7 @@[bindings :- Bindingssource :- SourceNameobj]- (lib.util.match/replace obj+ (lib.util.match/replace-lite obj[:dimension dimension] (->> dimension(get-dimension-binding bindings source)(resolve-dimension-clauses bindings source))))diff --git a/test/metabase/lib/util/match_test.cljc b/test/metabase/lib/util/match_test.cljcindex 2b313b7e579..3d10f6262a0 100644--- a/test/metabase/lib/util/match_test.cljc+++ b/test/metabase/lib/util/match_test.cljc@@ -1,7 +1,8 @@(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}}}}}+ metabase.lib.util.match/match-one {:level :off}+ metabase.lib.util.match/replace {:level :off}}}}}(:require[clojure.test :as t][metabase.lib.util.match :as lib.util.match]))