Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Make Jinja template applied in timestamp columns #17237

Merged
merged 3 commits into from
Oct 28, 2021
Merged
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
24 changes: 19 additions & 5 deletions superset/connectors/sqla/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,10 @@ def get_time_filter(
return and_(*l)

def get_timestamp_expression(
self, time_grain: Optional[str], label: Optional[str] = None
self,
time_grain: Optional[str],
label: Optional[str] = None,
template_processor: Optional[BaseTemplateProcessor] = None,
) -> Union[TimestampExpression, Label]:
"""
Return a SQLAlchemy Core element representation of self to be used in a query.
Expand All @@ -322,7 +325,10 @@ def get_timestamp_expression(
sqla_col = column(self.column_name, type_=type_)
return self.table.make_sqla_column_compatible(sqla_col, label)
if self.expression:
col = literal_column(self.expression, type_=type_)
expression = self.expression
if template_processor:
expression = template_processor.process_template(self.expression)
col = literal_column(expression, type_=type_)
else:
col = column(self.column_name, type_=type_)
time_expr = self.db_engine_spec.get_timestamp_expr(
Expand Down Expand Up @@ -1089,7 +1095,11 @@ def get_sqla_query( # pylint: disable=too-many-arguments,too-many-locals,too-ma
# if groupby field/expr equals granularity field/expr
table_col = columns_by_name.get(selected)
if table_col and table_col.type_generic == GenericDataType.TEMPORAL:
outer = table_col.get_timestamp_expression(time_grain, selected)
outer = table_col.get_timestamp_expression(
time_grain=time_grain,
label=selected,
template_processor=template_processor,
)
# if groupby field equals a selected column
elif table_col:
outer = table_col.get_sqla_col()
Expand Down Expand Up @@ -1122,7 +1132,9 @@ def get_sqla_query( # pylint: disable=too-many-arguments,too-many-locals,too-ma
time_filters = []

if is_timeseries:
timestamp = dttm_col.get_timestamp_expression(time_grain)
timestamp = dttm_col.get_timestamp_expression(
time_grain=time_grain, template_processor=template_processor
)
# always put timestamp as the first column
select_exprs.insert(0, timestamp)
groupby_all_columns[timestamp.name] = timestamp
Expand Down Expand Up @@ -1187,7 +1199,9 @@ def get_sqla_query( # pylint: disable=too-many-arguments,too-many-locals,too-ma

if col_obj:
if filter_grain:
sqla_col = col_obj.get_timestamp_expression(filter_grain)
sqla_col = col_obj.get_timestamp_expression(
time_grain=filter_grain, template_processor=template_processor
)
else:
sqla_col = col_obj.get_sqla_col()
col_spec = db_engine_spec.get_column_spec(col_obj.type)
Expand Down