Skip to content

Commit

Permalink
improve err msg
Browse files Browse the repository at this point in the history
  • Loading branch information
yaooqinn committed Nov 15, 2019
1 parent da7f9e8 commit 61626ff
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ object IntervalUtils {
import ParseState._
var state = PREFIX
def throwIAE(msg: String, e: Exception = null) = {
throw new IllegalArgumentException(s"Error parsing interval, $msg", e)
throw new IllegalArgumentException(s"Error parsing '$input' to interval, $msg", e)
}

if (input == null) {
Expand Down Expand Up @@ -409,8 +409,10 @@ object IntervalUtils {
}
}

def nextWord: UTF8String = {
s.substring(i, s.numBytes()).subStringIndex(UTF8String.blankString(1), 1)
def currentWord: String = {
val strings = s.toString.split("\\s+")
val lenLeft = s.substring(i, s.numBytes()).toString.split("\\s+").length
strings(strings.length - lenLeft)
}

while (i < bytes.length) {
Expand Down Expand Up @@ -449,10 +451,10 @@ object IntervalUtils {
isNegative = false
case '.' =>
isNegative = false
i += 1
fractionScale = (NANOS_PER_SECOND / 10).toInt
i += 1
state = VALUE_FRACTIONAL_PART
case _ => throwIAE( s"unrecognized sign '$nextWord'")
case _ => throwIAE( s"unrecognized number '$currentWord'")
}
case TRIM_BEFORE_VALUE => trimToNextState(b, VALUE)
case VALUE =>
Expand All @@ -467,7 +469,7 @@ object IntervalUtils {
case '.' =>
fractionScale = (NANOS_PER_SECOND / 10).toInt
state = VALUE_FRACTIONAL_PART
case _ => throwIAE(s"invalid value '$nextWord'")
case _ => throwIAE(s"invalid value '$currentWord'")
}
i += 1
case VALUE_FRACTIONAL_PART =>
Expand All @@ -479,15 +481,16 @@ object IntervalUtils {
fraction /= NANOS_PER_MICROS.toInt
state = TRIM_BEFORE_UNIT
case _ if '0' <= b && b <= '9' =>
throwIAE(s"invalid value fractional part '$fraction$nextWord' out of range")
case _ => throwIAE(s"invalid value '$nextWord' in fractional part")
throwIAE(s"interval can only support nanosecond precision, '$currentWord' is out" +
s" of range")
case _ => throwIAE(s"invalid value '$currentWord' in fractional part")
}
i += 1
case TRIM_BEFORE_UNIT => trimToNextState(b, UNIT_BEGIN)
case UNIT_BEGIN =>
// Checks that only seconds can have the fractional part
if (b != 's' && fractionScale >= 0) {
throwIAE(s"'$nextWord' with fractional part is unsupported")
throwIAE(s"'$currentWord' cannot have fractional part")
}
if (isNegative) {
currentValue = -currentValue
Expand Down Expand Up @@ -531,8 +534,8 @@ object IntervalUtils {
} else if (s.matchAt(microsStr, i)) {
microseconds = Math.addExact(microseconds, currentValue)
i += microsStr.numBytes()
} else throwIAE(s"invalid unit '$nextWord'")
case _ => throwIAE(s"invalid unit '$nextWord'")
} else throwIAE(s"invalid unit '$currentWord'")
case _ => throwIAE(s"invalid unit '$currentWord'")
}
} catch {
case e: ArithmeticException => throwIAE(e.getMessage, e)
Expand All @@ -542,15 +545,15 @@ object IntervalUtils {
b match {
case 's' => state = UNIT_END
case ' ' => state = TRIM_BEFORE_SIGN
case _ => throwIAE(s"invalid unit suffix '$nextWord'")
case _ => throwIAE(s"invalid unit '$currentWord'")
}
i += 1
case UNIT_END =>
b match {
case ' ' =>
i += 1
state = TRIM_BEFORE_SIGN
case _ => throwIAE(s"invalid unit suffix '$nextWord'")
case _ => throwIAE(s"invalid unit '$currentWord'")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class IntervalUtilsSuite extends SparkFunSuite {
checkFromInvalidString(null, "cannot be null")

for (input <- Seq("", " ", "interval", "interval1 day", "foo", "foo 1 day")) {
checkFromInvalidString(input, "Error parsing interval")
checkFromInvalidString(input, "Error parsing")
}
}

Expand All @@ -104,16 +104,15 @@ class IntervalUtilsSuite extends SparkFunSuite {
// Allow duplicated units and summarize their values
checkFromString("1 day 10 day", new CalendarInterval(0, 11, 0))
// Only the seconds units can have the fractional part
checkFromInvalidString("1.5 days", "'days' with fractional part is unsupported")
checkFromInvalidString("1. hour", "'hour' with fractional part is unsupported")
checkFromInvalidString("1 hourX", "invalid unit suffix")
checkFromInvalidString("~1 hour", "unrecognized sign")
checkFromInvalidString("1.5 days", "'days' cannot have fractional part")
checkFromInvalidString("1. hour", "'hour' cannot have fractional part")
checkFromInvalidString("1 hourX", "invalid unit 'hourx'")
checkFromInvalidString("~1 hour", "unrecognized number '~1'")
checkFromInvalidString("1 Mour", "invalid unit 'mour'")
checkFromInvalidString("1 aour", "invalid unit 'aour'")
checkFromInvalidString("1a1 hour", "invalid value 'a1'")
checkFromInvalidString("1.1a1 seconds", "invalid value 'a1' in fractional part")
checkFromInvalidString("1a1 hour", "invalid value '1a1'")
checkFromInvalidString("1.1a1 seconds", "invalid value '1.1a1' in fractional part")
checkFromInvalidString("2234567890 days", "integer overflow")

}

test("string to interval: seconds with fractional part") {
Expand All @@ -126,7 +125,7 @@ class IntervalUtilsSuite extends SparkFunSuite {
// truncate nanoseconds to microseconds
checkFromString("0.999999999 seconds", new CalendarInterval(0, 0, 999999))
checkFromString(".999999999 seconds", new CalendarInterval(0, 0, 999999))
checkFromInvalidString("0.123456789123 seconds", "'123456789123' out of range")
checkFromInvalidString("0.123456789123 seconds", "'0.123456789123' is out of range")
}

test("from year-month string") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ class DDLParserSuite extends AnalysisTest with SharedSparkSession {
assertError("select interval '23:61:15' hour to second",
"minute 61 outside range [0, 59]")
assertError("select interval '.1111111111' second",
"invalid value '1111111111' in fractional part")
"'.1111111111' is out of range")
}

test("use native json_tuple instead of hive's UDTF in LATERAL VIEW") {
Expand Down

0 comments on commit 61626ff

Please sign in to comment.