Skip to content

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

Fix sorting on aggregate columns (#63940)

Cam Saulcbdc23a7bfca
diff --git a/src/metabase/driver/sql/query_processor.clj b/src/metabase/driver/sql/query_processor.clj
index 77166f9fb79..4ca5d808577 100644
--- a/src/metabase/driver/sql/query_processor.clj
+++ b/src/metabase/driver/sql/query_processor.clj
@@ -1282,13 +1282,15 @@
;; aggregation REFERENCE e.g. the ["aggregation" 0] fields we allow in order-by
(defmethod ->honeysql [:sql :aggregation]
[driver [_ index]]
- (driver-api/match-one
- (nth (:aggregation *inner-query*) index)
+ (driver-api/match-one (nth (:aggregation *inner-query*) index)
+ [:aggregation-options ag (options :guard driver-api/qp.add.source-alias)]
+ (->honeysql driver (h2x/identifier :field-alias (driver-api/qp.add.source-alias options)))
+
[:aggregation-options ag (options :guard :name)]
(->honeysql driver (h2x/identifier :field-alias (:name options)))
- [:aggregation-options ag _]
- #_:clj-kondo/ignore
+ [:aggregation-options ag options]
+ #_{:clj-kondo/ignore [:invalid-arity]}
(recur ag)
;; For some arcane reason we name the results of a distinct aggregation "count", everything else is named the
diff --git a/test/metabase/driver/sql/query_processor_test.clj b/test/metabase/driver/sql/query_processor_test.clj
index b39b78546a7..3810b2accd4 100644
--- a/test/metabase/driver/sql/query_processor_test.clj
+++ b/test/metabase/driver/sql/query_processor_test.clj
@@ -14,6 +14,7 @@
[metabase.lib.test-metadata :as meta]
[metabase.lib.test-util :as lib.tu]
[metabase.lib.test-util.macros :as lib.tu.macros]
+ [metabase.lib.test-util.notebook-helpers :as lib.tu.notebook]
[metabase.query-processor :as qp]
[metabase.query-processor.compile :as qp.compile]
[metabase.query-processor.middleware.limit :as limit]
@@ -1528,3 +1529,35 @@
:params nil}
(-> (qp.compile/compile query)
(update :query #(str/split-lines (driver/prettify-native-form :h2 %))))))))))
+
+;;; see also [[metabase.query-processor-test.order-by-test/order-by-aggregate-fields-test-6]]
+(deftest ^:parallel order-by-aggregation-reference-test
+ (testing "Should order by aggregation references correctly (#62885)"
+ (let [mp meta/metadata-provider
+ query (-> (lib/query mp (meta/table-metadata :products))
+ (lib/aggregate (lib/count))
+ (lib/aggregate (lib/sum (meta/field-metadata :products :price)))
+ (lib/aggregate (lib/sum (meta/field-metadata :products :rating)))
+ (lib/breakout (meta/field-metadata :products :category))
+ (as-> $query (lib/order-by $query (lib.tu.notebook/find-col-with-spec
+ $query
+ (lib/orderable-columns $query)
+ {}
+ {:display-name "Sum of Rating"}))))]
+ (is (= ["SELECT"
+ " \"PUBLIC\".\"PRODUCTS\".\"CATEGORY\" AS \"CATEGORY\","
+ " COUNT(*) AS \"count\","
+ " SUM(\"PUBLIC\".\"PRODUCTS\".\"PRICE\") AS \"sum\","
+ " SUM(\"PUBLIC\".\"PRODUCTS\".\"RATING\") AS \"sum_2\""
+ "FROM"
+ " \"PUBLIC\".\"PRODUCTS\""
+ "GROUP BY"
+ " \"PUBLIC\".\"PRODUCTS\".\"CATEGORY\""
+ "ORDER BY"
+ " \"sum_2\" ASC,"
+ " \"PUBLIC\".\"PRODUCTS\".\"CATEGORY\" ASC"]
+ (-> query
+ qp.compile/compile
+ :query
+ (->> (driver/prettify-native-form :h2))
+ str/split-lines))))))
diff --git a/test/metabase/query_processor_test/order_by_test.clj b/test/metabase/query_processor_test/order_by_test.clj
index e82b298c80c..13f7dc967e0 100644
--- a/test/metabase/query_processor_test/order_by_test.clj
+++ b/test/metabase/query_processor_test/order_by_test.clj
@@ -2,6 +2,10 @@
"Tests for the `:order-by` clause."
(:require
[clojure.test :refer :all]
+ [metabase.lib.core :as lib]
+ [metabase.lib.metadata :as lib.metadata]
+ [metabase.lib.test-util.notebook-helpers :as lib.tu.notebook]
+ [metabase.query-processor :as qp]
[metabase.test :as mt]))
(deftest ^:parallel order-by-test
@@ -127,3 +131,32 @@
{:aggregation [[:stddev $category_id]]
:breakout [$price]
:order-by [[:desc [:aggregation 0]]]}))))))))
+
+;;; See also [[metabase.driver.sql.query-processor-test/order-by-aggregation-reference-test]]
+(deftest ^:parallel order-by-aggregate-fields-test-6
+ (mt/test-drivers (mt/normal-drivers)
+ (testing "Should order by aggregation references correctly (#62885)"
+ (let [mp (mt/metadata-provider)
+ query (-> (lib/query mp (lib.metadata/table mp (mt/id :products)))
+ (lib/aggregate (lib/count))
+ (lib/aggregate (lib/sum (lib.metadata/field mp (mt/id :products :price))))
+ (lib/aggregate (lib/sum (lib.metadata/field mp (mt/id :products :rating))))
+ (lib/breakout (lib.metadata/field mp (mt/id :products :category)))
+ (as-> $query (lib/order-by $query (lib.tu.notebook/find-col-with-spec
+ $query
+ (lib/orderable-columns $query)
+ {}
+ {:display-name "Sum of Rating"})))
+ (lib/limit 4))
+ results (qp/process-query query)]
+ (mt/with-native-query-testing-context query
+ (is (= ["Category"
+ "Count"
+ "Sum of Price"
+ "Sum of Rating"]
+ (map :display_name (mt/cols results))))
+ (is (= [["Doohickey" 42 2185.89 156.6]
+ ["Widget" 54 3109.31 170.3]
+ ["Gadget" 53 3019.2 181.9]
+ ["Gizmo" 51 2834.88 185.5]]
+ (mt/formatted-rows [str int 2.0 1.0] results))))))))