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
Don't crash query repair on malformed stages or clauses (#84967)
Andrei Simionescu6ced6b9c4a48
diff --git a/src/metabase/agent_lib/representations/repair.clj b/src/metabase/agent_lib/representations/repair.cljindex a106b654f22..79fc549f8b4 100644--- a/src/metabase/agent_lib/representations/repair.clj+++ b/src/metabase/agent_lib/representations/repair.clj@@ -1640,12 +1640,13 @@(contains? #{"asc" "desc"} (first clause))))(defn- rewrite-order-by-inline-aggs-in-stage- "Apply the inline-agg-in-`order-by` rewrite to a single string-keyed stage map. Returns the- updated stage. No-op when the stage has no `order-by` or no `aggregation`."+ "Rewrite inline aggregations in a stage's `order-by` clauses.++ Leave the stage unchanged if `order-by` is missing or `aggregation` isn't a list of clauses."[stage](let [aggs (get stage "aggregation")ord (get stage "order-by")]- (if-not (and (vector? aggs) (seq aggs) (vector? ord) (seq ord))+ (if-not (and (vector? aggs) (seq aggs) (every? clause-like? aggs) (vector? ord) (seq ord))stage;; Build a structural-key → index lookup over the aggregations once.(let [agg-key->idx (into {}@@ -1817,6 +1818,9 @@[stage](let [aggs (get stage "aggregation")](cond+ (not (map? stage))+ stage+;; No integer refs anywhere in the stage - nothing to do.(not (stage-has-integer-agg-ref? stage))stage@@ -2013,20 +2017,20 @@form))(defn- split-post-agg-filter-stage- "Detect post-agg-filter shape in `stage`. Return `[stage]` if no split is needed, or- `[stage' new-stage]` if the stage was split. Throws `:agent-error?` ex-info when split is- required but unsafe to perform automatically.-- Auto-fix is intentionally narrow: it moves the offending filter(s) into a second stage and- rewrites their agg-refs to cross-stage `[\"field\" {} \"<col-name>\"]` clauses. It does- NOT move `order-by` / `limit` / `fields` — those frequently reference pre-aggregation- columns that don't survive the split, and silently relocating them produces either a- hard-to-diagnose 'no matching field' error or, worse, a different-row-count query. When- any of those keys is present we refuse and ask the LLM to author two stages explicitly."+ "Split a stage whose filters refer to its own aggregations.++ Return `[stage]` if no split is needed, or `[stage' new-stage]` after a split.+ Rewrite aggregation references in the new stage as cross-stage field references.+ Throw an `:agent-error?` exception if a split is needed but can't be done safely.++ Moving `order-by`, `limit` or `fields` could change the results because those clauses+ may refer to columns absent from the aggregation output. Ask the LLM to write two stages+ explicitly in that case. Leave the stage unchanged for validation if `order-by` or `fields`+ can't be read as a sequence."[stage](let [aggs (get stage "aggregation")- same-stage-uuids (collect-stage-agg-uuids stage)]- (if-not (and (vector? aggs) (seq aggs) (seq same-stage-uuids))+ same-stage-uuids (when (vector? aggs) (collect-stage-agg-uuids stage))]+ (if-not (seq same-stage-uuids)[stage](let [filters (get stage "filters")offending-filter? (fn [f] (form-references-stage-agg? f same-stage-uuids))@@ -2035,6 +2039,9 @@(empty? offending-filters)[stage]+ (some #(not (seqable? (get stage %))) ["order-by" "fields"])+ [stage]+(or (seq (get stage "order-by"))(some? (get stage "limit"))(seq (get stage "fields")))@@ -2451,7 +2458,8 @@;;; ============================================================(defn- stage-join-aliases [stage]- (into #{} (keep #(when (map? %) (get % "alias"))) (get stage "joins")))+ (let [joins (get stage "joins")]+ (into #{} (keep #(when (map? %) (get % "alias"))) (when (sequential? joins) joins))))(defn- string-cross-stage-field-clause?"True if `v` is a string-named field ref to a previous-stage column of a stage whose joins are@@ -2746,7 +2754,8 @@problem per join that has refs naming columns it doesn't return."[query mp content-store import-resolver idx stage](let [{:keys [named path]} (join-alias-refs stage)- checks (vec (for [join (when (or (seq named) (seq path)) (get stage "joins"))+ joins (get stage "joins")+ checks (vec (for [join (when (and (or (seq named) (seq path)) (sequential? joins)) joins):let [alias (when (map? join) (get join "alias"))named-refs (get named alias)path-refs (when-not (plain-table-join? join) (get path alias))]@@ -2820,7 +2829,8 @@q query](if (>= i n)q- (let [cols (mini-resolved-columns mp q i content-store)+ (let [cols (when (map? (get-in q ["stages" i]))+ (mini-resolved-columns mp q i content-store))q' (if cols(update-in q ["stages" i] infer-cross-stage-field-types-in-stage cols)q)]diff --git a/test/metabase/agent_api/api_test.clj b/test/metabase/agent_api/api_test.cljindex 16b5fd3e3c7..e07c09293c9 100644--- a/test/metabase/agent_api/api_test.clj+++ b/test/metabase/agent_api/api_test.clj@@ -431,6 +431,29 @@(is (=? {:error "unknown-stage-key"} response))(is (re-find #"aggregation" (:message response)))))))+(deftest construct-query-rejects-malformed-clauses-test+ (testing "a malformed stage or clause returns HTTP 400 with an error about the query structure"+ (doseq [[label query] (let [post-agg (orders-query :aggregation [["count" {}]]+ :filters [[">" {} ["aggregation" {} 0] 1]])]+ {"a number as `aggregation`"+ (assoc-in (orders-query) [:stages 0 :aggregation] 42)+ "an empty aggregation clause used in `order-by`"+ (orders-query :aggregation [[]] :order-by [["desc" {} []]])+ "a number as `joins`"+ (assoc-in (orders-query) [:stages 0 :joins] 42)+ "a post-aggregation filter with a number as `order-by`"+ (assoc-in post-agg [:stages 0 :order-by] 42)+ "a post-aggregation filter with a number as `fields`"+ (assoc-in post-agg [:stages 0 :fields] 42)+ "a number as a later stage"+ (update (orders-query) :stages conj 42)+ "an aggregation list as a later stage"+ (update (orders-query) :stages conj [["count" {}]])})+ endpoint ["agent/v2/construct-query" "agent/v2/query"]]+ (testing (str endpoint ", " label)+ (is (=? {:error "invalid-representations-query"}+ (mt/user-http-request :rasta :post 400 endpoint {:query query})))))))+(deftest construct-query-ignores-legacy-envelope-test(testing (str "Legacy `source_entity` / `referenced_entities` envelope from the pre-repr program API ""is dropped by the request schema, so a caller still sending the old shape is served.")diff --git a/test/metabase/agent_lib/representations/repair_test.clj b/test/metabase/agent_lib/representations/repair_test.cljindex ce1bcb2b0ad..d7fd5b37ba6 100644--- a/test/metabase/agent_lib/representations/repair_test.clj+++ b/test/metabase/agent_lib/representations/repair_test.clj@@ -2990,6 +2990,21 @@(catch clojure.lang.ExceptionInfo e(is (= :post-agg-filter-with-trailing-clauses (:error (ex-data e)))))))))+(deftest ^:parallel split-post-agg-refuses-when-order-by-is-a-string-test+ (testing "a string `order-by` with a post-aggregation filter gets advice to split the stage"+ (is (=? {:error :post-agg-filter-with-trailing-clauses}+ (try+ (repair/repair trivial-mp {"lib/type" "mbql/query"+ "database" "Sample"+ "stages" [{"lib/type" "mbql.stage/mbql"+ "source-table" ["Sample" "PUBLIC" "ORDERS"]+ "aggregation" [["count" {}]]+ "filters" [[">" {} ["aggregation" {} 0] 10]]+ "order-by" "TOTAL"}]})+ nil+ (catch clojure.lang.ExceptionInfo e+ (ex-data e)))))))+(deftest ^:parallel split-post-agg-refuses-when-raw-column-order-by-test(testing (str "specifically the pre-agg `order-by [desc TOTAL]` case the reviewer flagged ""as silent-intent-destruction in the prior implementation: TOTAL doesn't "@@ -3165,6 +3180,35 @@(build ["count-where" {} ["=" {} ["field" {} ["Sample" "PUBLIC" "ORDERS" "STATUS"]] "x"]]))["stages" 1 "filters"])))))))+(deftest ^:parallel malformed-clauses-reach-validation-test+ (testing "repair leaves malformed stages and clauses for validation"+ ;; ORDERS has to resolve for the cross-stage pass to reach a later stage+ (let [orders {"lib/type" "mbql.stage/mbql" "source-table" ["Sample" "PUBLIC" "ORDERS"]}+ post-agg (assoc orders "aggregation" [["count" {}]] "filters" [[">" {} ["aggregation" {} 0] 1]])]+ (doseq [[label stages] {"a number as `aggregation`"+ [(assoc orders "aggregation" 42)]+ "an empty aggregation clause used in `order-by`"+ [(assoc orders "aggregation" [[]] "order-by" [["desc" {} []]])]+ "a number as `joins`"+ [(assoc orders "joins" 42)]+ "a join-alias reference with a number as `joins`"+ [(assoc orders "joins" 42 "breakout" [["field" {"join-alias" "P"} "CATEGORY"]])]+ "a post-aggregation filter with a number as `order-by`"+ [(assoc post-agg "order-by" 42)]+ "a post-aggregation filter with a number as `fields`"+ [(assoc post-agg "fields" 42)]+ "a number as a later stage" [orders 42]+ "a string as a later stage" [orders "ORDERS"]+ "true as a later stage" [orders true]+ "an empty list as a later stage" [orders []]+ "an aggregation list as a later stage" [orders [["count" {}]]]}]+ (testing label+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"Representations query has an invalid structure"+ (repr/validate-query+ (repair/repair mp-fks {"lib/type" "mbql/query"+ "database" "Sample"+ "stages" stages})))))))))+;;; ============================================================;;; Pass 6 - friendly error messages (E1..E6);;;diff --git a/test/metabase/mcp/v2/tools/query_test.clj b/test/metabase/mcp/v2/tools/query_test.cljindex 542acca3888..309674e868b 100644--- a/test/metabase/mcp/v2/tools/query_test.clj+++ b/test/metabase/mcp/v2/tools/query_test.clj@@ -986,6 +986,28 @@(is (str/includes? msg "1"))(is (str/includes? msg "fields"))))))+(deftest ^:parallel malformed-clauses-name-the-offending-path-test+ (mt/with-current-user (mt/user->id :rasta)+ (let [orders (first (:stages (numeric-orders-query)))+ post-agg (assoc orders :aggregation [["count" {}]] :filters [[">" {} ["aggregation" {} 0] 1]])]+ (doseq [[label path stages] [["a number as `aggregation`" "[0] \"aggregation\":"+ [(assoc orders :aggregation 42)]]+ ["an empty aggregation clause used in `order-by`" "[0] \"aggregation\":"+ [(assoc orders :aggregation [[]] :order-by [["desc" {} []]])]]+ ["a number as `joins`" "[0] \"joins\":"+ [(assoc orders :joins 42)]]+ ["a post-aggregation filter with a number as `order-by`" "[0] \"order-by\":"+ [(assoc post-agg :order-by 42)]]+ ["a post-aggregation filter with a number as `fields`" "[0] \"fields\":"+ [(assoc post-agg :fields 42)]]+ ["a number as a later stage" "[1]" [orders 42]]+ ["an aggregation list as a later stage" "[1]" [orders [["count" {}]]]]]]+ (testing label+ (is (str/starts-with? (error-text (registry/call-tool execute-scope (str (random-uuid)) "execute_query"+ {:query {:lib/type "mbql/query" :stages stages}}))+ (str "\"Representations query has an invalid structure.\" "+ "Invalid at \"stages\": " path " "))))))))+;; not ^:parallel: mt/with-model-cleanup on the shared query-handle table(deftest repair-forgiven-shortcuts-still-execute-test(testing "GHY-4313: the shortcuts the repair layer exists to forgive are not schema failures"diff --git a/test/metabase/metabot/tools_test.clj b/test/metabase/metabot/tools_test.cljindex 843a366b7c0..a9ba6c1c399 100644--- a/test/metabase/metabot/tools_test.clj+++ b/test/metabase/metabot/tools_test.clj@@ -301,6 +301,33 @@(is (= {:class :unrecoverable :code :internal}(construct-tool-failure-for-thrown e)))))))+(deftest construct-notebook-query-tool-malformed-clauses-test+ (testing "the model can retry after a malformed stage or clause"+ (let [orders {:lib/type "mbql.stage/mbql"+ :source-table [(t2/select-one-fn :name :model/Database (mt/id)) "PUBLIC" "ORDERS"]}+ post-agg (assoc orders :aggregation [["count" {}]] :filters [[">" {} ["aggregation" {} 0] 1]])]+ (mt/with-current-user (mt/user->id :rasta)+ (doseq [[label stages] {"a number as `aggregation`"+ [(assoc orders :aggregation 42)]+ "an empty aggregation clause used in `order-by`"+ [(assoc orders :aggregation [[]] :order-by [["desc" {} []]])]+ "a number as `joins`"+ [(assoc orders :joins 42)]+ "a post-aggregation filter with a number as `order-by`"+ [(assoc post-agg :order-by 42)]+ "a post-aggregation filter with a number as `fields`"+ [(assoc post-agg :fields 42)]+ "a number as a later stage" [orders 42]+ "an aggregation list as a later stage" [orders [["count" {}]]]}]+ (testing label+ (is (=? {:class :recoverable+ :code :metabase.metabot.tools.recoverable.pipeline/invalid-representations-query+ :text #"^Representations query has an invalid structure\."}+ (test-util/tool-failure agent-tools/construct-notebook-query-tool+ {:query {:lib/type "mbql/query" :stages stages}+ :title "Orders"+ :description "Orders by count."})))))))))+(deftest ->entries-test(testing "an entry carries what the adapters and the system message read"(let [entries (agent-tools/->entries [#'agent-tools/create-sql-query-tool