-
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
Simplify coalesce expressions #12192
Simplify coalesce expressions #12192
Conversation
presto-main/src/main/java/com/facebook/presto/sql/planner/ExpressionInterpreter.java
Outdated
Show resolved
Hide resolved
presto-main/src/main/java/com/facebook/presto/sql/planner/ExpressionInterpreter.java
Outdated
Show resolved
Hide resolved
presto-main/src/main/java/com/facebook/presto/sql/planner/ExpressionInterpreter.java
Outdated
Show resolved
Hide resolved
presto-main/src/test/java/com/facebook/presto/sql/TestExpressionInterpreter.java
Show resolved
Hide resolved
0cb5678
to
7f0e4ad
Compare
@kokosing Have added support for nested coalesce expressions. |
7f0e4ad
to
3dd19a7
Compare
@sopel39 Would you like to take a look also? |
@@ -546,19 +548,38 @@ protected Object visitCoalesceExpression(CoalesceExpression node, Object context | |||
List<Object> values = node.getOperands().stream() | |||
.map(value -> processWithExceptionHandling(value, context)) | |||
.filter(Objects::nonNull) | |||
.flatMap(expression -> { | |||
if (expression instanceof CoalesceExpression) { | |||
return ((CoalesceExpression) expression).getOperands().stream(); |
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.
what if there are more levels of recursion? Make it recursive
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.
But the current model is recursive right. We have added tests with more levels of coalesce
and it passed it.
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 only supports one level of recursion.
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.
Won't ExpressionInterpreter#processWithExceptionHandling
will call other levels ? We just need to check if one of argument is of CoalesceExpression
If yes we will extract its arguments and push it to the outer expression and processWithExceptionHandling will ideally handle at more levels
If I am not wrong recursive implementation will simplify
coalesce(coalesce(unbound_long, coalesce(unbound_long, 1)), unbound_long2)
to coalesce(unbound_long, 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.
Right, thanks
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.
Sorry I have replaced it with isDeterministic((CoalesceExpression) 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.
I mean I think that isDeterministic((CoalesceExpression) expression)
can be removed completely. We don't need to check if nested coalesce is deterministic
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.
But in that case it will simplify coalesce(unbound_long, coalesce(random(), 1)
to coalesce(unbound_long, random(), 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.
@sopel39 Removed the deterministic check as it is not required now
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.
Thank you. Ping us when release is done to land it
presto-main/src/main/java/com/facebook/presto/sql/planner/ExpressionInterpreter.java
Outdated
Show resolved
Hide resolved
presto-main/src/main/java/com/facebook/presto/sql/planner/ExpressionInterpreter.java
Outdated
Show resolved
Hide resolved
@Praveen2112 this will help a bit with #12155 (as per #12155 (comment)). Nice |
3dd19a7
to
fe3ef94
Compare
@@ -546,19 +548,38 @@ protected Object visitCoalesceExpression(CoalesceExpression node, Object context | |||
List<Object> values = node.getOperands().stream() | |||
.map(value -> processWithExceptionHandling(value, context)) | |||
.filter(Objects::nonNull) | |||
.flatMap(expression -> { | |||
if (expression instanceof CoalesceExpression) { | |||
return ((CoalesceExpression) expression).getOperands().stream(); |
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.
Right, thanks
presto-main/src/main/java/com/facebook/presto/sql/planner/ExpressionInterpreter.java
Show resolved
Hide resolved
6f1e657
to
0993125
Compare
b843fa2
to
8fc124b
Compare
8fc124b
to
2803ca6
Compare
Merged |
For coalesce expression like
coalesce(colA, colA)
can be simplified tocolA
and for expressions likecoalesce('1', colA)
can be simplified to'1'
. This patch captures those pattern and simplifies them. Also updated the test for the above changes