This is an automated email from the ASF dual-hosted git repository.
uros-b pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/spark.git
The following commit(s) were added to refs/heads/master by this push:
new 2b9d75274e8a [SPARK-60929][SQL] Fail on overflow in next_day, last_day
and trunc instead of wrapping around
2b9d75274e8a is described below
commit 2b9d75274e8a857aaa4425eda22486d3a4c15462
Author: Stevo Mitric <[email protected]>
AuthorDate: Thu Nov 12 11:29:08 2026 +0000
[SPARK-60929][SQL] Fail on overflow in next_day, last_day and trunc instead
of wrapping around
### What changes were proposed in this pull request?
`next_day` and `last_day` now fail with `DATETIME_OVERFLOW` when the result
is later than the maximum `DATE` (`+5881580-07-11`), and `trunc` on a `DATE`
fails with it when the result is earlier than the minimum `DATE`
(`-5877641-06-23`), regardless of `spark.sql.ansi.enabled`. The `DateTimeUtils`
helpers compute in `Long` and check the result instead of wrapping an `Int`.
All four `trunc` levels fail the same way; `QUARTER` used to raise
`ARITHMETIC_OVERFLOW`.
### Why are the changes needed?
Near the bounds of the `DATE` range these functions silently return a wrong
date, in both ANSI modes:
```sql
SELECT last_day(DATE'+5881580-07-11'); -- -5877641-07-12
SELECT next_day(DATE'-5877641-06-23', 'MO'); -- -5877641-06-25, a Thursday
SELECT trunc(DATE'-5877641-06-23', 'MONTH'); -- +5881580-06-20
```
Same bug class as #59135 (SPARK-59859) for `date_add`.
### Does this PR introduce _any_ user-facing change?
Yes, only near the bounds of the `DATE` range. Results that do not fit now
fail with `DATETIME_OVERFLOW`, and `next_day` and `trunc(d, 'WEEK')` return the
right date for the first days of the range, e.g.
`next_day(DATE'-5877641-06-23', 'MO')` is now `-5877641-06-29`. Ordinary dates
are unchanged. Documented in the migration guide.
### How was this patch tested?
- New tests in `DateTimeUtilsSuite` and `DateExpressionsSuite` for every
boundary date, interpreted and codegen; they fail without the fix.
- New `datetime-special.sql` golden cases for ANSI, non-ANSI and
`TIMESTAMP_NTZ` (`date.sql` can't carry years outside 0000-9999 through
`ThriftServerQueryTestSuite`).
- Existing `trunc` / `date_trunc` golden files, `DateFunctionsSuite`,
`ExpressionInfoSuite` and scalastyle pass.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code (Claude Fable 5.1, Claude Opus 5.5)
This pull request and its description were written by Isaac.
Closes #60130 from stevomitric/stevomitric/next-day-last-day-overflow.
Authored-by: Stevo Mitric <[email protected]>
Signed-off-by: Uros Bojanic <[email protected]>
skip-checks: true
---
docs/sql-migration-guide.md | 1 +
.../expressions/DateTimeExpressionUtils.java | 4 +-
.../catalyst/expressions/datetimeExpressions.scala | 14 ++-
.../spark/sql/catalyst/util/DateTimeUtils.scala | 39 +++++--
.../spark/sql/errors/QueryExecutionErrors.scala | 30 +++++
.../expressions/DateExpressionsSuite.scala | 63 +++++++++++
.../sql/catalyst/util/DateTimeUtilsSuite.scala | 74 +++++++++++++
.../analyzer-results/datetime-special.sql.out | 60 ++++++++++
.../nonansi/datetime-special.sql.out | 60 ++++++++++
.../timestampNTZ/datetime-special-ansi.sql.out | 60 ++++++++++
.../timestampNTZ/datetime-special.sql.out | 60 ++++++++++
.../sql-tests/inputs/datetime-special.sql | 14 +++
.../sql-tests/results/datetime-special.sql.out | 122 +++++++++++++++++++++
.../results/nonansi/datetime-special.sql.out | 122 +++++++++++++++++++++
.../timestampNTZ/datetime-special-ansi.sql.out | 122 +++++++++++++++++++++
.../results/timestampNTZ/datetime-special.sql.out | 122 +++++++++++++++++++++
16 files changed, 955 insertions(+), 12 deletions(-)
diff --git a/docs/sql-migration-guide.md b/docs/sql-migration-guide.md
index 0e74722273f4..f883512f4ef9 100644
--- a/docs/sql-migration-guide.md
+++ b/docs/sql-migration-guide.md
@@ -98,6 +98,7 @@ license: |
- Since Spark 4.4, `CREATE TABLE`, `REPLACE TABLE`, and `ALTER TABLE ... ADD
CONSTRAINT` on a Data Source V2 table fail with `SUBQUERY_IN_CHECK_CONSTRAINT`
when the condition of a CHECK constraint contains a subquery. `ALTER TABLE ...
ADD CONSTRAINT` keeps failing with
`UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.HIGHER_ORDER_FUNCTION` on a subquery
in a lambda function. Previously, most of these statements failed with
`INTERNAL_ERROR`, but a subquery that needed no resolution, such as `id [...]
- Since Spark 4.4, a CHECK constraint that `ALTER TABLE ... ADD CONSTRAINT`
adds to a Data Source V2 table, and whose condition reads the current time,
date, catalog, user and the like, such as `ts <= current_timestamp()` or `db =
current_database()`, is checked against their values at the time of each write.
Previously, it was checked against their values at the time of the `ALTER
TABLE`, unless a write could not read back a predicate for its condition, such
as one with a lambda functio [...]
- Since Spark 4.4, a function created by `CREATE FUNCTION ... AS 'class'`
whose class implements
`org.apache.spark.sql.connector.catalog.functions.UnboundFunction` and extends
either a Hive UDF class (such as `UDF` or `GenericUDF`) or
`org.apache.spark.sql.expressions.UserDefinedAggregateFunction` runs as a
DataSource V2 function instead of a Hive UDF or Spark UDAF, including functions
created in earlier versions. Its result type, argument coercion, and null
handling are defined by the V [...]
+- Since Spark 4.4, `next_day` and `last_day` fail with `DATETIME_OVERFLOW`
when the result is later than the maximum `DATE`, `+5881580-07-11`, and `trunc`
fails with it when the result is earlier than the minimum `DATE`,
`-5877641-06-23`, regardless of `spark.sql.ansi.enabled`. Previously, these
functions silently returned a wrong date, in most cases one at the other end of
the `DATE` range; for example, `last_day(DATE'+5881580-07-11')` returned
`-5877641-07-12` and `trunc(DATE'-5877641- [...]
## Upgrading from Spark SQL 4.3.0 to 4.3.1
diff --git
a/sql/catalyst/src/main/java/org/apache/spark/sql/catalyst/expressions/DateTimeExpressionUtils.java
b/sql/catalyst/src/main/java/org/apache/spark/sql/catalyst/expressions/DateTimeExpressionUtils.java
index 4a370aab5268..4e852732094b 100644
---
a/sql/catalyst/src/main/java/org/apache/spark/sql/catalyst/expressions/DateTimeExpressionUtils.java
+++
b/sql/catalyst/src/main/java/org/apache/spark/sql/catalyst/expressions/DateTimeExpressionUtils.java
@@ -200,7 +200,9 @@ public final class DateTimeExpressionUtils {
* ({@code failOnError = true}). The {@code SparkIllegalArgumentException}
* thrown by {@code DateTimeUtils.getDayOfWeekFromString} for an invalid
* day-of-week string propagates to the caller unchanged (in ANSI mode the
- * caller wants exactly that exception), so no try/catch wrapper is emitted.
+ * caller wants exactly that exception), so no try/catch wrapper is emitted;
+ * the {@code DATETIME_OVERFLOW} error of {@code getNextDateForDayOfWeek}
+ * propagates the same way.
*/
public static int getNextDateExact(int startDate, UTF8String dayOfWeek) {
int dow = DateTimeUtils.getDayOfWeekFromString(dayOfWeek);
diff --git
a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/datetimeExpressions.scala
b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/datetimeExpressions.scala
index 4ebb971fe19d..820ddac873c5 100644
---
a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/datetimeExpressions.scala
+++
b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/datetimeExpressions.scala
@@ -2548,7 +2548,10 @@ case class FromUnixTime(sec: Expression, format:
Expression, timeZoneId: Option[
* Returns the last day of the month which the date belongs to.
*/
@ExpressionDescription(
- usage = "_FUNC_(date) - Returns the last day of the month which the date
belongs to.",
+ usage = """_FUNC_(date) - Returns the last day of the month which the date
belongs to.
+ If that day is later than the maximum DATE `+5881580-07-11`, the
function fails with
+ `DATETIME_OVERFLOW` regardless of `spark.sql.ansi.enabled`.
+ """,
arguments = """
Arguments:
* date - The date whose month's last day is returned.
@@ -2600,6 +2603,7 @@ case class LastDay(startDate: Expression)
The function returns NULL if at least one of the input parameters is
NULL.
When both of the input parameters are not NULL and day_of_week is an
invalid input,
the function throws SparkIllegalArgumentException if
`spark.sql.ansi.enabled` is set to true, otherwise NULL.
+ If the result is later than the maximum DATE `+5881580-07-11`, the
function fails with `DATETIME_OVERFLOW` regardless of `spark.sql.ansi.enabled`.
""",
arguments = """
Arguments:
@@ -2639,6 +2643,7 @@ case class NextDay(
DateTimeExpressionUtils.getNextDateExact(
start.asInstanceOf[Int], dayOfW.asInstanceOf[UTF8String])
} else {
+ // NULL only for an invalid day of week; DATETIME_OVERFLOW propagates in
both modes.
try {
val dow =
DateTimeUtils.getDayOfWeekFromString(dayOfW.asInstanceOf[UTF8String])
DateTimeUtils.getNextDateForDayOfWeek(start.asInstanceOf[Int], dow)
@@ -2656,9 +2661,9 @@ case class NextDay(
sd: String,
dowS: String): String = {
if (failOnError) {
- // In ANSI mode the only exception (SparkIllegalArgumentException for an
- // invalid day-of-week) must propagate, so the previous catch merely
- // rethrew it. Delegate to the helper and drop the no-op try/catch.
+ // In ANSI mode an invalid day-of-week (SparkIllegalArgumentException)
must
+ // propagate like the overflow error, so the previous catch merely
rethrew
+ // it. Delegate to the helper and drop the no-op try/catch.
val utils = classOf[DateTimeExpressionUtils].getName
s"${ev.value} = $utils.getNextDateExact($sd, $dowS);"
} else {
@@ -3593,6 +3598,7 @@ trait TruncInstant extends BinaryExpression with
ImplicitCastInputTypes {
@ExpressionDescription(
usage = """
_FUNC_(date, fmt) - Returns `date` with the time portion of the day
truncated to the unit specified by the format model `fmt`.
+ If the result is earlier than the minimum DATE `-5877641-06-23`, the
function fails with `DATETIME_OVERFLOW` regardless of `spark.sql.ansi.enabled`.
""",
arguments = """
Arguments:
diff --git
a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/DateTimeUtils.scala
b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/DateTimeUtils.scala
index 25e2861f27e9..ba78568bdaa7 100644
---
a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/DateTimeUtils.scala
+++
b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/DateTimeUtils.scala
@@ -536,18 +536,34 @@ object DateTimeUtils extends SparkDateTimeUtils {
}
}
+ // Long arithmetic: `dayOfWeek - 1 - startDay` and the sum overflow Int near
the DATE bounds.
+ private def nextDateForDayOfWeek(startDay: Long, dayOfWeek: Int): Long = {
+ startDay + 1 + Math.floorMod(dayOfWeek - 1 - startDay, 7L)
+ }
+
/**
* Returns the first date which is later than startDate and is of the given
dayOfWeek.
* dayOfWeek is an integer ranges in [0, 6], and 0 is Thu, 1 is Fri, etc,.
+ * @throws org.apache.spark.SparkArithmeticException if the result is later
than the maximum DATE.
*/
def getNextDateForDayOfWeek(startDay: Int, dayOfWeek: Int): Int = {
- startDay + 1 + ((dayOfWeek - 1 - startDay) % 7 + 7) % 7
+ val result = nextDateForDayOfWeek(startDay, dayOfWeek)
+ if (result > Int.MaxValue) {
+ val dayName = DayOfWeek.THURSDAY.plus(dayOfWeek).toString
+ throw QueryExecutionErrors.nextDayOverflowError(startDay, dayName)
+ }
+ result.toInt
}
- /** Returns last day of the month for the given number of days since
1970-01-01. */
+ /**
+ * Returns last day of the month for the given number of days since
1970-01-01.
+ * @throws org.apache.spark.SparkArithmeticException if the result is later
than the maximum DATE.
+ */
def getLastDayOfMonth(days: Int): Int = {
val localDate = daysToLocalDate(days)
- (days - localDate.getDayOfMonth) + localDate.lengthOfMonth()
+ val result = days - localDate.getDayOfMonth.toLong +
localDate.lengthOfMonth()
+ if (result > Int.MaxValue) throw
QueryExecutionErrors.lastDayOverflowError(days)
+ result.toInt
}
// The constants are visible for testing purpose only.
@@ -572,22 +588,31 @@ object DateTimeUtils extends SparkDateTimeUtils {
/**
* Returns the trunc date from original date and trunc level.
* Trunc level should be generated using `parseTruncLevel()`, should be
between 6 and 9.
+ * @throws org.apache.spark.SparkArithmeticException if the result is
earlier than the minimum
+ * DATE.
*/
def truncDate(days: Int, level: Int): Int = {
level match {
- case TRUNC_TO_WEEK => getNextDateForDayOfWeek(days - 7, MONDAY)
- case TRUNC_TO_MONTH => days - getDayOfMonth(days) + 1
+ case TRUNC_TO_WEEK => checkTruncDate(days, nextDateForDayOfWeek(days -
7L, MONDAY), "WEEK")
+ case TRUNC_TO_MONTH => checkTruncDate(days, days -
getDayOfMonth(days).toLong + 1, "MONTH")
case TRUNC_TO_QUARTER =>
val ld = daysToLocalDate(days)
val firstMonthOfQuarter = ((ld.getMonthValue - 1) / 3) * 3 + 1
- localDateToDays(ld.withMonth(firstMonthOfQuarter).withDayOfMonth(1))
- case TRUNC_TO_YEAR => days - getDayInYear(days) + 1
+ val result =
ld.withMonth(firstMonthOfQuarter).withDayOfMonth(1).toEpochDay
+ checkTruncDate(days, result, "QUARTER")
+ case TRUNC_TO_YEAR => checkTruncDate(days, days -
getDayInYear(days).toLong + 1, "YEAR")
case _ =>
// caller make sure that this should never be reached
throw QueryExecutionErrors.unreachableError(s": Invalid trunc level:
$level")
}
}
+ // Truncation never moves a date later, so only the minimum DATE can be
crossed.
+ private def checkTruncDate(days: Int, result: Long, unit: String): Int = {
+ if (result < Int.MinValue) throw
QueryExecutionErrors.truncDateOverflowError(days, unit)
+ result.toInt
+ }
+
private def truncToUnit(micros: Long, zoneId: ZoneId, unit: ChronoUnit):
Long = {
val truncated = microsToInstant(micros).atZone(zoneId).truncatedTo(unit)
instantToMicros(truncated.toInstant)
diff --git
a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala
b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala
index 0e26f41acdf8..a086b9966caa 100644
---
a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala
+++
b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala
@@ -2925,6 +2925,36 @@ private[sql] object QueryExecutionErrors extends
QueryErrorsBase with ExecutionE
summary = "")
}
+ def nextDayOverflowError(days: Int, dayOfWeek: String):
SparkArithmeticException = {
+ new SparkArithmeticException(
+ errorClass = "DATETIME_OVERFLOW",
+ messageParameters = Map(
+ "operation" -> (s"find the next $dayOfWeek after " +
+ s"${toSQLValue(DateTimeUtils.daysToLocalDate(days), DateType)}")),
+ context = Array.empty,
+ summary = "")
+ }
+
+ def lastDayOverflowError(days: Int): SparkArithmeticException = {
+ new SparkArithmeticException(
+ errorClass = "DATETIME_OVERFLOW",
+ messageParameters = Map(
+ "operation" -> (s"find the last day of the month of " +
+ s"${toSQLValue(DateTimeUtils.daysToLocalDate(days), DateType)}")),
+ context = Array.empty,
+ summary = "")
+ }
+
+ def truncDateOverflowError(days: Int, unit: String):
SparkArithmeticException = {
+ new SparkArithmeticException(
+ errorClass = "DATETIME_OVERFLOW",
+ messageParameters = Map(
+ "operation" -> (s"truncate
${toSQLValue(DateTimeUtils.daysToLocalDate(days), DateType)} " +
+ s"to $unit")),
+ context = Array.empty,
+ summary = "")
+ }
+
def calendarIntervalArrowNanosOverflowError(
interval: CalendarInterval): SparkArithmeticException = {
new SparkArithmeticException(
diff --git
a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/DateExpressionsSuite.scala
b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/DateExpressionsSuite.scala
index cacbfd9a3193..bbc9ffe295c1 100644
---
a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/DateExpressionsSuite.scala
+++
b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/DateExpressionsSuite.scala
@@ -842,6 +842,69 @@ class DateExpressionsSuite extends SparkFunSuite with
ExpressionEvalHelper {
}
}
+ test("SPARK-60929: last_day near the DATE range bounds") {
+ val min = Int.MinValue // -5877641-06-23
+ val max = Int.MaxValue // +5881580-07-11
+ checkEvaluation(LastDay(Literal(min, DateType)), min + 7)
+ checkEvaluation(LastDay(Literal(max - 11, DateType)), max - 11)
+ Seq(max - 10 -> "+5881580-07-01", max -> "+5881580-07-11").foreach { case
(days, date) =>
+ checkErrorInExpression[SparkArithmeticException](
+ LastDay(Literal(days, DateType)),
+ condition = "DATETIME_OVERFLOW",
+ parameters = Map("operation" -> s"find the last day of the month of
DATE '$date'"))
+ }
+ }
+
+ test("SPARK-60929: next_day near the DATE range bounds") {
+ val min = Int.MinValue // -5877641-06-23, a Tuesday
+ val max = Int.MaxValue // +5881580-07-11, a Friday
+ // `failOnError` only governs an invalid day of week. Codegen has a path
for a foldable one.
+ Seq(true, false).foreach { failOnError =>
+ Seq[String => Expression](s => Literal(s), s =>
NonFoldableLiteral(s)).foreach { dayOfWeek =>
+ def nextDay(days: Int, dow: String): NextDay =
+ NextDay(Literal(days, DateType), dayOfWeek(dow), failOnError)
+ checkEvaluation(nextDay(min, "MO"), min + 6)
+ checkEvaluation(nextDay(min, "FR"), min + 3)
+ checkEvaluation(nextDay(min, "TU"), min + 7)
+ checkEvaluation(nextDay(max - 7, "FR"), max)
+ checkEvaluation(nextDay(max - 6, "FR"), max)
+ checkErrorInExpression[SparkArithmeticException](
+ nextDay(max - 6, "SA"),
+ condition = "DATETIME_OVERFLOW",
+ parameters = Map("operation" -> "find the next SATURDAY after DATE
'+5881580-07-05'"))
+ checkErrorInExpression[SparkArithmeticException](
+ nextDay(max, "Mon"),
+ condition = "DATETIME_OVERFLOW",
+ parameters = Map("operation" -> "find the next MONDAY after DATE
'+5881580-07-11'"))
+ }
+ }
+ }
+
+ test("SPARK-60929: trunc near the minimum DATE") {
+ val min = Int.MinValue // -5877641-06-23
+ val lastDayOfMinYear =
DateTimeUtils.localDateToDays(LocalDate.of(-5877641, 12, 31))
+ // Codegen has a path for a foldable format.
+ Seq[String => Expression](s => Literal(s), s =>
NonFoldableLiteral(s)).foreach { format =>
+ def trunc(days: Int, fmt: String): TruncDate = TruncDate(Literal(days,
DateType), format(fmt))
+ checkEvaluation(trunc(min + 6, "WEEK"), min + 6)
+ checkEvaluation(trunc(min + 8, "WEEK"), min + 6)
+ checkEvaluation(trunc(min + 8, "MM"), min + 8)
+ checkEvaluation(trunc(min + 8, "QUARTER"), min + 8)
+ checkEvaluation(trunc(lastDayOfMinYear + 1, "YEAR"), lastDayOfMinYear +
1)
+ Seq(
+ (min + 5, "WEEK", "DATE '-5877641-06-28' to WEEK"),
+ (min + 7, "MM", "DATE '-5877641-06-30' to MONTH"),
+ (min, "QUARTER", "DATE '-5877641-06-23' to QUARTER"),
+ (lastDayOfMinYear, "YEAR", "DATE '-5877641-12-31' to YEAR")).foreach {
+ case (days, fmt, operation) =>
+ checkErrorInExpression[SparkArithmeticException](
+ trunc(days, fmt),
+ condition = "DATETIME_OVERFLOW",
+ parameters = Map("operation" -> s"truncate $operation"))
+ }
+ }
+ }
+
private def testTruncDate(input: Date, fmt: String, expected: Date): Unit = {
checkEvaluation(TruncDate(Literal.create(input, DateType),
Literal.create(fmt, StringType)),
expected)
diff --git
a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/util/DateTimeUtilsSuite.scala
b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/util/DateTimeUtilsSuite.scala
index dc7728a367b6..de45d4bfc42b 100644
---
a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/util/DateTimeUtilsSuite.scala
+++
b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/util/DateTimeUtilsSuite.scala
@@ -1136,6 +1136,80 @@ class DateTimeUtilsSuite extends SparkFunSuite with
Matchers with SQLHelper {
parameters = Map("string" -> "\"quote"))
}
+ test("SPARK-60929: getNextDateForDayOfWeek near the DATE range bounds") {
+ val min = Int.MinValue // -5877641-06-23, a Tuesday
+ val max = Int.MaxValue // +5881580-07-11, a Friday
+ def nextDay(startDay: Int, dayOfWeek: String): Int =
+ getNextDateForDayOfWeek(startDay,
getDayOfWeekFromString(UTF8String.fromString(dayOfWeek)))
+ // The right weekday: `dayOfWeek - 1 - startDay` used to wrap.
+ assert(nextDay(min, "MO") === min + 6)
+ assert(nextDay(min, "FR") === min + 3)
+ assert(nextDay(min, "TU") === min + 7)
+ (min to min + 10).foreach { startDay =>
+ (0 to 6).foreach { dayOfWeek =>
+ val result = getNextDateForDayOfWeek(startDay, dayOfWeek)
+ assert(result > startDay && result <= startDay + 7)
+ assert(Math.floorMod(result, 7) === dayOfWeek)
+ }
+ }
+ // The largest results that fit.
+ assert(nextDay(max - 7, "FR") === max)
+ assert(nextDay(max - 6, "FR") === max)
+ assert(nextDay(max - 1, "FR") === max)
+ // Later than the maximum DATE.
+ Seq(
+ (max - 6, "SA", "SATURDAY", "+5881580-07-05"),
+ (max, "MO", "MONDAY", "+5881580-07-11"),
+ (max, "FR", "FRIDAY", "+5881580-07-11")).foreach { case (startDay, dow,
name, date) =>
+ checkError(
+ exception = intercept[SparkArithmeticException](nextDay(startDay,
dow)),
+ condition = "DATETIME_OVERFLOW",
+ parameters = Map("operation" -> s"find the next $name after DATE
'$date'"))
+ }
+ }
+
+ test("SPARK-60929: getLastDayOfMonth near the DATE range bounds") {
+ // -5877641-06-23 -> -5877641-06-30
+ assert(getLastDayOfMonth(Int.MinValue) === Int.MinValue + 7)
+ // +5881580-06-30 is the largest result that fits.
+ assert(getLastDayOfMonth(Int.MaxValue - 11) === Int.MaxValue - 11)
+ // +5881580-07-01 .. +5881580-07-11: +5881580-07-31 is later than the
maximum DATE.
+ (Int.MaxValue - 10 to Int.MaxValue).foreach { days =>
+ val date = f"+5881580-07-${days - (Int.MaxValue - 11)}%02d"
+ checkError(
+ exception =
intercept[SparkArithmeticException](getLastDayOfMonth(days)),
+ condition = "DATETIME_OVERFLOW",
+ parameters = Map("operation" -> s"find the last day of the month of
DATE '$date'"))
+ }
+ }
+
+ test("SPARK-60929: truncDate near the minimum DATE") {
+ val min = Int.MinValue // -5877641-06-23, a Tuesday
+ def assertOverflow(days: Int, level: Int, unit: String): Unit = {
+ checkError(
+ exception = intercept[SparkArithmeticException](truncDate(days,
level)),
+ condition = "DATETIME_OVERFLOW",
+ parameters = Map("operation" -> s"truncate DATE
'${daysToLocalDate(days)}' to $unit"))
+ }
+ // WEEK: min + 6 (-5877641-06-29) is the first Monday of the DATE range.
+ (min to min + 5).foreach(assertOverflow(_, TRUNC_TO_WEEK, "WEEK"))
+ (min + 6 to min + 12).foreach(d => assert(truncDate(d, TRUNC_TO_WEEK) ===
min + 6))
+ // MONTH and QUARTER: min + 8 is -5877641-07-01.
+ (min to min + 7).foreach { d =>
+ assertOverflow(d, TRUNC_TO_MONTH, "MONTH")
+ assertOverflow(d, TRUNC_TO_QUARTER, "QUARTER")
+ }
+ (min + 8 to min + 38).foreach(d => assert(truncDate(d, TRUNC_TO_MONTH) ===
min + 8))
+ (min + 8 to min + 99).foreach(d => assert(truncDate(d, TRUNC_TO_QUARTER)
=== min + 8))
+ // YEAR: every date of year -5877641.
+ val nextYear = LocalDate.of(-5877640, 1, 1)
+ val nextYearDays = localDateToDays(nextYear)
+ (min until nextYearDays).foreach(assertOverflow(_, TRUNC_TO_YEAR, "YEAR"))
+ (nextYearDays until nextYearDays + nextYear.lengthOfYear).foreach { d =>
+ assert(truncDate(d, TRUNC_TO_YEAR) === nextYearDays)
+ }
+ }
+
test("SPARK-34761: timestamp add day-time interval") {
// transit from Pacific Standard Time to Pacific Daylight Time
assert(timestampAddDayTime(
diff --git
a/sql/core/src/test/resources/sql-tests/analyzer-results/datetime-special.sql.out
b/sql/core/src/test/resources/sql-tests/analyzer-results/datetime-special.sql.out
index 6768297fd811..78dfa3f608b8 100644
---
a/sql/core/src/test/resources/sql-tests/analyzer-results/datetime-special.sql.out
+++
b/sql/core/src/test/resources/sql-tests/analyzer-results/datetime-special.sql.out
@@ -23,3 +23,63 @@ select make_timestamp(-1969, 12, 31, 16, 0, 0.0),
make_timestamp(-15, 3, 18, 16,
-- !query analysis
Project [make_timestamp(-1969, 12, 31, 16, 0, cast(0.0 as decimal(16,6)),
None, Some(America/Los_Angeles), true, TimestampType) AS make_timestamp(-1969,
12, 31, 16, 0, 0.0)#x, make_timestamp(-15, 3, 18, 16, 0, cast(0.0 as
decimal(16,6)), None, Some(America/Los_Angeles), true, TimestampType) AS
make_timestamp(-15, 3, 18, 16, 0, 0.0)#x, make_timestamp(99999, 3, 18, 12, 3,
cast(17.0 as decimal(16,6)), None, Some(America/Los_Angeles), true,
TimestampType) AS make_timestamp(99999, 3, 18, 12, [...]
+- OneRowRelation
+
+
+-- !query
+select next_day(date'-5877641-06-23', 'MO'), next_day(date'-5877641-06-23',
'FR')
+-- !query analysis
+[Analyzer test output redacted due to nondeterminism]
+
+
+-- !query
+select next_day(date'+5881580-07-04', 'FR')
+-- !query analysis
+[Analyzer test output redacted due to nondeterminism]
+
+
+-- !query
+select next_day(date'+5881580-07-11', 'MO')
+-- !query analysis
+[Analyzer test output redacted due to nondeterminism]
+
+
+-- !query
+select last_day(date'-5877641-06-23'), last_day(date'+5881580-06-30')
+-- !query analysis
+[Analyzer test output redacted due to nondeterminism]
+
+
+-- !query
+select last_day(date'+5881580-07-11')
+-- !query analysis
+[Analyzer test output redacted due to nondeterminism]
+
+
+-- !query
+select trunc(date'-5877641-06-29', 'WEEK'), trunc(date'-5877641-07-01',
'WEEK'), trunc(date'-5877641-07-01', 'MM'), trunc(date'-5877641-07-01',
'QUARTER'), trunc(date'-5877640-06-23', 'YEAR')
+-- !query analysis
+[Analyzer test output redacted due to nondeterminism]
+
+
+-- !query
+select trunc(date'-5877641-06-28', 'WEEK')
+-- !query analysis
+[Analyzer test output redacted due to nondeterminism]
+
+
+-- !query
+select trunc(date'-5877641-06-30', 'MONTH')
+-- !query analysis
+[Analyzer test output redacted due to nondeterminism]
+
+
+-- !query
+select trunc(date'-5877641-06-23', 'QUARTER')
+-- !query analysis
+[Analyzer test output redacted due to nondeterminism]
+
+
+-- !query
+select trunc(date'-5877641-12-31', 'YEAR')
+-- !query analysis
+[Analyzer test output redacted due to nondeterminism]
diff --git
a/sql/core/src/test/resources/sql-tests/analyzer-results/nonansi/datetime-special.sql.out
b/sql/core/src/test/resources/sql-tests/analyzer-results/nonansi/datetime-special.sql.out
index 01d1f2c40a4a..a165d6069846 100644
---
a/sql/core/src/test/resources/sql-tests/analyzer-results/nonansi/datetime-special.sql.out
+++
b/sql/core/src/test/resources/sql-tests/analyzer-results/nonansi/datetime-special.sql.out
@@ -23,3 +23,63 @@ select make_timestamp(-1969, 12, 31, 16, 0, 0.0),
make_timestamp(-15, 3, 18, 16,
-- !query analysis
Project [make_timestamp(-1969, 12, 31, 16, 0, cast(0.0 as decimal(16,6)),
None, Some(America/Los_Angeles), false, TimestampType) AS make_timestamp(-1969,
12, 31, 16, 0, 0.0)#x, make_timestamp(-15, 3, 18, 16, 0, cast(0.0 as
decimal(16,6)), None, Some(America/Los_Angeles), false, TimestampType) AS
make_timestamp(-15, 3, 18, 16, 0, 0.0)#x, make_timestamp(99999, 3, 18, 12, 3,
cast(17.0 as decimal(16,6)), None, Some(America/Los_Angeles), false,
TimestampType) AS make_timestamp(99999, 3, 18, 1 [...]
+- OneRowRelation
+
+
+-- !query
+select next_day(date'-5877641-06-23', 'MO'), next_day(date'-5877641-06-23',
'FR')
+-- !query analysis
+[Analyzer test output redacted due to nondeterminism]
+
+
+-- !query
+select next_day(date'+5881580-07-04', 'FR')
+-- !query analysis
+[Analyzer test output redacted due to nondeterminism]
+
+
+-- !query
+select next_day(date'+5881580-07-11', 'MO')
+-- !query analysis
+[Analyzer test output redacted due to nondeterminism]
+
+
+-- !query
+select last_day(date'-5877641-06-23'), last_day(date'+5881580-06-30')
+-- !query analysis
+[Analyzer test output redacted due to nondeterminism]
+
+
+-- !query
+select last_day(date'+5881580-07-11')
+-- !query analysis
+[Analyzer test output redacted due to nondeterminism]
+
+
+-- !query
+select trunc(date'-5877641-06-29', 'WEEK'), trunc(date'-5877641-07-01',
'WEEK'), trunc(date'-5877641-07-01', 'MM'), trunc(date'-5877641-07-01',
'QUARTER'), trunc(date'-5877640-06-23', 'YEAR')
+-- !query analysis
+[Analyzer test output redacted due to nondeterminism]
+
+
+-- !query
+select trunc(date'-5877641-06-28', 'WEEK')
+-- !query analysis
+[Analyzer test output redacted due to nondeterminism]
+
+
+-- !query
+select trunc(date'-5877641-06-30', 'MONTH')
+-- !query analysis
+[Analyzer test output redacted due to nondeterminism]
+
+
+-- !query
+select trunc(date'-5877641-06-23', 'QUARTER')
+-- !query analysis
+[Analyzer test output redacted due to nondeterminism]
+
+
+-- !query
+select trunc(date'-5877641-12-31', 'YEAR')
+-- !query analysis
+[Analyzer test output redacted due to nondeterminism]
diff --git
a/sql/core/src/test/resources/sql-tests/analyzer-results/timestampNTZ/datetime-special-ansi.sql.out
b/sql/core/src/test/resources/sql-tests/analyzer-results/timestampNTZ/datetime-special-ansi.sql.out
index 0f8ad6eb2ccf..b1d10c30fdba 100644
---
a/sql/core/src/test/resources/sql-tests/analyzer-results/timestampNTZ/datetime-special-ansi.sql.out
+++
b/sql/core/src/test/resources/sql-tests/analyzer-results/timestampNTZ/datetime-special-ansi.sql.out
@@ -24,3 +24,63 @@ select make_timestamp(-1969, 12, 31, 16, 0, 0.0),
make_timestamp(-15, 3, 18, 16,
-- !query analysis
Project [make_timestamp(-1969, 12, 31, 16, 0, cast(0.0 as decimal(16,6)),
None, Some(America/Los_Angeles), true, TimestampNTZType) AS
make_timestamp(-1969, 12, 31, 16, 0, 0.0)#x, make_timestamp(-15, 3, 18, 16, 0,
cast(0.0 as decimal(16,6)), None, Some(America/Los_Angeles), true,
TimestampNTZType) AS make_timestamp(-15, 3, 18, 16, 0, 0.0)#x,
make_timestamp(99999, 3, 18, 12, 3, cast(17.0 as decimal(16,6)), None,
Some(America/Los_Angeles), true, TimestampNTZType) AS make_timestamp(99999, 3,
[...]
+- OneRowRelation
+
+
+-- !query
+select next_day(date'-5877641-06-23', 'MO'), next_day(date'-5877641-06-23',
'FR')
+-- !query analysis
+[Analyzer test output redacted due to nondeterminism]
+
+
+-- !query
+select next_day(date'+5881580-07-04', 'FR')
+-- !query analysis
+[Analyzer test output redacted due to nondeterminism]
+
+
+-- !query
+select next_day(date'+5881580-07-11', 'MO')
+-- !query analysis
+[Analyzer test output redacted due to nondeterminism]
+
+
+-- !query
+select last_day(date'-5877641-06-23'), last_day(date'+5881580-06-30')
+-- !query analysis
+[Analyzer test output redacted due to nondeterminism]
+
+
+-- !query
+select last_day(date'+5881580-07-11')
+-- !query analysis
+[Analyzer test output redacted due to nondeterminism]
+
+
+-- !query
+select trunc(date'-5877641-06-29', 'WEEK'), trunc(date'-5877641-07-01',
'WEEK'), trunc(date'-5877641-07-01', 'MM'), trunc(date'-5877641-07-01',
'QUARTER'), trunc(date'-5877640-06-23', 'YEAR')
+-- !query analysis
+[Analyzer test output redacted due to nondeterminism]
+
+
+-- !query
+select trunc(date'-5877641-06-28', 'WEEK')
+-- !query analysis
+[Analyzer test output redacted due to nondeterminism]
+
+
+-- !query
+select trunc(date'-5877641-06-30', 'MONTH')
+-- !query analysis
+[Analyzer test output redacted due to nondeterminism]
+
+
+-- !query
+select trunc(date'-5877641-06-23', 'QUARTER')
+-- !query analysis
+[Analyzer test output redacted due to nondeterminism]
+
+
+-- !query
+select trunc(date'-5877641-12-31', 'YEAR')
+-- !query analysis
+[Analyzer test output redacted due to nondeterminism]
diff --git
a/sql/core/src/test/resources/sql-tests/analyzer-results/timestampNTZ/datetime-special.sql.out
b/sql/core/src/test/resources/sql-tests/analyzer-results/timestampNTZ/datetime-special.sql.out
index 7a7bd07ab61b..680466753c5b 100644
---
a/sql/core/src/test/resources/sql-tests/analyzer-results/timestampNTZ/datetime-special.sql.out
+++
b/sql/core/src/test/resources/sql-tests/analyzer-results/timestampNTZ/datetime-special.sql.out
@@ -24,3 +24,63 @@ select make_timestamp(-1969, 12, 31, 16, 0, 0.0),
make_timestamp(-15, 3, 18, 16,
-- !query analysis
Project [make_timestamp(-1969, 12, 31, 16, 0, cast(0.0 as decimal(16,6)),
None, Some(America/Los_Angeles), false, TimestampNTZType) AS
make_timestamp(-1969, 12, 31, 16, 0, 0.0)#x, make_timestamp(-15, 3, 18, 16, 0,
cast(0.0 as decimal(16,6)), None, Some(America/Los_Angeles), false,
TimestampNTZType) AS make_timestamp(-15, 3, 18, 16, 0, 0.0)#x,
make_timestamp(99999, 3, 18, 12, 3, cast(17.0 as decimal(16,6)), None,
Some(America/Los_Angeles), false, TimestampNTZType) AS make_timestamp(99999,
[...]
+- OneRowRelation
+
+
+-- !query
+select next_day(date'-5877641-06-23', 'MO'), next_day(date'-5877641-06-23',
'FR')
+-- !query analysis
+[Analyzer test output redacted due to nondeterminism]
+
+
+-- !query
+select next_day(date'+5881580-07-04', 'FR')
+-- !query analysis
+[Analyzer test output redacted due to nondeterminism]
+
+
+-- !query
+select next_day(date'+5881580-07-11', 'MO')
+-- !query analysis
+[Analyzer test output redacted due to nondeterminism]
+
+
+-- !query
+select last_day(date'-5877641-06-23'), last_day(date'+5881580-06-30')
+-- !query analysis
+[Analyzer test output redacted due to nondeterminism]
+
+
+-- !query
+select last_day(date'+5881580-07-11')
+-- !query analysis
+[Analyzer test output redacted due to nondeterminism]
+
+
+-- !query
+select trunc(date'-5877641-06-29', 'WEEK'), trunc(date'-5877641-07-01',
'WEEK'), trunc(date'-5877641-07-01', 'MM'), trunc(date'-5877641-07-01',
'QUARTER'), trunc(date'-5877640-06-23', 'YEAR')
+-- !query analysis
+[Analyzer test output redacted due to nondeterminism]
+
+
+-- !query
+select trunc(date'-5877641-06-28', 'WEEK')
+-- !query analysis
+[Analyzer test output redacted due to nondeterminism]
+
+
+-- !query
+select trunc(date'-5877641-06-30', 'MONTH')
+-- !query analysis
+[Analyzer test output redacted due to nondeterminism]
+
+
+-- !query
+select trunc(date'-5877641-06-23', 'QUARTER')
+-- !query analysis
+[Analyzer test output redacted due to nondeterminism]
+
+
+-- !query
+select trunc(date'-5877641-12-31', 'YEAR')
+-- !query analysis
+[Analyzer test output redacted due to nondeterminism]
diff --git a/sql/core/src/test/resources/sql-tests/inputs/datetime-special.sql
b/sql/core/src/test/resources/sql-tests/inputs/datetime-special.sql
index 2699274bb7c9..452a68ce4f40 100644
--- a/sql/core/src/test/resources/sql-tests/inputs/datetime-special.sql
+++ b/sql/core/src/test/resources/sql-tests/inputs/datetime-special.sql
@@ -8,3 +8,17 @@ select make_date(999999, 3, 18), make_date(-1, 1, 28);
-- timestamp with year outside [0000-9999]
select timestamp'-1969-12-31 16:00:00', timestamp'-0015-03-18 16:00:00',
timestamp'-000001', timestamp'99999-03-18T12:03:17';
select make_timestamp(-1969, 12, 31, 16, 0, 0.0), make_timestamp(-15, 3, 18,
16, 0, 0.0), make_timestamp(99999, 3, 18, 12, 3, 17.0);
+
+-- next_day and last_day at the bounds of the DATE range: -5877641-06-23 is
the minimum DATE and
+-- +5881580-07-11 (a Friday) the maximum. A result past the maximum fails
instead of wrapping around.
+select next_day(date'-5877641-06-23', 'MO'), next_day(date'-5877641-06-23',
'FR');
+select next_day(date'+5881580-07-04', 'FR');
+select next_day(date'+5881580-07-11', 'MO');
+select last_day(date'-5877641-06-23'), last_day(date'+5881580-06-30');
+select last_day(date'+5881580-07-11');
+-- trunc fails instead of wrapping around when the result is earlier than the
minimum DATE.
+select trunc(date'-5877641-06-29', 'WEEK'), trunc(date'-5877641-07-01',
'WEEK'), trunc(date'-5877641-07-01', 'MM'), trunc(date'-5877641-07-01',
'QUARTER'), trunc(date'-5877640-06-23', 'YEAR');
+select trunc(date'-5877641-06-28', 'WEEK');
+select trunc(date'-5877641-06-30', 'MONTH');
+select trunc(date'-5877641-06-23', 'QUARTER');
+select trunc(date'-5877641-12-31', 'YEAR');
diff --git
a/sql/core/src/test/resources/sql-tests/results/datetime-special.sql.out
b/sql/core/src/test/resources/sql-tests/results/datetime-special.sql.out
index 4f7ecf4b03f4..fc3fd7b5ad5b 100644
--- a/sql/core/src/test/resources/sql-tests/results/datetime-special.sql.out
+++ b/sql/core/src/test/resources/sql-tests/results/datetime-special.sql.out
@@ -29,3 +29,125 @@ select make_timestamp(-1969, 12, 31, 16, 0, 0.0),
make_timestamp(-15, 3, 18, 16,
struct<make_timestamp(-1969, 12, 31, 16, 0, 0.0):timestamp,make_timestamp(-15,
3, 18, 16, 0, 0.0):timestamp,make_timestamp(99999, 3, 18, 12, 3,
17.0):timestamp>
-- !query output
-1969-12-31 16:00:00 -0015-03-18 16:00:00 +99999-03-18 12:03:17
+
+
+-- !query
+select next_day(date'-5877641-06-23', 'MO'), next_day(date'-5877641-06-23',
'FR')
+-- !query schema
+struct<next_day(DATE '-5877641-06-23', MO):date,next_day(DATE
'-5877641-06-23', FR):date>
+-- !query output
+-5877641-06-29 -5877641-06-26
+
+
+-- !query
+select next_day(date'+5881580-07-04', 'FR')
+-- !query schema
+struct<next_day(DATE '+5881580-07-04', FR):date>
+-- !query output
++5881580-07-11
+
+
+-- !query
+select next_day(date'+5881580-07-11', 'MO')
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.SparkArithmeticException
+{
+ "errorClass" : "DATETIME_OVERFLOW",
+ "sqlState" : "22008",
+ "messageParameters" : {
+ "operation" : "find the next MONDAY after DATE '+5881580-07-11'"
+ }
+}
+
+
+-- !query
+select last_day(date'-5877641-06-23'), last_day(date'+5881580-06-30')
+-- !query schema
+struct<last_day(DATE '-5877641-06-23'):date,last_day(DATE
'+5881580-06-30'):date>
+-- !query output
+-5877641-06-30 +5881580-06-30
+
+
+-- !query
+select last_day(date'+5881580-07-11')
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.SparkArithmeticException
+{
+ "errorClass" : "DATETIME_OVERFLOW",
+ "sqlState" : "22008",
+ "messageParameters" : {
+ "operation" : "find the last day of the month of DATE '+5881580-07-11'"
+ }
+}
+
+
+-- !query
+select trunc(date'-5877641-06-29', 'WEEK'), trunc(date'-5877641-07-01',
'WEEK'), trunc(date'-5877641-07-01', 'MM'), trunc(date'-5877641-07-01',
'QUARTER'), trunc(date'-5877640-06-23', 'YEAR')
+-- !query schema
+struct<trunc(DATE '-5877641-06-29', WEEK):date,trunc(DATE '-5877641-07-01',
WEEK):date,trunc(DATE '-5877641-07-01', MM):date,trunc(DATE '-5877641-07-01',
QUARTER):date,trunc(DATE '-5877640-06-23', YEAR):date>
+-- !query output
+-5877641-06-29 -5877641-06-29 -5877641-07-01 -5877641-07-01 -5877640-01-01
+
+
+-- !query
+select trunc(date'-5877641-06-28', 'WEEK')
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.SparkArithmeticException
+{
+ "errorClass" : "DATETIME_OVERFLOW",
+ "sqlState" : "22008",
+ "messageParameters" : {
+ "operation" : "truncate DATE '-5877641-06-28' to WEEK"
+ }
+}
+
+
+-- !query
+select trunc(date'-5877641-06-30', 'MONTH')
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.SparkArithmeticException
+{
+ "errorClass" : "DATETIME_OVERFLOW",
+ "sqlState" : "22008",
+ "messageParameters" : {
+ "operation" : "truncate DATE '-5877641-06-30' to MONTH"
+ }
+}
+
+
+-- !query
+select trunc(date'-5877641-06-23', 'QUARTER')
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.SparkArithmeticException
+{
+ "errorClass" : "DATETIME_OVERFLOW",
+ "sqlState" : "22008",
+ "messageParameters" : {
+ "operation" : "truncate DATE '-5877641-06-23' to QUARTER"
+ }
+}
+
+
+-- !query
+select trunc(date'-5877641-12-31', 'YEAR')
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.SparkArithmeticException
+{
+ "errorClass" : "DATETIME_OVERFLOW",
+ "sqlState" : "22008",
+ "messageParameters" : {
+ "operation" : "truncate DATE '-5877641-12-31' to YEAR"
+ }
+}
diff --git
a/sql/core/src/test/resources/sql-tests/results/nonansi/datetime-special.sql.out
b/sql/core/src/test/resources/sql-tests/results/nonansi/datetime-special.sql.out
index 4f7ecf4b03f4..fc3fd7b5ad5b 100644
---
a/sql/core/src/test/resources/sql-tests/results/nonansi/datetime-special.sql.out
+++
b/sql/core/src/test/resources/sql-tests/results/nonansi/datetime-special.sql.out
@@ -29,3 +29,125 @@ select make_timestamp(-1969, 12, 31, 16, 0, 0.0),
make_timestamp(-15, 3, 18, 16,
struct<make_timestamp(-1969, 12, 31, 16, 0, 0.0):timestamp,make_timestamp(-15,
3, 18, 16, 0, 0.0):timestamp,make_timestamp(99999, 3, 18, 12, 3,
17.0):timestamp>
-- !query output
-1969-12-31 16:00:00 -0015-03-18 16:00:00 +99999-03-18 12:03:17
+
+
+-- !query
+select next_day(date'-5877641-06-23', 'MO'), next_day(date'-5877641-06-23',
'FR')
+-- !query schema
+struct<next_day(DATE '-5877641-06-23', MO):date,next_day(DATE
'-5877641-06-23', FR):date>
+-- !query output
+-5877641-06-29 -5877641-06-26
+
+
+-- !query
+select next_day(date'+5881580-07-04', 'FR')
+-- !query schema
+struct<next_day(DATE '+5881580-07-04', FR):date>
+-- !query output
++5881580-07-11
+
+
+-- !query
+select next_day(date'+5881580-07-11', 'MO')
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.SparkArithmeticException
+{
+ "errorClass" : "DATETIME_OVERFLOW",
+ "sqlState" : "22008",
+ "messageParameters" : {
+ "operation" : "find the next MONDAY after DATE '+5881580-07-11'"
+ }
+}
+
+
+-- !query
+select last_day(date'-5877641-06-23'), last_day(date'+5881580-06-30')
+-- !query schema
+struct<last_day(DATE '-5877641-06-23'):date,last_day(DATE
'+5881580-06-30'):date>
+-- !query output
+-5877641-06-30 +5881580-06-30
+
+
+-- !query
+select last_day(date'+5881580-07-11')
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.SparkArithmeticException
+{
+ "errorClass" : "DATETIME_OVERFLOW",
+ "sqlState" : "22008",
+ "messageParameters" : {
+ "operation" : "find the last day of the month of DATE '+5881580-07-11'"
+ }
+}
+
+
+-- !query
+select trunc(date'-5877641-06-29', 'WEEK'), trunc(date'-5877641-07-01',
'WEEK'), trunc(date'-5877641-07-01', 'MM'), trunc(date'-5877641-07-01',
'QUARTER'), trunc(date'-5877640-06-23', 'YEAR')
+-- !query schema
+struct<trunc(DATE '-5877641-06-29', WEEK):date,trunc(DATE '-5877641-07-01',
WEEK):date,trunc(DATE '-5877641-07-01', MM):date,trunc(DATE '-5877641-07-01',
QUARTER):date,trunc(DATE '-5877640-06-23', YEAR):date>
+-- !query output
+-5877641-06-29 -5877641-06-29 -5877641-07-01 -5877641-07-01 -5877640-01-01
+
+
+-- !query
+select trunc(date'-5877641-06-28', 'WEEK')
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.SparkArithmeticException
+{
+ "errorClass" : "DATETIME_OVERFLOW",
+ "sqlState" : "22008",
+ "messageParameters" : {
+ "operation" : "truncate DATE '-5877641-06-28' to WEEK"
+ }
+}
+
+
+-- !query
+select trunc(date'-5877641-06-30', 'MONTH')
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.SparkArithmeticException
+{
+ "errorClass" : "DATETIME_OVERFLOW",
+ "sqlState" : "22008",
+ "messageParameters" : {
+ "operation" : "truncate DATE '-5877641-06-30' to MONTH"
+ }
+}
+
+
+-- !query
+select trunc(date'-5877641-06-23', 'QUARTER')
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.SparkArithmeticException
+{
+ "errorClass" : "DATETIME_OVERFLOW",
+ "sqlState" : "22008",
+ "messageParameters" : {
+ "operation" : "truncate DATE '-5877641-06-23' to QUARTER"
+ }
+}
+
+
+-- !query
+select trunc(date'-5877641-12-31', 'YEAR')
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.SparkArithmeticException
+{
+ "errorClass" : "DATETIME_OVERFLOW",
+ "sqlState" : "22008",
+ "messageParameters" : {
+ "operation" : "truncate DATE '-5877641-12-31' to YEAR"
+ }
+}
diff --git
a/sql/core/src/test/resources/sql-tests/results/timestampNTZ/datetime-special-ansi.sql.out
b/sql/core/src/test/resources/sql-tests/results/timestampNTZ/datetime-special-ansi.sql.out
index c8cdec4eeb98..16b7ffb820f8 100644
---
a/sql/core/src/test/resources/sql-tests/results/timestampNTZ/datetime-special-ansi.sql.out
+++
b/sql/core/src/test/resources/sql-tests/results/timestampNTZ/datetime-special-ansi.sql.out
@@ -29,3 +29,125 @@ select make_timestamp(-1969, 12, 31, 16, 0, 0.0),
make_timestamp(-15, 3, 18, 16,
struct<make_timestamp(-1969, 12, 31, 16, 0,
0.0):timestamp_ntz,make_timestamp(-15, 3, 18, 16, 0,
0.0):timestamp_ntz,make_timestamp(99999, 3, 18, 12, 3, 17.0):timestamp_ntz>
-- !query output
-1969-12-31 16:00:00 -0015-03-18 16:00:00 +99999-03-18 12:03:17
+
+
+-- !query
+select next_day(date'-5877641-06-23', 'MO'), next_day(date'-5877641-06-23',
'FR')
+-- !query schema
+struct<next_day(DATE '-5877641-06-23', MO):date,next_day(DATE
'-5877641-06-23', FR):date>
+-- !query output
+-5877641-06-29 -5877641-06-26
+
+
+-- !query
+select next_day(date'+5881580-07-04', 'FR')
+-- !query schema
+struct<next_day(DATE '+5881580-07-04', FR):date>
+-- !query output
++5881580-07-11
+
+
+-- !query
+select next_day(date'+5881580-07-11', 'MO')
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.SparkArithmeticException
+{
+ "errorClass" : "DATETIME_OVERFLOW",
+ "sqlState" : "22008",
+ "messageParameters" : {
+ "operation" : "find the next MONDAY after DATE '+5881580-07-11'"
+ }
+}
+
+
+-- !query
+select last_day(date'-5877641-06-23'), last_day(date'+5881580-06-30')
+-- !query schema
+struct<last_day(DATE '-5877641-06-23'):date,last_day(DATE
'+5881580-06-30'):date>
+-- !query output
+-5877641-06-30 +5881580-06-30
+
+
+-- !query
+select last_day(date'+5881580-07-11')
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.SparkArithmeticException
+{
+ "errorClass" : "DATETIME_OVERFLOW",
+ "sqlState" : "22008",
+ "messageParameters" : {
+ "operation" : "find the last day of the month of DATE '+5881580-07-11'"
+ }
+}
+
+
+-- !query
+select trunc(date'-5877641-06-29', 'WEEK'), trunc(date'-5877641-07-01',
'WEEK'), trunc(date'-5877641-07-01', 'MM'), trunc(date'-5877641-07-01',
'QUARTER'), trunc(date'-5877640-06-23', 'YEAR')
+-- !query schema
+struct<trunc(DATE '-5877641-06-29', WEEK):date,trunc(DATE '-5877641-07-01',
WEEK):date,trunc(DATE '-5877641-07-01', MM):date,trunc(DATE '-5877641-07-01',
QUARTER):date,trunc(DATE '-5877640-06-23', YEAR):date>
+-- !query output
+-5877641-06-29 -5877641-06-29 -5877641-07-01 -5877641-07-01 -5877640-01-01
+
+
+-- !query
+select trunc(date'-5877641-06-28', 'WEEK')
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.SparkArithmeticException
+{
+ "errorClass" : "DATETIME_OVERFLOW",
+ "sqlState" : "22008",
+ "messageParameters" : {
+ "operation" : "truncate DATE '-5877641-06-28' to WEEK"
+ }
+}
+
+
+-- !query
+select trunc(date'-5877641-06-30', 'MONTH')
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.SparkArithmeticException
+{
+ "errorClass" : "DATETIME_OVERFLOW",
+ "sqlState" : "22008",
+ "messageParameters" : {
+ "operation" : "truncate DATE '-5877641-06-30' to MONTH"
+ }
+}
+
+
+-- !query
+select trunc(date'-5877641-06-23', 'QUARTER')
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.SparkArithmeticException
+{
+ "errorClass" : "DATETIME_OVERFLOW",
+ "sqlState" : "22008",
+ "messageParameters" : {
+ "operation" : "truncate DATE '-5877641-06-23' to QUARTER"
+ }
+}
+
+
+-- !query
+select trunc(date'-5877641-12-31', 'YEAR')
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.SparkArithmeticException
+{
+ "errorClass" : "DATETIME_OVERFLOW",
+ "sqlState" : "22008",
+ "messageParameters" : {
+ "operation" : "truncate DATE '-5877641-12-31' to YEAR"
+ }
+}
diff --git
a/sql/core/src/test/resources/sql-tests/results/timestampNTZ/datetime-special.sql.out
b/sql/core/src/test/resources/sql-tests/results/timestampNTZ/datetime-special.sql.out
index c8cdec4eeb98..16b7ffb820f8 100644
---
a/sql/core/src/test/resources/sql-tests/results/timestampNTZ/datetime-special.sql.out
+++
b/sql/core/src/test/resources/sql-tests/results/timestampNTZ/datetime-special.sql.out
@@ -29,3 +29,125 @@ select make_timestamp(-1969, 12, 31, 16, 0, 0.0),
make_timestamp(-15, 3, 18, 16,
struct<make_timestamp(-1969, 12, 31, 16, 0,
0.0):timestamp_ntz,make_timestamp(-15, 3, 18, 16, 0,
0.0):timestamp_ntz,make_timestamp(99999, 3, 18, 12, 3, 17.0):timestamp_ntz>
-- !query output
-1969-12-31 16:00:00 -0015-03-18 16:00:00 +99999-03-18 12:03:17
+
+
+-- !query
+select next_day(date'-5877641-06-23', 'MO'), next_day(date'-5877641-06-23',
'FR')
+-- !query schema
+struct<next_day(DATE '-5877641-06-23', MO):date,next_day(DATE
'-5877641-06-23', FR):date>
+-- !query output
+-5877641-06-29 -5877641-06-26
+
+
+-- !query
+select next_day(date'+5881580-07-04', 'FR')
+-- !query schema
+struct<next_day(DATE '+5881580-07-04', FR):date>
+-- !query output
++5881580-07-11
+
+
+-- !query
+select next_day(date'+5881580-07-11', 'MO')
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.SparkArithmeticException
+{
+ "errorClass" : "DATETIME_OVERFLOW",
+ "sqlState" : "22008",
+ "messageParameters" : {
+ "operation" : "find the next MONDAY after DATE '+5881580-07-11'"
+ }
+}
+
+
+-- !query
+select last_day(date'-5877641-06-23'), last_day(date'+5881580-06-30')
+-- !query schema
+struct<last_day(DATE '-5877641-06-23'):date,last_day(DATE
'+5881580-06-30'):date>
+-- !query output
+-5877641-06-30 +5881580-06-30
+
+
+-- !query
+select last_day(date'+5881580-07-11')
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.SparkArithmeticException
+{
+ "errorClass" : "DATETIME_OVERFLOW",
+ "sqlState" : "22008",
+ "messageParameters" : {
+ "operation" : "find the last day of the month of DATE '+5881580-07-11'"
+ }
+}
+
+
+-- !query
+select trunc(date'-5877641-06-29', 'WEEK'), trunc(date'-5877641-07-01',
'WEEK'), trunc(date'-5877641-07-01', 'MM'), trunc(date'-5877641-07-01',
'QUARTER'), trunc(date'-5877640-06-23', 'YEAR')
+-- !query schema
+struct<trunc(DATE '-5877641-06-29', WEEK):date,trunc(DATE '-5877641-07-01',
WEEK):date,trunc(DATE '-5877641-07-01', MM):date,trunc(DATE '-5877641-07-01',
QUARTER):date,trunc(DATE '-5877640-06-23', YEAR):date>
+-- !query output
+-5877641-06-29 -5877641-06-29 -5877641-07-01 -5877641-07-01 -5877640-01-01
+
+
+-- !query
+select trunc(date'-5877641-06-28', 'WEEK')
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.SparkArithmeticException
+{
+ "errorClass" : "DATETIME_OVERFLOW",
+ "sqlState" : "22008",
+ "messageParameters" : {
+ "operation" : "truncate DATE '-5877641-06-28' to WEEK"
+ }
+}
+
+
+-- !query
+select trunc(date'-5877641-06-30', 'MONTH')
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.SparkArithmeticException
+{
+ "errorClass" : "DATETIME_OVERFLOW",
+ "sqlState" : "22008",
+ "messageParameters" : {
+ "operation" : "truncate DATE '-5877641-06-30' to MONTH"
+ }
+}
+
+
+-- !query
+select trunc(date'-5877641-06-23', 'QUARTER')
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.SparkArithmeticException
+{
+ "errorClass" : "DATETIME_OVERFLOW",
+ "sqlState" : "22008",
+ "messageParameters" : {
+ "operation" : "truncate DATE '-5877641-06-23' to QUARTER"
+ }
+}
+
+
+-- !query
+select trunc(date'-5877641-12-31', 'YEAR')
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.SparkArithmeticException
+{
+ "errorClass" : "DATETIME_OVERFLOW",
+ "sqlState" : "22008",
+ "messageParameters" : {
+ "operation" : "truncate DATE '-5877641-12-31' to YEAR"
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]