Skip to content

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

Cast date arithmetic back to original type for aggregation (#85082)

Riley Thompson1070e6733518
diff --git a/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk/query_processor.clj b/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk/query_processor.clj
index 30354320a17..6bff57eec54 100644
--- a/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk/query_processor.clj
+++ b/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk/query_processor.clj
@@ -925,6 +925,20 @@
driver
clause))))
+(defmethod sql.qp/->honeysql [:bigquery-cloud-sdk :case]
+ [driver clause]
+ ;; Bigquery requires all case expressions to have the same type, so coerce all temporal expressions to the same type
+ (let [[tag & args :as form] ((get-method sql.qp/->honeysql [:sql :case]) driver clause)]
+ (if-let [target-type (temporal-type form)]
+ (into [tag]
+ (map-indexed (fn [i arg]
+ (let [arg-type (when (odd? i) (temporal-type arg))]
+ (if (and arg-type (not= arg-type target-type))
+ (->temporal-type target-type arg)
+ arg))))
+ args)
+ form)))
+
;;; +----------------------------------------------------------------------------------------------------------------+
;;; | Other Driver / SQLDriver Method Implementations |
;;; +----------------------------------------------------------------------------------------------------------------+
@@ -990,13 +1004,10 @@
(defmethod sql.qp/add-interval-honeysql-form :bigquery-cloud-sdk
[_ hsql-form amount unit]
- ;; `timestamp_add()` doesn't support month/quarter/year, so cast it to `datetime` so we can use `datetime_add()`
- ;; instead in those cases.
- (let [hsql-form (cond->> hsql-form
- (and (= (temporal-type hsql-form) :timestamp)
- (not (contains? (temporal-type->supported-units :timestamp) unit)))
- (h2x/cast :datetime))]
- (add-interval-form hsql-form amount unit)))
+ ;; `timestamp_add()` doesn't support month/quarter/year, `add-interval-form` casts it to
+ ;; `datetime` so we can use `datetime_add()` instead in those cases. We then cast it back
+ ;; to the original temporal type if needed.
+ (->temporal-type (temporal-type hsql-form) (add-interval-form hsql-form amount unit)))
(defmethod driver/mbql->native :bigquery-cloud-sdk
[driver outer-query]
diff --git a/modules/drivers/oracle/src/metabase/driver/oracle.clj b/modules/drivers/oracle/src/metabase/driver/oracle.clj
index 6dbf4cffbbe..e9bc2ebc45e 100644
--- a/modules/drivers/oracle/src/metabase/driver/oracle.clj
+++ b/modules/drivers/oracle/src/metabase/driver/oracle.clj
@@ -520,8 +520,13 @@
(h2x/cast-unless-type-in "date" (conj timestamp-types "date") hsql-form))
(defn- add-months [hsql-form amount]
- (-> [:add_months (cast-to-date-if-needed hsql-form) amount]
- (h2x/with-database-type-info "date")))
+ ;; `add_months()` returns a `date`, which has no time zone. Convert a timestamp with time zone to the results
+ ;; timezone first, so the months are added in that timezone.
+ (let [hsql-form (cond-> hsql-form
+ (h2x/is-of-type? hsql-form #"(?i)timestamp(\(\d\))? with time zone")
+ (h2x/at-time-zone (driver-api/results-timezone-id)))]
+ (-> [:add_months (cast-to-date-if-needed hsql-form) amount]
+ (h2x/with-database-type-info "date"))))
(defmethod sql.qp/add-interval-honeysql-form :oracle
[driver hsql-form amount unit]
diff --git a/modules/drivers/presto-jdbc/src/metabase/driver/presto_jdbc.clj b/modules/drivers/presto-jdbc/src/metabase/driver/presto_jdbc.clj
index d7269860913..244f5551079 100644
--- a/modules/drivers/presto-jdbc/src/metabase/driver/presto_jdbc.clj
+++ b/modules/drivers/presto-jdbc/src/metabase/driver/presto_jdbc.clj
@@ -373,7 +373,10 @@
db-type (h2x/type-info->db-type type-info)]
(if (and ;; AT TIME ZONE is only valid on these Presto types; if applied to something else (ex: `date`), then
;; an error will be thrown by the query analyzer
- (contains? #{"timestamp" "timestamp with time zone" "time" "time with time zone"} db-type)
+ (if db-type
+ (contains? #{"timestamp" "timestamp with time zone" "time" "time with time zone"} db-type)
+ ;; the database type is lost when `expr` comes from a nested query, e.g. a custom column
+ (isa? (h2x/effective-type expr) :type/DateTimeWithTZ))
;; if one has already been set, don't do so again
(not (::in-report-zone? (meta expr)))
report-zone)
diff --git a/modules/drivers/snowflake/src/metabase/driver/snowflake.clj b/modules/drivers/snowflake/src/metabase/driver/snowflake.clj
index 3720fb37110..f3c3197461f 100644
--- a/modules/drivers/snowflake/src/metabase/driver/snowflake.clj
+++ b/modules/drivers/snowflake/src/metabase/driver/snowflake.clj
@@ -673,7 +673,7 @@
[expr]
(let [report-timezone (driver-api/report-timezone-id-if-supported)]
(if (and report-timezone
- (= (h2x/database-type expr) "timestamptz"))
+ (h2x/database-or-effective-type-isa? expr "timestamptz" :type/DateTimeWithLocalTZ))
[:to_timestamp_ltz expr]
expr)))
diff --git a/modules/drivers/starburst/src/metabase/driver/starburst.clj b/modules/drivers/starburst/src/metabase/driver/starburst.clj
index bf1e40aa19e..d4e2cdf1910 100644
--- a/modules/drivers/starburst/src/metabase/driver/starburst.clj
+++ b/modules/drivers/starburst/src/metabase/driver/starburst.clj
@@ -210,8 +210,10 @@
db-type (h2x/type-info->db-type type-info)]
(if (and ;; AT TIME ZONE is only valid on these starburst types; if applied to something else (ex: `date`), then
;; an error will be thrown by the query analyzer
- db-type
- (re-find #"(?i)^time(?:stamp)?(?:\(\d+\))?(?: with time zone)?$" db-type)
+ (if db-type
+ (re-find #"(?i)^time(?:stamp)?(?:\(\d+\))?(?: with time zone)?$" db-type)
+ ;; the database type is lost when `expr` comes from a nested query, e.g. a custom column
+ (isa? (h2x/effective-type expr) :type/DateTimeWithTZ))
;; if one has already been set, don't do so again
(not (::in-report-zone? (meta expr)))
report-zone)
diff --git a/test/metabase/query_processor/expression_aggregations_test.clj b/test/metabase/query_processor/expression_aggregations_test.clj
index b9fce380db0..0f0d0967e20 100644
--- a/test/metabase/query_processor/expression_aggregations_test.clj
+++ b/test/metabase/query_processor/expression_aggregations_test.clj
@@ -420,3 +420,19 @@
[120 317763.17]
[140 128488.72]]
(mt/formatted-rows [int 2.0] (qp/process-query query))))))))
+
+(deftest aggregate-datetime-add-on-timestamp-test
+ (mt/test-drivers (mt/normal-drivers-with-feature :expressions :date-arithmetics :set-timezone
+ :test/dynamic-dataset-loading)
+ (mt/dataset attempted-murders-no-time
+ (mt/with-report-timezone-id! "Pacific/Honolulu"
+ (let [mp (mt/metadata-provider)
+ attempts (lib.metadata/table mp (mt/id :attempts))
+ datetime-tz (lib.metadata/field mp (mt/id :attempts :datetime_tz))
+ query (as-> (lib/query mp attempts) $
+ (lib/expression $ "shifted" (lib/datetime-add datetime-tz 12 :month))
+ (lib/aggregate $ (lib/count))
+ (lib/breakout $ (lib/with-temporal-bucket (lib/expression-ref $ "shifted") :month)))]
+ (is (= [["2020-10-01T00:00:00-10:00" 1]
+ ["2020-11-01T00:00:00-10:00" 19]]
+ (mt/formatted-rows [str int] (qp/process-query query)))))))))