This is an automated email from the ASF dual-hosted git repository.

zabetak pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hive.git


The following commit(s) were added to refs/heads/master by this push:
     new e87b5bb8f4b HIVE-28483: CAST string to date should return null when 
format is invalid (Zoltan Ratkai reviewed by Shohei Okumiya, Stamatis 
Zampetakis)
e87b5bb8f4b is described below

commit e87b5bb8f4bded30b17e46ee573151488c78d178
Author: zratkai <[email protected]>
AuthorDate: Thu Aug 29 12:43:17 2024 +0200

    HIVE-28483: CAST string to date should return null when format is invalid 
(Zoltan Ratkai reviewed by Shohei Okumiya, Stamatis Zampetakis)
    
    For instance '03-08-2023' is not in the expected format (i.e., YYYY-MM-DD) 
so from a user perspective it is better to return null rather than a seemingly 
wrong date.
    Tests were modified to denote the new expected behavior.
    
    Close apache/hive#5421
---
 .../org/apache/hadoop/hive/common/type/Date.java   | 17 +++++++++---
 .../apache/hive/common/util/TestDateParser.java    | 31 +++++++++-------------
 2 files changed, 26 insertions(+), 22 deletions(-)

diff --git a/common/src/java/org/apache/hadoop/hive/common/type/Date.java 
b/common/src/java/org/apache/hadoop/hive/common/type/Date.java
index 0f47ba513d4..024ab566087 100644
--- a/common/src/java/org/apache/hadoop/hive/common/type/Date.java
+++ b/common/src/java/org/apache/hadoop/hive/common/type/Date.java
@@ -84,7 +84,7 @@ public class Date implements Comparable<Date> {
   private static final LocalDate EPOCH = LocalDate.of(1970, 1, 1);
 
   private static final DateTimeFormatter PARSE_FORMATTER =
-      new DateTimeFormatterBuilder().appendValue(YEAR, 1, 10, 
SignStyle.NORMAL).appendLiteral('-')
+      new DateTimeFormatterBuilder().appendValue(YEAR, 1, 4, 
SignStyle.NORMAL).appendLiteral('-')
           .appendValue(MONTH_OF_YEAR, 1, 2, 
SignStyle.NORMAL).appendLiteral('-')
           .appendValue(DAY_OF_MONTH, 1, 2, 
SignStyle.NORMAL).toFormatter().withResolverStyle(ResolverStyle.STRICT);
 
@@ -177,13 +177,22 @@ public class Date implements Comparable<Date> {
    * @throws NullPointerException if {@code text} is null
    */
   public static Date valueOf(final String text) {
-    String s = Objects.requireNonNull(text).trim();
+    String trimedText = Objects.requireNonNull(text).trim();
     ParsePosition pos = new ParsePosition(0);
     try {
-      TemporalAccessor t = PARSE_FORMATTER.parseUnresolved(s, pos);
+      TemporalAccessor t = PARSE_FORMATTER.parse(trimedText, pos);
       if (pos.getErrorIndex() >= 0) {
-        throw new DateTimeParseException("Text could not be parsed to date", 
s, pos.getErrorIndex());
+        throw new DateTimeParseException("Text could not be parsed to date", 
trimedText, pos.getErrorIndex());
       }
+      //Check if there is still text left after parsing
+      if(pos.getIndex() < trimedText.length()) {
+        char lastChar = trimedText.charAt(pos.getIndex());
+        //Check if the first character of the remaining is a digit, e.g. 
"2023-08-0800" if yes, then it is a parse error and must throw an exception
+        if (lastChar >= '0' && lastChar <= '9'){
+          throw new DateTimeParseException("Text '" + trimedText + "' could 
not be parsed, unparsed text found at index " + pos.getIndex(), trimedText,
+              pos.getIndex());
+        }
+      }  
       return new Date(LocalDate.of(t.get(YEAR), t.get(MONTH_OF_YEAR), 
t.get(DAY_OF_MONTH)));
     } catch (DateTimeException e) {
       throw new IllegalArgumentException("Cannot create date, parsing error");
diff --git a/common/src/test/org/apache/hive/common/util/TestDateParser.java 
b/common/src/test/org/apache/hive/common/util/TestDateParser.java
index f492ad52ead..95271fe75ac 100644
--- a/common/src/test/org/apache/hive/common/util/TestDateParser.java
+++ b/common/src/test/org/apache/hive/common/util/TestDateParser.java
@@ -45,37 +45,29 @@ public class TestDateParser {
 
   @Test
   public void testValidCases() throws Exception {
-    checkValidCase("1945-12-31", Date.valueOf("1945-12-31"));
-    checkValidCase("1946-01-01", Date.valueOf("1946-01-01"));
-    checkValidCase("2001-11-12", Date.valueOf("2001-11-12"));
-    checkValidCase("0004-05-06", Date.valueOf("0004-05-06"));
-    checkValidCase("1678-09-10", Date.valueOf("1678-09-10"));
-    checkValidCase("9999-10-11", Date.valueOf("9999-10-11"));
+    checkValidCase("1945-12-31", Date.of(1945,12,31));
+    checkValidCase("1946-01-01", Date.of(1946,1,1));
+    checkValidCase("2001-11-12", Date.of(2001,11,12));
+    checkValidCase("0004-05-06", Date.of(4,5,6));
+    checkValidCase("1678-09-10", Date.of(1678,9,10));
+    checkValidCase("9999-10-11", Date.of(9999,10,11));
 
     // Timestamp strings should parse ok
-    checkValidCase("2001-11-12 01:02:03", Date.valueOf("2001-11-12"));
+    checkValidCase("2001-11-12 01:02:03", Date.of(2001,11,12));
 
     // Leading spaces
-    checkValidCase(" 1946-01-01", Date.valueOf("1946-01-01"));
-    checkValidCase(" 2001-11-12 01:02:03", Date.valueOf("2001-11-12"));
+    checkValidCase(" 1946-01-01", Date.of(1946,01,01));
+    checkValidCase(" 2001-11-12 01:02:03", Date.of(2001,11,12));
   }
 
   @Test
   public void testParseDateFromTimestampWithCommonTimeDelimiter() {
-    for (String d : new String[] { "T", " ", "-", ".", "_", "" }) {
+    for (String d : new String[] { "T", " ", "-", ".", "_" }) {
       String ts = "2023-08-03" + d + "01:02:03";
       assertEquals("Parsing " + ts, Date.of(2023, 8, 3), 
DateParser.parseDate(ts));
     }
   }
 
-  @Test
-  public void testParseDateFromValidDateLiteralWithTrailingDigits() {
-    assertEquals(Date.of(2023, 8, 8), DateParser.parseDate("2023-08-0800"));
-    // The result may seem unexpected but for many "08-08-20" is a valid date 
so there is no reason to reject
-    // "08-08-2023" and return null unless in the future Hive becomes stricter 
in terms of parsing dates.
-    assertEquals(Date.of(8, 8, 20), DateParser.parseDate("08-08-2023"));
-  }
-
   @Test
   public void testInvalidCases() throws Exception {
     checkInvalidCase("2001");
@@ -86,5 +78,8 @@ public class TestDateParser {
     checkInvalidCase("0000-00-00");
     checkInvalidCase("2001-13-12");
     checkInvalidCase("2001-11-31");
+    checkInvalidCase("19999-10-11");
+    checkInvalidCase("08-08-2023");
+    checkInvalidCase("2023-08-0800");
   }
 }

Reply via email to