Skip to content

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

Fix downloading tables grouped by datetime->int (#64245)

John Swansonc5da59566ed3
diff --git a/enterprise/backend/test/metabase_enterprise/sandbox/query_processor/middleware/sandboxing_test.clj b/enterprise/backend/test/metabase_enterprise/sandbox/query_processor/middleware/sandboxing_test.clj
index 7acd1f09276..9622cd31699 100644
--- a/enterprise/backend/test/metabase_enterprise/sandbox/query_processor/middleware/sandboxing_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/sandbox/query_processor/middleware/sandboxing_test.clj
@@ -1551,3 +1551,31 @@
[3]]
(mt/rows
(qp/process-query (query))))))))
+
+(deftest datetime-extraction-to-int-test
+ (testing "Downloading CSV/XLSX for query with COUNT grouped by day of month should work for sandboxed users (#UXW-660)"
+ (met/with-gtaps! {:gtaps {:orders {:remappings {"user_id" ["variable" [:field (mt/id :orders :user_id) nil]]}}},
+ :attributes {"user_id" 1}}
+ (let [query (mt/mbql-query orders
+ {:aggregation [[:count]]
+ :breakout [[:field (mt/id :orders :created_at) {:effective-type :type/Integer
+ :base-type :type/DateTime
+ :temporal-unit :day-of-month}]]
+ :order-by [[:asc [:field (mt/id :orders :created_at) {:effective-type :type/Integer
+ :base-type :type/DateTime
+ :temporal-unit :day-of-month}]]]
+ :limit 5})]
+ (testing "Query should complete successfully with normal processing"
+ (let [result (qp/process-query query)]
+ (is (= :completed (:status result)))
+ (is (seq (mt/rows result)))))
+ (testing "Query should complete successfully when downloading as CSV"
+ (let [result (streaming.test-util/process-query-basic-streaming :csv query ["Created At: Day of month" "Count"])]
+ (is (seq result))
+ (testing "Results should include integer day-of-month values and counts"
+ (is (every? (fn [[day count]]
+ (and (string? day)
+ (string? count)
+ (let [day-int (parse-long day)]
+ (<= 1 day-int 31))))
+ (next result))))))))))
diff --git a/src/metabase/types/core.cljc b/src/metabase/types/core.cljc
index 5c6ced78637..cabe5d3ec3e 100644
--- a/src/metabase/types/core.cljc
+++ b/src/metabase/types/core.cljc
@@ -389,7 +389,9 @@
{:deprecated "0.57.0"}
[tyype :- :keyword
{base-type :base_type, effective-type :effective_type} :- ::snake-cased-type-info]
- (some #(isa? % tyype) [base-type effective-type]))
+ (if (nil? effective-type)
+ (isa? base-type tyype)
+ (isa? effective-type tyype)))
(mu/defn temporal-field?
"True if a Metabase `Field` instance has a temporal base or semantic type, i.e. if this Field represents a value