Skip to content

Commit 972e15e

Browse files
authored
fix(sql): remove WHERE 1 = 1 when temporal filter has "No filter" selected (#38704)
1 parent 03de7e1 commit 972e15e

File tree

2 files changed

+336
-26
lines changed

2 files changed

+336
-26
lines changed

superset/models/helpers.py

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2399,7 +2399,7 @@ def get_time_filter( # pylint: disable=too-many-arguments
23992399
time_grain: Optional[str] = None,
24002400
label: Optional[str] = "__time",
24012401
template_processor: Optional[BaseTemplateProcessor] = None,
2402-
) -> ColumnElement:
2402+
) -> Optional[ColumnElement]:
24032403
col = (
24042404
time_col.get_timestamp_expression(
24052405
time_grain=time_grain,
@@ -2427,6 +2427,8 @@ def get_time_filter( # pylint: disable=too-many-arguments
24272427
self.dttm_sql_literal(end_dttm, time_col)
24282428
)
24292429
)
2430+
if not l:
2431+
return None
24302432
return and_(True, *l)
24312433

24322434
def values_for_column( # pylint: disable=too-many-locals
@@ -2945,14 +2947,14 @@ def get_sqla_query( # pylint: disable=too-many-arguments,too-many-locals,too-ma
29452947
and self.main_dttm_col != dttm_col.column_name
29462948
and self.main_dttm_col not in removed_filters
29472949
):
2948-
time_filters.append(
2949-
self.get_time_filter(
2950-
time_col=columns_by_name[self.main_dttm_col],
2951-
start_dttm=from_dttm,
2952-
end_dttm=to_dttm,
2953-
template_processor=template_processor,
2954-
)
2950+
_main_dttm_filter = self.get_time_filter(
2951+
time_col=columns_by_name[self.main_dttm_col],
2952+
start_dttm=from_dttm,
2953+
end_dttm=to_dttm,
2954+
template_processor=template_processor,
29552955
)
2956+
if _main_dttm_filter is not None:
2957+
time_filters.append(_main_dttm_filter)
29562958

29572959
# Check if time filter should be skipped because it was handled in template.
29582960
# Check both the actual column name and __timestamp alias
@@ -2968,7 +2970,8 @@ def get_sqla_query( # pylint: disable=too-many-arguments,too-many-locals,too-ma
29682970
end_dttm=to_dttm,
29692971
template_processor=template_processor,
29702972
)
2971-
time_filters.append(time_filter_column)
2973+
if time_filter_column is not None:
2974+
time_filters.append(time_filter_column)
29722975

29732976
# Always remove duplicates by column name, as sometimes `metrics_exprs`
29742977
# can have the same name as a groupby column (e.g. when users use
@@ -3205,16 +3208,16 @@ def get_sqla_query( # pylint: disable=too-many-arguments,too-many-locals,too-ma
32053208
time_shift=time_shift,
32063209
extras=extras,
32073210
)
3208-
target_clause_list.append(
3209-
self.get_time_filter(
3210-
time_col=col_obj,
3211-
start_dttm=_since,
3212-
end_dttm=_until,
3213-
time_grain=flt_grain,
3214-
label=sqla_col.key,
3215-
template_processor=template_processor,
3216-
)
3211+
_temporal_filter = self.get_time_filter(
3212+
time_col=col_obj,
3213+
start_dttm=_since,
3214+
end_dttm=_until,
3215+
time_grain=flt_grain,
3216+
label=sqla_col.key,
3217+
template_processor=template_processor,
32173218
)
3219+
if _temporal_filter is not None:
3220+
target_clause_list.append(_temporal_filter)
32183221
else:
32193222
raise QueryObjectValidationError(
32203223
_("Invalid filter operation type: %(op)s", op=op)
@@ -3301,14 +3304,14 @@ def get_sqla_query( # pylint: disable=too-many-arguments,too-many-locals,too-ma
33013304
inner_time_filter = []
33023305

33033306
if dttm_col and not db_engine_spec.time_groupby_inline:
3304-
inner_time_filter = [
3305-
self.get_time_filter(
3306-
time_col=dttm_col,
3307-
start_dttm=inner_from_dttm or from_dttm,
3308-
end_dttm=inner_to_dttm or to_dttm,
3309-
template_processor=template_processor,
3310-
)
3311-
]
3307+
_inner_filter = self.get_time_filter(
3308+
time_col=dttm_col,
3309+
start_dttm=inner_from_dttm or from_dttm,
3310+
end_dttm=inner_to_dttm or to_dttm,
3311+
template_processor=template_processor,
3312+
)
3313+
if _inner_filter is not None:
3314+
inner_time_filter = [_inner_filter]
33123315
subq = subq.where(and_(*(where_clause_and + inner_time_filter)))
33133316
subq = subq.group_by(*inner_groupby_exprs)
33143317

0 commit comments

Comments
 (0)