Skip to content

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

Repair bare aggregations before resolving sort references (#84899)

Andrei Simionescuf4d153799d8d
diff --git a/src/metabase/agent_lib/representations/repair.clj b/src/metabase/agent_lib/representations/repair.clj
index 3f5ccad898a..b776682f996 100644
--- a/src/metabase/agent_lib/representations/repair.clj
+++ b/src/metabase/agent_lib/representations/repair.clj
@@ -1350,8 +1350,7 @@
form))
;;; ============================================================
-;;; Pass 1.55 -- normalise `fields:` on a stage / join when the LLM wrote a single clause
-;;; instead of a sequential of clauses.
+;;; Pass 1.55 -- wrap a single `fields:` or `aggregation:` clause in a list
;;;
;;; The repr schema for both stage and join requires
;;; `"fields": [:sequential [:ref ::clause]]` (joins additionally accept the enum strings
@@ -1384,6 +1383,20 @@
[(_ :guard string?) (_ :guard map?) & _])}
(assoc &match "fields" [single-clause])))
+(defn- normalise-aggregation-clauses*
+ "Wrap a stage's bare aggregation clause in a one-element list."
+ [query]
+ (if-not (and (map? query) (vector? (get query "stages")))
+ query
+ (update query "stages"
+ (fn [stages]
+ (mapv (fn [stage]
+ (let [aggregation (get stage "aggregation")]
+ (cond-> stage
+ (and (vector? aggregation) (string? (first aggregation)))
+ (assoc "aggregation" [aggregation]))))
+ stages)))))
+
;;; ============================================================
;;; Pass 1.5 -- normalize `expressions:` shape (map -> sequential; stamp `lib/expression-name`)
;;;
@@ -1772,6 +1785,10 @@
{:agent-error? true
:error :aggregation-ref-no-aggregations}))
+ ;; Let query validation report malformed aggregation entries.
+ (not-every? clause-like? aggs)
+ stage
+
:else
(let [[stamped-aggs index->uuid] (ensure-aggregation-uuids aggs)
index->type (mapv infer-agg-base-type stamped-aggs)
@@ -3099,6 +3116,7 @@
swap-between-bounds*
normalise-case-clauses*
normalise-fields-shape*
+ normalise-aggregation-clauses*
normalize-expressions-shape*
ensure-lib-types*))
diff --git a/test/metabase/agent_lib/representations/repair_test.clj b/test/metabase/agent_lib/representations/repair_test.clj
index 23de130784f..c3bb9339984 100644
--- a/test/metabase/agent_lib/representations/repair_test.clj
+++ b/test/metabase/agent_lib/representations/repair_test.clj
@@ -2564,6 +2564,46 @@
;;; Pass 2.8 - integer-index aggregation refs → canonical UUID form (repr-plan step 10)
;;; ============================================================
+(deftest ^:parallel bare-aggregation-clause-test
+ (doseq [aggregation [["count"] ["count" {}]]
+ ordered? [false true]]
+ (testing (str "single aggregation " (pr-str aggregation) ", sorting enabled: " ordered?)
+ (let [stage (cond-> {"source-table" ["Sample" "PUBLIC" "ORDERS"]
+ "aggregation" aggregation}
+ ordered? (assoc "order-by" [["desc" {} ["aggregation" {} 0]]]))
+ repaired (repair/repair trivial-mp {"lib/type" "mbql/query", "stages" [stage]})
+ aggs (get-in repaired ["stages" 0 "aggregation"])
+ uuid (get-in aggs [0 1 "lib/uuid"])]
+ (if ordered?
+ (do
+ (is (uuid-string? uuid))
+ (is (= [["count" {"lib/uuid" uuid}]] aggs))
+ (is (= ["aggregation" {"base-type" "type/BigInteger", "effective-type" "type/BigInteger"} uuid]
+ (get-in repaired ["stages" 0 "order-by" 0 2]))))
+ (is (= [["count" {}]] aggs)))
+ (is (= repaired (repair/repair trivial-mp repaired)))))))
+
+(deftest ^:parallel bare-aggregation-repair-keeps-expression-named-aggregation-test
+ (let [q {"lib/type" "mbql/query"
+ "stages" [{"source-table" ["Sample" "PUBLIC" "ORDERS"]
+ "expressions" {"aggregation" ["+" {} 1 2]}}]}
+ repaired (repair/repair trivial-mp q)]
+ (is (= ["+" {"lib/expression-name" "aggregation"} 1 2]
+ (get-in repaired ["stages" 0 "expressions" 0])))))
+
+(deftest ^:parallel aggregation-ref-invalid-entry-test
+ (doseq [entry [nil {} 42 [] [42 {}]]
+ aggs [[["count" {}] entry] [entry ["count" {}]]]]
+ (testing (pr-str aggs)
+ (let [q {"lib/type" "mbql/query"
+ "stages" [{"source-table" ["Sample" "PUBLIC" "ORDERS"]
+ "aggregation" aggs
+ "order-by" [["desc" {} ["aggregation" {} 0]]]}]}
+ repaired (repair/repair trivial-mp q)]
+ (is (= aggs (get-in repaired ["stages" 0 "aggregation"])))
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"invalid structure"
+ (repr/validate-query repaired)))))))
+
(deftest ^:parallel aggregation-ref-integer-index-happy-path-test
(testing "a 0-based integer agg-ref in order-by is rewritten to the canonical UUID form"
(let [q {"lib/type" "mbql/query"
diff --git a/test/metabase/metabot/tools/construct_representations_test.clj b/test/metabase/metabot/tools/construct_representations_test.clj
index 99e20ea9032..0e65bdb8c45 100644
--- a/test/metabase/metabot/tools/construct_representations_test.clj
+++ b/test/metabase/metabot/tools/construct_representations_test.clj
@@ -23,7 +23,9 @@
[metabase.metabot.agent.links :as links]
[metabase.metabot.db :as metabot.db]
[metabase.metabot.tools.construct :as construct]
+ [metabase.metabot.tools.core :as tools]
[metabase.metabot.tools.entity-details :as entity-details]
+ [metabase.metabot.tools.error :as tools.error]
[metabase.metabot.tools.recovery-hints :as v1-hints]
[metabase.models.interface :as mi]
[metabase.models.serialization.resolve :as serdes.resolve]
@@ -849,6 +851,44 @@
;;; Step-10 contract: aggregation references via 0-based integer index
;;; ============================================================
+(deftest bare-aggregation-clause-test
+ (with-mp-and-stubs!
+ (fn []
+ (doseq [ordered? [false true]]
+ (testing (str "single count aggregation, sorting enabled: " ordered?)
+ (let [stage (cond-> {"source-table" ["Sample" "PUBLIC" "ORDERS"]
+ "aggregation" ["count" {}]}
+ ordered? (assoc "order-by" [["desc" {} ["aggregation" {} 0]]]))
+ result (construct/execute-representations-query
+ (query-data {"lib/type" "mbql/query", "stages" [stage]}))
+ query (get-in result [:structured-output :query])
+ aggs (get-in query [:stages 0 :aggregation])]
+ (is (mr/validate :metabase.lib.schema/query query))
+ (is (= [:count] (mapv first aggs)))
+ (when ordered?
+ (is (= (get-in aggs [0 1 :lib/uuid])
+ (get-in query [:stages 0 :order-by 0 2 2]))))))))))
+
+(deftest invalid-aggregation-entry-surfaces-agent-error-test
+ (with-mp-and-stubs!
+ (fn []
+ (try
+ (tools/handle construct/construct-notebook-query-tool
+ {:query (query-data {"lib/type" "mbql/query"
+ "stages" [{"source-table" ["Sample" "PUBLIC" "ORDERS"]
+ "aggregation" [["count" {}] 42]
+ "order-by" [["desc" {} ["aggregation" {} 0]]]}]})}
+ {})
+ (is false "Invalid aggregation entries should throw an exception")
+ (catch clojure.lang.ExceptionInfo e
+ (is (true? (:agent-error? (ex-data e))))
+ (is (= 400 (:status-code (ex-data e))))
+ (is (= {:class :recoverable
+ :code :metabase.metabot.tools.recoverable.pipeline/invalid-representations-query
+ :message "Representations query has an invalid structure."
+ :recovery []}
+ (select-keys (tools.error/classify e) [:class :code :message :recovery]))))))))
+
(deftest aggregation-ref-integer-index-in-order-by-end-to-end-test
(testing (str "`[aggregation, {}, <index>]` in order-by is resolved to the canonical\n"
"UUID-keyed MBQL 5 form, and the resolved query validates via lib/query.")