Github user maropu commented on a diff in the pull request:

    https://github.com/apache/spark/pull/10896#discussion_r75819461
  
    --- Diff: 
sql/core/src/main/scala/org/apache/spark/sql/execution/aggregate/AggUtils.scala 
---
    @@ -27,26 +27,87 @@ import 
org.apache.spark.sql.execution.streaming.{StateStoreRestoreExec, StateSto
      */
     object AggUtils {
     
    -  def planAggregateWithoutPartial(
    +  private[execution] def isAggregate(operator: SparkPlan): Boolean = {
    +    operator.isInstanceOf[HashAggregateExec] || 
operator.isInstanceOf[SortAggregateExec]
    +  }
    +
    +  private[execution] def supportPartialAggregate(operator: SparkPlan): 
Boolean = {
    +    assert(isAggregate(operator))
    +    def supportPartial(exprs: Seq[AggregateExpression]) =
    +      exprs.map(_.aggregateFunction).forall(_.supportsPartial)
    +    operator match {
    +      case agg @ HashAggregateExec(_, _, aggregateExpressions, _, _, _, _) 
=>
    +        supportPartial(aggregateExpressions)
    +      case agg @ SortAggregateExec(_, _, aggregateExpressions, _, _, _, _) 
=>
    +        supportPartial(aggregateExpressions)
    +    }
    +  }
    +
    +  private def createPartialAggregateExec(
           groupingExpressions: Seq[NamedExpression],
           aggregateExpressions: Seq[AggregateExpression],
    -      resultExpressions: Seq[NamedExpression],
    -      child: SparkPlan): Seq[SparkPlan] = {
    +      child: SparkPlan): SparkPlan = {
    +    val groupingAttributes = groupingExpressions.map(_.toAttribute)
    +    val functionsWithDistinct = aggregateExpressions.filter(_.isDistinct)
    +    val partialAggregateExpressions = aggregateExpressions.map {
    +      case agg @ AggregateExpression(_, _, false, _) if 
functionsWithDistinct.length > 0 =>
    +        agg.copy(mode = PartialMerge)
    +      case agg =>
    +        agg.copy(mode = Partial)
    +    }
    +    val partialAggregateAttributes =
    +      
partialAggregateExpressions.flatMap(_.aggregateFunction.aggBufferAttributes)
    +    val partialResultExpressions =
    +      groupingAttributes ++
    +        
partialAggregateExpressions.flatMap(_.aggregateFunction.inputAggBufferAttributes)
     
    -    val completeAggregateExpressions = 
aggregateExpressions.map(_.copy(mode = Complete))
    -    val completeAggregateAttributes = 
completeAggregateExpressions.map(_.resultAttribute)
    -    SortAggregateExec(
    -      requiredChildDistributionExpressions = Some(groupingExpressions),
    +    createAggregateExec(
    +      requiredChildDistributionExpressions = None,
           groupingExpressions = groupingExpressions,
    -      aggregateExpressions = completeAggregateExpressions,
    -      aggregateAttributes = completeAggregateAttributes,
    -      initialInputBufferOffset = 0,
    -      resultExpressions = resultExpressions,
    -      child = child
    -    ) :: Nil
    +      aggregateExpressions = partialAggregateExpressions,
    +      aggregateAttributes = partialAggregateAttributes,
    +      initialInputBufferOffset = if (functionsWithDistinct.length > 0) {
    +        groupingExpressions.length + 
functionsWithDistinct.head.aggregateFunction.children.length
    +      } else {
    +        0
    +      },
    +      resultExpressions = partialResultExpressions,
    +      child = child)
    +  }
    +
    +  private def updateMergeAggregateMode(aggregateExpressions: 
Seq[AggregateExpression]) = {
    +    def updateMode(mode: AggregateMode) = mode match {
    +      case Partial => PartialMerge
    +      case Complete => Final
    +      case mode => mode
    +    }
    +    aggregateExpressions.map(e => e.copy(mode = updateMode(e.mode)))
    +  }
    +
    +  private[execution] def createPartialAggregate(operator: SparkPlan)
    --- End diff --
    
    fixed


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to