-
Notifications
You must be signed in to change notification settings - Fork 29.1k
[SPARK-34775][SQL] Push down limit through window when partitionSpec is not empty #32475
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5cb559d
[SPARK-34775][SQL] Push down limit through window when partitionSpec …
AngersZhuuuu c4f8220
Empty
leoluan2009 bf9d041
Merge branch 'master' into SPARK-34775
AngersZhuuuu b1be48b
update
AngersZhuuuu 78dd97a
Update LimitPushdownThroughWindowSuite.scala
AngersZhuuuu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,7 +17,7 @@ | |
|
|
||
| package org.apache.spark.sql.catalyst.optimizer | ||
|
|
||
| import org.apache.spark.sql.catalyst.expressions.{Alias, CurrentRow, IntegerLiteral, NamedExpression, RankLike, RowFrame, RowNumberLike, SpecifiedWindowFrame, UnboundedPreceding, WindowExpression, WindowSpecDefinition} | ||
| import org.apache.spark.sql.catalyst.expressions.{Alias, Ascending, CurrentRow, IntegerLiteral, NamedExpression, RankLike, RowFrame, RowNumberLike, SortOrder, SpecifiedWindowFrame, UnboundedPreceding, WindowExpression, WindowSpecDefinition} | ||
| import org.apache.spark.sql.catalyst.plans.logical.{Limit, LocalLimit, LogicalPlan, Project, Sort, Window} | ||
| import org.apache.spark.sql.catalyst.rules.Rule | ||
| import org.apache.spark.sql.catalyst.trees.TreePattern.{LIMIT, WINDOW} | ||
|
|
@@ -33,7 +33,7 @@ object LimitPushDownThroughWindow extends Rule[LogicalPlan] { | |
| // The window frame of RankLike and RowNumberLike can only be UNBOUNDED PRECEDING to CURRENT ROW. | ||
| private def supportsPushdownThroughWindow( | ||
| windowExpressions: Seq[NamedExpression]): Boolean = windowExpressions.forall { | ||
| case Alias(WindowExpression(_: RankLike | _: RowNumberLike, WindowSpecDefinition(Nil, _, | ||
| case Alias(WindowExpression(_: RankLike | _: RowNumberLike, WindowSpecDefinition(_, _, | ||
| SpecifiedWindowFrame(RowFrame, UnboundedPreceding, CurrentRow))), _) => true | ||
| case _ => false | ||
| } | ||
|
|
@@ -42,17 +42,19 @@ object LimitPushDownThroughWindow extends Rule[LogicalPlan] { | |
| _.containsAllPatterns(WINDOW, LIMIT), ruleId) { | ||
| // Adding an extra Limit below WINDOW when the partitionSpec of all window functions is empty. | ||
| case LocalLimit(limitExpr @ IntegerLiteral(limit), | ||
| window @ Window(windowExpressions, Nil, orderSpec, child)) | ||
| window @ Window(windowExpressions, partitionSpec, orderSpec, child)) | ||
| if supportsPushdownThroughWindow(windowExpressions) && child.maxRows.forall(_ > limit) && | ||
| limit < conf.topKSortFallbackThreshold => | ||
| // Sort is needed here because we need global sort. | ||
| window.copy(child = Limit(limitExpr, Sort(orderSpec, true, child))) | ||
| window.copy(child = Limit(limitExpr, | ||
| Sort(partitionSpec.map(SortOrder(_, Ascending)) ++ orderSpec, true, child))) | ||
|
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. @cloud-fan Do we need to changed the sort order from
Contributor
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. is |
||
| // There is a Project between LocalLimit and Window if they do not have the same output. | ||
| case LocalLimit(limitExpr @ IntegerLiteral(limit), project @ Project(_, | ||
| window @ Window(windowExpressions, Nil, orderSpec, child))) | ||
| window @ Window(windowExpressions, partitionSpec, orderSpec, child))) | ||
| if supportsPushdownThroughWindow(windowExpressions) && child.maxRows.forall(_ > limit) && | ||
| limit < conf.topKSortFallbackThreshold => | ||
| // Sort is needed here because we need global sort. | ||
| project.copy(child = window.copy(child = Limit(limitExpr, Sort(orderSpec, true, child)))) | ||
| project.copy(child = window.copy(child = Limit(limitExpr, | ||
| Sort(partitionSpec.map(SortOrder(_, Ascending)) ++ orderSpec, true, child)))) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
can you provide a bit more context? why can we pushdown limit through window when there are partition specs?