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
Show expected type in custom expression error message (#70400)
Riley Thompson0c0f3450dd28
diff --git a/e2e/test/scenarios/custom-column/custom-column-reproductions-2.cy.spec.js b/e2e/test/scenarios/custom-column/custom-column-reproductions-2.cy.spec.jsindex 22e9cde586e..f3cee28c42f 100644--- a/e2e/test/scenarios/custom-column/custom-column-reproductions-2.cy.spec.js+++ b/e2e/test/scenarios/custom-column/custom-column-reproductions-2.cy.spec.js@@ -790,7 +790,9 @@ describe("Issue 26512", () => {H.CustomExpressionEditor.clear().type(formula, { allowFastSet: true }).blur();- H.popover().findByText("Types are incompatible.").should("be.visible");+ H.popover()+ .findByText(/Types are incompatible/)+ .should("be.visible");});});});diff --git a/src/metabase/lib/expression.cljc b/src/metabase/lib/expression.cljcindex 9da1307daef..5346846fc4b 100644--- a/src/metabase/lib/expression.cljc+++ b/src/metabase/lib/expression.cljc@@ -1,5 +1,5 @@(ns metabase.lib.expression- (:refer-clojure :exclude [+ - * / case coalesce abs time concat replace float mapv some select-keys not-empty get-in+ (:refer-clojure :exclude [+ - * / case coalesce abs time concat replace float mapv some select-keys not-empty get-in every?#?(:clj doseq) #?(:clj for)])(:require[clojure.string :as str]@@ -31,7 +31,7 @@[metabase.util.malli :as mu][metabase.util.malli.registry :as mr][metabase.util.number :as u.number]- [metabase.util.performance :refer [mapv some select-keys not-empty get-in #?(:clj doseq) #?(:clj for)]]))+ [metabase.util.performance :refer [mapv some select-keys not-empty get-in every? #?(:clj doseq) #?(:clj for)]]))(mu/defn column-metadata->expression-ref :- :mbql.clause/expression"Given `:metadata/column` column metadata for an expression, construct an `:expression` reference."@@ -632,6 +632,62 @@(lib.util/clause? expr)(some #(invalid-nesting % (conj path-tags (first expr))) (nnext expr)))))+(defn- find-type-error+ "Given a malli explanation, return the first error with an `:error/type-description`."+ [explanation]+ (some (fn [error]+ (let [schema (:schema error)+ props (or (mc/properties schema)+ (mc/type-properties schema))+ type-desc-msg-or-fn (:error/type-description props)+ type-desc (if (fn? type-desc-msg-or-fn)+ (type-desc-msg-or-fn)+ type-desc-msg-or-fn)]+ (when type-desc+ [(str type-desc) (:in error)])))+ (:errors explanation)))++(defn- parent-operator-name+ "Given an expression and a path to a value, return the name of the parent operator of that value."+ [expr in-path]+ (when (and (seq in-path) (every? integer? in-path))+ (let [parent-path (pop (vec in-path))+ parent (get-in expr parent-path)]+ (when (and (vector? parent) (first parent))+ (name (first parent))))))++(defn- friendly-error-message+ [explanation]+ (let [error (first (:errors explanation))+ schema (:schema error)+ props (or (mc/properties schema)+ (mc/type-properties schema))+ message-or-fn (:error/message props)]+ (when (:error/friendly props)+ (if (fn? message-or-fn)+ (str (message-or-fn))+ (str message-or-fn)))))++(defn- ordinal-str+ "Returns a translated ordinal string"+ [n]+ (cond+ (= n 1) (i18n/tru "1st")+ (= n 2) (i18n/tru "2nd")+ (= n 3) (i18n/tru "3rd")+ (and (>= n 4) (<= n 20)) (i18n/tru "{0}th" n)+ :else (str n)))++(defn- type-error-message+ [explanation expr]+ (let [[type-desc in-path] (find-type-error explanation)+ op-name (parent-operator-name expr in-path)+ param-pos (when (last in-path)+ (ordinal-str (dec (last in-path))))]+ (if (and op-name type-desc param-pos)+ (i18n/tru "Types are incompatible: {0} expects {1} as the {2} parameter." op-name type-desc param-pos)+ (i18n/tru "Types are incompatible."))))+(mu/defn diagnose-expression :- [:maybe [:map [:message :string]]]"Checks `expr` for type errors and, if `expression-mode` is :expression and`expression-position` is provided, for cyclic references with other expressions.@@ -659,15 +715,9 @@:aggregation aggregation-explainer:filter filter-explainer)](or (when-let [explanation (explainer expr)]- (let [error (first (:errors explanation))- schema (:schema error)- props (or (mc/properties schema)- (mc/type-properties schema))- error-friendly? (:error/friendly props)- error-message-or-fn (:error/message props)- error-message (if (fn? error-message-or-fn) (str (error-message-or-fn)) (str error-message-or-fn))- fallback-message (i18n/tru "Types are incompatible.")- message (if error-friendly? error-message fallback-message)]+ (let [friendly-message (friendly-error-message explanation)+ type-message (type-error-message explanation expr)+ message (or friendly-message type-message)]{:message message:friendly true}))(when-let [dependency-pathdiff --git a/src/metabase/lib/schema/expression.cljc b/src/metabase/lib/schema/expression.cljcindex 3fa3dee5403..0f4de81ff21 100644--- a/src/metabase/lib/schema/expression.cljc+++ b/src/metabase/lib/schema/expression.cljc@@ -125,8 +125,11 @@1b. expression is an registered MBQL clause and matches the schema registeredwith [[metabase.lib.schema.mbql-clause]], AND- 2. expression's [[type-of]] isa? `base-type`"- [base-type description]+ 2. expression's [[type-of]] isa? `base-type`++ Returns a zero-arg function that returns a translated string describing the expected type+ if the expression does not match the schema. This is done to avoid premature i18n lookups at compile time."+ [base-type type-desc-fn message][:and;; vector = MBQL clause, anything else = not an MBQL clause[:multi@@ -134,45 +137,55 @@[true [:ref :metabase.lib.schema.mbql-clause/clause]][false [:ref :metabase.lib.schema.literal/literal]]][:fn- {:error/message description}+ {:error/message message+ :error/type-description type-desc-fn}#(and (not (non-expression-clause? %))(or *suppress-expression-type-check?*(type-of? % base-type)))]])(mr/def ::boolean- (expression-schema :type/Boolean "expression returning a boolean"))+ (expression-schema :type/Boolean #(i18n/tru "a boolean")+ "an expression returning a boolean"))(mr/def ::string- (expression-schema :type/Text "expression returning a string"))+ (expression-schema :type/Text #(i18n/tru "a string")+ "an expression returning a string"))(mr/def ::integer- (expression-schema :type/Integer "expression returning an integer"))+ (expression-schema :type/Integer #(i18n/tru "an integer")+ "an expression returning an integer"))(mr/def ::non-integer-real- (expression-schema :type/Float "expression returning a non-integer real number"))+ (expression-schema :type/Float #(i18n/tru "a non-integer real number")+ "an expression returning a non-integer real number"))(mr/def ::number- (expression-schema :type/Number "expression returning a number"))+ (expression-schema :type/Number #(i18n/tru "a number")+ "an expression returning a number"))(mr/def ::date- (expression-schema :type/Date "expression returning a date"))+ (expression-schema :type/Date #(i18n/tru "a date")+ "an expression returning a date"))(mr/def ::time- (expression-schema :type/Time "expression returning a time"))+ (expression-schema :type/Time #(i18n/tru "a time")+ "an expression returning a time"))(mr/def ::datetime- (expression-schema :type/DateTime "expression returning a date time"))+ (expression-schema :type/DateTime #(i18n/tru "a date time")+ "an expression returning a date time"))(mr/def ::temporal- (expression-schema :type/Temporal "expression returning a date, time, or date time"))+ (expression-schema :type/Temporal #(i18n/tru "a temporal value")+ "an expression returning a temporal value"))(def orderable-types"Set of base types that are orderable."#{:type/Text :type/Number :type/Temporal :type/Boolean :type/MongoBSONID})(mr/def ::orderable- (expression-schema orderable-types- "an expression that can be compared with :> or :<"))+ (expression-schema orderable-types #(i18n/tru "an orderable type (e.g. number, string, temporal)")+ "an expression returning an orderable type (e.g. number, string, temporal)"))(defn comparable-expressions?"Returns whether expressions `x` and `y` can be compared.@@ -190,6 +203,13 @@(and (types/assignable? t1 t)(types/assignable? t2 t))))))+(derive :type/Text ::emptyable)+(derive :type/MongoBSONID ::emptyable)++(mr/def ::emptyable+ (expression-schema ::emptyable #(i18n/tru "an emptyable type (e.g. string, BSON ID))")+ "an expression returning an emptyable type (e.g. string, BSON ID)"))+(def equality-comparable-types"Set of base types that can be compared with equality.";; TODO: Adding :type/* here was necessary to prevent type errors for queries where a field's type in the DB could not@@ -200,20 +220,15 @@;; `:type/Boolean` should fail; we can prove it's the wrong thing to do.#{:type/Boolean :type/Text :type/Number :type/Temporal :type/IPAddress :type/MongoBSONID :type/Array :type/*})-(derive :type/Text ::emptyable)-(derive :type/MongoBSONID ::emptyable)--(mr/def ::emptyable- (expression-schema ::emptyable "expression returning something emptyable (e.g. a string or BSON ID)"))-(mr/def ::equality-comparable[:maybe- (expression-schema equality-comparable-types- "an expression that can appear in := or :!=")])+ (expression-schema equality-comparable-types #(i18n/tru "an equality comparable type (e.g. number, string, boolean)")+ "an expression returning an equality comparable type (e.g. number, string, boolean)")]);;; any type of expression.(mr/def ::expression- [:maybe (expression-schema :type/* "any type of expression")])+ [:maybe (expression-schema :type/* #(i18n/tru "any type")+ "an expression returning any type")])(mr/def ::expression.definition[:anddiff --git a/test/metabase/lib/expression_test.cljc b/test/metabase/lib/expression_test.cljcindex e0feeb38762..2c3ce6b19d7 100644--- a/test/metabase/lib/expression_test.cljc+++ b/test/metabase/lib/expression_test.cljc@@ -502,20 +502,20 @@(deftest ^:parallel diagnose-expression-test-2(testing "type errors are reported"- (are [mode expr] (=? {:message "Types are incompatible."- :friendly true}- (lib.expression/diagnose-expression- (lib.tu/venues-query) 0 mode- (lib.convert/->pMBQL expr)- #?(:clj nil :cljs js/undefined)))- :expression [:/ [:field 1 {:base-type :type/Address}] 100]- ;; To make this test case work, the aggregation schema has to be- ;; tighter and not allow anything. That's a bigger piece of work,- ;; because it makes expressions and aggregations mutually recursive- ;; or requires a large amount of duplication.+ (are [mode expr msg] (=? {:message msg+ :friendly true}+ (lib.expression/diagnose-expression+ (lib.tu/venues-query) 0 mode+ (lib.convert/->pMBQL expr)+ #?(:clj nil :cljs js/undefined)))+ :expression [:/ [:field 1 {:base-type :type/Address}] 100] "Types are incompatible: / expects a number as the 1st parameter."+ ;; To make this test case work, the aggregation schema has to be+ ;; tighter and not allow anything. That's a bigger piece of work,+ ;; because it makes expressions and aggregations mutually recursive+ ;; or requires a large amount of duplication.#_#_:aggregation [:sum [:is-empty [:field 1 {:base-type :type/Boolean}]]]- :filter [:sum [:field 1 {:base-type :type/Integer}]]- :filter [:value "not a boolean" {:base_type :type/Text}])))+ :filter [:sum [:field 1 {:base-type :type/Integer}]] "Types are incompatible."+ :filter [:value "not a boolean" {:base_type :type/Text}] "Types are incompatible.")))(deftest ^:parallel diagnose-expression-test-3(testing "correct expression are accepted silently"@@ -627,6 +627,39 @@(is (=? {:message (str "Cycle detected: " expr-name " → " expr-name)}(lib.expression/diagnose-expression query2 0 :aggregation expr 1))))))+(deftest ^:parallel diagnose-expression-incompatible-types-test+ (testing "expressions with incompatible types show correct error messages"+ (are [expr msg] (= {:message msg+ :friendly true}+ (lib.expression/diagnose-expression+ (lib.tu/venues-query) 0 :expression+ (lib.convert/->pMBQL expr)+ #?(:clj nil :cljs js/undefined)))+ ;; basic checks with different types+ [:+ 1 true] "Types are incompatible: + expects a number as the 2nd parameter."+ [:- 1 "str"] "Types are incompatible: - expects a number as the 2nd parameter."+ [:* false 1] "Types are incompatible: * expects a number as the 1st parameter."+ [:/ "str" 1] "Types are incompatible: / expects a number as the 1st parameter."+ ;; functions that expect different types+ [:upper 1] "Types are incompatible: upper expects a string as the 1st parameter."+ [:get-day 1] "Types are incompatible: get-day expects a temporal value as the 1st parameter."+ [:not 1] "Types are incompatible: not expects a boolean as the 1st parameter."+ [:> 1 true] "Types are incompatible."+ [:> [:field 1 {:base-type :type/JSON}] 1] "Types are incompatible: > expects an orderable type (e.g. number, string, temporal) as the 1st parameter."+ ;; function with multiple args of different types+ [:substring 1 1 1] "Types are incompatible: substring expects a string as the 1st parameter."+ [:substring "str" 1 true] "Types are incompatible: substring expects an integer as the 3rd parameter."+ [:substring "str" true 1] "Types are incompatible: substring expects an integer as the 2nd parameter."+ [:substring 1 true false] "Types are incompatible: substring expects a string as the 1st parameter."+ ;; nested expressions+ [:+ 1 [:- 1 true] 1] "Types are incompatible: - expects a number as the 2nd parameter."+ [:+ true [:- 1 1] 1] "Types are incompatible: + expects a number as the 1st parameter."+ [:+ 1 [:- 1 1] true] "Types are incompatible: + expects a number as the 3rd parameter."+ [:+ true [:- 1 true] 1] "Types are incompatible: + expects a number as the 1st parameter."+ [:+ 1 [:- 1 true] true] "Types are incompatible: - expects a number as the 2nd parameter."+ [:+ 1 [:- 1 [:* 1 [:/ 1 true]]]] "Types are incompatible: / expects a number as the 2nd parameter."+ [:+ 1 [:- 1 [:* true [:/ 1 1]]]] "Types are incompatible: * expects a number as the 1st parameter.")))+(deftest ^:parallel date-and-time-string-literals-test-1-dates(are [types input] (= types (lib.schema.expression/type-of input))#{:type/Date :type/Text} "2024-07-02"))