Skip to content

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

fix: preserve model column settings metadata in aggregations (#68692) (#68828)

Shreedhar Bhat6d5087f77bf0
diff --git a/src/metabase/lib/field/resolution.cljc b/src/metabase/lib/field/resolution.cljc
index b251f2044ff..bf5f33b6833 100644
--- a/src/metabase/lib/field/resolution.cljc
+++ b/src/metabase/lib/field/resolution.cljc
@@ -249,6 +249,7 @@
:display-name
:id
:semantic-type
+ :settings
:table-id}))
(declare resolve-in-previous-stage-returned-columns-and-update-keys)
diff --git a/test/metabase/lib/aggregation_test.cljc b/test/metabase/lib/aggregation_test.cljc
index 67d9d4d0eeb..0632637da96 100644
--- a/test/metabase/lib/aggregation_test.cljc
+++ b/test/metabase/lib/aggregation_test.cljc
@@ -7,6 +7,7 @@
[metabase.lib.aggregation :as lib.aggregation]
[metabase.lib.convert :as lib.convert]
[metabase.lib.core :as lib]
+ [metabase.lib.metadata :as lib.metadata]
[metabase.lib.metadata.calculation :as lib.metadata.calculation]
[metabase.lib.query :as lib.query]
[metabase.lib.schema.expression :as lib.schema.expression]
@@ -630,6 +631,49 @@
:lib/source :source/aggregations}
(lib/metadata query (first (lib/aggregations-metadata query -1))))))))
+(deftest ^:parallel preserve-model-column-settings-metadata-test
+ (testing "Aggregation metadata should return the `:settings` from model's result_metadata for the column being aggregated (#68692)"
+ (let [;; Create a model with a column that has custom settings (like "multiply by a number")
+ model-result-metadata [{:id (meta/id :venues :id)
+ :name "ID"
+ :base-type :type/BigInteger
+ :effective-type :type/BigInteger
+ :display-name "ID"}
+ {:id (meta/id :venues :price)
+ :name "PRICE"
+ :base-type :type/Integer
+ :effective-type :type/Integer
+ :display-name "Price"
+ ;; Custom settings like "multiply by a number" (scale: 0.01)
+ :settings {:scale 0.01}}]
+ mp (lib.tu/mock-metadata-provider
+ meta/metadata-provider
+ {:cards [{:id 1
+ :name "Venues Model"
+ :database-id (meta/id)
+ :type :model
+ :dataset-query {:lib/type :mbql/query
+ :database (meta/id)
+ :stages [{:lib/type :mbql.stage/mbql
+ :source-table (meta/id :venues)}]}
+ :result-metadata model-result-metadata}]})
+ ;; Query the model and do a Sum on the Price column
+ model-query (lib/query mp (lib.metadata/card mp 1))
+ price-col (first (filter #(= (:name %) "PRICE")
+ (lib/returned-columns model-query)))
+ agg-query (lib/aggregate model-query (lib/sum price-col))
+ agg-meta (first (lib/aggregations-metadata agg-query))]
+ (testing "the price column from model should have :settings"
+ (is (=? {:settings {:scale 0.01}}
+ price-col)))
+ (testing "the aggregation metadata should preserve the :settings from the model column"
+ (is (=? {:settings {:scale 0.01}
+ :lib/type :metadata/column
+ :name "sum"
+ :display-name "Sum of Price"
+ :lib/source :source/aggregations}
+ agg-meta))))))
+
(deftest ^:parallel count-aggregation-type-test
(testing "Count aggregation should produce numeric columns"
(let [query (-> (lib.tu/venues-query)