-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-32241][SQL] Remove empty children of union #29053
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
[SPARK-32241][SQL] Remove empty children of union #29053
Conversation
|
Test build #125472 has started for PR 29053 at commit |
| comparePlans(optimized, correctAnswer) | ||
| } | ||
|
|
||
| test("remove empty relation children from Union") { |
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.
Although this is an improvement, shall we add SPARK-32241: ?
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.
Fixed, thanks.
...catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/PropagateEmptyRelation.scala
Show resolved
Hide resolved
|
Also, cc @cloud-fan |
| if (newChildren.isEmpty) { | ||
| empty(p) | ||
| } else { | ||
| val newFirstChild = if (newChildren.head eq children.head) { |
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 can use sameOutput like this;
if (newChildren.isEmpty) {
empty(p)
} else {
val newPlan = if (newChildren.size > 1) Union(newChildren) else newChildren.head
// Describes why we need this handling
if (newPlan.sameOutput(p)) {
newPlan
} else {
val outputAliases = newPlan.output.zip(p.output).map { case (newAttr, outputName) =>
Alias(newAttr, outputName.name)(newAttr.exprId)
}
Project(outputAliases, newPlan)
}
}
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.
Seems to be a good idea, but sameOutput uses semanticEquals that fails if an expression is non-deterministic. How about just comparing the expression ids like this?
if (newChildren.isEmpty) {
empty(p)
} else {
val newPlan = if (newChildren.size > 1) Union(newChildren) else newChildren.head
val outputs = newPlan.output.zip(p.output)
// the original Union may produce different output attributes than the new one so we alias them if needed
if (outputs.forall { case (newAttr, oldAttr) => newAttr.exprId == oldAttr.exprId }) {
newPlan
} else {
val outputAliases = outputs.map { case (newAttr, oldAttr) =>
Alias(newAttr, oldAttr.name)(oldAttr.exprId)
}
Project(outputAliases, newPlan)
}
}
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've changed it to my version in the latest commit. @maropu please let me know if yours with sameOutput would be better.
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.
Yea, your version looks okay.
|
also cc: @viirya |
|
also @maryannxue :-) |
| val pl = children.head.output.zip(newChildren.head.output).map { | ||
| case (oa, na) => Alias(na, oa.name)(oa.exprId) | ||
| } | ||
| Project(pl, newChildren.head) | ||
| } |
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.
Looks like this may change output nullability of original Union?
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 it's corrected?
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.
This project looks correct. Just the new Union's output nullability might change.
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.
Maybe a stupid question, but is that an issue? It can change in one direction, from nullable to non-nullable.
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 should not be an issue. But the nullability updates may not be effective unless we run the rule UpdateAttributeNullability
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.
Ah ok, I see. So shall we run UpdateAttributeNullability after PropagateEmptyRelation in the Optimizer?
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.
currently UpdateAttributeNullability is put in a Once batch. We need to evaluate if it's OK (idempotent and cost-efficient) to run it in a fix-point batch.
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 looks like it is idempotent and doesn't do costly things just a simple bottom-up traversal on nodes and one expression traversal per node.
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've added UpdateAttributeNullability after PropagateEmptyRelation in the latest commit.
| comparePlans(optimized, correctAnswer) | ||
| } | ||
|
|
||
| test("SPARK-32241: remove empty relation children from Union") { |
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.
can we add a test to check the nullability update?
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.
A simple test case like here: b454cd7 is ok? Or you want me to test that UpdateAttributeNullability kicks in and fixes some nullability after PropagateEmptyRelation?
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.
This test case does not apply the UpdateAttributeNullability rules in Optimizer. Do you need to add a test case?
|
Test build #125610 has finished for PR 29053 at commit
|
|
Test build #125623 has started for PR 29053 at commit |
| PropagateEmptyRelation, | ||
| UpdateAttributeNullability) :: |
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.
Can you leave a comment above explaining why we run UpdateAttributeNullability here?
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.
Sure, added in the latest commit.
|
Test build #125630 has finished for PR 29053 at commit
|
|
Retest this please. |
|
Test build #125720 has finished for PR 29053 at commit
|
| newPlan | ||
| } else { | ||
| val outputAliases = outputs.map { case (newAttr, oldAttr) => | ||
| Alias(newAttr, oldAttr.name)(oldAttr.exprId) |
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.
Since this is introducing a new Alias, I'm wondering if we need to worry about explicitMetadata in this case.
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.
You are right, we can lose it in some cases. Fixed in cea0a48
|
Test build #125767 has finished for PR 29053 at commit
|
|
thanks, merging to master! |
|
Thanks for the review. |
|
Thank you, all! |
What changes were proposed in this pull request?
This PR removes the empty child relations of a
Union.E.g. the query
SELECT c FROM t UNION ALL SELECT c FROM t WHERE falsehas the following plan before this PR:and after this PR:
Why are the changes needed?
To have a simpler plan.
Does this PR introduce any user-facing change?
No.
How was this patch tested?
Added new UTs.