Skip to content

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

Fix for the limit in the static embed context (#66811)

Roman Abdulmanov8f2447f64493
diff --git a/src/metabase/embedding_rest/api/common.clj b/src/metabase/embedding_rest/api/common.clj
index 9e6af63e5b5..bb910f6a286 100644
--- a/src/metabase/embedding_rest/api/common.clj
+++ b/src/metabase/embedding_rest/api/common.clj
@@ -299,6 +299,15 @@
(remove-locked-and-disabled-params (or embedding-params
(t2/select-one-fn :embedding_params :model/Card :id card-id))))))
+(defn- get-embed-card-context
+ "If a certain export-format is given, return the correct embedded card context."
+ [export-format]
+ (case (keyword export-format)
+ :csv :embedded-csv-download
+ :xlsx :embedded-xlsx-download
+ :json :embedded-json-download
+ :embedded-question))
+
(defn process-query-for-card-with-params
"Run the query associated with Card with `card-id` using JWT `token-params`, user-supplied URL `query-params`,
an `embedding-params` whitelist, and additional query `options`. Returns `StreamingResponse` that should be
@@ -310,7 +319,7 @@
parameters (apply-slug->value (resolve-card-parameters card-id) merged-slug->value)]
(m/mapply api.public/process-query-for-card-with-id
card-id export-format parameters
- :context :embedded-question
+ :context (get-embed-card-context export-format)
:constraints constraints
:qp qp
options)))
diff --git a/src/metabase/query_processor/middleware/limit.clj b/src/metabase/query_processor/middleware/limit.clj
index 5691d28933e..382b530d918 100644
--- a/src/metabase/query_processor/middleware/limit.clj
+++ b/src/metabase/query_processor/middleware/limit.clj
@@ -58,14 +58,16 @@
:native query)))
(when-not (disable-max-results? query)
(let [context (-> query :info :context)
- download-context? #{:csv-download :json-download :xlsx-download}
+ download-context? #{:csv-download :json-download :xlsx-download
+ :embedded-csv-download :embedded-json-download :embedded-xlsx-download
+ :public-csv-download :public-json-download :public-xlsx-download}
attachment-context? #{:dashboard-subscription :pulse :notification}
download-limit (when (download-context? context) (qp.settings/download-row-limit))
attachment-limit (when (attachment-context? context) (qp.settings/attachment-row-limit))
res (u/safe-min (lib/max-rows-limit query)
download-limit
attachment-limit)]
- (if (= context :xlsx-download)
+ (if (#{:xlsx-download :embedded-xlsx-download :public-xlsx-download} context)
(u/safe-min res qp.settings/absolute-max-results)
(or res qp.settings/absolute-max-results))))))
diff --git a/test/metabase/query_processor/middleware/limit_test.clj b/test/metabase/query_processor/middleware/limit_test.clj
index 990a0154e57..6abd3ae9dd4 100644
--- a/test/metabase/query_processor/middleware/limit_test.clj
+++ b/test/metabase/query_processor/middleware/limit_test.clj
@@ -130,3 +130,45 @@
:constraints (when limit {:max-results-bare-rows limit})
:info {:context context}})
[:query :limit])))))))
+
+(deftest embedded-download-row-limit-test
+ (testing "Apply custom download row limits for embedded contexts"
+ (doseq [[limit expected context] [[1100000 1100000 :embedded-csv-download]
+ [1100000 1100000 :embedded-json-download]
+ [1100000 qp.settings/absolute-max-results :embedded-xlsx-download]
+ [nil qp.settings/absolute-max-results :embedded-csv-download]
+ [nil qp.settings/absolute-max-results :embedded-json-download]
+ [nil qp.settings/absolute-max-results :embedded-xlsx-download]]]
+ (testing (format "%s the absolute limit for %s"
+ (if (< expected qp.settings/absolute-max-results)
+ "below"
+ "above")
+ context)
+ (mt/with-temp-env-var-value! [mb-download-row-limit limit]
+ (is (= expected
+ (get-in (add-default-limit
+ {:type :query
+ :query {:source-table (meta/id :venues)}
+ :info {:context context}})
+ [:query :limit]))))))))
+
+(deftest public-download-row-limit-test
+ (testing "Apply custom download row limits for public contexts"
+ (doseq [[limit expected context] [[1100000 1100000 :public-csv-download]
+ [1100000 1100000 :public-json-download]
+ [1100000 qp.settings/absolute-max-results :public-xlsx-download]
+ [nil qp.settings/absolute-max-results :public-csv-download]
+ [nil qp.settings/absolute-max-results :public-json-download]
+ [nil qp.settings/absolute-max-results :public-xlsx-download]]]
+ (testing (format "%s the absolute limit for %s"
+ (if (< expected qp.settings/absolute-max-results)
+ "below"
+ "above")
+ context)
+ (mt/with-temp-env-var-value! [mb-download-row-limit limit]
+ (is (= expected
+ (get-in (add-default-limit
+ {:type :query
+ :query {:source-table (meta/id :venues)}
+ :info {:context context}})
+ [:query :limit]))))))))