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
Allow `:aggregation` refs inside (aggregation) expressions in MBQL 4 schema (#67311)
Cam Saul7057724abf82
diff --git a/src/metabase/legacy_mbql/schema.cljc b/src/metabase/legacy_mbql/schema.cljcindex bfdb2f557c6..baf022dbbea 100644--- a/src/metabase/legacy_mbql/schema.cljc+++ b/src/metabase/legacy_mbql/schema.cljc@@ -433,12 +433,22 @@;; wouldn't it;;;; As of 0.42.0 `:aggregation` references can have an optional options map.+(mr/def ::AggregationRefOptions+ [:map+ {:decode/normalize (fn [m]+ (when-let [m (lib.schema.ref/normalize-aggregation-ref-options m)]+ (not-empty (dissoc m :lib/uuid))))}+ [:name {:optional true} ::lib.schema.common/non-blank-string]+ [:display-name {:optional true} ::lib.schema.common/non-blank-string]+ [:base-type {:optional true} [:maybe ::lib.schema.common/base-type]]+ [:effective-type {:optional true} [:maybe ::lib.schema.common/base-type]]])+(defclause* aggregation[:and(helpers/clause:aggregation"aggregation-clause-index" :int- "options" [:optional :map])+ "options" [:optional [:ref ::AggregationRefOptions]])[:fn{:error/message ":aggregation should not have empty opts":decode/normalize (fn [x]@@ -548,6 +558,7 @@(is-clause? numeric-functions x) :numeric-expression(is-clause? datetime-functions x) :datetime-expression(is-clause? aggregations x) :aggregation+ (is-clause? :aggregation x) :aggregation-ref(string? x) :string(is-clause? string-functions x) :string-expression(is-clause? :value x) :value@@ -558,6 +569,7 @@[:numeric-expression [:ref ::NumericExpression]][:datetime-expression [:ref ::DatetimeExpression]][:aggregation [:ref ::Aggregation]]+ [:aggregation-ref [:ref ::aggregation]][:string :string][:string-expression [:ref ::StringExpression]][:value [:ref ::value]]diff --git a/src/metabase/lib/schema/ref.cljc b/src/metabase/lib/schema/ref.cljcindex 7cd2d59e92c..620fbd64efe 100644--- a/src/metabase/lib/schema/ref.cljc+++ b/src/metabase/lib/schema/ref.cljc@@ -204,12 +204,19 @@(lib.hierarchy/derive :expression ::ref)+(defn normalize-aggregation-ref-options+ "Normalize an `:aggregation` ref options map."+ [m]+ (when (map? m)+ (normalize-field-options-map m)))+(mr/def ::aggregation-options[:merge+ {:decode/normalize normalize-aggregation-ref-options}::common/options[:map- [:name {:optional true} ::common/non-blank-string]- [:display-name {:optional true} ::common/non-blank-string]+ [:name {:optional true} ::common/non-blank-string]+ [:display-name {:optional true} ::common/non-blank-string][:lib/source-name {:optional true} ::common/non-blank-string]]])(mbql-clause/define-mbql-clause :aggregationdiff --git a/test/metabase/legacy_mbql/schema_test.cljc b/test/metabase/legacy_mbql/schema_test.cljcindex 97c4e2ea0f6..e903c5c6947 100644--- a/test/metabase/legacy_mbql/schema_test.cljc+++ b/test/metabase/legacy_mbql/schema_test.cljc@@ -594,3 +594,39 @@(deftest ^:parallel normalize-unwrapped-field-id-test(is (= [:sum [:field 10 nil]](lib/normalize ::mbql.s/sum [:sum 10]))))++(deftest ^:parallel allow-aggregation-refs-inside-expressions-test+ (let [expr ["concat"+ "We saw a change of "+ ["aggregation" 1 {"base-type" "type/Integer", "name" "Ag 1"}] "%."]+ normalized (lib/normalize ::mbql.s/concat expr)]+ (is (= [:concat+ "We saw a change of "+ [:aggregation 1 {:base-type :type/Integer, :name "Ag 1"}] "%."]+ normalized))+ (is (mr/validate ::mbql.s/concat normalized))))++(deftest ^:parallel allow-aggregation-refs-inside-aggregations-test+ (let [ags [["concat"+ "We saw a change of "+ ["aggregation" 1 {"base-type" "type/Integer"}] "%."]+ ["round"+ ["*"+ ["avg"+ ["/"+ ["field" "rating_change" {"base-type" "type/Decimal"}]+ ["field" "first_rating" {"base-type" "type/Integer"}]]]+ 100]]]+ normalized (lib/normalize ::mbql.s/Aggregations ags)]+ (is (= [[:concat+ "We saw a change of "+ [:aggregation 1 {:base-type :type/Integer}] "%."]+ [:round+ [:*+ [:avg+ [:/+ [:field "rating_change" {:base-type :type/Decimal}]+ [:field "first_rating" {:base-type :type/Integer}]]]+ 100]]]+ normalized))+ (is (mr/validate ::mbql.s/Aggregations normalized))))diff --git a/test/metabase/lib/convert_test.cljc b/test/metabase/lib/convert_test.cljcindex 032caf68f81..529743589b4 100644--- a/test/metabase/lib/convert_test.cljc+++ b/test/metabase/lib/convert_test.cljc@@ -1659,7 +1659,7 @@(deftest ^:parallel case-schema-aggregation-test(is (= [:aggregation-options[:case- [[[:< [:aggregation 0 {"base-type" "type/Float"}] 0.591] "60%"]]]+ [[[:< [:aggregation 0 {:base-type :type/Float}] 0.591] "60%"]]]{:name "A", :display-name "B"}](mbql.normalize/normalize :metabase.legacy-mbql.schema/aggregation-options["aggregation-options"