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 c431bc794f3c..b9b83daccc25 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 @@ 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 @@ /** * 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 void consume(ResultSet resultSet) throws SQLException { 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 NonNullableDateConsumer(DateDayVector vector, int index, Calendar calenda 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 da33d9d47cd1..bca886ceb4e8 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'