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 @@ -254,7 +254,9 @@ object DateTimeUtils {
val maxDigitsYear = 6
// For the nanosecond part, more than 6 digits is allowed, but will be truncated.
segment == 6 || (segment == 0 && digits >= 4 && digits <= maxDigitsYear) ||
(segment != 0 && segment != 6 && digits <= 2)
// For the zoneId segment(7), it's could be zero digits when it's a region-based zone ID
(segment == 7 && digits <= 2) ||
(segment != 0 && segment != 6 && segment != 7 && digits > 0 && digits <= 2)
}
if (s == null || s.trimAll().numBytes() == 0) {
return (Array.empty, None, false)
Expand Down Expand Up @@ -527,7 +529,8 @@ object DateTimeUtils {
def isValidDigits(segment: Int, digits: Int): Boolean = {
// An integer is able to represent a date within [+-]5 million years.
var maxDigitsYear = 7
(segment == 0 && digits >= 4 && digits <= maxDigitsYear) || (segment != 0 && digits <= 2)
(segment == 0 && digits >= 4 && digits <= maxDigitsYear) ||
(segment != 0 && digits > 0 && digits <= 2)
}
if (s == null || s.trimAll().numBytes() == 0) {
return None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -576,4 +576,8 @@ class CastSuite extends CastSuiteBase {
checkEvaluation(cast(invalidInput, TimestampNTZType), null)
}
}

test("SPARK-36286: invalid string cast to timestamp") {
checkEvaluation(cast(Literal("2015-03-18T"), TimestampType), null)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ abstract class CastSuiteBase extends SparkFunSuite with ExpressionEvalHelper {
c.set(Calendar.MILLISECOND, 0)
checkCastStringToTimestamp("2015-03-18", new Timestamp(c.getTimeInMillis))
checkCastStringToTimestamp("2015-03-18 ", new Timestamp(c.getTimeInMillis))
checkCastStringToTimestamp("2015-03-18T", new Timestamp(c.getTimeInMillis))

c = Calendar.getInstance(tz)
c.set(2015, 2, 18, 12, 3, 17)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ class DateTimeUtilsSuite extends SparkFunSuite with Matchers with SQLHelper {
assert(toDate("1999 08 01").isEmpty)
assert(toDate("1999-08 01").isEmpty)
assert(toDate("1999 08").isEmpty)
assert(toDate("1999-08-").isEmpty)
assert(toDate("").isEmpty)
assert(toDate(" ").isEmpty)
}
Expand Down Expand Up @@ -182,7 +183,7 @@ class DateTimeUtilsSuite extends SparkFunSuite with Matchers with SQLHelper {
checkStringToTimestamp("1969-12-31 16:00:00", Option(date(1969, 12, 31, 16, zid = zid)))
checkStringToTimestamp("0001", Option(date(1, 1, 1, 0, zid = zid)))
checkStringToTimestamp("2015-03", Option(date(2015, 3, 1, zid = zid)))
Seq("2015-03-18", "2015-03-18 ", " 2015-03-18", " 2015-03-18 ", "2015-03-18T").foreach { s =>
Seq("2015-03-18", "2015-03-18 ", " 2015-03-18", " 2015-03-18 ").foreach { s =>
checkStringToTimestamp(s, Option(date(2015, 3, 18, zid = zid)))
}

Expand Down Expand Up @@ -289,6 +290,11 @@ class DateTimeUtilsSuite extends SparkFunSuite with Matchers with SQLHelper {
checkStringToTimestamp("", None)
checkStringToTimestamp(" ", None)
checkStringToTimestamp("+", None)
checkStringToTimestamp("T", None)
checkStringToTimestamp("2015-03-18T", None)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe we can do some special handling to allow this.

Copy link
Member

Choose a reason for hiding this comment

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

Is this valid? doesn't quite look like it

Copy link
Contributor Author

@linhongliu-db linhongliu-db Jul 26, 2021

Choose a reason for hiding this comment

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

@srowen, thanks for reviewing. Personally I think it's invalid and this usage is blocked in my PR. But since previously it's allowed in the test case, so maybe someone will argue we should still allow it

Copy link
Member

Choose a reason for hiding this comment

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

Was it previously allowed? this test is new

Copy link
Contributor

Choose a reason for hiding this comment

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

checkStringToTimestamp("12::", None)
checkStringToTimestamp("2015-03-18T12:03:17-8:", None)
checkStringToTimestamp("2015-03-18T12:03:17-8:30:", None)

// Truncating the fractional seconds
expected = Option(date(2015, 3, 18, 12, 3, 17, 123456, zid = UTC))
Expand Down