Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -505,6 +505,7 @@ object IntervalUtils {
var days: Int = 0
var microseconds: Long = 0
var fractionScale: Int = 0
val validOriginFractionScale = (NANOS_PER_SECOND / 10).toInt

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about just initialFractionScale?

var fraction: Int = 0

def trimToNextState(b: Byte, next: ParseState): Unit = {
Expand Down Expand Up @@ -556,7 +557,7 @@ object IntervalUtils {
isNegative = false
case '.' =>
isNegative = false
fractionScale = (NANOS_PER_SECOND / 10).toInt
fractionScale = validOriginFractionScale
i += 1
state = VALUE_FRACTIONAL_PART
case _ => throwIAE( s"unrecognized number '$currentWord'")
Expand All @@ -572,7 +573,7 @@ object IntervalUtils {
}
case ' ' => state = TRIM_BEFORE_UNIT
case '.' =>
fractionScale = (NANOS_PER_SECOND / 10).toInt
fractionScale = validOriginFractionScale
state = VALUE_FRACTIONAL_PART
case _ => throwIAE(s"invalid value '$currentWord'")
}
Expand All @@ -582,7 +583,7 @@ object IntervalUtils {
case _ if '0' <= b && b <= '9' && fractionScale > 0 =>
fraction += (b - '0') * fractionScale
fractionScale /= 10
case ' ' =>
case ' ' if fractionScale != validOriginFractionScale =>
fraction /= NANOS_PER_MICROS.toInt
state = TRIM_BEFORE_UNIT
case _ if '0' <= b && b <= '9' =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class IntervalUtilsSuite extends SparkFunSuite {
checkFromString("1 day 10 day", new CalendarInterval(0, 11, 0))
// Only the seconds units can have the fractional part
checkFromInvalidString("1.5 days", "'days' cannot have fractional part")
checkFromInvalidString("1. hour", "'hour' cannot have fractional part")
checkFromInvalidString("1. hour", "invalid value '1.'")
checkFromInvalidString("1 hourX", "invalid unit 'hourx'")
checkFromInvalidString("~1 hour", "unrecognized number '~1'")
checkFromInvalidString("1 Mour", "invalid unit 'mour'")
Expand All @@ -115,12 +115,12 @@ class IntervalUtilsSuite extends SparkFunSuite {
checkFromInvalidString("2234567890 days", "integer overflow")
checkFromInvalidString("\n", "Error parsing '\n' to interval")
checkFromInvalidString("\t", "Error parsing '\t' to interval")

checkFromInvalidString("1. seconds", "invalid value '1.'")
checkFromInvalidString(". seconds", "invalid value '.'")
}

test("string to interval: seconds with fractional part") {
checkFromString("0.1 seconds", new CalendarInterval(0, 0, 100000))
checkFromString("1. seconds", new CalendarInterval(0, 0, 1000000))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will we explicitly disable this case?

scala> sql("select 0.")
res0: org.apache.spark.sql.DataFrame = [0: decimal(1,0)]

At least I know Python, Java, Scala and R support this way as well.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

presto> select interval '1.' second;
     _col0
----------------
 0 00:00:01.000
(1 row)

Query 20191119_051438_00001_f5kcs, FINISHED, 1 node
Splits: 17 total, 17 done (100.00%)
0:03 [0 rows, 0B] [0 rows/s, 0B/s]

presto> select interval '.' second;
Query 20191119_051452_00002_f5kcs failed: Invalid INTERVAL SECOND value: .

Also check with presto, '1.' seconds is valid

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HyukjinKwon good point. To be consistent with Spark's own parser, maybe we should allow interval '1.' second. But interval '.' second should be invalid.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^ +1!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated, please recheck, thanks.

checkFromString("123.001 seconds", new CalendarInterval(0, 0, 123001000))
checkFromString("1.001001 seconds", new CalendarInterval(0, 0, 1001001))
checkFromString("1 minute 1.001001 seconds", new CalendarInterval(0, 0, 61001001))
Expand Down