Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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 @@ -83,12 +83,19 @@ case class UnaryMinus(
val iu = IntervalUtils.getClass.getCanonicalName.stripSuffix("$")
val method = if (failOnError) "negateExact" else "negate"
defineCodeGen(ctx, ev, c => s"$iu.$method($c)")
case DayTimeIntervalType | YearMonthIntervalType =>
nullSafeCodeGen(ctx, ev, eval => {
val mathClass = classOf[Math].getName
s"${ev.value} = $mathClass.negateExact($eval);"
})
}

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

Expand Down Expand Up @@ -173,6 +180,11 @@ abstract class BinaryArithmetic extends BinaryOperator with NullIntolerant {
def calendarIntervalMethod: String =
sys.error("BinaryArithmetics must override either calendarIntervalMethod or genCode")

/** Name of the function for this expression on [[DayTimeIntervalType]] and
* [[YearMonthIntervalType]] types. */
def intervalMethod: String =
Copy link
Contributor

Choose a reason for hiding this comment

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

Shall we reuse exactMathMethod?

Copy link
Member Author

@MaxGekk MaxGekk Mar 10, 2021

Choose a reason for hiding this comment

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

Sure, since exactMathMethod is always defined in the expressions, we can directly invoke exactMathMethod.get. Though I can add a guard if exactMathMethod.isDefined.

Copy link
Contributor

Choose a reason for hiding this comment

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

we can use assert to make sure it's defined for interval types.

sys.error("BinaryArithmetics must override either intervalMethod or genCode")

// Name of the function for the exact version of this expression in [[Math]].
// If the option "spark.sql.ansi.enabled" is enabled and there is corresponding
// function in [[Math]], the exact function will be called instead of evaluation with [[symbol]].
Expand All @@ -185,6 +197,9 @@ abstract class BinaryArithmetic extends BinaryOperator with NullIntolerant {
case CalendarIntervalType =>
val iu = IntervalUtils.getClass.getCanonicalName.stripSuffix("$")
defineCodeGen(ctx, ev, (eval1, eval2) => s"$iu.$calendarIntervalMethod($eval1, $eval2)")
case DayTimeIntervalType | YearMonthIntervalType =>
val mathClass = classOf[Math].getName
defineCodeGen(ctx, ev, (eval1, eval2) => s"$mathClass.${intervalMethod}($eval1, $eval2)")
// byte and short are casted into int when add, minus, times or divide
case ByteType | ShortType =>
nullSafeCodeGen(ctx, ev, (eval1, eval2) => {
Expand Down Expand Up @@ -257,6 +272,7 @@ case class Add(
override def decimalMethod: String = "$plus"

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

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

Expand All @@ -267,6 +283,10 @@ case class Add(
case CalendarIntervalType =>
IntervalUtils.add(
input1.asInstanceOf[CalendarInterval], input2.asInstanceOf[CalendarInterval])
case DayTimeIntervalType =>
Math.addExact(input1.asInstanceOf[Long], input2.asInstanceOf[Long])
case YearMonthIntervalType =>
Math.addExact(input1.asInstanceOf[Int], input2.asInstanceOf[Int])
case _ => numeric.plus(input1, input2)
}

Expand Down Expand Up @@ -296,6 +316,7 @@ case class Subtract(
override def decimalMethod: String = "$minus"

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

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

Expand All @@ -306,6 +327,10 @@ case class Subtract(
case CalendarIntervalType =>
IntervalUtils.subtract(
input1.asInstanceOf[CalendarInterval], input2.asInstanceOf[CalendarInterval])
case DayTimeIntervalType =>
Math.subtractExact(input1.asInstanceOf[Long], input2.asInstanceOf[Long])
case YearMonthIntervalType =>
Math.subtractExact(input1.asInstanceOf[Int], input2.asInstanceOf[Int])
case _ => numeric.minus(input1, input2)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ private[sql] object TypeCollection {
* Types that include numeric types and interval type. They are only used in unary_minus,
* unary_positive, add and subtract operations.
*/
val NumericAndInterval = TypeCollection(NumericType, CalendarIntervalType)
val NumericAndInterval = TypeCollection(
NumericType,
CalendarIntervalType,
DayTimeIntervalType,
YearMonthIntervalType)

def apply(types: AbstractDataType*): TypeCollection = new TypeCollection(types)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ class ExpressionTypeCheckingSuite extends SparkFunSuite {
assertErrorForDifferingTypes(BitwiseXor(Symbol("intField"), Symbol("booleanField")))

assertError(Add(Symbol("booleanField"), Symbol("booleanField")),
"requires (numeric or interval) type")
"requires (numeric or interval or daytimeinterval or yearmonthinterval) type")
assertError(Subtract(Symbol("booleanField"), Symbol("booleanField")),
"requires (numeric or interval) type")
"requires (numeric or interval or daytimeinterval or yearmonthinterval) type")
assertError(Multiply(Symbol("booleanField"), Symbol("booleanField")), "requires numeric type")
assertError(Divide(Symbol("booleanField"), Symbol("booleanField")),
"requires (double or decimal) type")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.spark.sql.catalyst.expressions

import java.sql.{Date, Timestamp}
import java.time.{Duration, Period}

import org.apache.spark.SparkFunSuite
import org.apache.spark.sql.catalyst.InternalRow
Expand Down Expand Up @@ -576,4 +577,41 @@ class ArithmeticExpressionSuite extends SparkFunSuite with ExpressionEvalHelper
}
}
}

test("SPARK-34677: exact add and subtract of day-time and year-month intervals") {
checkExceptionInExpression[ArithmeticException](
UnaryMinus(Literal.create(Period.ofMonths(Int.MinValue), YearMonthIntervalType)),
"overflow")
Seq(true, false).foreach { failOnError =>
checkExceptionInExpression[ArithmeticException](
Subtract(
Literal.create(Period.ofMonths(Int.MinValue), YearMonthIntervalType),
Literal.create(Period.ofMonths(10), YearMonthIntervalType),
failOnError
),
"overflow")
checkExceptionInExpression[ArithmeticException](
Add(
Literal.create(Period.ofMonths(Int.MaxValue), YearMonthIntervalType),
Literal.create(Period.ofMonths(10), YearMonthIntervalType),
failOnError
),
"overflow")

checkExceptionInExpression[ArithmeticException](
Subtract(
Literal.create(Duration.ofDays(-106751991), DayTimeIntervalType),
Literal.create(Duration.ofDays(10), DayTimeIntervalType),
failOnError
),
"overflow")
checkExceptionInExpression[ArithmeticException](
Add(
Literal.create(Duration.ofDays(106751991), DayTimeIntervalType),
Literal.create(Duration.ofDays(10), DayTimeIntervalType),
failOnError
),
"overflow")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ object DataTypeTestUtils {
/**
* Instances of all [[NumericType]]s and [[CalendarIntervalType]]
*/
val numericAndInterval: Set[DataType] = numericTypeWithoutDecimal + CalendarIntervalType
val numericAndInterval: Set[DataType] = numericTypeWithoutDecimal ++ Set(
CalendarIntervalType,
DayTimeIntervalType,
YearMonthIntervalType)

/**
* All the types that support ordering
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ select +date '1999-01-01'
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
cannot resolve '(+ DATE '1999-01-01')' due to data type mismatch: argument 1 requires (numeric or interval) type, however, 'DATE '1999-01-01'' is of date type.; line 1 pos 7
cannot resolve '(+ DATE '1999-01-01')' due to data type mismatch: argument 1 requires (numeric or interval or daytimeinterval or yearmonthinterval) type, however, 'DATE '1999-01-01'' is of date type.; line 1 pos 7


-- !query
Expand All @@ -445,7 +445,7 @@ select +timestamp '1999-01-01'
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
cannot resolve '(+ TIMESTAMP '1999-01-01 00:00:00')' due to data type mismatch: argument 1 requires (numeric or interval) type, however, 'TIMESTAMP '1999-01-01 00:00:00'' is of timestamp type.; line 1 pos 7
cannot resolve '(+ TIMESTAMP '1999-01-01 00:00:00')' due to data type mismatch: argument 1 requires (numeric or interval or daytimeinterval or yearmonthinterval) type, however, 'TIMESTAMP '1999-01-01 00:00:00'' is of timestamp type.; line 1 pos 7


-- !query
Expand All @@ -462,7 +462,7 @@ select +map(1, 2)
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
cannot resolve '(+ map(1, 2))' due to data type mismatch: argument 1 requires (numeric or interval) type, however, 'map(1, 2)' is of map<int,int> type.; line 1 pos 7
cannot resolve '(+ map(1, 2))' due to data type mismatch: argument 1 requires (numeric or interval or daytimeinterval or yearmonthinterval) type, however, 'map(1, 2)' is of map<int,int> type.; line 1 pos 7


-- !query
Expand All @@ -471,7 +471,7 @@ select +array(1,2)
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
cannot resolve '(+ array(1, 2))' due to data type mismatch: argument 1 requires (numeric or interval) type, however, 'array(1, 2)' is of array<int> type.; line 1 pos 7
cannot resolve '(+ array(1, 2))' due to data type mismatch: argument 1 requires (numeric or interval or daytimeinterval or yearmonthinterval) type, however, 'array(1, 2)' is of array<int> type.; line 1 pos 7


-- !query
Expand All @@ -480,7 +480,7 @@ select +named_struct('a', 1, 'b', 'spark')
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
cannot resolve '(+ named_struct('a', 1, 'b', 'spark'))' due to data type mismatch: argument 1 requires (numeric or interval) type, however, 'named_struct('a', 1, 'b', 'spark')' is of struct<a:int,b:string> type.; line 1 pos 7
cannot resolve '(+ named_struct('a', 1, 'b', 'spark'))' due to data type mismatch: argument 1 requires (numeric or interval or daytimeinterval or yearmonthinterval) type, however, 'named_struct('a', 1, 'b', 'spark')' is of struct<a:int,b:string> type.; line 1 pos 7


-- !query
Expand All @@ -489,7 +489,7 @@ select +X'1'
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
cannot resolve '(+ X'01')' due to data type mismatch: argument 1 requires (numeric or interval) type, however, 'X'01'' is of binary type.; line 1 pos 7
cannot resolve '(+ X'01')' due to data type mismatch: argument 1 requires (numeric or interval or daytimeinterval or yearmonthinterval) type, however, 'X'01'' is of binary type.; line 1 pos 7


-- !query
Expand All @@ -498,7 +498,7 @@ select -date '1999-01-01'
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
cannot resolve '(- DATE '1999-01-01')' due to data type mismatch: argument 1 requires (numeric or interval) type, however, 'DATE '1999-01-01'' is of date type.; line 1 pos 7
cannot resolve '(- DATE '1999-01-01')' due to data type mismatch: argument 1 requires (numeric or interval or daytimeinterval or yearmonthinterval) type, however, 'DATE '1999-01-01'' is of date type.; line 1 pos 7


-- !query
Expand All @@ -507,7 +507,7 @@ select -timestamp '1999-01-01'
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
cannot resolve '(- TIMESTAMP '1999-01-01 00:00:00')' due to data type mismatch: argument 1 requires (numeric or interval) type, however, 'TIMESTAMP '1999-01-01 00:00:00'' is of timestamp type.; line 1 pos 7
cannot resolve '(- TIMESTAMP '1999-01-01 00:00:00')' due to data type mismatch: argument 1 requires (numeric or interval or daytimeinterval or yearmonthinterval) type, however, 'TIMESTAMP '1999-01-01 00:00:00'' is of timestamp type.; line 1 pos 7


-- !query
Expand All @@ -516,4 +516,4 @@ select -x'2379ACFe'
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
cannot resolve '(- X'2379ACFE')' due to data type mismatch: argument 1 requires (numeric or interval) type, however, 'X'2379ACFE'' is of binary type.; line 1 pos 7
cannot resolve '(- X'2379ACFE')' due to data type mismatch: argument 1 requires (numeric or interval or daytimeinterval or yearmonthinterval) type, however, 'X'2379ACFE'' is of binary type.; line 1 pos 7
Expand Down
18 changes: 9 additions & 9 deletions sql/core/src/test/resources/sql-tests/results/literals.sql.out
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ select +date '1999-01-01'
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
cannot resolve '(+ DATE '1999-01-01')' due to data type mismatch: argument 1 requires (numeric or interval) type, however, 'DATE '1999-01-01'' is of date type.; line 1 pos 7
cannot resolve '(+ DATE '1999-01-01')' due to data type mismatch: argument 1 requires (numeric or interval or daytimeinterval or yearmonthinterval) type, however, 'DATE '1999-01-01'' is of date type.; line 1 pos 7


-- !query
Expand All @@ -445,7 +445,7 @@ select +timestamp '1999-01-01'
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
cannot resolve '(+ TIMESTAMP '1999-01-01 00:00:00')' due to data type mismatch: argument 1 requires (numeric or interval) type, however, 'TIMESTAMP '1999-01-01 00:00:00'' is of timestamp type.; line 1 pos 7
cannot resolve '(+ TIMESTAMP '1999-01-01 00:00:00')' due to data type mismatch: argument 1 requires (numeric or interval or daytimeinterval or yearmonthinterval) type, however, 'TIMESTAMP '1999-01-01 00:00:00'' is of timestamp type.; line 1 pos 7


-- !query
Expand All @@ -462,7 +462,7 @@ select +map(1, 2)
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
cannot resolve '(+ map(1, 2))' due to data type mismatch: argument 1 requires (numeric or interval) type, however, 'map(1, 2)' is of map<int,int> type.; line 1 pos 7
cannot resolve '(+ map(1, 2))' due to data type mismatch: argument 1 requires (numeric or interval or daytimeinterval or yearmonthinterval) type, however, 'map(1, 2)' is of map<int,int> type.; line 1 pos 7


-- !query
Expand All @@ -471,7 +471,7 @@ select +array(1,2)
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
cannot resolve '(+ array(1, 2))' due to data type mismatch: argument 1 requires (numeric or interval) type, however, 'array(1, 2)' is of array<int> type.; line 1 pos 7
cannot resolve '(+ array(1, 2))' due to data type mismatch: argument 1 requires (numeric or interval or daytimeinterval or yearmonthinterval) type, however, 'array(1, 2)' is of array<int> type.; line 1 pos 7


-- !query
Expand All @@ -480,7 +480,7 @@ select +named_struct('a', 1, 'b', 'spark')
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
cannot resolve '(+ named_struct('a', 1, 'b', 'spark'))' due to data type mismatch: argument 1 requires (numeric or interval) type, however, 'named_struct('a', 1, 'b', 'spark')' is of struct<a:int,b:string> type.; line 1 pos 7
cannot resolve '(+ named_struct('a', 1, 'b', 'spark'))' due to data type mismatch: argument 1 requires (numeric or interval or daytimeinterval or yearmonthinterval) type, however, 'named_struct('a', 1, 'b', 'spark')' is of struct<a:int,b:string> type.; line 1 pos 7


-- !query
Expand All @@ -489,7 +489,7 @@ select +X'1'
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
cannot resolve '(+ X'01')' due to data type mismatch: argument 1 requires (numeric or interval) type, however, 'X'01'' is of binary type.; line 1 pos 7
cannot resolve '(+ X'01')' due to data type mismatch: argument 1 requires (numeric or interval or daytimeinterval or yearmonthinterval) type, however, 'X'01'' is of binary type.; line 1 pos 7


-- !query
Expand All @@ -498,7 +498,7 @@ select -date '1999-01-01'
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
cannot resolve '(- DATE '1999-01-01')' due to data type mismatch: argument 1 requires (numeric or interval) type, however, 'DATE '1999-01-01'' is of date type.; line 1 pos 7
cannot resolve '(- DATE '1999-01-01')' due to data type mismatch: argument 1 requires (numeric or interval or daytimeinterval or yearmonthinterval) type, however, 'DATE '1999-01-01'' is of date type.; line 1 pos 7


-- !query
Expand All @@ -507,7 +507,7 @@ select -timestamp '1999-01-01'
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
cannot resolve '(- TIMESTAMP '1999-01-01 00:00:00')' due to data type mismatch: argument 1 requires (numeric or interval) type, however, 'TIMESTAMP '1999-01-01 00:00:00'' is of timestamp type.; line 1 pos 7
cannot resolve '(- TIMESTAMP '1999-01-01 00:00:00')' due to data type mismatch: argument 1 requires (numeric or interval or daytimeinterval or yearmonthinterval) type, however, 'TIMESTAMP '1999-01-01 00:00:00'' is of timestamp type.; line 1 pos 7


-- !query
Expand All @@ -516,4 +516,4 @@ select -x'2379ACFe'
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
cannot resolve '(- X'2379ACFE')' due to data type mismatch: argument 1 requires (numeric or interval) type, however, 'X'2379ACFE'' is of binary type.; line 1 pos 7
cannot resolve '(- X'2379ACFE')' due to data type mismatch: argument 1 requires (numeric or interval or daytimeinterval or yearmonthinterval) type, however, 'X'2379ACFE'' is of binary type.; line 1 pos 7
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ SELECT COUNT(*) OVER (PARTITION BY 1 ORDER BY cast(1 as string) DESC RANGE BETWE
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
cannot resolve 'RANGE BETWEEN CURRENT ROW AND CAST(1 AS STRING) FOLLOWING' due to data type mismatch: The data type of the upper bound 'string' does not match the expected data type '(numeric or interval)'.; line 1 pos 21
cannot resolve 'RANGE BETWEEN CURRENT ROW AND CAST(1 AS STRING) FOLLOWING' due to data type mismatch: The data type of the upper bound 'string' does not match the expected data type '(numeric or interval or daytimeinterval or yearmonthinterval)'.; line 1 pos 21


-- !query
Expand All @@ -177,7 +177,7 @@ SELECT COUNT(*) OVER (PARTITION BY 1 ORDER BY cast('1' as binary) DESC RANGE BET
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
cannot resolve 'RANGE BETWEEN CURRENT ROW AND CAST(1 AS BINARY) FOLLOWING' due to data type mismatch: The data type of the upper bound 'binary' does not match the expected data type '(numeric or interval)'.; line 1 pos 21
cannot resolve 'RANGE BETWEEN CURRENT ROW AND CAST(1 AS BINARY) FOLLOWING' due to data type mismatch: The data type of the upper bound 'binary' does not match the expected data type '(numeric or interval or daytimeinterval or yearmonthinterval)'.; line 1 pos 21


-- !query
Expand All @@ -186,7 +186,7 @@ SELECT COUNT(*) OVER (PARTITION BY 1 ORDER BY cast(1 as boolean) DESC RANGE BETW
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
cannot resolve 'RANGE BETWEEN CURRENT ROW AND CAST(1 AS BOOLEAN) FOLLOWING' due to data type mismatch: The data type of the upper bound 'boolean' does not match the expected data type '(numeric or interval)'.; line 1 pos 21
cannot resolve 'RANGE BETWEEN CURRENT ROW AND CAST(1 AS BOOLEAN) FOLLOWING' due to data type mismatch: The data type of the upper bound 'boolean' does not match the expected data type '(numeric or interval or daytimeinterval or yearmonthinterval)'.; line 1 pos 21


-- !query
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.spark.sql

import java.sql.{Date, Timestamp}
import java.time.{Duration, Period}
import java.util.Locale

import org.apache.hadoop.io.{LongWritable, Text}
Expand Down Expand Up @@ -2375,4 +2376,16 @@ class ColumnExpressionSuite extends QueryTest with SharedSparkSession {
assert(e2.getCause.isInstanceOf[RuntimeException])
assert(e2.getCause.getMessage == "hello")
}

test("SPARK-34677: negate/add/subtract year-month and day-time intervals") {
import testImplicits._
val df = Seq((Period.ofMonths(10), Duration.ofDays(10), Period.ofMonths(1), Duration.ofDays(1)))
.toDF("year-month-A", "day-time-A", "year-month-B", "day-time-B")
val negatedDF = df.select(-$"year-month-A", -$"day-time-A")
checkAnswer(negatedDF, Row(Period.ofMonths(-10), Duration.ofDays(-10)))
val sumDF = df.select($"year-month-A" + $"year-month-B", $"day-time-A" + $"day-time-B")
Copy link
Contributor

Choose a reason for hiding this comment

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

sumDF -> addDF

Copy link
Contributor

Choose a reason for hiding this comment

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

does sum function accept the new interval types?

Copy link
Member Author

Choose a reason for hiding this comment

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

does sum function accept the new interval types?

sum operates over NumericType only, see

override def inputTypes: Seq[AbstractDataType] = Seq(NumericType)
. If you think we should support new intervals in it, let's do that separately since this PR is about operators +/-.

Copy link
Member Author

Choose a reason for hiding this comment

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

I opened the JIRA SPARK-34716 to don't forget about it.

checkAnswer(sumDF, Row(Period.ofMonths(11), Duration.ofDays(11)))
val subDF = df.select($"year-month-A" - $"year-month-B", $"day-time-A" - $"day-time-B")
checkAnswer(subDF, Row(Period.ofMonths(9), Duration.ofDays(9)))
}
}