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 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 @@ -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
Loading