You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2020/06/01 18:00:34 UTC
[GitHub] [spark] skambha commented on a change in pull request #27627: [WIP][SPARK-28067][SQL] Fix incorrect results for decimal aggregate sum by returning null on decimal overflow
skambha commented on a change in pull request #27627:
URL: https://github.com/apache/spark/pull/27627#discussion_r433397053
##########
File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/aggregate/Sum.scala
##########
@@ -62,38 +62,74 @@ case class Sum(child: Expression) extends DeclarativeAggregate with ImplicitCast
private lazy val sum = AttributeReference("sum", sumDataType)()
+ private lazy val isEmpty = AttributeReference("isEmpty", BooleanType, nullable = false)()
+
private lazy val zero = Literal.default(sumDataType)
- override lazy val aggBufferAttributes = sum :: Nil
+ override lazy val aggBufferAttributes = resultType match {
+ case _: DecimalType => sum :: isEmpty :: Nil
+ case _ => sum :: Nil
+ }
- override lazy val initialValues: Seq[Expression] = Seq(
- /* sum = */ Literal.create(null, sumDataType)
- )
+ override lazy val initialValues: Seq[Expression] = resultType match {
+ case _: DecimalType => Seq(Literal(null, resultType), Literal(true, BooleanType))
+ case _ => Seq(Literal(null, resultType))
+ }
override lazy val updateExpressions: Seq[Expression] = {
if (child.nullable) {
- Seq(
- /* sum = */
- coalesce(coalesce(sum, zero) + child.cast(sumDataType), sum)
- )
+ val updateSumExpr = coalesce(coalesce(sum, zero) + child.cast(sumDataType), sum)
+ resultType match {
+ case _: DecimalType =>
+ Seq(updateSumExpr, isEmpty && child.isNull)
+ case _ => Seq(updateSumExpr)
+ }
} else {
- Seq(
- /* sum = */
- coalesce(sum, zero) + child.cast(sumDataType)
- )
+ val updateSumExpr = coalesce(sum, zero) + child.cast(sumDataType)
+ resultType match {
+ case _: DecimalType =>
+ Seq(updateSumExpr, Literal(false, BooleanType))
+ case _ => Seq(updateSumExpr)
+ }
}
}
+ /**
+ * For decimal type:
+ * If isEmpty is false and if sum is null, then it means we have an overflow.
+ *
+ * update of the sum is as follows:
+ * Check if either portion of the left.sum or right.sum has overflowed
+ * If it has, then the sum value will remain null.
+ * If it did not have overflow, then add the sum.left and sum.right and check for overflow.
+ *
+ * isEmpty: Set to false if either one of the left or right is set to false. This
+ * means we have seen atleast a row that was not null.
Review comment:
sure. 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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org