You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by va...@apache.org on 2026/06/22 18:56:24 UTC

(superset) branch fix/apply-time-grain-filters-dashboard-id created (now 0358411a22f)

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

Vitor-Avila pushed a change to branch fix/apply-time-grain-filters-dashboard-id
in repository https://gitbox.apache.org/repos/asf/superset.git


      at 0358411a22f fix(chart API): Consider time grain filters with the filters_dashboard_id param

This branch includes the following new commits:

     new 0358411a22f fix(chart API): Consider time grain filters with the filters_dashboard_id param

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(superset) 01/01: fix(chart API): Consider time grain filters with the filters_dashboard_id param

Posted by va...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

Vitor-Avila pushed a commit to branch fix/apply-time-grain-filters-dashboard-id
in repository https://gitbox.apache.org/repos/asf/superset.git

commit 0358411a22f5a89f8c6ce8836b8b41f744c30f2b
Author: Vitor Avila <vi...@gmail.com>
AuthorDate: Mon Jun 22 15:55:58 2026 -0300

    fix(chart API): Consider time grain filters with the filters_dashboard_id param
---
 superset/charts/data/dashboard_filter_context.py | 14 ++++-
 tests/unit_tests/charts/test_chart_data_api.py   | 73 ++++++++++++++++++++++++
 2 files changed, 86 insertions(+), 1 deletion(-)

diff --git a/superset/charts/data/dashboard_filter_context.py b/superset/charts/data/dashboard_filter_context.py
index b7d745b49bc..648da737ec5 100644
--- a/superset/charts/data/dashboard_filter_context.py
+++ b/superset/charts/data/dashboard_filter_context.py
@@ -306,7 +306,7 @@ def get_dashboard_filter_context(
     return context
 
 
-def apply_dashboard_filter_context(
+def apply_dashboard_filter_context(  # noqa: C901
     query_context: dict[str, Any],
     extra_form_data: dict[str, Any],
 ) -> None:
@@ -332,6 +332,18 @@ def apply_dashboard_filter_context(
         for key in EXTRA_FORM_DATA_OVERRIDE_EXTRA_KEYS:
             if key in extra_form_data:
                 extras[key] = extra_form_data[key]
+
+        # EXTRA_FORM_DATA_OVERRIDE_EXTRA_KEYS is originally used with form_data objects,
+        # not query_context objects. form_data objects expect time_grain_sqla as a
+        # top-level key, but query_context objects expect it as an extra key.
+        if custom_time_grain := extra_form_data.get("time_grain_sqla"):
+            extras["time_grain_sqla"] = custom_time_grain
+            # Inject ``filter_timegrain`` into X-Axis column
+            for column in query.get("columns") or []:
+                if isinstance(column, dict) and column.get("columnType") == "BASE_AXIS":
+                    column["timeGrain"] = custom_time_grain
+                    break
+
         if extras:
             query["extras"] = extras
 
diff --git a/tests/unit_tests/charts/test_chart_data_api.py b/tests/unit_tests/charts/test_chart_data_api.py
index eea180f01da..0c0fc8f31cf 100644
--- a/tests/unit_tests/charts/test_chart_data_api.py
+++ b/tests/unit_tests/charts/test_chart_data_api.py
@@ -113,6 +113,79 @@ def test_apply_dashboard_filter_context_does_not_duplicate_filters(
         assert ExtraCache().filter_values("country") == ["USA"]
 
 
+def test_apply_dashboard_filter_context_applies_time_grain_to_extras() -> None:
+    """
+    A dashboard time-grain filter must land in ``query["extras"]``, where
+    get_time_grain() reads it for charts that have no adhoc x-axis column.
+    """
+    query_context_json: dict[str, Any] = {
+        "queries": [{"extras": {"time_grain_sqla": "P1D", "having": "", "where": ""}}],
+    }
+
+    apply_dashboard_filter_context(query_context_json, {"time_grain_sqla": "P1M"})
+
+    assert query_context_json["queries"][0]["extras"]["time_grain_sqla"] == "P1M"
+
+
+def test_apply_dashboard_filter_context_overrides_x_axis_time_grain() -> None:
+    """
+    For charts with an adhoc X-Axis, the dashboard grain must override the
+    BASE_AXIS column's ``timeGrain`` (which get_time_grain() reads before
+    falling back to extras), mirroring the frontend's normalizeTimeColumn.
+    """
+    query_context_json: dict[str, Any] = {
+        "queries": [
+            {
+                "columns": [
+                    {
+                        "timeGrain": "P1D",
+                        "columnType": "BASE_AXIS",
+                        "sqlExpression": "order_date",
+                    }
+                ],
+                "extras": {"time_grain_sqla": "P1D"},
+            }
+        ],
+    }
+
+    apply_dashboard_filter_context(query_context_json, {"time_grain_sqla": "P1Y"})
+
+    query = query_context_json["queries"][0]
+    assert query["columns"][0]["timeGrain"] == "P1Y"
+    assert query["extras"]["time_grain_sqla"] == "P1Y"
+
+
+def test_apply_dashboard_filter_context_keeps_grain_when_no_grain_filter() -> None:
+    """
+    When the dashboard applies a non-grain filter (e.g. a value filter), the
+    chart's own x-axis ``timeGrain`` must be preserved -- not wiped -- since no
+    dashboard grain was provided.
+    """
+    query_context_json: dict[str, Any] = {
+        "queries": [
+            {
+                "columns": [
+                    {
+                        "timeGrain": "P1M",
+                        "columnType": "BASE_AXIS",
+                        "sqlExpression": "order_date",
+                    }
+                ],
+                "extras": {"time_grain_sqla": "P1M"},
+            }
+        ],
+    }
+
+    # extra_form_data carries a value filter but NO time_grain_sqla
+    apply_dashboard_filter_context(
+        query_context_json,
+        {"filters": [{"col": "country", "op": "IN", "val": ["US"]}]},
+    )
+
+    query = query_context_json["queries"][0]
+    assert query["columns"][0]["timeGrain"] == "P1M"
+
+
 def _extract_filename(form_value: str) -> str | None:
     """Run _extract_export_params_from_request with a form filename value."""
     from superset.charts.data.api import ChartDataRestApi