You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by el...@apache.org on 2026/09/20 16:52:06 UTC

(superset) 01/01: fix(chart): wrap raw pandas TypeError/DataError from post-processing as QueryObjectValidationError

This is an automated email from the ASF dual-hosted git repository.

eschutho pushed a commit to branch fix-post-processing-raw-typeerror
in repository https://gitbox.apache.org/repos/asf/superset.git

commit 91bcff4c452f558bf526a05d0655f3f1e0f1405a
Author: Elizabeth Thompson <es...@gmail.com>
AuthorDate: Sun Sep 20 16:51:29 2026 +0000

    fix(chart): wrap raw pandas TypeError/DataError from post-processing as QueryObjectValidationError
    
    When a post-processing operator (e.g. resample with method=mean) is called on
    a DataFrame that includes object-dtype columns, pandas raises a raw TypeError
    (resample) or DataError (rolling). The narrow `except InvalidPostProcessingError`
    in `ExploreMixin.get_query_result` did not catch these, so the exception
    propagated as an opaque 500 instead of a clean 400.
    
    Widen the except to also catch TypeError and pandas.errors.DataError and
    re-raise as QueryObjectValidationError (same 400 semantics as the existing arm).
---
 superset/models/helpers.py              |  2 ++
 tests/unit_tests/models/helpers_test.py | 51 +++++++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+)

diff --git a/superset/models/helpers.py b/superset/models/helpers.py
index 29a05cc6ce6..610f98f5ec1 100644
--- a/superset/models/helpers.py
+++ b/superset/models/helpers.py
@@ -2645,6 +2645,8 @@ class ExploreMixin:  # pylint: disable=too-many-public-methods
                 df = query_object.exec_post_processing(df)
             except InvalidPostProcessingError as ex:
                 raise QueryObjectValidationError(ex.message) from ex
+            except (TypeError, pd.errors.DataError) as ex:
+                raise QueryObjectValidationError(str(ex)) from ex
 
         # Update result with processed data
         result.df = df
diff --git a/tests/unit_tests/models/helpers_test.py b/tests/unit_tests/models/helpers_test.py
index 0ecfb7af7e7..2f6f337fe7a 100644
--- a/tests/unit_tests/models/helpers_test.py
+++ b/tests/unit_tests/models/helpers_test.py
@@ -5759,3 +5759,54 @@ def test_filter_adhoc_column(database: Database) -> None:
     # The adhoc column resolved by label is parenthesized in the WHERE clause,
     # consistent with inline adhoc columns, to guard operator precedence.
     assert "lower((real_name)) LIKE lower('Zona%')" in sql
+
+
+def test_get_query_result_wraps_post_processing_type_error(
+    database: "Database",
+) -> None:
+    """
+    A raw TypeError from pandas inside exec_post_processing (e.g. resample.mean()
+    on a DataFrame that contains object-dtype columns) must be surfaced as
+    QueryObjectValidationError (400) rather than propagating as a system 500.
+    """
+    from datetime import timedelta
+    from unittest.mock import patch
+
+    import pandas as pd
+
+    from superset.common.query_object import QueryObject
+    from superset.connectors.sqla.models import SqlaTable
+    from superset.exceptions import QueryObjectValidationError
+    from superset.models.helpers import QueryResult
+
+    table = SqlaTable(table_name="t", database=database)
+
+    # DatetimeIndex + object-dtype "category" column causes
+    # df.resample("1D").mean() to raise TypeError in pandas ≥ 2.x
+    df = pd.DataFrame(
+        {"metric": [1.0, 2.0], "category": ["a", "b"]},
+        index=pd.to_datetime(["2023-01-01", "2023-01-03"]),
+    )
+
+    query_object = QueryObject(
+        row_limit=10,
+        post_processing=[
+            {"operation": "resample", "options": {"method": "mean", "rule": "1D"}}
+        ],
+    )
+
+    with (
+        patch.object(
+            table,
+            "query",
+            return_value=QueryResult(
+                df=df,
+                query="SELECT 1",
+                duration=timedelta(0),
+                sql_shifted_temporal_labels=set(),
+            ),
+        ),
+        patch.object(table, "normalize_df", return_value=df),
+        pytest.raises(QueryObjectValidationError),
+    ):
+        table.get_query_result(query_object)