You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@gluten.apache.org by "beliefer (via GitHub)" <gi...@apache.org> on 2024/10/09 12:45:34 UTC
[PR] [GLUTEN-7450][CORE] Improve CollectRewriteRule for Velox [incubator-gluten]
beliefer opened a new pull request, #7451:
URL: https://github.com/apache/incubator-gluten/pull/7451
## What changes were proposed in this pull request?
This PR proposes to improve `CollectRewriteRule` for Velox.
## How was this patch tested?
integration tests
--
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: commits-unsubscribe@gluten.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@gluten.apache.org
For additional commands, e-mail: commits-help@gluten.apache.org
Re: [PR] [GLUTEN-7450][VL] Improve CollectRewriteRule for Velox [incubator-gluten]
Posted by "beliefer (via GitHub)" <gi...@apache.org>.
beliefer commented on code in PR #7451:
URL: https://github.com/apache/incubator-gluten/pull/7451#discussion_r1802958539
##########
backends-velox/src/main/scala/org/apache/gluten/extension/CollectRewriteRule.scala:
##########
@@ -37,70 +37,58 @@ import scala.reflect.{classTag, ClassTag}
case class CollectRewriteRule(spark: SparkSession) extends Rule[LogicalPlan] {
import CollectRewriteRule._
override def apply(plan: LogicalPlan): LogicalPlan = LogicalPlanSelector.maybe(spark, plan) {
- val out = plan.transformUp {
- case node =>
- val out = replaceCollectSet(replaceCollectList(node))
- out
- }
- if (out.fastEquals(plan)) {
+ if (!has[VeloxCollectSet] && !has[VeloxCollectList]) {
return plan
}
- out
- }
- private def replaceCollectList(node: LogicalPlan): LogicalPlan = {
- node.transformExpressions {
- case func @ AggregateExpression(l: CollectList, _, _, _, _) if has[VeloxCollectList] =>
- func.copy(VeloxCollectList(l.child))
+ val newPlan = plan.transformUp {
+ case node =>
+ replaceAggCollect(node)
}
+ if (newPlan.fastEquals(plan)) {
+ return plan
+ }
+ newPlan
}
- private def replaceCollectSet(node: LogicalPlan): LogicalPlan = {
- // 1. Replace null result from VeloxCollectSet with empty array to align with
- // vanilla Spark.
- // 2. Filter out null inputs from VeloxCollectSet to align with vanilla Spark.
- //
- // Since https://github.com/apache/incubator-gluten/pull/4805
+ private def replaceAggCollect(node: LogicalPlan): LogicalPlan = {
node match {
case agg: Aggregate =>
- agg.transformExpressions {
- case ToVeloxCollectSet(newAggFunc) =>
- val out = ensureNonNull(newAggFunc)
- out
+ agg.transformExpressionsWithPruning(_.containsPattern(AGGREGATE_EXPRESSION)) {
+ case ToVeloxCollect(newAggExpr) =>
+ newAggExpr
}
case w: Window =>
- w.transformExpressions {
- case func @ WindowExpression(ToVeloxCollectSet(newAggFunc), _) =>
- val out = ensureNonNull(func.copy(newAggFunc))
- out
+ w.transformExpressionsWithPruning(
+ _.containsAllPatterns(AGGREGATE_EXPRESSION, WINDOW_EXPRESSION)) {
+ case windowExpr @ WindowExpression(ToVeloxCollect(newAggExpr), _) =>
+ windowExpr.copy(newAggExpr)
}
case other => other
}
}
}
object CollectRewriteRule {
- private def ensureNonNull(expr: Expression): Expression = {
- val out =
- Coalesce(List(expr, Literal.create(Seq.empty, expr.dataType)))
- assert(!out.nullable)
- assert(!out.dataType.asInstanceOf[ArrayType].containsNull)
- out
- }
Review Comment:
The definition of defaultResult in AggregateFunction show below.
```
/**
* Result of the aggregate function when the input is empty.
*/
def defaultResult: Option[Literal] = None
```
The below code is the key logic path.
```
// Some aggregate functions (COUNT) have the special property that they can return a
// non-null result without any input. We need to make sure we return a result in this case.
val resultWithDefault = af.defaultResult match {
case Some(lit) => Coalesce(Seq(result, lit))
case None => result
}
```
--
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: commits-unsubscribe@gluten.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@gluten.apache.org
For additional commands, e-mail: commits-help@gluten.apache.org
Re: [PR] [GLUTEN-7450][CORE] Improve CollectRewriteRule for Velox [incubator-gluten]
Posted by "beliefer (via GitHub)" <gi...@apache.org>.
beliefer commented on PR #7451:
URL: https://github.com/apache/incubator-gluten/pull/7451#issuecomment-2409719803
ping @zhztheplayer @zhouyuan @Yohahaha
--
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: commits-unsubscribe@gluten.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@gluten.apache.org
For additional commands, e-mail: commits-help@gluten.apache.org
Re: [PR] [GLUTEN-7450][VL] Improve CollectRewriteRule for Velox [incubator-gluten]
Posted by "beliefer (via GitHub)" <gi...@apache.org>.
beliefer commented on code in PR #7451:
URL: https://github.com/apache/incubator-gluten/pull/7451#discussion_r1800443104
##########
backends-velox/src/main/scala/org/apache/gluten/extension/CollectRewriteRule.scala:
##########
@@ -37,70 +37,58 @@ import scala.reflect.{classTag, ClassTag}
case class CollectRewriteRule(spark: SparkSession) extends Rule[LogicalPlan] {
import CollectRewriteRule._
override def apply(plan: LogicalPlan): LogicalPlan = LogicalPlanSelector.maybe(spark, plan) {
- val out = plan.transformUp {
- case node =>
- val out = replaceCollectSet(replaceCollectList(node))
- out
- }
- if (out.fastEquals(plan)) {
+ if (!has[VeloxCollectSet] && !has[VeloxCollectList]) {
return plan
}
- out
- }
- private def replaceCollectList(node: LogicalPlan): LogicalPlan = {
- node.transformExpressions {
- case func @ AggregateExpression(l: CollectList, _, _, _, _) if has[VeloxCollectList] =>
- func.copy(VeloxCollectList(l.child))
+ val newPlan = plan.transformUp {
+ case node =>
+ replaceAggCollect(node)
}
+ if (newPlan.fastEquals(plan)) {
+ return plan
+ }
+ newPlan
}
- private def replaceCollectSet(node: LogicalPlan): LogicalPlan = {
- // 1. Replace null result from VeloxCollectSet with empty array to align with
- // vanilla Spark.
- // 2. Filter out null inputs from VeloxCollectSet to align with vanilla Spark.
- //
- // Since https://github.com/apache/incubator-gluten/pull/4805
+ private def replaceAggCollect(node: LogicalPlan): LogicalPlan = {
node match {
case agg: Aggregate =>
- agg.transformExpressions {
- case ToVeloxCollectSet(newAggFunc) =>
- val out = ensureNonNull(newAggFunc)
- out
+ agg.transformExpressionsWithPruning(_.containsPattern(AGGREGATE_EXPRESSION)) {
+ case ToVeloxCollect(newAggExpr) =>
+ newAggExpr
}
case w: Window =>
- w.transformExpressions {
- case func @ WindowExpression(ToVeloxCollectSet(newAggFunc), _) =>
- val out = ensureNonNull(func.copy(newAggFunc))
- out
+ w.transformExpressionsWithPruning(
+ _.containsAllPatterns(AGGREGATE_EXPRESSION, WINDOW_EXPRESSION)) {
+ case windowExpr @ WindowExpression(ToVeloxCollect(newAggExpr), _) =>
+ windowExpr.copy(newAggExpr)
}
case other => other
}
}
}
object CollectRewriteRule {
- private def ensureNonNull(expr: Expression): Expression = {
- val out =
- Coalesce(List(expr, Literal.create(Seq.empty, expr.dataType)))
- assert(!out.nullable)
- assert(!out.dataType.asInstanceOf[ArrayType].containsNull)
- out
- }
Review Comment:
Because `VeloxCollect` overrides the defaultResult with the default value. The Spark optimizer will ensure that it is not null.
--
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: commits-unsubscribe@gluten.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@gluten.apache.org
For additional commands, e-mail: commits-help@gluten.apache.org
Re: [PR] [GLUTEN-7450][VL] Improve CollectRewriteRule for Velox [incubator-gluten]
Posted by "zhztheplayer (via GitHub)" <gi...@apache.org>.
zhztheplayer commented on code in PR #7451:
URL: https://github.com/apache/incubator-gluten/pull/7451#discussion_r1800295168
##########
backends-velox/src/main/scala/org/apache/gluten/extension/CollectRewriteRule.scala:
##########
@@ -37,70 +37,58 @@ import scala.reflect.{classTag, ClassTag}
case class CollectRewriteRule(spark: SparkSession) extends Rule[LogicalPlan] {
import CollectRewriteRule._
override def apply(plan: LogicalPlan): LogicalPlan = LogicalPlanSelector.maybe(spark, plan) {
- val out = plan.transformUp {
- case node =>
- val out = replaceCollectSet(replaceCollectList(node))
- out
- }
- if (out.fastEquals(plan)) {
+ if (!has[VeloxCollectSet] && !has[VeloxCollectList]) {
return plan
}
- out
- }
- private def replaceCollectList(node: LogicalPlan): LogicalPlan = {
- node.transformExpressions {
- case func @ AggregateExpression(l: CollectList, _, _, _, _) if has[VeloxCollectList] =>
- func.copy(VeloxCollectList(l.child))
+ val newPlan = plan.transformUp {
+ case node =>
+ replaceAggCollect(node)
}
+ if (newPlan.fastEquals(plan)) {
+ return plan
+ }
+ newPlan
}
- private def replaceCollectSet(node: LogicalPlan): LogicalPlan = {
- // 1. Replace null result from VeloxCollectSet with empty array to align with
- // vanilla Spark.
- // 2. Filter out null inputs from VeloxCollectSet to align with vanilla Spark.
- //
- // Since https://github.com/apache/incubator-gluten/pull/4805
+ private def replaceAggCollect(node: LogicalPlan): LogicalPlan = {
node match {
case agg: Aggregate =>
- agg.transformExpressions {
- case ToVeloxCollectSet(newAggFunc) =>
- val out = ensureNonNull(newAggFunc)
- out
+ agg.transformExpressionsWithPruning(_.containsPattern(AGGREGATE_EXPRESSION)) {
+ case ToVeloxCollect(newAggExpr) =>
+ newAggExpr
}
case w: Window =>
- w.transformExpressions {
- case func @ WindowExpression(ToVeloxCollectSet(newAggFunc), _) =>
- val out = ensureNonNull(func.copy(newAggFunc))
- out
+ w.transformExpressionsWithPruning(
+ _.containsAllPatterns(AGGREGATE_EXPRESSION, WINDOW_EXPRESSION)) {
+ case windowExpr @ WindowExpression(ToVeloxCollect(newAggExpr), _) =>
+ windowExpr.copy(newAggExpr)
}
case other => other
}
}
}
object CollectRewriteRule {
- private def ensureNonNull(expr: Expression): Expression = {
- val out =
- Coalesce(List(expr, Literal.create(Seq.empty, expr.dataType)))
- assert(!out.nullable)
- assert(!out.dataType.asInstanceOf[ArrayType].containsNull)
- out
- }
Review Comment:
Curious what's the reason of removing this? I remember it was for the compatibility with vanilla Spark.
--
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: commits-unsubscribe@gluten.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@gluten.apache.org
For additional commands, e-mail: commits-help@gluten.apache.org
Re: [PR] [GLUTEN-7450][VL] Improve CollectRewriteRule for Velox [incubator-gluten]
Posted by "zhztheplayer (via GitHub)" <gi...@apache.org>.
zhztheplayer merged PR #7451:
URL: https://github.com/apache/incubator-gluten/pull/7451
--
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: commits-unsubscribe@gluten.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@gluten.apache.org
For additional commands, e-mail: commits-help@gluten.apache.org
Re: [PR] [GLUTEN-7450][VL] Improve CollectRewriteRule for Velox [incubator-gluten]
Posted by "beliefer (via GitHub)" <gi...@apache.org>.
beliefer commented on PR #7451:
URL: https://github.com/apache/incubator-gluten/pull/7451#issuecomment-2421430233
@zhztheplayer Thank you!
--
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: commits-unsubscribe@gluten.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@gluten.apache.org
For additional commands, e-mail: commits-help@gluten.apache.org
Re: [PR] [GLUTEN-7450][VL] Improve CollectRewriteRule for Velox [incubator-gluten]
Posted by "beliefer (via GitHub)" <gi...@apache.org>.
beliefer commented on code in PR #7451:
URL: https://github.com/apache/incubator-gluten/pull/7451#discussion_r1800443104
##########
backends-velox/src/main/scala/org/apache/gluten/extension/CollectRewriteRule.scala:
##########
@@ -37,70 +37,58 @@ import scala.reflect.{classTag, ClassTag}
case class CollectRewriteRule(spark: SparkSession) extends Rule[LogicalPlan] {
import CollectRewriteRule._
override def apply(plan: LogicalPlan): LogicalPlan = LogicalPlanSelector.maybe(spark, plan) {
- val out = plan.transformUp {
- case node =>
- val out = replaceCollectSet(replaceCollectList(node))
- out
- }
- if (out.fastEquals(plan)) {
+ if (!has[VeloxCollectSet] && !has[VeloxCollectList]) {
return plan
}
- out
- }
- private def replaceCollectList(node: LogicalPlan): LogicalPlan = {
- node.transformExpressions {
- case func @ AggregateExpression(l: CollectList, _, _, _, _) if has[VeloxCollectList] =>
- func.copy(VeloxCollectList(l.child))
+ val newPlan = plan.transformUp {
+ case node =>
+ replaceAggCollect(node)
}
+ if (newPlan.fastEquals(plan)) {
+ return plan
+ }
+ newPlan
}
- private def replaceCollectSet(node: LogicalPlan): LogicalPlan = {
- // 1. Replace null result from VeloxCollectSet with empty array to align with
- // vanilla Spark.
- // 2. Filter out null inputs from VeloxCollectSet to align with vanilla Spark.
- //
- // Since https://github.com/apache/incubator-gluten/pull/4805
+ private def replaceAggCollect(node: LogicalPlan): LogicalPlan = {
node match {
case agg: Aggregate =>
- agg.transformExpressions {
- case ToVeloxCollectSet(newAggFunc) =>
- val out = ensureNonNull(newAggFunc)
- out
+ agg.transformExpressionsWithPruning(_.containsPattern(AGGREGATE_EXPRESSION)) {
+ case ToVeloxCollect(newAggExpr) =>
+ newAggExpr
}
case w: Window =>
- w.transformExpressions {
- case func @ WindowExpression(ToVeloxCollectSet(newAggFunc), _) =>
- val out = ensureNonNull(func.copy(newAggFunc))
- out
+ w.transformExpressionsWithPruning(
+ _.containsAllPatterns(AGGREGATE_EXPRESSION, WINDOW_EXPRESSION)) {
+ case windowExpr @ WindowExpression(ToVeloxCollect(newAggExpr), _) =>
+ windowExpr.copy(newAggExpr)
}
case other => other
}
}
}
object CollectRewriteRule {
- private def ensureNonNull(expr: Expression): Expression = {
- val out =
- Coalesce(List(expr, Literal.create(Seq.empty, expr.dataType)))
- assert(!out.nullable)
- assert(!out.dataType.asInstanceOf[ArrayType].containsNull)
- out
- }
Review Comment:
Because `VeloxCollect` overrides the `defaultResult` with the default value. The Spark optimizer will ensure that it is not null.
--
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: commits-unsubscribe@gluten.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@gluten.apache.org
For additional commands, e-mail: commits-help@gluten.apache.org
Re: [PR] [GLUTEN-7450][CORE] Improve CollectRewriteRule for Velox [incubator-gluten]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #7451:
URL: https://github.com/apache/incubator-gluten/pull/7451#issuecomment-2402224230
https://github.com/apache/incubator-gluten/issues/7450
--
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: commits-unsubscribe@gluten.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@gluten.apache.org
For additional commands, e-mail: commits-help@gluten.apache.org
Re: [PR] [GLUTEN-7450][VL] Improve CollectRewriteRule for Velox [incubator-gluten]
Posted by "zhztheplayer (via GitHub)" <gi...@apache.org>.
zhztheplayer commented on code in PR #7451:
URL: https://github.com/apache/incubator-gluten/pull/7451#discussion_r1802267777
##########
backends-velox/src/main/scala/org/apache/gluten/extension/CollectRewriteRule.scala:
##########
@@ -37,70 +37,58 @@ import scala.reflect.{classTag, ClassTag}
case class CollectRewriteRule(spark: SparkSession) extends Rule[LogicalPlan] {
import CollectRewriteRule._
override def apply(plan: LogicalPlan): LogicalPlan = LogicalPlanSelector.maybe(spark, plan) {
- val out = plan.transformUp {
- case node =>
- val out = replaceCollectSet(replaceCollectList(node))
- out
- }
- if (out.fastEquals(plan)) {
+ if (!has[VeloxCollectSet] && !has[VeloxCollectList]) {
return plan
}
- out
- }
- private def replaceCollectList(node: LogicalPlan): LogicalPlan = {
- node.transformExpressions {
- case func @ AggregateExpression(l: CollectList, _, _, _, _) if has[VeloxCollectList] =>
- func.copy(VeloxCollectList(l.child))
+ val newPlan = plan.transformUp {
+ case node =>
+ replaceAggCollect(node)
}
+ if (newPlan.fastEquals(plan)) {
+ return plan
+ }
+ newPlan
}
- private def replaceCollectSet(node: LogicalPlan): LogicalPlan = {
- // 1. Replace null result from VeloxCollectSet with empty array to align with
- // vanilla Spark.
- // 2. Filter out null inputs from VeloxCollectSet to align with vanilla Spark.
- //
- // Since https://github.com/apache/incubator-gluten/pull/4805
+ private def replaceAggCollect(node: LogicalPlan): LogicalPlan = {
node match {
case agg: Aggregate =>
- agg.transformExpressions {
- case ToVeloxCollectSet(newAggFunc) =>
- val out = ensureNonNull(newAggFunc)
- out
+ agg.transformExpressionsWithPruning(_.containsPattern(AGGREGATE_EXPRESSION)) {
+ case ToVeloxCollect(newAggExpr) =>
+ newAggExpr
}
case w: Window =>
- w.transformExpressions {
- case func @ WindowExpression(ToVeloxCollectSet(newAggFunc), _) =>
- val out = ensureNonNull(func.copy(newAggFunc))
- out
+ w.transformExpressionsWithPruning(
+ _.containsAllPatterns(AGGREGATE_EXPRESSION, WINDOW_EXPRESSION)) {
+ case windowExpr @ WindowExpression(ToVeloxCollect(newAggExpr), _) =>
+ windowExpr.copy(newAggExpr)
}
case other => other
}
}
}
object CollectRewriteRule {
- private def ensureNonNull(expr: Expression): Expression = {
- val out =
- Coalesce(List(expr, Literal.create(Seq.empty, expr.dataType)))
- assert(!out.nullable)
- assert(!out.dataType.asInstanceOf[ArrayType].containsNull)
- out
- }
Review Comment:
> Because VeloxCollect overrides the `defaultResult` with the default value. The Spark optimizer will ensure that it is not null.
Didn't see this method is used much by Spark in regular aggregation planning routine: https://github.com/search?q=repo%3Aapache%2Fspark%20defaultResult&type=code. Do you see the `Coalesce` in plan after removing this logic anyway?
BTW as Rui's Velox PR was merged recently https://github.com/facebookincubator/velox/pull/10737, @rui-mo did you make any corresponding changes in Gluten at that time?
--
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: commits-unsubscribe@gluten.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@gluten.apache.org
For additional commands, e-mail: commits-help@gluten.apache.org