Skip to content

Commit

Permalink
[ARROW-7301][Java] Resolve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
liyafan82 committed Jan 13, 2020
1 parent eea8b79 commit be73192
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public static Schema jdbcToArrowSchema(ResultSetMetaData rsmd, JdbcToArrowConfig
* <li>BINARY --> ArrowType.Binary</li>
* <li>VARBINARY --> ArrowType.Binary</li>
* <li>LONGVARBINARY --> ArrowType.Binary</li>
* <li>DATE --> ArrowType.Date(DateUnit.MILLISECOND)</li>
* <li>DATE --> ArrowType.Date(DateUnit.DAY)</li>
* <li>TIME --> ArrowType.Time(TimeUnit.MILLISECOND, 32)</li>
* <li>TIMESTAMP --> ArrowType.Timestamp(TimeUnit.MILLISECOND, calendar timezone)</li>
* <li>CLOB --> ArrowType.Utf8</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@

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.Date;
import java.util.concurrent.TimeUnit;

import org.apache.arrow.vector.DateDayVector;
import org.apache.arrow.vector.DateMilliVector;
Expand All @@ -31,6 +34,23 @@
*/
public class DateConsumer {

/**
* The number of milli-seconds in a day.
*/
public static final long MILLIS_PER_DAY = TimeUnit.DAYS.toMillis(1);

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) (date.getTime() / MILLIS_PER_DAY);
} catch (ParseException e) {
throw new IllegalArgumentException("Failed to parse max day", e);
}
}

/**
* Creates a consumer for {@link DateMilliVector}.
*/
Expand Down Expand Up @@ -70,7 +90,11 @@ public void consume(ResultSet resultSet) throws SQLException {
Date date = calendar == null ? resultSet.getDate(columnIndexInResultSet) :
resultSet.getDate(columnIndexInResultSet, calendar);
if (!resultSet.wasNull()) {
vector.setSafe(currentIndex, (int) (date.getTime() / DateDayVector.MILLIS_PER_DAY));
int day = (int) (date.getTime() / MILLIS_PER_DAY);
if (day < 0 || day > MAX_DAY) {
throw new IllegalArgumentException("Day overflow: " + day);
}
vector.setSafe(currentIndex, day);
}
currentIndex++;
}
Expand Down Expand Up @@ -102,7 +126,11 @@ 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);
vector.setSafe(currentIndex, (int) (date.getTime() / DateDayVector.MILLIS_PER_DAY));
int day = (int) (date.getTime() / MILLIS_PER_DAY);
if (day < 0 || day > MAX_DAY) {
throw new IllegalArgumentException("Day overflow: " + day);
}
vector.setSafe(currentIndex, day);
currentIndex++;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@
*/
public final class DateDayVector extends BaseFixedWidthVector {

/**
* The number of milli-seconds in a day.
*/
public static final long MILLIS_PER_DAY = 3600 * 24 * 1000L;

private static final byte TYPE_WIDTH = 4;
private final FieldReader reader;

Expand Down

0 comments on commit be73192

Please sign in to comment.