Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,16 @@ class CommonSubexpressionEliminateRule(spark: SparkSession) extends Rule[Logical

private def replaceAggCommonExprWithAttribute(
expr: Expression,
commonExprMap: mutable.HashMap[ExpressionEquals, AliasAndAttribute]): Expression = {
commonExprMap: mutable.HashMap[ExpressionEquals, AliasAndAttribute],
inAgg: Boolean = false): Expression = {
val exprEquals = commonExprMap.get(ExpressionEquals(expr))
if (expr.isInstanceOf[AggregateExpression]) {
if (exprEquals.isDefined) {
expr match {
case _ if exprEquals.isDefined && inAgg =>
exprEquals.get.attribute
} else {
expr
}
} else {
expr.mapChildren(replaceAggCommonExprWithAttribute(_, commonExprMap))
case _: AggregateExpression =>
expr.mapChildren(replaceAggCommonExprWithAttribute(_, commonExprMap, true))
case _ =>
expr.mapChildren(replaceAggCommonExprWithAttribute(_, commonExprMap, inAgg))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import org.apache.spark.sql.{DataFrame, GlutenTestUtils, Row}
import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.catalyst.expressions.aggregate._
import org.apache.spark.sql.catalyst.optimizer.{ConstantFolding, NullPropagation}
import org.apache.spark.sql.catalyst.plans.logical.{Filter, LogicalPlan, Project}
import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, Filter, LogicalPlan, Project}
import org.apache.spark.sql.execution._
import org.apache.spark.sql.execution.datasources.v2.clickhouse.ClickHouseConfig
import org.apache.spark.sql.internal.SQLConf
Expand Down Expand Up @@ -825,8 +825,28 @@ class GlutenFunctionValidateSuite extends GlutenClickHouseWholeStageTransformerS
|FROM (select id, cast(id as string) name from range(10))
|GROUP BY name
|""".stripMargin) {
df => checkOperatorCount[ProjectExecTransformer](3)(df)
df => checkOperatorCount[ProjectExecTransformer](4)(df)
}

runQueryAndCompare(
s"""
|select id % 2, max(hash(id)), min(hash(id)) from range(10) group by id % 2
|""".stripMargin)(
df => {
df.queryExecution.optimizedPlan.collect {
case Aggregate(_, aggregateExpressions, _) =>
val result =
aggregateExpressions
.map(a => a.asInstanceOf[Alias].child)
.filter(_.isInstanceOf[AggregateExpression])
.map(expr => expr.asInstanceOf[AggregateExpression].aggregateFunction)
.filter(aggFunc => aggFunc.children.head.isInstanceOf[AttributeReference])
.map(aggFunc => aggFunc.children.head.asInstanceOf[AttributeReference].name)
.distinct
assertResult(1)(result.size)
}
checkOperatorCount[ProjectExecTransformer](1)(df)
})
}
}

Expand Down Expand Up @@ -1339,7 +1359,7 @@ class GlutenFunctionValidateSuite extends GlutenClickHouseWholeStageTransformerS

test("Test rewrite aggregate if to aggregate with filter") {
val sql = "select sum(if(id % 2=0, id, null)), count(if(id % 2 = 0, 1, null)), " +
"avg(if(id % 2 = 0, id, null)), sum(if(id % 3 = 0, id, 0)) from range(10)"
"avg(if(id % 4 = 0, id, null)), sum(if(id % 3 = 0, id, 0)) from range(10)"

def checkAggregateWithFilter(df: DataFrame): Unit = {
val aggregates = collectWithSubqueries(df.queryExecution.executedPlan) {
Expand Down