|
17 | 17 |
|
18 | 18 | package org.apache.arrow.adapter.jdbc.consumer; |
19 | 19 |
|
| 20 | +import java.sql.Date; |
20 | 21 | import java.sql.ResultSet; |
21 | 22 | import java.sql.SQLException; |
| 23 | +import java.text.ParseException; |
| 24 | +import java.text.SimpleDateFormat; |
22 | 25 | import java.util.Calendar; |
23 | | -import java.util.Date; |
| 26 | +import java.util.concurrent.TimeUnit; |
24 | 27 |
|
25 | 28 | import org.apache.arrow.vector.DateDayVector; |
26 | 29 | import org.apache.arrow.vector.DateMilliVector; |
|
31 | 34 | */ |
32 | 35 | public class DateConsumer { |
33 | 36 |
|
| 37 | + /** |
| 38 | + * The number of milli-seconds in a day. |
| 39 | + */ |
| 40 | + public static final long MILLIS_PER_DAY = TimeUnit.DAYS.toMillis(1); |
| 41 | + |
| 42 | + public static final int MAX_DAY; |
| 43 | + |
| 44 | + static { |
| 45 | + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); |
| 46 | + try { |
| 47 | + java.util.Date date = dateFormat.parse("9999-12-31"); |
| 48 | + MAX_DAY = (int) (date.getTime() / MILLIS_PER_DAY); |
| 49 | + } catch (ParseException e) { |
| 50 | + throw new IllegalArgumentException("Failed to parse max day", e); |
| 51 | + } |
| 52 | + } |
| 53 | + |
34 | 54 | /** |
35 | 55 | * Creates a consumer for {@link DateMilliVector}. |
36 | 56 | */ |
@@ -70,7 +90,11 @@ public void consume(ResultSet resultSet) throws SQLException { |
70 | 90 | Date date = calendar == null ? resultSet.getDate(columnIndexInResultSet) : |
71 | 91 | resultSet.getDate(columnIndexInResultSet, calendar); |
72 | 92 | if (!resultSet.wasNull()) { |
73 | | - vector.setSafe(currentIndex, (int) (date.getTime() / DateDayVector.MILLIS_PER_DAY)); |
| 93 | + int day = (int) (date.getTime() / MILLIS_PER_DAY); |
| 94 | + if (day < 0 || day > MAX_DAY) { |
| 95 | + throw new IllegalArgumentException("Day overflow: " + day); |
| 96 | + } |
| 97 | + vector.setSafe(currentIndex, day); |
74 | 98 | } |
75 | 99 | currentIndex++; |
76 | 100 | } |
@@ -102,7 +126,11 @@ public NonNullableDateConsumer(DateDayVector vector, int index, Calendar calenda |
102 | 126 | public void consume(ResultSet resultSet) throws SQLException { |
103 | 127 | Date date = calendar == null ? resultSet.getDate(columnIndexInResultSet) : |
104 | 128 | resultSet.getDate(columnIndexInResultSet, calendar); |
105 | | - vector.setSafe(currentIndex, (int) (date.getTime() / DateDayVector.MILLIS_PER_DAY)); |
| 129 | + int day = (int) (date.getTime() / MILLIS_PER_DAY); |
| 130 | + if (day < 0 || day > MAX_DAY) { |
| 131 | + throw new IllegalArgumentException("Day overflow: " + day); |
| 132 | + } |
| 133 | + vector.setSafe(currentIndex, day); |
106 | 134 | currentIndex++; |
107 | 135 | } |
108 | 136 | } |
|
0 commit comments