Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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}.
*/
Expand Down Expand Up @@ -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++;
}
Expand Down Expand Up @@ -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++;
}
}
Expand Down
2 changes: 2 additions & 0 deletions java/adapter/jdbc/src/test/resources/h2/test1_date_h2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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;'

Expand All @@ -44,3 +45,4 @@ values:
- '17574'
- '17574'
- '17574'
- '-365'