You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The following works because the SUM gets mapped directly to the projected SUM expression (by resolve_columns):
SELECT category, SUM(price) FROM products GROUP BY category ORDER BYSUM(price) ASC
The following does not work, because the SUM(price) (sub)expression currently sticks around in the SortFields of the plan.Sort node, but it cannot evaluate correctly in this context:
SELECT category, SUM(price) FROM products GROUP BY category ORDER BYSUM(price) +1ASC
Other things that should generally work but do not:
SELECT category, SUM(price) FROM products GROUP BY category ORDER BYAVG(price) ASCSELECT category, SUM(price) FROM products GROUP BY category ORDER BYCOUNT(*) ASCSELECT category, SUM(price) FROM products GROUP BY category ORDER BYSUM(price) % 2, SUM(price), AVG(price) ASC
In general, a correct way to handle an aggregation in a sort expression is to push the expression down to the group by node, replace the expression with an appropriately indexed GetField in the sort node itself, and project the expression away in a projection above the Sort node. The logic to do that does not currently exist in the analyzer.
This issue also might apply to window functions, but I have not investigated there yet.
For now, we are going to add a validation step that looks for aggregations outside of GroupBy expressions. If they exist, the query is unsupported and we will return an error.
The text was updated successfully, but these errors were encountered:
zachmu
transferred this issue from dolthub/go-mysql-server
Oct 6, 2022
> create table xy (x intprimary key, y int);
>select x, sum(y) from xy group by x order byavg(y);
column "AVG(xy.y)" could not be found in any table in scope
>select x, sum(y) from xy group by x order bysum(y)+1;
an aggregation remained in the expression '(SUM(xy.y) + 1)' after analysis, outside of a node capable of evaluating it; this query is currently unsupported.
test_subra/main*> create table xy (x int primary key, y int);
test_subra/main*> select x, sum(y) from xy group by x order by avg(y);
Empty set (0.00 sec)
test_subra/main*> select x, sum(y) from xy group by x order by sum(y)+1;
Empty set (0.00 sec)
test_subra/main*>
The following works because the SUM gets mapped directly to the projected SUM expression (by resolve_columns):
The following does not work, because the
SUM(price)
(sub)expression currently sticks around in the SortFields of theplan.Sort
node, but it cannot evaluate correctly in this context:Other things that should generally work but do not:
In general, a correct way to handle an aggregation in a sort expression is to push the expression down to the group by node, replace the expression with an appropriately indexed
GetField
in the sort node itself, and project the expression away in a projection above the Sort node. The logic to do that does not currently exist in the analyzer.This issue also might apply to window functions, but I have not investigated there yet.
For now, we are going to add a validation step that looks for aggregations outside of GroupBy expressions. If they exist, the query is unsupported and we will return an error.
The text was updated successfully, but these errors were encountered: