Skip to content

Commit

Permalink
Fix inner query labels for Vertica (#2041)
Browse files Browse the repository at this point in the history
In Vertica, inner queries require different aliases than the main query.

This is an example of query generated before this patch:
SELECT chain AS chain,
                weekstartday AS __timestamp,
                SUM(inventory) AS "Inventory"
FROM mytable
JOIN
  (SELECT chain AS chain__,
                   SUM(inventory) AS "Inventory"
   FROM mytable
   WHERE weekstartday >= '2016-01-24 00:00:00'
     AND weekstartday <= '2017-01-17 00:00:00'
   GROUP BY chain
   ORDER BY "Inventory" DESC LIMIT 50) AS anon_1 ON chain = chain__
WHERE weekstartday >= '2016-01-24 00:00:00'
  AND weekstartday <= '2017-01-17 00:00:00'
GROUP BY chain,
         weekstartday
ORDER BY "Inventory" DESC LIMIT 50000

Which in Vertica produces the error:
Error: ('42702', '[42702] ERROR 2671: Column reference "inventory" is ambiguous\n (2671) (SQLExecDirectW)')


And this is the same example after the patch:
SELECT chain AS chain,
                weekstartday AS __timestamp,
                SUM(inventory) AS "Inventory"
FROM mytable
JOIN
  (SELECT chain AS chain__,
                   SUM(inventory) AS mme_inner__
   FROM mytable
   WHERE weekstartday >= '2016-01-24 00:00:00'
     AND weekstartday <= '2017-01-17 00:00:00'
   GROUP BY chain
   ORDER BY mme_inner__ DESC LIMIT 50) AS anon_1 ON chain = chain__
WHERE weekstartday >= '2016-01-24 00:00:00'
  AND weekstartday <= '2017-01-17 00:00:00'
GROUP BY chain,
         weekstartday
ORDER BY "Inventory" DESC LIMIT 50000

Related PR:
19f5371
  • Loading branch information
0x0ece authored and mistercrunch committed Jan 25, 2017
1 parent 45c72d2 commit 7441cf7
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions superset/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1386,8 +1386,10 @@ def visit_column(element, compiler, **kw):

if is_timeseries and timeseries_limit and groupby:
# some sql dialects require for order by expressions
# to also be in the select clause
inner_select_exprs += [main_metric_expr]
# to also be in the select clause -- others, e.g. vertica,
# require a unique inner alias
inner_main_metric_expr = main_metric_expr.label('mme_inner__')
inner_select_exprs += [inner_main_metric_expr]
subq = select(inner_select_exprs)
subq = subq.select_from(tbl)
inner_time_filter = dttm_col.get_time_filter(
Expand All @@ -1396,7 +1398,7 @@ def visit_column(element, compiler, **kw):
)
subq = subq.where(and_(*(where_clause_and + [inner_time_filter])))
subq = subq.group_by(*inner_groupby_exprs)
ob = main_metric_expr
ob = inner_main_metric_expr
if timeseries_limit_metric_expr is not None:
ob = timeseries_limit_metric_expr
subq = subq.order_by(desc(ob))
Expand Down

0 comments on commit 7441cf7

Please sign in to comment.