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

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


The following commit(s) were added to refs/heads/master by this push:
     new e620cbd  ARROW-9973: [Java] JDBC DateConsumer does not allow dates 
before epoch
e620cbd is described below

commit e620cbdccceec3775dc73015fab6aea836f0818c
Author: Patrick Woody <[email protected]>
AuthorDate: Tue Sep 15 15:01:30 2020 +0800

    ARROW-9973: [Java] JDBC DateConsumer does not allow dates before epoch
    
    Reported here: https://issues.apache.org/jira/browse/ARROW-9973
    
    Looks like a pretty straightforward fix. Let me know if you have any 
comments - thanks!
    
    Closes #8167 from pwoody/pw/ARROW-9973
    
    Authored-by: Patrick Woody <[email protected]>
    Signed-off-by: liyafan82 <[email protected]>
---
 .../arrow/adapter/jdbc/consumer/DateConsumer.java  | 28 +++-------------------
 .../jdbc/src/test/resources/h2/test1_date_h2.yml   |  2 ++
 2 files changed, 5 insertions(+), 25 deletions(-)

diff --git 
a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/DateConsumer.java
 
b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/DateConsumer.java
index c431bc7..b9b83da 100644
--- 
a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/DateConsumer.java
+++ 
b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/DateConsumer.java
@@ -20,8 +20,6 @@ package org.apache.arrow.adapter.jdbc.consumer;
 import java.sql.Date;
 import java.sql.ResultSet;
 import java.sql.SQLException;
-import java.text.ParseException;
-import java.text.SimpleDateFormat;
 import java.util.Calendar;
 import java.util.concurrent.TimeUnit;
 
@@ -30,22 +28,10 @@ import org.apache.arrow.vector.DateMilliVector;
 
 /**
  * Consumer which consume date type values from {@link ResultSet}.
- * Write the data to {@link org.apache.arrow.vector.DateMilliVector}.
+ * Write the data to {@link org.apache.arrow.vector.DateDayVector}.
  */
 public class DateConsumer {
 
-  public static final int MAX_DAY;
-
-  static {
-    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
-    try {
-      java.util.Date date = dateFormat.parse("9999-12-31");
-      MAX_DAY = (int) TimeUnit.MILLISECONDS.toDays(date.getTime());
-    } catch (ParseException e) {
-      throw new IllegalArgumentException("Failed to parse max day", e);
-    }
-  }
-
   /**
    * Creates a consumer for {@link DateMilliVector}.
    */
@@ -85,13 +71,9 @@ public class DateConsumer {
       Date date = calendar == null ? resultSet.getDate(columnIndexInResultSet) 
:
           resultSet.getDate(columnIndexInResultSet, calendar);
       if (!resultSet.wasNull()) {
-        int day = (int) TimeUnit.MILLISECONDS.toDays(date.getTime());
-        if (day < 0 || day > MAX_DAY) {
-          throw new IllegalArgumentException("Day overflow: " + day);
-        }
         // for fixed width vectors, we have allocated enough memory 
proactively,
         // so there is no need to call the setSafe method here.
-        vector.set(currentIndex, day);
+        vector.set(currentIndex, 
Math.toIntExact(TimeUnit.MILLISECONDS.toDays(date.getTime())));
       }
       currentIndex++;
     }
@@ -123,13 +105,9 @@ public class DateConsumer {
     public void consume(ResultSet resultSet) throws SQLException {
       Date date = calendar == null ? resultSet.getDate(columnIndexInResultSet) 
:
           resultSet.getDate(columnIndexInResultSet, calendar);
-      int day = (int) TimeUnit.MILLISECONDS.toDays(date.getTime());
-      if (day < 0 || day > MAX_DAY) {
-        throw new IllegalArgumentException("Day overflow: " + day);
-      }
       // for fixed width vectors, we have allocated enough memory proactively,
       // so there is no need to call the setSafe method here.
-      vector.set(currentIndex, day);
+      vector.set(currentIndex, 
Math.toIntExact(TimeUnit.MILLISECONDS.toDays(date.getTime())));
       currentIndex++;
     }
   }
diff --git a/java/adapter/jdbc/src/test/resources/h2/test1_date_h2.yml 
b/java/adapter/jdbc/src/test/resources/h2/test1_date_h2.yml
index da33d9d..bca886c 100644
--- a/java/adapter/jdbc/src/test/resources/h2/test1_date_h2.yml
+++ b/java/adapter/jdbc/src/test/resources/h2/test1_date_h2.yml
@@ -28,6 +28,7 @@ data:
   - 'INSERT INTO table1 VALUES (''2018-02-12'');'
   - 'INSERT INTO table1 VALUES (''2018-02-12'');'
   - 'INSERT INTO table1 VALUES (''2018-02-12'');'
+  - 'INSERT INTO table1 VALUES (''1969-01-01'');'
 
 query: 'select date_field10 from table1;'
 
@@ -44,3 +45,4 @@ values:
  - '17574'
  - '17574'
  - '17574'
+ - '-365'

Reply via email to