luluorta commented on a change in pull request #29882:
URL: https://github.com/apache/spark/pull/29882#discussion_r504448776



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/aggregate/Average.scala
##########
@@ -77,11 +77,15 @@ case class Average(child: Expression) extends 
DeclarativeAggregate with Implicit
   )
 
   // If all input are nulls, count will be 0 and we will get null after the 
division.
-  override lazy val evaluateExpression = child.dataType match {
-    case _: DecimalType =>
-      DecimalPrecision.decimalAndDecimal(sum / 
count.cast(DecimalType.LongDecimal)).cast(resultType)
-    case _ =>
-      sum.cast(resultType) / count.cast(resultType)
+  override lazy val evaluateExpression = {
+    val eval = child.dataType match {
+      case _: DecimalType =>
+        DecimalPrecision.decimalAndDecimal(sum / 
count.cast(DecimalType.LongDecimal))
+          .cast(resultType)
+      case _ =>
+        sum.cast(resultType) / count.cast(resultType)
+    }
+    If(EqualTo(count, Literal(0L)), Literal(null, resultType), eval)

Review comment:
       done




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



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

Reply via email to