Skip to content

Instantly share code, notes, and snippets.

@devlatte
Last active June 19, 2025 10:04
Show Gist options
  • Save devlatte/203015c3c4a8cfc12f0313d87b3868aa to your computer and use it in GitHub Desktop.
Save devlatte/203015c3c4a8cfc12f0313d87b3868aa to your computer and use it in GitHub Desktop.
Superset 4.1.2: Skip Automatic time_range Filter on Jinja Temporal Templates

Superset 4.1.2: Disable Auto Time Filter for {{ from_dttm }} / {{ to_dttm }}

This patch disables Superset’s built-in time_range filter whenever Jinja templates {{ from_dttm }} or {{ to_dttm }} appear in any of the following:

  • Dataset SQL
  • Calculated metrics
  • Adhoc filters

As a result, no automatic date filter is applied when those templates are used.

Features

  • Template detection via regex in query_context_processor.py
  • Flag injection: adds disable_auto_time_filter to query extras
  • Filter skip: helpers.py omits the TEMPORAL_RANGE logic when flagged

query_context_processor.py – Detecting template usage and injecting a disable_auto_time_filter flag into the query’s extras. https://github.com/apache/superset/blob/4.1.2rc1/superset/common/query_context_processor.py#L225

helpers.py – Skipping the temporal‐range filter logic if that flag is present. https://github.com/apache/superset/blob/4.1.2rc1/superset/models/helpers.py#L1897

supserset 4.1.2rc

# superset/common/query_context_processor.py
# --- In def get_query_result(...), replace lines ~233–238 with: ---
# origin code 232L
query = ""
# build the raw SQL + any custom metrics/filters for template detection
qdict = query_object.to_dict()
form_data = query_context.form_data
form_sql = query_context.datasource.sql or ""
custom = (form_data.get("metrics") or []) + (form_data.get("adhoc_filters") or [])
form_sql += " " + " ".join(
m.get("sqlExpression") or "" for m in custom if isinstance(m, dict)
)
# detect Jinja templates {{ from_dttm }} or {{ to_dttm }}
pattern = re.compile(r"\{\{\s*(?:from_dttm|to_dttm)\b[^}]*\}\}")
uses_template = bool(pattern.search(form_sql))
if uses_template:
# when templates are used, turn off Superset's auto time filter
query_object.extras["disable_auto_time_filter"] = True
qdict.setdefault("extras", {})
qdict["extras"]["disable_auto_time_filter"] = True
# proceed with execution as usual
if isinstance(query_context.datasource, Query):
result = query_context.datasource.exc_query(qdict)
else:
result = query_context.datasource.query(qdict)
# origin code, 240L
df = result.df
# superset/models/helpers.py
# --- In def get_sqla_query(...), locate the TEMPORAL_RANGE filter block around line 1897 ---
elif (
op == utils.FilterOperator.TEMPORAL_RANGE.value
and isinstance(eq, str)
and col_obj is not None
):
# ✱ skip applying the auto time filter if we flagged it off above
if extras.get("disable_auto_time_filter"):
continue
# original logic to compute the since/until for the time_range
_since, _until = get_since_until_from_time_range(
time_range=eq,
time_shift=time_shift,
extras=extras,
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment