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
Tidy query preprocessing, dataset streaming, and permission helpers (#75551)
Nathan Voxland4d1d85814c87
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.ednindex 409ee858184..576c26ac848 100644--- a/.clj-kondo/config/modules/config.edn+++ b/.clj-kondo/config/modules/config.edn@@ -1792,7 +1792,8 @@metabase.query-processor.streaming.interfacemetabase.query-processor.timezonemetabase.query-processor.util- metabase.query-processor.util.add-alias-info}+ metabase.query-processor.util.add-alias-info+ metabase.query-processor.util.persisted-cache}:uses #{analyticsanalytics-interfaceanalyzediff --git a/src/metabase/documents/api/document.clj b/src/metabase/documents/api/document.cljindex 7b18cc7d0e1..f1a950de404 100644--- a/src/metabase/documents/api/document.clj+++ b/src/metabase/documents/api/document.clj@@ -56,7 +56,7 @@(defn- create-card!"Checks that the query is runnable by the current user then saves"[{query :dataset_query :as card} creator]- (query-perms/check-run-permissions-for-query query)+ (query-perms/check-run-permissions-for-query (dissoc query :query-permissions/perms))(card/create-card! (assoc card :type :question :dashboard_id nil) creator))(mu/defn- update-cards-in-ast :- [:map [:document :any]diff --git a/src/metabase/driver/sql/query_processor.clj b/src/metabase/driver/sql/query_processor.cljindex 0adf3a69b5c..d0607f34e41 100644--- a/src/metabase/driver/sql/query_processor.clj+++ b/src/metabase/driver/sql/query_processor.clj@@ -12,6 +12,8 @@[metabase.driver-api.core :as driver-api][metabase.driver.common :as driver.common][metabase.driver.sql.query-processor.deprecated :as sql.qp.deprecated]+ [metabase.lib.metadata.protocols :as lib.metadata.protocols]+ [metabase.query-processor.util.persisted-cache :as qp.persisted][metabase.util :as u][metabase.util.date-2 :as u.date][metabase.util.experiment :as experiment]@@ -2188,16 +2190,33 @@(> (count source-aliases) 1)(not (apply distinct? source-aliases)))))+(defn- resolve-persisted-source-sql+ "If `source-query` came from a card with a valid persisted cache, independently look up the cache from the metadata+ provider and return the native SQL. Returns nil if no cache exists, or if persisted cache use has been disabled for+ this query (e.g. due to sandboxing or connection impersonation — see+ [[metabase.query-processor.middleware.persistence/substitute-persisted-query]])."+ [source-query]+ (when-not (:qp/skip-persisted-cache source-query)+ (when-let [card-id (:qp/stage-is-from-source-card source-query)]+ (let [mp (driver-api/metadata-provider)+ card (lib.metadata.protocols/card mp card-id)]+ (when-let [persisted-info (:lib/persisted-info card)]+ (when (qp.persisted/can-substitute? card persisted-info)+ (qp.persisted/persisted-info-native-query+ (:id (lib.metadata.protocols/database mp))+ persisted-info)))))))+(defn- apply-source-query"Handle a `:source-query` clause by adding a recursive `SELECT` or native query. If the source query has ambiguouscolumn names, use a `WITH` statement to rename the source columns. At the time of this writing, all source queriesare aliased as `__mb_source`."- [driver honeysql-form {{:keys [native params] persisted :persisted-info/native :as source-query} :source-query+ [driver honeysql-form {{:keys [native params] :as source-query} :source-querysource-metadata :source-metadata}](let [table-alias (->honeysql driver (h2x/identifier :table-alias source-query-alias))+ persisted-sql (resolve-persisted-source-sql source-query)source-clause (cond- persisted- (sql-source-query persisted nil)+ persisted-sql+ (sql-source-query persisted-sql nil)native(sql-source-query native params)diff --git a/src/metabase/queries_rest/api/card.clj b/src/metabase/queries_rest/api/card.cljindex d39315dc075..edda84f8d54 100644--- a/src/metabase/queries_rest/api/card.clj+++ b/src/metabase/queries_rest/api/card.clj@@ -573,8 +573,10 @@(update :collection_id #(eid-translation/->id-or-404 :collection %))))query (:dataset_query card)](check-if-card-can-be-saved query card-type)- ;; check that we have permissions to run the query that we're trying to save- (query-perms/check-run-permissions-for-query query)+ ;; check that we have permissions to run the query that we're trying to save.+ ;; Strip :query-permissions/perms first -- it is populated internally by the QP+ ;; middleware, so any value already on the incoming query is dropped here.+ (query-perms/check-run-permissions-for-query (dissoc query :query-permissions/perms));; check that we have permissions for the collection we're trying to save this card to, if applicable.;; if a `dashboard-id` is specified, check permissions on the *dashboard's* collection ID.(api/create-check :model/Card {:collection_id (actual-collection-id card)})@@ -614,7 +616,7 @@[card-before-updates :- ::queries.schema/cardcard-updates :- ::queries.schema/card](when (api/column-will-change? :dataset_query card-before-updates card-updates)- (query-perms/check-run-permissions-for-query (:dataset_query card-updates))))+ (query-perms/check-run-permissions-for-query (dissoc (:dataset_query card-updates) :query-permissions/perms))))(defn- check-allowed-to-change-embedding"You must be a superuser to change the value of `enable_embedding`, `embedding_type` or `embedding_params`. Embedding must bediff --git a/src/metabase/query_permissions/impl.clj b/src/metabase/query_permissions/impl.cljindex d1877f35fad..8042078afe2 100644--- a/src/metabase/query_permissions/impl.clj+++ b/src/metabase/query_permissions/impl.clj@@ -283,12 +283,15 @@{:query query}))))))(defn- has-perm-for-db?+ "Checks that the current user has at least `required-perm` for the entire DB specified by `db-id`."[perm-type required-perm db-id](perms/at-least-as-permissive? perm-type(perms/full-db-permission-for-user api/*current-user-id* perm-type db-id)required-perm))(defn- has-perm-for-table?+ "Checks that the current user has the permissions for tables specified in `table-id->perm`. Returns true if access+ is allowed, otherwise false."[perm-type table-id->required-perm db-id](every? (fn [[table-id required-perm]](perms/user-has-permission-for-table?@@ -366,7 +369,9 @@{:card-id card-id}))))))(defn check-data-perms- "Checks whether the current user has sufficient view data and query permissions to run `query`."+ "Checks whether the current user has sufficient view data and query permissions to run `query`. Returns `true` if the+ user has perms for the query, and throws an exception otherwise (exceptions can be disabled by setting+ `throw-exceptions?` to `false`)."[query required-perms & {:keys [throw-exceptions?]:or {throw-exceptions? true}}](trydiff --git a/src/metabase/query_processor/api.clj b/src/metabase/query_processor/api.cljindex 5c035ad5425..966650473d0 100644--- a/src/metabase/query_processor/api.clj+++ b/src/metabase/query_processor/api.clj@@ -85,9 +85,13 @@(:constraints query))](qp.pivot/run-pivot-query (-> query(assoc :constraints constraints)+ ;; Use assoc rather than merge so :info comes entirely from the+ ;; server-built map. :info carries :card-id and similar fields the+ ;; server derives, so we replace it rather than combining it with+ ;; whatever was on the incoming query.(assoc :info info))rff))- (qp/process-query (update query :info merge info) rff))))))+ (qp/process-query (assoc query :info info) rff))))))(api.macros/defendpoint :post "/":- (server/streaming-response-schema ::qp.schema/query-result)diff --git a/src/metabase/query_processor/middleware/permissions.clj b/src/metabase/query_processor/middleware/permissions.cljindex f409aec7965..dc6378183b4 100644--- a/src/metabase/query_processor/middleware/permissions.clj+++ b/src/metabase/query_processor/middleware/permissions.clj@@ -92,7 +92,9 @@(dissoc stage-or-join :query-permissions/sandboxed-table))))(defn remove-persisted-info-native-keys- "Strips `:persisted-info/native` from query stages."+ "Pre-processing middleware. Removes any `:persisted-info/native` keys from the query. This key is populated later by+ the fetch-source-query middleware to point at a persisted/cached native query, so any value already present at this+ stage is stale and is cleared (like the functions above)."[query](lib.walk/walkquerydiff --git a/src/metabase/query_processor/middleware/persistence.clj b/src/metabase/query_processor/middleware/persistence.cljindex 09d9d24ff7e..a02e4059474 100644--- a/src/metabase/query_processor/middleware/persistence.clj+++ b/src/metabase/query_processor/middleware/persistence.clj@@ -18,5 +18,7 @@(perms/impersonation-enforced-for-db? (:database query))))(match/replace query{:persisted-info/native &truthy}- (dissoc &match :persisted-info/native))+ ;; Signal to the SQL QP's independent persisted-cache lookup that it should not use the cache for this+ ;; query. See [[metabase.driver.sql.query-processor/resolve-persisted-source-sql]].+ (-> &match (dissoc :persisted-info/native) (assoc :qp/skip-persisted-cache true)))query))diff --git a/src/metabase/query_processor/pivot.clj b/src/metabase/query_processor/pivot.cljindex 9d3f7e7b03e..cb3d209be78 100644--- a/src/metabase/query_processor/pivot.clj+++ b/src/metabase/query_processor/pivot.clj@@ -504,6 +504,10 @@([query :- ::qp.schema/any-queryrff :- [:maybe ::qp.schema/rff]](log/debugf "Running pivot query:\n%s" (u/pprint-to-str query))+ ;; Do not bind *card-id* here. Callers that run pivot queries for saved cards+ ;; (e.g. card.clj, dashboards) bind *card-id* themselves before calling+ ;; run-pivot-query, so binding it here from the query's :info map would be+ ;; redundant and could mis-set it for ad-hoc queries that carry a :card-id in :info.(qp.setup/with-qp-setup [query query](let [query (qp.middleware.normalize/normalize-preprocessing-middleware query) ; normalize to MBQL 5 if needed.rff (or rff qp.reducible/default-rff)diff --git a/src/metabase/revisions/api.clj b/src/metabase/revisions/api.cljindex 1d53b299d0c..5e1509aa4dd 100644--- a/src/metabase/revisions/api.clj+++ b/src/metabase/revisions/api.clj@@ -63,7 +63,7 @@(when (= model :model/Card);; TODO -- we should be using something like `api/read-check` for this, but unfortunately the impl for Cards;; doesn't actually check important stuff like this.- (query-perms/check-run-permissions-for-query (get-in revision [:object :dataset_query])))+ (query-perms/check-run-permissions-for-query (dissoc (get-in revision [:object :dataset_query]) :query-permissions/perms)));; ok, we're g2g(revision/revert!{:entity modeldiff --git a/test/metabase/query_processor/middleware/permissions_test.clj b/test/metabase/query_processor/middleware/permissions_test.cljindex 490235cbf38..80ec570ad13 100644--- a/test/metabase/query_processor/middleware/permissions_test.clj+++ b/test/metabase/query_processor/middleware/permissions_test.clj@@ -6,10 +6,13 @@[clojure.test :refer :all][metabase.api.common :as api][metabase.lib-be.core :as lib-be]+ [metabase.lib.core :as lib]+ [metabase.lib.metadata :as lib.metadata][metabase.lib.test-metadata :as meta][metabase.permissions.core :as perms][metabase.query-processor.middleware.permissions :as qp.perms][metabase.query-processor.pipeline :as qp.pipeline]+ [metabase.query-processor.pivot :as qp.pivot][metabase.query-processor.setup :as qp.setup]^{:clj-kondo/ignore [:deprecated-namespace]} [metabase.query-processor.store :as qp.store][metabase.query-processor.test :as qp]@@ -729,12 +732,13 @@clojure.lang.ExceptionInfo#"You do not have permissions to run this query"(qp/process-query (mt/mbql-query venues {:limit 1})))))- (is (thrown-with-msg?- clojure.lang.ExceptionInfo- #"You do not have permissions to run this query"- (qp/process-query (assoc (mt/mbql-query venues {:limit 1})- :query-permissions/perms {:gtaps {:perms/view-data :unrestricted- :perms/create-queries {(mt/id :venues) :query-builder}}}))))))))+ (testing "Query carrying a :query-permissions/perms value is still rejected"+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"You do not have permissions to run this query"+ (qp/process-query (assoc (mt/mbql-query venues {:limit 1})+ :query-permissions/perms {:gtaps {:perms/view-data :unrestricted+ :perms/create-queries {(mt/id :venues) :query-builder}}})))))))))(deftest e2e-ignore-user-supplied-sandboxed-tables-test(testing "You shouldn't be able to bypass security restrictions by passing in `:query-permissions/sandboxed-table` in the query"@@ -746,23 +750,11 @@:query-permissions/perms {:gtaps {:perms/view-data :unrestricted:perms/create-queries :query-builder-and-native}}}](mt/with-test-user :rasta- (testing "Sanity check: should not be able to run this query the normal way"- (is (thrown-with-msg?- clojure.lang.ExceptionInfo- #"You do not have permissions to run this query"- (qp/process-query bad-query))))- (letfn [(process-query []- (qp/process-query bad-query))]- (testing "Testing that we will still throw due to the :query-permissions/perms stripping"- (with-redefs [qp.perms/remove-sandboxed-table-keys identity]- (is (thrown-with-msg?- clojure.lang.ExceptionInfo- #"You do not have permissions to run this query"- (process-query)))))+ (testing "Query carrying :query-permissions/sandboxed-table and perms keys is still rejected"(is (thrown-with-msg?clojure.lang.ExceptionInfo#"You do not have permissions to run this query"- (process-query)))))))))+ (qp/process-query bad-query)))))))))(deftest e2e-ignore-user-supplied-compiled-from-mbql-key(testing "Make sure the NATIVE query fails to run if current user doesn't have perms even if you try to include an MBQL :query"@@ -2205,3 +2197,230 @@:condition [:= $id [:field (mt/id :users :id) {:join-alias "u"}]]}]:fields [$id $name]:limit 2})))))))))))))++;;; +----------------------------------------------------------------------------------------------------------------++;;; | Persisted-info source-query handling tests |+;;; +----------------------------------------------------------------------------------------------------------------+++(deftest e2e-persisted-info-native-source-query-test+ (testing "persisted-info/native on an incoming source-query is resolved from the source-table, not the query map"+ (testing "a query whose source-query carries persisted-info/native still reads from the source-table"+ (mt/with-no-data-perms-for-all-users!+ (perms/set-database-permission! (perms/all-users-group) (mt/id) :perms/view-data :unrestricted)+ (perms/set-table-permission! (perms/all-users-group) (mt/id :venues) :perms/create-queries :query-builder)+ (perms/set-table-permission! (perms/all-users-group) (mt/id :people) :perms/create-queries :no)+ (perms/set-table-permission! (perms/all-users-group) (mt/id :orders) :perms/create-queries :no)+ (mt/with-test-user :rasta+ (let [mp (mt/metadata-provider)+ venues-query (-> (lib/query mp (lib.metadata/table mp (mt/id :venues)))+ (lib/limit 1))]+ (testing "Sanity check: user can query venues normally"+ (is (=? {:status :completed}+ (qp/process-query venues-query))))+ (testing "Sanity check: user cannot query people normally"+ (is (thrown-with-msg?+ ExceptionInfo+ #"You do not have permissions to run this query"+ (qp/process-query (-> (lib/query mp (lib.metadata/table mp (mt/id :people)))+ (lib/limit 1))))))+ (testing "persisted-info/native in the query map is ignored in favor of the source-table"+ (let [people-sql (str "SELECT id AS ID, address AS NAME, 0 AS CATEGORY_ID,"+ " 0.0 AS LATITUDE, 0.0 AS LONGITUDE, 0 AS PRICE FROM PEOPLE")+ ;; Inject the internally-managed :persisted-info/native key into the source stage+ ;; to confirm it is ignored in favor of the real source-table.+ result (qp/process-query+ (-> (lib/query mp (lib.metadata/table mp (mt/id :venues)))+ lib/append-stage+ (lib/limit 1)+ (assoc-in [:stages 0 :persisted-info/native] people-sql)))+ expected (qp/process-query venues-query)]+ (is (=? {:status :completed} result))+ (testing "Results come from the source-table, not the inline native query"+ (is (= (mt/rows expected)+ (mt/rows result))))))))))))++(deftest e2e-card-creation-perms-key-test+ (testing "a :query-permissions/perms value in the dataset_query does not affect card-creation permission checks"+ (mt/with-temp [:model/Collection {collection-id :id} {}]+ (mt/with-no-data-perms-for-all-users!+ (perms/set-database-permission! (perms/all-users-group) (mt/id) :perms/view-data :unrestricted)+ (perms/set-database-permission! (perms/all-users-group) (mt/id) :perms/create-queries :no)+ (mt/with-test-user :rasta+ (testing "Sanity check: user cannot run a native query against this database"+ (is (thrown-with-msg?+ ExceptionInfo+ #"You do not have permissions to run this query"+ (qp/process-query+ {:database (mt/id)+ :type :native+ :native {:query "SELECT * FROM VENUES"}}))))+ ;; Disable malli enforcement so the endpoint sees the raw request map as it would in production.+ (binding [mu.fn/*enforce* false]+ (testing "Card creation with a :query-permissions/perms value in the query is rejected"+ (mt/with-model-cleanup [:model/Card]+ (mt/user-http-request :rasta :post 403 "card"+ {:name "test-card"+ :display :table+ :collection_id collection-id+ :dataset_query {:database (mt/id)+ :type :native+ :native {:query "SELECT * FROM VENUES"+ :template-tags {}}+ :query-permissions/perms {:gtaps {:perms/create-queries :query-builder-and-native+ :perms/view-data :unrestricted}}}+ :visualization_settings {}})))))))))++(deftest at-least-as-permissive-rejects-unknown-values-test+ (testing "at-least-as-permissive? should throw on values not in the permission type's value set"+ (binding [mu.fn/*enforce* false]+ (testing "String values should throw"+ (is (thrown-with-msg?+ ExceptionInfo+ #"Invalid permission value"+ (perms/at-least-as-permissive? :perms/create-queries "query-builder-and-native" :no))))+ (testing "Unknown keyword values should throw"+ (is (thrown-with-msg?+ ExceptionInfo+ #"Invalid permission value"+ (perms/at-least-as-permissive? :perms/create-queries :bogus-value :no))))+ (testing "Valid keyword values should still work"+ (is (true? (perms/at-least-as-permissive? :perms/create-queries :query-builder-and-native :no)))+ (is (false? (perms/at-least-as-permissive? :perms/create-queries :no :query-builder-and-native)))))))++;;; +----------------------------------------------------------------------------------------------------------------++;;; | Pivot Query Permission Tests |+;;; +----------------------------------------------------------------------------------------------------------------+++(defn- with-venues-only-access*! [thunk]+ (mt/with-temp-copy-of-db+ (mt/with-no-data-perms-for-all-users!+ (perms/set-database-permission! (perms/all-users-group) (mt/id) :perms/view-data :unrestricted)+ (perms/set-table-permission! (perms/all-users-group) (mt/id :venues) :perms/create-queries :query-builder)+ (perms/set-table-permission! (perms/all-users-group) (mt/id :people) :perms/create-queries :no)+ (mt/with-test-user :rasta+ (thunk)))))++(defmacro ^:private with-venues-only-access [& body]+ `(with-venues-only-access*! (fn [] ~@body)))++(deftest pivot-query-does-not-bind-card-id-test+ (testing "run-pivot-query should not bind *card-id* from :info, so permission checks use the+ full ad-hoc path even when :card-id is present in the query's :info map"+ (mt/with-non-admin-groups-no-root-collection-perms+ (with-venues-only-access+ (mt/with-temp [:model/Collection collection {}+ :model/Card {card-id :id} {:collection_id (:id collection)+ :dataset_query (mt/mbql-query venues)}]+ (perms/grant-collection-read-permissions! (perms/all-users-group) collection)+ ;; User cannot query people directly+ (is (thrown-with-msg? ExceptionInfo perms-error-msg+ (qp/process-query+ {:database (mt/id)+ :type :query+ :query {:source-table (mt/id :people)+ :limit 1}})))+ ;; Pivot path with :card-id in :info still routes through the ad-hoc permission path.+ ;; The pivot path wraps queries as userland, so permission errors are returned as+ ;; {:status :failed} results rather than thrown exceptions.+ (testing "Pivot query uses the ad-hoc permission path when :card-id is in :info (source-table)"+ (let [result (qp.pivot/run-pivot-query+ {:database (mt/id)+ :type :query+ :info {:executed-by (mt/user->id :rasta)+ :context :ad-hoc+ :card-id card-id}+ :constraints {:max-results 10000 :max-results-bare-rows 2000}+ :query {:source-table (mt/id :people)+ :limit 1}})]+ (is (= :failed (:status result)))+ (is (re-find perms-error-msg (str (:error result))))))+ ;; Pivot path with :card-id and a join still routes through the ad-hoc permission path+ (testing "Pivot query uses the ad-hoc permission path when :card-id is in :info (join)"+ (let [result (qp.pivot/run-pivot-query+ {:database (mt/id)+ :type :query+ :info {:executed-by (mt/user->id :rasta)+ :context :ad-hoc+ :card-id card-id}+ :constraints {:max-results 10000 :max-results-bare-rows 2000}+ :query {:source-table (format "card__%d" card-id)+ :joins [{:source-table (mt/id :people)+ :alias "p"+ :condition [:=+ [:field (mt/id :venues :id)]+ [:field (mt/id :people :id) {:join-alias "p"}]]+ :fields :all}]+ :limit 5}})]+ (is (= :failed (:status result)))+ (is (re-find perms-error-msg (str (:error result)))))))))))++(deftest pivot-query-with-card-id-bound-externally-test+ (testing "When *card-id* is bound by the caller (e.g. card.clj), pivot queries should+ still work correctly with card-level permission checks"+ (mt/with-non-admin-groups-no-root-collection-perms+ (with-venues-only-access+ (mt/with-temp [:model/Collection collection {}+ :model/Card {card-id :id} {:collection_id (:id collection)+ :dataset_query (mt/mbql-query venues+ {:breakout [$price]+ :aggregation [[:count]]})}]+ (perms/grant-collection-read-permissions! (perms/all-users-group) collection)+ ;; Simulates what card.clj does — binding *card-id* itself+ (binding [qp.perms/*card-id* card-id]+ (let [result (qp.pivot/run-pivot-query+ {:database (mt/id)+ :type :query+ :info {:executed-by (mt/user->id :rasta)+ :context :ad-hoc+ :card-id card-id}+ :constraints {:max-results 10000 :max-results-bare-rows 2000}+ :query {:source-table (format "card__%d" card-id)+ :breakout [[:field (mt/id :venues :price)]]+ :aggregation [[:count]]}})]+ (is (=? {:status :completed} result)+ "Pivot query for an authorized card should succeed when *card-id* is bound by the caller"))))))))++(deftest download-endpoint-replaces-info-test+ (testing "POST /api/dataset/:format replaces :info entirely rather than merging it with the query map"+ (with-venues-only-access+ (mt/with-temp [:model/Collection collection {}+ :model/Card {card-id :id} {:collection_id (:id collection)+ :dataset_query (mt/mbql-query venues)}]+ (perms/grant-collection-read-permissions! (perms/all-users-group) collection)+ (testing ":info in the request body is replaced by the server-generated :info"+ (let [result (mt/user-http-request+ :rasta :post "dataset/json"+ {:query {:database (mt/id)+ :type :query+ :info {:card-id card-id+ :executed-by 99999}+ :query {:source-table (mt/id :people)+ :limit 1}}})]+ (when (map? result)+ (is (re-find #"(?i)permission" (str (:message result) (:error result) (:error_type result)))+ "Query to an unauthorized table is blocked regardless of the :info in the body"))))))))++(deftest download-endpoint-pivot-join-permission-test+ (testing "POST /api/dataset/:format with was-pivot applies permission checks to joins"+ (mt/with-non-admin-groups-no-root-collection-perms+ (with-venues-only-access+ (mt/with-temp [:model/Collection collection {}+ :model/Card {card-id :id} {:collection_id (:id collection)+ :dataset_query (mt/mbql-query venues)}]+ (perms/grant-collection-read-permissions! (perms/all-users-group) collection)+ (let [result (mt/user-http-request+ :rasta :post "dataset/json"+ {:query {:database (mt/id)+ :type :query+ :was-pivot true+ :query {:source-table (format "card__%d" card-id)+ :joins [{:source-table (mt/id :people)+ :alias "p"+ :condition [:=+ [:field (mt/id :venues :id)]+ [:field (mt/id :people :id) {:join-alias "p"}]]+ :fields :all}]+ :limit 5}}})]+ (when (map? result)+ (is (re-find #"(?i)permission" (str (:message result) (:error result) (:error_type result)))+ "Join to an unauthorized table via pivot download is blocked"))))))))diff --git a/test/metabase/query_processor/persistence_test.clj b/test/metabase/query_processor/persistence_test.cljindex 7a619d46ad5..11d738a9d08 100644--- a/test/metabase/query_processor/persistence_test.clj+++ b/test/metabase/query_processor/persistence_test.clj@@ -10,6 +10,7 @@[honey.sql :as sql][metabase.driver :as driver][metabase.driver.ddl.interface :as ddl.i]+ [metabase.driver.sql.query-processor :as sql.qp][metabase.query-processor.metadata :as qp.metadata][metabase.query-processor.settings :as qp.settings][metabase.query-processor.test :as qp]@@ -181,3 +182,48 @@(is (= {"Doohickey" 3976, "Gadget" 4939,"Gizmo" 4784, "Widget" 5061}(->> results :data :rows (into {})))))))))))++(deftest sql-qp-independent-persisted-lookup-test+ (testing "SQL QP looks up the persisted cache rather than using an inline :persisted-info/native from the query map"+ (mt/test-drivers (mt/normal-drivers-with-feature :persist-models)+ (mt/dataset test-data+ (mt/with-persistence-enabled! [persist-models!]+ (mt/with-temp [:model/Card model {:type :model+ :database_id (mt/id)+ :query_type :query+ :dataset_query (mt/mbql-query products)}]+ (persist-models!)+ (let [inline-sql "SELECT 'INLINE' AS marker"+ query {:database (mt/id)+ :type :query+ :query {:source-table (str "card__" (:id model))+ :persisted-info/native inline-sql+ :limit 1}}+ results (qp/process-query query)+ native-sql (-> results :data :native_form :query)+ persisted-schema (ddl.i/schema-name (mt/db) (system/site-uuid))]+ (testing "Uses the persisted cache, not the inline SQL from the query map"+ (is (str/includes? native-sql persisted-schema))+ (is (not (str/includes? native-sql "INLINE")))))))))))++(deftest resolve-persisted-source-sql-test+ (testing "resolve-persisted-source-sql returns persisted SQL for a valid persisted card"+ (mt/test-drivers (mt/normal-drivers-with-feature :persist-models)+ (mt/dataset test-data+ (mt/with-persistence-enabled! [persist-models!]+ (mt/with-temp [:model/Card model {:type :model+ :database_id (mt/id)+ :query_type :query+ :dataset_query (mt/mbql-query products)}]+ (persist-models!)+ (mt/with-metadata-provider (mt/id)+ (let [source-query {:qp/stage-is-from-source-card (:id model)+ :source-table (mt/id :products)}+ result (#'sql.qp/resolve-persisted-source-sql source-query)+ persisted-schema (ddl.i/schema-name (mt/db) (system/site-uuid))]+ (testing "Returns SQL pointing to the persisted cache table"+ (is (some? result))+ (is (str/includes? result persisted-schema)))+ (testing "Returns nil when card-id is not present"+ (is (nil? (#'sql.qp/resolve-persisted-source-sql+ {:source-table (mt/id :products)}))))))))))))