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
Don't double-convert expressions with convertTimezone() (#68740)
Eric Normandad8376f6ccfa
diff --git a/src/metabase/query_processor/middleware/format_rows.clj b/src/metabase/query_processor/middleware/format_rows.cljindex 7568425fc9c..d51adf97c62 100644--- a/src/metabase/query_processor/middleware/format_rows.clj+++ b/src/metabase/query_processor/middleware/format_rows.clj@@ -11,6 +11,8 @@(:import(java.time Instant LocalDate LocalDateTime LocalTime OffsetDateTime OffsetTime ZonedDateTime ZoneId)))+(set! *warn-on-reflection* true)+(p.types/defprotocol+ ^:private FormatValue"Protocol for determining how QP results of various classes are serialized. Drivers can add implementations to supportcustom driver types as needed."@@ -55,14 +57,34 @@(format-value [t timezone-id](t/format :iso-offset-date-time (u.date/with-time-zone-same-instant t timezone-id))))+(defn- strip-timezone [t]+ (cond+ (instance? OffsetDateTime t)+ (.toLocalDateTime ^OffsetDateTime t)++ (instance? ZonedDateTime t)+ (.toLocalDateTime ^ZonedDateTime t)++ :else+ t))+(defn- format-rows-xform [rf metadata]{:pre [(fn? rf)]}(log/debugf "Formatting rows with results timezone ID %s" (qp.timezone/results-timezone-id))- (let [timezone-id (t/zone-id (qp.timezone/results-timezone-id))- ;; a column will have `converted_timezone` metadata if it is the result of `convert-timezone` expression- ;; in that case, we'll format the results with the target timezone.- ;; Otherwise format it with results-timezone- cols-zone-id (perf/mapv #(t/zone-id (get % :converted_timezone timezone-id)) (:cols metadata))]+ (let [cols-converted-timezones (perf/mapv :converted_timezone (:cols metadata))+ timezone-id (t/zone-id (qp.timezone/results-timezone-id))+ format (fn [value converted-timezone]+ (cond-> value+ ;; For columns with converted_timezone, if the driver returns an+ ;; OffsetDateTime/ZonedDateTime, the value has already been timezone-converted+ ;; in SQL but the db may have re-cast it as timestamp with tz. We need to+ ;; strip the offset and treat it as a LocalDateTime so format-value attaches+ ;; the target zone instead of shifting from UTC. (#68712)+ converted-timezone strip-timezone+ ;; a column will have `converted_timezone` metadata if it is the result of `convert-timezone`+ ;; expression in that case, we'll format the results with the target timezone. Otherwise+ ;; format it with results-timezone+ true (format-value (t/zone-id (or converted-timezone timezone-id)))))](fn([](rf))@@ -71,7 +93,7 @@(rf result))([result row]- (rf result (perf/mapv format-value row cols-zone-id))))))+ (rf result (perf/mapv format row cols-converted-timezones))))))(defn format-rows"Format individual query result values as needed. Ex: format temporal values as ISO-8601 strings w/ timezone offset."diff --git a/test/metabase/query_processor/native_test.clj b/test/metabase/query_processor/native_test.cljindex e212e9d13d5..f9aeb6ed203 100644--- a/test/metabase/query_processor/native_test.clj+++ b/test/metabase/query_processor/native_test.clj@@ -2,8 +2,11 @@(:require[clojure.test :refer :all][metabase.lib.core :as lib]+ [metabase.lib.metadata :as lib.metadata]+ [metabase.lib.options :as lib.options][metabase.lib.test-util :as lib.tu][metabase.query-processor :as qp]+ [metabase.query-processor.date-time-zone-functions-test :as dt-fn-test]^{:clj-kondo/ignore [:deprecated-namespace]} [metabase.query-processor.store :as qp.store][metabase.query-processor.test-util :as qp.test-util][metabase.test :as mt]@@ -96,3 +99,30 @@:card-id 2}}}](is (= [["Gizmo" "Swaniawski, Casper and Hilll"]](mt/rows (qp/process-query (mt/native-query query))))))))))++(deftest convert-timezone-in-case-with-default-test+ (testing "convert-timezone inside case with a default value should not double-convert (#68712)"+ (mt/test-drivers (mt/normal-drivers-with-feature :convert-timezone)+ (mt/with-report-timezone-id! "UTC"+ (mt/dataset dt-fn-test/times-mixed+ (let [mp (mt/metadata-provider)+ query (lib/query mp (lib.metadata/table mp (mt/id :times)))+ dt-tz-col (lib.metadata/field mp (mt/id :times :dt_tz))+ index-col (lib.metadata/field mp (mt/id :times :index))+ convert-tz (fn [col]+ (lib.options/ensure-uuid+ [:convert-timezone {} col "Asia/Seoul"]))+ with-default (lib/case [[(lib/= index-col 1) (convert-tz dt-tz-col)]]+ dt-tz-col)+ without-default (lib/case [[(lib/= index-col 1) (convert-tz dt-tz-col)]])+ q (-> query+ (lib/expression "with-default" with-default)+ (lib/expression "without-default" without-default)+ (lib/filter (lib/= index-col 1))+ (as-> q (lib/with-fields q [index-col+ (lib/expression-ref q "with-default")+ (lib/expression-ref q "without-default")])))+ results (qp/process-query q)+ [_idx with-default-val without-default-val] (first (mt/rows results))]+ (testing "MBQL: case with and without default should produce the same result for matching rows"+ (is (= without-default-val with-default-val)))))))))