rusackas commented on code in PR #37014:
URL: https://github.com/apache/superset/pull/37014#discussion_r3432197884
##########
superset/viz.py:
##########
@@ -324,6 +324,7 @@ def get_df(self, query_obj: QueryObjectDict | None = None)
-> pd.DataFrame:
timestamp_format=timestamp_format,
offset=self.datasource.offset,
time_shift=self.form_data.get("time_shift"),
+ timezone=self.datasource.get_dataset_timezone(),
Review Comment:
Tests for this got added in `test_time_filter_timezone.py` and
`test_core.py` (covering the UTC conversion, a DST case, and the invalid-tz
fallback), so I think we're covered here. Resolving.
##########
tests/unit_tests/models/test_time_filter_timezone.py:
##########
@@ -0,0 +1,76 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Tests for dataset-timezone handling in ``get_time_filter`` (PR #37014)."""
+
+from __future__ import annotations
+
+from datetime import datetime
+
+from flask import Flask
+
+from superset.connectors.sqla.models import SqlaTable, TableColumn
+from superset.models.core import Database
+
+
+def _table(extra: str | None) -> tuple[SqlaTable, TableColumn]:
+ database = Database(database_name="test_db", sqlalchemy_uri="sqlite://")
+ time_col = TableColumn(column_name="last_modified", is_dttm=1,
type="TIMESTAMP")
+ table = SqlaTable(
+ table_name="events",
+ columns=[time_col],
+ main_dttm_col="last_modified",
+ database=database,
+ extra=extra,
+ )
+ return table, time_col
Review Comment:
It's a tiny test helper and the name says what it does... not going to lose
sleep over a missing docstring here :)
##########
superset/models/helpers.py:
##########
@@ -2762,19 +2780,47 @@ def get_time_filter( # pylint:
disable=too-many-arguments
)
)
+ # Apply timezone conversion for time filter boundaries
+ # This converts user's local time boundaries to UTC for querying
UTC-stored data
+ adjusted_start, adjusted_end = start_dttm, end_dttm
+ dataset_timezone = self.get_dataset_timezone()
+
+ if dataset_timezone and (start_dttm or end_dttm):
+ try:
+ tz = ZoneInfo(dataset_timezone)
+
+ # The datetimes from the UI are naive (no timezone info)
+ # We interpret them as being in the dataset's configured
timezone
+ # and convert them to UTC for comparison with UTC-stored data
+ if start_dttm:
+ local_start = start_dttm.replace(tzinfo=tz)
+ adjusted_start =
local_start.astimezone(timezone.utc).replace(
+ tzinfo=None
+ )
+ if end_dttm:
+ local_end = end_dttm.replace(tzinfo=tz)
+ adjusted_end = local_end.astimezone(timezone.utc).replace(
+ tzinfo=None
+ )
Review Comment:
These are the UI time-filter boundaries, which are naive by design (per the
comment right above), so interpreting them in the dataset tz is intentional for
this path. Fair flag in the general case though.
##########
superset/utils/core.py:
##########
@@ -2037,8 +2041,28 @@ def normalize_dttm_col(
_process_datetime_column(df, _col)
- if _col.offset:
+ if _col.timezone:
+ try:
+ tz = ZoneInfo(_col.timezone)
+ # Data is stored in UTC, convert to the dataset's configured
timezone
+ # First make the datetime UTC-aware, then convert to target
timezone
+ series = df[_col.col_label]
+ if not series.empty and series.notna().any():
+ # Convert UTC to target timezone
+ df[_col.col_label] = (
+ series.dt.tz_localize("UTC")
+ .dt.tz_convert(tz)
Review Comment:
Same story as the other one... the parsed series is naive here, and the DST
+ invalid-tz cases are covered in the new tests. Leaving as-is.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]