From 7f3403583fd2e18f84519e67f18d5619cd091bad Mon Sep 17 00:00:00 2001 From: gengjiaan Date: Mon, 19 Apr 2021 16:13:13 +0300 Subject: [PATCH] [SPARK-34715][SQL][TESTS] Add round trip tests for period <-> month and duration <-> micros ### What changes were proposed in this pull request? Similarly to the test from the PR https://github.com/apache/spark/pull/31799, add tests: 1. Months -> Period -> Months 2. Period -> Months -> Period 3. Duration -> micros -> Duration ### Why are the changes needed? Add round trip tests for period <-> month and duration <-> micros ### Does this PR introduce _any_ user-facing change? 'No'. Just test cases. ### How was this patch tested? Jenkins test Closes #32234 from beliefer/SPARK-34715. Authored-by: gengjiaan Signed-off-by: Max Gekk --- .../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"),