|
17 | 17 |
|
18 | 18 | package org.apache.spark.sql.catalyst.util
|
19 | 19 |
|
20 |
| -import java.time.{DateTimeException, LocalDateTime} |
| 20 | +import java.time.{DateTimeException, LocalDateTime, ZoneId} |
| 21 | +import java.util.Locale |
21 | 22 |
|
22 |
| -import org.apache.spark.SparkUpgradeException |
| 23 | +import org.apache.spark.{SparkException, SparkUpgradeException} |
23 | 24 | import org.apache.spark.sql.catalyst.util.DateTimeTestUtils._
|
24 | 25 | import org.apache.spark.sql.catalyst.util.DateTimeUtils._
|
| 26 | +import org.apache.spark.sql.catalyst.util.LegacyDateFormats.LENIENT_SIMPLE_DATE_FORMAT |
25 | 27 | import org.apache.spark.sql.internal.{LegacyBehaviorPolicy, SQLConf}
|
26 | 28 | import org.apache.spark.unsafe.types.UTF8String
|
27 | 29 |
|
@@ -502,4 +504,23 @@ class TimestampFormatterSuite extends DatetimeFormatterSuite {
|
502 | 504 | assert(formatter.parseOptional("9999-12-31 23:59:59.999").isEmpty)
|
503 | 505 | assert(formatter.parseWithoutTimeZoneOptional("9999-12-31 23:59:59.999", true).isEmpty)
|
504 | 506 | }
|
| 507 | + |
| 508 | + test("fail to parse string as TimestampNTZ with invalid format") { |
| 509 | + val zoneId = ZoneId.systemDefault() |
| 510 | + val locale = Locale.getDefault() |
| 511 | + val formatter = new DefaultTimestampFormatter( |
| 512 | + zoneId, locale, LENIENT_SIMPLE_DATE_FORMAT, isParsing = true) |
| 513 | + |
| 514 | + val invalidTimestampStr = "2021-13-01T25:61:61" |
| 515 | + |
| 516 | + checkError( |
| 517 | + exception = intercept[SparkException] { |
| 518 | + formatter.parseWithoutTimeZone(invalidTimestampStr, allowTimeZone = false) |
| 519 | + }, |
| 520 | + errorClass = "INTERNAL_ERROR", |
| 521 | + parameters = Map( |
| 522 | + "message" -> ("Cannot parse field value '2021-13-01T25:61:61' for pattern " + |
| 523 | + "'yyyy-MM-dd HH:mm:ss' as the target spark data type \"TIMESTAMP_NTZ\".")) |
| 524 | + ) |
| 525 | + } |
505 | 526 | }
|
0 commit comments