Skip to content

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

Compile current time in filters the converted timezone for Postgres (#80244)

metamben87841b3fe69a
diff --git a/src/metabase/driver/postgres.clj b/src/metabase/driver/postgres.clj
index fe2bba0116a..682a50c7187 100644
--- a/src/metabase/driver/postgres.clj
+++ b/src/metabase/driver/postgres.clj
@@ -556,8 +556,8 @@
:else
(let [expr' (h2x/->pg-timestamp expr)]
- (-> [:date_trunc (h2x/literal unit) expr']
- (h2x/with-database-type-info (h2x/database-type expr'))))))
+ (cond-> [:date_trunc (h2x/literal unit) expr']
+ (h2x/type-info expr') (h2x/with-type-info (h2x/type-info expr'))))))
(defn- extract-from-timestamp [unit expr]
(extract unit (h2x/->pg-timestamp expr)))
@@ -628,7 +628,30 @@
expr [:timezone target-timezone (if (not timestamptz?)
[:timezone source-timezone expr]
expr)]]
- (h2x/with-database-type-info expr "timestamp")))
+ (h2x/with-type-info expr {:database-type "timestamp"
+ ::target-timezone target-timezone})))
+
+(defn- current-datetime-in-parent-lhs-timezone
+ "Return a HoneySQL form for the current datetime that shares the wall-clock frame of the enclosing filter's LHS.
+ When the LHS is a convertTimezone expression targeting `target-tz`, wrap the driver's default NOW() with
+ `TIMEZONE(target-tz, ...)` so both sides compare as plain timestamps in the same zone (#80155). Otherwise
+ return the driver's default unchanged."
+ [driver]
+ (let [now (sql.qp/current-datetime-honeysql-form driver)]
+ (if-let [target-tz (::target-timezone sql.qp/*parent-honeysql-col-type-info*)]
+ (h2x/with-database-type-info [:timezone target-tz now] "timestamp")
+ now)))
+
+(defmethod sql.qp/->honeysql [:postgres :relative-datetime]
+ [driver [_ _opts amount unit]]
+ (let [now (current-datetime-in-parent-lhs-timezone driver)]
+ (sql.qp/date driver unit (if (zero? amount)
+ now
+ (sql.qp/add-interval-honeysql-form driver now amount unit)))))
+
+(defmethod sql.qp/->honeysql [:postgres :now]
+ [driver _clause]
+ (current-datetime-in-parent-lhs-timezone driver))
(defmethod sql.qp/->honeysql [:postgres :value]
[driver [_ {:keys [base-type database-type] :as opts} raw-value]]
diff --git a/src/metabase/driver/sql/query_processor.clj b/src/metabase/driver/sql/query_processor.clj
index dabdab21957..e5cf0b46483 100644
--- a/src/metabase/driver/sql/query_processor.clj
+++ b/src/metabase/driver/sql/query_processor.clj
@@ -1843,39 +1843,36 @@
[:effective-type])))
(def ^:dynamic *parent-honeysql-col-type-info*
- "To be bound in `->honeysql <driver> <op>` where op is on of {:>, :>=, :<, :<=, :=, :between}`. Its value should be
- `{:base-type keyword? :database-type string?}`. The value is used in `->honeysql <driver> :relative-datetime`,
- the :snowflake implementation at the time of writing, and later in the [[metabase.query-processor.util.relative-datetime/maybe-cacheable-relative-datetime-honeysql]]
- to determine (1) format of server-side generated sql temporal string and (2) the database type it should be cast to."
+ "To be bound in `->honeysql <driver> <op>` where op is on of {:>, :>=, :<, :<=, :=, :between}`. It carries the
+ compiled LHS's full HoneySQL type-info (see [[metabase.util.honey-sql-2/type-info]]) merged with `:base-type` and
+ `:effective-type` from the MBQL field clause. Driver-specific keys (e.g. Postgres's `::target-timezone` on a
+ `convertTimezone` output) round-trip through this map."
nil)
+(defn- parent-honeysql-col-type-info
+ [field field-honeysql]
+ (merge (h2x/type-info field-honeysql)
+ (parent-honeysql-col-effective-type-map field)
+ (parent-honeysql-col-base-type-map field)))
+
(defmethod ->honeysql [:sql :between]
[driver [_ _opts field min-val max-val]]
(let [field-honeysql (->honeysql driver field)]
- (binding [*parent-honeysql-col-type-info* (merge (when-let [database-type (h2x/database-type field-honeysql)]
- {:database-type database-type})
- (parent-honeysql-col-effective-type-map field)
- (parent-honeysql-col-base-type-map field))]
+ (binding [*parent-honeysql-col-type-info* (parent-honeysql-col-type-info field field-honeysql)]
[:between field-honeysql (->honeysql driver min-val) (->honeysql driver max-val)])))
(doseq [operator [:> :>= :< :<=]]
(defmethod ->honeysql [:sql operator] ; [:> :>= :< :<=] -- For grep.
[driver [_ _opts field value]]
(let [field-honeysql (->honeysql driver field)]
- (binding [*parent-honeysql-col-type-info* (merge (when-let [database-type (h2x/database-type field-honeysql)]
- {:database-type database-type})
- (parent-honeysql-col-effective-type-map field)
- (parent-honeysql-col-base-type-map field))]
+ (binding [*parent-honeysql-col-type-info* (parent-honeysql-col-type-info field field-honeysql)]
[operator field-honeysql (->honeysql driver value)]))))
(defmethod ->honeysql [:sql :=]
[driver [_ _opts field value]]
(assert (some? field))
(let [field-honeysql (->honeysql driver (maybe-cast-uuid-for-equality driver field value))]
- (binding [*parent-honeysql-col-type-info* (merge (when-let [database-type (h2x/database-type field-honeysql)]
- {:database-type database-type})
- (parent-honeysql-col-effective-type-map field)
- (parent-honeysql-col-base-type-map field))]
+ (binding [*parent-honeysql-col-type-info* (parent-honeysql-col-type-info field field-honeysql)]
[:= field-honeysql (->honeysql driver value)])))
(defn- correct-null-behaviour
diff --git a/test/metabase/driver/postgres_test.clj b/test/metabase/driver/postgres_test.clj
index 4568e4e2323..f42844960cd 100644
--- a/test/metabase/driver/postgres_test.clj
+++ b/test/metabase/driver/postgres_test.clj
@@ -40,6 +40,7 @@
[metabase.lib.test-metadata :as meta]
[metabase.lib.test-util :as lib.tu]
[metabase.lib.test-util.metadata-providers.mock :as providers.mock]
+ [metabase.lib.test-util.notebook-helpers :as lib.tu.notebook]
[metabase.notification.payload.temp-storage :as temp-storage]
[metabase.query-processor.compile :as qp.compile]
[metabase.query-processor.error-type :as qp.error-type]
@@ -1757,6 +1758,88 @@
(-> (qp.compile/compile query)
(update :query #(str/split-lines (driver/prettify-native-form :postgres %)))))))))))
+(defn- ist-convert-timezone-expression
+ "Returns `[base ist-expr]` for a lib query on `attempts` with a `convert-timezone` expression `ist_dt`."
+ []
+ (let [mp (mt/metadata-provider)
+ datetime (lib.metadata/field mp (mt/id :attempts :datetime))
+ base (-> (lib/query mp (lib.metadata/table mp (mt/id :attempts)))
+ (lib/expression "ist_dt" (lib/convert-timezone datetime "Asia/Kolkata" "UTC")))
+ ist-expr (lib.tu.notebook/find-col-with-spec base
+ (lib/filterable-columns base)
+ {}
+ {:display-name "ist_dt"})]
+ [base ist-expr]))
+
+(defn- assert-now-wrapped-in-target-timezone
+ "Compile `query` and assert that every `NOW()` in the SQL is wrapped in `TIMEZONE(?, NOW())`."
+ [query]
+ (let [sql (:query (qp.compile/compile query))
+ bare-nows (count (re-seq #"(?i)\bNOW\(\)" sql))
+ wrapped (count (re-seq #"(?i)TIMEZONE\(\s*\?\s*,\s*NOW\(\)\s*\)" sql))]
+ (is (pos? bare-nows)
+ "sanity: the compiled SQL uses NOW() as a filter boundary")
+ (is (= bare-nows wrapped)
+ (str "Every NOW() must be wrapped in TIMEZONE(?, NOW()) so it lands in the"
+ " target timezone of the convertTimezone LHS.\nSQL:\n" sql))))
+
+(deftest ^:parallel convert-timezone-relative-datetime-filter-test
+ ;; Regression for #80155.
+ (testing "Relative-datetime filter on a convertTimezone expression compiles LHS and RHS in the same wall-clock frame"
+ (mt/test-driver :postgres
+ (mt/dataset attempted-murders
+ (let [[base ist-expr] (ist-convert-timezone-expression)]
+ (assert-now-wrapped-in-target-timezone
+ (lib/filter base (lib/time-interval ist-expr -3 :month))))))))
+
+(deftest ^:parallel convert-timezone-bucketed-lhs-filter-test
+ ;; Regression for #80155.
+ (testing "A bucketed convertTimezone LHS still compiles LHS and RHS in the same wall-clock frame"
+ (mt/test-driver :postgres
+ (mt/dataset attempted-murders
+ ;; Field bucket (week) and relative-datetime bucket (month) are incompatible, so
+ ;; `optimize-temporal-clauses` (an index-friendliness rewrite that otherwise unbuckets the LHS)
+ ;; leaves this alone.
+ (let [[base ist-expr] (ist-convert-timezone-expression)]
+ (assert-now-wrapped-in-target-timezone
+ (lib/filter base (lib/>= (lib/with-temporal-bucket ist-expr :week)
+ (lib/relative-datetime -2 :month)))))))))
+
+(deftest ^:parallel convert-timezone-now-filter-test
+ ;; Regression for #80155.
+ (testing "A :now filter RHS on a convertTimezone LHS compiles both sides in the same wall-clock frame"
+ (mt/test-driver :postgres
+ (mt/dataset attempted-murders
+ (let [[base ist-expr] (ist-convert-timezone-expression)]
+ (assert-now-wrapped-in-target-timezone
+ (lib/filter base (lib/< ist-expr (lib/now)))))))))
+
+(deftest ^:parallel convert-timezone-today-filter-test
+ ;; Regression for #80155.
+ (testing "A :today filter RHS on a convertTimezone LHS compiles both sides in the same wall-clock frame"
+ (mt/test-driver :postgres
+ (mt/dataset attempted-murders
+ (let [[base ist-expr] (ist-convert-timezone-expression)]
+ (assert-now-wrapped-in-target-timezone
+ (lib/filter base (lib/< ist-expr (lib/today)))))))))
+
+(deftest ^:parallel convert-timezone-wrapped-in-datetime-add-filter-test
+ ;; Regression for #80155.
+ (testing "A convertTimezone nested inside datetime-add still compiles LHS and RHS in the same wall-clock frame"
+ (mt/test-driver :postgres
+ (mt/dataset attempted-murders
+ (let [mp (mt/metadata-provider)
+ datetime (lib.metadata/field mp (mt/id :attempts :datetime))
+ base (-> (lib/query mp (lib.metadata/table mp (mt/id :attempts)))
+ (lib/expression "shifted"
+ (lib/datetime-add
+ (lib/convert-timezone datetime "Asia/Kolkata" "UTC")
+ 1 :hour)))
+ shifted (lib.tu.notebook/find-col-with-spec base (lib/filterable-columns base)
+ {} {:display-name "shifted"})]
+ (assert-now-wrapped-in-target-timezone
+ (lib/filter base (lib/time-interval shifted -3 :month))))))))
+
(deftest postgres-ssl-connectivity-test
(mt/test-driver :postgres
(if (config/config-bool :mb-postgres-ssl-test-ssl)