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
  • Loading branch information
john-bodley authored Feb 15, 2019
1 parent b0f7f51 commit 5728946
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 @@ -752,15 +752,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 @@ -775,6 +771,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 @@ -805,6 +811,19 @@ def get_sqla_query( # sqla
return SqlaQuery(sqla_query=qry.select_from(tbl),
labels_expected=labels_expected)

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, groupby_exprs):
groups = []
for unused, row in df.iterrows():
Expand Down

0 comments on commit 5728946

Please sign in to comment.