rusackas commented on code in PR #41964:
URL: https://github.com/apache/superset/pull/41964#discussion_r3565543881


##########
superset/connectors/sqla/models.py:
##########
@@ -1516,7 +1516,18 @@ def full_name(self) -> str:
     def dttm_cols(self) -> list[str]:
         l = [c.column_name for c in self.columns if c.is_dttm]  # noqa: E741
         if self.main_dttm_col and self.main_dttm_col not in l:
-            l.append(self.main_dttm_col)
+            # Only treat ``main_dttm_col`` as a datetime column when the 
column it
+            # points to is actually temporal. A column whose "Is Temporal" 
flag was
+            # removed must not keep being reported as a datetime column just 
because
+            # it is still referenced by ``main_dttm_col`` (#30510). When the 
column
+            # is not present on the dataset, fall back to the legacy behavior 
of
+            # trusting ``main_dttm_col``.
+            main_dttm_column = next(
+                (c for c in self.columns if c.column_name == 
self.main_dttm_col),
+                None,
+            )

Review Comment:
   Added the annotation, thanks: `main_dttm_column: TableColumn | None`.



##########
tests/unit_tests/connectors/sqla/models_test.py:
##########
@@ -1177,3 +1177,46 @@ def 
test_validate_stored_expression_rejects_subquery_around_jinja(
             None,
             "(SELECT password FROM ab_user LIMIT 1) {# x #}",
         )
+
+
+def test_dttm_cols_excludes_column_after_temporal_flag_removed(
+    session: Session,
+) -> None:
+    """
+    Regression for #30510: when a column is mistakenly marked temporal, set as 
the
+    dataset's default datetime (``main_dttm_col``) and saved, then later has 
its
+    ``is_dttm`` flag removed, the dataset must stop treating that column as 
temporal.
+
+    Otherwise ``dttm_cols`` (which feeds time-column selection and the default 
time
+    filter for every chart built on the dataset) keeps returning a non-temporal
+    column, corrupting the dataset with a time filter that cannot be removed.
+    """
+    Database.metadata.create_all(session.bind)
+    database = Database(database_name="my_db", sqlalchemy_uri="sqlite://")
+
+    # A column the user mistakenly marks as temporal ("Is Temporal") and then 
picks
+    # as the dataset "Default Datetime" (``main_dttm_col``).
+    column = TableColumn(column_name="not_really_a_date", type="VARCHAR", 
is_dttm=True)

Review Comment:
   Same as the throwaway-local case above, this is a simple test fixture and 
mypy infers it fine. Leaving as is.



##########
tests/unit_tests/connectors/sqla/models_test.py:
##########
@@ -1177,3 +1177,46 @@ def 
test_validate_stored_expression_rejects_subquery_around_jinja(
             None,
             "(SELECT password FROM ab_user LIMIT 1) {# x #}",
         )
+
+
+def test_dttm_cols_excludes_column_after_temporal_flag_removed(
+    session: Session,
+) -> None:
+    """
+    Regression for #30510: when a column is mistakenly marked temporal, set as 
the
+    dataset's default datetime (``main_dttm_col``) and saved, then later has 
its
+    ``is_dttm`` flag removed, the dataset must stop treating that column as 
temporal.
+
+    Otherwise ``dttm_cols`` (which feeds time-column selection and the default 
time
+    filter for every chart built on the dataset) keeps returning a non-temporal
+    column, corrupting the dataset with a time filter that cannot be removed.
+    """
+    Database.metadata.create_all(session.bind)
+    database = Database(database_name="my_db", sqlalchemy_uri="sqlite://")
+
+    # A column the user mistakenly marks as temporal ("Is Temporal") and then 
picks
+    # as the dataset "Default Datetime" (``main_dttm_col``).
+    column = TableColumn(column_name="not_really_a_date", type="VARCHAR", 
is_dttm=True)
+    dataset = SqlaTable(

Review Comment:
   Same as the throwaway-local case above, this is a simple test fixture and 
mypy infers it fine. 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]

Reply via email to