Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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 @@ -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
Expand Down Expand Up @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand All @@ -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") {
Expand Down
32 changes: 16 additions & 16 deletions sql/core/src/test/resources/sql-tests/results/extract.sql.out
Original file line number Diff line number Diff line change
Expand Up @@ -135,23 +135,23 @@ select extract(day from c), extract(day from i) from t
-- !query schema
struct<extract('day' FROM t.`c`):int,extract('day' FROM t.`i`):int>
-- !query output
6 30
6 31


-- !query
select extract(d from c), extract(d from i) from t
-- !query schema
struct<extract('d' FROM t.`c`):int,extract('d' FROM t.`i`):int>
-- !query output
6 30
6 31


-- !query
select extract(days from c), extract(days from i) from t
-- !query schema
struct<extract('days' FROM t.`c`):int,extract('days' FROM t.`i`):int>
-- !query output
6 30
6 31


-- !query
Expand Down Expand Up @@ -199,39 +199,39 @@ select extract(hour from c), extract(hour from i) from t
-- !query schema
struct<extract('hour' FROM t.`c`):int,extract('hour' FROM t.`i`):bigint>
-- !query output
7 40
7 16


-- !query
select extract(h from c), extract(h from i) from t
-- !query schema
struct<extract('h' FROM t.`c`):int,extract('h' FROM t.`i`):bigint>
-- !query output
7 40
7 16


-- !query
select extract(hours from c), extract(hours from i) from t
-- !query schema
struct<extract('hours' FROM t.`c`):int,extract('hours' FROM t.`i`):bigint>
-- !query output
7 40
7 16


-- !query
select extract(hr from c), extract(hr from i) from t
-- !query schema
struct<extract('hr' FROM t.`c`):int,extract('hr' FROM t.`i`):bigint>
-- !query output
7 40
7 16


-- !query
select extract(hrs from c), extract(hrs from i) from t
-- !query schema
struct<extract('hrs' FROM t.`c`):int,extract('hrs' FROM t.`i`):bigint>
-- !query output
7 40
7 16


-- !query
Expand Down Expand Up @@ -457,23 +457,23 @@ select date_part('day', c), date_part('day', i) from t
-- !query schema
struct<date_part('day', t.`c`):int,date_part('day', t.`i`):int>
-- !query output
6 30
6 31


-- !query
select date_part('d', c), date_part('d', i) from t
-- !query schema
struct<date_part('d', t.`c`):int,date_part('d', t.`i`):int>
-- !query output
6 30
6 31


-- !query
select date_part('days', c), date_part('days', i) from t
-- !query schema
struct<date_part('days', t.`c`):int,date_part('days', t.`i`):int>
-- !query output
6 30
6 31


-- !query
Expand Down Expand Up @@ -521,39 +521,39 @@ select date_part('hour', c), date_part('hour', i) from t
-- !query schema
struct<date_part('hour', t.`c`):int,date_part('hour', t.`i`):bigint>
-- !query output
7 40
7 16


-- !query
select date_part('h', c), date_part('h', i) from t
-- !query schema
struct<date_part('h', t.`c`):int,date_part('h', t.`i`):bigint>
-- !query output
7 40
7 16


-- !query
select date_part('hours', c), date_part('hours', i) from t
-- !query schema
struct<date_part('hours', t.`c`):int,date_part('hours', t.`i`):bigint>
-- !query output
7 40
7 16


-- !query
select date_part('hr', c), date_part('hr', i) from t
-- !query schema
struct<date_part('hr', t.`c`):int,date_part('hr', t.`i`):bigint>
-- !query output
7 40
7 16


-- !query
select date_part('hrs', c), date_part('hrs', i) from t
-- !query schema
struct<date_part('hrs', t.`c`):int,date_part('hrs', t.`i`):bigint>
-- !query output
7 40
7 16


-- !query
Expand Down