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
Fix the walk of :case and :if when checking for window functions (#84541)
metamben3d68f098b28e
diff --git a/src/metabase/lib/schema/aggregation.cljc b/src/metabase/lib/schema/aggregation.cljcindex ace2a258ca4..85783a6a95e 100644--- a/src/metabase/lib/schema/aggregation.cljc+++ b/src/metabase/lib/schema/aggregation.cljc@@ -144,8 +144,9 @@;; Case has shape [:case opts [[cond expr]...] default-expr?].;; `:if` is an alias for `:case`.(if (#{:case :if} tag)- (or (some walk (ffirst args))- (some walk (fnext args)))+ (let [[pairs fallback] args]+ (or (some walk (eduction cat pairs))+ (walk fallback)))(some walk args)))))](walk x)))diff --git a/test/metabase/lib/schema/aggregation_test.cljc b/test/metabase/lib/schema/aggregation_test.cljcindex aa10ca28077..94d9020e484 100644--- a/test/metabase/lib/schema/aggregation_test.cljc+++ b/test/metabase/lib/schema/aggregation_test.cljc@@ -114,6 +114,61 @@::lib.schema.aggregation/aggregation ag::lib.schema.aggregation/aggregations [ag])))))+(deftest ^:parallel window-aggregation-expression?-test+ (let [field [:field {:lib/uuid "00000000-0000-0000-0000-000000000001"} 1]+ other [:field {:lib/uuid "00000000-0000-0000-0000-000000000002"} 2]+ cum-sum [:cum-sum {:lib/uuid "00000000-0000-0000-0000-000000000003"} field]+ sum [:sum {:lib/uuid "00000000-0000-0000-0000-000000000004"} field]+ gt [:> {:lib/uuid "00000000-0000-0000-0000-000000000005"} field 3]+ other-gt [:> {:lib/uuid "00000000-0000-0000-0000-000000000006"} field 7]]+ (testing "bare window aggregation clauses"+ (are [clause] (lib.schema.aggregation/window-aggregation-expression? clause)+ cum-sum+ [:cum-count {:lib/uuid "00000000-0000-0000-0000-000000000007"}]+ [:offset {:lib/uuid "00000000-0000-0000-0000-000000000008"} sum -1]))+ (testing "non-window aggregations"+ (are [clause] (not (lib.schema.aggregation/window-aggregation-expression? clause))+ sum+ [:count {:lib/uuid "00000000-0000-0000-0000-000000000009"}]+ [:avg {:lib/uuid "00000000-0000-0000-0000-00000000000a"} field]))+ (testing "walks arithmetic expressions"+ (is (lib.schema.aggregation/window-aggregation-expression?+ [:/ {:lib/uuid "00000000-0000-0000-0000-00000000000b"} cum-sum sum]))+ (is (not (lib.schema.aggregation/window-aggregation-expression?+ [:/ {:lib/uuid "00000000-0000-0000-0000-00000000000c"} sum sum]))))+ (doseq [tag [:case :if]]+ (testing (str tag " with a literal else value doesn't crash (#84452)")+ ;; literal else values of various types — the walk must not try to seq a scalar+ (are [else] (not (lib.schema.aggregation/window-aggregation-expression?+ [:sum {:lib/uuid "00000000-0000-0000-0000-00000000000d"}+ [tag {:lib/uuid "00000000-0000-0000-0000-00000000000e"}+ [[gt field]]+ else]]))+ 0+ 0.0+ true+ false+ "x"))+ (testing (str tag " detects a window aggregation inside the else branch")+ (is (lib.schema.aggregation/window-aggregation-expression?+ [tag {:lib/uuid "00000000-0000-0000-0000-00000000000f"}+ [[gt field]]+ cum-sum])))+ (testing (str tag " detects a window aggregation inside a non-first case pair")+ (is (lib.schema.aggregation/window-aggregation-expression?+ [tag {:lib/uuid "00000000-0000-0000-0000-000000000010"}+ [[gt other]+ [other-gt cum-sum]]])))+ (testing (str tag " detects a window aggregation inside a case condition")+ (is (lib.schema.aggregation/window-aggregation-expression?+ [tag {:lib/uuid "00000000-0000-0000-0000-000000000011"}+ [[[:> {:lib/uuid "00000000-0000-0000-0000-000000000012"} cum-sum 0] field]]])))+ (testing (str tag " with no fallback and no window aggregation returns false")+ (is (not (lib.schema.aggregation/window-aggregation-expression?+ [tag {:lib/uuid "00000000-0000-0000-0000-000000000013"}+ [[gt field]+ [other-gt other]]])))))))+(deftest ^:parallel case-and-if-should-be-considered-valid-aggregations-test(doseq [clause [:if :case]](testing (str clause " should be allowed as an aggregation expression if it contains an aggregation")diff --git a/test/metabase/query_processor/pivot_test.clj b/test/metabase/query_processor/pivot_test.cljindex 727d0d6c102..0e5a642df14 100644--- a/test/metabase/query_processor/pivot_test.clj+++ b/test/metabase/query_processor/pivot_test.clj@@ -1094,6 +1094,21 @@:aggregation [[:count]]:breakout [$user_id->people.source [:expression "Product Rating + 1"]]})))))))+(deftest ^:parallel pivot-with-case-with-literal-else-in-aggregation-test+ (testing "pivot runs when an aggregation wraps a :case with a literal else value (#84452)"+ (mt/dataset test-data+ (let [mp (mt/metadata-provider)+ orders (lib.metadata/table mp (mt/id :orders))+ total (lib.metadata/field mp (mt/id :orders :total))+ quantity (lib.metadata/field mp (mt/id :orders :quantity))+ created (lib.metadata/field mp (mt/id :orders :created_at))+ query (-> (lib/query mp orders)+ (lib/aggregate (lib/sum (lib/case [[(lib/> quantity 3) total]] 0)))+ (lib/breakout (lib/with-temporal-bucket created :year))+ (lib/breakout quantity))]+ (is (=? {:status :completed}+ (qp.pivot/run-pivot-query query)))))))+(deftest ^:parallel measure-in-pivot-table-test(testing "a :measure clause used inside a pivot query executes like the equivalent inline aggregation"(mt/test-drivers (qp.pivot.test-util/applicable-drivers)