Skip to content
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 @@ -1257,9 +1257,9 @@ object CollapseWindow extends Rule[LogicalPlan] {
*/
object TransposeWindow extends Rule[LogicalPlan] {
private def compatiblePartitions(ps1 : Seq[Expression], ps2: Seq[Expression]): Boolean = {
ps1.length < ps2.length && ps2.take(ps1.length).permutations.exists(ps1.zip(_).forall {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd consider this as a perf bug...

Copy link
Author

Choose a reason for hiding this comment

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

You mean we should only optimize the performance without changing the logic?Maybe we could find a way to avoid the permutation

Copy link
Contributor

Choose a reason for hiding this comment

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

I mean the previous code is kind of buggy. This PR is more like a bug fix instead of perf improvement and we should backport it.

case (l, r) => l.semanticEquals(r)
})
ps1.length < ps2.length && ps1.forall { expr1 =>
ps2.exists(expr1.semanticEquals)
}
}

private def windowsCompatible(w1: Window, w2: Window): Boolean = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,21 @@ class TransposeWindowSuite extends PlanTest {
comparePlans(optimized, analyzed)
}

test("SPARK-38034: transpose two adjacent windows with compatible partitions " +
"which is not a prefix") {
val query = testRelation
.window(Seq(sum(c).as('sum_a_2)), partitionSpec4, orderSpec2)
.window(Seq(sum(c).as('sum_a_1)), partitionSpec3, orderSpec1)

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

val correctAnswer = testRelation
.window(Seq(sum(c).as('sum_a_1)), partitionSpec3, orderSpec1)
.window(Seq(sum(c).as('sum_a_2)), partitionSpec4, orderSpec2)
.select('a, 'b, 'c, 'd, 'sum_a_2, 'sum_a_1)

comparePlans(optimized, correctAnswer.analyze)
}

}