Skip to content

Commit 3b1667e

Browse files
committed
add uts
1 parent 0814fc4 commit 3b1667e

2 files changed

Lines changed: 15 additions & 5 deletions

File tree

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/IntervalUtils.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ object IntervalUtils {
447447
i += 1
448448
fractionScale = (NANOS_PER_SECOND / 10).toInt
449449
state = VALUE_FRACTIONAL_PART
450-
case _ => exceptionWithState( s"Unrecognized sign '$nextWord'")
450+
case _ => exceptionWithState( s"unrecognized sign '$nextWord'")
451451
}
452452
case TRIM_BEFORE_VALUE => trimToNextState(b, VALUE)
453453
case VALUE =>
@@ -456,8 +456,7 @@ object IntervalUtils {
456456
try {
457457
currentValue = Math.addExact(Math.multiplyExact(10, currentValue), (b - '0'))
458458
} catch {
459-
case _: ArithmeticException =>
460-
exceptionWithState(s"'$currentValue$nextWord' out of range")
459+
case e: ArithmeticException => exceptionWithState(e.getMessage, e)
461460
}
462461
case ' ' => state = TRIM_BEFORE_UNIT
463462
case '.' =>
@@ -474,7 +473,9 @@ object IntervalUtils {
474473
case ' ' =>
475474
fraction /= NANOS_PER_MICROS.toInt
476475
state = TRIM_BEFORE_UNIT
477-
case _ => exceptionWithState(s"invalid value fractional part '$fraction$nextWord'")
476+
case _ if '0' <= b && b <= '9' =>
477+
exceptionWithState(s"invalid value fractional part '$fraction$nextWord' out of range")
478+
case _ => exceptionWithState(s"invalid value fractional part '$nextWord'")
478479
}
479480
i += 1
480481
case TRIM_BEFORE_UNIT => trimToNextState(b, UNIT_BEGIN)

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/util/IntervalUtilsSuite.scala

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ class IntervalUtilsSuite extends SparkFunSuite {
9595
// Only the seconds units can have the fractional part
9696
checkFromInvalidString("1.5 days", "'days' with fractional part is unsupported")
9797
checkFromInvalidString("1. hour", "'hour' with fractional part is unsupported")
98+
checkFromInvalidString("1 hourX", "invalid unit suffix")
99+
checkFromInvalidString("~1 hour", "unrecognized sign")
100+
checkFromInvalidString("1 Mour", "invalid unit 'mour'")
101+
checkFromInvalidString("1 aour", "invalid unit 'aour'")
102+
checkFromInvalidString("1a1 hour", "invalid value 'a1'")
103+
checkFromInvalidString("1.1a1 seconds", "invalid value fractional part 'a1'")
104+
checkFromInvalidString("2234567890 days", "integer overflow")
105+
98106
}
99107

100108
test("string to interval: seconds with fractional part") {
@@ -106,7 +114,8 @@ class IntervalUtilsSuite extends SparkFunSuite {
106114
checkFromString("-1.5 seconds", new CalendarInterval(0, 0, -1500000))
107115
// truncate nanoseconds to microseconds
108116
checkFromString("0.999999999 seconds", new CalendarInterval(0, 0, 999999))
109-
checkFromInvalidString("0.123456789123 seconds", "invalid value fractional part '123456789123'")
117+
checkFromString(".999999999 seconds", new CalendarInterval(0, 0, 999999))
118+
checkFromInvalidString("0.123456789123 seconds", "'123456789123' out of range")
110119
}
111120

112121
test("from year-month string") {

0 commit comments

Comments
 (0)