From 53efb6bd5ef18d0edd4a12606e68e3c59a030bec Mon Sep 17 00:00:00 2001 From: XcYyCx Date: Tue, 26 Oct 2021 12:10:02 -0700 Subject: [PATCH 1/3] Update models.py --- superset/connectors/sqla/models.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/superset/connectors/sqla/models.py b/superset/connectors/sqla/models.py index 1f68dea2be3de..1e707ec91cb7d 100644 --- a/superset/connectors/sqla/models.py +++ b/superset/connectors/sqla/models.py @@ -322,7 +322,9 @@ 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_) + tp = self.table.get_template_processor() + expression = tp.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( From f838d2b6c47e455a8d772b3459416badcca330bf Mon Sep 17 00:00:00 2001 From: Yuchen Xing Date: Wed, 27 Oct 2021 11:04:22 -0700 Subject: [PATCH 2/3] Add optional template processor to get_timestamp_expression() --- superset/connectors/sqla/models.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/superset/connectors/sqla/models.py b/superset/connectors/sqla/models.py index 1e707ec91cb7d..a6df8a93cacca 100644 --- a/superset/connectors/sqla/models.py +++ b/superset/connectors/sqla/models.py @@ -303,7 +303,8 @@ 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. @@ -322,8 +323,9 @@ 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: - tp = self.table.get_template_processor() - expression = tp.process_template(self.expression) + 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_) @@ -1091,7 +1093,10 @@ 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() @@ -1124,7 +1129,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 @@ -1189,7 +1196,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) From f0aec1abe1f04dedb6ea97d9a91cf567f91a909b Mon Sep 17 00:00:00 2001 From: Yuchen Xing Date: Wed, 27 Oct 2021 23:09:34 -0700 Subject: [PATCH 3/3] Update models.py --- superset/connectors/sqla/models.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/superset/connectors/sqla/models.py b/superset/connectors/sqla/models.py index a6df8a93cacca..6587f5e966432 100644 --- a/superset/connectors/sqla/models.py +++ b/superset/connectors/sqla/models.py @@ -303,8 +303,10 @@ def get_time_filter( return and_(*l) def get_timestamp_expression( - self, time_grain: Optional[str], label: Optional[str] = None, - template_processor: Optional[BaseTemplateProcessor] = 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. @@ -1096,7 +1098,8 @@ def get_sqla_query( # pylint: disable=too-many-arguments,too-many-locals,too-ma outer = table_col.get_timestamp_expression( time_grain=time_grain, label=selected, - template_processor=template_processor) + template_processor=template_processor, + ) # if groupby field equals a selected column elif table_col: outer = table_col.get_sqla_col() @@ -1130,8 +1133,8 @@ def get_sqla_query( # pylint: disable=too-many-arguments,too-many-locals,too-ma if is_timeseries: timestamp = dttm_col.get_timestamp_expression( - time_grain=time_grain, - template_processor=template_processor) + 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 @@ -1197,8 +1200,8 @@ 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( - time_grain=filter_grain, - template_processor=template_processor) + 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)