Skip to content

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

[BOT-1901] Fix missing type info on metric & measure refs (#78520)

Tyler Plude3c9fd3afe9d6
diff --git a/src/metabase/agent_lib/representations/resolve.clj b/src/metabase/agent_lib/representations/resolve.clj
index c69cfd8853a..39028f68539 100644
--- a/src/metabase/agent_lib/representations/resolve.clj
+++ b/src/metabase/agent_lib/representations/resolve.clj
@@ -29,6 +29,8 @@
encoding or for handing back to the LLM as the canonical MBQL 5 representation."
(:require
[clojure.walk :as walk]
+ [metabase.lib.metadata :as lib.metadata]
+ [metabase.lib.metadata.calculation :as lib.metadata.calculation]
[metabase.lib.metadata.protocols :as lib.metadata.protocols]
[metabase.lib.normalize :as lib.normalize]
[metabase.lib.schema :as lib.schema]
@@ -103,6 +105,29 @@
node))
pmbql-query))
+(defn- annotate-metric-and-measure-ref-types
+ "BOT-1901: Stamp `:effective-type` on `:metric` / `:measure` refs missing it, computed
+ from the metric's / measure's aggregation definition — mirroring the FE's `lib.ref/ref-method
+ :metadata/metric`. Untyped refs poison arithmetic type inference in
+ [[metabase.agent-lib.representations.repair/assert-editor-accepts-expressions!]]"
+ [pmbql-query]
+ (walk/postwalk
+ (fn [node]
+ (if (and (vector? node)
+ (#{:metric :measure} (nth node 0 nil))
+ (map? (nth node 1 nil))
+ (pos-int? (nth node 2 nil))
+ (not (contains? (nth node 1) :effective-type)))
+ (let [md (case (nth node 0)
+ :metric (lib.metadata/metric pmbql-query (nth node 2))
+ :measure (lib.metadata/measure pmbql-query (nth node 2)))
+ t (when md (lib.metadata.calculation/type-of pmbql-query md))]
+ (if (and t (isa? t :type/*) (not= t :type/*))
+ (update node 1 assoc :effective-type t)
+ node))
+ node))
+ pmbql-query))
+
(defn- assert-parseable-temporal-literal!
"Throw an agent-facing error if temporal string `s` is not a real date / datetime / year /
year-month — i.e. one [[u.date/parse]] can read. The query schema only checks structure with a
@@ -162,6 +187,7 @@
with-mp (assoc resolved :lib/metadata metadata-provider)]
(-> (lib.normalize/normalize ::lib.schema/query with-mp)
(annotate-field-types metadata-provider)
+ annotate-metric-and-measure-ref-types
validate-absolute-datetime-literals))))
;;; ============================================================
diff --git a/test/metabase/agent_lib/representations/resolve_test.clj b/test/metabase/agent_lib/representations/resolve_test.clj
index 07c3c4c7c9a..afaca10c90e 100644
--- a/test/metabase/agent_lib/representations/resolve_test.clj
+++ b/test/metabase/agent_lib/representations/resolve_test.clj
@@ -15,10 +15,15 @@
covered by `metabase.agent-lib.representations-test`; this suite focuses on resolution."
(:require
[clojure.test :refer [deftest is testing]]
+ [clojure.walk :as walk]
[metabase.agent-lib.representations :as repr]
+ [metabase.agent-lib.representations.repair :as repr.repair]
[metabase.agent-lib.representations.resolve :as repr.resolve]
+ [metabase.lib.core :as lib]
+ [metabase.lib.metadata :as lib.metadata]
[metabase.lib.schema :as lib.schema]
[metabase.lib.test-util :as lib.tu]
+ [metabase.models.serialization.resolve.mp :as resolve.mp]
[metabase.util.malli.registry :as mr]))
(set! *warn-on-reflection* true)
@@ -27,17 +32,26 @@
;;; Fixture metadata provider
;;; ============================================================
+(def ^:private mock-metadata
+ "Base warehouse shape shared by the fixture providers. Fixtures needing extra content
+ (cards, measures) `assoc` onto this and build their own flat provider — deliberately NOT
+ the 2-arity composing form of [[lib.tu/mock-metadata-provider]]: a composed provider
+ satisfies `cached-metadata-provider?`, which makes the resolver treat the application DB
+ as authoritative for table lookups. With this fixture's `db-id 1`, that resolves ORDERS
+ against whatever real database happens to have id 1 in the test-run's app DB instead of
+ the mock."
+ {:database {:id 1 :name "Sample" :dbms-version {:flavor "PostgreSQL"} :engine :postgres}
+ :tables [{:id 10 :name "ORDERS" :schema "PUBLIC" :db-id 1}
+ {:id 11 :name "PRODUCTS" :schema "PUBLIC" :db-id 1}]
+ :fields [{:id 100 :name "ID" :table-id 10 :base-type :type/Integer}
+ {:id 101 :name "TOTAL" :table-id 10 :base-type :type/Float}
+ {:id 102 :name "CREATED_AT" :table-id 10 :base-type :type/DateTime}
+ {:id 103 :name "PRODUCT_ID" :table-id 10 :base-type :type/Integer}
+ {:id 110 :name "ID" :table-id 11 :base-type :type/Integer}
+ {:id 111 :name "CATEGORY" :table-id 11 :base-type :type/Text}]})
+
(def ^:private mp
- (lib.tu/mock-metadata-provider
- {:database {:id 1 :name "Sample" :dbms-version {:flavor "PostgreSQL"} :engine :postgres}
- :tables [{:id 10 :name "ORDERS" :schema "PUBLIC" :db-id 1}
- {:id 11 :name "PRODUCTS" :schema "PUBLIC" :db-id 1}]
- :fields [{:id 100 :name "ID" :table-id 10 :base-type :type/Integer}
- {:id 101 :name "TOTAL" :table-id 10 :base-type :type/Float}
- {:id 102 :name "CREATED_AT" :table-id 10 :base-type :type/DateTime}
- {:id 103 :name "PRODUCT_ID" :table-id 10 :base-type :type/Integer}
- {:id 110 :name "ID" :table-id 11 :base-type :type/Integer}
- {:id 111 :name "CATEGORY" :table-id 11 :base-type :type/Text}]}))
+ (lib.tu/mock-metadata-provider mock-metadata))
(defn- schema-valid? [query]
(nil? (mr/explain ::lib.schema/query query)))
@@ -383,6 +397,157 @@
(is (= :type/Float (:base-type opts))
"inner field clause still gets provider type when it has none"))))
+;;; ============================================================
+;;; annotate-metric-and-measure-ref-types — effective-type stamping
+;;; ============================================================
+
+(def ^:private metric-entity-id "MetricRevenue_0000001")
+
+(def ^:private measure-entity-id "MeasureRevenue_000001")
+
+(def ^:private datetime-metric-entity-id "MetricLatest_00000001")
+
+(def ^:private revenue-definition
+ "A `sum(TOTAL)` definition query, shared by the fixture metric card and measure."
+ (-> (lib/query mp (lib.metadata/table mp 10))
+ (lib/aggregate (lib/sum (lib.metadata/field mp 101)))))
+
+(def ^:private mp-with-metric
+ "The base fixture warehouse plus a metric card and a measure, both summing ORDERS.TOTAL.
+ A flat provider (see [[mock-metadata]] for why not the composing form)."
+ (lib.tu/mock-metadata-provider
+ (assoc mock-metadata
+ :cards [{:id 900
+ :name "Revenue"
+ :database-id 1
+ :table-id 10
+ :type :metric
+ :entity-id metric-entity-id
+ :dataset-query revenue-definition}
+ {:id 901
+ :name "No Aggregation"
+ :database-id 1
+ :table-id 10
+ :type :metric
+ :dataset-query (lib/query mp (lib.metadata/table mp 10))}
+ {:id 902
+ :name "Latest Order"
+ :database-id 1
+ :table-id 10
+ :type :metric
+ :entity-id datetime-metric-entity-id
+ :dataset-query (-> (lib/query mp (lib.metadata/table mp 10))
+ (lib/aggregate (lib/max (lib.metadata/field mp 102))))}]
+ :measures [{:lib/type :metadata/measure
+ :id 77
+ :name "Revenue measure"
+ :table-id 10
+ :definition revenue-definition}])))
+
+(def ^:private metric-content-store
+ (reify resolve.mp/ContentStore
+ (card-by-entity-id [_ eid] (get {metric-entity-id {:id 900 :database_id 1}
+ datetime-metric-entity-id {:id 902 :database_id 1}}
+ eid))
+ (measure-by-entity-id [_ eid] (when (= eid measure-entity-id) {:id 77 :table_id 10}))
+ (segment-by-entity-id [_ _] nil)
+ (measure-by-id [_ _] nil)
+ (segment-by-id [_ _] nil)))
+
+(defn- collect-clauses [q tag]
+ (let [acc (volatile! [])]
+ (walk/postwalk (fn [x]
+ (when (and (vector? x) (= tag (nth x 0 nil)) (map? (nth x 1 nil)))
+ (vswap! acc conj x))
+ x)
+ (:stages q))
+ @acc))
+
+(defn- yoy-growth-parsed
+ "Portable query computing `(agg - offset(agg, -12)) / offset(agg, -12)` by month, where
+ `agg` is a `[\"metric\" ...]` or `[\"measure\" ...]` ref — the notebook shape for
+ year-over-year growth of a saved metric/measure."
+ [agg-ref]
+ (let [off ["offset" {} agg-ref -12]]
+ {"lib/type" "mbql/query"
+ "database" "Sample"
+ "stages" [{"lib/type" "mbql.stage/mbql"
+ "source-table" ["Sample" "PUBLIC" "ORDERS"]
+ "aggregation" [agg-ref
+ off
+ ["/" {} ["-" {} agg-ref off] off]]
+ "breakout" [["field" {"temporal-unit" "month"}
+ ["Sample" "PUBLIC" "ORDERS" "CREATED_AT"]]]}]}))
+
+(deftest metric-refs-carry-effective-type-test
+ (testing "resolved :metric refs are stamped with :effective-type from the metric's definition"
+ (let [parsed (yoy-growth-parsed ["metric" {} metric-entity-id])
+ q (repr.resolve/resolve-query mp-with-metric parsed metric-content-store)
+ refs (collect-clauses q :metric)]
+ (is (= 5 (count refs)))
+ (is (every? #(= :type/Float (:effective-type (nth % 1))) refs))
+ (testing "so metric + offset growth arithmetic passes the expression-editor gate"
+ (is (= q (repr.repair/assert-editor-accepts-expressions! q))))
+ (testing "and the query is still schema-valid"
+ (is (schema-valid? q))))))
+
+(deftest measure-refs-carry-effective-type-test
+ (testing "resolved :measure refs are stamped with :effective-type from the measure's definition"
+ (let [parsed (yoy-growth-parsed ["measure" {} measure-entity-id])
+ q (repr.resolve/resolve-query mp-with-metric parsed metric-content-store)
+ refs (collect-clauses q :measure)]
+ (is (= 5 (count refs)))
+ (is (every? #(= :type/Float (:effective-type (nth % 1))) refs))
+ (testing "so measure + offset growth arithmetic passes the expression-editor gate"
+ (is (= q (repr.repair/assert-editor-accepts-expressions! q))))
+ (testing "and the query is still schema-valid"
+ (is (schema-valid? q))))))
+
+(deftest non-numeric-metric-arithmetic-rejected-test
+ (testing "stamping uses the metric's real type: a datetime metric (max of a datetime field)
+ in numeric arithmetic is stamped :type/DateTime and rejected by the expression-editor
+ gate, just as the FE editor rejects it for FE-authored typed refs"
+ (let [parsed {"lib/type" "mbql/query"
+ "database" "Sample"
+ "stages" [{"lib/type" "mbql.stage/mbql"
+ "source-table" ["Sample" "PUBLIC" "ORDERS"]
+ "aggregation" [["/" {}
+ ["metric" {} datetime-metric-entity-id]
+ 100]]}]}
+ q (repr.resolve/resolve-query mp-with-metric parsed metric-content-store)
+ refs (collect-clauses q :metric)]
+ (is (= 1 (count refs)))
+ (is (= :type/DateTime (:effective-type (nth (first refs) 1))))
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo
+ #"/ expects a number"
+ (repr.repair/assert-editor-accepts-expressions! q))))))
+
+(deftest annotate-metric-ref-types-leaves-hard-cases-untouched-test
+ (let [agg-query (fn [agg]
+ {:lib/type :mbql/query
+ :lib/metadata mp-with-metric
+ :database 1
+ :stages [{:lib/type :mbql.stage/mbql
+ :source-table 10
+ :aggregation [agg]}]})]
+ (testing "a metric id unknown to the metadata provider is left untouched"
+ (let [q (agg-query [:metric {:lib/uuid (str (random-uuid))} 424242])]
+ (is (= q (#'repr.resolve/annotate-metric-and-measure-ref-types q)))))
+ (testing "a metric whose definition has no aggregation (`lib/type-of` falls back to :type/*)
+ is not stamped, so flat arithmetic over the ref keeps passing the gate via the
+ untyped-ref-to-number coercion — the FE's `ref-method :metadata/metric` skips
+ stamping there too"
+ (let [q (agg-query [:/ {:lib/uuid (str (random-uuid))}
+ [:metric {:lib/uuid (str (random-uuid))} 901]
+ 100])
+ q' (#'repr.resolve/annotate-metric-and-measure-ref-types q)]
+ (is (= q q'))
+ (is (= q' (repr.repair/assert-editor-accepts-expressions! q')))))
+ (testing "a ref that already carries :effective-type keeps its authored value"
+ (let [q (agg-query [:metric {:lib/uuid (str (random-uuid))
+ :effective-type :type/Integer} 900])]
+ (is (= q (#'repr.resolve/annotate-metric-and-measure-ref-types q)))))))
+
;;; ============================================================
;;; try-export-query: structured + native + nil/error fallback
;;; ============================================================
diff --git a/test/metabase/metabot/tools/construct_representations_test.clj b/test/metabase/metabot/tools/construct_representations_test.clj
index 6a0e4cd6177..edec7a8fcca 100644
--- a/test/metabase/metabot/tools/construct_representations_test.clj
+++ b/test/metabase/metabot/tools/construct_representations_test.clj
@@ -1012,3 +1012,100 @@
opts (nth field-ref 1)]
(is (= "TOTAL" (nth field-ref 2)))
(is (= "type/Float" (get opts "base-type"))))))))
+
+;;; ============================================================
+;;; Metric aggregations end-to-end: YoY growth via metric + offset
+;;; ============================================================
+
+(def ^:private metric-entity-id
+ "A valid 21-char NanoID so `import-mbql` dispatches on the `[\"metric\" ...]` branch."
+ "MetricRevenue_0000001")
+
+(def ^:private mp-with-metric
+ "ORDERS plus a metric card summing TOTAL. `CREATED_AT` supports the month breakout and the
+ trailing time-interval filter."
+ (lib.tu/mock-metadata-provider
+ {:database {:id 1 :name "Sample"}
+ :tables [{:id 10 :name "ORDERS" :schema "PUBLIC" :db-id 1}]
+ :fields [{:id 100 :name "ID" :table-id 10 :base-type :type/Integer}
+ {:id 101 :name "TOTAL" :table-id 10 :base-type :type/Float}
+ {:id 105 :name "CREATED_AT" :table-id 10 :base-type :type/DateTime}]
+ :cards [{:id 900
+ :name "Revenue"
+ :database-id 1
+ :table-id 10
+ :type :metric
+ :entity-id metric-entity-id
+ :dataset-query {:lib/type :mbql/query
+ :database 1
+ :stages [{:lib/type :mbql.stage/mbql
+ :source-table 10
+ :aggregation [[:sum {}
+ [:field {:base-type :type/Float} 101]]]}]}}]}))
+
+(defn- lookup-metric-stub [model eid]
+ (when (and (or (= model 'Card) (= model :model/Card))
+ (= eid metric-entity-id))
+ {:id 900 :database_id 1 :entity_id eid}))
+
+(defn- with-metric-mp-and-stubs! [f]
+ (with-redefs [lib-be/application-database-metadata-provider (fn [_] mp-with-metric)
+ construct/resolve-database-id-from-first-stage (fn [_] 1)
+ serdes/lookup-by-id lookup-metric-stub
+ api/read-check allow-read-check
+ api/query-check allow-read-check]
+ (f)))
+
+(deftest metric-offset-growth-ratio-end-to-end-test
+ (testing
+ (str "a monthly YoY growth query built from a saved metric - `metric`, `offset(metric, -12)`,\n"
+ "and the ratio `(metric - offset) / offset` - resolves to structured output: the metric\n"
+ "refs get :effective-type stamped from the metric's definition, so the expression-editor\n"
+ "gate accepts the nested arithmetic")
+ (with-metric-mp-and-stubs!
+ (fn []
+ (let [m ["metric" {} metric-entity-id]
+ off ["offset" {} m -12]
+ result (construct/execute-representations-query
+ (query-data
+ {"lib/type" "mbql/query"
+ "database" "Sample"
+ "stages" [{"lib/type" "mbql.stage/mbql"
+ "source-table" ["Sample" "PUBLIC" "ORDERS"]
+ "filters" [["time-interval" {}
+ ["field" {} ["Sample" "PUBLIC" "ORDERS" "CREATED_AT"]]
+ -24 "month"]]
+ "aggregation" [m
+ off
+ ["/" {} ["-" {} m off] off]]
+ "breakout" [["field" {"temporal-unit" "month"}
+ ["Sample" "PUBLIC" "ORDERS" "CREATED_AT"]]]}
+ {"lib/type" "mbql.stage/mbql"
+ "filters" [["time-interval" {}
+ ["field" {} "CREATED_AT"]
+ -12 "month"]]}]}))
+ structured (:structured-output result)
+ q (:query structured)
+ metric-refs (let [acc (volatile! [])]
+ (walk/postwalk (fn [x]
+ (when (and (vector? x)
+ (= :metric (first x)))
+ (vswap! acc conj x))
+ x)
+ (:stages q))
+ @acc)]
+ (testing "the tool returns structured output rather than an expression-editor rejection"
+ (is (string? (:query-id structured)))
+ (is (vector? (:result-columns structured))))
+ (testing "every resolved :metric ref carries :effective-type"
+ (is (= 5 (count metric-refs)))
+ (is (every? #(= :type/Float (:effective-type (nth % 1))) metric-refs)))
+ (testing "query-json exports the metric back to its portable entity_id with the stamp"
+ (let [exported (:query-json structured)
+ exported-agg (get-in exported ["stages" 0 "aggregation" 0])]
+ (is (= "metric" (nth exported-agg 0)))
+ (is (= metric-entity-id (nth exported-agg 2)))
+ (is (= "type/Float" (get-in exported-agg [1 "effective-type"])))
+ (testing "and the export round-trips: re-running the exported form yields an equal export"
+ (let [redo (construct/execute-representations-query (walk/keywordize-keys exported))]
+ (is (= exported (get-in redo [:structured-output :query-json]))))))))))))