Skip to content

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

Trivial middleware, qp.compile, qp.execute => Lib (#63419)

Cam Saul1919a6cd6282
diff --git a/enterprise/backend/src/metabase_enterprise/advanced_permissions/query_processor/middleware/permissions.clj b/enterprise/backend/src/metabase_enterprise/advanced_permissions/query_processor/middleware/permissions.clj
index dd70285b91d..978a29c20e1 100644
--- a/enterprise/backend/src/metabase_enterprise/advanced_permissions/query_processor/middleware/permissions.clj
+++ b/enterprise/backend/src/metabase_enterprise/advanced_permissions/query_processor/middleware/permissions.clj
@@ -2,8 +2,9 @@
(:require
[clojure.string :as str]
[metabase.api.common :as api]
- [metabase.legacy-mbql.schema :as mbql.s]
- [metabase.lib.util.match :as lib.util.match]
+ [metabase.lib.core :as lib]
+ [metabase.lib.schema :as lib.schema]
+ [metabase.lib.walk :as lib.walk]
[metabase.permissions.core :as perms]
[metabase.premium-features.core :refer [defenterprise-schema]]
[metabase.query-permissions.core :as query-perms]
@@ -20,27 +21,29 @@
(some-> query :info :context name (str/includes? "download")))
(defmulti ^:private current-user-download-perms-level
- {:arglists '([mbql-query])}
- (mu/fn [query :- ::mbql.s/Query]
- (:type query)))
+ {:arglists '([mbql5-query])}
+ (mu/fn [query :- ::lib.schema/query]
+ (:lib/type (lib/query-stage query -1))))
-(defmethod current-user-download-perms-level :default
- [_]
- :one-million-rows)
-
-(defmethod current-user-download-perms-level :native
+(defmethod current-user-download-perms-level :mbql.stage/native
[{database-id :database, :as _query}]
(perms/native-download-permission-for-user api/*current-user-id* database-id))
+;;; TODO (Cam 9/11/25) -- move this to `lib.walk.util` once my PR that adds it finally gets reviewed
(mu/defn- any-native-stage?
"Returns true if any stage of this query is native."
- [query :- ::mbql.s/Query]
- (boolean (lib.util.match/match-one query
- (m :guard (every-pred map? :native))
- true)))
+ [query :- ::lib.schema/query]
+ (let [has-native-stage? (atom false)]
+ (lib.walk/walk-stages
+ query
+ (fn [_query _path stage]
+ (when (= (:lib/type stage) :mbql.stage/native)
+ (reset! has-native-stage? true))
+ nil))
+ @has-native-stage?))
-(mu/defmethod current-user-download-perms-level :query
- [{db-id :database, :as query} :- ::mbql.s/Query]
+(mu/defmethod current-user-download-perms-level :mbql.stage/mbql
+ [{db-id :database, :as query} :- ::lib.schema/query]
(let [{:keys [table-ids native?]} (query-perms/query->source-ids query)
perms (if (or native? (any-native-stage? query))
;; If we detect any native subqueries/joins, even with source-card IDs, require full native
@@ -54,26 +57,24 @@
(perms :ten-thousand-rows)
:one-million-rows)))
-(defenterprise-schema apply-download-limit :- ::mbql.s/Query
+(defenterprise-schema apply-download-limit :- ::lib.schema/query
"Pre-processing middleware to apply row limits to MBQL export queries if the user has `ten-thousand-rows` download
perms. This does not apply to native queries, which are instead limited by the [[limit-download-result-rows]]
post-processing middleware."
:feature :advanced-permissions
- [{query-type :type, {original-limit :limit} :query, :as query} :- ::mbql.s/Query]
- (if (and (is-download? query)
- (= query-type :query)
- (= (current-user-download-perms-level query) :ten-thousand-rows))
- (assoc-in query
- [:query :limit]
- (apply min (filter some? [original-limit max-rows-in-limited-downloads])))
- query))
+ [query :- ::lib.schema/query]
+ (cond-> query
+ (and (is-download? query)
+ (= (:lib/type (lib/query-stage query -1)) :mbql.stage/mbql)
+ (= (current-user-download-perms-level query) :ten-thousand-rows))
+ (lib/limit ((fnil min Integer/MAX_VALUE) (lib/current-limit query -1) max-rows-in-limited-downloads))))
(defenterprise-schema limit-download-result-rows :- ::qp.schema/rff
"Post-processing middleware to limit the number of rows included in downloads if the user has `limited` download
perms. Mainly useful for native queries, which are not modified by the [[apply-download-limit]] pre-processing
middleware."
:feature :advanced-permissions
- [query :- ::mbql.s/Query
+ [query :- ::lib.schema/query
rff :- ::qp.schema/rff]
(if (and (is-download? query)
(= (current-user-download-perms-level query) :ten-thousand-rows))
@@ -89,7 +90,7 @@
the query metadata so that the frontend can determine whether to show the download option on the UI."
:feature :advanced-permissions
[qp :- ::qp.schema/qp]
- (mu/fn [query :- ::mbql.s/Query
+ (mu/fn [query :- ::lib.schema/query
rff :- ::qp.schema/rff]
(let [download-perms-level (if api/*current-user-id*
(current-user-download-perms-level query)
diff --git a/enterprise/backend/src/metabase_enterprise/sandbox/query_processor/middleware/column_level_perms_check.clj b/enterprise/backend/src/metabase_enterprise/sandbox/query_processor/middleware/column_level_perms_check.clj
index c102c79d1c1..67985ca1bd8 100644
--- a/enterprise/backend/src/metabase_enterprise/sandbox/query_processor/middleware/column_level_perms_check.clj
+++ b/enterprise/backend/src/metabase_enterprise/sandbox/query_processor/middleware/column_level_perms_check.clj
@@ -6,6 +6,8 @@
;; Lib only soon
^{:clj-kondo/ignore [:discouraged-namespace]}
[metabase.legacy-mbql.schema :as mbql.s]
+ [metabase.lib.core :as lib]
+ [metabase.lib.schema :as lib.schema]
[metabase.lib.util.match :as lib.util.match]
[metabase.premium-features.core :refer [defenterprise-schema]]
[metabase.query-processor.schema :as qp.schema]
@@ -24,7 +26,6 @@
nil)
(mu/defn- maybe-apply-column-level-perms-check*
- {:arglists '([query])}
[{{{source-query-fields :fields} :source-query} :query, :as query} :- ::mbql.s/Query]
(let [restricted-field-ids (and *gtap-perms*
(field-ids source-query-fields))]
@@ -39,7 +40,8 @@
"Check column-level permissions if applicable."
:feature :sandboxes
[qp :- ::qp.schema/qp]
- (mu/fn [query :- ::mbql.s/Query
+ (mu/fn [query :- ::lib.schema/query
rff :- ::qp.schema/rff]
- (maybe-apply-column-level-perms-check* query)
+ ;; TODO (Cam 9/11/25) -- update this middleware to use Lib/MBQL 5
+ (maybe-apply-column-level-perms-check* (lib/->legacy-MBQL query))
(qp query rff)))
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.clj
index 37e05a4e8c1..453e7b66676 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
@@ -5,6 +5,7 @@
[metabase-enterprise.advanced-permissions.query-processor.middleware.permissions :as ee.qp.perms]
[metabase-enterprise.sandbox.query-processor.middleware.sandboxing :as sandboxing]
[metabase.legacy-mbql.normalize :as mbql.normalize]
+ [metabase.lib.core :as lib]
[metabase.lib.test-metadata :as meta]
[metabase.lib.util.match :as lib.util.match]
[metabase.permissions.core :as perms]
@@ -56,25 +57,31 @@
(defn- mbql-download-query
([]
- (mbql-download-query 'venues))
+ (mbql-download-query :venues))
([table-name]
- {:database (mt/id)
- :type :query
- :query {:source-table (mt/id table-name)}
- :info {:context (api.dataset/export-format->context :csv)}}))
+ (lib/query
+ (mt/metadata-provider)
+ {:database (mt/id)
+ :type :query
+ :query {:source-table (mt/id table-name)}
+ :info {:context (api.dataset/export-format->context :csv)}})))
(defn- native-download-query []
- {:database (mt/id)
- :type :native
- :native {:query "select * from venues"}
- :info {:context (api.dataset/export-format->context :csv)}})
+ (lib/query
+ (mt/metadata-provider)
+ {:database (mt/id)
+ :type :native
+ :native {:query "select * from venues"}
+ :info {:context (api.dataset/export-format->context :csv)}}))
(defn- download-limit
[query]
- (-> query
- (ee.qp.perms/apply-download-limit)
- (get-in [:query :limit])))
+ (-> (if (:lib/type query)
+ query
+ (lib/query (mt/metadata-provider) query))
+ ee.qp.perms/apply-download-limit
+ lib/current-limit))
(deftest apply-download-limit-test
(let [limited-download-max-rows @#'ee.qp.perms/max-rows-in-limited-downloads]
@@ -86,9 +93,7 @@
(testing "If the query already has a limit lower than the download limit, the limit is not changed"
(is (= (dec limited-download-max-rows)
- (download-limit (assoc-in (mbql-download-query)
- [:query :limit]
- (dec limited-download-max-rows))))))
+ (download-limit (lib/limit (mbql-download-query) (dec limited-download-max-rows))))))
(testing "Native queries are unmodified"
(is (= (native-download-query) (ee.qp.perms/apply-download-limit (native-download-query))))))
@@ -124,7 +129,7 @@
(is (= (inc limited-download-max-rows)
(-> (native-download-query) limit-download-result-rows mt/rows count))))))))
-(defn- check-download-permisions [query]
+(defn- check-download-permissions [query]
(let [qp (ee.qp.perms/check-download-permissions
(fn [query _rff]
query))]
@@ -139,23 +144,24 @@
(is (thrown-with-msg?
ExceptionInfo
download-perms-error-msg
- (check-download-permisions (mbql-download-query))))
-
+ (check-download-permissions (mbql-download-query))))
(testing "No exception is thrown for non-download queries"
- (let [query (dissoc (mbql-download-query 'venues) :info)]
- (is (= query (check-download-permisions query)))))))))
+ (let [query (dissoc (mbql-download-query :venues) :info)]
+ (is (= query (check-download-permissions query)))))))))
(deftest check-download-permissions-test-2
(testing "No exception is thrown if the user has any (full or limited) download permissions for the DB"
(with-download-perms-for-db! (mt/id) :full
(mt/with-current-user (mt/user->id :rasta)
(is (= (mbql-download-query)
- (check-download-permisions (mbql-download-query))))))
+ (check-download-permissions (mbql-download-query))))))))
+(deftest check-download-permissions-test-3
+ (testing "No exception is thrown if the user has any (full or limited) download permissions for the DB"
(with-download-perms-for-db! (mt/id) :limited
(mt/with-current-user (mt/user->id :rasta)
(is (= (mbql-download-query)
- (check-download-permisions (mbql-download-query))))))))
+ (check-download-permissions (mbql-download-query))))))))
(deftest check-download-permissions-when-advanced-mbql-sandboxed-test
(testing "Applying a basic sandbox does not affect the download permissions for a table"
@@ -174,9 +180,9 @@
(mt/id :reviews) :limited
(mt/id :orders) :limited}}}
(mt/with-metadata-provider (mt/id)
- (let [with-sandbox (apply-row-level-permissions (mbql-download-query 'checkins))]
+ (let [with-sandbox (apply-row-level-permissions (mbql-download-query :checkins))]
(is (= with-sandbox
- (check-download-permisions with-sandbox))))))))))
+ (check-download-permissions with-sandbox))))))))))
(deftest check-download-permissions-when-advanced-sql-sandboxed-test
(testing "Applying a advanced sandbox does not affect the download permissions for a table"
@@ -197,7 +203,7 @@
(mt/with-metadata-provider (mt/id)
(let [with-sandbox (apply-row-level-permissions (mbql-download-query 'checkins))]
(is (= with-sandbox
- (check-download-permisions with-sandbox)))))))))
+ (check-download-permissions with-sandbox)))))))))
;;; +----------------------------------------------------------------------- -----------------------------------------+
;;; | E2E tests |
diff --git a/src/metabase/lib/core.cljc b/src/metabase/lib/core.cljc
index 63052bbca2e..a2c6a63420c 100644
--- a/src/metabase/lib/core.cljc
+++ b/src/metabase/lib/core.cljc
@@ -46,6 +46,7 @@
[metabase.lib.remove-replace :as lib.remove-replace]
[metabase.lib.schema.util]
[metabase.lib.segment :as lib.segment]
+ [metabase.lib.serialize]
[metabase.lib.stage :as lib.stage]
[metabase.lib.swap :as lib.swap]
[metabase.lib.table :as lib.table]
@@ -96,6 +97,7 @@
lib.remove-replace/keep-me
metabase.lib.schema.util/keep-me
lib.segment/keep-me
+ metabase.lib.serialize/keep-me
lib.stage/keep-me
lib.swap/keep-me
lib.table/keep-me
@@ -408,6 +410,8 @@
[metabase.lib.schema.util]
[lib.segment
available-segments]
+ [metabase.lib.serialize
+ prepare-for-serialization]
[lib.stage
append-stage
drop-stage
@@ -429,6 +433,8 @@
temporal-bucket
with-temporal-bucket]
[lib.util
+ clause?
+ clause-of-type?
fresh-uuids
native-stage?
normalized-query-type
diff --git a/src/metabase/lib/query.cljc b/src/metabase/lib/query.cljc
index 5287b2a003a..d8fcbc57eea 100644
--- a/src/metabase/lib/query.cljc
+++ b/src/metabase/lib/query.cljc
@@ -442,15 +442,6 @@
(let [{q :query, n :stage-number} (wrap-native-query-with-mbql a-query stage-number card-id)]
(apply f q n args)))
-;;; TODO (Cam 7/10/25) -- not sure this is really needed since the `:encode/serialize` keys for schemas already say
-;;; how to do this
-(defn serializable
- "Given a query, ensure it doesn't have any keys or structures that aren't safe for serialization.
-
- For example, any Atoms or Delays or should be removed."
- [a-query]
- (dissoc a-query :lib/metadata))
-
(defn- template-tag-stages
[template-tags]
(for [{:keys [card-id snippet-id] tag-type :type} (vals template-tags)
diff --git a/src/metabase/lib/schema.cljc b/src/metabase/lib/schema.cljc
index f300be0851d..c422301ce01 100644
--- a/src/metabase/lib/schema.cljc
+++ b/src/metabase/lib/schema.cljc
@@ -68,9 +68,10 @@
;; are the parameters we pass in for a `PreparedStatement` for `?` placeholders. These can be anything, including
;; nil.
;;
- ;; TODO -- pretty sure this is supposed to be `:params`, not `:args`, and this is allowed to be anything rather
- ;; than just `literal`... I think we're using the `literal` schema tho for either normalization or serialization
- [:args {:optional true} [:sequential ::literal/literal]]
+ ;; This schema is `[:or ::literal/literal :any]` so Malli encoding [[metabase.lib.serialize]] will use it if
+ ;; applicable... e.g. a Java time type will get serialized to a
+ ;; string (see [[metabase.lib.serialize-test/encode-java-time-types-in-native-query-args-test]])
+ [:params {:optional true} [:maybe [:sequential [:or [:ref ::literal/literal] :any]]]]
;; the Table/Collection/etc. that this query should be executed against; currently only used for MongoDB, where it
;; is required.
[:collection {:optional true} ::common/non-blank-string]
@@ -93,7 +94,8 @@
:limit "MBQL stage keys like :limit are not allowed in a native query stage."
:order-by "MBQL stage keys like :order-by are not allowed in a native query stage."
:offset "MBQL stage keys like :offset are not allowed in a native query stage."
- :page "MBQL stage keys like :page are not allowed in a native query stage."})])
+ :page "MBQL stage keys like :page are not allowed in a native query stage."
+ :args "Native query parameters should use :params, not :args."})])
(mr/def ::breakout
[:ref ::ref/ref])
@@ -263,7 +265,6 @@
:decode/normalize normalize-stage
:encode/serialize #(dissoc %
;; this stuff is all added at runtime by QP middleware.
- :params
:parameters
:lib/stage-metadata
;; TODO (Cam 8/7/25) -- wait a minute, `:middleware` is not supposed to be added here,
diff --git a/src/metabase/query_processor/compile.clj b/src/metabase/query_processor/compile.clj
index af4b101975b..42bb8dd0b78 100644
--- a/src/metabase/query_processor/compile.clj
+++ b/src/metabase/query_processor/compile.clj
@@ -1,8 +1,11 @@
(ns metabase.query-processor.compile
(:refer-clojure :exclude [compile])
(:require
+ [clojure.set :as set]
[metabase.driver :as driver]
[metabase.lib.core :as lib]
+ [metabase.lib.schema :as lib.schema]
+ [metabase.lib.walk :as lib.walk]
[metabase.query-processor.debug :as qp.debug]
[metabase.query-processor.error-type :as qp.error-type]
[metabase.query-processor.preprocess :as qp.preprocess]
@@ -14,24 +17,59 @@
[metabase.util.malli.registry :as mr]))
(mr/def ::compiled
+ "Compiled query and parameters (SQL or whatever native query language)."
[:map
- [:query :any] ; e.g. a SQL string
+ [:query :any]
[:params {:optional true} [:maybe [:sequential :any]]]])
-(mu/defn- compile*
- [query :- ::qp.schema/any-query]
+(mr/def ::compiled-with-inlined-parameters
+ "Query with inlined parameters (:params must be empty)"
+ [:map
+ [:query :any]
+ [:params {:optional true} [:maybe [:sequential {:max 0} :any]]]])
+
+(mr/def ::query-with-compiled-query
+ "A legacy query that also has a compiled native query attached (unless it was already a native-only query in the first
+ place)."
+ [:merge
+ [:ref ::lib.schema/query]
+ [:map
+ [:qp/compiled ::compiled]
+ ;; If the query was already native, then don't include this because we're not about to go splice the parameters
+ ;; into the existing query. Otherwise we should include this.
+ [:qp/compiled-inline {:optional true} ::compiled-with-inlined-parameters]]])
+
+(defn- native-only?
+ "Whether the query is native-only and thus does not need further compiling; true if the only stage is native. Native
+ stages can only be the first stage, so if the last stage is native it means we have only one stage and this query is
+ native-only."
+ [query]
+ (= (:lib/type (lib/query-stage query -1)) :mbql.stage/native))
+
+;;; TODO (Cam 9/11/25) -- move this to `lib.walk.util` once my PR that adds it finally gets reviewed
+(mu/defn- any-native-stage?
+ "Returns true if any stage of this query is native."
+ [query :- ::lib.schema/query]
+ (let [has-native-stage? (atom false)]
+ (lib.walk/walk-stages
+ query
+ (fn [_query _path stage]
+ (when (= (:lib/type stage) :mbql.stage/native)
+ (reset! has-native-stage? true))
+ nil))
+ @has-native-stage?))
+
+(mu/defn- compile* :- ::compiled
+ [query :- ::lib.schema/query]
(assert (not (:qp/compiled query)) "This query has already been compiled!")
- (if (:lib/type query)
- (recur (lib/->legacy-MBQL query))
- (let [{query-type :type, :as query} query]
- (if (= query-type :native)
- (:native query)
- (driver/mbql->native driver/*driver* query)))))
+ (if (native-only? query)
+ (set/rename-keys (lib/query-stage query -1) {:native :query})
+ (driver/mbql->native driver/*driver* (lib/->legacy-MBQL query))))
(mu/defn compile-preprocessed :- ::compiled
"Compile an already-preprocessed query, if needed. Returns just the resulting 'inner' native query.
`:native` key in a legacy query."
- [preprocessed-query :- ::qp.schema/any-query]
+ [preprocessed-query :- ::lib.schema/query]
(qp.setup/with-qp-setup [preprocessed-query preprocessed-query]
(try
(u/prog1 (compile* preprocessed-query)
@@ -47,24 +85,19 @@
(qp.setup/with-qp-setup [query query]
(compile-preprocessed (qp.preprocess/preprocess query))))
-(mu/defn attach-compiled-query :- ::qp.schema/any-query
+(mu/defn attach-compiled-query :- ::query-with-compiled-query
"If this is an MBQL query, compile it and attach it to the query under the `:qp/compiled` key. Previously, we attached
this under `:native`, but that causes the MBQL schema to blow up. We can't just change this to a regular native
query outright and remove the MBQL `:query`, because that would break perms checks."
- [preprocessed :- ::qp.schema/any-query]
+ [preprocessed :- ::lib.schema/query]
(let [preprocessed (dissoc preprocessed :qp/compiled :qp/compiled-inline)]
- (case ((some-fn :lib/type :type) preprocessed)
- :native preprocessed
- (:mbql/query :query) (assoc preprocessed
- :qp/compiled (compile-preprocessed preprocessed)
- :qp/compiled-inline (binding [driver/*compile-with-inline-parameters* true]
- (compile-preprocessed preprocessed))))))
-
-(mr/def ::compiled-with-inlined-parameters
- [:map
- {:error/message "Query with inlined parameters (:params must be empty)"}
- [:query :any]
- [:params {:optional true} [:maybe [:sequential {:max 0} :any]]]])
+ (-> preprocessed
+ (assoc :qp/compiled (compile-preprocessed preprocessed))
+ ;; if this query is pure-MBQL then we can reliably (re)compile it with inline parameters. If it has any native
+ ;; stage then it might already have parameters, and we're not about to try to splice them back in.
+ (cond-> (not (any-native-stage? preprocessed))
+ (assoc :qp/compiled-inline (binding [driver/*compile-with-inline-parameters* true]
+ (compile-preprocessed preprocessed)))))))
(mu/defn compile-with-inline-parameters :- ::compiled-with-inlined-parameters
"Return the native form for a `query`, with any prepared statement (or equivalent) parameters spliced into the query
diff --git a/src/metabase/query_processor/execute.clj b/src/metabase/query_processor/execute.clj
index cb62b52937a..42321f072a1 100644
--- a/src/metabase/query_processor/execute.clj
+++ b/src/metabase/query_processor/execute.clj
@@ -1,7 +1,7 @@
(ns metabase.query-processor.execute
(:require
[metabase.lib.core :as lib]
- [metabase.lib.schema.id :as lib.schema.id]
+ [metabase.query-processor.compile :as qp.compile]
[metabase.query-processor.middleware.cache :as cache]
[metabase.query-processor.middleware.enterprise :as qp.middleware.enterprise]
[metabase.query-processor.middleware.permissions :as qp.perms]
@@ -12,8 +12,7 @@
[metabase.query-processor.util :as qp.util]
[metabase.util :as u]
[metabase.util.log :as log]
- [metabase.util.malli :as mu]
- [metabase.util.malli.registry :as mr]))
+ [metabase.util.malli :as mu]))
(set! *warn-on-reflection* true)
@@ -24,7 +23,7 @@
{:pre [(map? metadata)]}
(rff (cond-> metadata
(not (:native_form metadata))
- (assoc :native_form ((some-fn :qp/compiled-inline :qp/compiled :native) query)))))]
+ (assoc :native_form ((some-fn :qp/compiled-inline :qp/compiled) query)))))]
(qp query rff*))))
(mu/defn- add-preprocessed-query-to-result-metadata-for-userland-query :- ::qp.schema/qp
@@ -46,7 +45,9 @@
e.g.
- (f (f query rff)) -> (f query rff)"
+ (f (f query rff)) -> (f query rff)
+
+ All of these middlewares assume MBQL 5."
[#'qp.middleware.enterprise/swap-destination-db-middleware
#'qp.middleware.enterprise/apply-impersonation-postprocessing-middleware
#'update-used-cards/update-used-cards!
@@ -55,15 +56,20 @@
#'cache/maybe-return-cached-results
#'qp.perms/check-query-permissions
#'qp.middleware.enterprise/check-download-permissions-middleware
+ ;; TODO (Cam 9/11/25) -- update this middleware to use Lib/MBQL 5. Does a [[lib/->legacy]] call under the hood.
#'qp.middleware.enterprise/maybe-apply-column-level-perms-check-middleware])
(def ^:private execute* nil)
-(defn- run [query rff]
+(mu/defn- run [query :- ::qp.schema/any-query
+ rff :- ::qp.schema/rff]
;; if the query has a `:qp/compiled` key (i.e., this query was compiled from MBQL), rename it to `:native`, so the
;; driver implementations only need to look for one key. Can't really do this any sooner because it will break schema
;; checks in the middleware
(let [query (cond-> query
+ ;; TODO (Cam 9/15/25) -- update this and downstream code (drivers) to handle MBQL 5
+ (:lib/type query) lib/->legacy-MBQL)
+ query (cond-> query
(not (:native query)) (assoc :native (:qp/compiled query)))]
(qp.pipeline/*run* query rff)))
@@ -86,20 +92,10 @@
(log/infof "%s changed, rebuilding %s" varr `execute*)
(rebuild-execute-fn!))))
-(mr/def ::compiled-query
- [:and
- [:map
- [:database ::lib.schema.id/database]]
- [:fn
- {:error/message "Query must be compiled -- should have either :native or :qp/compiled."}
- (some-fn :native :qp/compiled)]])
-
;;; TODO -- consider whether this should return an `IReduceInit` that we can reduce as a separate step.
(mu/defn execute :- some?
"Execute a compiled query, then reduce the results."
- [compiled-query :- ::compiled-query
+ [compiled-query :- ::qp.compile/query-with-compiled-query
rff :- ::qp.schema/rff]
(qp.setup/with-qp-setup [compiled-query compiled-query]
- ;; TODO (Cam 8/20/25) -- all `execute` middleware currently expects legacy MBQL. We need to start updating them to
- ;; expect MBQL 5
- (execute* (lib/->legacy-MBQL compiled-query) rff)))
+ (execute* compiled-query rff)))
diff --git a/src/metabase/query_processor/middleware/cache.clj b/src/metabase/query_processor/middleware/cache.clj
index 4153d4ab092..ea160bb7a94 100644
--- a/src/metabase/query_processor/middleware/cache.clj
+++ b/src/metabase/query_processor/middleware/cache.clj
@@ -12,7 +12,7 @@
[medley.core :as m]
[metabase.cache.core :as cache]
[metabase.config.core :as config]
- [metabase.lib.query :as lib.query]
+ [metabase.lib.core :as lib]
[metabase.query-processor.middleware.cache-backend.db :as backend.db]
[metabase.query-processor.middleware.cache-backend.interface :as i]
[metabase.query-processor.middleware.cache.impl :as impl]
@@ -59,7 +59,8 @@
[object]
(when *in-fn*
(*in-fn* (cond-> object
- (map? object) (m/update-existing :json_query lib.query/serializable)))))
+ (map? object) (-> (m/update-existing :json_query lib/prepare-for-serialization)
+ (m/update-existing :preprocessed_query lib/prepare-for-serialization))))))
(def ^:private ^:dynamic *result-fn*
"The `result-fn` provided by [[impl/do-with-serialization]]."
diff --git a/src/metabase/query_processor/middleware/check_features.clj b/src/metabase/query_processor/middleware/check_features.clj
index d1bdc96cd4e..21bef9825e8 100644
--- a/src/metabase/query_processor/middleware/check_features.clj
+++ b/src/metabase/query_processor/middleware/check_features.clj
@@ -2,21 +2,19 @@
(:require
[metabase.driver :as driver]
[metabase.driver.util :as driver.u]
- ;; legacy usage -- don't use Legacy MBQL utils in QP code going forward, prefer Lib. This will be updated to use
- ;; Lib soon
- ^{:clj-kondo/ignore [:discouraged-namespace]}
- [metabase.legacy-mbql.schema :as mbql.s]
+ [metabase.lib.core :as lib]
[metabase.lib.metadata :as lib.metadata]
- [metabase.lib.util.match :as lib.util.match]
+ [metabase.lib.schema :as lib.schema]
+ [metabase.lib.walk :as lib.walk]
[metabase.query-processor.error-type :as qp.error-type]
- [metabase.query-processor.store :as qp.store]
[metabase.util :as u]
- [metabase.util.i18n :refer [tru]]))
+ [metabase.util.i18n :refer [tru]]
+ [metabase.util.malli :as mu]))
-(defn assert-driver-supports
+(defn- assert-driver-supports
"Assert that the driver/database supports keyword `feature`."
- [feature]
- (let [database (lib.metadata/database (qp.store/metadata-provider))]
+ [metadata-providerable feature]
+ (let [database (lib.metadata/database metadata-providerable)]
(when-not (driver.u/supports? driver/*driver* feature database)
(throw (ex-info (tru "{0} is not supported by {1} driver." (name feature) (name driver/*driver*))
{:type qp.error-type/unsupported-feature
@@ -25,22 +23,26 @@
;; TODO - definitely a little incomplete. It would be cool if we cool look at the metadata in the schema namespace and
;; auto-generate this logic
-(defn- query->required-features [query]
- (into
- #{}
- (lib.util.match/match (:query query)
- :stddev
- :standard-deviation-aggregations
-
- (join :guard (every-pred map? (comp mbql.s/join-strategies :strategy)))
- (let [{:keys [strategy]} join]
- (assert-driver-supports strategy)))))
+(mu/defn- query->required-features
+ [query :- ::lib.schema/query]
+ (let [required-features (volatile! (transient #{}))]
+ (lib.walk/walk-clauses
+ query
+ (fn [_query _path-type _stage-or-join-path clause]
+ (when (lib/clause-of-type? clause :stddev)
+ (vswap! required-features conj! :standard-deviation-aggregations))
+ nil))
+ (lib.walk/walk
+ query
+ (fn [_query path-type _path join]
+ (when (= path-type :lib.walk/join)
+ (vswap! required-features conj! (:strategy join)))
+ nil))
+ (persistent! @required-features)))
(defn check-features
"Middleware that checks that drivers support the `:features` required to use certain clauses, like `:stddev`."
- [{query-type :type, :as query}]
- (if-not (= query-type :query)
- query
- (u/prog1 query
- (doseq [required-feature (query->required-features query)]
- (assert-driver-supports required-feature)))))
+ [query]
+ (u/prog1 query
+ (doseq [required-feature (query->required-features query)]
+ (assert-driver-supports query required-feature))))
diff --git a/src/metabase/query_processor/middleware/fetch_source_query.clj b/src/metabase/query_processor/middleware/fetch_source_query.clj
index e3b3ecdd30a..592652aa8a7 100644
--- a/src/metabase/query_processor/middleware/fetch_source_query.clj
+++ b/src/metabase/query_processor/middleware/fetch_source_query.clj
@@ -5,7 +5,6 @@
[metabase.lib-be.core :as lib-be]
[metabase.lib.card :as lib.card]
[metabase.lib.metadata :as lib.metadata]
- [metabase.lib.metadata.protocols :as lib.metadata.protocols]
[metabase.lib.query :as lib.query]
[metabase.lib.schema :as lib.schema]
[metabase.lib.schema.common :as lib.schema.common]
@@ -13,7 +12,6 @@
[metabase.lib.schema.metadata :as lib.schema.metadata]
[metabase.lib.walk :as lib.walk]
[metabase.query-processor.error-type :as qp.error-type]
- [metabase.query-processor.store :as qp.store]
[metabase.query-processor.util.persisted-cache :as qp.persisted]
[metabase.system.core :as system]
[metabase.util :as u]
@@ -165,9 +163,9 @@
TODO -- we should remove remove the `:dataset` key and make sure nothing breaks, and make sure everything is looking
at `:model` instead."
- [{:qp/keys [source-card-id], :as _preprocessed-query} rff]
+ [{:qp/keys [source-card-id], :as preprocessed-query} rff]
(if-not source-card-id
rff
- (let [model? (= (:type (lib.metadata.protocols/card (qp.store/metadata-provider) source-card-id)) :model)]
+ (let [model? (= (:type (lib.metadata/card preprocessed-query source-card-id)) :model)]
(fn rff' [metadata]
(rff (cond-> metadata model? (assoc :dataset model?, :model model?)))))))
diff --git a/src/metabase/query_processor/middleware/large_int.clj b/src/metabase/query_processor/middleware/large_int.clj
index 51695e0c29e..a2398cfac5a 100644
--- a/src/metabase/query_processor/middleware/large_int.clj
+++ b/src/metabase/query_processor/middleware/large_int.clj
@@ -78,7 +78,7 @@
(defn convert-large-int-to-string
"Converts any large integer in a result to a string to handle a number > 2^51 or < -2^51, the JavaScript float
mantissa. This will allow proper display of large integers, like IDs from services like social media."
- [{{:keys [js-int-to-string?] :or {js-int-to-string? false}} :middleware} rff]
+ [{{:keys [js-int-to-string?] :or {js-int-to-string? false}} :middleware, :as _query} rff]
(let [rff' (when js-int-to-string?
(fn [metadata]
(let [mask (column-index-mask (:cols metadata))]
diff --git a/src/metabase/query_processor/middleware/results_metadata.clj b/src/metabase/query_processor/middleware/results_metadata.clj
index 93a91daf557..788aed88f61 100644
--- a/src/metabase/query_processor/middleware/results_metadata.clj
+++ b/src/metabase/query_processor/middleware/results_metadata.clj
@@ -8,11 +8,8 @@
[metabase.analyze.core :as analyze]
[metabase.driver :as driver]
[metabase.driver.util :as driver.u]
- ;; legacy usage -- don't use Legacy MBQL utils in QP code going forward, prefer Lib. This will be updated to use
- ;; Lib soon
- ^{:clj-kondo/ignore [:discouraged-namespace]}
- [metabase.legacy-mbql.schema :as mbql.s]
[metabase.lib.metadata :as lib.metadata]
+ [metabase.lib.schema :as lib.schema]
[metabase.lib.schema.metadata :as lib.schema.metadata]
[metabase.query-processor.reducible :as qp.reducible]
[metabase.query-processor.schema :as qp.schema]
@@ -57,7 +54,7 @@
(mapv standardize-metadata metadata)))
(mu/defn- record-metadata!
- [{{:keys [card-id]} :info, :as query} :- ::mbql.s/Query
+ [{{:keys [card-id]} :info, :as query} :- ::lib.schema/query
metadata :- [:maybe [:sequential ::lib.schema.metadata/lib-or-legacy-column]]]
(try
;; At the very least we can skip the Extra DB call to update this Card's metadata results
@@ -70,7 +67,7 @@
driver/*driver*
;; pivot queries can run multiple queries, only record metadata for the main query
(not= actual-metadata :none)
- (driver.u/supports? driver/*driver* :nested-queries (lib.metadata/database (qp.store/metadata-provider)))
+ (driver.u/supports? driver/*driver* :nested-queries (lib.metadata/database query))
card-id
;; don't want to update metadata when we use a Card as a source Card.
(not (:qp/source-card-id query))
diff --git a/src/metabase/query_processor/postprocess.clj b/src/metabase/query_processor/postprocess.clj
index a02fda6f5ad..6855855dbf9 100644
--- a/src/metabase/query_processor/postprocess.clj
+++ b/src/metabase/query_processor/postprocess.clj
@@ -30,20 +30,20 @@
Where `rff` has the form
(f metadata) -> rf"
- [[::legacy #'format-rows/format-rows]
- [::legacy #'results-metadata/record-and-return-metadata!]
+ [[::mbql5 #'format-rows/format-rows]
+ [::mbql5 #'results-metadata/record-and-return-metadata!]
[::mbql5 #'limit/limit-result-rows]
- [::legacy #'qp.middleware.enterprise/limit-download-result-rows]
+ [::mbql5 #'qp.middleware.enterprise/limit-download-result-rows]
[::legacy #'qp.add-rows-truncated/add-rows-truncated]
- [::legacy #'qp.add-timezone-info/add-timezone-info]
- [::legacy #'qp.middleware.enterprise/merge-sandboxing-metadata]
- [::legacy #'qp.add-remaps/remap-results]
- [::legacy #'pivot-export/add-data-for-pivot-export]
- [::legacy #'large-int/convert-large-int-to-string]
+ [::mbql5 #'qp.add-timezone-info/add-timezone-info]
+ [::mbql5 #'qp.middleware.enterprise/merge-sandboxing-metadata]
+ [::mbql5 #'qp.add-remaps/remap-results]
+ [::mbql5 #'pivot-export/add-data-for-pivot-export]
+ [::mbql5 #'large-int/convert-large-int-to-string]
[::legacy #'viz-settings/update-viz-settings]
[::legacy #'qp.cumulative-aggregations/sum-cumulative-aggregation-columns]
[::mbql5 #'annotate/add-column-info]
- [::legacy #'fetch-source-query/add-dataset-info]])
+ [::mbql5 #'fetch-source-query/add-dataset-info]])
;; ↑↑↑ POST-PROCESSING ↑↑↑ happens from BOTTOM TO TOP
(mu/defn post-processing-rff :- ::qp.schema/rff
diff --git a/src/metabase/query_processor/preprocess.clj b/src/metabase/query_processor/preprocess.clj
index 6487000b699..90ea02d8d9f 100644
--- a/src/metabase/query_processor/preprocess.clj
+++ b/src/metabase/query_processor/preprocess.clj
@@ -118,8 +118,8 @@
(ensure-mbql5 #'qp.middleware.enterprise/apply-impersonation)
(ensure-mbql5 #'qp.middleware.enterprise/attach-destination-db-middleware)
(ensure-mbql5 #'qp.middleware.enterprise/apply-sandboxing)
- (ensure-legacy #'qp.persistence/substitute-persisted-query)
- (ensure-mbql5 #'qp.add-implicit-clauses/add-implicit-clauses)
+ (ensure-mbql5 #'qp.persistence/substitute-persisted-query)
+ (ensure-mbql5 #'qp.add-implicit-clauses/add-implicit-clauses) ; #61398
;; this needs to be done twice, once before adding remaps (since we want to add remaps inside joins) and then again
;; after adding any implicit joins. Implicit joins do not need to get remaps since we only use them for fetching
;; specific columns.
@@ -141,10 +141,8 @@
(ensure-mbql5 #'validate-temporal-bucketing/validate-temporal-bucketing)
(ensure-mbql5 #'optimize-temporal-filters/optimize-temporal-filters)
(ensure-mbql5 #'limit/add-default-limit)
- (ensure-legacy #'qp.middleware.enterprise/apply-download-limit)
- (ensure-legacy #'check-features/check-features)
- ;; return MBQL 5 at the end
- (ensure-mbql5 identity)])
+ (ensure-mbql5 #'qp.middleware.enterprise/apply-download-limit)
+ (ensure-mbql5 #'check-features/check-features)])
(defn- middleware-fn-name [middleware-fn]
(if-let [fn-name (:name (meta middleware-fn))]
diff --git a/test/metabase/driver/common/parameters/values_test.clj b/test/metabase/driver/common/parameters/values_test.clj
index b319fb8b180..508d8debdb7 100644
--- a/test/metabase/driver/common/parameters/values_test.clj
+++ b/test/metabase/driver/common/parameters/values_test.clj
@@ -346,13 +346,13 @@
[{:database (meta/id)
:type "native"
:native {:query test-query}}])
- (is (= {:card-id 1, :query test-query, :params nil}
- (value-for-tag
- {:name "card-template-tag-test"
- :display-name "Card template tag test"
- :type :card
- :card-id 1}
- []))))))))
+ (is (=? {:card-id 1, :query test-query, :params nil}
+ (value-for-tag
+ {:name "card-template-tag-test"
+ :display-name "Card template tag test"
+ :type :card
+ :card-id 1}
+ []))))))))
(deftest ^:parallel card-query-test-2
(mt/with-test-user :rasta
diff --git a/test/metabase/lib/serialize_test.cljc b/test/metabase/lib/serialize_test.cljc
index ba9b977e694..043ec306888 100644
--- a/test/metabase/lib/serialize_test.cljc
+++ b/test/metabase/lib/serialize_test.cljc
@@ -76,11 +76,11 @@
(is (= {:lib/type :mbql/query
:stages [{:lib/type :mbql.stage/native
:native "SELECT *"
- :args ["2022-10-03T00:00"]}]
+ :params ["2022-10-03T00:00"]}]
:database 1}
(lib.serialize/prepare-for-serialization
{:lib/type :mbql/query
:stages [{:lib/type :mbql.stage/native
:native "SELECT *"
- :args [#t "2022-10-03T00:00"]}]
+ :params [#t "2022-10-03T00:00"]}]
:database 1})))))
diff --git a/test/metabase/query_processor/middleware/check_features_test.clj b/test/metabase/query_processor/middleware/check_features_test.clj
new file mode 100644
index 00000000000..2d27d43ad3c
--- /dev/null
+++ b/test/metabase/query_processor/middleware/check_features_test.clj
@@ -0,0 +1,47 @@
+(ns metabase.query-processor.middleware.check-features-test
+ (:require
+ [clojure.test :refer :all]
+ [metabase.driver :as driver]
+ [metabase.lib.core :as lib]
+ [metabase.lib.test-metadata :as meta]
+ [metabase.query-processor.middleware.check-features :as check-features]))
+
+(driver/register! ::mock-driver, :abstract? true)
+
+(defmethod driver/database-supports? [::mock-driver :standard-deviation-aggregations]
+ [_driver _feature _database]
+ false)
+
+(defmethod driver/database-supports? [::mock-driver :left-join]
+ [_driver _feature _database]
+ true)
+
+(defmethod driver/database-supports? [::mock-driver :right-join]
+ [_driver _feature _database]
+ false)
+
+(deftest ^:parallel check-join-strategies-test
+ (driver/with-driver ::mock-driver
+ (let [query-with-join (fn [strategy]
+ (-> (lib/query meta/metadata-provider (meta/table-metadata :venues))
+ (lib/join (-> (lib/join-clause (meta/table-metadata :categories))
+ (lib/with-join-strategy strategy)))))]
+ (testing ":left-join should be ok"
+ (let [query (query-with-join :left-join)]
+ (is (= query
+ (check-features/check-features query)))))
+ (testing ":right-join should trigger an error"
+ (let [query (query-with-join :right-join)]
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"\Qright-join is not supported by mock-driver driver.\E"
+ (check-features/check-features query))))))))
+
+(deftest ^:parallel check-standard-deviations-test
+ (driver/with-driver ::mock-driver
+ (let [query (-> (lib/query meta/metadata-provider (meta/table-metadata :venues))
+ (lib/aggregate (lib/stddev (meta/field-metadata :venues :price))))]
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"\Qstandard-deviation-aggregations is not supported by mock-driver driver.\E"
+ (check-features/check-features query))))))
diff --git a/test/metabase/query_processor_test/parameters_test.clj b/test/metabase/query_processor_test/parameters_test.clj
index 98e411a40ab..9363ad95993 100644
--- a/test/metabase/query_processor_test/parameters_test.clj
+++ b/test/metabase/query_processor_test/parameters_test.clj
@@ -287,12 +287,15 @@
(is (= [[1]]
(mt/formatted-rows [int] results)))))))
+;;; TODO (Cam 9/11/25) -- add an "airports" mock metadata provider and update tests here so they use Mock MPs and not
+;;; the app DB at all
(deftest ^:parallel native-with-spliced-params-test
(testing "Make sure we can convert a parameterized query to a native query with spliced params"
(testing "Multiple values"
(mt/dataset airports
- (is (= {:query "SELECT NAME FROM COUNTRY WHERE \"PUBLIC\".\"COUNTRY\".\"NAME\" IN ('US', 'MX')"
- :params []}
+ (is (= {:lib/type :mbql.stage/native
+ :query "SELECT NAME FROM COUNTRY WHERE \"PUBLIC\".\"COUNTRY\".\"NAME\" IN ('US', 'MX')"
+ :params []}
(qp.compile/compile-with-inline-parameters
{:type :native
:native {:query "SELECT NAME FROM COUNTRY WHERE {{country}}"
@@ -310,8 +313,9 @@
(deftest ^:parallel native-with-param-options-different-than-tag-type-test
(testing "Overriding the widget type in parameters should drop case-senstive option when incompatible"
(mt/dataset airports
- (is (= {:query "SELECT NAME FROM COUNTRY WHERE (\"PUBLIC\".\"COUNTRY\".\"NAME\" = 'US')"
- :params []}
+ (is (= {:lib/type :mbql.stage/native
+ :query "SELECT NAME FROM COUNTRY WHERE (\"PUBLIC\".\"COUNTRY\".\"NAME\" = 'US')"
+ :params []}
(qp.compile/compile-with-inline-parameters
{:type :native
:native {:query "SELECT NAME FROM COUNTRY WHERE {{country}}"
@@ -330,8 +334,9 @@
(deftest ^:parallel native-with-param-options-different-than-tag-type-test-2
(testing "Overriding the widget type in parameters should not drop case-senstive option when compatible"
(mt/dataset airports
- (is (= {:query "SELECT NAME FROM COUNTRY WHERE (LOWER(\"PUBLIC\".\"COUNTRY\".\"NAME\") LIKE '%us')"
- :params []}
+ (is (= {:lib/type :mbql.stage/native
+ :query "SELECT NAME FROM COUNTRY WHERE (LOWER(\"PUBLIC\".\"COUNTRY\".\"NAME\") LIKE '%us')"
+ :params []}
(qp.compile/compile-with-inline-parameters
{:type :native
:native {:query "SELECT NAME FROM COUNTRY WHERE {{country}}"
@@ -350,8 +355,9 @@
(deftest ^:parallel native-with-spliced-params-test-2
(testing "Make sure we can convert a parameterized query to a native query with spliced params"
(testing "Comma-separated numbers"
- (is (= {:query "SELECT * FROM VENUES WHERE \"PUBLIC\".\"VENUES\".\"PRICE\" IN (1, 2)"
- :params []}
+ (is (= {:lib/type :mbql.stage/native
+ :query "SELECT * FROM VENUES WHERE \"PUBLIC\".\"VENUES\".\"PRICE\" IN (1, 2)"
+ :params []}
(qp.compile/compile-with-inline-parameters
{:type :native
:native {:query "SELECT * FROM VENUES WHERE {{price}}"
@@ -370,45 +376,48 @@
(testing "Make sure we can convert a parameterized query to a native query with spliced params"
(testing "Comma-separated numbers in a number field"
;; this is an undocumented feature but lots of people rely on it, so we want it to continue working.
- (is (= {:query "SELECT * FROM VENUES WHERE price IN (1, 2, 3)"
- :params []}
+ (is (= {:lib/type :mbql.stage/native
+ :query "SELECT * FROM VENUES WHERE price IN (1, 2, 3)"
+ :params []}
(qp.compile/compile-with-inline-parameters
- {:type :native
- :native {:query "SELECT * FROM VENUES WHERE price IN ({{number_comma}})"
- :template-tags {"number_comma"
- {:name "number_comma"
- :display-name "Number Comma"
- :type :number}}}
- :database (mt/id)
- :parameters [{:type "number/="
- :value ["1,2,3"]
+ {:type :native
+ :native {:query "SELECT * FROM VENUES WHERE price IN ({{number_comma}})"
+ :template-tags {"number_comma"
+ {:name "number_comma"
+ :display-name "Number Comma"
+ :type :number}}}
+ :database (mt/id)
+ :parameters [{:type "number/="
+ :value ["1,2,3"]
:target [:variable [:template-tag "number_comma"]]}]}))))))
(deftest ^:parallel native-with-spliced-params-test-4
(testing "Make sure we can convert a parameterized query to a native query with spliced params"
(testing "Trailing commas do not cause errors"
;; this is an undocumented feature but lots of people rely on it, so we want it to continue working.
- (is (= {:query "SELECT * FROM VENUES WHERE price IN (1, 2)"
- :params []}
+ (is (= {:lib/type :mbql.stage/native
+ :query "SELECT * FROM VENUES WHERE price IN (1, 2)"
+ :params []}
(qp.compile/compile-with-inline-parameters
- {:type :native
- :native {:query "SELECT * FROM VENUES WHERE price IN ({{number_comma}})"
- :template-tags {"number_comma"
- {:name "number_comma"
- :display-name "Number Comma"
- :type :number
- :dimension [:field (mt/id :venues :price) nil]}}}
- :database (mt/id)
- :parameters [{:type "number/="
- :value ["1,2,"]
+ {:type :native
+ :native {:query "SELECT * FROM VENUES WHERE price IN ({{number_comma}})"
+ :template-tags {"number_comma"
+ {:name "number_comma"
+ :display-name "Number Comma"
+ :type :number
+ :dimension [:field (mt/id :venues :price) nil]}}}
+ :database (mt/id)
+ :parameters [{:type "number/="
+ :value ["1,2,"]
:target [:variable [:template-tag "number_comma"]]}]}))))))
(deftest ^:parallel params-in-comments-test
(testing "Params in SQL comments are ignored"
(testing "Single-line comments"
(mt/dataset airports
- (is (= {:query "SELECT NAME FROM COUNTRY WHERE \"PUBLIC\".\"COUNTRY\".\"NAME\" IN ('US', 'MX') -- {{ignoreme}}"
- :params []}
+ (is (= {:lib/type :mbql.stage/native
+ :query "SELECT NAME FROM COUNTRY WHERE \"PUBLIC\".\"COUNTRY\".\"NAME\" IN ('US', 'MX') -- {{ignoreme}}"
+ :params []}
(qp.compile/compile-with-inline-parameters
{:type :native
:native {:query "SELECT NAME FROM COUNTRY WHERE {{country}} -- {{ignoreme}}"
@@ -426,8 +435,9 @@
(deftest ^:parallel params-in-comments-test-2
(testing "Params in SQL comments are ignored"
(testing "Multi-line comments"
- (is (= {:query "SELECT * FROM VENUES WHERE\n/*\n{{ignoreme}}\n*/ \"PUBLIC\".\"VENUES\".\"PRICE\" IN (1, 2)"
- :params []}
+ (is (= {:lib/type :mbql.stage/native
+ :query "SELECT * FROM VENUES WHERE\n/*\n{{ignoreme}}\n*/ \"PUBLIC\".\"VENUES\".\"PRICE\" IN (1, 2)"
+ :params []}
(qp.compile/compile-with-inline-parameters
{:type :native
:native {:query "SELECT * FROM VENUES WHERE\n/*\n{{ignoreme}}\n*/ {{price}}"
@@ -625,8 +635,9 @@
(deftest ^:parallel inlined-number-test
(testing "Number parameters are inlined into the SQL query and not parameterized (#29690)"
(mt/dataset test-data
- (is (= {:query "SELECT NOW() - INTERVAL '30 DAYS'"
- :params []}
+ (is (= {:lib/type :mbql.stage/native
+ :query "SELECT NOW() - INTERVAL '30 DAYS'"
+ :params []}
(qp.compile/compile-with-inline-parameters
{:type :native
:native {:query "SELECT NOW() - INTERVAL '{{n}} DAYS'"
@@ -635,10 +646,10 @@
:display-name "n"
:type :number}}}
:database (mt/id)
- :parameters [{:type :number
+ :parameters [{:type :number
:target [:variable [:template-tag "n"]]
- :slug "n"
- :value "30"}]}))))))
+ :slug "n"
+ :value "30"}]}))))))
(deftest sql-permissions-but-no-card-permissions-template-tag-test
(testing "If we have full SQL perms for a DW but no Card perms we shouldn't be able to include it with a ref or template tag"
diff --git a/test/metabase/query_processor_test/query_to_native_test.clj b/test/metabase/query_processor_test/query_to_native_test.clj
index ba6fa877ee0..43b634a89bc 100644
--- a/test/metabase/query_processor_test/query_to_native_test.clj
+++ b/test/metabase/query_processor_test/query_to_native_test.clj
@@ -21,19 +21,22 @@
(deftest ^:parallel already-native-test
(testing "If query is already native, `compile` should still do stuff like parsing parameters"
- (is (= {:query "SELECT * FROM VENUES WHERE price = 3;"
- :params []}
+ (is (= {:lib/type :mbql.stage/native
+ :query "SELECT * FROM VENUES WHERE price = 3;"
+ :params []}
(qp.compile/compile
{:database (mt/id)
:type :native
:native {:query "SELECT * FROM VENUES [[WHERE price = {{price}}]];"
:template-tags {"price" {:name "price", :display-name "Price", :type :number, :required false}}}
- :parameters [{:type "category", :target [:variable [:template-tag "price"]], :value 3}]}))))
+ :parameters [{:type "category", :target [:variable [:template-tag "price"]], :value 3}]})))))
+
+(deftest ^:parallel already-native-test-2
(testing "If query is already native, `compile` should not execute the query (metabase#13572)"
;; 1000,000,000 rows, no way this will finish in 2 seconds if executed
(let [long-query "SELECT CHECKINS.* FROM CHECKINS LEFT JOIN CHECKINS C2 ON 1=1 LEFT JOIN CHECKINS C3 ON 1=1"]
(u/with-timeout 2000
- (is (= {:query long-query}
+ (is (= {:lib/type :mbql.stage/native, :query long-query}
(qp.compile/compile
{:database (mt/id)
:type :native