-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-20636] Add new optimization rule to transpose adjacent Window expressions. #17899
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 all commits
f444b2c
6f8c036
b7793cd
f183b78
1f21841
9a28535
76f1721
f840c69
e9f6928
72a4b3a
94e8115
4328831
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 |
|---|---|---|
|
|
@@ -734,6 +734,28 @@ object CollapseWindow extends Rule[LogicalPlan] { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Transpose Adjacent Window Expressions. | ||
| * - If the partition spec of the parent Window expression is compatible with the partition spec | ||
| * of the child window expression, transpose them. | ||
| */ | ||
| object TransposeWindow extends Rule[LogicalPlan] { | ||
| private def compatibleParititions(ps1 : Seq[Expression], ps2: Seq[Expression]): Boolean = { | ||
| ps1.length < ps2.length && ps2.take(ps1.length).permutations.exists(ps1.zip(_).forall { | ||
| case (l, r) => l.semanticEquals(r) | ||
| }) | ||
| } | ||
|
|
||
| def apply(plan: LogicalPlan): LogicalPlan = plan transformUp { | ||
| case w1 @ Window(we1, ps1, os1, w2 @ Window(we2, ps2, os2, grandChild)) | ||
|
||
| if w1.references.intersect(w2.windowOutputSet).isEmpty && | ||
| w1.expressions.forall(_.deterministic) && | ||
| w2.expressions.forall(_.deterministic) && | ||
| compatibleParititions(ps1, ps2) => | ||
| Project(w1.output, Window(we2, ps2, os2, Window(we1, ps1, os1, grandChild))) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Generate a list of additional filters from an operator's existing constraint but remove those | ||
| * that are either already part of the operator's condition or are part of the operator's child | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.sql.catalyst.optimizer | ||
|
|
||
| import org.apache.spark.sql.catalyst.dsl.expressions._ | ||
| import org.apache.spark.sql.catalyst.dsl.plans._ | ||
| import org.apache.spark.sql.catalyst.expressions.Rand | ||
| import org.apache.spark.sql.catalyst.plans.PlanTest | ||
| import org.apache.spark.sql.catalyst.plans.logical.{LocalRelation, LogicalPlan} | ||
| import org.apache.spark.sql.catalyst.rules.RuleExecutor | ||
|
|
||
| class TransposeWindowSuite extends PlanTest { | ||
| object Optimize extends RuleExecutor[LogicalPlan] { | ||
| val batches = | ||
| Batch("CollapseProject", FixedPoint(100), CollapseProject, RemoveRedundantProject) :: | ||
| Batch("FlipWindow", Once, CollapseWindow, TransposeWindow) :: Nil | ||
| } | ||
|
|
||
| val testRelation = LocalRelation('a.string, 'b.string, 'c.int, 'd.string) | ||
|
|
||
| val a = testRelation.output(0) | ||
| val b = testRelation.output(1) | ||
| val c = testRelation.output(2) | ||
| val d = testRelation.output(3) | ||
|
|
||
| val partitionSpec1 = Seq(a) | ||
| val partitionSpec2 = Seq(a, b) | ||
| val partitionSpec3 = Seq(d) | ||
| val partitionSpec4 = Seq(b, a, d) | ||
|
|
||
| val orderSpec1 = Seq(d.asc) | ||
| val orderSpec2 = Seq(d.desc) | ||
|
|
||
| test("transpose two adjacent windows with compatible partitions") { | ||
| val query = testRelation | ||
| .window(Seq(sum(c).as('sum_a_2)), partitionSpec2, orderSpec2) | ||
| .window(Seq(sum(c).as('sum_a_1)), partitionSpec1, orderSpec1) | ||
|
|
||
| val analyzed = query.analyze | ||
| val optimized = Optimize.execute(analyzed) | ||
|
|
||
| val correctAnswer = testRelation | ||
| .window(Seq(sum(c).as('sum_a_1)), partitionSpec1, orderSpec1) | ||
| .window(Seq(sum(c).as('sum_a_2)), partitionSpec2, orderSpec2) | ||
| .select('a, 'b, 'c, 'd, 'sum_a_2, 'sum_a_1) | ||
|
|
||
| comparePlans(optimized, correctAnswer.analyze) | ||
| } | ||
|
|
||
| test("transpose two adjacent windows with differently ordered compatible partitions") { | ||
| val query = testRelation | ||
| .window(Seq(sum(c).as('sum_a_2)), partitionSpec4, Seq.empty) | ||
| .window(Seq(sum(c).as('sum_a_1)), partitionSpec2, Seq.empty) | ||
|
|
||
| val analyzed = query.analyze | ||
| val optimized = Optimize.execute(analyzed) | ||
|
|
||
| val correctAnswer = testRelation | ||
| .window(Seq(sum(c).as('sum_a_1)), partitionSpec2, Seq.empty) | ||
| .window(Seq(sum(c).as('sum_a_2)), partitionSpec4, Seq.empty) | ||
| .select('a, 'b, 'c, 'd, 'sum_a_2, 'sum_a_1) | ||
|
|
||
| comparePlans(optimized, correctAnswer.analyze) | ||
| } | ||
|
|
||
| test("don't transpose two adjacent windows with incompatible partitions") { | ||
| val query = testRelation | ||
| .window(Seq(sum(c).as('sum_a_2)), partitionSpec3, Seq.empty) | ||
| .window(Seq(sum(c).as('sum_a_1)), partitionSpec1, Seq.empty) | ||
|
|
||
| val analyzed = query.analyze | ||
| val optimized = Optimize.execute(analyzed) | ||
|
|
||
| comparePlans(optimized, analyzed) | ||
| } | ||
|
|
||
| test("don't transpose two adjacent windows with intersection of partition and output set") { | ||
| val query = testRelation | ||
| .window(Seq(('a + 'b).as('e), sum(c).as('sum_a_2)), partitionSpec3, Seq.empty) | ||
| .window(Seq(sum(c).as('sum_a_1)), Seq(a, 'e), Seq.empty) | ||
|
|
||
| val analyzed = query.analyze | ||
| val optimized = Optimize.execute(analyzed) | ||
|
|
||
| comparePlans(optimized, analyzed) | ||
| } | ||
|
|
||
| test("don't transpose two adjacent windows with non-deterministic expressions") { | ||
| val query = testRelation | ||
| .window(Seq(Rand(0).as('e), sum(c).as('sum_a_2)), partitionSpec3, Seq.empty) | ||
| .window(Seq(sum(c).as('sum_a_1)), partitionSpec1, Seq.empty) | ||
|
|
||
| val analyzed = query.analyze | ||
| val optimized = Optimize.execute(analyzed) | ||
|
|
||
| comparePlans(optimized, analyzed) | ||
| } | ||
|
|
||
| } |
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.
why is this rule useful?