-
Notifications
You must be signed in to change notification settings - Fork 29k
[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 1 commit
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 |
|---|---|---|
|
|
@@ -563,23 +563,27 @@ 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]]. | ||
| * 1. For adjacent [[Repartition]]s, collapse into the last [[Repartition]] if their shuffle types | ||
| * are the same or the parent's shuffle is true. | ||
| * 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. | ||
| * 3. When a shuffle-enabled [[Repartition]] is above a [[RepartitionByExpression]], collapse as a | ||
| * single [[RepartitionByExpression]] with the expression and the last number of partition. | ||
| * 4. When a [[RepartitionByExpression]] is above a [[Repartition]], collapse as a single | ||
|
||
| * [[RepartitionByExpression]] with the expression and the last number of partition. | ||
| */ | ||
| object CollapseRepartition extends Rule[LogicalPlan] { | ||
| def apply(plan: LogicalPlan): LogicalPlan = plan transformUp { | ||
| // Case 1 | ||
| case Repartition(numPartitions, shuffle, Repartition(_, _, child)) => | ||
| case Repartition(numPartitions, shuffle, Repartition(_, shuffleChild, child)) | ||
| if shuffle == shuffleChild || shuffle => | ||
|
||
| Repartition(numPartitions, shuffle, child) | ||
| // Case 2 | ||
| case RepartitionByExpression(exprs, RepartitionByExpression(_, child, _), numPartitions) => | ||
| RepartitionByExpression(exprs, child, numPartitions) | ||
| // Case 3 | ||
| case Repartition(numPartitions, _, r: RepartitionByExpression) => | ||
| case Repartition(numPartitions, shuffle, r: RepartitionByExpression) if shuffle => | ||
| r.copy(numPartitions = Some(numPartitions)) | ||
| // Case 3 | ||
| // Case 4 | ||
| case RepartitionByExpression(exprs, Repartition(_, _, child), numPartitions) => | ||
| RepartitionByExpression(exprs, child, numPartitions) | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,18 @@ class CollapseRepartitionSuite extends PlanTest { | |
|
|
||
| val testRelation = LocalRelation('a.int, 'b.int) | ||
|
|
||
|
|
||
| test("collapse two adjacent coalesces into one") { | ||
| val query = 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 optimized = Optimize.execute(query.analyze) | ||
| val correctAnswer = testRelation.coalesce(20).analyze | ||
|
|
||
| comparePlans(optimized, correctAnswer) | ||
| } | ||
|
|
||
| test("collapse two adjacent repartitions into one") { | ||
| val query = testRelation | ||
| .repartition(10) | ||
|
|
@@ -43,15 +55,44 @@ class CollapseRepartitionSuite extends PlanTest { | |
| comparePlans(optimized, correctAnswer) | ||
| } | ||
|
|
||
| test("collapse one coalesce and one repartition into one") { | ||
| val query1 = testRelation | ||
| .coalesce(20) | ||
| .repartition(5) | ||
|
|
||
| val optimized1 = Optimize.execute(query1.analyze) | ||
| val correctAnswer1 = testRelation.repartition(5).analyze | ||
|
|
||
| comparePlans(optimized1, correctAnswer1) | ||
|
|
||
| val query2 = testRelation | ||
| .repartition(5) | ||
| .coalesce(20) | ||
|
|
||
| val optimized2 = Optimize.execute(query2.analyze) | ||
| val correctAnswer2 = testRelation.repartition(5).coalesce(20).analyze | ||
|
||
|
|
||
| comparePlans(optimized2, correctAnswer2) | ||
| } | ||
|
|
||
| test("collapse repartition and repartitionBy into one") { | ||
| val query = testRelation | ||
| 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(optimized, correctAnswer) | ||
| comparePlans(optimized1, correctAnswer1) | ||
|
|
||
| val query2 = testRelation | ||
| .coalesce(10) | ||
| .distribute('a)(20) | ||
|
|
||
| val optimized2 = Optimize.execute(query2.analyze) | ||
| val correctAnswer2 = testRelation.distribute('a)(20).analyze | ||
|
|
||
| comparePlans(optimized2, correctAnswer2) | ||
| } | ||
|
|
||
| test("collapse repartitionBy and repartition into one") { | ||
|
|
@@ -65,6 +106,17 @@ class CollapseRepartitionSuite extends PlanTest { | |
| comparePlans(optimized, correctAnswer) | ||
| } | ||
|
|
||
| test("do not collapse coalesce above repartitionBy into one") { | ||
| val query = testRelation | ||
| .distribute('a)(20) | ||
| .coalesce(10) | ||
|
|
||
| val optimized = Optimize.execute(query.analyze) | ||
| val correctAnswer = testRelation.distribute('a)(20).coalesce(10).analyze | ||
|
|
||
| comparePlans(optimized, correctAnswer) | ||
| } | ||
|
|
||
| test("collapse two adjacent repartitionBys into one") { | ||
| val query = testRelation | ||
| .distribute('b)(10) | ||
|
|
||
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