Skip to content
Snippets Groups Projects
Unverified Commit 356ddd59 authored by github-automation-metabase's avatar github-automation-metabase Committed by GitHub
Browse files

fix: add timezone to bigquery param substitution (#45661) (#45688)


* fix: add timezone to bigquery param substitution

Fixes #43597

* Fix indentation and remove report-timezone lookup from loop

Co-authored-by: default avatarCase Nelson <case@metabase.com>
parent 8c9bda5b
No related branches found
No related tags found
No related merge requests found
...@@ -936,10 +936,25 @@ ...@@ -936,10 +936,25 @@
result (parent-method driver field-filter)] result (parent-method driver field-filter)]
(cond-> result (cond-> result
field-temporal-type (update :prepared-statement-args (fn [args] field-temporal-type (update :prepared-statement-args (fn [args]
(for [arg args] (let [request-time-zone-id (qp.timezone/requested-timezone-id)]
(if (instance? java.time.temporal.Temporal arg) (map (fn [arg]
(->temporal-type field-temporal-type arg) (if (instance? java.time.temporal.Temporal arg)
arg))))))) ;; Since we add the zone as part of the
;; LHS of the filter, we need to add the zone to
;; the RHS as well.
(let [result (->temporal-type field-temporal-type arg)]
(cond
(or (not request-time-zone-id)
(not= :type/DateTimeWithLocalTZ (:base-type field)))
result
(instance? java.time.ZonedDateTime result)
(t/with-zone-same-instant result request-time-zone-id)
(instance? java.time.OffsetDateTime result)
(t/with-zone-same-instant (t/zoned-date-time result) request-time-zone-id)))
arg))
args)))))))
(defmethod sql.qp/cast-temporal-string [:bigquery-cloud-sdk :Coercion/ISO8601->DateTime] (defmethod sql.qp/cast-temporal-string [:bigquery-cloud-sdk :Coercion/ISO8601->DateTime]
[_driver _semantic_type expr] [_driver _semantic_type expr]
......
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
[metabase.test.data.bigquery-cloud-sdk :as bigquery.tx] [metabase.test.data.bigquery-cloud-sdk :as bigquery.tx]
[metabase.test.util.timezone :as test.tz] [metabase.test.util.timezone :as test.tz]
[metabase.util :as u] [metabase.util :as u]
[metabase.util.date-2 :as u.date]
[metabase.util.honey-sql-2 :as h2x] [metabase.util.honey-sql-2 :as h2x]
[toucan2.tools.with-temp :as t2.with-temp])) [toucan2.tools.with-temp :as t2.with-temp]))
...@@ -621,6 +622,55 @@ ...@@ -621,6 +622,55 @@
(is (= [[0]] (is (= [[0]]
(mt/rows (qp/process-query query))))))))))))) (mt/rows (qp/process-query query)))))))))))))
(deftest datetime-timezone-parameter-test
(testing "Date Field Filter not includes Timezone (#43597)"
(mt/test-driver
:bigquery-cloud-sdk
(mt/dataset
attempted-murders
(doseq [:let [expectations {["Europe/Oslo" :date "2020-01-09"] #t"2020-01-09",
["Europe/Oslo" :datetime "2020-01-09"] #t"2020-01-09T00:00",
["Europe/Oslo" :datetime "2020-01-09T01:03"] #t"2020-01-09T01:03",
["Europe/Oslo" :datetime_tz "2020-01-09"] #t"2020-01-09T01:00+01:00[Europe/Oslo]",
["Europe/Oslo" :datetime_tz "2020-01-09T01:03"] #t"2020-01-09T02:03+01:00[Europe/Oslo]",
["UTC" :date "2020-01-09"] #t"2020-01-09",
["UTC" :datetime "2020-01-09"] #t"2020-01-09T00:00",
["UTC" :datetime "2020-01-09T01:03"] #t"2020-01-09T01:03",
["UTC" :datetime_tz "2020-01-09"] #t"2020-01-09T00:00Z[UTC]",
["UTC" :datetime_tz "2020-01-09T01:03"] #t"2020-01-09T01:03Z[UTC]",
[nil :date "2020-01-09"] #t"2020-01-09"
[nil :datetime "2020-01-09"] #t"2020-01-09T00:00",
[nil :datetime "2020-01-09T01:03"] #t"2020-01-09T01:03",
[nil :datetime_tz "2020-01-09"] (t/offset-date-time (u.date/parse "2020-01-09T00:00Z"))
[nil :datetime_tz "2020-01-09T01:03"] #t"2020-01-09T01:03Z[UTC]"}]
tz [nil "Europe/Oslo" "UTC"]
field [:date :datetime :datetime_tz]
value (cond-> ["2020-01-09"]
(not= field :date)
(conj "2020-01-09T01:03"))]
(testing (format "With TZ %s: field: %s value: %s parsed: %s" tz field value (pr-str (u.date/parse value)))
(mt/with-report-timezone-id!
tz
(let [expected (get expectations [tz field value])
value-type :date/single
query {:database (mt/id)
:type :native
:native {:query (str "SELECT count(*)\n"
(format "FROM `%s.attempts`\n"
(bigquery.tx/test-dataset-id "attempted_murders"))
"WHERE {{d}}")
:template-tags {"d" {:name "d"
:display-name "Date"
:type :dimension
:widget-type :date/all-options
:dimension [:field (mt/id :attempts field) nil]}}}
:parameters [{:type value-type
:name "d"
:target [:dimension [:template-tag "d"]]
:value value}]}]
(is (= [expected] (:params (qp.compile/compile query))))))))))))
(deftest current-datetime-honeysql-form-test (deftest current-datetime-honeysql-form-test
(mt/test-driver :bigquery-cloud-sdk (mt/test-driver :bigquery-cloud-sdk
(qp.store/with-metadata-provider (mt/id) (qp.store/with-metadata-provider (mt/id)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment