Skip to content

Commit be73192

Browse files
committed
[ARROW-7301][Java] Resolve comments
1 parent eea8b79 commit be73192

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/JdbcToArrowUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public static Schema jdbcToArrowSchema(ResultSetMetaData rsmd, JdbcToArrowConfig
210210
* <li>BINARY --> ArrowType.Binary</li>
211211
* <li>VARBINARY --> ArrowType.Binary</li>
212212
* <li>LONGVARBINARY --> ArrowType.Binary</li>
213-
* <li>DATE --> ArrowType.Date(DateUnit.MILLISECOND)</li>
213+
* <li>DATE --> ArrowType.Date(DateUnit.DAY)</li>
214214
* <li>TIME --> ArrowType.Time(TimeUnit.MILLISECOND, 32)</li>
215215
* <li>TIMESTAMP --> ArrowType.Timestamp(TimeUnit.MILLISECOND, calendar timezone)</li>
216216
* <li>CLOB --> ArrowType.Utf8</li>

java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/DateConsumer.java

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@
1717

1818
package org.apache.arrow.adapter.jdbc.consumer;
1919

20+
import java.sql.Date;
2021
import java.sql.ResultSet;
2122
import java.sql.SQLException;
23+
import java.text.ParseException;
24+
import java.text.SimpleDateFormat;
2225
import java.util.Calendar;
23-
import java.util.Date;
26+
import java.util.concurrent.TimeUnit;
2427

2528
import org.apache.arrow.vector.DateDayVector;
2629
import org.apache.arrow.vector.DateMilliVector;
@@ -31,6 +34,23 @@
3134
*/
3235
public class DateConsumer {
3336

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+
3454
/**
3555
* Creates a consumer for {@link DateMilliVector}.
3656
*/
@@ -70,7 +90,11 @@ public void consume(ResultSet resultSet) throws SQLException {
7090
Date date = calendar == null ? resultSet.getDate(columnIndexInResultSet) :
7191
resultSet.getDate(columnIndexInResultSet, calendar);
7292
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);
7498
}
7599
currentIndex++;
76100
}
@@ -102,7 +126,11 @@ public NonNullableDateConsumer(DateDayVector vector, int index, Calendar calenda
102126
public void consume(ResultSet resultSet) throws SQLException {
103127
Date date = calendar == null ? resultSet.getDate(columnIndexInResultSet) :
104128
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);
106134
currentIndex++;
107135
}
108136
}

java/vector/src/main/java/org/apache/arrow/vector/DateDayVector.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,6 @@
3838
*/
3939
public final class DateDayVector extends BaseFixedWidthVector {
4040

41-
/**
42-
* The number of milli-seconds in a day.
43-
*/
44-
public static final long MILLIS_PER_DAY = 3600 * 24 * 1000L;
45-
4641
private static final byte TYPE_WIDTH = 4;
4742
private final FieldReader reader;
4843

0 commit comments

Comments
 (0)