diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/IntervalUtils.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/IntervalUtils.scala index c3f0e44b8435..7e5a71e0cb3f 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/IntervalUtils.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/IntervalUtils.scala @@ -17,7 +17,6 @@ package org.apache.spark.sql.catalyst.util -import java.math.BigDecimal import java.util.concurrent.TimeUnit import scala.util.control.NonFatal @@ -55,11 +54,12 @@ object IntervalUtils { } def getDays(interval: CalendarInterval): Int = { - interval.days + val daysInMicroseconds = (interval.microseconds / MICROS_PER_DAY).toInt + Math.addExact(interval.days, daysInMicroseconds) } def getHours(interval: CalendarInterval): Long = { - interval.microseconds / MICROS_PER_HOUR + (interval.microseconds % MICROS_PER_DAY) / MICROS_PER_HOUR } def getMinutes(interval: CalendarInterval): Byte = { diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/IntervalExpressionsSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/IntervalExpressionsSuite.scala index a2ee1c6ef279..8c972a991704 100644 --- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/IntervalExpressionsSuite.scala +++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/IntervalExpressionsSuite.scala @@ -68,6 +68,7 @@ class IntervalExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper { // Years and months must not be taken into account checkEvaluation(ExtractIntervalDays("100 year 10 months 5 days"), 5) checkEvaluation(ExtractIntervalDays(largeInterval), 31) + checkEvaluation(ExtractIntervalDays("25 hours"), 1) } test("hours") { @@ -81,6 +82,8 @@ class IntervalExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper { // Minutes should be taken into account checkEvaluation(ExtractIntervalHours("10 hours 100 minutes"), 11L) checkEvaluation(ExtractIntervalHours(largeInterval), 11L) + checkEvaluation(ExtractIntervalHours("25 hours"), 1L) + } test("minutes") { diff --git a/sql/core/src/test/resources/sql-tests/results/extract.sql.out b/sql/core/src/test/resources/sql-tests/results/extract.sql.out index aea09e4b90f4..29cbefdb3854 100644 --- a/sql/core/src/test/resources/sql-tests/results/extract.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/extract.sql.out @@ -135,7 +135,7 @@ select extract(day from c), extract(day from i) from t -- !query schema struct -- !query output -6 30 +6 31 -- !query @@ -143,7 +143,7 @@ select extract(d from c), extract(d from i) from t -- !query schema struct -- !query output -6 30 +6 31 -- !query @@ -151,7 +151,7 @@ select extract(days from c), extract(days from i) from t -- !query schema struct -- !query output -6 30 +6 31 -- !query @@ -199,7 +199,7 @@ select extract(hour from c), extract(hour from i) from t -- !query schema struct -- !query output -7 40 +7 16 -- !query @@ -207,7 +207,7 @@ select extract(h from c), extract(h from i) from t -- !query schema struct -- !query output -7 40 +7 16 -- !query @@ -215,7 +215,7 @@ select extract(hours from c), extract(hours from i) from t -- !query schema struct -- !query output -7 40 +7 16 -- !query @@ -223,7 +223,7 @@ select extract(hr from c), extract(hr from i) from t -- !query schema struct -- !query output -7 40 +7 16 -- !query @@ -231,7 +231,7 @@ select extract(hrs from c), extract(hrs from i) from t -- !query schema struct -- !query output -7 40 +7 16 -- !query @@ -457,7 +457,7 @@ select date_part('day', c), date_part('day', i) from t -- !query schema struct -- !query output -6 30 +6 31 -- !query @@ -465,7 +465,7 @@ select date_part('d', c), date_part('d', i) from t -- !query schema struct -- !query output -6 30 +6 31 -- !query @@ -473,7 +473,7 @@ select date_part('days', c), date_part('days', i) from t -- !query schema struct -- !query output -6 30 +6 31 -- !query @@ -521,7 +521,7 @@ select date_part('hour', c), date_part('hour', i) from t -- !query schema struct -- !query output -7 40 +7 16 -- !query @@ -529,7 +529,7 @@ select date_part('h', c), date_part('h', i) from t -- !query schema struct -- !query output -7 40 +7 16 -- !query @@ -537,7 +537,7 @@ select date_part('hours', c), date_part('hours', i) from t -- !query schema struct -- !query output -7 40 +7 16 -- !query @@ -545,7 +545,7 @@ select date_part('hr', c), date_part('hr', i) from t -- !query schema struct -- !query output -7 40 +7 16 -- !query @@ -553,7 +553,7 @@ select date_part('hrs', c), date_part('hrs', i) from t -- !query schema struct -- !query output -7 40 +7 16 -- !query