beliefer commented on code in PR #36541:
URL: https://github.com/apache/spark/pull/36541#discussion_r873402602
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/SparkStrategies.scala:
##########
@@ -81,55 +81,56 @@ abstract class SparkStrategies extends
QueryPlanner[SparkPlan] {
*/
object SpecialLimits extends Strategy {
override def apply(plan: LogicalPlan): Seq[SparkPlan] = plan match {
- case ReturnAnswer(rootPlan) => rootPlan match {
- case Limit(IntegerLiteral(limit), Sort(order, true, child))
- if limit < conf.topKSortFallbackThreshold =>
- TakeOrderedAndProjectExec(limit, order, child.output,
planLater(child)) :: Nil
- case Limit(IntegerLiteral(limit), Project(projectList, Sort(order,
true, child)))
- if limit < conf.topKSortFallbackThreshold =>
- TakeOrderedAndProjectExec(limit, order, projectList,
planLater(child)) :: Nil
+ // Call `planTakeOrdered` first which matches a larger plan.
+ case ReturnAnswer(rootPlan) =>
planTakeOrdered(rootPlan).getOrElse(rootPlan match {
+ // We should match the combination of limit and offset first, to get
the optimal physical
+ // plan, instead of planning limit and offset separately.
+ case LimitAndOffset(limit, offset, child) =>
+ CollectLimitExec(limit = limit, child = planLater(child), offset =
offset)
+ case OffsetAndLimit(offset, limit, child) =>
+ // 'Offset a' then 'Limit b' is the same as 'Limit a + b' then
'Offset a'.
+ CollectLimitExec(limit = offset + limit, child = planLater(child),
offset = offset)
case Limit(IntegerLiteral(limit), child) =>
- CollectLimitExec(limit, planLater(child)) :: Nil
- case LimitAndOffset(IntegerLiteral(limit), IntegerLiteral(offset),
- Sort(order, true, child)) if limit + offset <
conf.topKSortFallbackThreshold =>
- TakeOrderedAndProjectExec(
- limit, order, child.output, planLater(child), offset) :: Nil
- case LimitAndOffset(IntegerLiteral(limit), IntegerLiteral(offset),
- Project(projectList, Sort(order, true, child)))
- if limit + offset < conf.topKSortFallbackThreshold =>
- TakeOrderedAndProjectExec(
- limit, order, projectList, planLater(child), offset) :: Nil
- case LimitAndOffset(IntegerLiteral(limit), IntegerLiteral(offset),
child) =>
- CollectLimitExec(limit, planLater(child), offset) :: Nil
+ CollectLimitExec(limit = limit, child = planLater(child))
case logical.Offset(IntegerLiteral(offset), child) =>
- CollectLimitExec(child = planLater(child), offset = offset) :: Nil
+ CollectLimitExec(child = planLater(child), offset = offset)
case Tail(IntegerLiteral(limit), child) =>
- CollectTailExec(limit, planLater(child)) :: Nil
- case other => planLater(other) :: Nil
- }
+ CollectTailExec(limit, planLater(child))
+ case other => planLater(other)
+ }) :: Nil
+
+ case other => planTakeOrdered(other).toSeq
+ }
+
+ private def planTakeOrdered(plan: LogicalPlan): Option[SparkPlan] = plan
match {
+ // We should match the combination of limit and offset first, to get the
optimal physical
+ // plan, instead of planning limit and offset separately.
+ case LimitAndOffset(limit, offset, Sort(order, true, child))
+ if limit < conf.topKSortFallbackThreshold =>
+ Some(TakeOrderedAndProjectExec(
+ limit, order, child.output, planLater(child), offset))
+ case LimitAndOffset(limit, offset, Project(projectList, Sort(order,
true, child)))
+ if limit < conf.topKSortFallbackThreshold =>
+ Some(TakeOrderedAndProjectExec(
+ limit, order, projectList, planLater(child), offset))
+ // 'Offset a' then 'Limit b' is the same as 'Limit a + b' then 'Offset
a'.
+ case OffsetAndLimit(offset, limit, Sort(order, true, child))
+ if offset + limit < conf.topKSortFallbackThreshold =>
Review Comment:
If we use `localLimit` directly, we can avoid `offset + limit` here.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/SparkStrategies.scala:
##########
@@ -81,55 +81,56 @@ abstract class SparkStrategies extends
QueryPlanner[SparkPlan] {
*/
object SpecialLimits extends Strategy {
override def apply(plan: LogicalPlan): Seq[SparkPlan] = plan match {
- case ReturnAnswer(rootPlan) => rootPlan match {
- case Limit(IntegerLiteral(limit), Sort(order, true, child))
- if limit < conf.topKSortFallbackThreshold =>
- TakeOrderedAndProjectExec(limit, order, child.output,
planLater(child)) :: Nil
- case Limit(IntegerLiteral(limit), Project(projectList, Sort(order,
true, child)))
- if limit < conf.topKSortFallbackThreshold =>
- TakeOrderedAndProjectExec(limit, order, projectList,
planLater(child)) :: Nil
+ // Call `planTakeOrdered` first which matches a larger plan.
+ case ReturnAnswer(rootPlan) =>
planTakeOrdered(rootPlan).getOrElse(rootPlan match {
+ // We should match the combination of limit and offset first, to get
the optimal physical
+ // plan, instead of planning limit and offset separately.
+ case LimitAndOffset(limit, offset, child) =>
+ CollectLimitExec(limit = limit, child = planLater(child), offset =
offset)
+ case OffsetAndLimit(offset, limit, child) =>
+ // 'Offset a' then 'Limit b' is the same as 'Limit a + b' then
'Offset a'.
+ CollectLimitExec(limit = offset + limit, child = planLater(child),
offset = offset)
case Limit(IntegerLiteral(limit), child) =>
- CollectLimitExec(limit, planLater(child)) :: Nil
- case LimitAndOffset(IntegerLiteral(limit), IntegerLiteral(offset),
- Sort(order, true, child)) if limit + offset <
conf.topKSortFallbackThreshold =>
- TakeOrderedAndProjectExec(
- limit, order, child.output, planLater(child), offset) :: Nil
- case LimitAndOffset(IntegerLiteral(limit), IntegerLiteral(offset),
- Project(projectList, Sort(order, true, child)))
- if limit + offset < conf.topKSortFallbackThreshold =>
- TakeOrderedAndProjectExec(
- limit, order, projectList, planLater(child), offset) :: Nil
- case LimitAndOffset(IntegerLiteral(limit), IntegerLiteral(offset),
child) =>
- CollectLimitExec(limit, planLater(child), offset) :: Nil
+ CollectLimitExec(limit = limit, child = planLater(child))
case logical.Offset(IntegerLiteral(offset), child) =>
- CollectLimitExec(child = planLater(child), offset = offset) :: Nil
+ CollectLimitExec(child = planLater(child), offset = offset)
case Tail(IntegerLiteral(limit), child) =>
- CollectTailExec(limit, planLater(child)) :: Nil
- case other => planLater(other) :: Nil
- }
+ CollectTailExec(limit, planLater(child))
+ case other => planLater(other)
+ }) :: Nil
+
+ case other => planTakeOrdered(other).toSeq
+ }
+
+ private def planTakeOrdered(plan: LogicalPlan): Option[SparkPlan] = plan
match {
+ // We should match the combination of limit and offset first, to get the
optimal physical
+ // plan, instead of planning limit and offset separately.
+ case LimitAndOffset(limit, offset, Sort(order, true, child))
+ if limit < conf.topKSortFallbackThreshold =>
+ Some(TakeOrderedAndProjectExec(
+ limit, order, child.output, planLater(child), offset))
+ case LimitAndOffset(limit, offset, Project(projectList, Sort(order,
true, child)))
+ if limit < conf.topKSortFallbackThreshold =>
+ Some(TakeOrderedAndProjectExec(
+ limit, order, projectList, planLater(child), offset))
+ // 'Offset a' then 'Limit b' is the same as 'Limit a + b' then 'Offset
a'.
+ case OffsetAndLimit(offset, limit, Sort(order, true, child))
+ if offset + limit < conf.topKSortFallbackThreshold =>
+ Some(TakeOrderedAndProjectExec(
+ offset + limit, order, child.output, planLater(child), offset))
Review Comment:
ditto
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]