Skip to content

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

Show Metabot why an aggregation with no function fails (#85065)

Andrei Simionescua0a3d03f8172
diff --git a/src/metabase/metabot/tools/construct.clj b/src/metabase/metabot/tools/construct.clj
index 3438a204e40..8d6e9ad9672 100644
--- a/src/metabase/metabot/tools/construct.clj
+++ b/src/metabase/metabot/tools/construct.clj
@@ -934,7 +934,8 @@
to Malli's own message. Missing keys the LLM never writes itself are translated into their usual
cause instead of being named."
[error]
- (let [missing (when (= :malli.core/missing-key (:type error)) (peek (:in error)))]
+ (let [missing (when (= :malli.core/missing-key (:type error)) (peek (:in error)))
+ message (when-not missing (me/error-message error))]
(cond
;; normalization stamps `:lib/uuid` on every clause; one missing means normalizing a clause
;; inside it failed, often because it references something that doesn't exist
@@ -947,9 +948,13 @@
missing
(format "missing `%s`" (name missing))
+ ;; `:metabase.lib.schema.aggregation/aggregation`'s check, whose message is a function that calls `tru`
+ (and (fn? message) (= :aggregation (last (butlast (:in error)))))
+ "Aggregations should contain at least one aggregation function. Wrap the calculation in one, such as `sum` or `count`, or move a per-row calculation to `expressions:`"
+
;; the message of `::lib.schema.expression/expression.definition`'s aggregation check
- (= "non-aggregation expression" (me/error-message error))
- (format "%s: aggregation and window functions such as `offset` cannot be custom columns - move them to `aggregation:` or `order-by:`" (me/error-message error)))))
+ (= "non-aggregation expression" message)
+ (format "%s: aggregation and window functions such as `offset` cannot be custom columns - move them to `aggregation:` or `order-by:`" message))))
(defn- query-not-runnable-explanation
"Explain why `pmbql-query` fails the query builder's `canRun` check, or return nil.
diff --git a/test/metabase/metabot/tools/construct_representations_test.clj b/test/metabase/metabot/tools/construct_representations_test.clj
index fff8f136ee3..34460cc7879 100644
--- a/test/metabase/metabot/tools/construct_representations_test.clj
+++ b/test/metabase/metabot/tools/construct_representations_test.clj
@@ -863,6 +863,21 @@
(is (= :filter (:mode (ex-data e))))
(is (re-find #"OFFSET is not supported in custom filters" (ex-message e))))))))
+(deftest aggregation-without-aggregation-function-explained-test
+ (testing "an aggregation with no aggregation function in it is explained, with how to fix it"
+ (with-mp-and-stubs!
+ (fn []
+ (let [e (editor-rejection
+ {"lib/type" "mbql/query"
+ "stages" [{"lib/type" "mbql.stage/mbql"
+ "source-table" ["Sample" "PUBLIC" "ORDERS"]
+ "aggregation" [["+" {} 1 2]]}]})]
+ (is (= :query-not-runnable (:error (ex-data e))))
+ (is (str/ends-with? (ex-message e)
+ (str ": Aggregations should contain at least one aggregation function. Wrap the "
+ "calculation in one, such as `sum` or `count`, or move a per-row calculation "
+ "to `expressions:`."))))))))
+
(deftest editor-rejection-between-message-test
(with-mp-and-stubs!
(fn []