-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-19601] [SQL] Fix CollapseRepartition rule to preserve shuffle-enabled Repartition #16933
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
Changes from 5 commits
7b4a9dd
7722781
f9483cb
0f95a6f
680c3af
5453ad4
4649af4
8306c49
379a15a
d69c5a1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -562,27 +562,43 @@ object CollapseProject extends Rule[LogicalPlan] { | |
| } | ||
|
|
||
| /** | ||
| * Combines adjacent [[Repartition]] and [[RepartitionByExpression]] operator combinations | ||
| * by keeping only the one. | ||
| * 1. For adjacent [[Repartition]]s, collapse into the last [[Repartition]]. | ||
| * 2. For adjacent [[RepartitionByExpression]]s, collapse into the last [[RepartitionByExpression]]. | ||
| * 3. For a combination of [[Repartition]] and [[RepartitionByExpression]], collapse as a single | ||
| * [[RepartitionByExpression]] with the expression and last number of partition. | ||
| * Combines adjacent [[RepartitionOperation]] operators | ||
| */ | ||
| object CollapseRepartition extends Rule[LogicalPlan] { | ||
| def apply(plan: LogicalPlan): LogicalPlan = plan transformUp { | ||
| // Case 1 | ||
| case Repartition(numPartitions, shuffle, Repartition(_, _, child)) => | ||
| Repartition(numPartitions, shuffle, child) | ||
| // Case 2 | ||
| case RepartitionByExpression(exprs, RepartitionByExpression(_, child, _), numPartitions) => | ||
| RepartitionByExpression(exprs, child, numPartitions) | ||
| // Case 3 | ||
| case Repartition(numPartitions, _, r: RepartitionByExpression) => | ||
| r.copy(numPartitions = numPartitions) | ||
| // Case 3 | ||
| case RepartitionByExpression(exprs, Repartition(_, _, child), numPartitions) => | ||
| RepartitionByExpression(exprs, child, numPartitions) | ||
| // Case 1: When a Repartition has a child of Repartition or RepartitionByExpression, | ||
| // we can collapse it with the child based on the type of shuffle and the specified number | ||
| // of partitions. | ||
| case r @ Repartition(_, _, child: Repartition) => | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can just write one case
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great! |
||
| collapseRepartition(r, child) | ||
| case r @ Repartition(_, _, child: RepartitionByExpression) => | ||
| collapseRepartition(r, child) | ||
| // Case 2: When a RepartitionByExpression has a child of Repartition or RepartitionByExpression | ||
| // we can remove the child. | ||
| case r @ RepartitionByExpression(_, child: RepartitionByExpression, _) => | ||
| r.copy(child = child.child) | ||
| case r @ RepartitionByExpression(_, child: Repartition, _) => | ||
| r.copy(child = child.child) | ||
| } | ||
|
|
||
| /** | ||
| * Collapses the [[Repartition]] with its child [[RepartitionOperation]], if possible. | ||
| * - Case 1 the top [[Repartition]] does not enable shuffle (i.e., coalesce API): | ||
| * If the last numPartitions is bigger, returns the child node; otherwise, keep unchanged. | ||
| * - Case 2 the top [[Repartition]] enables shuffle (i.e., repartition API): | ||
| * returns the child node with the last numPartitions. | ||
| */ | ||
| private def collapseRepartition(r: Repartition, child: RepartitionOperation): LogicalPlan = { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we inline this method? |
||
| (r.shuffle, child.shuffle) match { | ||
| case (false, true) => child match { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why this pattern match? we can just call
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes! |
||
| case c: Repartition => if (r.numPartitions >= c.numPartitions) c else r | ||
| case c: RepartitionByExpression => if (r.numPartitions >= c.numPartitions) c else r | ||
| } | ||
| case _ => child match { | ||
| case child: Repartition => child.copy(numPartitions = r.numPartitions, shuffle = r.shuffle) | ||
| case child: RepartitionByExpression => child.copy(numPartitions = r.numPartitions) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,47 +32,180 @@ class CollapseRepartitionSuite extends PlanTest { | |
|
|
||
| val testRelation = LocalRelation('a.int, 'b.int) | ||
|
|
||
|
|
||
| test("collapse two adjacent coalesces into one") { | ||
| // Always respects the top coalesces amd removes useless coalesce below coalesce | ||
| val query1 = testRelation | ||
| .coalesce(10) | ||
| .coalesce(20) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm, I can see the argument.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be better to respect the later input number, which is specified by users, for avoiding any surprise to users.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, agreed. |
||
| val query2 = testRelation | ||
| .coalesce(30) | ||
| .coalesce(20) | ||
|
|
||
| val optimized1 = Optimize.execute(query1.analyze) | ||
| val optimized2 = Optimize.execute(query2.analyze) | ||
|
|
||
| val correctAnswer = testRelation.coalesce(20).analyze | ||
|
|
||
| comparePlans(optimized1, correctAnswer) | ||
| comparePlans(optimized2, correctAnswer) | ||
| } | ||
|
|
||
| test("collapse two adjacent repartitions into one") { | ||
| val query = testRelation | ||
| // Always respects the top repartition amd removes useless repartition below repartition | ||
| val query1 = testRelation | ||
| .repartition(10) | ||
| .repartition(20) | ||
| val query2 = testRelation | ||
| .repartition(30) | ||
| .repartition(20) | ||
|
|
||
| val optimized1 = Optimize.execute(query1.analyze) | ||
| val optimized2 = Optimize.execute(query2.analyze) | ||
| val correctAnswer = testRelation.repartition(20).analyze | ||
|
|
||
| comparePlans(optimized1, correctAnswer) | ||
| comparePlans(optimized2, correctAnswer) | ||
| } | ||
|
|
||
| test("coalesce above repartition") { | ||
| // Remove useless coalesce above repartition | ||
| val query1 = testRelation | ||
| .repartition(10) | ||
| .coalesce(20) | ||
|
|
||
| val optimized1 = Optimize.execute(query1.analyze) | ||
| val correctAnswer1 = testRelation.repartition(10).analyze | ||
|
|
||
| comparePlans(optimized1, correctAnswer1) | ||
|
|
||
| // No change in this case | ||
| val query2 = testRelation | ||
| .repartition(30) | ||
| .coalesce(20) | ||
|
|
||
| val optimized2 = Optimize.execute(query2.analyze) | ||
| val correctAnswer2 = query2.analyze | ||
|
|
||
| comparePlans(optimized2, correctAnswer2) | ||
| } | ||
|
|
||
| test("repartition above coalesce") { | ||
| // Always respects the top repartition amd removes useless coalesce below repartition | ||
| val query1 = testRelation | ||
| .coalesce(10) | ||
| .repartition(20) | ||
| // Remove useless coalesce above repartition | ||
| val query2 = testRelation | ||
| .coalesce(30) | ||
| .repartition(20) | ||
|
|
||
| val optimized = Optimize.execute(query.analyze) | ||
| val optimized1 = Optimize.execute(query1.analyze) | ||
| val optimized2 = Optimize.execute(query2.analyze) | ||
|
|
||
| val correctAnswer = testRelation.repartition(20).analyze | ||
|
|
||
| comparePlans(optimized, correctAnswer) | ||
| comparePlans(optimized1, correctAnswer) | ||
| comparePlans(optimized2, correctAnswer) | ||
| } | ||
|
|
||
| test("collapse repartition and repartitionBy into one") { | ||
| val query = testRelation | ||
| test("repartitionBy above repartition") { | ||
| val query1 = testRelation | ||
| .repartition(10) | ||
| .distribute('a)(20) | ||
|
|
||
| val optimized = Optimize.execute(query.analyze) | ||
| val correctAnswer = testRelation.distribute('a)(20).analyze | ||
| val optimized1 = Optimize.execute(query1.analyze) | ||
| val correctAnswer1 = testRelation.distribute('a)(20).analyze | ||
|
|
||
| comparePlans(optimized1, correctAnswer1) | ||
|
|
||
| val query2 = testRelation | ||
| .repartition(30) | ||
| .distribute('a)(20) | ||
|
|
||
| val optimized2 = Optimize.execute(query2.analyze) | ||
| val correctAnswer2 = testRelation.distribute('a)(20).analyze | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is same as |
||
|
|
||
| comparePlans(optimized, correctAnswer) | ||
| comparePlans(optimized2, correctAnswer2) | ||
| } | ||
|
|
||
| test("collapse repartitionBy and repartition into one") { | ||
| val query = testRelation | ||
| test("repartitionBy above coalesce") { | ||
| val query1 = testRelation | ||
| .coalesce(10) | ||
| .distribute('a)(20) | ||
|
|
||
| val optimized1 = Optimize.execute(query1.analyze) | ||
| val correctAnswer1 = testRelation.distribute('a)(20).analyze | ||
|
|
||
| comparePlans(optimized1, correctAnswer1) | ||
|
|
||
| val query2 = testRelation | ||
| .coalesce(20) | ||
| .distribute('a)(30) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd like to make i.e. the numPartitions of |
||
|
|
||
| val optimized2 = Optimize.execute(query2.analyze) | ||
| val correctAnswer2 = testRelation.distribute('a)(30).analyze | ||
|
|
||
| comparePlans(optimized2, correctAnswer2) | ||
| } | ||
|
|
||
| test("repartition above repartitionBy") { | ||
| val query1 = testRelation | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can add a comment: |
||
| .distribute('a)(20) | ||
| .repartition(10) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can still pick the same numPartition pairs: |
||
|
|
||
| val optimized = Optimize.execute(query.analyze) | ||
| val correctAnswer = testRelation.distribute('a)(10).analyze | ||
| val optimized1 = Optimize.execute(query1.analyze) | ||
| val correctAnswer1 = testRelation.distribute('a)(10).analyze | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I not quite sure about this. Shall we optimize to
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here, I just followed what we did before. After more code reading, I think we can do it, since case logical.Repartition(numPartitions, shuffle, child) =>
if (shuffle) {
ShuffleExchange(RoundRobinPartitioning(numPartitions), planLater(child)) :: Nil
} else {
execution.CoalesceExec(numPartitions, planLater(child)) :: Nil
}
case logical.RepartitionByExpression(expressions, child, numPartitions) =>
exchange.ShuffleExchange(HashPartitioning(
expressions, numPartitions), planLater(child)) :: Nil
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My concern is, optimization should not change the result. |
||
|
|
||
| comparePlans(optimized1, correctAnswer1) | ||
|
|
||
| val query2 = testRelation | ||
| .distribute('a)(20) | ||
| .repartition(30) | ||
|
|
||
| val optimized2 = Optimize.execute(query2.analyze) | ||
| val correctAnswer2 = testRelation.distribute('a)(30).analyze | ||
|
|
||
| comparePlans(optimized, correctAnswer) | ||
| comparePlans(optimized2, correctAnswer2) | ||
| } | ||
|
|
||
| test("coalesce above repartitionBy") { | ||
| val query1 = testRelation | ||
| .distribute('a)(20) | ||
| .coalesce(10) | ||
|
|
||
| val optimized1 = Optimize.execute(query1.analyze) | ||
| val correctAnswer1 = testRelation.distribute('a)(20).coalesce(10).analyze | ||
|
|
||
| comparePlans(optimized1, correctAnswer1) | ||
|
|
||
| val query2 = testRelation | ||
| .distribute('a)(20) | ||
| .coalesce(30) | ||
|
|
||
| val optimized2 = Optimize.execute(query2.analyze) | ||
| val correctAnswer2 = testRelation.distribute('a)(20).analyze | ||
|
|
||
| comparePlans(optimized2, correctAnswer2) | ||
| } | ||
|
|
||
| test("collapse two adjacent repartitionBys into one") { | ||
| val query = testRelation | ||
| val query1 = testRelation | ||
| .distribute('b)(10) | ||
| .distribute('a)(20) | ||
|
|
||
| val optimized = Optimize.execute(query.analyze) | ||
| val correctAnswer = testRelation.distribute('a)(20).analyze | ||
| val optimized1 = Optimize.execute(query1.analyze) | ||
| val correctAnswer1 = testRelation.distribute('a)(20).analyze | ||
|
|
||
| comparePlans(optimized1, correctAnswer1) | ||
|
|
||
| val query2 = testRelation | ||
| .distribute('b)(30) | ||
| .distribute('a)(20) | ||
|
|
||
| val optimized2 = Optimize.execute(query2.analyze) | ||
| val correctAnswer2 = testRelation.distribute('a)(20).analyze | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's same with |
||
|
|
||
| comparePlans(optimized, correctAnswer) | ||
| comparePlans(optimized2, correctAnswer2) | ||
| } | ||
| } | ||
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.
does it conflict with sql coalesce by having it 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.
They are used for the test cases in catalyst package, in which Dataset APIs are not available. Thus, that is why we add these DSL for test cases