Skip to content

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

Add test showing that bucketed breakouts truncate properly (#75811)

metambena5287f0238b4
diff --git a/modules/drivers/mongo/src/metabase/driver/mongo/query_processor.clj b/modules/drivers/mongo/src/metabase/driver/mongo/query_processor.clj
index c751587c7b6..54524a432af 100644
--- a/modules/drivers/mongo/src/metabase/driver/mongo/query_processor.clj
+++ b/modules/drivers/mongo/src/metabase/driver/mongo/query_processor.clj
@@ -216,10 +216,12 @@
x)
(defmethod ->rvalue :expression
- [[_ expression-name]]
- (let [expression-value (driver-api/expression-with-name (:query *query*) expression-name)]
- (cond->> (->rvalue expression-value)
- (driver-api/is-clause? :value expression-value) (array-map $literal))))
+ [[_ expression-name {:keys [temporal-unit]}]]
+ (let [expression-value (driver-api/expression-with-name (:query *query*) expression-name)
+ rvalue (cond->> (->rvalue expression-value)
+ (driver-api/is-clause? :value expression-value) (array-map $literal))]
+ (cond-> rvalue
+ temporal-unit (with-rvalue-temporal-bucketing temporal-unit))))
(def ^:private base64-decoder "
function(bin) {
diff --git a/test/metabase/query_processor/date_time_zone_functions_test.clj b/test/metabase/query_processor/date_time_zone_functions_test.clj
index 4df8bc6bf0f..34d3a907929 100644
--- a/test/metabase/query_processor/date_time_zone_functions_test.clj
+++ b/test/metabase/query_processor/date_time_zone_functions_test.clj
@@ -464,6 +464,26 @@
(testing (format "%s %s function works as expected on %s column for driver %s" op unit col-type driver/*driver*)
(is (= (set expected) (set (test-datetime-math query)))))))))))
+(deftest ^:parallel datetime-arithmetic-expression-temporal-bucket-in-breakout-test
+ (testing "Breaking out by a datetime-arithmetic expression with a `:temporal-unit` truncates the value (#27808)"
+ (mt/test-drivers (mt/normal-drivers-with-feature :date-arithmetics)
+ (mt/dataset times-mixed
+ (let [mp (mt/metadata-provider)
+ times (lib.metadata/table mp (mt/id :times))
+ dt (lib.metadata/field mp (mt/id :times :dt))
+ base (-> (lib/query mp times)
+ (lib/expression "shifted" (lib/datetime-subtract dt 2 :year)))
+ query (-> base
+ (lib/aggregate (lib/count))
+ (lib/breakout (-> (lib/expression-ref base "shifted")
+ (lib/with-temporal-bucket :year))))]
+ (is (= #{["2002-01-01T00:00:00Z" 1]
+ ["2006-01-01T00:00:00Z" 1]
+ ["2010-01-01T00:00:00Z" 2]}
+ (set (mt/formatted-rows
+ [u.date/temporal-str->iso8601-str int]
+ (qp/process-query query))))))))))
+
(deftest temporal-arithmetic-with-literal-date-test
(mt/with-temporary-setting-values [start-of-week :sunday
report-timezone "UTC"]