-
Notifications
You must be signed in to change notification settings - Fork 466
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
[GLUTEN-8966][VL] Propagate HashAggregate's ignoreNullKeys when possible #8967
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* 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 | ||
|
||
import org.apache.gluten.config.VeloxConfig | ||
import org.apache.gluten.execution._ | ||
|
||
import org.apache.spark.sql.SparkSession | ||
import org.apache.spark.sql.catalyst.expressions.Expression | ||
import org.apache.spark.sql.catalyst.plans.Inner | ||
import org.apache.spark.sql.catalyst.rules.Rule | ||
import org.apache.spark.sql.execution.SparkPlan | ||
import org.apache.spark.sql.execution.adaptive.ShuffleQueryStageExec | ||
import org.apache.spark.sql.execution.exchange.ShuffleExchangeLike | ||
import org.apache.spark.sql.execution.joins.BaseJoinExec | ||
|
||
/** | ||
* To identify aggregates that the groupby key is used as inner join keys. In this case, we can set | ||
* ignoreNullKeys to true when convert to velox's AggregateNode. | ||
*/ | ||
case class HashAggregateIgnoreNullKeysRule(session: SparkSession) extends Rule[SparkPlan] { | ||
override def apply(plan: SparkPlan): SparkPlan = { | ||
if ( | ||
!session.conf.get(VeloxConfig.VELOX_PROPAGATE_IGNORE_NULL_KEYS_ENABLED.key, "true").toBoolean | ||
) { | ||
return plan | ||
} | ||
plan.transformUp { | ||
case join: BaseJoinExec if join.joinType == Inner => | ||
val newLeftChild = setIgnoreKeysIfAggregateOnJoinKeys(join.left, join.leftKeys) | ||
val newRightChild = setIgnoreKeysIfAggregateOnJoinKeys(join.right, join.rightKeys) | ||
if (!newLeftChild.fastEquals(join.left) || !newRightChild.fastEquals(join.right)) { | ||
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. nit: change it to |
||
join.withNewChildren(Seq(newLeftChild, newRightChild)) | ||
} else { | ||
join | ||
} | ||
case p => p | ||
} | ||
} | ||
|
||
private def setIgnoreKeysIfAggregateOnJoinKeys( | ||
plan: SparkPlan, | ||
joinKeys: Seq[Expression]): SparkPlan = { | ||
def transformDown: SparkPlan => 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. Why we need a function here? can we directly use |
||
case agg: FlushableHashAggregateExecTransformer => | ||
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. nit: use |
||
val newChild = transformDown(agg.child) | ||
val canIgnoreNullKeysRule = semanticEquals(agg.groupingExpressions, joinKeys) | ||
agg.copy(ignoreNullKeys = canIgnoreNullKeysRule, child = newChild) | ||
case agg: RegularHashAggregateExecTransformer => | ||
val newChild = transformDown(agg.child) | ||
val canIgnoreNullKeysRule = semanticEquals(agg.groupingExpressions, joinKeys) | ||
agg.copy(ignoreNullKeys = canIgnoreNullKeysRule, child = newChild) | ||
case s: ShuffleQueryStageExec => s.copy(plan = transformDown(s.plan)) | ||
case p if !canPropagate(p) => p | ||
case other => other.withNewChildren(other.children.map(transformDown)) | ||
} | ||
val out = transformDown(plan) | ||
out | ||
} | ||
|
||
private def canPropagate(plan: SparkPlan): Boolean = plan match { | ||
case _: ProjectExecTransformer => true | ||
case _: WholeStageTransformer => true | ||
case _: VeloxResizeBatchesExec => true | ||
case _: ShuffleExchangeLike => true | ||
case _: VeloxColumnarToRowExec => true | ||
case _ => false | ||
} | ||
|
||
private def semanticEquals(aggExpression: Seq[Expression], joinKeys: Seq[Expression]): Boolean = { | ||
aggExpression.size == joinKeys.size && aggExpression.zip(joinKeys).forall { | ||
case (e1: Expression, e2: Expression) => e1.semanticEquals(e2) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ | |
*/ | ||
package org.apache.gluten.execution | ||
|
||
import org.apache.gluten.config.VeloxConfig | ||
import org.apache.gluten.config.{GlutenConfig, VeloxConfig} | ||
import org.apache.gluten.extension.columnar.validator.FallbackInjects | ||
|
||
import org.apache.spark.SparkConf | ||
|
@@ -1193,6 +1193,28 @@ class VeloxAggregateFunctionsDefaultSuite extends VeloxAggregateFunctionsSuite { | |
} | ||
} | ||
} | ||
|
||
test("aggregate on join keys can set ignoreNullKeys") { | ||
val s = | ||
""" | ||
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. The test data does not contains nulls by 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. The test case here is used to test the plan is correctly mark |
||
|select count(1) from | ||
| (select l_orderkey, max(l_partkey) from lineitem group by l_orderkey) a | ||
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. What if agg offload but join fallback? 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. it doesn't matter. if the pattern is
then we can safely ignore the null values in k1 when doing aggregation, no matter whether |
||
|inner join | ||
| (select l_orderkey from lineitem) b | ||
|on a.l_orderkey = b.l_orderkey | ||
|""".stripMargin | ||
withSQLConf(GlutenConfig.COLUMNAR_FORCE_SHUFFLED_HASH_JOIN_ENABLED.key -> "true") { | ||
runQueryAndCompare(s) { | ||
df => | ||
val executedPlan = getExecutedPlan(df) | ||
assert(executedPlan.exists { | ||
case a: RegularHashAggregateExecTransformer if a.ignoreNullKeys => true | ||
case a: FlushableHashAggregateExecTransformer if a.ignoreNullKeys => true | ||
case _ => false | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
|
||
class VeloxAggregateFunctionsFlushSuite extends VeloxAggregateFunctionsSuite { | ||
|
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.
nit: use
VeloxConfig.get.enablePropagateIgnoreNullKeys