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 @@ -406,6 +406,10 @@ object DateTimeUtils {
// year should have exact four digits
return None
}
if (i < 2 && j < bytes.length) {
// For the `yyyy` and `yyyy-[m]m` formats, entire input must be consumed.
return None
}
segments(i) = currentSegmentValue
try {
val localDate = LocalDate.of(segments(0), segments(1), segments(2))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ class DateTimeUtilsSuite extends SparkFunSuite {
assert(stringToDate(UTF8String.fromString("015-03-18")).isEmpty)
assert(stringToDate(UTF8String.fromString("015")).isEmpty)
assert(stringToDate(UTF8String.fromString("02015")).isEmpty)
assert(stringToDate(UTF8String.fromString("1999 08 01")).isEmpty)
assert(stringToDate(UTF8String.fromString("1999-08 01")).isEmpty)
assert(stringToDate(UTF8String.fromString("1999 08")).isEmpty)
}

test("string to timestamp") {
Expand Down Expand Up @@ -242,6 +245,9 @@ class DateTimeUtilsSuite extends SparkFunSuite {
checkStringToTimestamp("2015-03-18T12:03.17-20:0", None)
checkStringToTimestamp("2015-03-18T12:03.17-0:70", None)
checkStringToTimestamp("2015-03-18T12:03.17-1:0:0", None)
checkStringToTimestamp("1999 08 01", None)
checkStringToTimestamp("1999-08 01", None)
checkStringToTimestamp("1999 08", None)

// Truncating the fractional seconds
timeZone = TimeZone.getTimeZone("GMT+00:00")
Expand Down
120 changes: 96 additions & 24 deletions sql/core/src/test/resources/sql-tests/results/pgSQL/date.sql.out
Original file line number Diff line number Diff line change
Expand Up @@ -198,17 +198,29 @@ struct<DATE '1999-01-18':date>
-- !query 21
SELECT date '1999 Jan 08'
-- !query 21 schema
struct<DATE '1999-01-01':date>
struct<>
-- !query 21 output
1999-01-01
Copy link
Member Author

Choose a reason for hiding this comment

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

The result is wrong, it is better to throw the exception because the format with spaces (yyyy [m]m [d]d) is not supported currently.

Copy link
Member

Choose a reason for hiding this comment

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

Oops. I missed this. Thank you for the catching this.
cc @wangyum , too.

org.apache.spark.sql.catalyst.parser.ParseException

Cannot parse the DATE value: 1999 Jan 08(line 1, pos 7)

== SQL ==
SELECT date '1999 Jan 08'
Copy link
Member

Choose a reason for hiding this comment

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

Note that this is only for date prefix. With this PR, we will return NULL like Hive.

spark-sql> SELECT CAST('1999 08 01' AS DATE);
NULL

@MaxGekk . Could you mention these two cases together in the PR title?

Copy link
Member

Choose a reason for hiding this comment

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

I updated the PR description and JIRA.

-------^^^


-- !query 22
SELECT date '1999 08 Jan'
-- !query 22 schema
struct<DATE '1999-01-01':date>
struct<>
-- !query 22 output
1999-01-01
org.apache.spark.sql.catalyst.parser.ParseException

Cannot parse the DATE value: 1999 08 Jan(line 1, pos 7)

== SQL ==
SELECT date '1999 08 Jan'
-------^^^


-- !query 23
Expand All @@ -230,17 +242,29 @@ struct<DATE '1999-08-01':date>
-- !query 25
SELECT date '1999 01 08'
-- !query 25 schema
struct<DATE '1999-01-01':date>
struct<>
-- !query 25 output
1999-01-01
org.apache.spark.sql.catalyst.parser.ParseException

Cannot parse the DATE value: 1999 01 08(line 1, pos 7)

== SQL ==
SELECT date '1999 01 08'
-------^^^


-- !query 26
SELECT date '1999 08 01'
-- !query 26 schema
struct<DATE '1999-01-01':date>
struct<>
-- !query 26 output
1999-01-01
org.apache.spark.sql.catalyst.parser.ParseException

Cannot parse the DATE value: 1999 08 01(line 1, pos 7)

== SQL ==
SELECT date '1999 08 01'
-------^^^


-- !query 27
Expand All @@ -254,17 +278,29 @@ struct<DATE '1999-01-08':date>
-- !query 28
SELECT date '1999 Jan 08'
-- !query 28 schema
struct<DATE '1999-01-01':date>
struct<>
-- !query 28 output
1999-01-01
org.apache.spark.sql.catalyst.parser.ParseException

Cannot parse the DATE value: 1999 Jan 08(line 1, pos 7)

== SQL ==
SELECT date '1999 Jan 08'
-------^^^


-- !query 29
SELECT date '1999 08 Jan'
-- !query 29 schema
struct<DATE '1999-01-01':date>
struct<>
-- !query 29 output
1999-01-01
org.apache.spark.sql.catalyst.parser.ParseException

Cannot parse the DATE value: 1999 08 Jan(line 1, pos 7)

== SQL ==
SELECT date '1999 08 Jan'
-------^^^


-- !query 30
Expand All @@ -286,17 +322,29 @@ struct<DATE '1999-08-01':date>
-- !query 32
SELECT date '1999 01 08'
-- !query 32 schema
struct<DATE '1999-01-01':date>
struct<>
-- !query 32 output
1999-01-01
org.apache.spark.sql.catalyst.parser.ParseException

Cannot parse the DATE value: 1999 01 08(line 1, pos 7)

== SQL ==
SELECT date '1999 01 08'
-------^^^


-- !query 33
SELECT date '1999 08 01'
-- !query 33 schema
struct<DATE '1999-01-01':date>
struct<>
-- !query 33 output
1999-01-01
org.apache.spark.sql.catalyst.parser.ParseException

Cannot parse the DATE value: 1999 08 01(line 1, pos 7)

== SQL ==
SELECT date '1999 08 01'
-------^^^


-- !query 34
Expand All @@ -318,17 +366,29 @@ struct<DATE '1999-01-18':date>
-- !query 36
SELECT date '1999 Jan 08'
-- !query 36 schema
struct<DATE '1999-01-01':date>
struct<>
-- !query 36 output
1999-01-01
org.apache.spark.sql.catalyst.parser.ParseException

Cannot parse the DATE value: 1999 Jan 08(line 1, pos 7)

== SQL ==
SELECT date '1999 Jan 08'
-------^^^


-- !query 37
SELECT date '1999 08 Jan'
-- !query 37 schema
struct<DATE '1999-01-01':date>
struct<>
-- !query 37 output
1999-01-01
org.apache.spark.sql.catalyst.parser.ParseException

Cannot parse the DATE value: 1999 08 Jan(line 1, pos 7)

== SQL ==
SELECT date '1999 08 Jan'
-------^^^


-- !query 38
Expand All @@ -350,17 +410,29 @@ struct<DATE '1999-08-01':date>
-- !query 40
SELECT date '1999 01 08'
-- !query 40 schema
struct<DATE '1999-01-01':date>
struct<>
-- !query 40 output
1999-01-01
org.apache.spark.sql.catalyst.parser.ParseException

Cannot parse the DATE value: 1999 01 08(line 1, pos 7)

== SQL ==
SELECT date '1999 01 08'
-------^^^


-- !query 41
SELECT date '1999 08 01'
-- !query 41 schema
struct<DATE '1999-01-01':date>
struct<>
-- !query 41 output
1999-01-01
org.apache.spark.sql.catalyst.parser.ParseException

Cannot parse the DATE value: 1999 08 01(line 1, pos 7)

== SQL ==
SELECT date '1999 08 01'
-------^^^


-- !query 42
Expand Down