Skip to content
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-7778][CH] Make aggregation output schema same as CH native #7811

Merged
merged 5 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -158,16 +158,21 @@ class CHSparkPlanExecApi extends SparkPlanExecApi with Logging {
aggregateAttributes: Seq[Attribute],
initialInputBufferOffset: Int,
resultExpressions: Seq[NamedExpression],
child: SparkPlan): HashAggregateExecBaseTransformer =
child: SparkPlan): HashAggregateExecBaseTransformer = {
val replacedResultExpressions = CHHashAggregateExecTransformer.getCHAggregateResultExpressions(
groupingExpressions,
aggregateExpressions,
resultExpressions)
CHHashAggregateExecTransformer(
requiredChildDistributionExpressions,
groupingExpressions.distinct,
aggregateExpressions,
aggregateAttributes,
initialInputBufferOffset,
resultExpressions.distinct,
replacedResultExpressions.distinct,
child
)
}

/** Generate HashAggregateExecPullOutHelper */
override def genHashAggregateExecPullOutHelper(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,13 @@ class CHTransformerApi extends TransformerApi with Logging {
// output name will be different from grouping expressions,
// so using output attribute instead of grouping expression
val groupingExpressions = hash.output.splitAt(hash.groupingExpressions.size)._1
val aggResultAttributes = CHHashAggregateExecTransformer.getAggregateResultAttributes(
groupingExpressions,
hash.aggregateExpressions
)
val aggResultAttributes = CHHashAggregateExecTransformer
.getCHAggregateResultExpressions(
groupingExpressions,
hash.aggregateExpressions,
hash.resultExpressions
)
.map(_.toAttribute)
if (aggResultAttributes.size == hash.output.size) {
aggResultAttributes
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,45 @@ import scala.collection.JavaConverters._
import scala.collection.mutable.ListBuffer

object CHHashAggregateExecTransformer {
// The result attributes of aggregate expressions from vanilla may be different from CH native.
// For example, the result attributes of `avg(x)` are `sum(x)` and `count(x)`. This could bring
// some unexpected issues. So we need to make the result attributes consistent with CH native.
def getCHAggregateResultExpressions(
groupingExpressions: Seq[NamedExpression],
aggregateExpressions: Seq[AggregateExpression],
resultExpressions: Seq[NamedExpression]): Seq[NamedExpression] = {
var adjustedResultExpressions = resultExpressions.slice(0, groupingExpressions.length)
var resultExpressionIndex = groupingExpressions.length
adjustedResultExpressions ++ aggregateExpressions.flatMap {
aggExpr =>
aggExpr.mode match {
case Partial | PartialMerge =>
// For partial aggregate, the size of the result expressions of an aggregate expression
// is the same as aggBufferAttributes' length
val aggBufferAttributesCount = aggExpr.aggregateFunction.aggBufferAttributes.length
aggExpr.aggregateFunction match {
case avg: Average =>
val res = Seq(aggExpr.resultAttribute)
resultExpressionIndex += aggBufferAttributesCount
res
case sum: Sum if (sum.dataType.isInstanceOf[DecimalType]) =>
val res = Seq(resultExpressions(resultExpressionIndex))
resultExpressionIndex += aggBufferAttributesCount
res
case _ =>
val res = resultExpressions
.slice(resultExpressionIndex, resultExpressionIndex + aggBufferAttributesCount)
resultExpressionIndex += aggBufferAttributesCount
res
}
case _ =>
val res = Seq(resultExpressions(resultExpressionIndex))
resultExpressionIndex += 1
res
}
}
}

def getAggregateResultAttributes(
groupingExpressions: Seq[NamedExpression],
aggregateExpressions: Seq[AggregateExpression]): Seq[Attribute] = {
Expand Down Expand Up @@ -245,6 +284,7 @@ case class CHHashAggregateExecTransformer(
"PartialMerge's child not being HashAggregateExecBaseTransformer" +
" is unsupported yet")
}
val hashAggregateChild = child.asInstanceOf[BaseAggregateExec]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this line, it's unused ?

val aggTypesExpr = ExpressionConverter
.replaceWithExpressionTransformer(
aggExpr.resultAttribute,
Expand Down
1 change: 0 additions & 1 deletion cpp-ch/local-engine/Parser/SerializedPlanParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ void adjustOutput(const DB::QueryPlanPtr & query_plan, const substrait::PlanRel
"Missmatch result columns size. plan column size {}, subtrait plan name size {}.",
cols.getNames().size(),
root_rel.root().names_size());
}
for (int i = 0; i < static_cast<int>(cols.getNames().size()); i++)
aliases.emplace_back(NameWithAlias(cols.getNames()[i], root_rel.root().names(i)));
actions_dag.project(aliases);
Expand Down
Loading