Skip to content
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

Conversation

sopel39
Copy link
Contributor

@sopel39 sopel39 commented Dec 5, 2016

This makes various TPCDS queries to execute unmodified

FYI: @martint

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)");
Copy link
Contributor

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

Copy link
Contributor

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?

Copy link
Contributor

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 '(', ')')

@sopel39 sopel39 force-pushed the feature_support_complex_order_by_expressions branch from aa93b4b to a9ff893 Compare December 5, 2016 08:08
Copy link
Contributor

@kokosing kokosing left a 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)");
Copy link
Contributor

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)");
Copy link
Contributor

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);
Copy link
Contributor

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

Copy link
Contributor

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

Copy link
Contributor Author

@sopel39 sopel39 Dec 6, 2016

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

@kokosing
Copy link
Contributor

kokosing commented Dec 5, 2016

Add to commit message that this commit fixes: #4698

@sopel39 sopel39 force-pushed the feature_support_complex_order_by_expressions branch from a9ff893 to 6e2cdac Compare December 6, 2016 12:59
@sopel39
Copy link
Contributor Author

sopel39 commented Dec 6, 2016

applied comments

@sopel39 sopel39 force-pushed the feature_support_complex_order_by_expressions branch 2 times, most recently from 8d94fb8 to 4f62c5e Compare December 6, 2016 13:41
@martint
Copy link
Contributor

martint commented Dec 12, 2016

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
@sopel39 sopel39 force-pushed the feature_support_complex_order_by_expressions branch from 4f62c5e to 9e44ae5 Compare December 12, 2016 09:13
@sopel39
Copy link
Contributor Author

sopel39 commented Dec 12, 2016

rebased & amended commits so it is more descriptive.

@martint
Copy link
Contributor

martint commented Dec 12, 2016

I made a couple of minor changes and merged it. Thanks!

@martint martint closed this Dec 12, 2016
@sopel39 sopel39 deleted the feature_support_complex_order_by_expressions branch January 9, 2017 08:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants