Skip to content
Snippets Groups Projects
Commit c716769d authored by Allen Gilliland's avatar Allen Gilliland
Browse files

make use or the :report-timezone in the newly applied query :settings instead...

make use or the :report-timezone in the newly applied query :settings instead of having lots of different code look up the value itself.  update a few unit tests to account for the fact that query results are now converting temporal values to strings.
parent 299eb54d
No related branches found
No related tags found
No related merge requests found
......@@ -17,7 +17,7 @@
(defn process-and-run
"Process and run a native (raw SQL) QUERY."
[driver {{sql :query} :native, database-id :database, :as query}]
[driver {{sql :query} :native, database-id :database, settings :settings, :as query}]
(try (let [database (sel :one :fields [Database :engine :details] :id database-id)
db-conn (sql/db->jdbc-connection-spec database)]
......@@ -27,9 +27,8 @@
(.setAutoCommit jdbc-connection false)
(try
;; Set the timezone if applicable
(when-let [timezone (driver/report-timezone)]
(when (and (seq timezone)
(contains? (driver/features driver) :set-timezone))
(when-let [timezone (:report-timezone settings)]
(when (seq timezone)
(log/debug (u/format-color 'green "%s" (sql/set-timezone-sql driver)))
(try (jdbc/db-do-prepared t-conn (sql/set-timezone-sql driver) [timezone])
(catch Throwable e
......
......@@ -251,9 +251,9 @@
(recur korma-form more)
korma-form))))
(defn- do-with-timezone [driver f]
(defn- do-with-timezone [driver timezone f]
(log/debug (u/format-color 'blue (sql/set-timezone-sql driver)))
(try (kdb/transaction (k/exec-raw [(sql/set-timezone-sql driver) [(driver/report-timezone)]])
(try (kdb/transaction (k/exec-raw [(sql/set-timezone-sql driver) [timezone]])
(f))
(catch Throwable e
(log/error (u/format-color 'red "Failed to set timezone:\n%s"
......@@ -279,16 +279,15 @@
(defn process-structured
"Convert QUERY into a korma `select` form, execute it, and annotate the results."
[driver {{:keys [source-table]} :query, database :database, :as outer-query}]
(let [set-timezone? (and (seq (driver/report-timezone))
(contains? (driver/features driver) :set-timezone))
entity ((resolve 'metabase.driver.generic-sql/korma-entity) database source-table)
[driver {{:keys [source-table]} :query, database :database, settings :settings, :as outer-query}]
(let [timezone (:report-timezone settings)
entity ((resolve 'metabase.driver.generic-sql/korma-entity) database source-table)
korma-form (build-korma-form driver outer-query entity)
f (partial k/exec korma-form)
f (fn []
(kdb/with-db (:db entity)
(if set-timezone?
(do-with-timezone driver f)
(if (seq timezone)
(do-with-timezone driver timezone f)
(f))))]
(log-korma-form korma-form)
(do-with-try-catch f)))
......@@ -113,7 +113,7 @@
(defn- pre-add-settings [qp]
(fn [{:keys [driver] :as query}]
(let [settings (->> {:report_timezone (when (driver/driver-supports? driver :set-timezone)
(let [settings (->> {:report-timezone (when (driver/driver-supports? driver :set-timezone)
(setting/get :report-timezone))}
(filter (comp not nil? val))
(into {}))]
......
......@@ -37,17 +37,17 @@
;; post-format-rows
(expect
[["2011-04-18T10:12:47.232Z"]
["2011-04-18T00:00:00.000Z"]
["2011-04-18T10:12:47.232Z"]]
{:rows [["2011-04-18T10:12:47.232Z"]
["2011-04-18T00:00:00.000Z"]
["2011-04-18T10:12:47.232Z"]]}
((post-format-rows (constantly {:rows [[(tc/to-sql-time 1303121567232)]
[(tc/to-sql-date "2011-04-18")] ; joda-time assumes this is UTC time when parsing it
[(tc/to-date 1303121567232)]]})) {:settings {}}))
(expect
[["2011-04-18T19:12:47.232+09:00"]
["2011-04-18T09:00:00.000+09:00"]
["2011-04-18T19:12:47.232+09:00"]]
{:rows [["2011-04-18T19:12:47.232+09:00"]
["2011-04-18T09:00:00.000+09:00"]
["2011-04-18T19:12:47.232+09:00"]]}
((post-format-rows (constantly {:rows [[(tc/to-sql-time 1303121567232)]
[(tc/to-sql-date "2011-04-18")] ; joda-time assumes this is UTC time when parsing it
[(tc/to-date 1303121567232)]]})) {:settings {:report-timezone "Asia/Tokyo"}}))
......@@ -885,29 +885,29 @@
;; SQL Server, Mongo, and Redshift don't have a concept of timezone so results are all grouped by UTC
(i/has-questionable-timezone-support? *data-loader*)
[[#inst "2015-06-01T07" 6]
[#inst "2015-06-02T07" 10]
[#inst "2015-06-03T07" 4]
[#inst "2015-06-04T07" 9]
[#inst "2015-06-05T07" 9]
[#inst "2015-06-06T07" 8]
[#inst "2015-06-07T07" 8]
[#inst "2015-06-08T07" 9]
[#inst "2015-06-09T07" 7]
[#inst "2015-06-10T07" 9]]
[["2015-06-01T07:00:00.000Z" 6]
["2015-06-02T07:00:00.000Z" 10]
["2015-06-03T07:00:00.000Z" 4]
["2015-06-04T07:00:00.000Z" 9]
["2015-06-05T07:00:00.000Z" 9]
["2015-06-06T07:00:00.000Z" 8]
["2015-06-07T07:00:00.000Z" 8]
["2015-06-08T07:00:00.000Z" 9]
["2015-06-09T07:00:00.000Z" 7]
["2015-06-10T07:00:00.000Z" 9]]
;; Postgres, MySQL, and H2 -- grouped by DB timezone, US/Pacific in this case
:else
[[#inst "2015-06-01T07" 8]
[#inst "2015-06-02T07" 9]
[#inst "2015-06-03T07" 9]
[#inst "2015-06-04T07" 4]
[#inst "2015-06-05T07" 11]
[#inst "2015-06-06T07" 8]
[#inst "2015-06-07T07" 6]
[#inst "2015-06-08T07" 10]
[#inst "2015-06-09T07" 6]
[#inst "2015-06-10T07" 10]])
[["2015-06-01T07:00:00.000Z" 8]
["2015-06-02T07:00:00.000Z" 9]
["2015-06-03T07:00:00.000Z" 9]
["2015-06-04T07:00:00.000Z" 4]
["2015-06-05T07:00:00.000Z" 11]
["2015-06-06T07:00:00.000Z" 8]
["2015-06-07T07:00:00.000Z" 6]
["2015-06-08T07:00:00.000Z" 10]
["2015-06-09T07:00:00.000Z" 6]
["2015-06-10T07:00:00.000Z" 10]])
(->> (dataset sad-toucan-incidents
(run-query incidents
(ql/aggregation (ql/count))
......@@ -1233,16 +1233,16 @@
(expect-with-non-timeseries-dbs
(cond
(contains? #{:redshift :sqlserver :bigquery} *engine*)
[[#inst "2015-06-01T17:31" 1]
[#inst "2015-06-01T23:06" 1]
[#inst "2015-06-02T00:23" 1]
[#inst "2015-06-02T01:55" 1]
[#inst "2015-06-02T04:04" 1]
[#inst "2015-06-02T04:19" 1]
[#inst "2015-06-02T09:13" 1]
[#inst "2015-06-02T12:37" 1]
[#inst "2015-06-02T15:20" 1]
[#inst "2015-06-02T18:11" 1]]
[["2015-06-01T17:31:00.000Z" 1]
["2015-06-01T23:06:00.000Z" 1]
["2015-06-02T00:23:00.000Z" 1]
["2015-06-02T01:55:00.000Z" 1]
["2015-06-02T04:04:00.000Z" 1]
["2015-06-02T04:19:00.000Z" 1]
["2015-06-02T09:13:00.000Z" 1]
["2015-06-02T12:37:00.000Z" 1]
["2015-06-02T15:20:00.000Z" 1]
["2015-06-02T18:11:00.000Z" 1]]
(= *engine* :sqlite)
[["2015-06-01 10:31:00" 1]
......@@ -1257,16 +1257,16 @@
["2015-06-02 11:11:00" 1]]
:else
[[#inst "2015-06-01T10:31" 1]
[#inst "2015-06-01T16:06" 1]
[#inst "2015-06-01T17:23" 1]
[#inst "2015-06-01T18:55" 1]
[#inst "2015-06-01T21:04" 1]
[#inst "2015-06-01T21:19" 1]
[#inst "2015-06-02T02:13" 1]
[#inst "2015-06-02T05:37" 1]
[#inst "2015-06-02T08:20" 1]
[#inst "2015-06-02T11:11" 1]])
[["2015-06-01T10:31:00.000Z" 1]
["2015-06-01T16:06:00.000Z" 1]
["2015-06-01T17:23:00.000Z" 1]
["2015-06-01T18:55:00.000Z" 1]
["2015-06-01T21:04:00.000Z" 1]
["2015-06-01T21:19:00.000Z" 1]
["2015-06-02T02:13:00.000Z" 1]
["2015-06-02T05:37:00.000Z" 1]
["2015-06-02T08:20:00.000Z" 1]
["2015-06-02T11:11:00.000Z" 1]])
(sad-toucan-incidents-with-bucketing :default))
(expect-with-non-timeseries-dbs
......@@ -1284,28 +1284,28 @@
["2015-06-02 11:11:00" 1]]
(i/has-questionable-timezone-support? *data-loader*)
[[#inst "2015-06-01T17:31" 1]
[#inst "2015-06-01T23:06" 1]
[#inst "2015-06-02T00:23" 1]
[#inst "2015-06-02T01:55" 1]
[#inst "2015-06-02T04:04" 1]
[#inst "2015-06-02T04:19" 1]
[#inst "2015-06-02T09:13" 1]
[#inst "2015-06-02T12:37" 1]
[#inst "2015-06-02T15:20" 1]
[#inst "2015-06-02T18:11" 1]]
[["2015-06-01T17:31:00.000Z" 1]
["2015-06-01T23:06:00.000Z" 1]
["2015-06-02T00:23:00.000Z" 1]
["2015-06-02T01:55:00.000Z" 1]
["2015-06-02T04:04:00.000Z" 1]
["2015-06-02T04:19:00.000Z" 1]
["2015-06-02T09:13:00.000Z" 1]
["2015-06-02T12:37:00.000Z" 1]
["2015-06-02T15:20:00.000Z" 1]
["2015-06-02T18:11:00.000Z" 1]]
:else
[[#inst "2015-06-01T10:31" 1]
[#inst "2015-06-01T16:06" 1]
[#inst "2015-06-01T17:23" 1]
[#inst "2015-06-01T18:55" 1]
[#inst "2015-06-01T21:04" 1]
[#inst "2015-06-01T21:19" 1]
[#inst "2015-06-02T02:13" 1]
[#inst "2015-06-02T05:37" 1]
[#inst "2015-06-02T08:20" 1]
[#inst "2015-06-02T11:11" 1]])
[["2015-06-01T10:31:00.000Z" 1]
["2015-06-01T16:06:00.000Z" 1]
["2015-06-01T17:23:00.000Z" 1]
["2015-06-01T18:55:00.000Z" 1]
["2015-06-01T21:04:00.000Z" 1]
["2015-06-01T21:19:00.000Z" 1]
["2015-06-02T02:13:00.000Z" 1]
["2015-06-02T05:37:00.000Z" 1]
["2015-06-02T08:20:00.000Z" 1]
["2015-06-02T11:11:00.000Z" 1]])
(sad-toucan-incidents-with-bucketing :minute))
(expect-with-non-timeseries-dbs
......@@ -1336,28 +1336,28 @@
["2015-06-02 13:00:00" 1]]
(i/has-questionable-timezone-support? *data-loader*)
[[#inst "2015-06-01T17" 1]
[#inst "2015-06-01T23" 1]
[#inst "2015-06-02T00" 1]
[#inst "2015-06-02T01" 1]
[#inst "2015-06-02T04" 2]
[#inst "2015-06-02T09" 1]
[#inst "2015-06-02T12" 1]
[#inst "2015-06-02T15" 1]
[#inst "2015-06-02T18" 1]
[#inst "2015-06-02T20" 1]]
[["2015-06-01T17:00:00.000Z" 1]
["2015-06-01T23:00:00.000Z" 1]
["2015-06-02T00:00:00.000Z" 1]
["2015-06-02T01:00:00.000Z" 1]
["2015-06-02T04:00:00.000Z" 2]
["2015-06-02T09:00:00.000Z" 1]
["2015-06-02T12:00:00.000Z" 1]
["2015-06-02T15:00:00.000Z" 1]
["2015-06-02T18:00:00.000Z" 1]
["2015-06-02T20:00:00.000Z" 1]]
:else
[[#inst "2015-06-01T10" 1]
[#inst "2015-06-01T16" 1]
[#inst "2015-06-01T17" 1]
[#inst "2015-06-01T18" 1]
[#inst "2015-06-01T21" 2]
[#inst "2015-06-02T02" 1]
[#inst "2015-06-02T05" 1]
[#inst "2015-06-02T08" 1]
[#inst "2015-06-02T11" 1]
[#inst "2015-06-02T13" 1]])
[["2015-06-01T10:00:00.000Z" 1]
["2015-06-01T16:00:00.000Z" 1]
["2015-06-01T17:00:00.000Z" 1]
["2015-06-01T18:00:00.000Z" 1]
["2015-06-01T21:00:00.000Z" 2]
["2015-06-02T02:00:00.000Z" 1]
["2015-06-02T05:00:00.000Z" 1]
["2015-06-02T08:00:00.000Z" 1]
["2015-06-02T11:00:00.000Z" 1]
["2015-06-02T13:00:00.000Z" 1]])
(sad-toucan-incidents-with-bucketing :hour))
(expect-with-non-timeseries-dbs
......@@ -1381,28 +1381,28 @@
["2015-06-10" 9]]
(i/has-questionable-timezone-support? *data-loader*)
[[#inst "2015-06-01T07" 6]
[#inst "2015-06-02T07" 10]
[#inst "2015-06-03T07" 4]
[#inst "2015-06-04T07" 9]
[#inst "2015-06-05T07" 9]
[#inst "2015-06-06T07" 8]
[#inst "2015-06-07T07" 8]
[#inst "2015-06-08T07" 9]
[#inst "2015-06-09T07" 7]
[#inst "2015-06-10T07" 9]]
[["2015-06-01T07:00:00.000Z" 6]
["2015-06-02T07:00:00.000Z" 10]
["2015-06-03T07:00:00.000Z" 4]
["2015-06-04T07:00:00.000Z" 9]
["2015-06-05T07:00:00.000Z" 9]
["2015-06-06T07:00:00.000Z" 8]
["2015-06-07T07:00:00.000Z" 8]
["2015-06-08T07:00:00.000Z" 9]
["2015-06-09T07:00:00.000Z" 7]
["2015-06-10T07:00:00.000Z" 9]]
:else
[[#inst "2015-06-01T07" 8]
[#inst "2015-06-02T07" 9]
[#inst "2015-06-03T07" 9]
[#inst "2015-06-04T07" 4]
[#inst "2015-06-05T07" 11]
[#inst "2015-06-06T07" 8]
[#inst "2015-06-07T07" 6]
[#inst "2015-06-08T07" 10]
[#inst "2015-06-09T07" 6]
[#inst "2015-06-10T07" 10]])
[["2015-06-01T07:00:00.000Z" 8]
["2015-06-02T07:00:00.000Z" 9]
["2015-06-03T07:00:00.000Z" 9]
["2015-06-04T07:00:00.000Z" 4]
["2015-06-05T07:00:00.000Z" 11]
["2015-06-06T07:00:00.000Z" 8]
["2015-06-07T07:00:00.000Z" 6]
["2015-06-08T07:00:00.000Z" 10]
["2015-06-09T07:00:00.000Z" 6]
["2015-06-10T07:00:00.000Z" 10]])
(sad-toucan-incidents-with-bucketing :day))
(expect-with-non-timeseries-dbs
......@@ -1433,18 +1433,18 @@
["2015-06-28" 7]]
(i/has-questionable-timezone-support? *data-loader*)
[[#inst "2015-05-31T07" 46]
[#inst "2015-06-07T07" 47]
[#inst "2015-06-14T07" 40]
[#inst "2015-06-21T07" 60]
[#inst "2015-06-28T07" 7]]
[["2015-05-31T07:00:00.000Z" 46]
["2015-06-07T07:00:00.000Z" 47]
["2015-06-14T07:00:00.000Z" 40]
["2015-06-21T07:00:00.000Z" 60]
["2015-06-28T07:00:00.000Z" 7]]
:else
[[#inst "2015-05-31T07" 49]
[#inst "2015-06-07T07" 47]
[#inst "2015-06-14T07" 39]
[#inst "2015-06-21T07" 58]
[#inst "2015-06-28T07" 7]])
[["2015-05-31T07:00:00.000Z" 49]
["2015-06-07T07:00:00.000Z" 47]
["2015-06-14T07:00:00.000Z" 39]
["2015-06-21T07:00:00.000Z" 58]
["2015-06-28T07:00:00.000Z" 7]])
(sad-toucan-incidents-with-bucketing :week))
(expect-with-non-timeseries-dbs
......@@ -1461,7 +1461,7 @@
(sad-toucan-incidents-with-bucketing :week-of-year))
(expect-with-non-timeseries-dbs
[[(if (= *engine* :sqlite) "2015-06-01", #inst "2015-06-01T07") 200]]
[[(if (= *engine* :sqlite) "2015-06-01", "2015-06-01T07:00:00.000Z") 200]]
(sad-toucan-incidents-with-bucketing :month))
(expect-with-non-timeseries-dbs
......@@ -1469,7 +1469,7 @@
(sad-toucan-incidents-with-bucketing :month-of-year))
(expect-with-non-timeseries-dbs
[[(if (= *engine* :sqlite) "2015-04-01", #inst "2015-04-01T07") 200]]
[[(if (= *engine* :sqlite) "2015-04-01", "2015-04-01T07:00:00.000Z") 200]]
(sad-toucan-incidents-with-bucketing :quarter))
(expect-with-non-timeseries-dbs
......
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