Skip to content
Closed
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 @@ -17,10 +17,9 @@

package org.apache.spark.sql.catalyst.expressions.aggregate

import org.apache.spark.sql.catalyst.analysis.{DecimalPrecision, FunctionRegistry, TypeCheckResult}
import org.apache.spark.sql.catalyst.analysis.{DecimalPrecision, FunctionRegistry}
import org.apache.spark.sql.catalyst.dsl.expressions._
import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.catalyst.util.TypeUtils
import org.apache.spark.sql.types._

@ExpressionDescription(
Expand Down Expand Up @@ -81,7 +80,8 @@ case class Average(child: Expression) extends DeclarativeAggregate with Implicit
case _: DecimalType =>
DecimalPrecision.decimalAndDecimal(sum / count.cast(DecimalType.LongDecimal)).cast(resultType)
case CalendarIntervalType =>
DivideInterval(sum.cast(resultType), count.cast(DoubleType))
val newCount = If(EqualTo(count, Literal(0L)), Literal(null, LongType), count)
Copy link
Contributor

Choose a reason for hiding this comment

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

avg(interval) is also new in 3.0 right? We can also fail here if this is the SQL standard.

Copy link
Contributor

Choose a reason for hiding this comment

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

I checked pgsql, avg on empty table returns null. So this is corrected.

DivideInterval(sum.cast(resultType), newCount.cast(DoubleType))
case _ =>
sum.cast(resultType) / count.cast(resultType)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,15 @@ case class UnaryMinus(child: Expression) extends UnaryExpression
"""})
case _: CalendarIntervalType =>
val iu = IntervalUtils.getClass.getCanonicalName.stripSuffix("$")
defineCodeGen(ctx, ev, c => s"$iu.negate($c)")
val method = if (checkOverflow) "negateExact" else "negate"
defineCodeGen(ctx, ev, c => s"$iu.$method($c)")
}

protected override def nullSafeEval(input: Any): Any = dataType match {
case CalendarIntervalType if checkOverflow =>
IntervalUtils.negateExact(input.asInstanceOf[CalendarInterval])
case CalendarIntervalType => IntervalUtils.negate(input.asInstanceOf[CalendarInterval])
case _ => numeric.negate(input)
case _ => numeric.negate(input)
}

override def sql: String = s"(- ${child.sql})"
Expand Down Expand Up @@ -224,13 +227,17 @@ case class Add(left: Expression, right: Expression) extends BinaryArithmetic {

override def decimalMethod: String = "$plus"

override def calendarIntervalMethod: String = "add"
override def calendarIntervalMethod: String = if (checkOverflow) "addExact" else "add"

private lazy val numeric = TypeUtils.getNumeric(dataType, checkOverflow)

protected override def nullSafeEval(input1: Any, input2: Any): Any = dataType match {
case CalendarIntervalType => IntervalUtils.add(
input1.asInstanceOf[CalendarInterval], input2.asInstanceOf[CalendarInterval])
case CalendarIntervalType if checkOverflow =>
IntervalUtils.addExact(
input1.asInstanceOf[CalendarInterval], input2.asInstanceOf[CalendarInterval])
case CalendarIntervalType =>
IntervalUtils.add(
input1.asInstanceOf[CalendarInterval], input2.asInstanceOf[CalendarInterval])
case _ => numeric.plus(input1, input2)
}

Expand All @@ -252,13 +259,17 @@ case class Subtract(left: Expression, right: Expression) extends BinaryArithmeti

override def decimalMethod: String = "$minus"

override def calendarIntervalMethod: String = "subtract"
override def calendarIntervalMethod: String = if (checkOverflow) "subtractExact" else "subtract"

private lazy val numeric = TypeUtils.getNumeric(dataType, checkOverflow)

protected override def nullSafeEval(input1: Any, input2: Any): Any = dataType match {
case CalendarIntervalType => IntervalUtils.subtract(
input1.asInstanceOf[CalendarInterval], input2.asInstanceOf[CalendarInterval])
case CalendarIntervalType if checkOverflow =>
IntervalUtils.subtractExact(
input1.asInstanceOf[CalendarInterval], input2.asInstanceOf[CalendarInterval])
case CalendarIntervalType =>
IntervalUtils.subtract(
input1.asInstanceOf[CalendarInterval], input2.asInstanceOf[CalendarInterval])
case _ => numeric.minus(input1, input2)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,34 +125,22 @@ abstract class IntervalNumOperation(
override def nullable: Boolean = true

override def nullSafeEval(interval: Any, num: Any): Any = {
try {
operation(interval.asInstanceOf[CalendarInterval], num.asInstanceOf[Double])
} catch {
case _: java.lang.ArithmeticException => null
}
operation(interval.asInstanceOf[CalendarInterval], num.asInstanceOf[Double])
}

override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
nullSafeCodeGen(ctx, ev, (interval, num) => {
val iu = IntervalUtils.getClass.getName.stripSuffix("$")
s"""
try {
${ev.value} = $iu.$operationName($interval, $num);
} catch (java.lang.ArithmeticException e) {
${ev.isNull} = true;
}
"""
})
val iu = IntervalUtils.getClass.getName.stripSuffix("$")
defineCodeGen(ctx, ev, (interval, num) => s"$iu.$operationName($interval, $num)")
}

override def prettyName: String = operationName + "_interval"
override def prettyName: String = operationName.stripSuffix("Exact") + "_interval"
}

case class MultiplyInterval(interval: Expression, num: Expression)
extends IntervalNumOperation(interval, num, multiply, "multiply")
extends IntervalNumOperation(interval, num, multiplyExact, "multiplyExact")

case class DivideInterval(interval: Expression, num: Expression)
extends IntervalNumOperation(interval, num, divide, "divide")
extends IntervalNumOperation(interval, num, divideExact, "divideExact")

// scalastyle:off line.size.limit
@ExpressionDescription(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ object IntervalUtils {
assert(input.length == input.trim.length)
input match {
case yearMonthPattern("-", yearStr, monthStr) =>
negate(toInterval(yearStr, monthStr))
negateExact(toInterval(yearStr, monthStr))
case yearMonthPattern(_, yearStr, monthStr) =>
toInterval(yearStr, monthStr)
case _ =>
Expand Down Expand Up @@ -401,6 +401,8 @@ object IntervalUtils {
/**
* Makes an interval from months, days and micros with the fractional part by
* adding the month fraction to days and the days fraction to micros.
*
* @throws ArithmeticException if the result overflows any field value
*/
private def fromDoubles(
monthsWithFraction: Double,
Expand All @@ -416,13 +418,34 @@ object IntervalUtils {
/**
* Unary minus, return the negated the calendar interval value.
*
* @param interval the interval to be negated
* @return a new calendar interval instance with all it parameters negated from the origin one.
* @throws ArithmeticException if the result overflows any field value
*/
def negateExact(interval: CalendarInterval): CalendarInterval = {
val months = Math.negateExact(interval.months)
val days = Math.negateExact(interval.days)
val microseconds = Math.negateExact(interval.microseconds)
new CalendarInterval(months, days, microseconds)
}

/**
* Unary minus, return the negated the calendar interval value.
*/
def negate(interval: CalendarInterval): CalendarInterval = {
new CalendarInterval(-interval.months, -interval.days, -interval.microseconds)
}

/**
* Return a new calendar interval instance of the sum of two intervals.
*
* @throws ArithmeticException if the result overflows any field value
*/
def addExact(left: CalendarInterval, right: CalendarInterval): CalendarInterval = {
val months = Math.addExact(left.months, right.months)
val days = Math.addExact(left.days, right.days)
val microseconds = Math.addExact(left.microseconds, right.microseconds)
new CalendarInterval(months, days, microseconds)
}

/**
* Return a new calendar interval instance of the sum of two intervals.
*/
Expand All @@ -434,7 +457,19 @@ object IntervalUtils {
}

/**
* Return a new calendar interval instance of the left intervals minus the right one.
* Return a new calendar interval instance of the left interval minus the right one.
*
* @throws ArithmeticException if the result overflows any field value
*/
def subtractExact(left: CalendarInterval, right: CalendarInterval): CalendarInterval = {
val months = Math.subtractExact(left.months, right.months)
val days = Math.subtractExact(left.days, right.days)
val microseconds = Math.subtractExact(left.microseconds, right.microseconds)
new CalendarInterval(months, days, microseconds)
}

/**
* Return a new calendar interval instance of the left interval minus the right one.
*/
def subtract(left: CalendarInterval, right: CalendarInterval): CalendarInterval = {
val months = left.months - right.months
Expand All @@ -443,12 +478,22 @@ object IntervalUtils {
new CalendarInterval(months, days, microseconds)
}

def multiply(interval: CalendarInterval, num: Double): CalendarInterval = {
/**
* Return a new calendar interval instance of the left interval times a multiplier.
*
* @throws ArithmeticException if the result overflows any field value
*/
def multiplyExact(interval: CalendarInterval, num: Double): CalendarInterval = {
fromDoubles(num * interval.months, num * interval.days, num * interval.microseconds)
}

def divide(interval: CalendarInterval, num: Double): CalendarInterval = {
if (num == 0) throw new java.lang.ArithmeticException("divide by zero")
/**
* Return a new calendar interval instance of the left interval divides by a dividend.
*
* @throws ArithmeticException if the result overflows any field value or divided by zero
*/
def divideExact(interval: CalendarInterval, num: Double): CalendarInterval = {
if (num == 0) throw new ArithmeticException("divide by zero")
fromDoubles(interval.months / num, interval.days / num, interval.microseconds / num)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ class CollectionExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper
checkEvaluation(new Sequence(
Literal(Timestamp.valueOf("2018-01-02 00:00:00")),
Literal(Timestamp.valueOf("2018-01-01 00:00:00")),
Literal(negate(stringToInterval("interval 12 hours")))),
Literal(negateExact(stringToInterval("interval 12 hours")))),
Seq(
Timestamp.valueOf("2018-01-02 00:00:00"),
Timestamp.valueOf("2018-01-01 12:00:00"),
Expand All @@ -742,7 +742,7 @@ class CollectionExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper
checkEvaluation(new Sequence(
Literal(Timestamp.valueOf("2018-01-02 00:00:00")),
Literal(Timestamp.valueOf("2017-12-31 23:59:59")),
Literal(negate(stringToInterval("interval 12 hours")))),
Literal(negateExact(stringToInterval("interval 12 hours")))),
Seq(
Timestamp.valueOf("2018-01-02 00:00:00"),
Timestamp.valueOf("2018-01-01 12:00:00"),
Expand All @@ -760,7 +760,7 @@ class CollectionExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper
checkEvaluation(new Sequence(
Literal(Timestamp.valueOf("2018-03-01 00:00:00")),
Literal(Timestamp.valueOf("2018-01-01 00:00:00")),
Literal(negate(stringToInterval("interval 1 month")))),
Literal(negateExact(stringToInterval("interval 1 month")))),
Seq(
Timestamp.valueOf("2018-03-01 00:00:00"),
Timestamp.valueOf("2018-02-01 00:00:00"),
Expand All @@ -769,7 +769,7 @@ class CollectionExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper
checkEvaluation(new Sequence(
Literal(Timestamp.valueOf("2018-03-03 00:00:00")),
Literal(Timestamp.valueOf("2018-01-01 00:00:00")),
Literal(negate(stringToInterval("interval 1 month 1 day")))),
Literal(negateExact(stringToInterval("interval 1 month 1 day")))),
Seq(
Timestamp.valueOf("2018-03-03 00:00:00"),
Timestamp.valueOf("2018-02-02 00:00:00"),
Expand Down Expand Up @@ -815,7 +815,7 @@ class CollectionExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper
checkEvaluation(new Sequence(
Literal(Timestamp.valueOf("2022-04-01 00:00:00")),
Literal(Timestamp.valueOf("2017-01-01 00:00:00")),
Literal(negate(fromYearMonthString("1-5")))),
Literal(negateExact(fromYearMonthString("1-5")))),
Seq(
Timestamp.valueOf("2022-04-01 00:00:00.000"),
Timestamp.valueOf("2020-11-01 00:00:00.000"),
Expand Down Expand Up @@ -907,7 +907,7 @@ class CollectionExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper
new Sequence(
Literal(Date.valueOf("1970-01-01")),
Literal(Date.valueOf("1970-02-01")),
Literal(negate(stringToInterval("interval 1 month")))),
Literal(negateExact(stringToInterval("interval 1 month")))),
EmptyRow,
s"sequence boundaries: 0 to 2678400000000 by -${28 * MICROS_PER_DAY}")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import scala.language.implicitConversions

import org.apache.spark.SparkFunSuite
import org.apache.spark.sql.catalyst.util.DateTimeConstants._
import org.apache.spark.sql.catalyst.util.IntervalUtils.stringToInterval
import org.apache.spark.sql.catalyst.util.IntervalUtils.{safeStringToInterval, stringToInterval}
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.types.Decimal
import org.apache.spark.unsafe.types.{CalendarInterval, UTF8String}

Expand Down Expand Up @@ -198,9 +199,17 @@ class IntervalExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {

test("multiply") {
def check(interval: String, num: Double, expected: String): Unit = {
checkEvaluation(
MultiplyInterval(Literal(stringToInterval(interval)), Literal(num)),
if (expected == null) null else stringToInterval(expected))
val expr = MultiplyInterval(Literal(stringToInterval(interval)), Literal(num))
val expectedRes = safeStringToInterval(expected)
Seq("true", "false").foreach { v =>
withSQLConf(SQLConf.ANSI_ENABLED.key -> v) {
if (expectedRes == null) {
checkExceptionInExpression[ArithmeticException](expr, expected)
} else {
checkEvaluation(expr, expectedRes)
}
}
}
}

check("0 seconds", 10, "0 seconds")
Expand All @@ -211,14 +220,22 @@ class IntervalExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
check("-100 years -1 millisecond", 0.5, "-50 years -500 microseconds")
check("2 months 4 seconds", -0.5, "-1 months -2 seconds")
check("1 month 2 microseconds", 1.5, "1 months 15 days 3 microseconds")
check("2 months", Int.MaxValue, null)
check("2 months", Int.MaxValue, "integer overflow")
}

test("divide") {
def check(interval: String, num: Double, expected: String): Unit = {
checkEvaluation(
DivideInterval(Literal(stringToInterval(interval)), Literal(num)),
if (expected == null) null else stringToInterval(expected))
val expr = DivideInterval(Literal(stringToInterval(interval)), Literal(num))
val expectedRes = safeStringToInterval(expected)
Seq("true", "false").foreach { v =>
withSQLConf(SQLConf.ANSI_ENABLED.key -> v) {
if (expectedRes == null) {
checkExceptionInExpression[ArithmeticException](expr, expected)
} else {
checkEvaluation(expr, expectedRes)
}
}
}
}

check("0 seconds", 10, "0 seconds")
Expand All @@ -228,7 +245,8 @@ class IntervalExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
check("2 years -8 seconds", 0.5, "4 years -16 seconds")
check("-1 month 2 microseconds", -0.25, "4 months -8 microseconds")
check("1 month 3 microsecond", 1.5, "20 days 2 microseconds")
check("1 second", 0, null)
check("1 second", 0, "divide by zero")
check(s"${Int.MaxValue} months", 0.9, "integer overflow")
}

test("make interval") {
Expand Down
Loading