-
Notifications
You must be signed in to change notification settings - Fork 411
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
[CORE] Remove local sort for TopNRowNumber #6381
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* 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.gluten.extension.columnar | ||
|
||
import org.apache.gluten.execution.{HashAggregateExecBaseTransformer, ProjectExecTransformer, ShuffledHashJoinExecTransformerBase, SortExecTransformer, WindowExecTransformer, WindowGroupLimitExecTransformer} | ||
|
||
import org.apache.spark.sql.catalyst.expressions.SortOrder | ||
import org.apache.spark.sql.catalyst.rules.Rule | ||
import org.apache.spark.sql.execution.{ProjectExec, SortExec, SparkPlan, UnaryExecNode} | ||
|
||
/** | ||
* This rule is used to eliminate unnecessary local sort. | ||
* | ||
* This could happen if: | ||
* - Convert sort merge join to shuffled hash join | ||
* - Offload SortAggregate to native hash aggregate | ||
* - Offload WindowGroupLimit to native TopNRowNumber | ||
* - The columnar window type is `sort` | ||
*/ | ||
object EliminateLocalSort extends Rule[SparkPlan] { | ||
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. @ulysses-you Do we have chance to eliminate the local sort for VeloxColumnarWriteFilesExec here? 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. There are some limitation to remove sort before write files, e.g., we can not remove the sort which is created by user (to improve compression ratio). We can only remove the sort which is added from Spark, but it's hard to find that sort in this new rule only using require child ordering framework, we need to check if all sort fields are partition columns. |
||
private def canEliminateLocalSort(p: SparkPlan): Boolean = p match { | ||
case _: HashAggregateExecBaseTransformer => true | ||
case _: ShuffledHashJoinExecTransformerBase => true | ||
case _: WindowGroupLimitExecTransformer => true | ||
case _: WindowExecTransformer => true | ||
case _ => false | ||
} | ||
|
||
private def canThrough(p: SparkPlan): Boolean = p match { | ||
case _: ProjectExec => true | ||
case _: ProjectExecTransformer => true | ||
case _ => false | ||
} | ||
|
||
private def orderingSatisfies(gChild: SparkPlan, requiredOrdering: Seq[SortOrder]): Boolean = { | ||
SortOrder.orderingSatisfies(gChild.outputOrdering, requiredOrdering) | ||
} | ||
|
||
override def apply(plan: SparkPlan): SparkPlan = { | ||
plan.transformDown { | ||
case p if canEliminateLocalSort(p) => | ||
val requiredChildOrdering = p.requiredChildOrdering | ||
assert(requiredChildOrdering.size == p.children.size) | ||
val newChildren = p.children.zipWithIndex.map { | ||
case (SortWithChild(gChild), i) if orderingSatisfies(gChild, requiredChildOrdering(i)) => | ||
gChild | ||
case (p: UnaryExecNode, i) if canThrough(p) => | ||
// There may be more than one project between target operator and sort, | ||
// e.g., both hash aggregate and sort pull out project | ||
p.child match { | ||
case SortWithChild(gChild) if orderingSatisfies(gChild, requiredChildOrdering(i)) => | ||
p.withNewChildren(gChild :: Nil) | ||
case _ => p | ||
} | ||
case p => p._1 | ||
} | ||
p.withNewChildren(newChildren) | ||
} | ||
} | ||
} | ||
|
||
object SortWithChild { | ||
def unapply(plan: SparkPlan): Option[SparkPlan] = { | ||
plan match { | ||
case p1 @ ProjectExec(_, SortExecTransformer(_, false, p2: ProjectExec, _)) | ||
if p1.outputSet == p2.child.outputSet => | ||
Some(p2.child) | ||
case p1 @ ProjectExecTransformer( | ||
_, | ||
SortExecTransformer(_, false, p2: ProjectExecTransformer, _)) | ||
if p1.outputSet == p2.child.outputSet => | ||
Some(p2.child) | ||
case SortExec(_, false, child, _) => | ||
Some(child) | ||
case SortExecTransformer(_, false, child, _) => | ||
Some(child) | ||
case _ => None | ||
} | ||
} | ||
} |
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.
super.requiredChildOrdering Implemented differently in different spark versions. In addition, according to the original logic, ck backend should not need child order.
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.
for 1, it's a good point, it seems the
WindowExecBase
has implemented these methods since 3.3for 2, the ck backend requires child ordering and before we did not remove local sort before window
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.
I think we should retain requiredChildOrderingForWindow instead of using velox configuration to control ck backend
although it has no effect by default value.