mayurnewase commented on a change in pull request #12482:
URL: https://github.com/apache/superset/pull/12482#discussion_r560804875
##########
File path: superset/connectors/sqla/models.py
##########
@@ -1147,109 +1125,361 @@ def get_sqla_query( # pylint:
disable=too-many-arguments,too-many-locals,too-ma
if isinstance(col, Label):
label = col._label # pylint: disable=protected-access
- if label in metrics_exprs_by_label:
- col = metrics_exprs_by_label[label]
+ if label in metric_expressions_by_label:
+ col = metric_expressions_by_label[label]
+
+ order_by.append(direction(col))
+ return order_by
+
+ def _get_inner_expressions(
+ self, groupby_expressions: Dict[str, Label], inner_main_metric_expr:
Label
+ ) -> Tuple[List[Label], List[Label]]:
+ """
+ generate select and groupby expressions for inner query
+ """
+ inner_select_expressions = []
+ inner_groupby_expressions = []
+ for gby_name, gby_obj in groupby_expressions.items():
+ inner = self.make_sqla_column_compatible(gby_obj, gby_name + "__")
+ inner_groupby_expressions.append(inner)
+ inner_select_expressions.append(inner)
+
+ inner_select_expressions += [inner_main_metric_expr]
+ return inner_select_expressions, inner_groupby_expressions
+
+ def _get_subquery( # pylint:disable=too-many-arguments
+ self,
+ inner_select_expressions: List[Label],
+ query_table: TableClause,
+ inner_main_metric_expr: Label,
+ where_clause: List[BooleanClauseList],
+ inner_time_filter: BooleanClauseList,
+ inner_groupby_expressions: List[Label],
+ timeseries_limit_metric: Optional[Metric],
+ timeseries_limit: int,
+ metrics_by_name: Dict[str, SqlMetric],
+ columns_by_name: Dict[str, TableColumn],
+ order_desc: bool,
+ ) -> Select:
+ """
+ generate complete subquery
+ """
+ subquery = select(inner_select_expressions).select_from(query_table)
+ subquery = subquery.where(and_(*(where_clause + [inner_time_filter])))
+ subquery = subquery.group_by(*inner_groupby_expressions)
+
+ order_direction_column = inner_main_metric_expr
+ if timeseries_limit_metric:
+ order_direction_column = self._get_timeseries_orderby(
+ timeseries_limit_metric, metrics_by_name, columns_by_name
+ )
+ direction = desc if order_desc else asc
+ subquery = subquery.order_by(direction(order_direction_column))
+ subquery = subquery.limit(timeseries_limit)
+ return subquery
+
+ def _get_inner_query( # pylint:disable=too-many-arguments,too-many-locals
+ self,
+ timeseries_limit: int,
+ is_sip_38: bool,
+ main_metric_expression: Label,
+ groupby_expressions: "OrderedDict[str, Label]",
+ query_table: TableClause,
+ time_range_endpoints: Optional[Any],
+ from_dttm: Optional[datetime],
+ metrics_by_name: Dict[str, SqlMetric],
+ columns_by_name: Dict[str, ColumnElement],
+ order_desc: bool,
+ to_dttm: Optional[datetime],
+ where_clause: List[BooleanClauseList],
+ timeseries_limit_metric: Optional[Metric],
+ db_engine_spec: SqliteEngineSpec,
+ metrics: List[Metric],
+ granularity: str,
+ filter_: Optional[List[Dict[str, Any]]] = None,
+ extras: Optional[Dict[str, Any]] = None,
+ columns: Optional[List[str]] = None,
+ groupby: Optional[List[str]] = None,
+ orderby: Optional[List[Tuple[ColumnElement, bool]]] = None,
+ inner_from_dttm: Optional[datetime] = None,
+ inner_to_dttm: Optional[datetime] = None,
+ ) -> Tuple[TableClause, List[str]]:
Review comment:
I can try to break this into pieces.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]