-
Notifications
You must be signed in to change notification settings - Fork 5.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for complex ORDER BY expressions referencing projection output #6766
Support for complex ORDER BY expressions referencing projection output #6766
Conversation
public void testOrderByWithProjectionColumnReference() | ||
{ | ||
assertQueryOrdered("SELECT a*2 AS b FROM (VALUES -1, 0, 2) t(a) ORDER BY b*-1", "VALUES (4), (0), (-2)"); | ||
assertQueryOrdered("SELECT a*2 AS b FROM (VALUES -1, 0, 2) t(a) ORDER BY b", "VALUES (-2), (0), (4)"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you need to add more testing:
SELECT a* -1 AS a FROM (VALUES -1, 0, 2) t(a) ORDER BY a*-1
SELECT a*-1 AS a FROM (VALUES -1, 0, 2) t(a) ORDER BY t.a*-1
SELECT a*-1 FROM (VALUES -1, 0, 2) t(a) ORDER BY a*-1
SELECT a*-1 FROM (VALUES -1, 0, 2) t(a) ORDER BY t.a*-1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More tests ;)
SELECT a, a* -1 AS a FROM (VALUES -1, 0, 2) t(a) ORDER BY a; -- expected failure
SELECT a, a* -1 AS a FROM (VALUES -1, 0, 2) t(a) ORDER BY t.a;
SELECT a, a* -2 AS b FROM (VALUES -1, 0, 2) t(a) ORDER BY a + b
SELECT a as b, a* -2 AS a FROM (VALUES -1, 0, 2) t(a) ORDER BY a + b;
SELECT a* -2 AS a FROM (VALUES -1, 0, 2) t(a) ORDER BY a + t.a;
SELECT (SELECT 1 as a) FROM (VALUES -1, 0, 2) t(a) ORDER BY a; -- expected failure?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIK you can write "VALUES -1, 0, 2" for h2 check query (no need for '(', ')')
aa93b4b
to
a9ff893
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just small comments
public void testOrderByWithProjectionColumnReference() | ||
{ | ||
assertQueryOrdered("SELECT a*2 AS b FROM (VALUES -1, 0, 2) t(a) ORDER BY b*-1", "VALUES (4), (0), (-2)"); | ||
assertQueryOrdered("SELECT a*2 AS b FROM (VALUES -1, 0, 2) t(a) ORDER BY b", "VALUES (-2), (0), (4)"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More tests ;)
SELECT a, a* -1 AS a FROM (VALUES -1, 0, 2) t(a) ORDER BY a; -- expected failure
SELECT a, a* -1 AS a FROM (VALUES -1, 0, 2) t(a) ORDER BY t.a;
SELECT a, a* -2 AS b FROM (VALUES -1, 0, 2) t(a) ORDER BY a + b
SELECT a as b, a* -2 AS a FROM (VALUES -1, 0, 2) t(a) ORDER BY a + b;
SELECT a* -2 AS a FROM (VALUES -1, 0, 2) t(a) ORDER BY a + t.a;
SELECT (SELECT 1 as a) FROM (VALUES -1, 0, 2) t(a) ORDER BY a; -- expected failure?
public void testOrderByWithProjectionColumnReference() | ||
{ | ||
assertQueryOrdered("SELECT a*2 AS b FROM (VALUES -1, 0, 2) t(a) ORDER BY b*-1", "VALUES (4), (0), (-2)"); | ||
assertQueryOrdered("SELECT a*2 AS b FROM (VALUES -1, 0, 2) t(a) ORDER BY b", "VALUES (-2), (0), (4)"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIK you can write "VALUES -1, 0, 2" for h2 check query (no need for '(', ')')
if (orderByExpression == null) { | ||
orderByExpression = expression; | ||
else { | ||
orderByExpression = ExpressionTreeRewriter.rewriteWith(new OrderByExpressionRewriter(byAlias), expression); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO instead of using byAlias
you should use outputScope
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may blow up, so please just give it a try
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Scope doesn't provide information about expressions. Extracted a separate method for building assignments map
Add to commit message that this commit fixes: #4698 |
a9ff893
to
6e2cdac
Compare
applied comments |
8d94fb8
to
4f62c5e
Compare
Instead of referencing the issue in the commit message, describe the actual problem. |
SQL standard specifies that ORDER BY expressions can reference projection outputs. For instance following query works: SELECT x as y FROM (VALUES(1) t(x)) ORDER BY y*-1; This commit adds support for referencing projection outputs from ORDER BY expressions. This fixes prestodb#4698
4f62c5e
to
9e44ae5
Compare
rebased & amended commits so it is more descriptive. |
I made a couple of minor changes and merged it. Thanks! |
This makes various TPCDS queries to execute unmodified
FYI: @martint