-
Notifications
You must be signed in to change notification settings - Fork 29k
[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 1 commit
f444b2c
6f8c036
b7793cd
f183b78
1f21841
9a28535
76f1721
f840c69
e9f6928
72a4b3a
94e8115
4328831
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -88,6 +88,7 @@ abstract class Optimizer(sessionCatalog: SessionCatalog) | |
| CollapseRepartition, | ||
| CollapseProject, | ||
| CollapseWindow, | ||
| FlipWindow, | ||
| CombineFilters, | ||
| CombineLimits, | ||
| CombineUnions, | ||
|
|
@@ -624,6 +625,19 @@ object CollapseWindow extends Rule[LogicalPlan] { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Flip Adjacent Window Expressions. | ||
| * - If the partition spec of the parent Window expression is a subset of the partition spec | ||
| * of the child window expression, flip them. | ||
| */ | ||
| object FlipWindow extends Rule[LogicalPlan] { | ||
| def apply(plan: LogicalPlan): LogicalPlan = plan transformUp { | ||
| case w1 @ Window(we1, ps1, os1, w2 @ Window(we2, ps2, os2, grandChild)) | ||
| if ps2.containsSlice(ps1) => | ||
|
||
| 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,97 @@ | ||
| /* | ||
| * 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.{RowFrame, SpecifiedWindowFrame, UnboundedFollowing, UnboundedPreceding, UnspecifiedFrame} | ||
| 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 FlipWindowSuite extends PlanTest { | ||
| object Optimize extends RuleExecutor[LogicalPlan] { | ||
| val batches = | ||
| Batch("CollapseProject", FixedPoint(100), CollapseProject, RemoveRedundantProject) :: | ||
| Batch("FlipWindow", Once, CollapseWindow, FlipWindow) :: 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 orderSpec1 = Seq(d.asc) | ||
| val orderSpec2 = Seq(d.desc) | ||
|
|
||
| test("flip two adjacent windows with compatible partitions in multiple selects") { | ||
| val query = testRelation | ||
| .select('a, 'b, 'c, windowExpr(sum('c), windowSpec(partitionSpec2, Seq.empty, UnspecifiedFrame)).as('sum_a_2)) | ||
| .select('a, 'b, 'c, 'sum_a_2, windowExpr(sum('c), windowSpec(partitionSpec1, Seq.empty, UnspecifiedFrame)).as('sum_a_1)) | ||
|
|
||
| val analyzed = query.analyze | ||
| val optimized = Optimize.execute(analyzed) | ||
|
|
||
| val query2 = testRelation | ||
| .select('a, 'b, 'c, windowExpr(sum('c), windowSpec(partitionSpec2, Seq.empty, UnspecifiedFrame)).as('sum_a_2), | ||
| windowExpr(sum('c), windowSpec(partitionSpec1, Seq.empty, UnspecifiedFrame)).as('sum_a_1) | ||
| ) | ||
|
|
||
| val correctAnswer = Optimize.execute(query2.analyze) | ||
|
|
||
| comparePlans(optimized, correctAnswer) | ||
| } | ||
|
|
||
| test("flip 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) | ||
|
|
||
| comparePlans(optimized, correctAnswer.analyze) | ||
| } | ||
|
|
||
| test("don't flip 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) | ||
|
|
||
| val correctAnswer = testRelation | ||
| .window(Seq(sum(c).as('sum_a_2)), partitionSpec3, Seq.empty) | ||
| .window(Seq(sum(c).as('sum_a_1)), partitionSpec1, Seq.empty) | ||
|
|
||
| comparePlans(optimized, correctAnswer.analyze) | ||
| } | ||
|
|
||
| } |
Uh oh!
There was an error while loading. Please reload this page.
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.
The expressions in both
w1.expressionsandw2.expressionsmust be deterministic. If not, we should not flipThere 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? This seems overly restrictive to me.
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.
Just to ensure the results are still the same with and without the rule.