From 94369ca6b5ac77b0c9e164a2f216ace79d6906dd Mon Sep 17 00:00:00 2001 From: Kasper Mol Date: Thu, 5 Jun 2025 05:46:33 +0200 Subject: [PATCH] fix(template_processing): get_filters now works for IS_NULL and IS_NOT_NULL operators (#33296) (cherry picked from commit cc3460832f914f25de4bb7d0b6f158ccc4069a28) --- superset/jinja_context.py | 10 ++++++++- tests/unit_tests/jinja_context_test.py | 30 ++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/superset/jinja_context.py b/superset/jinja_context.py index 3bd8a9c2ecf..2baece8941f 100644 --- a/superset/jinja_context.py +++ b/superset/jinja_context.py @@ -357,7 +357,15 @@ class ExtraCache: flt.get("expressionType") == "SIMPLE" and flt.get("clause") == "WHERE" and flt.get("subject") == column - and val + and ( + val + # IS_NULL and IS_NOT_NULL operators do not have a value + or op + in ( + FilterOperator.IS_NULL.value, + FilterOperator.IS_NOT_NULL.value, + ) + ) ): if remove_filter: if column not in self.removed_filters: diff --git a/tests/unit_tests/jinja_context_test.py b/tests/unit_tests/jinja_context_test.py index d2ec9c83452..8a57f2e2374 100644 --- a/tests/unit_tests/jinja_context_test.py +++ b/tests/unit_tests/jinja_context_test.py @@ -211,6 +211,36 @@ def test_get_filters_adhoc_filters() -> None: assert cache.applied_filters == ["name"] +def test_get_filters_is_null_operator() -> None: + """ + Test the ``get_filters`` macro with a IS_NULL operator, + which doesn't have a comparator + """ + with app.test_request_context( + data={ + "form_data": json.dumps( + { + "adhoc_filters": [ + { + "clause": "WHERE", + "expressionType": "SIMPLE", + "operator": "IS NULL", + "subject": "name", + "comparator": None, + } + ], + } + ) + } + ): + cache = ExtraCache() + assert cache.get_filters("name", remove_filter=True) == [ + {"op": "IS NULL", "col": "name", "val": None} + ] + assert cache.removed_filters == ["name"] + assert cache.applied_filters == ["name"] + + def test_get_filters_remove_not_present() -> None: """ Test the ``get_filters`` macro without a match and ``remove_filter`` set to True.