Skip to content

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

Several MBQL 4 normalization fixes :wrench: (#67203)

Cam Saulc78ccaaaa070
diff --git a/src/metabase/legacy_mbql/schema.cljc b/src/metabase/legacy_mbql/schema.cljc
index e799e499dd6..bfdb2f557c6 100644
--- a/src/metabase/legacy_mbql/schema.cljc
+++ b/src/metabase/legacy_mbql/schema.cljc
@@ -503,7 +503,7 @@
(def ^:private datetime-functions
"Functions that return Date or DateTime values. Should match `::DatetimeExpression`."
- #{:+ :datetime-add :datetime-subtract :convert-timezone :now :date :datetime :today})
+ #{:+ :datetime-add :datetime-subtract :convert-timezone :now :date :datetime :today :coalesce :case :if})
(mr/def ::NumericExpressionArg
[:multi
@@ -811,7 +811,7 @@
(mr/def ::DatetimeExpression
"Schema for the definition of a date function expression."
- (one-of + datetime-add datetime-subtract convert-timezone now date datetime today))
+ (one-of + datetime-add datetime-subtract convert-timezone now date datetime today coalesce case if))
(defn- compound-filter-schema [tag]
[:multi
@@ -1193,6 +1193,7 @@
(defmethod options-style-method :case [_tag] ::options-style.last-unless-empty)
+;; `:if` is just an alias for `:case`
(defclause if
clauses [:ref ::CaseSubclauses], options (optional [:ref ::CaseOptions]))
@@ -1216,31 +1217,50 @@
{:error/message ":field or :expression reference or expression"
:dispatch (fn [x]
(cond
- (is-clause? numeric-functions x) :numeric
- (is-clause? string-functions x) :string
- (is-clause? boolean-functions x) :boolean
- (is-clause? datetime-functions x) :datetime
- (is-clause? :case x) :case
- (is-clause? :if x) :if
- (is-clause? :offset x) :offset
- (is-clause? :value x) :value
- :else :else))}
- [:numeric [:ref ::NumericExpression]]
- [:string [:ref ::StringExpression]]
- [:boolean [:ref ::BooleanExpression]]
- [:datetime [:ref ::DatetimeExpression]]
- [:case [:ref ::case]]
- [:if [:ref ::if]]
- [:offset [:ref ::offset]]
- [:value [:ref ::value]]
- [:else [:ref ::FieldOrExpressionRef]]])
+ (is-clause? numeric-functions x) :numeric
+ (number? x) :number-literal
+ (is-clause? string-functions x) :string
+ (string? x) :string-literal
+ (is-clause? boolean-functions x) :boolean
+ (boolean? x) :boolean-literal
+ (is-clause? datetime-functions x) :datetime
+ #?(:clj (instance? java.time.temporal.Temporal x)) #?(:clj :temporal-literal)
+ (is-clause? :case x) :case
+ (is-clause? :if x) :if
+ (is-clause? :offset x) :offset
+ (is-clause? :value x) :value
+ :else :else))}
+ [:numeric [:ref ::NumericExpression]]
+ [:number-literal number?]
+ [:string [:ref ::StringExpression]]
+ [:string-literal string?]
+ [:boolean [:ref ::BooleanExpression]]
+ [:boolean-literal boolean?]
+ [:datetime [:ref ::DatetimeExpression]]
+ #?(:clj
+ [:temporal-literal (lib.schema.common/instance-of-class java.time.temporal.Temporal)])
+ [:case [:ref ::case]]
+ [:if [:ref ::if]]
+ [:offset [:ref ::offset]]
+ [:value [:ref ::value]]
+ [:else [:ref ::FieldOrExpressionRef]]])
(mr/def ::AggregationArg
"Schema for the argument to an aggregation clause like `:sum`.
- Strings are allowed as literals here, unlike at the top level as `::Expressions`, so `::FieldOrExpressionDef` is
- not enough. However, nested aggregations are not allowed here, so we can't use `::ExpressionArg` either. (#66199)"
- [:or ::FieldOrExpressionDef :string])
+ Nested aggregations are not allowed here, so we can't use `::ExpressionArg` directly. (#66199)
+
+ Unlike `::FieldOrExpressionDef`, raw integers are treated as unwrapped `:field` clauses for backwards compatibility
+ with MBQL 1 and 2, e.g.
+
+ [:sum 1] => [:sum [:field 1 nil]]"
+ [:and
+ [:ref ::FieldOrExpressionDef]
+ [:any
+ {:decode/normalize (fn [x]
+ (if (pos-int? x)
+ [:field x nil]
+ x))}]])
;; For all of the 'normal' Aggregations below (excluding Metrics) fields are implicit Field IDs
@@ -1305,8 +1325,10 @@
(when (helpers/normalized-mbql-clause? x)
(when-let [[tag & args] x]
(or (lib.hierarchy/isa? tag ::lib.schema.aggregation/aggregation-clause-tag)
- ;; Case has the following shape [:case [[cond expr]...] default-expr?]
- (if (= :case tag)
+ ;; `:case` has the following shape [:case [[cond expr]...] default-expr?]
+ ;;
+ ;; `:if` is an alias for `:case`
+ (if (#{:if :case} tag)
(or (some aggregation-expression? (ffirst args))
(some aggregation-expression? (fnext args)))
(some aggregation-expression? args))))))
@@ -1322,7 +1344,11 @@
"Additional options for any aggregation clause when wrapping it in `:aggregation-options`."
[:map
{:error/message ":aggregation-options options"
- :decode/normalize lib.schema.common/normalize-map}
+ :decode/normalize (fn [m]
+ (let [m (if (nil? m)
+ {}
+ m)]
+ (lib.schema.common/normalize-map m)))}
;; name to use for this aggregation in the native query instead of the default name (e.g. `count`)
[:name {:optional true} ::lib.schema.common/non-blank-string]
;; user-facing display name for this aggregation instead of the default one
@@ -1352,14 +1378,23 @@
(mr/def ::Aggregation
"Schema for anything that is a valid `:aggregation` clause."
- [:multi
- {:error/message "aggregation clause or numeric expression"
- :dispatch (fn [x]
- (if (is-clause? #{:aggregation-options :named} x)
- :aggregation-options
- :unnamed-aggregation))}
- [:aggregation-options [:ref ::aggregation-options]]
- [:unnamed-aggregation [:ref ::UnnamedAggregation]]])
+ [:and
+ [:multi
+ {:error/message "aggregation clause or numeric expression"
+ :dispatch (fn [x]
+ (if (is-clause? #{:aggregation-options :named} x)
+ :aggregation-options
+ :unnamed-aggregation))}
+ [:aggregation-options [:ref ::aggregation-options]]
+ [:unnamed-aggregation [:ref ::UnnamedAggregation]]]
+ [:any
+ {:description "Normalization should automatically unwrap :aggregation-options with an empty options map"
+ :decode/normalize (fn [x]
+ (or (when (is-clause? :aggregation-options x)
+ (let [[_tag wrapped options] x]
+ (when (empty? options)
+ wrapped)))
+ x))}]])
(defn- normalize-aggregations [x]
(let [xs (cond
@@ -1502,6 +1537,14 @@
;; :type :dimension,
;; :dimension [:field 4 nil]
;; :widget-type :date/all-options}
+(mr/def ::TemplateTag.FieldFilter.Options
+ [:map-of
+ {:decode/normalize (fn [m]
+ (when (map? m)
+ (update-keys m lib.schema.common/normalize-keyword)))}
+ :keyword
+ :any])
+
(mr/def ::TemplateTag.FieldFilter
"Schema for a field filter template tag."
[:merge
@@ -1522,7 +1565,7 @@
[:options
{:optional true
:description "optional map to be appended to filter clause"}
- [:maybe [:map-of :keyword :any]]]]])
+ [:maybe [:ref ::TemplateTag.FieldFilter.Options]]]]])
;; Example:
;;
@@ -1904,7 +1947,8 @@
:breakout "A join should not have top-level 'inner' query keys like :breakout"
:aggreggation "A join should not have top-level 'inner' query keys like :aggreggation"
:expressions "A join should not have top-level 'inner' query keys like :expressions"
- :joins "A join should not have top-level 'inner' query keys like :joins"})])
+ :joins "A join should not have top-level 'inner' query keys like :joins"
+ :ident ":ident is deprecated and should not be included in joins"})])
(mr/def ::Joins
"Schema for a valid sequence of `Join`s. Must be a non-empty sequence, and `:alias`, if specified, must be unique."
@@ -2009,9 +2053,13 @@
(fn [query]
(= 1 (count (select-keys query [:source-query :source-table]))))]
[:ref ::RemoveFieldRefsFromFieldsAlreadyInBreakout]
+ ;; `disallowed-keys` will remove these keys if we see them automatically during normalization.
(lib.schema.common/disallowed-keys
- {:lib/type "Legacy MBQL inner queries must not have :lib/type"
- :type "An inner query must not include :type, this will cause us to mix it up with an outer query"})])
+ {:lib/type "Legacy MBQL inner queries must not have :lib/type"
+ :type "An inner query must not include :type, this will cause us to mix it up with an outer query"
+ :aggregation-idents ":aggregation-idents is deprecated and should not be used"
+ :breakout-idents ":breakout-idents is deprecated and should not be used"
+ :expression-idents ":expression-idents is deprecated and should not be used"})])
(mr/def ::WidgetType
"Schema for valid values of `:widget-type` for a `::TemplateTag.FieldFilter`."
diff --git a/src/metabase/lib/schema/aggregation.cljc b/src/metabase/lib/schema/aggregation.cljc
index 616510d0c72..4c5790e27e6 100644
--- a/src/metabase/lib/schema/aggregation.cljc
+++ b/src/metabase/lib/schema/aggregation.cljc
@@ -130,10 +130,12 @@
"A clause is a valid aggregation if it is an aggregation clause, or it is an expression that transitively contains
a single aggregation clause."
[x]
- (when-let [[tag _opts & args] (and (vector? x) x)]
+ (when-let [[tag _opts & args] (when (vector? x) x)]
(or (lib.hierarchy/isa? tag ::aggregation-clause-tag)
;; Case has the following shape [:case opts [[cond expr]...] default-expr?]
- (if (= :case tag)
+ ;;
+ ;; `:if` is an alias for `:case`
+ (if (#{:case :if} tag)
(or (some aggregation-expression? (ffirst args))
(some aggregation-expression? (fnext args)))
(some aggregation-expression? args)))))
diff --git a/src/metabase/lib/schema/expression/string.cljc b/src/metabase/lib/schema/expression/string.cljc
index 2d3c316a659..a8ed197330a 100644
--- a/src/metabase/lib/schema/expression/string.cljc
+++ b/src/metabase/lib/schema/expression/string.cljc
@@ -28,9 +28,9 @@
#_regex [:schema [:ref ::expression/string]])
(mbql-clause/define-tuple-mbql-clause :replace :- :type/Text
- #_str [:schema [:ref ::expression/string]]
- #_find [:schema :string]
- #_replace [:schema :string])
+ #_str [:schema [:ref ::expression/string]]
+ #_find :string
+ #_replace :string)
(mbql-clause/define-catn-mbql-clause :substring :- :type/Text
[:str [:schema [:ref ::expression/string]]]
@@ -39,12 +39,12 @@
(mbql-clause/define-tuple-mbql-clause :split-part :- :type/Text
#_text [:schema [:ref ::expression/string]]
- #_delimiter [:schema [:string {:min 1}]] ;; literal string
+ #_delimiter [:string {:min 1}] ; literal string
#_position [:schema [:ref ::expression/positive-integer-or-numeric-expression]])
(mbql-clause/define-tuple-mbql-clause :collate :- :type/Text
#_str [:schema [:ref ::expression/string]]
- #_collation [:schema :string])
+ #_collation :string)
(mbql-clause/define-catn-mbql-clause :concat :- :type/Text
[:args [:repeat {:min 2} [:schema [:ref ::expression/expression]]]])
diff --git a/src/metabase/lib/schema/join.cljc b/src/metabase/lib/schema/join.cljc
index 64839ef2357..5a0d65baa44 100644
--- a/src/metabase/lib/schema/join.cljc
+++ b/src/metabase/lib/schema/join.cljc
@@ -131,7 +131,8 @@
:source-query "join should not have :source-query; use :stages instead"
:filter "join should not have top-level :filters; these should belong to one of the join :stages"
:filters "join should not have top-level :filters; these should belong to one of the join :stages"
- :parameters "join should not have top-level :parameters; these should belong to one of the join :stages"})
+ :parameters "join should not have top-level :parameters; these should belong to one of the join :stages"
+ :ident ":ident is deprecated and should not be included in joins"})
[:ref ::validate-field-aliases-match-join-alias]])
(mr/def ::joins
diff --git a/test/metabase/legacy_mbql/normalize_test.cljc b/test/metabase/legacy_mbql/normalize_test.cljc
index 7530ad6909c..7db43f5177b 100644
--- a/test/metabase/legacy_mbql/normalize_test.cljc
+++ b/test/metabase/legacy_mbql/normalize_test.cljc
@@ -777,9 +777,12 @@
(deftest ^:parallel canonicalize-aggregations-test-11
(normalize-tests
- "subclauses of `:aggregation-options` should get canonicalized correctly"
+ "subclauses of `:aggregation-options` should get canonicalized correctly; unwrap :aggregation-options with empty options"
{{:query {:aggregation [[:aggregation-options [:sum 10] {}]]}}
- {:query {:aggregation [[:aggregation-options [:sum [:field 10 nil]] {}]]}}}))
+ {:query {:aggregation [[:sum [:field 10 nil]]]}}
+
+ {:query {:aggregation [[:aggregation-options [:sum 10] nil]]}}
+ {:query {:aggregation [[:sum [:field 10 nil]]]}}}))
(deftest ^:parallel canonicalize-aggregations-test-12
(normalize-tests
diff --git a/test/metabase/legacy_mbql/schema_test.cljc b/test/metabase/legacy_mbql/schema_test.cljc
index 8ef68086cad..97c4e2ea0f6 100644
--- a/test/metabase/legacy_mbql/schema_test.cljc
+++ b/test/metabase/legacy_mbql/schema_test.cljc
@@ -1,5 +1,7 @@
(ns metabase.legacy-mbql.schema-test
(:require
+ #?@(:clj
+ ([java-time.api :as t]))
[clojure.string :as str]
[clojure.test :refer [are deftest is testing]]
[malli.error :as me]
@@ -89,10 +91,10 @@
(deftest ^:parallel coalesce-aggregation-test
(testing "should be able to nest aggregation functions within a coalesce"
- (let [query {:database 1,
- :type :query,
+ (let [query {:database 1
+ :type :query
:query
- {:source-table 5,
+ {:source-table 5
:aggregation
[[:aggregation-options
[:/
@@ -104,11 +106,11 @@
(deftest ^:parallel year-of-era-test
(testing "year-of-era aggregations should be recognized"
- (let [query {:database 1,
- :type :query,
+ (let [query {:database 1
+ :type :query
:query
- {:source-table 5,
- :aggregation [[:count]],
+ {:source-table 5
+ :aggregation [[:count]]
:breakout [[:field 49 {:base-type :type/Date, :temporal-unit :year-of-era, :source-field 43}]]}
:parameters []}]
(is (not (me/humanize (mr/explain ::mbql.s/Query query)))))))
@@ -172,10 +174,9 @@
true
false))
+;; Allowed in #67203 and above
(deftest ^:parallel expression-unwrapped-literals-test
- (are [value] (= {:expressions {"expr" ["valid instance of one of these MBQL clauses: expression, field"]}}
- (me/humanize (mr/explain ::mbql.s/MBQLQuery
- {:source-table 1, :expressions {"expr" value}})))
+ (are [value] (mr/validate ::mbql.s/MBQLQuery {:source-table 1, :expressions {"expr" value}})
""
"192.168.1.1"
"2025-03-11"
@@ -275,14 +276,14 @@
(testing "Fix really messed up fingerprints with lower-cased type names (only in prod) (#63397)"
(mu/disable-enforcement
(is (= {:base_type :type/*
- :fingerprint {:global {:distinct-count 418, :nil% 0.0},
+ :fingerprint {:global {:distinct-count 418, :nil% 0.0}
:type
{:type/Text
{:percent-json 0.0, :percent-url 0.0, :percent-email 0.0, :percent-state 0.0, :average-length 13.26388888888889}}}}
(lib/normalize
::mbql.s/legacy-column-metadata
{:fingerprint
- {:global {:distinct-count 418, :nil% 0.0},
+ {:global {:distinct-count 418, :nil% 0.0}
:type
{:type/text
{:percent-json 0.0, :percent-url 0.0, :percent-email 0.0, :percent-state 0.0, :average-length 13.26388888888889}}}}))))))
@@ -424,3 +425,172 @@
{:name "Min+sec", :display-name "Min+sec"}]
normalized))
(is (mr/validate ::mbql.s/Aggregation normalized))))
+
+(deftest ^:parallel normalize-aggregation-options-with-nil-options-test
+ (testing "nil options in :aggregation-options should get normalized to an empty map"
+ (let [normalized (lib/normalize ::mbql.s/aggregation-options [:aggregation-options [:count] nil])]
+ (is (= [:aggregation-options [:count] {}]
+ normalized))
+ (is (mr/validate ::mbql.s/aggregation-options normalized))))
+ (testing "::Aggregation itself should unwrap :aggregation-options with an empty options map"
+ (is (= [:count]
+ (lib/normalize ::mbql.s/Aggregation [:aggregation-options [:count] nil])
+ (lib/normalize ::mbql.s/Aggregation [:aggregation-options [:count] {}])))))
+
+(deftest ^:parallel normalize-template-tag-options-test
+ (testing "template tag `:options` should get normalized correctly"
+ (let [normalized (lib/normalize
+ ::mbql.s/TemplateTag
+ {"type" "dimension"
+ "name" "owner_name"
+ "id" "d14f7964-4de7-4d0a-905c-2d2799e87db7"
+ "display-name" "Owner Name"
+ "default" nil
+ "dimension" ["field" 28486 nil]
+ "widget-type" "string/contains"
+ "options" {"case-sensitive" false}})]
+ (is (= {:default nil
+ :dimension [:field 28486 nil]
+ :display-name "Owner Name"
+ :id "d14f7964-4de7-4d0a-905c-2d2799e87db7"
+ :name "owner_name"
+ :options {:case-sensitive false}
+ :type :dimension
+ :widget-type :string/contains}
+ normalized))
+ (is (mr/validate ::mbql.s/TemplateTag normalized)))))
+
+(deftest ^:parallel automatically-remove-expression-idents-during-normalization-test
+ (is (= {:type :query
+ :database 3
+ :query {:expressions {"Organizer fees should be" 0.5}
+ :source-table 310}}
+ (lib/normalize
+ ::mbql.s/Query
+ {"database" 3
+ "type" "query"
+ "query" {"source-table" 310
+ "expressions" {"Organizer fees should be" 0.5}
+ "expression-idents" {"Organizer fees should be"
+ "expression_9CRGGaQ6ASwsIRouX0ID0@0__Organizer fees should be"}}}))))
+
+(deftest ^:parallel allow-raw-literals-as-expressions-tst
+ (doseq [[message xs] {"Raw numeric literals" [10 10.5]
+ "Raw string literals" ["X"]
+ "Raw boolean literals" [true false]
+ #?(:clj "Raw JVM temporal types") #?(:clj [(t/local-date "2025-12-18")
+ (t/local-date-time "2025-12-18T12:57:00")
+ (t/offset-date-time "2025-12-18T12:57:00-08:00")
+ (t/zoned-date-time "2025-12-18T12:57:00-08:00[US/Pacific]")
+ (t/local-time "12:57:00")
+ (t/offset-time "12:57:00-08:00")
+ (t/instant (t/zoned-date-time "2025-12-18T12:57:00-08:00[US/Pacific]"))])}
+ x xs]
+ (testing (str message " should be allowed as expressions (x =" (pr-str x) ")")
+ (let [query {:type :query
+ :database 3
+ :query {:expressions {"Organizer fees should be" x}
+ :source-table 310}}]
+ (is (mr/validate ::mbql.s/Query query))))))
+
+(deftest ^:parallel case-if-aggregation-expression-test
+ (doseq [clause [:if :case]]
+ (testing (str clause " should be allowed as an aggregation expression if it contains an aggregation")
+ (let [normalized (lib/normalize
+ ::mbql.s/Aggregation
+ [(name clause)
+ [[["="
+ ["sum-where"
+ ["field" 781 {"base-type" "type/Float"}]
+ ["=" ["expression" "Paid" {"base-type" "type/Boolean"}] true]]
+ 0]
+ 0]]
+ {"default"
+ ["/"
+ ["sum-where"
+ ["field" 755 {"base-type" "type/Float"}]
+ ["=" ["expression" "Refund" {"base-type" "type/Boolean"}] true]]
+ ["sum-where"
+ ["field" 781 {"base-type" "type/Float"}]
+ ["=" ["expression" "Paid" {"base-type" "type/Boolean"}] true]]]}])]
+ (is (= [clause
+ [[[:=
+ [:sum-where
+ [:field 781 {:base-type :type/Float}]
+ [:= [:expression "Paid" {:base-type :type/Boolean}] true]]
+ 0]
+ 0]]
+ {:default
+ [:/
+ [:sum-where
+ [:field 755 {:base-type :type/Float}]
+ [:= [:expression "Refund" {:base-type :type/Boolean}] true]]
+ [:sum-where
+ [:field 781 {:base-type :type/Float}]
+ [:= [:expression "Paid" {:base-type :type/Boolean}] true]]]}]
+ normalized))
+ (is (mr/validate ::mbql.s/Aggregation normalized))))))
+
+(deftest ^:parallel allow-coalesce-as-datetime-expression-test
+ (let [normalized (lib/normalize
+ ::mbql.s/datetime-diff
+ ["datetime-diff"
+ ["coalesce"
+ ["field" 191303 {"base-type" "type/DateTimeWithLocalTZ"}]
+ ["field" 191302 {"base-type" "type/DateTimeWithLocalTZ"}]]
+ ["coalesce"
+ ["field" 191332 {"base-type" "type/DateTimeWithLocalTZ"}]
+ ["field" 191333 {"base-type" "type/DateTimeWithLocalTZ"}]]
+ "minute"])]
+ (is (= [:datetime-diff
+ [:coalesce
+ [:field 191303 {:base-type :type/DateTimeWithLocalTZ}]
+ [:field 191302 {:base-type :type/DateTimeWithLocalTZ}]]
+ [:coalesce
+ [:field 191332 {:base-type :type/DateTimeWithLocalTZ}]
+ [:field 191333 {:base-type :type/DateTimeWithLocalTZ}]]
+ :minute]
+ normalized))
+ (is (mr/validate ::mbql.s/datetime-diff normalized))))
+
+(deftest ^:parallel allow-if-and-case-as-datetime-expression-test
+ (doseq [clause [:if :case]]
+ (testing (str clause " should be allowed as an aggregation expression if it contains an aggregation")
+ (let [normalized (lib/normalize
+ ::mbql.s/DateTimeExpressionArg
+ [(name clause)
+ [[["="
+ ["sum-where"
+ ["field" 781 {"base-type" "type/Float"}]
+ ["=" ["expression" "Paid" {"base-type" "type/Boolean"}] true]]
+ 0]
+ 0]]
+ {"default"
+ ["/"
+ ["sum-where"
+ ["field" 755 {"base-type" "type/Float"}]
+ ["=" ["expression" "Refund" {"base-type" "type/Boolean"}] true]]
+ ["sum-where"
+ ["field" 781 {"base-type" "type/Float"}]
+ ["=" ["expression" "Paid" {"base-type" "type/Boolean"}] true]]]}])]
+ (is (= [clause
+ [[[:=
+ [:sum-where
+ [:field 781 {:base-type :type/Float}]
+ [:= [:expression "Paid" {:base-type :type/Boolean}] true]]
+ 0]
+ 0]]
+ {:default
+ [:/
+ [:sum-where
+ [:field 755 {:base-type :type/Float}]
+ [:= [:expression "Refund" {:base-type :type/Boolean}] true]]
+ [:sum-where
+ [:field 781 {:base-type :type/Float}]
+ [:= [:expression "Paid" {:base-type :type/Boolean}] true]]]}]
+ normalized))
+ (is (mr/validate ::mbql.s/DateTimeExpressionArg normalized))))))
+
+(deftest ^:parallel normalize-unwrapped-field-id-test
+ (is (= [:sum [:field 10 nil]]
+ (lib/normalize ::mbql.s/sum [:sum 10]))))
diff --git a/test/metabase/lib/schema/aggregation_test.cljc b/test/metabase/lib/schema/aggregation_test.cljc
index 8a247020f84..aa10ca28077 100644
--- a/test/metabase/lib/schema/aggregation_test.cljc
+++ b/test/metabase/lib/schema/aggregation_test.cljc
@@ -1,8 +1,9 @@
(ns metabase.lib.schema.aggregation-test
(:require
- [clojure.test :refer [are deftest testing]]
+ [clojure.test :refer [are deftest is testing]]
[malli.error :as me]
[metabase.lib.schema]
+ [metabase.lib.schema.aggregation :as lib.schema.aggregation]
[metabase.lib.schema.expression :as lib.schema.expression]
[metabase.util.malli.registry :as mr]))
@@ -53,7 +54,7 @@
(deftest ^:parallel arithmetic-expression-test
(testing "valid"
- (are [clause] (not (me/humanize (mr/explain :metabase.lib.schema.aggregation/aggregation clause)))
+ (are [clause] (not (me/humanize (mr/explain ::lib.schema.aggregation/aggregation clause)))
;; DIY average
[:/ {:lib/uuid "00000000-0000-0000-0000-000000000000"}
[:sum {:lib/uuid "00000000-0000-0000-0000-000000000000"}
@@ -81,7 +82,7 @@
[:field {:lib/uuid "00000000-0000-0000-0000-000000000000"} 1]]
-1]]))
(testing "invalid - no aggregation inside"
- (are [clause] (me/humanize (mr/explain :metabase.lib.schema.aggregation/aggregation clause))
+ (are [clause] (me/humanize (mr/explain ::lib.schema.aggregation/aggregation clause))
[:get-day {:lib/uuid "00000000-0000-0000-0000-000000000000"}
[:now {:lib/uuid "00000000-0000-0000-0000-000000000000"}]]
[:+ {:lib/uuid "00000000-0000-0000-0000-000000000000"} 7 8]
@@ -109,6 +110,46 @@
:let [ag [:/ {:lib/uuid "00000000-0000-0000-0000-000000000000"} x y]]]
(testing (pr-str ag)
(are [schema x] (not (me/humanize (mr/explain schema x)))
- :metabase.lib.schema.expression/number ag
- :metabase.lib.schema.aggregation/aggregation ag
- :metabase.lib.schema.aggregation/aggregations [ag])))))
+ ::lib.schema.expression/number ag
+ ::lib.schema.aggregation/aggregation ag
+ ::lib.schema.aggregation/aggregations [ag])))))
+
+(deftest ^:parallel case-and-if-should-be-considered-valid-aggregations-test
+ (doseq [clause [:if :case]]
+ (testing (str clause " should be allowed as an aggregation expression if it contains an aggregation")
+ (let [expr [:if
+ {:lib/uuid "9cbbbcf5-6861-4cf2-8509-a531e53d01a8"}
+ [[[:=
+ {:lib/uuid "56deb1b8-5bea-4c26-9bbc-6f7d2787ea73"}
+ [:field
+ {:base-type :type/Float, :lib/uuid "8bd17b68-8b66-4d63-abde-5d73377e3173"}
+ 781]
+ 0]
+ 0]]
+ [:/
+ {:lib/uuid "96c97774-74b1-4e20-8df5-5a23ae7c9a07"}
+ [:+
+ {:lib/uuid "9ee6e40e-5882-4b80-bac7-8d83a71498f4"}
+ [:sum-where
+ {:lib/uuid "41fa466e-d61e-40b1-b213-5e2e25a6586e"}
+ [:field
+ {:base-type :type/Float, :lib/uuid "6004e6ce-a6b0-4205-b018-a967c91e1582"}
+ 755]
+ [:=
+ {:lib/uuid "de25f409-3982-4e24-a4d5-bbc6f739adc5"}
+ [:expression {:base-type :type/Boolean, :lib/uuid "742fcdce-9b3b-48ed-ae93-72a48560b32f"} "Refund"]
+ true]]
+ 1]
+ [:+
+ {:lib/uuid "6d83c61c-55ed-4444-9a04-77bc20608de4"}
+ [:sum-where
+ {:lib/uuid "94dae912-8d9a-405d-aef4-f3686fd156c8"}
+ [:field
+ {:base-type :type/Float, :lib/uuid "2e3d5468-1633-4ec3-beeb-521f5fb2be80"}
+ 781]
+ [:=
+ {:lib/uuid "3c117b80-4e0e-4305-89d3-6a9dd6a51871"}
+ [:expression {:base-type :type/Boolean, :lib/uuid "5f460efe-d0b3-4d09-ba85-29724fab6fee"} "Paid"]
+ true]]
+ 1]]]]
+ (is (mr/validate ::lib.schema.aggregation/aggregation expr))))))