-
Notifications
You must be signed in to change notification settings - Fork 439
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
908b2f6
commit 98ab12b
Showing
7 changed files
with
124 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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
76 changes: 76 additions & 0 deletions
76
gluten-core/src/main/scala/io/glutenproject/extension/RewriteCollectSet.scala
This file contains 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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* 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 io.glutenproject.extension | ||
|
||
import io.glutenproject.backendsapi.BackendsApiManager | ||
import io.glutenproject.utils.PullOutProjectHelper | ||
|
||
import org.apache.spark.sql.catalyst.expressions.{And, IsNotNull} | ||
import org.apache.spark.sql.catalyst.expressions.aggregate.{AggregateExpression, CollectSet, Complete, Partial} | ||
import org.apache.spark.sql.catalyst.rules.Rule | ||
import org.apache.spark.sql.execution.SparkPlan | ||
import org.apache.spark.sql.execution.aggregate.BaseAggregateExec | ||
|
||
/** | ||
* This rule Add `IsNotNull` to skip null value before going to native collect_set | ||
* | ||
* TODO: remove this rule once Velox collect_set skip null value | ||
*/ | ||
object RewriteCollectSet extends Rule[SparkPlan] with PullOutProjectHelper { | ||
private lazy val shouldRewriteCollect = | ||
BackendsApiManager.getSettings.shouldRewriteCollectSet() | ||
|
||
private def shouldRewrite(ae: AggregateExpression): Boolean = { | ||
ae.aggregateFunction match { | ||
case _: CollectSet => | ||
ae.mode match { | ||
case Partial | Complete => true | ||
case _ => false | ||
} | ||
case _ => false | ||
} | ||
} | ||
|
||
private def rewriteCollectFilter(aggExprs: Seq[AggregateExpression]): Seq[AggregateExpression] = { | ||
aggExprs | ||
.map { | ||
aggExpr => | ||
if (shouldRewrite(aggExpr)) { | ||
val newFilter = | ||
(aggExpr.filter ++ Seq(IsNotNull(aggExpr.aggregateFunction.children.head))) | ||
.reduce(And) | ||
aggExpr.copy(filter = Option(newFilter)) | ||
} else { | ||
aggExpr | ||
} | ||
} | ||
} | ||
|
||
override def apply(plan: SparkPlan): SparkPlan = { | ||
if (!shouldRewriteCollect) { | ||
return plan | ||
} | ||
|
||
plan match { | ||
case agg: BaseAggregateExec if agg.aggregateExpressions.exists(shouldRewrite) => | ||
val newAggExprs = rewriteCollectFilter(agg.aggregateExpressions) | ||
copyBaseAggregateExec(agg)(newAggregateExpressions = newAggExprs) | ||
|
||
case _ => plan | ||
} | ||
} | ||
} |