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
[QP] Allow multiple dashboard/card params to exist, if only one is set (#64428)
Braden Shepherdson17291f9d7930
diff --git a/src/metabase/driver/common/parameters/values.clj b/src/metabase/driver/common/parameters/values.cljindex 5ce84a1f7fd..3017cd6a36d 100644--- a/src/metabase/driver/common/parameters/values.clj+++ b/src/metabase/driver/common/parameters/values.clj@@ -135,6 +135,15 @@param-display-name){:type qp.error-type/missing-required-parameter}))+(defn- multiple-conflicting-param-values-exception [param-display-name]+ (ex-info (tru "Multiple conflicting values were given for ''{0}''" param-display-name)+ {:type qp.error-type/multiple-conflicting-parameter-values}))++(defn- multiple-conflicting-default-values-exception [param-display-name]+ (ex-info (tru "No value was given for ''{0}'' and multiple filters have conflicting default values"+ param-display-name)+ {:type qp.error-type/multiple-conflicting-default-values}))+(mu/defn- dimension->field-id :- ::lib.schema.id/field[dimension](second dimension))@@ -303,27 +312,31 @@"Get the value that should be used for a raw value (i.e., non-Field filter) template tag from `params`."[tag :- ::mbql.s/TemplateTagparams :- [:maybe [:sequential ::lib.schema.parameter/parameter]]]- (let [matching-param (when-let [matching-params (not-empty (tag-params tag params))]- ;; double-check and make sure we didn't end up with multiple mappings or something crazy like that.- (when (> (count matching-params) 1)- (throw (ex-info (tru "Error: multiple values specified for parameter; non-Field Filter parameters can only have one value.")- {:type qp.error-type/invalid-parameter- :template-tag tag- :matching-parameters params})))- (first matching-params))- nil-value? (and matching-param- (nil? (:value matching-param)))]- ;; But if the param is present in `params` and its value is nil, don't use the default.- ;; If the param is not present in `params` use a default from either the tag or the Dashboard parameter.- ;; If both the tag and Dashboard parameter specify a default value, prefer the default value from the tag.- (or (:value matching-param)- (when (and nil-value? (not (:required tag)))- params/no-value)- (:default tag)- (:default matching-param)- (if (:required tag)- (throw (missing-required-param-exception (:display-name tag)))- params/no-value))))+ (let [matching-params (tag-params tag params)+ real-values (into #{} (keep :value) matching-params)+ defaults (into #{} (keep :default) matching-params)]+ (cond+ ;; Multiple parameters with different values set does not make sense.+ (> (count real-values) 1)+ (throw (multiple-conflicting-param-values-exception (:display-name tag)))++ ;; A single real value wins.+ (= (count real-values) 1) (first real-values)+ ;; If some parameters matched, but none have values, AND this tag is not required, then return a blank value.+ (and (seq matching-params)+ (not (:required tag))) params/no-value++ ;; Prefer the default value of the tag over those of the parameters.+ (:default tag) (:default tag)+ ;; If there are conflicting default values among the parameters, throw.+ (> (count defaults) 1)+ (throw (multiple-conflicting-default-values-exception (:display-name tag)))+ ;; If there is exactly one (distinct) default value among the matching parameters, use it.+ (= (count defaults) 1) (first defaults)+ ;; No value at all - throw if this tag was required.+ (:required tag) (throw (missing-required-param-exception (:display-name tag)))+ ;; Fall back to no value.+ :else params/no-value)))(defmethod parse-tag :number[tag params]diff --git a/src/metabase/query_processor/error_type.clj b/src/metabase/query_processor/error_type.cljindex 7e10bebf19d..660665a4643 100644--- a/src/metabase/query_processor/error_type.clj+++ b/src/metabase/query_processor/error_type.clj@@ -58,6 +58,16 @@:parent invalid-query:show-in-embeds? true)+(deferror multiple-conflicting-parameter-values+ "The query is parameterized, and multiple filters provided conflicting values."+ :parent invalid-query+ :show-in-embeds? true)++(deferror multiple-conflicting-default-values+ "The query is parameterized, no value was supplied, and multiple filters have conflicting default values."+ :parent invalid-query+ :show-in-embeds? true)+(deferror invalid-parameter"The query is parameterized, and a supplied parameter has an invalid value.":parent invalid-querydiff --git a/test/metabase/driver/common/parameters/values_test.clj b/test/metabase/driver/common/parameters/values_test.cljindex e6e2e7e5817..06325facc39 100644--- a/test/metabase/driver/common/parameters/values_test.clj+++ b/test/metabase/driver/common/parameters/values_test.clj@@ -90,6 +90,42 @@{:name "id", :id test-uuid, :display-name "ID", :type :number}[{:type :category, :target [:variable [:template-tag {:id test-uuid}]], :value "9223372036854775808"}])))))+(deftest ^:parallel variable-multiple-values-test+ (testing "Allows multiple bindings of the same tag"+ (testing "if only one has a value set"+ (is (= "2"+ (#'params.values/value-for-tag+ {:name "id", :display-name "ID", :type :text, :required true, :default "100"}+ [{:type :category, :target [:variable [:template-tag "id"]], :value "2"}+ {:type :category, :target [:variable [:template-tag "id"]], :value nil}+ {:type :category, :target [:variable [:template-tag "id"]], :value nil}]))))+ (testing "if all values are equal"+ (is (= "2"+ (#'params.values/value-for-tag+ {:name "id", :display-name "ID", :type :text, :required true, :default "100"}+ [{:type :category, :target [:variable [:template-tag "id"]], :value "2"}+ {:type :category, :target [:variable [:template-tag "id"]], :value "2"}+ {:type :category, :target [:variable [:template-tag "id"]], :value nil}]))))+ (testing "if no values are given"+ (testing "required tags use their defaults"+ (is (= "100"+ (#'params.values/value-for-tag+ {:name "id", :display-name "ID", :type :text, :required true, :default "100"}+ [{:type :category, :target [:variable [:template-tag "id"]], :value nil}+ {:type :category, :target [:variable [:template-tag "id"]], :value nil}]))))+ (testing "optional tags get no value"+ (is (= params/no-value+ (#'params.values/value-for-tag+ {:name "id", :display-name "ID", :type :text, :required false, :default "100"}+ [{:type :category, :target [:variable [:template-tag "id"]], :value nil}+ {:type :category, :target [:variable [:template-tag "id"]], :value nil}]))))))+ (testing "Throws if multiple real values are set"+ (is (thrown-with-msg? Exception #"Multiple conflicting values"+ (#'params.values/value-for-tag+ {:name "id", :display-name "ID", :type :text, :required true, :default "100"}+ [{:type :category, :target [:variable [:template-tag "id"]], :value "2"}+ {:type :category, :target [:variable [:template-tag "id"]], :value "8"}])))))+(defn- value-for-tag"Call the private function and de-recordize the field"[field-info info]