Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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 @@ -46,15 +46,20 @@ case class Sum(child: Expression) extends DeclarativeAggregate with ImplicitCast
// Return data type.
override def dataType: DataType = resultType

override def inputTypes: Seq[AbstractDataType] = Seq(NumericType)
override def inputTypes: Seq[AbstractDataType] =
Seq(TypeCollection(NumericType, YearMonthIntervalType, DayTimeIntervalType))

override def checkInputDataTypes(): TypeCheckResult =
TypeUtils.checkForNumericExpr(child.dataType, "function sum")
override def checkInputDataTypes(): TypeCheckResult = child.dataType match {
case YearMonthIntervalType | DayTimeIntervalType => TypeCheckResult.TypeCheckSuccess
case _ => TypeUtils.checkForNumericExpr(child.dataType, "function sum")
}

private lazy val resultType = child.dataType match {
case DecimalType.Fixed(precision, scale) =>
DecimalType.bounded(precision + 10, scale)
case _: IntegralType => LongType
case _: YearMonthIntervalType => YearMonthIntervalType
case _: DayTimeIntervalType => DayTimeIntervalType
case _ => DoubleType
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.apache.spark.sql

import java.time.{Duration, Period}

import scala.util.Random

import org.scalatest.matchers.must.Matchers.the
Expand Down Expand Up @@ -1110,6 +1112,14 @@ class DataFrameAggregateSuite extends QueryTest
val e = intercept[AnalysisException](arrayDF.groupBy(struct($"col.a")).count())
assert(e.message.contains("requires integral type"))
}

test("SPARK-34716: Support ANSI SQL intervals by the aggregate function `sum`") {
Copy link
Member

Choose a reason for hiding this comment

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

Could you test more cases:

  1. Negative tests such as overflow
  2. Aggregate nulls and non-nulls
  3. Looking at [SPARK-29663][SQL] Support sum with interval type values #26325, we will need to add more tests as soon as we support construction of the intervals in SQL via cast or make_interval

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For the third suggestion, If we support intervals in SQL, we will add new test cases.

val df = Seq((Period.ofMonths(10), Duration.ofDays(10)),
(Period.ofMonths(1), Duration.ofDays(1)))
.toDF("year-month", "day-time")
val sumDF = df.select(sum($"year-month"), sum($"day-time"))
checkAnswer(sumDF, Row(Period.ofMonths(11), Duration.ofDays(11)))
Copy link
Member

Choose a reason for hiding this comment

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

You modified the resultType but don't check the type in schema. Could you add such check, please.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK

}
}

case class B(c: Option[Double])
Expand Down