The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Be selective about metadata flowing from columns into aggregations (#83689)
metamben645627469bba
diff --git a/src/metabase/lib/aggregation.cljc b/src/metabase/lib/aggregation.cljcindex 6447e5b2ae3..d7bb37961aa 100644--- a/src/metabase/lib/aggregation.cljc+++ b/src/metabase/lib/aggregation.cljc@@ -167,10 +167,37 @@:cum-count "count":count-where "count_where"))+;;; -------- Field.settings → aggregation inheritance policy (see #83416) --------+;;;+;;; Field.settings is a grab-bag — value-formatting (currency, decimals, date style…), per-row cell+;;; behavior (view_as:link, click_behavior), and cell-generic layout (show_mini_bar, text_align). Only+;;; a subset makes sense to flow into an aggregation.++(def ^:private cell-generic-settings-keys+ "Pure cell-level visual settings — meaningful for any aggregation that produces a cell."+ #{:show_mini_bar :text_wrapping :text_align})++(def ^:private value-formatting-settings-keys+ "Settings that describe how the field's own values are rendered — number and date formatting plus+ cell-generic layout. Safe to inherit for aggregations whose output IS a value of the operand's type+ (sum/avg/max/min/median/stddev/cum-sum/sum-where/percentile).++ Anything outside the number-formatting, date-formatting, and cell-generic categories is dropped. In+ particular, per-row cell behavior (view_as:link templates, click_behavior) can't resolve against an+ aggregated row (#83416)."+ (into cell-generic-settings-keys+ [;; number formatting+ :number_style :currency :currency_style :currency_in_header+ :number_separators :decimals :scale :prefix :suffix+ ;; date/time formatting (only meaningful when the aggregation preserves datetime: min/max/median)+ :date_style :date_separator :date_abbreviate :time_enabled :time_style]))+(defmethod lib.metadata.calculation/metadata-method ::quantity-aggregation[query stage-number clause]- (assoc ((get-method lib.metadata.calculation/metadata-method ::aggregation) query stage-number clause)- :semantic-type :type/Quantity))+ (let [base ((get-method lib.metadata.calculation/metadata-method ::aggregation) query stage-number clause)]+ (-> (dissoc base :settings)+ (m/assoc-some :settings (not-empty (select-keys (:settings base) cell-generic-settings-keys)))+ (assoc :semantic-type :type/Quantity))))(lib.hierarchy/derive ::quantity-aggregation ::aggregation)(lib.hierarchy/derive ::count-aggregation ::quantity-aggregation)@@ -305,12 +332,12 @@(defmethod lib.metadata.calculation/metadata-method ::aggregation[query stage-number [_tag _opts first-arg :as clause]](merge- ;; flow the `:options` from the field we're aggregating. This is important, for some reason.- ;; See [[metabase.query-processor.aggregation-test/field-settings-for-aggregate-fields-test]]+ ;; Flow :semantic-type and value-formatting :settings from the aggregated-over field; drop per-row cell+ ;; behavior and other categories that can't apply to an aggregated row.(when first-arg- ;; This might be an inner aggregation expression without an ident of its own, but that's fine since we're only- ;; here for its type!- (select-keys (lib.metadata.calculation/metadata query stage-number first-arg) [:settings :semantic-type]))+ (let [col (lib.metadata.calculation/metadata query stage-number first-arg)]+ (m/assoc-some (select-keys col [:semantic-type])+ :settings (not-empty (select-keys (:settings col) value-formatting-settings-keys)))))((get-method lib.metadata.calculation/metadata-method :default) query stage-number clause)))(lib.common/defop count [] [x])diff --git a/test/metabase/lib/aggregation_test.cljc b/test/metabase/lib/aggregation_test.cljcindex 983b3c05ca1..3e695687e3a 100644--- a/test/metabase/lib/aggregation_test.cljc+++ b/test/metabase/lib/aggregation_test.cljc@@ -443,7 +443,7 @@:name "count":display-name "Count":lib/source :source/aggregations}- {:settings {:is_priceless true}+ {:settings {:decimals 2}:lib/type :metadata/column:effective-type :type/Integer:name "sum"@@ -623,10 +623,10 @@(filterv :selected?))))))))(deftest ^:parallel preserve-field-settings-metadata-test- (testing "Aggregation metadata should return the `:settings` for the field being aggregated, for some reason."+ (testing "Aggregation metadata should inherit value-formatting `:settings` from the field being aggregated."(let [query (-> (lib.tu/venues-query)(lib/aggregate (lib/sum (meta/field-metadata :venues :price))))]- (is (=? {:settings {:is_priceless true}+ (is (=? {:settings {:decimals 2}:lib/type :metadata/column:effective-type :type/Integer:name "sum"diff --git a/test/metabase/lib/metadata/result_metadata_test.cljc b/test/metabase/lib/metadata/result_metadata_test.cljcindex c38b4eb367a..645d25f4213 100644--- a/test/metabase/lib/metadata/result_metadata_test.cljc+++ b/test/metabase/lib/metadata/result_metadata_test.cljc@@ -457,7 +457,7 @@(testing "`:aggregation-options`"(testing "`:name` and `:display-name`"(is (=? {:base-type :type/Integer- :settings {:is_priceless true}+ :settings {:decimals 2}:name "sum_2":display-name "My custom name"}(lib.tu.macros/$ids venues@@ -468,7 +468,7 @@(testing "`:aggregation-options`"(testing "`:name` only"(is (=? {:base-type :type/Integer- :settings {:is_priceless true}+ :settings {:decimals 2}:name "sum_2":display-name "Sum of Price"}(lib.tu.macros/$ids venues@@ -478,7 +478,7 @@(testing "`:aggregation-options`"(testing "`:display-name` only"(is (=? {:base-type :type/Integer- :settings {:is_priceless true}+ :settings {:decimals 2}:name "sum":display-name "My Custom Name"}(lib.tu.macros/$ids venuesdiff --git a/test/metabase/lib/test_metadata.cljc b/test/metabase/lib/test_metadata.cljcindex 850396bc6d3..f5389119d6b 100644--- a/test/metabase/lib/test_metadata.cljc+++ b/test/metabase/lib/test_metadata.cljc@@ -789,7 +789,7 @@:name "PRICE":fingerprint-version 5:has-field-values :list- :settings {:is_priceless true}+ :settings {:decimals 2}:caveats nil:fk-target-field-id nil:custom-position 0diff --git a/test/metabase/query_processor/aggregation_test.clj b/test/metabase/query_processor/aggregation_test.cljindex b4b0fef23e5..06fb5531170 100644--- a/test/metabase/query_processor/aggregation_test.clj+++ b/test/metabase/query_processor/aggregation_test.clj@@ -168,14 +168,32 @@(qp.store/with-metadata-provider (lib.tu/merged-mock-metadata-provider(mt/metadata-provider){:fields [{:id (mt/id :venues :price)- :settings {:is_priceless false}}]})+ :settings {:decimals 2}}]})(let [results (mt/run-mbql-query venues{:aggregation [[:sum $price]]})](is (=? (assoc (qp.test-util/aggregate-col :sum :venues :price)- :settings {:is_priceless false})+ :settings {:decimals 2})(or (-> results mt/cols first)results)))))))+(deftest ^:parallel click-behavior-settings-dont-flow-into-count-aggregations-test+ (testing (str "Click/link behavior configured on a Field should not flow into count-like aggregations whose "+ "result is a count, not a value drawn from the field (#83416).")+ (qp.store/with-metadata-provider (lib.tu/merged-mock-metadata-provider+ (mt/metadata-provider)+ {:fields [{:id (mt/id :venues :price)+ :settings {:view_as "link"+ :link_text "{{name}}"+ :link_url "https://example.com/{{id}}"}}]})+ (let [price [:field (mt/id :venues :price) nil]]+ (doseq [aggregation [[:count price]+ [:cum-count price]+ [:distinct price]+ [:count-where [:> price 1]]]]+ (testing (str (first aggregation))+ (let [col (-> (mt/run-mbql-query venues {:aggregation [aggregation]}) mt/cols first)]+ (is (nil? (get-in col [:settings :view_as]))))))))))+(deftest ^:parallel semantic-type-for-aggregate-fields-test(testing "Does `:semantic-type` show up for aggregate Fields? (#38022)"(qp.store/with-metadata-provider (lib.tu/merged-mock-metadata-provider