Skip to content
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-27514] Skip collapsing windows with empty window expressions #24411

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,7 @@ object CollapseWindow extends Rule[LogicalPlan] {
def apply(plan: LogicalPlan): LogicalPlan = plan transformUp {
case w1 @ Window(we1, ps1, os1, w2 @ Window(we2, ps2, os2, grandChild))
if ps1 == ps2 && os1 == os2 && w1.references.intersect(w2.windowOutputSet).isEmpty &&
we1.nonEmpty && we2.nonEmpty &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just for curiosity: do you know why we can't collapse windows with empty window expressions? Seems we can change WindowFunctionType.functionType(we1.head) == WindowFunctionType.functionType(we2.head) to we1.isEmpty || we2.isEmpty || WindowFunctionType.functionType(we1.head) == WindowFunctionType.functionType(we2.head)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think either change would be equivalent. If we don't collapse here, the rule that removes no-ops would prune out the empty window anyways, effectively resulting in the same collapsing behavior.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah i see, makes sense!

// This assumes Window contains the same type of window expressions. This is ensured
// by ExtractWindowFunctions.
WindowFunctionType.functionType(we1.head) == WindowFunctionType.functionType(we2.head) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,15 @@ class CollapseWindowSuite extends PlanTest {
val optimized = Optimize.execute(query.analyze)
comparePlans(optimized, expected)
}

test("Skip windows with empty window expressions") {
val query = testRelation
.window(Seq(), partitionSpec1, orderSpec1)
.window(Seq(sum(a).as('sum_a)), partitionSpec1, orderSpec1)

val optimized = Optimize.execute(query.analyze)
val correctAnswer = query.analyze

comparePlans(optimized, correctAnswer)
}
}