Skip to content

Commit

Permalink
[sqla] Fixing order-by for non-inner-joins (#6862)
Browse files Browse the repository at this point in the history
(cherry picked from commit 5728946)
  • Loading branch information
john-bodley authored and John Bodley committed Feb 15, 2019
1 parent be8ead4 commit f566190
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions superset/connectors/sqla/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -724,15 +724,11 @@ def get_sqla_query( # sqla

ob = inner_main_metric_expr
if timeseries_limit_metric:
if utils.is_adhoc_metric(timeseries_limit_metric):
ob = self.adhoc_metric_to_sqla(timeseries_limit_metric, cols)
elif timeseries_limit_metric in metrics_dict:
timeseries_limit_metric = metrics_dict.get(
timeseries_limit_metric,
)
ob = timeseries_limit_metric.get_sqla_col()
else:
raise Exception(_("Metric '{}' is not valid".format(m)))
ob = self._get_timeseries_orderby(
timeseries_limit_metric,
metrics_dict,
cols,
)
direction = desc if order_desc else asc
subq = subq.order_by(direction(ob))
subq = subq.limit(timeseries_limit)
Expand All @@ -744,6 +740,16 @@ def get_sqla_query( # sqla

tbl = tbl.join(subq.alias(), and_(*on_clause))
else:
if timeseries_limit_metric:
orderby = [(
self._get_timeseries_orderby(
timeseries_limit_metric,
metrics_dict,
cols,
),
False,
)]

# run subquery to get top groups
subquery_obj = {
'prequeries': prequeries,
Expand Down Expand Up @@ -772,6 +778,19 @@ def get_sqla_query( # sqla

return qry.select_from(tbl)

def _get_timeseries_orderby(self, timeseries_limit_metric, metrics_dict, cols):
if utils.is_adhoc_metric(timeseries_limit_metric):
ob = self.adhoc_metric_to_sqla(timeseries_limit_metric, cols)
elif timeseries_limit_metric in metrics_dict:
timeseries_limit_metric = metrics_dict.get(
timeseries_limit_metric,
)
ob = timeseries_limit_metric.get_sqla_col()
else:
raise Exception(_("Metric '{}' is not valid".format(timeseries_limit_metric)))

return ob

def _get_top_groups(self, df, dimensions):
cols = {col.column_name: col for col in self.columns}
groups = []
Expand Down

0 comments on commit f566190

Please sign in to comment.