Skip to content

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

Check custom aggregations don't contain unaggregated refs (#75797)

Riley Thompson5af51e630dce
diff --git a/src/metabase/lib/expression.cljc b/src/metabase/lib/expression.cljc
index ccb0a51cee0..92f845d44ad 100644
--- a/src/metabase/lib/expression.cljc
+++ b/src/metabase/lib/expression.cljc
@@ -567,7 +567,7 @@
(assoc opts :name new-name :display-name new-name))))))
(def ^:private aggregation-explainer
- (mr/explainer ::lib.schema.aggregation/aggregation))
+ (mr/explainer ::lib.schema.aggregation/aggregation-with-no-unaggregated-refs))
(def ^:private filter-explainer
(mr/explainer ::lib.schema.expression/boolean))
diff --git a/src/metabase/lib/schema/aggregation.cljc b/src/metabase/lib/schema/aggregation.cljc
index 4d31d7917cf..e07ac6beff4 100644
--- a/src/metabase/lib/schema/aggregation.cljc
+++ b/src/metabase/lib/schema/aggregation.cljc
@@ -152,6 +152,39 @@
(mr/def ::aggregations
[:sequential {:min 1} [:ref ::aggregation]])
+(defn- has-unaggregated-ref?
+ "Checks if `x` contains a column reference that is not wrapped in an aggregation
+ function, e.g. the `[:field 2]` in `[:+ [:sum [:field 1]] [:field 2]]`."
+ [x]
+ (when (vector? x)
+ (let [[tag _opts & args] x]
+ (cond
+ (#{:field :expression} tag)
+ true
+
+ (lib.hierarchy/isa? tag ::aggregation-clause-tag)
+ false
+
+ ;; case/if conditions can reference unaggregated refs, we
+ ;; only need to check the result branches are aggregated
+ (#{:case :if} tag)
+ (let [[pairs default] args]
+ (boolean (or (some (fn [[_cond expr]] (has-unaggregated-ref? expr)) pairs)
+ (has-unaggregated-ref? default))))
+
+ :else
+ (boolean (some has-unaggregated-ref? args))))))
+
+(mr/def ::aggregation-with-no-unaggregated-refs
+ "Stricter variant of ::aggregation used for query builder verification. Not enforced
+ on whole queries, so saved questions with unaggregated fields keep working."
+ [:and
+ [:ref ::aggregation]
+ [:fn
+ {:error/message #(i18n/tru "Fields in custom aggregations must be wrapped with a function like Sum.")
+ :error/friendly true}
+ (complement has-unaggregated-ref?)]])
+
(def aggregation-operators
"The list of available aggregation operator.
The order of operators is relevant for the front end."
diff --git a/test/metabase/lib/expression_test.cljc b/test/metabase/lib/expression_test.cljc
index 41902afb6d0..d989d7eb498 100644
--- a/test/metabase/lib/expression_test.cljc
+++ b/test/metabase/lib/expression_test.cljc
@@ -543,7 +543,8 @@
(nil? (lib.expression/diagnose-expression query 0 mode expr c-pos))
:expression (get exprs "non-circular-c")
:aggregation (-> (get exprs "circular-c")
- (assoc 3 (lib/count)))
+ (update 2 lib/sum)
+ (assoc 4 (lib/count)))
:filter (assoc (get exprs "circular-c") 0 :=)))
(testing "circular definition"
(is (=? {:message "Cycle detected: c → x → b → c"}
@@ -659,6 +660,34 @@
[:+ 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 diagnose-expression-unaggregated-fields-test
+ (let [query (lib/query meta/metadata-provider (meta/table-metadata :orders))
+ created-at [:field (meta/id :orders :created-at)]
+ total [:field (meta/id :orders :total)]
+ subtotal [:field (meta/id :orders :subtotal)]
+ diagnose-expr (fn [expr]
+ (lib.expression/diagnose-expression query 0 :aggregation (lib/->mbql5 expr) nil))]
+ (testing "aggregations without an aggregation function are rejected with an understandable error message"
+ (are [expr] (= {:message "Aggregations should contain at least one aggregation function."
+ :friendly true}
+ (diagnose-expr expr))
+ [:datetime-diff created-at created-at :hour]
+ total
+ [:+ total subtotal]))
+ (testing "aggregations with unaggregated refs are rejected with an understandable error message"
+ (are [expr] (= {:message "Fields in custom aggregations must be wrapped with a function like Sum."
+ :friendly true}
+ (diagnose-expr expr))
+ [:+ [:sum total] subtotal]
+ [:+ [:sum total] [:floor subtotal]]))
+ (testing "aggregations that contain an aggregation function are accepted"
+ (are [expr] (nil? (diagnose-expr expr))
+ [:sum total]
+ [:+ [:sum total] [:sum subtotal]]
+ [:+ [:sum total] 4]
+ [:+ [:sum total] [:floor [:sum subtotal]]]
+ [:+ [:sum total] [:count]]))))
+
(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"))