Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 31 additions & 9 deletions superset/models/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2042,6 +2042,11 @@ def get_sqla_query( # pylint: disable=too-many-arguments,too-many-locals,too-ma
metrics_exprs = []

time_filters = []

# Process FROM clause early to populate removed_filters from virtual dataset
# templates before we decide whether to add time filters
tbl, cte = self.get_from_clause(template_processor)

if granularity:
if granularity not in columns_by_name or not dttm_col:
raise QueryObjectValidationError(
Expand All @@ -2064,6 +2069,7 @@ def get_sqla_query( # pylint: disable=too-many-arguments,too-many-locals,too-ma
self.always_filter_main_dttm
and self.main_dttm_col in self.dttm_cols
and self.main_dttm_col != dttm_col.column_name
and self.main_dttm_col not in removed_filters
):
time_filters.append(
self.get_time_filter(
Expand All @@ -2074,13 +2080,21 @@ def get_sqla_query( # pylint: disable=too-many-arguments,too-many-locals,too-ma
)
)

time_filter_column = self.get_time_filter(
time_col=dttm_col,
start_dttm=from_dttm,
end_dttm=to_dttm,
template_processor=template_processor,
# Check if time filter should be skipped because it was handled in template.
# Check both the actual column name and __timestamp alias
should_skip_time_filter = (
dttm_col.column_name in removed_filters
or utils.DTTM_ALIAS in removed_filters
)
time_filters.append(time_filter_column)

if not should_skip_time_filter:
time_filter_column = self.get_time_filter(
time_col=dttm_col,
start_dttm=from_dttm,
end_dttm=to_dttm,
template_processor=template_processor,
)
time_filters.append(time_filter_column)

# Always remove duplicates by column name, as sometimes `metrics_exprs`
# can have the same name as a groupby column (e.g. when users use
Expand All @@ -2099,8 +2113,6 @@ def get_sqla_query( # pylint: disable=too-many-arguments,too-many-locals,too-ma

qry = sa.select(select_exprs)

tbl, cte = self.get_from_clause(template_processor)

if groupby_all_columns:
qry = qry.group_by(*groupby_all_columns.values())

Expand Down Expand Up @@ -2145,7 +2157,17 @@ def get_sqla_query( # pylint: disable=too-many-arguments,too-many-locals,too-ma
is_metric_filter = True
filter_grain = flt.get("grain")

if get_column_name(flt_col) in removed_filters:
# Check if this filter should be skipped because it was handled in
# template. Special handling for __timestamp alias: check both the
# alias and the actual column name
filter_col_name = get_column_name(flt_col)
should_skip_filter = filter_col_name in removed_filters
if not should_skip_filter and flt_col == utils.DTTM_ALIAS and col_obj:
# For __timestamp, also check if the actual datetime column was
# removed
should_skip_filter = col_obj.column_name in removed_filters

if should_skip_filter:
# Skip generating SQLA filter when the jinja template handles it.
continue

Expand Down
Loading
Loading