From 1c55a9d81bef1b7dec01ec194815bc37235034a5 Mon Sep 17 00:00:00 2001 From: gengjiaan Date: Mon, 19 Apr 2021 18:34:38 +0800 Subject: [PATCH] Add round trip tests for period <-> month and duration <-> micros --- .../catalyst/util/IntervalUtilsSuite.scala | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/util/IntervalUtilsSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/util/IntervalUtilsSuite.scala index a04c4b002d00c..5c460f70a9ce4 100644 --- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/util/IntervalUtilsSuite.scala +++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/util/IntervalUtilsSuite.scala @@ -443,6 +443,51 @@ class IntervalUtilsSuite extends SparkFunSuite with SQLHelper { } } + test("SPARK-34715: Add round trip tests for period <-> month and duration <-> micros") { + // Months -> Period -> Months + Seq( + 0, + MONTHS_PER_YEAR - 1, + MONTHS_PER_YEAR + 1, + MONTHS_PER_YEAR, + -MONTHS_PER_YEAR, + Int.MaxValue - MONTHS_PER_YEAR, + Int.MinValue + MONTHS_PER_YEAR, + Int.MaxValue, + Int.MinValue).foreach { months => + val period = monthsToPeriod(months) + assert(periodToMonths(period) === months) + } + // Period -> Months -> Period + Seq( + monthsToPeriod(0), + monthsToPeriod(MONTHS_PER_YEAR - 1), + monthsToPeriod(MONTHS_PER_YEAR + 1), + monthsToPeriod(MONTHS_PER_YEAR), + monthsToPeriod(-MONTHS_PER_YEAR), + monthsToPeriod(Int.MaxValue - MONTHS_PER_YEAR), + monthsToPeriod(Int.MinValue + MONTHS_PER_YEAR), + monthsToPeriod(Int.MaxValue), + monthsToPeriod(Int.MinValue)).foreach { period => + val months = periodToMonths(period) + assert(monthsToPeriod(months) === period) + } + // Duration -> micros -> Duration + Seq( + microsToDuration(0), + microsToDuration(MICROS_PER_SECOND - 1), + microsToDuration(-MICROS_PER_SECOND + 1), + microsToDuration(MICROS_PER_SECOND), + microsToDuration(-MICROS_PER_SECOND), + microsToDuration(Long.MaxValue - MICROS_PER_SECOND), + microsToDuration(Long.MinValue + MICROS_PER_SECOND), + microsToDuration(Long.MaxValue), + microsToDuration(Long.MinValue)).foreach { duration => + val micros = durationToMicros(duration) + assert(microsToDuration(micros) === duration) + } + } + test("SPARK-35016: format year-month intervals") { Seq( 0 -> ("0-0", "INTERVAL '0-0' YEAR TO MONTH"),