Skip to content

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

Measures cleanup (#67960)

metambenfd9a13e05ea1
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.edn
index e138093e9ac..26564317c22 100644
--- a/.clj-kondo/config/modules/config.edn
+++ b/.clj-kondo/config/modules/config.edn
@@ -758,6 +758,7 @@
metabase.lib.util.match
metabase.lib.walk}
:uses #{config
+ graph
legacy-mbql
types
util}}
@@ -800,7 +801,6 @@
:api #{metabase.measures.api}
:uses #{api
events
- legacy-mbql
lib
lib-be
models
diff --git a/enterprise/backend/test/metabase_enterprise/dependencies/api_test.clj b/enterprise/backend/test/metabase_enterprise/dependencies/api_test.clj
index 2432d3db4d5..b663d4c084f 100644
--- a/enterprise/backend/test/metabase_enterprise/dependencies/api_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/dependencies/api_test.clj
@@ -1463,14 +1463,18 @@
(deftest ^:sequential unreferenced-archived-measure-test
(testing "GET /api/ee/dependencies/graph/unreferenced with archived parameter for measures"
(mt/with-premium-features #{:dependencies}
- (let [products-id (mt/id :products)
- price-field-id (mt/id :products :price)]
+ (let [mp (mt/metadata-provider)
+ products-id (mt/id :products)
+ products (lib.metadata/table mp products-id)
+ price (lib.metadata/field mp (mt/id :products :price))
+ measure-definition (-> (lib/query mp products)
+ (lib/aggregate (lib/sum price)))]
(mt/with-temp [:model/Measure {unreffed-measure-id :id :as unreffed-measure} {:name "Unreferenced Measure - archivedtest"
:table_id products-id
- :definition {:aggregation [[:sum [:field price-field-id nil]]]}}
+ :definition measure-definition}
:model/Measure {archived-measure-id :id :as archived-measure} {:name "Archived Unreferenced Measure - archivedtest"
:table_id products-id
- :definition {:aggregation [[:sum [:field price-field-id nil]]]}
+ :definition measure-definition
:archived true}]
(events/publish-event! :event/measure-create {:object unreffed-measure :user-id (mt/user->id :crowberto)})
(events/publish-event! :event/measure-create {:object archived-measure :user-id (mt/user->id :crowberto)})
diff --git a/enterprise/backend/test/metabase_enterprise/serialization/v2/extract_test.clj b/enterprise/backend/test/metabase_enterprise/serialization/v2/extract_test.clj
index e27041bc332..e18c09b7d9b 100644
--- a/enterprise/backend/test/metabase_enterprise/serialization/v2/extract_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/serialization/v2/extract_test.clj
@@ -962,6 +962,92 @@
{:model "Field" :id "Some Field"}]}
(set (serdes/dependencies ser))))))))))))
+(deftest measure-referencing-measure-test
+ (mt/with-empty-h2-app-db!
+ (ts/with-temp-dpc [:model/User {ann-id :id} {:first_name "Ann"
+ :last_name "Wilson"
+ :email "[email protected]"}
+ :model/Database {db-id :id} {:name "My Database"}
+ :model/Table {table-id :id} {:name "My Table" :db_id db-id}
+ :model/Field {field-id :id} {:name "Amount" :table_id table-id}]
+ (let [base-definition (pmbql-measure-definition db-id table-id field-id)]
+ (ts/with-temp-dpc [:model/Measure
+ {m1-id :id
+ m1-eid :entity_id}
+ {:name "Base Measure"
+ :creator_id ann-id
+ :table_id table-id
+ :definition base-definition}]
+ ;; Create a measure that references the base measure
+ (let [metadata-provider (lib-be/application-database-metadata-provider db-id)
+ table (lib.metadata/table metadata-provider table-id)
+ query (lib/query metadata-provider table)
+ measure-ref (lib.metadata/measure metadata-provider m1-id)
+ derived-definition (lib/aggregate query (lib/* measure-ref 2))]
+ (ts/with-temp-dpc [:model/Measure
+ {m2-id :id
+ m2-eid :entity_id}
+ {:name "Derived Measure"
+ :creator_id ann-id
+ :table_id table-id
+ :definition derived-definition}]
+ (testing "measure referencing another measure"
+ (let [ser (serdes/extract-one "Measure" {} (t2/select-one :model/Measure :id m2-id))]
+ (is (=? {:serdes/meta [{:model "Measure" :id m2-eid :label "derived_measure"}]
+ :table_id ["My Database" nil "My Table"]
+ :creator_id "[email protected]"
+ :definition {:database "My Database"
+ :type :query
+ :query {:source-table ["My Database" nil "My Table"]
+ :aggregation [[:* [:measure m1-eid] 2]]}}}
+ ser))
+ (testing "depends on the referenced Measure"
+ (is (contains? (set (serdes/dependencies ser))
+ [{:model "Measure" :id m1-eid}]))))))))))))
+
+(deftest measure-referencing-segment-test
+ (mt/with-empty-h2-app-db!
+ (ts/with-temp-dpc [:model/User {ann-id :id} {:first_name "Ann"
+ :last_name "Wilson"
+ :email "[email protected]"}
+ :model/Database {db-id :id} {:name "My Database"}
+ :model/Table {table-id :id} {:name "My Table" :db_id db-id}
+ :model/Field {field-id :id} {:name "Price" :table_id table-id}
+ :model/Segment
+ {seg-id :id
+ seg-eid :entity_id}
+ {:name "Expensive Items"
+ :creator_id ann-id
+ :table_id table-id
+ :definition {:source-table table-id
+ :filter [:> [:field field-id nil] 100]}}]
+ ;; Create a measure that uses count-where with a segment reference
+ (let [metadata-provider (lib-be/application-database-metadata-provider db-id)
+ table (lib.metadata/table metadata-provider table-id)
+ query (lib/query metadata-provider table)
+ segment-ref (lib.metadata/segment metadata-provider seg-id)
+ measure-definition (lib/aggregate query (lib/count-where segment-ref))]
+ (ts/with-temp-dpc [:model/Measure
+ {m-id :id
+ m-eid :entity_id}
+ {:name "Expensive Item Count"
+ :creator_id ann-id
+ :table_id table-id
+ :definition measure-definition}]
+ (testing "measure referencing a segment"
+ (let [ser (serdes/extract-one "Measure" {} (t2/select-one :model/Measure :id m-id))]
+ (is (=? {:serdes/meta [{:model "Measure" :id m-eid :label "expensive_item_count"}]
+ :table_id ["My Database" nil "My Table"]
+ :creator_id "[email protected]"
+ :definition {:database "My Database"
+ :type :query
+ :query {:source-table ["My Database" nil "My Table"]
+ :aggregation [[:count-where [:segment seg-eid]]]}}}
+ ser))
+ (testing "depends on the referenced Segment"
+ (is (contains? (set (serdes/dependencies ser))
+ [{:model "Segment" :id seg-eid}]))))))))))
+
(deftest table-publishing-serdes-test
(mt/with-empty-h2-app-db!
(ts/with-temp-dpc [:model/Database {db-id :id} {:name "My Database"}
diff --git a/enterprise/backend/test/metabase_enterprise/serialization/v2/load_test.clj b/enterprise/backend/test/metabase_enterprise/serialization/v2/load_test.clj
index bf9bf617355..fd149db1402 100644
--- a/enterprise/backend/test/metabase_enterprise/serialization/v2/load_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/serialization/v2/load_test.clj
@@ -436,6 +436,145 @@
:database (:id @db1d)}
(:definition @msr1d))))))))))
+(deftest measure-referencing-measure-test
+ ;; Test that a measure referencing another measure can be serialized and deserialized correctly.
+ ;; The :measure clause in the definition should use entity_id for portability.
+ (testing "measure referencing another measure round-trip"
+ (let [serialized (atom nil)
+ db1s (atom nil)
+ table1s (atom nil)
+ field1s (atom nil)
+ msr1s (atom nil)
+ msr2s (atom nil)
+ user1s (atom nil)
+ db1d (atom nil)
+ table1d (atom nil)
+ field1d (atom nil)
+ msr1d (atom nil)
+ msr2d (atom nil)]
+
+ (ts/with-dbs [source-db dest-db]
+ (testing "serializing measures where one references the other"
+ (ts/with-db source-db
+ (reset! db1s (ts/create! :model/Database :name "my-db"))
+ (reset! table1s (ts/create! :model/Table :name "sales" :db_id (:id @db1s)))
+ (reset! field1s (ts/create! :model/Field :name "amount" :table_id (:id @table1s)))
+ (reset! user1s (ts/create! :model/User :first_name "Tom" :last_name "Scholz" :email "[email protected]"))
+ ;; Create base measure: sum(amount)
+ (reset! msr1s (ts/create! :model/Measure :table_id (:id @table1s) :name "Total Sales"
+ :definition (pmbql-measure-definition (:id @db1s) (:id @table1s) (:id @field1s))
+ :creator_id (:id @user1s)))
+ ;; Create derived measure: base_measure * 2
+ (let [mp (lib-be/application-database-metadata-provider (:id @db1s))
+ table (lib.metadata/table mp (:id @table1s))
+ query (lib/query mp table)
+ base-measure (lib.metadata/measure mp (:id @msr1s))
+ derived-definition (lib/aggregate query (lib/* base-measure 2))]
+ (reset! msr2s (ts/create! :model/Measure :table_id (:id @table1s) :name "Double Sales"
+ :definition derived-definition
+ :creator_id (:id @user1s))))
+ (reset! serialized (into [] (serdes.extract/extract {})))))
+
+ (testing "exported form has measure reference with entity_id"
+ (let [derived-measure (first (filter #(= "Double Sales" (:name %)) (by-model @serialized "Measure")))]
+ (is (=? {:definition {:query {:aggregation [[:* [:measure (:entity_id @msr1s)] 2]]}}}
+ derived-measure))))
+
+ (testing "deserializing adjusts the measure IDs properly"
+ (ts/with-db dest-db
+ (reset! user1s (ts/create! :model/User :first_name "Tom" :last_name "Scholz" :email "[email protected]"))
+
+ ;; Load the serialized content
+ (serdes.load/load-metabase! (ingestion-in-memory @serialized))
+
+ ;; Fetch the relevant bits
+ (reset! db1d (t2/select-one :model/Database :name "my-db"))
+ (reset! table1d (t2/select-one :model/Table :name "sales"))
+ (reset! field1d (t2/select-one :model/Field :table_id (:id @table1d) :name "amount"))
+ (reset! msr1d (t2/select-one :model/Measure :name "Total Sales"))
+ (reset! msr2d (t2/select-one :model/Measure :name "Double Sales"))
+
+ (testing "both measures were loaded"
+ (is (some? @msr1d))
+ (is (some? @msr2d)))
+
+ (testing "the derived measure's definition references the base measure by new ID"
+ (is (=? {:lib/type :mbql/query
+ :stages [{:source-table (:id @table1d)
+ :aggregation [[:* {} [:measure {} (:id @msr1d)] 2]]}]
+ :database (:id @db1d)}
+ (:definition @msr2d))))))))))
+
+(deftest measure-referencing-segment-test
+ ;; Test that a measure referencing a segment can be serialized and deserialized correctly.
+ ;; The :segment clause in the definition should use entity_id for portability.
+ (testing "measure referencing a segment round-trip"
+ (let [serialized (atom nil)
+ db1s (atom nil)
+ table1s (atom nil)
+ field1s (atom nil)
+ seg1s (atom nil)
+ msr1s (atom nil)
+ user1s (atom nil)
+ db1d (atom nil)
+ table1d (atom nil)
+ field1d (atom nil)
+ seg1d (atom nil)
+ msr1d (atom nil)]
+
+ (ts/with-dbs [source-db dest-db]
+ (testing "serializing measure that references a segment"
+ (ts/with-db source-db
+ (reset! db1s (ts/create! :model/Database :name "my-db"))
+ (reset! table1s (ts/create! :model/Table :name "products" :db_id (:id @db1s)))
+ (reset! field1s (ts/create! :model/Field :name "price" :table_id (:id @table1s)))
+ (reset! user1s (ts/create! :model/User :first_name "Tom" :last_name "Scholz" :email "[email protected]"))
+ ;; Create segment: price > 100
+ (reset! seg1s (ts/create! :model/Segment :table_id (:id @table1s) :name "Expensive"
+ :definition {:source-table (:id @table1s)
+ :filter [:> [:field (:id @field1s) nil] 100]}
+ :creator_id (:id @user1s)))
+ ;; Create measure: count-where(segment)
+ (let [mp (lib-be/application-database-metadata-provider (:id @db1s))
+ table (lib.metadata/table mp (:id @table1s))
+ query (lib/query mp table)
+ segment (lib.metadata/segment mp (:id @seg1s))
+ measure-definition (lib/aggregate query (lib/count-where segment))]
+ (reset! msr1s (ts/create! :model/Measure :table_id (:id @table1s) :name "Expensive Count"
+ :definition measure-definition
+ :creator_id (:id @user1s))))
+ (reset! serialized (into [] (serdes.extract/extract {})))))
+
+ (testing "exported form has segment reference with entity_id"
+ (let [measure (first (by-model @serialized "Measure"))]
+ (is (=? {:definition {:query {:aggregation [[:count-where [:segment (:entity_id @seg1s)]]]}}}
+ measure))))
+
+ (testing "deserializing adjusts the segment IDs properly"
+ (ts/with-db dest-db
+ (reset! user1s (ts/create! :model/User :first_name "Tom" :last_name "Scholz" :email "[email protected]"))
+
+ ;; Load the serialized content
+ (serdes.load/load-metabase! (ingestion-in-memory @serialized))
+
+ ;; Fetch the relevant bits
+ (reset! db1d (t2/select-one :model/Database :name "my-db"))
+ (reset! table1d (t2/select-one :model/Table :name "products"))
+ (reset! field1d (t2/select-one :model/Field :table_id (:id @table1d) :name "price"))
+ (reset! seg1d (t2/select-one :model/Segment :name "Expensive"))
+ (reset! msr1d (t2/select-one :model/Measure :name "Expensive Count"))
+
+ (testing "segment and measure were loaded"
+ (is (some? @seg1d))
+ (is (some? @msr1d)))
+
+ (testing "the measure's definition references the segment by new ID"
+ (is (=? {:lib/type :mbql/query
+ :stages [{:source-table (:id @table1d)
+ :aggregation [[:count-where {} [:segment {} (:id @seg1d)]]]}]
+ :database (:id @db1d)}
+ (:definition @msr1d))))))))))
+
#_{:clj-kondo/ignore [:metabase/i-like-making-cams-eyes-bleed-with-horrifically-long-tests]}
(deftest dashboard-card-test
;; DashboardCard.parameter_mappings and Card.parameter_mappings are JSON-encoded lists of parameter maps, which
diff --git a/src/metabase/graph/core.cljc b/src/metabase/graph/core.cljc
index a6de1d46c63..271c86e9a29 100644
--- a/src/metabase/graph/core.cljc
+++ b/src/metabase/graph/core.cljc
@@ -8,14 +8,13 @@
Each graph defines an arbitrary **key**. This can be any hash map key, such as a number, a `[type id]` pair, etc."
(:require
- #?@(:cljs ([flatland.ordered.set :as oset]))
- [metabase.util.malli.registry :as mr]
- [potemkin :as p])
+ [metabase.util :as u]
+ [metabase.util.malli.registry :as mr])
#?@(:clj ((:import java.util.LinkedHashSet))))
#?(:clj (set! *warn-on-reflection* true))
-(p/defprotocol+ Graph
+(defprotocol Graph
(children-of [graph key-seq]
"Given a graph and a seq of keys, returns a map from each input key to the set of keys **directly reachable** from
that key.
@@ -27,7 +26,7 @@
- If a key is found in the graph, and it has no children, its value is `#{}`.
- If the input `key-seq` is empty, return `{}`."))
-(p/deftype+ CachedGraph [graph cache]
+(deftype CachedGraph [graph cache]
Graph
(children-of [_this key-seq]
(let [new-cache (swap! cache (fn [current-cache]
@@ -48,10 +47,10 @@
{:insert-many! #(.addAll s %)
:seen? #(.contains s %)
:->iterable (constantly s)})
- :cljs (let [s (volatile! (oset/ordered-set))]
- {:insert-many! #(vswap! s into %)
- :seen? #(contains? @s %)
- :->iterable #(deref s)})))
+ :cljs (let [s (js/Set.)]
+ {:insert-many! #(doseq [x %] (.add s x))
+ :seen? #(.has s %)
+ :->iterable #(es6-iterator-seq (.values s))})))
(defn transitive
"Given a graph and `key-seq`, returns a seq of all transitive children of those starting keys.
@@ -102,7 +101,7 @@
#'graph?])
;; ## In-memory Graphs
-(p/deftype+ InMemoryGraph [adjacency-map]
+(deftype InMemoryGraph [adjacency-map]
Graph
(children-of [_this key-seq]
(select-keys adjacency-map key-seq)))
@@ -118,3 +117,32 @@
;; TODO: (Braden, 09/19/2025) We may find use for some extra helpers. For example, a wrapper that implements the
;; multi-key [[children-of]] on top of a single-key `(children graph key) => #{keys...}` function.
;; But YAGNI, so since I don't have a use-case in mind right now, I'm just leaving a note.
+
+(defn find-cycle
+ "Given a graph and starting keys, returns a cycle path if one is reachable, or nil if no cycle exists.
+
+ The returned cycle path is a vector of keys representing the cycle, starting and ending with the same key.
+ For example, `[1 2 3 1]` means 1 -> 2 -> 3 -> 1.
+
+ The cycle path excludes any prefix that leads to the cycle but is not part of it."
+ [graph key-seq]
+ (let [visited (volatile! #{})
+ dfs (fn dfs [node path path-set]
+ (cond
+ (contains? path-set node)
+ ;; Found a cycle - trim the path to start from the cycle node
+ (let [cycle-start-idx (u/index-of #{node} path)]
+ (conj (subvec path cycle-start-idx) node))
+
+ (contains? @visited node)
+ nil
+
+ :else
+ (let [children (get (children-of graph [node]) node #{})
+ path' (conj path node)
+ path-set' (conj path-set node)
+ result (some #(dfs % path' path-set') children)]
+ (when-not result
+ (vswap! visited conj node))
+ result)))]
+ (some #(dfs % [] #{}) key-seq)))
diff --git a/src/metabase/lib/js.cljs b/src/metabase/lib/js.cljs
index 520ddbaee2c..941a273c095 100644
--- a/src/metabase/lib/js.cljs
+++ b/src/metabase/lib/js.cljs
@@ -595,13 +595,6 @@
;; will also remove anything that depended on it, recursively. Moving or replacing a clause will update any references
;; to it in other places (eg. an aggregation based on a custom expression that was just renamed).
-(defn- ->clj
- "Convert JS objects to CLJS, but pass through CLJS data structures unchanged."
- [x]
- (if (or (map? x) (vector? x))
- x
- (js->clj x :keywordize-keys true)))
-
(defn ^:export remove-clause
"Removes the `target-clause` from the given stage of `a-query`.
@@ -616,7 +609,7 @@
[a-query stage-number clause]
(lib.core/remove-clause
a-query stage-number
- (lib.core/normalize (->clj clause))))
+ (lib.core/normalize (js->clj clause :keywordize-keys true))))
(defn ^:export replace-clause
"Replaces the `target-clause` with `new-clause` in the `query` stage.
@@ -627,8 +620,8 @@
[a-query stage-number target-clause new-clause]
(lib.core/replace-clause
a-query stage-number
- (lib.core/normalize (->clj target-clause))
- (lib.core/normalize (->clj new-clause))))
+ (lib.core/normalize (js->clj target-clause :keywordize-keys true))
+ (lib.core/normalize (js->clj new-clause :keywordize-keys true))))
(defn ^:export swap-clauses
"Exchanges the positions of two clauses of the same kind. Can be used for filters, aggregations, breakouts, and
@@ -641,8 +634,8 @@
[a-query stage-number source-clause target-clause]
(lib.core/swap-clauses
a-query stage-number
- (lib.core/normalize (->clj source-clause))
- (lib.core/normalize (->clj target-clause))))
+ (lib.core/normalize (js->clj source-clause :keywordize-keys true))
+ (lib.core/normalize (js->clj target-clause :keywordize-keys true))))
(defn- -unwrap
"Sometimes JS queries are passed in with a `Join` or `Aggregation` clause object instead of a simple Array.
@@ -867,7 +860,7 @@
> **Code health:** Healthy"
[a-query stage-number an-aggregate-clause]
- (lib.core/aggregate a-query stage-number (->clj an-aggregate-clause)))
+ (lib.core/aggregate a-query stage-number (js->clj an-aggregate-clause :keywordize-keys true)))
(defn ^:export aggregations
"Return a JS array of aggregations on a given stage of `a-query`.
@@ -975,7 +968,7 @@
(defn ^:export filter
"Adds `a-filter-clause` as a filter on `a-query`."
[a-query stage-number a-filter-clause]
- (lib.core/filter a-query stage-number (->clj a-filter-clause)))
+ (lib.core/filter a-query stage-number (js->clj a-filter-clause :keywordize-keys true)))
(defn ^:export filters
"Returns a JS array of all the filters on stage `stage-number` of `a-query`.
diff --git a/src/metabase/lib/measure.cljc b/src/metabase/lib/measure.cljc
index 685de7e54bb..e3b023459e1 100644
--- a/src/metabase/lib/measure.cljc
+++ b/src/metabase/lib/measure.cljc
@@ -1,12 +1,13 @@
(ns metabase.lib.measure
"A Measure is a saved MBQL query stage snippet with `:aggregation`. Measures are numeric expressions."
- (:refer-clojure :exclude [mapv empty? some])
+ (:refer-clojure :exclude [not-empty])
(:require
[clojure.string :as str]
+ [metabase.graph.core :as graph]
[metabase.lib.aggregation :as lib.aggregation]
+ [metabase.lib.join :as lib.join]
[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.options :as lib.options]
[metabase.lib.query :as lib.query]
@@ -17,10 +18,9 @@
[metabase.lib.schema.metadata :as lib.schema.metadata]
[metabase.lib.util :as lib.util]
[metabase.lib.walk.util :as lib.walk.util]
- [metabase.util :as u]
[metabase.util.i18n :as i18n]
[metabase.util.malli :as mu]
- [metabase.util.performance :refer [mapv empty? some]]))
+ [metabase.util.performance :refer [not-empty]]))
(defn- resolve-measure [query measure-id]
(when (integer? measure-id)
@@ -113,60 +113,62 @@
:display-name (fallback-display-name)}))
(mu/defn available-measures :- [:maybe [:sequential {:min 1} ::lib.schema.metadata/measure]]
- "Get a list of Measures that you may consider using as aggregation for a query. Only Measures that have the same
- `table-id` as the `source-table` for this query will be suggested."
+ "Get a list of Measures usable as aggregation in `query`. Only Measures with the same
+ `table-id` as the `source-table` of `query` are returned. Measures for joined tables are NOT returned."
([query]
(available-measures query -1))
([query :- ::lib.schema/query
stage-number :- :int]
(when (zero? (lib.util/canonical-stage-index query stage-number))
(when-let [source-table-id (lib.util/source-table-id query)]
- (let [measures (lib.metadata.protocols/measures (lib.metadata/->metadata-provider query) source-table-id)
- measure-aggregations (into {}
- (keep-indexed (fn [index aggregation-clause]
- (when (lib.util/clause-of-type? aggregation-clause :measure)
- [(get aggregation-clause 2) index])))
- (lib.aggregation/aggregations query 0))]
- (cond
- (empty? measures) nil
- (empty? measure-aggregations) (vec measures)
- :else (mapv (fn [measure-metadata]
- (let [aggregation-pos (-> measure-metadata :id measure-aggregations)]
- (cond-> measure-metadata
- aggregation-pos (assoc :aggregation-positions [aggregation-pos]))))
- measures)))))))
+ (let [measures (lib.metadata/metadatas-for-table query :metadata/measure source-table-id)
+ measure-aggregations (transduce
+ (map-indexed vector)
+ (completing (fn [acc [index clause]]
+ (if (lib.util/clause-of-type? clause :measure)
+ (let [k [(get clause 2)
+ (:join-alias (lib.options/options clause))]]
+ (update acc k (fnil conj []) index))
+ acc)))
+ {}
+ (lib.aggregation/aggregations query 0))]
+ (not-empty
+ (into []
+ (map (fn [measure-metadata]
+ (let [positions (-> measure-metadata
+ ((juxt :id ::lib.join/join-alias))
+ measure-aggregations)]
+ (cond-> measure-metadata
+ positions (assoc :aggregation-positions positions)))))
+ measures)))))))
+
+(defn- measure-graph
+ "Create a Graph for measure dependency traversal.
+ Uses `initial-definition` for `initial-measure-id`, and looks up other measures from `metadata-provider`."
+ [metadata-provider initial-definition initial-measure-id]
+ (reify graph/Graph
+ (children-of [_this measure-ids]
+ (reduce (fn [acc measure-id]
+ (let [definition (if (= measure-id initial-measure-id)
+ initial-definition
+ (:definition (lib.metadata/measure metadata-provider measure-id)))]
+ (if definition
+ (assoc acc measure-id (set (lib.walk.util/all-measure-ids definition)))
+ (throw (ex-info (i18n/tru "Measure {0} does not exist." measure-id)
+ {:measure-id measure-id})))))
+ {}
+ measure-ids))))
(defn check-measure-cycles
- "DFS to detect cycles starting from `measure-id` with `definition`.
- `metadata-provider` is used for looking up referenced measures.
- `path` is a vector of measure IDs representing the current DFS path (for cycle detection and reporting).
- `visited` is the set of measure IDs we've fully processed (for avoiding redundant work).
- Returns updated `visited` set. Throws if a cycle is detected or if a referenced measure doesn't exist."
- ([metadata-provider definition measure-id]
- (check-measure-cycles metadata-provider definition measure-id [] #{}))
- ([metadata-provider definition measure-id path visited]
- (cond
- (some #{measure-id} path)
- (let [cycle-start-idx (u/index-of #{measure-id} path)
- cycle-path (conj (subvec path cycle-start-idx) measure-id)]
- (throw (ex-info (i18n/tru "Measure cycle detected: {0}" (str/join " → " cycle-path))
- {:measure-id measure-id
- :cycle-path cycle-path
- :path path})))
-
- (contains? visited measure-id)
- visited
-
- :else
- (let [referenced-ids (lib.walk.util/all-measure-ids definition)
- path' (conj path measure-id)]
- (reduce (fn [visited' ref-id]
- (if-let [measure (lib.metadata/measure metadata-provider ref-id)]
- (check-measure-cycles metadata-provider (:definition measure) ref-id path' visited')
- (throw (ex-info (i18n/tru "Measure {0} does not exist." ref-id)
- {:measure-id ref-id}))))
- (conj visited measure-id)
- referenced-ids)))))
+ "Check for cycles in measure dependencies starting from `measure-id` with `definition`.
+ `definition` also serves as the metadata provider for looking up referenced measures.
+ Throws if a cycle is detected or if a referenced measure doesn't exist."
+ [metadata-provider definition measure-id]
+ (when-let [cycle-path (graph/find-cycle (measure-graph metadata-provider definition measure-id)
+ [measure-id])]
+ (throw (ex-info (i18n/tru "Measure cycle detected: {0}" (str/join " → " cycle-path))
+ {:measure-id measure-id
+ :cycle-path cycle-path}))))
(mu/defn check-measure-overwrite
"Check if saving a measure with `measure-id` and `definition` would create a cycle.
diff --git a/src/metabase/lib/segment.cljc b/src/metabase/lib/segment.cljc
index 24ef166af8f..f0e7415d9c3 100644
--- a/src/metabase/lib/segment.cljc
+++ b/src/metabase/lib/segment.cljc
@@ -2,6 +2,8 @@
"A Segment is a saved MBQL query stage snippet with `:filter`. Segments are always boolean expressions."
(:refer-clojure :exclude [mapv empty?])
(:require
+ [clojure.string :as str]
+ [metabase.graph.core :as graph]
[metabase.lib.filter :as lib.filter]
[metabase.lib.metadata :as lib.metadata]
[metabase.lib.metadata.calculation :as lib.metadata.calculation]
@@ -92,33 +94,33 @@
filter-pos (assoc :filter-positions [filter-pos]))))
segments)))))))
-(defn- check-segment-cycles
- "DFS to detect cycles starting from `segment-id` with `definition`.
- `metadata-provider` is used for looking up referenced segments.
- `path-nodes` is the set of segment IDs currently in the DFS path (for cycle detection).
- `visited` is the set of segment IDs we've fully processed (for avoiding redundant work).
- Returns updated `visited` set. Throws if a cycle is detected or if a referenced segment doesn't exist."
- ([metadata-provider definition segment-id]
- (check-segment-cycles metadata-provider definition segment-id #{} #{}))
- ([metadata-provider definition segment-id path-nodes visited]
- (cond
- (contains? path-nodes segment-id)
- (throw (ex-info (i18n/tru "Segment cycle detected.")
- {:segment-id segment-id}))
-
- (contains? visited segment-id)
- visited
+(defn- segment-graph
+ "Create a Graph for segment dependency traversal.
+ Uses `initial-definition` for `initial-segment-id`, and looks up other segments from `metadata-provider`."
+ [metadata-provider initial-definition initial-segment-id]
+ (reify graph/Graph
+ (children-of [_this segment-ids]
+ (reduce (fn [acc segment-id]
+ (let [definition (if (= segment-id initial-segment-id)
+ initial-definition
+ (:definition (lib.metadata/segment metadata-provider segment-id)))]
+ (if definition
+ (assoc acc segment-id (set (lib.walk.util/all-segment-ids definition)))
+ (throw (ex-info (i18n/tru "Segment {0} does not exist." segment-id)
+ {:segment-id segment-id})))))
+ {}
+ segment-ids))))
- :else
- (let [referenced-ids (lib.walk.util/all-segment-ids definition)
- path-nodes' (conj path-nodes segment-id)]
- (reduce (fn [visited' ref-id]
- (if-let [segment (lib.metadata/segment metadata-provider ref-id)]
- (check-segment-cycles metadata-provider (:definition segment) ref-id path-nodes' visited')
- (throw (ex-info (i18n/tru "Segment {0} does not exist." ref-id)
- {:segment-id ref-id}))))
- (conj visited segment-id)
- referenced-ids)))))
+(defn- check-segment-cycles
+ "Check for cycles in segment dependencies starting from `segment-id` with `definition`.
+ `definition` also serves as the metadata provider for looking up referenced segments.
+ Throws if a cycle is detected or if a referenced segment doesn't exist."
+ [metadata-provider definition segment-id]
+ (when-let [cycle-path (graph/find-cycle (segment-graph metadata-provider definition segment-id)
+ [segment-id])]
+ (throw (ex-info (i18n/tru "Segment cycle detected: {0}" (str/join " → " cycle-path))
+ {:segment-id segment-id
+ :cycle-path cycle-path}))))
(mu/defn check-segment-overwrite
"Check if saving a segment with `segment-id` and `definition` would create a cycle.
diff --git a/src/metabase/measures/api.clj b/src/metabase/measures/api.clj
index 25329647232..7c7eda2549e 100644
--- a/src/metabase/measures/api.clj
+++ b/src/metabase/measures/api.clj
@@ -4,8 +4,7 @@
[metabase.api.common :as api]
[metabase.api.macros :as api.macros]
[metabase.events.core :as events]
- ^{:clj-kondo/ignore [:discouraged-namespace]} [metabase.legacy-mbql.normalize :as mbql.normalize]
- ^{:clj-kondo/ignore [:discouraged-namespace]} [metabase.legacy-mbql.schema :as mbql.s]
+ [metabase.lib-be.core :as lib-be]
[metabase.lib.core :as lib]
[metabase.models.interface :as mi]
[metabase.util :as u]
@@ -29,6 +28,24 @@
[:entity_id {:optional true} [:maybe :string]]
[:creator {:optional true} [:maybe :map]]])
+(defn- normalize-input-definition
+ "Normalize measure definition from API input to MBQL5.
+ Accepts MBQL4 definitions for Cypress e2e test support:
+ - MBQL5 full queries (passed through)
+ - MBQL4 full queries (converted to MBQL5)
+ - MBQL4 fragments (wrapped in full query, then converted to MBQL5)"
+ [definition table-id database-id]
+ (if (seq definition)
+ (-> (case (lib/normalized-mbql-version definition)
+ (:mbql-version/mbql5 :mbql-version/legacy)
+ definition
+ ;; default: MBQL4 fragment - wrap it in a full query
+ {:database database-id
+ :type :query
+ :query (merge {:source-table table-id} definition)})
+ lib-be/normalize-query)
+ {}))
+
(api.macros/defendpoint :post "/" :- ::measure
"Create a new `Measure`."
[_route-params
@@ -39,13 +56,15 @@
[:definition ms/Map]
[:description {:optional true} [:maybe :string]]]]
(api/create-check :model/Measure body)
- (let [measure (api/check-500
+ (let [database-id (t2/select-one-fn :db_id :model/Table :id table_id)
+ normalized-definition (normalize-input-definition definition table_id database-id)
+ measure (api/check-500
(first (t2/insert-returning-instances! :model/Measure
:table_id table_id
:creator_id api/*current-user-id*
:name name
:description description
- :definition definition)))]
+ :definition normalized-definition)))]
(events/publish-event! :event/measure-create {:object measure :user-id api/*current-user-id*})
(t2/hydrate measure :creator)))
@@ -75,9 +94,9 @@
:present #{:description}
:non-nil #{:archived :definition :name})
new-def (when-let [def (:definition clean-body)]
- (cond->> def
- (not= :mbql-version/mbql5 (lib/normalized-mbql-version def))
- (mbql.normalize/normalize ::mbql.s/MBQLQuery)))
+ (let [table-id (:table_id existing)
+ database-id (t2/select-one-fn :db_id :model/Table :id table-id)]
+ (normalize-input-definition def table-id database-id)))
new-body (merge
(dissoc clean-body :revision_message)
(when new-def {:definition new-def}))
diff --git a/src/metabase/measures/models/measure.clj b/src/metabase/measures/models/measure.clj
index dbc0b0ac89f..9f96b811562 100644
--- a/src/metabase/measures/models/measure.clj
+++ b/src/metabase/measures/models/measure.clj
@@ -25,34 +25,18 @@
(methodical/defmethod t2/model-for-automagic-hydration [:default :measure] [_original-model _k] :model/Measure)
(defn- validate-mbql5-definition
- "Validate that an MBQL 5 measure definition has the correct structure."
+ "Validate that a measure definition is in MBQL5 format.
+ Throws an exception with 'Invalid measure definition' if the definition is not valid MBQL5."
[definition]
(when (seq definition)
- (mu/validate-throw ::lib.schema.measure/definition definition)
- definition))
-
-(defn- normalize-measure-definition
- "Normalize measure definition.
- Accepts:
- - MBQL 5 full queries (passed through)
- - MBQL 4 full queries (from serialization - converted to MBQL 5)
- Empty seqs are normalized to `{}`."
- [definition table-id database-id]
- (if (seq definition)
- (u/prog1 (-> (case (lib/normalized-mbql-version definition)
- (:mbql-version/mbql5 :mbql-version/legacy)
- definition
- ;; default MBQL4 fragment - wrap it in a full query
- {:database database-id
- :type :query
- :query (merge {:source-table table-id} definition)})
- lib-be/normalize-query)
- (validate-mbql5-definition <>))
- {}))
+ (when-not (= :mbql-version/mbql5 (lib/normalized-mbql-version definition))
+ (throw (ex-info (tru "Invalid measure definition: expected MBQL5 format")
+ {:definition definition})))
+ (mu/validate-throw ::lib.schema.measure/definition definition)))
(def ^:private transform-measure-definition
- "Transform for measure definitions. Only handles JSON serialization/deserialization.
- Normalization and validation happen in before-insert and after-select hooks."
+ "Transform for measure definitions. Handles JSON serialization/deserialization.
+ Validation happens in before-insert and before-update hooks."
{:in mi/json-in
:out mi/json-out-with-keywordization})
@@ -78,27 +62,21 @@
([model pk]
(mi/can-read? (t2/select-one model pk))))
-(defn- migrated-measure-definition
- [{:keys [definition], table-id :table_id}]
- (let [database-id (t2/select-one-fn :db_id :model/Table :id table-id)]
- (normalize-measure-definition definition table-id database-id)))
-
(t2/define-before-insert :model/Measure
[{:keys [definition] :as measure}]
- (let [measure (cond-> measure
- (some? definition) (assoc :definition (migrated-measure-definition measure)))]
- (when (seq (:definition measure))
- (lib/check-measure-overwrite nil (:definition measure)))
- measure))
+ (validate-mbql5-definition definition)
+ (when (seq definition)
+ (lib/check-measure-overwrite nil definition))
+ measure)
(t2/define-before-update :model/Measure [{:keys [id] :as measure}]
;; throw an Exception if someone tries to update creator_id
(when (contains? (t2/changes measure) :creator_id)
(throw (UnsupportedOperationException. (tru "You cannot update the creator_id of a Measure."))))
- ;; check for cycles if definition is being updated
+ ;; validate and check for cycles if definition is being updated
(when-let [def-change (:definition (t2/changes measure))]
- (let [normalized-def (migrated-measure-definition (assoc measure :definition def-change))]
- (lib/check-measure-overwrite id normalized-def)))
+ (validate-mbql5-definition def-change)
+ (lib/check-measure-overwrite id def-change))
measure)
(defmethod mi/perms-objects-set :model/Measure
@@ -107,18 +85,21 @@
(t2/select-one ['Table :db_id :schema :id] :id (u/the-id (:table_id measure))))]
(mi/perms-objects-set table read-or-write)))
-(defn- maybe-migrated-measure-definition
- [measure]
- (try
- (migrated-measure-definition measure)
- (catch Throwable e
- (log/error e "Error upgrading measure definition:" (ex-message e))
- nil)))
+(defn- normalize-definition-from-db
+ "Normalize a measure definition read from the database.
+ This handles keyword normalization after JSON round-trip (e.g., string \"mbql/query\" -> keyword :mbql/query)."
+ [{:keys [definition] :as measure}]
+ (if (seq definition)
+ (try
+ (assoc measure :definition (lib-be/normalize-query definition))
+ (catch Throwable e
+ (log/error e "Error normalizing measure definition:" (ex-message e))
+ measure))
+ measure))
(t2/define-after-select :model/Measure
- [{:keys [definition] :as measure}]
- (cond-> measure
- (some? definition) (assoc :definition (maybe-migrated-measure-definition measure))))
+ [measure]
+ (normalize-definition-from-db measure))
(mu/defn- definition-description :- [:maybe ::lib.schema.common/non-blank-string]
"Calculate a nice description of a Measure's definition."
@@ -153,13 +134,21 @@
serdes/storage-path-prefixes
(concat ["measures" (serdes/storage-leaf-file-name id label)]))))
+(defn- import-measure-definition
+ "Import a measure definition from serialization format.
+ Converts portable IDs back to numeric IDs, then converts MBQL4 to MBQL5."
+ [exported]
+ (let [with-ids (serdes/import-mbql exported)]
+ (when (seq with-ids)
+ (lib-be/normalize-query with-ids))))
+
(defmethod serdes/make-spec "Measure" [_model-name _opts]
{:copy [:name :archived :description :entity_id]
:skip [:dependency_analysis_version]
:transform {:created_at (serdes/date)
:table_id (serdes/fk :model/Table)
:creator_id (serdes/fk :model/User)
- :definition {:export serdes/export-mbql :import serdes/import-mbql}}})
+ :definition {:export serdes/export-mbql :import import-measure-definition}}})
;;;; ------------------------------------------------- Search ----------------------------------------------------------
diff --git a/src/metabase/models/serialization.clj b/src/metabase/models/serialization.clj
index 7f878aa5ccf..be287cf0523 100644
--- a/src/metabase/models/serialization.clj
+++ b/src/metabase/models/serialization.clj
@@ -1109,7 +1109,7 @@
(defn- mbql-entity-reference?
"Is given form an MBQL entity reference?"
[form]
- (mbql.normalize/is-clause? #{:field :field-id :fk-> :dimension :metric :segment} form))
+ (mbql.normalize/is-clause? #{:field :field-id :fk-> :dimension :metric :segment :measure} form))
(defn- normalize [mbql]
(if-not (mbql-entity-reference? mbql)
@@ -1149,7 +1149,10 @@
[:metric (*export-fk* id 'Card)]
[:segment (id :guard integer?)]
- [:segment (*export-fk* id 'Segment)])))
+ [:segment (*export-fk* id 'Segment)]
+
+ [:measure (id :guard integer?)]
+ [:measure (*export-fk* id 'Measure)])))
(defn- export-source-table
[source-table]
@@ -1242,6 +1245,9 @@
[(:or :segment "segment") (fully-qualified-name :guard portable-id?)]
[:segment (*import-fk* fully-qualified-name 'Segment)]
+ [(:or :measure "measure") (fully-qualified-name :guard portable-id?)]
+ [:measure (*import-fk* fully-qualified-name 'Measure)]
+
(_ :guard (every-pred map? #(vector? (:source-table %))))
(-> &match
(assoc :source-table (*import-table-fk* (:source-table &match)))
@@ -1292,6 +1298,8 @@
["metric" (field :guard portable-id?)] #{[{:model "Card" :id field}]}
[:segment (field :guard portable-id?)] #{[{:model "Segment" :id field}]}
["segment" (field :guard portable-id?)] #{[{:model "Segment" :id field}]}
+ [:measure (field :guard portable-id?)] #{[{:model "Measure" :id field}]}
+ ["measure" (field :guard portable-id?)] #{[{:model "Measure" :id field}]}
:else (reduce #(cond
(map? %2) (into %1 (mbql-deps-map %2))
(vector? %2) (into %1 (mbql-deps-vector %2))
diff --git a/src/metabase/query_processor/error_type.clj b/src/metabase/query_processor/error_type.clj
index ba2e4e4192c..8c5a48ce1f9 100644
--- a/src/metabase/query_processor/error_type.clj
+++ b/src/metabase/query_processor/error_type.clj
@@ -83,6 +83,17 @@
:parent invalid-query
:show-in-embeds? true)
+(deferror missing-measure
+ "The query references a measure that does not exist or belongs to a different database."
+ :parent invalid-query
+ :show-in-embeds? true)
+
+(deferror invalid-measure
+ "The query references a measure that has an invalid definition (e.g., no aggregation, contains metric references,
+ has cycles, etc.)."
+ :parent invalid-query
+ :show-in-embeds? true)
+
(deferror dangling-lhs-ref-in-join-condition
"The query contains a join condition which refers to a field on the LHS of the join which is no longer available."
:parent invalid-query
diff --git a/src/metabase/query_processor/middleware/measures.clj b/src/metabase/query_processor/middleware/measures.clj
index 9c98ae9468c..f73e97e4008 100644
--- a/src/metabase/query_processor/middleware/measures.clj
+++ b/src/metabase/query_processor/middleware/measures.clj
@@ -6,11 +6,13 @@
Measures can reference other measures, so expansion is recursive. Cycles are detected
by tracking the path of measure IDs being expanded."
- (:refer-clojure :exclude [not-empty some])
+ (:refer-clojure :exclude [select-keys some])
(:require
+ [better-cond.core :as b]
[clojure.set :as set]
[metabase.lib.core :as lib]
[metabase.lib.metadata :as lib.metadata]
+ [metabase.lib.options :as lib.options]
[metabase.lib.schema :as lib.schema]
[metabase.lib.util :as lib.util]
[metabase.lib.util.match :as lib.util.match]
@@ -20,20 +22,7 @@
[metabase.util.i18n :refer [tru]]
[metabase.util.log :as log]
[metabase.util.malli :as mu]
- [metabase.util.performance :refer [not-empty some]]))
-
-(mu/defn- unresolved-measure-ids :- [:maybe [:set {:min 1} pos-int?]]
- "Find all the unresolved :measure references in `query`."
- [query :- ::lib.schema/query]
- (let [ids (transient #{})]
- (lib.walk/walk-stages
- query
- (fn [_query _path stage]
- (lib.util.match/match stage
- [:measure _opts (id :guard pos-int?)]
- (conj! ids id))
- nil))
- (not-empty (persistent! ids))))
+ [metabase.util.performance :refer [select-keys some]]))
(defn- contains-metric-reference?
"Check if the given query or clause contains any `:metric` references.
@@ -61,7 +50,7 @@
(when (contains-metric-reference? definition)
(throw (ex-info (tru "Measures cannot reference metrics. Measure \"{0}\" (ID {1}) contains a metric reference."
name id)
- {:type qp.error-type/invalid-query
+ {:type qp.error-type/invalid-measure
:measure-id id
:measure-name name}))))
@@ -77,7 +66,7 @@
(check-no-metric-references! measure)
(throw (ex-info (tru "Measure {0} does not exist or belongs to a different Database."
id)
- {:type qp.error-type/invalid-query, :id id}))))))
+ {:type qp.error-type/missing-measure, :id id}))))))
(defn- measure-aggregation
"Extract the aggregation clause from a measure's definition.
@@ -92,19 +81,18 @@
id->measure :- [:map-of pos-int? :map]]
(lib.util.match/replace stage
[:measure opts (id :guard pos-int?)]
- (if-let [measure (get id->measure id)]
- (if-let [aggregation (measure-aggregation measure)]
- (do
- (log/debugf "Expanding measure %d:\n%s\n->\n%s" id (u/pprint-to-str &match) (u/pprint-to-str aggregation))
- ;; Preserve :lib/uuid and :display-name from the measure clause options if present
- ;; This is important so that :aggregation refs pointing to the measure remain valid
- (cond-> aggregation
- (:lib/uuid opts) (assoc-in [1 :lib/uuid] (:lib/uuid opts))
- (:display-name opts) (assoc-in [1 :display-name] (:display-name opts))))
- (throw (ex-info (tru "Measure {0} has no aggregation defined." id)
- {:type qp.error-type/invalid-query, :measure measure})))
- (throw (ex-info (tru "Measure {0} not found." id)
- {:type qp.error-type/invalid-query, :id id})))))
+ (b/cond
+ :let [measure (get id->measure id)]
+ (not measure) (throw (ex-info (tru "Measure {0} not found." id)
+ {:type qp.error-type/missing-measure, :id id}))
+ :let [aggregation (measure-aggregation measure)]
+ (not aggregation) (throw (ex-info (tru "Measure {0} has no aggregation defined." id)
+ {:type qp.error-type/invalid-measure, :measure measure}))
+ :else (do
+ (log/debugf "Expanding measure %d:\n%s\n->\n%s" id (u/pprint-to-str &match) (u/pprint-to-str aggregation))
+ ;; Preserve :lib/uuid and :display-name from the measure clause options if present
+ ;; This is important so that :aggregation refs pointing to the measure remain valid
+ (lib.options/update-options aggregation merge (select-keys opts [:lib/uuid :display-name]))))))
(mu/defn- expand-measures-once :- ::lib.schema/query
"Expand all :measure clauses in the query (single pass)."
@@ -132,9 +120,9 @@
depth :- :int]
(when (> depth max-expansion-depth)
(throw (ex-info (tru "Measure expansion exceeded maximum depth of {0}." max-expansion-depth)
- {:type qp.error-type/invalid-query
+ {:type qp.error-type/invalid-measure
:seen-ids seen-ids})))
- (if-let [measure-ids (unresolved-measure-ids query)]
+ (if-let [measure-ids (lib/all-measure-ids query)]
;; Check for cycles: if any measure ID is already in seen-ids, we have a cycle
(if-let [cycle-id (some seen-ids measure-ids)]
;; Found a cycle - use lib/check-measure-cycles to get a detailed error with the cycle path
diff --git a/test/metabase/graph/core_test.cljc b/test/metabase/graph/core_test.cljc
new file mode 100644
index 00000000000..294930031e3
--- /dev/null
+++ b/test/metabase/graph/core_test.cljc
@@ -0,0 +1,55 @@
+(ns metabase.graph.core-test
+ (:require
+ [clojure.test :refer [deftest is testing]]
+ [metabase.graph.core :as graph]))
+
+(deftest ^:parallel find-cycle-test
+ (testing "no cycle in simple graph"
+ (let [g (graph/in-memory {1 #{2}
+ 2 #{3}
+ 3 #{}})]
+ (is (nil? (graph/find-cycle g [1])))))
+
+ (testing "no cycle with multiple paths to same node"
+ (let [g (graph/in-memory {1 #{2 3}
+ 2 #{4}
+ 3 #{4}
+ 4 #{}})]
+ (is (nil? (graph/find-cycle g [1])))))
+
+ (testing "self-loop is a cycle"
+ (let [g (graph/in-memory {1 #{1}})]
+ (is (= [1 1] (graph/find-cycle g [1])))))
+
+ (testing "direct cycle between two nodes"
+ (let [g (graph/in-memory {1 #{2}
+ 2 #{1}})]
+ (is (= [1 2 1] (graph/find-cycle g [1])))))
+
+ (testing "longer cycle"
+ (let [g (graph/in-memory {1 #{2}
+ 2 #{3}
+ 3 #{1}})]
+ (is (= [1 2 3 1] (graph/find-cycle g [1])))))
+
+ (testing "cycle not reachable from start"
+ (let [g (graph/in-memory {1 #{2}
+ 2 #{}
+ 3 #{4}
+ 4 #{3}})]
+ ;; Starting from 1, we can't reach the 3->4->3 cycle
+ (is (nil? (graph/find-cycle g [1])))))
+
+ (testing "multiple start nodes - finds cycle from any"
+ (let [g (graph/in-memory {1 #{}
+ 2 #{3}
+ 3 #{2}})]
+ (is (= [2 3 2] (graph/find-cycle g [1 2])))))
+
+ (testing "cycle path excludes prefix before cycle"
+ ;; Graph: 1 -> 2 -> 3 -> 2 (cycle is 2 -> 3 -> 2, prefix is 1)
+ (let [g (graph/in-memory {1 #{2}
+ 2 #{3}
+ 3 #{2}})]
+ ;; The cycle-path should be [2 3 2], not [1 2 3 2]
+ (is (= [2 3 2] (graph/find-cycle g [1]))))))
diff --git a/test/metabase/lib/measure_test.cljc b/test/metabase/lib/measure_test.cljc
index dd253896b8b..4e13dedd8d1 100644
--- a/test/metabase/lib/measure_test.cljc
+++ b/test/metabase/lib/measure_test.cljc
@@ -1,10 +1,14 @@
(ns metabase.lib.measure-test
(:require
+ #?@(:cljs ([metabase.test-runner.assert-exprs.approximately-equal]))
[clojure.test :refer [deftest is testing]]
[metabase.lib.core :as lib]
+ [metabase.lib.measure :as lib.measure]
[metabase.lib.test-metadata :as meta]
[metabase.lib.test-util :as lib.tu]))
+#?(:cljs (comment metabase.test-runner.assert-exprs.approximately-equal/keep-me))
+
(defn- measure-definition-with-aggregation
"Create an MBQL5 measure definition with the given aggregation clause and metadata provider."
[mp aggregation-clause]
@@ -39,12 +43,12 @@
(is (nil? (lib/check-measure-overwrite 1 measure-1-def))))))
(deftest ^:parallel check-measure-overwrite-self-reference-not-in-mp-test
- (testing "Measure referencing itself - should throw (reported as does not exist since measure being saved isn't in mp)"
- (let [mp (lib.tu/mock-metadata-provider meta/metadata-provider {})
+ (testing "Measure referencing itself - should throw cycle (even if measure is not in mp)"
+ (let [mp meta/metadata-provider
measure-1-def (measure-definition-referencing mp 1)]
(is (thrown-with-msg?
#?(:clj Exception :cljs js/Error)
- #"does not exist"
+ #"[Cc]ycle"
(lib/check-measure-overwrite 1 measure-1-def))))))
(deftest ^:parallel check-measure-overwrite-self-reference-in-mp-test
@@ -213,3 +217,94 @@
query (lib/query mp (meta/table-metadata :venues))]
(is (= :type/*
(lib/type-of query [:measure {} 999]))))))
+
+(def ^:private measure-id 100)
+
+(def ^:private measure-definition
+ (-> (lib/query meta/metadata-provider (meta/table-metadata :venues))
+ (lib/aggregate (lib/sum (meta/field-metadata :venues :price)))))
+
+(def ^:private measures-db
+ {:measures [{:id measure-id
+ :name "Sum of Prices"
+ :table-id (meta/id :venues)
+ :definition measure-definition
+ :description "Sum of all venue prices"}]})
+
+(def ^:private measure-metadata-provider
+ (lib.tu/mock-metadata-provider meta/metadata-provider measures-db))
+
+(def ^:private measure-clause
+ [:measure {:lib/uuid (str (random-uuid))} measure-id])
+
+(def ^:private query-with-measure
+ (-> (lib/query measure-metadata-provider (meta/table-metadata :venues))
+ (lib/aggregate measure-clause)))
+
+(deftest ^:parallel available-measures-test
+ (let [expected-measure-metadata {:lib/type :metadata/measure
+ :id measure-id
+ :name "Sum of Prices"
+ :table-id (meta/id :venues)
+ :definition measure-definition
+ :description "Sum of all venue prices"}]
+ (testing "Should return Measures with the same Table ID as query's `:source-table`"
+ (is (=? [expected-measure-metadata]
+ (lib.measure/available-measures (lib/query measure-metadata-provider (meta/table-metadata :venues))))))
+ (testing "Shouldn't return archived Measures"
+ (is (nil? (lib.measure/available-measures
+ (lib/query (lib.tu/mock-metadata-provider
+ meta/metadata-provider
+ (assoc-in measures-db [:measures 0 :archived] true))
+ (meta/table-metadata :venues))))))
+ (testing "Should return the positions in the list of aggregations"
+ (let [measures (lib.measure/available-measures query-with-measure)]
+ (is (=? [(assoc expected-measure-metadata :aggregation-positions [0])]
+ measures))
+ (testing "Display info should contain aggregation-positions"
+ (is (=? [{:name "sum_of_prices"
+ :display-name "Sum of Prices"
+ :long-display-name "Sum of Prices"
+ :description "Sum of all venue prices"
+ :aggregation-positions [0]}]
+ (map #(lib/display-info query-with-measure %)
+ measures))))))))
+
+(deftest ^:parallel available-measures-different-table-test
+ (testing "query with different Table -- don't return Measures"
+ (is (nil? (lib.measure/available-measures (lib/query measure-metadata-provider (meta/table-metadata :orders)))))))
+
+(deftest ^:parallel available-measures-subsequent-stages-test
+ (testing "for subsequent stages -- don't return Measures"
+ (let [query (lib/append-stage (lib/query measure-metadata-provider (meta/table-metadata :venues)))]
+ (is (nil? (lib.measure/available-measures query)))
+ (is (nil? (lib.measure/available-measures query 1)))
+ (is (nil? (lib.measure/available-measures query -1))))))
+
+(deftest ^:parallel available-measures-aggregation-positions-with-join-alias-test
+ (testing "aggregation-positions should use join-alias to distinguish measure applications"
+ (let [;; Add the same measure twice with different join-aliases
+ query (-> (lib/query measure-metadata-provider (meta/table-metadata :venues))
+ (lib/aggregate [:measure {:lib/uuid (str (random-uuid))} measure-id])
+ (lib/aggregate [:measure {:lib/uuid (str (random-uuid))
+ :join-alias "J1"} measure-id]))
+ measures (lib.measure/available-measures query)]
+ ;; The measure without join-alias should have aggregation-positions [0]
+ ;; The measure with join-alias "J1" is at position 1 but won't be returned
+ ;; since available-measures only returns measures for the source table (no join-alias)
+ (is (=? [{:id measure-id
+ :aggregation-positions [0]}]
+ measures)))))
+
+(deftest ^:parallel available-measures-multiple-aggregation-positions-test
+ (testing "aggregation-positions should contain all positions when same measure is used multiple times"
+ (let [;; Add the same measure twice without join-alias
+ query (-> (lib/query measure-metadata-provider (meta/table-metadata :venues))
+ (lib/aggregate [:measure {:lib/uuid (str (random-uuid))} measure-id])
+ (lib/aggregate (lib/count))
+ (lib/aggregate [:measure {:lib/uuid (str (random-uuid))} measure-id]))
+ measures (lib.measure/available-measures query)]
+ ;; The measure appears at positions 0 and 2
+ (is (=? [{:id measure-id
+ :aggregation-positions [0 2]}]
+ measures)))))
diff --git a/test/metabase/lib/segment_test.cljc b/test/metabase/lib/segment_test.cljc
index ef9423ba3e3..b1c2f466845 100644
--- a/test/metabase/lib/segment_test.cljc
+++ b/test/metabase/lib/segment_test.cljc
@@ -181,12 +181,12 @@
(is (nil? (lib/check-segment-overwrite 1 segment-1-def))))))
(deftest ^:parallel check-segment-overwrite-self-reference-not-in-mp-test
- (testing "Segment referencing itself - should throw (reported as does not exist since segment being saved isn't in mp)"
+ (testing "Segment referencing itself - should throw cycle (even if segment is not in mp)"
(let [mp (lib.tu/mock-metadata-provider meta/metadata-provider {})
segment-1-def (segment-definition-referencing mp 1)]
(is (thrown-with-msg?
#?(:clj Exception :cljs js/Error)
- #"does not exist"
+ #"[Cc]ycle"
(lib/check-segment-overwrite 1 segment-1-def))))))
(deftest ^:parallel check-segment-overwrite-self-reference-in-mp-test
diff --git a/test/metabase/measures/api_test.clj b/test/metabase/measures/api_test.clj
index aac5940ea76..273a76b3b4f 100644
--- a/test/metabase/measures/api_test.clj
+++ b/test/metabase/measures/api_test.clj
@@ -12,10 +12,9 @@
[metabase.util :as u]
[toucan2.core :as t2]))
-;; TODO (Tamas 2026-01-05): Remove this helper once FE tests switch to using MBQL5
(defn- legacy-mbql-measure-definition
- "Create an MBQL4 (legacy MBQL) measure definition. This format is used by Cypress e2e tests.
- Remove this helper when FE tests switch to MBQL5."
+ "Create an MBQL4 (legacy MBQL) full query measure definition.
+ This format is used by Cypress e2e tests - the API accepts it and converts to MBQL5."
[database-id table-id aggregation]
{:database database-id
:type :query
@@ -229,28 +228,81 @@
(contains? #{id-1 id-2 id-3} measure-id))
(mt/user-http-request :rasta :get 200 "measure/"))))))))
-(deftest cypress-e2e-workflow-measure-in-expression-test
- ;; TODO (Tamas 2026-01-05): Remove this test once FE tests switch to using MBQL5
- (testing "Cypress workflow: create measure via API, then use in expression with legacy MBQL4"
+;;; ------------------------------------------------ MBQL4 API Support Tests ------------------------------------------------
+;;; The API layer accepts MBQL4 definitions (for Cypress e2e test support) and converts them to MBQL5.
+;;; TODO (Tamas 2026-01-09): Remove these tests once FE tests switch to using MBQL5.
+
+(defn- mbql4-fragment-definition
+ "Create an MBQL4 fragment definition (no :database or :type keys)."
+ [table-id aggregation]
+ {:source-table table-id
+ :aggregation aggregation})
+
+(defn- mbql5-definition?
+ "Check if a definition is in MBQL5 format (has :lib/type key)."
+ [definition]
+ (contains? definition :lib/type))
+
+(deftest api-accepts-mbql4-on-post-test
+ (testing "POST /api/measure accepts MBQL4 definitions and returns MBQL5"
+ (mt/with-model-cleanup [:model/Measure]
+ (testing "MBQL4 fragment format"
+ (let [response (mt/user-http-request
+ :crowberto :post 200 "measure"
+ {:name "Fragment Measure"
+ :table_id (mt/id :venues)
+ :definition (mbql4-fragment-definition (mt/id :venues) [[:count]])})]
+ (is (some? (:id response)))
+ (is (mbql5-definition? (:definition response))
+ "Returned definition should be MBQL5")))
+ (testing "MBQL4 full query format"
+ (let [response (mt/user-http-request
+ :crowberto :post 200 "measure"
+ {:name "Full Query Measure"
+ :table_id (mt/id :venues)
+ :definition (legacy-mbql-measure-definition (mt/id) (mt/id :venues) [[:count]])})]
+ (is (some? (:id response)))
+ (is (mbql5-definition? (:definition response))
+ "Returned definition should be MBQL5"))))))
+
+(deftest api-accepts-mbql4-on-put-test
+ (testing "PUT /api/measure/:id accepts MBQL4 definitions and returns MBQL5"
+ (mt/with-temp [:model/Measure {measure-id :id} {:table_id (mt/id :venues)
+ :definition (pmbql-measure-definition (mt/id :venues) (mt/id :venues :price))}]
+ (testing "MBQL4 fragment format"
+ (let [response (mt/user-http-request
+ :crowberto :put 200 (str "measure/" measure-id)
+ {:revision_message "Update with fragment"
+ :definition (mbql4-fragment-definition (mt/id :venues) [[:count]])})]
+ (is (= measure-id (:id response)))
+ (is (mbql5-definition? (:definition response))
+ "Returned definition should be MBQL5")))
+ (testing "MBQL4 full query format"
+ (let [response (mt/user-http-request
+ :crowberto :put 200 (str "measure/" measure-id)
+ {:revision_message "Update with full query"
+ :definition (legacy-mbql-measure-definition (mt/id) (mt/id :venues) [[:sum [:field (mt/id :venues :price) nil]]])})]
+ (is (= measure-id (:id response)))
+ (is (mbql5-definition? (:definition response))
+ "Returned definition should be MBQL5"))))))
+
+(deftest api-mbql4-measure-is-executable-test
+ (testing "Measures created via API with MBQL4 can be used in queries"
(mt/with-model-cleanup [:model/Measure]
- ;; Create a measure via POST /api/measure
(let [{measure-id :id} (mt/user-http-request
:crowberto :post 200 "measure"
{:name "Venue Count"
:table_id (mt/id :venues)
- :definition (legacy-mbql-measure-definition
- (mt/id)
- (mt/id :venues)
- [[:count]])})
- ;; Reference the measure in an expression: [:+ [:measure id] 100]
- legacy-query {:database (mt/id)
- :type :query
- :query {:source-table (mt/id :venues)
- :aggregation [[:+ [:measure measure-id] 100]]}}
- ;; Equivalent direct query: count + 100
- direct-query {:database (mt/id)
- :type :query
- :query {:source-table (mt/id :venues)
- :aggregation [[:+ [:count] 100]]}}]
+ :definition (mbql4-fragment-definition (mt/id :venues) [[:count]])})
+ ;; Query using the measure
+ measure-query {:database (mt/id)
+ :type :query
+ :query {:source-table (mt/id :venues)
+ :aggregation [[:measure measure-id]]}}
+ ;; Equivalent direct query
+ direct-query {:database (mt/id)
+ :type :query
+ :query {:source-table (mt/id :venues)
+ :aggregation [[:count]]}}]
(is (= (mt/rows (qp/process-query direct-query))
- (mt/rows (qp/process-query legacy-query))))))))
+ (mt/rows (qp/process-query measure-query))))))))
diff --git a/test/metabase/measures/models/measure_test.clj b/test/metabase/measures/models/measure_test.clj
index 21f75216e14..6db880b4c26 100644
--- a/test/metabase/measures/models/measure_test.clj
+++ b/test/metabase/measures/models/measure_test.clj
@@ -141,3 +141,54 @@
#"[Mm]easures cannot reference metrics"
(t2/update! :model/Measure measure-id
{:definition (measure-definition (lib.metadata/metric mp metric-id))}))))))))
+
+;;; ------------------------------------------------ MBQL4 Rejection Tests ------------------------------------------------
+;;; The model layer should only accept MBQL5 definitions. MBQL4 conversion still happens at the API/serdes layer.
+
+(deftest model-rejects-mbql4-on-insert-test
+ (testing "Model layer should reject MBQL4 definitions on insert"
+ (testing "MBQL4 fragment"
+ (is (thrown-with-msg?
+ Exception
+ #"Invalid measure definition"
+ (t2/insert! :model/Measure
+ {:name "Bad Measure"
+ :table_id (mt/id :venues)
+ :creator_id (mt/user->id :rasta)
+ :definition {:source-table (mt/id :venues)
+ :aggregation [[:count]]}}))))
+ (testing "MBQL4 full query"
+ (is (thrown-with-msg?
+ Exception
+ #"Invalid measure definition"
+ (t2/insert! :model/Measure
+ {:name "Bad Measure"
+ :table_id (mt/id :venues)
+ :creator_id (mt/user->id :rasta)
+ :definition {:database (mt/id)
+ :type :query
+ :query {:source-table (mt/id :venues)
+ :aggregation [[:count]]}}}))))))
+
+(deftest model-rejects-mbql4-on-update-test
+ (testing "Model layer should reject MBQL4 definitions on update"
+ (mt/with-temp [:model/Measure {measure-id :id} {:name "Good Measure"
+ :table_id (mt/id :venues)
+ :creator_id (mt/user->id :rasta)
+ :definition (measure-definition (lib/count))}]
+ (testing "MBQL4 fragment"
+ (is (thrown-with-msg?
+ Exception
+ #"Invalid measure definition"
+ (t2/update! :model/Measure measure-id
+ {:definition {:source-table (mt/id :venues)
+ :aggregation [[:count]]}}))))
+ (testing "MBQL4 full query"
+ (is (thrown-with-msg?
+ Exception
+ #"Invalid measure definition"
+ (t2/update! :model/Measure measure-id
+ {:definition {:database (mt/id)
+ :type :query
+ :query {:source-table (mt/id :venues)
+ :aggregation [[:count]]}}})))))))
diff --git a/test/metabase/query_processor/aggregation_test.clj b/test/metabase/query_processor/aggregation_test.clj
index 3b87121f470..7aae4b2bc25 100644
--- a/test/metabase/query_processor/aggregation_test.clj
+++ b/test/metabase/query_processor/aggregation_test.clj
@@ -2,11 +2,14 @@
"Tests for MBQL aggregations."
(:require
[clojure.test :refer :all]
+ [mb.hawk.assert-exprs.approximately-equal :as =?]
+ [medley.core :as m]
[metabase.driver :as driver]
[metabase.lib.core :as lib]
[metabase.lib.metadata :as lib.metadata]
[metabase.lib.test-util :as lib.tu]
[metabase.query-processor :as qp]
+ [metabase.query-processor.preprocess :as qp.preprocess]
^{:clj-kondo/ignore [:deprecated-namespace]} [metabase.query-processor.store :as qp.store]
[metabase.query-processor.test-util :as qp.test-util]
[metabase.test :as mt]
@@ -317,6 +320,144 @@
(qp/process-query)
(->> (mt/formatted-rows [int])))))))))
+;;; +----------------------------------------------------------------------------------------------------------------+
+;;; | Measure Tests |
+;;; +----------------------------------------------------------------------------------------------------------------+
+
+(deftest ^:parallel measure-results-test
+ (testing "Execute query with measure through full QP, verify results match equivalent direct aggregation"
+ (let [mp (mt/metadata-provider)
+ measure-definition (-> (lib/query mp (lib.metadata/table mp (mt/id :products)))
+ (lib/aggregate (lib/sum (lib.metadata/field mp (mt/id :products :price)))))
+ mp (lib.tu/mock-metadata-provider
+ mp
+ {:measures [{:id 1
+ :name "Total Price"
+ :table-id (mt/id :products)
+ :definition measure-definition}]})
+ measure-query (-> (lib/query mp (lib.metadata/table mp (mt/id :products)))
+ (lib/aggregate (lib.metadata/measure mp 1)))]
+ (is (= (mt/rows (qp/process-query measure-definition))
+ (mt/rows (qp/process-query measure-query)))))))
+
+(deftest ^:parallel nested-measure-results-test
+ (testing "Execute query with nested measures, verify results"
+ (let [mp (mt/metadata-provider)
+ measure2-definition (-> (lib/query mp (lib.metadata/table mp (mt/id :products)))
+ (lib/aggregate (lib/count)))
+ mp (lib.tu/mock-metadata-provider
+ mp
+ {:measures [{:id 2
+ :name "Product Count"
+ :table-id (mt/id :products)
+ :definition measure2-definition}]})
+ measure1-definition (-> (lib/query mp (lib.metadata/table mp (mt/id :products)))
+ (lib/aggregate (lib/* (lib.metadata/measure mp 2) 10)))
+ mp (lib.tu/mock-metadata-provider
+ mp
+ {:measures [{:id 1
+ :name "Product Count x10"
+ :table-id (mt/id :products)
+ :definition measure1-definition}]})
+ measure-query (-> (lib/query mp (lib.metadata/table mp (mt/id :products)))
+ (lib/aggregate (lib.metadata/measure mp 1)))
+ direct-query (-> (lib/query mp (lib.metadata/table mp (mt/id :products)))
+ (lib/aggregate (lib/* (lib/count) 10)))]
+ (is (= (mt/rows (qp/process-query direct-query))
+ (mt/rows (qp/process-query measure-query)))))))
+
+(deftest ^:parallel measure-with-breakout-test
+ (testing "Measure works correctly with breakout"
+ (let [mp (mt/metadata-provider)
+ measure-definition (-> (lib/query mp (lib.metadata/table mp (mt/id :products)))
+ (lib/aggregate (lib/sum (lib.metadata/field mp (mt/id :products :price)))))
+ mp (lib.tu/mock-metadata-provider
+ mp
+ {:measures [{:id 1
+ :name "Total Price"
+ :table-id (mt/id :products)
+ :definition measure-definition}]})
+ measure-query (-> (lib/query mp (lib.metadata/table mp (mt/id :products)))
+ (lib/aggregate (lib.metadata/measure mp 1))
+ (lib/breakout (lib.metadata/field mp (mt/id :products :category))))
+ direct-query (-> (lib/query mp (lib.metadata/table mp (mt/id :products)))
+ (lib/aggregate (lib/sum (lib.metadata/field mp (mt/id :products :price))))
+ (lib/breakout (lib.metadata/field mp (mt/id :products :category))))]
+ (is (= (mt/rows (qp/process-query direct-query))
+ (mt/rows (qp/process-query measure-query)))))))
+
+(deftest ^:parallel measure-with-segment-test
+ (testing "Measure with segment reference works through full QP pipeline"
+ (let [mp (mt/metadata-provider)
+ mp (lib.tu/mock-metadata-provider
+ mp
+ {:segments [{:id 1
+ :name "Affordable Venues"
+ :table-id (mt/id :venues)
+ :definition (-> (lib/query mp (lib.metadata/table mp (mt/id :venues)))
+ (lib/filter (lib/< (lib.metadata/field mp (mt/id :venues :price)) 4)))}]})
+ measure-definition (-> (lib/query mp (lib.metadata/table mp (mt/id :venues)))
+ (lib/aggregate (lib/count-where (lib.metadata/segment mp 1))))
+ mp (lib.tu/mock-metadata-provider
+ mp
+ {:measures [{:id 1
+ :name "Affordable Venue Count"
+ :table-id (mt/id :venues)
+ :definition measure-definition}]})
+ measure-query (-> (lib/query mp (lib.metadata/table mp (mt/id :venues)))
+ (lib/aggregate (lib.metadata/measure mp 1)))
+ direct-query (-> (lib/query mp (lib.metadata/table mp (mt/id :venues)))
+ (lib/aggregate (lib/count-where (lib/< (lib.metadata/field mp (mt/id :venues :price)) 4))))]
+ (is (= (mt/rows (qp/process-query direct-query))
+ (mt/rows (qp/process-query measure-query)))))))
+
+(deftest ^:parallel measure-multi-stage-query-test
+ (testing "Measure works correctly when referenced in a subsequent stage (field ref should use operator name, not display name)"
+ (let [mp (mt/metadata-provider)
+ measure-definition (-> (lib/query mp (lib.metadata/table mp (mt/id :products)))
+ (lib/aggregate (lib/sum (lib.metadata/field mp (mt/id :products :price)))))
+ mp (lib.tu/mock-metadata-provider
+ mp
+ {:measures [{:id 1
+ :name "Total Price"
+ :table-id (mt/id :products)
+ :definition measure-definition}]})
+ measure-query (-> (lib/query mp (lib.metadata/table mp (mt/id :products)))
+ (lib/breakout (lib.metadata/field mp (mt/id :products :category)))
+ (lib/aggregate (lib.metadata/measure mp 1))
+ lib/append-stage)
+ measure-cols (lib/filterable-columns measure-query)
+ measure-col (m/find-first #(= (:name %) "sum") measure-cols)
+ measure-query (lib/filter measure-query (lib/> measure-col 100))
+ direct-query (-> (lib/query mp (lib.metadata/table mp (mt/id :products)))
+ (lib/breakout (lib.metadata/field mp (mt/id :products :category)))
+ (lib/aggregate (lib/sum (lib.metadata/field mp (mt/id :products :price))))
+ lib/append-stage)
+ direct-cols (lib/filterable-columns direct-query)
+ direct-col (m/find-first #(= (:name %) "sum") direct-cols)
+ direct-query (lib/filter direct-query (lib/> direct-col 100))]
+ (is (= (mt/rows (qp/process-query direct-query))
+ (mt/rows (qp/process-query measure-query)))))))
+
+(deftest ^:parallel measure-maintains-aggregation-refs-test
+ (testing "the aggregation that replaces a :measure ref should keep the :measure's :lib/uuid, so :aggregation refs pointing to it are still valid"
+ (let [mp (mt/metadata-provider)
+ definition (-> (lib/query mp (lib.metadata/table mp (mt/id :venues)))
+ (lib/aggregate (lib/sum (lib.metadata/field mp (mt/id :venues :price)))))
+ mp (lib.tu/mock-metadata-provider
+ mp
+ {:measures [{:id 1
+ :name "Total Revenue"
+ :table-id (mt/id :venues)
+ :definition definition}]})
+ measure (lib.metadata/measure mp 1)
+ query (-> (lib/query mp (lib.metadata/table mp (mt/id :venues)))
+ (lib/aggregate measure)
+ (as-> <> (lib/order-by <> (lib/aggregation-ref <> 0))))]
+ (is (=? {:stages [{:aggregation [[:sum {:lib/uuid (=?/same :uuid)} [:field {} (mt/id :venues :price)]]]
+ :order-by [[:asc {} [:aggregation {} (=?/same :uuid)]]]}]}
+ (qp.preprocess/preprocess query))))))
+
;; !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
;; ! !
;; ! tests for named aggregations can be found in `expression-aggregations-test` !
diff --git a/test/metabase/query_processor/middleware/measures_test.clj b/test/metabase/query_processor/middleware/measures_test.clj
index 3cf5dff4809..5601b37ccfa 100644
--- a/test/metabase/query_processor/middleware/measures_test.clj
+++ b/test/metabase/query_processor/middleware/measures_test.clj
@@ -1,15 +1,12 @@
(ns metabase.query-processor.middleware.measures-test
(:require
[clojure.test :refer [deftest is testing]]
- [mb.hawk.assert-exprs.approximately-equal :as =?]
+ [mb.hawk.assert-exprs.approximately-equal]
[metabase.lib.core :as lib]
[metabase.lib.metadata :as lib.metadata]
[metabase.lib.test-metadata :as meta]
[metabase.lib.test-util :as lib.tu]
- [metabase.query-processor :as qp]
- [metabase.query-processor.middleware.measures :as measures]
- [metabase.query-processor.preprocess :as qp.preprocess]
- [metabase.test :as mt]))
+ [metabase.query-processor.middleware.measures :as measures]))
;;; +----------------------------------------------------------------------------------------------------------------+
;;; | Test Helpers |
@@ -124,6 +121,22 @@
:aggregation [[:+ {} [:count {}] 1]]}]}
(adjust query))))))
+(deftest ^:parallel measure-composing-two-measures-test
+ (testing "Measure C that combines Measure A and Measure B expands correctly"
+ (let [[_measure1 mp] (mock-measure 1 (basic-sum-measure-query))
+ [_measure2 mp] (mock-measure mp 2 (basic-count-measure-query))
+ measure3-definition (-> (lib/query mp (meta/table-metadata :products))
+ (lib/aggregate (lib// (lib.metadata/measure mp 1)
+ (lib.metadata/measure mp 2))))
+ [_measure3 mp] (mock-measure mp 3 measure3-definition)
+ query (-> (lib/query mp (meta/table-metadata :products))
+ (lib/aggregate (lib.metadata/measure mp 3)))]
+ (is (=? {:stages [{:source-table (meta/id :products)
+ :aggregation [[:/ {}
+ [:sum {} [:field {} (meta/id :products :price)]]
+ [:count {}]]]}]}
+ (adjust query))))))
+
(deftest ^:parallel three-level-nested-measures-test
(testing "A → B → C chain expands correctly"
(let [;; Measure 3 is a simple sum
@@ -220,150 +233,6 @@
(lib/aggregate (lib.metadata/measure mp 1)))]
(is (thrown-with-msg? clojure.lang.ExceptionInfo #"Measure cycle detected" (adjust query))))))
-;;; +----------------------------------------------------------------------------------------------------------------+
-;;; | E2E Tests |
-;;; +----------------------------------------------------------------------------------------------------------------+
-
-(deftest ^:parallel e2e-measure-results-test
- (testing "Execute query with measure through full QP, verify results match equivalent direct aggregation"
- (let [mp (mt/metadata-provider)
- ;; Create a measure that sums the price field
- measure-definition (-> (lib/query mp (lib.metadata/table mp (mt/id :products)))
- (lib/aggregate (lib/sum (lib.metadata/field mp (mt/id :products :price)))))
- mp (lib.tu/mock-metadata-provider
- mp
- {:measures [{:id 1
- :name "Total Price"
- :table-id (mt/id :products)
- :definition measure-definition}]})
- ;; Query using the measure
- measure-query (-> (lib/query mp (lib.metadata/table mp (mt/id :products)))
- (lib/aggregate (lib.metadata/measure mp 1)))
- ;; Equivalent direct query
- direct-query (-> (lib/query mp (lib.metadata/table mp (mt/id :products)))
- (lib/aggregate (lib/sum (lib.metadata/field mp (mt/id :products :price)))))]
- (is (= (mt/rows (qp/process-query direct-query))
- (mt/rows (qp/process-query measure-query)))))))
-
-(deftest ^:parallel e2e-nested-measure-results-test
- (testing "Execute query with nested measures, verify results"
- (let [mp (mt/metadata-provider)
- ;; Measure 2: count of products
- measure2-definition (-> (lib/query mp (lib.metadata/table mp (mt/id :products)))
- (lib/aggregate (lib/count)))
- mp (lib.tu/mock-metadata-provider
- mp
- {:measures [{:id 2
- :name "Product Count"
- :table-id (mt/id :products)
- :definition measure2-definition}]})
- ;; Measure 1: references measure 2 with arithmetic (count * 10)
- measure1-definition (-> (lib/query mp (lib.metadata/table mp (mt/id :products)))
- (lib/aggregate (lib/* (lib.metadata/measure mp 2) 10)))
- mp (lib.tu/mock-metadata-provider
- mp
- {:measures [{:id 1
- :name "Product Count x10"
- :table-id (mt/id :products)
- :definition measure1-definition}]})
- ;; Query using measure 1
- measure-query (-> (lib/query mp (lib.metadata/table mp (mt/id :products)))
- (lib/aggregate (lib.metadata/measure mp 1)))
- ;; Equivalent direct query: count * 10
- direct-query (-> (lib/query mp (lib.metadata/table mp (mt/id :products)))
- (lib/aggregate (lib/* (lib/count) 10)))]
- (is (= (mt/rows (qp/process-query direct-query))
- (mt/rows (qp/process-query measure-query)))))))
-
-(deftest ^:parallel e2e-measure-with-breakout-test
- (testing "Measure works correctly with breakout"
- (let [mp (mt/metadata-provider)
- measure-definition (-> (lib/query mp (lib.metadata/table mp (mt/id :products)))
- (lib/aggregate (lib/sum (lib.metadata/field mp (mt/id :products :price)))))
- mp (lib.tu/mock-metadata-provider
- mp
- {:measures [{:id 1
- :name "Total Price"
- :table-id (mt/id :products)
- :definition measure-definition}]})
- measure-query (-> (lib/query mp (lib.metadata/table mp (mt/id :products)))
- (lib/aggregate (lib.metadata/measure mp 1))
- (lib/breakout (lib.metadata/field mp (mt/id :products :category))))
- direct-query (-> (lib/query mp (lib.metadata/table mp (mt/id :products)))
- (lib/aggregate (lib/sum (lib.metadata/field mp (mt/id :products :price))))
- (lib/breakout (lib.metadata/field mp (mt/id :products :category))))]
- (is (= (mt/rows (qp/process-query direct-query))
- (mt/rows (qp/process-query measure-query)))))))
-
-;;; +----------------------------------------------------------------------------------------------------------------+
-;;; | Measures with Segment References |
-;;; +----------------------------------------------------------------------------------------------------------------+
-
-(deftest ^:parallel e2e-measure-with-segment-test
- (testing "Measure with segment reference works through full QP pipeline"
- (let [mp (mt/metadata-provider)
- ;; Create a segment for venues with price < 4
- mp (lib.tu/mock-metadata-provider
- mp
- {:segments [{:id 1
- :name "Affordable Venues"
- :table-id (mt/id :venues)
- :definition (-> (lib/query mp (lib.metadata/table mp (mt/id :venues)))
- (lib/filter (lib/< (lib.metadata/field mp (mt/id :venues :price)) 4)))}]})
- ;; Create a measure that counts venues matching the segment
- measure-definition (-> (lib/query mp (lib.metadata/table mp (mt/id :venues)))
- (lib/aggregate (lib/count-where (lib.metadata/segment mp 1))))
- mp (lib.tu/mock-metadata-provider
- mp
- {:measures [{:id 1
- :name "Affordable Venue Count"
- :table-id (mt/id :venues)
- :definition measure-definition}]})
- ;; Query using the measure
- measure-query (-> (lib/query mp (lib.metadata/table mp (mt/id :venues)))
- (lib/aggregate (lib.metadata/measure mp 1)))
- ;; Equivalent direct query
- direct-query (-> (lib/query mp (lib.metadata/table mp (mt/id :venues)))
- (lib/aggregate (lib/count-where (lib/< (lib.metadata/field mp (mt/id :venues :price)) 4))))]
- (is (= (mt/rows (qp/process-query direct-query))
- (mt/rows (qp/process-query measure-query)))))))
-
-(deftest ^:parallel e2e-measure-multi-stage-query-test
- (testing "Measure works correctly when referenced in a subsequent stage (field ref should use operator name, not display name)"
- (let [mp (mt/metadata-provider)
- ;; Create a measure "Total Price" that sums products.price
- ;; The measure's name is "Total Price" but the operator name should be "sum"
- measure-definition (-> (lib/query mp (lib.metadata/table mp (mt/id :products)))
- (lib/aggregate (lib/sum (lib.metadata/field mp (mt/id :products :price)))))
- mp (lib.tu/mock-metadata-provider
- mp
- {:measures [{:id 1
- :name "Total Price"
- :table-id (mt/id :products)
- :definition measure-definition}]})
- ;; Create a multi-stage query:
- ;; Stage 0: Group by category, aggregate with measure
- ;; Stage 1: Filter on the aggregation result
- measure-query (-> (lib/query mp (lib.metadata/table mp (mt/id :products)))
- (lib/breakout (lib.metadata/field mp (mt/id :products :category)))
- (lib/aggregate (lib.metadata/measure mp 1))
- lib/append-stage)
- ;; Find the sum column (from the aggregation) in the second stage
- ;; The column should have :name "sum" not "Total Price"
- measure-cols (lib/filterable-columns measure-query)
- measure-col (first (filter #(= (:name %) "sum") measure-cols))
- measure-query (lib/filter measure-query (lib/> measure-col 100))
- ;; Equivalent direct query
- direct-query (-> (lib/query mp (lib.metadata/table mp (mt/id :products)))
- (lib/breakout (lib.metadata/field mp (mt/id :products :category)))
- (lib/aggregate (lib/sum (lib.metadata/field mp (mt/id :products :price))))
- lib/append-stage)
- direct-cols (lib/filterable-columns direct-query)
- direct-col (first (filter #(= (:name %) "sum") direct-cols))
- direct-query (lib/filter direct-query (lib/> direct-col 100))]
- (is (= (mt/rows (qp/process-query direct-query))
- (mt/rows (qp/process-query measure-query)))))))
-
;;; +----------------------------------------------------------------------------------------------------------------+
;;; | Measures with Metric References (disallowed) |
;;; +----------------------------------------------------------------------------------------------------------------+
@@ -425,25 +294,6 @@
#"[Mm]easures cannot reference metrics"
(adjust query))))))
-(deftest ^:parallel maintain-aggregation-refs-test
- (testing "the aggregation that replaces a :measure ref should keep the :measure's :lib/uuid, so :aggregation refs pointing to it are still valid"
- (let [mp (mt/metadata-provider)
- definition (-> (lib/query mp (lib.metadata/table mp (mt/id :venues)))
- (lib/aggregate (lib/sum (lib.metadata/field mp (mt/id :venues :price)))))
- mp (lib.tu/mock-metadata-provider
- mp
- {:measures [{:id 1
- :name "Total Revenue"
- :table-id (mt/id :venues)
- :definition definition}]})
- measure (lib.metadata/measure mp 1)
- query (-> (lib/query mp (lib.metadata/table mp (mt/id :venues)))
- (lib/aggregate measure)
- (as-> <> (lib/order-by <> (lib/aggregation-ref <> 0))))]
- (is (=? {:stages [{:aggregation [[:sum {:lib/uuid (=?/same :uuid)} [:field {} (mt/id :venues :price)]]]
- :order-by [[:asc {} [:aggregation {} (=?/same :uuid)]]]}]}
- (qp.preprocess/preprocess query))))))
-
(deftest max-expansion-depth-test
(let [mp (lib.tu/mock-metadata-provider
meta/metadata-provider
diff --git a/test/metabase/segments/models/segment_test.clj b/test/metabase/segments/models/segment_test.clj
index 560d19050f8..a283ec98d7d 100644
--- a/test/metabase/segments/models/segment_test.clj
+++ b/test/metabase/segments/models/segment_test.clj
@@ -149,11 +149,11 @@
(testing "Inserting a segment that references an existing segment should succeed"
(mt/with-temp [:model/Segment {segment-1-id :id} {:name "Segment 1"
:table_id (mt/id :venues)
- :definition {:filter [:> [:field (mt/id :venues :price) nil] 2]}}]
- (mt/with-temp [:model/Segment segment-2 {:name "Segment 2"
- :table_id (mt/id :venues)
- :definition {:filter [:segment segment-1-id]}}]
- (is (some? (:id segment-2)))))))
+ :definition {:filter [:> [:field (mt/id :venues :price) nil] 2]}}
+ :model/Segment segment-2 {:name "Segment 2"
+ :table_id (mt/id :venues)
+ :definition {:filter [:segment segment-1-id]}}]
+ (is (some? (:id segment-2))))))
(deftest update-segment-cycle-detection-test
(testing "Updating a segment to reference a non-existent segment should fail"
@@ -175,11 +175,11 @@
(testing "Updating a segment to create an indirect cycle should fail"
(mt/with-temp [:model/Segment {segment-1-id :id} {:name "Segment 1"
:table_id (mt/id :venues)
- :definition {:filter [:> [:field (mt/id :venues :price) nil] 2]}}]
- (mt/with-temp [:model/Segment {segment-2-id :id} {:name "Segment 2"
- :table_id (mt/id :venues)
- :definition {:filter [:segment segment-1-id]}}]
- (is (thrown-with-msg?
- Exception
- #"[Cc]ycle"
- (t2/update! :model/Segment segment-1-id {:definition {:filter [:segment segment-2-id]}})))))))
+ :definition {:filter [:> [:field (mt/id :venues :price) nil] 2]}}
+ :model/Segment {segment-2-id :id} {:name "Segment 2"
+ :table_id (mt/id :venues)
+ :definition {:filter [:segment segment-1-id]}}]
+ (is (thrown-with-msg?
+ Exception
+ #"[Cc]ycle"
+ (t2/update! :model/Segment segment-1-id {:definition {:filter [:segment segment-2-id]}}))))))