-
Notifications
You must be signed in to change notification settings - Fork 4k
ARROW-7301: [Java] Sql type DATE should correspond to DateDayVector #5944
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,11 +17,15 @@ | |
|
|
||
| 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; | ||
|
|
||
| /** | ||
|
|
@@ -30,11 +34,23 @@ | |
| */ | ||
| 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}. | ||
| */ | ||
| public static JdbcConsumer<DateMilliVector> createConsumer( | ||
| DateMilliVector vector, int index, boolean nullable, Calendar calendar) { | ||
| public static JdbcConsumer<DateDayVector> createConsumer( | ||
| DateDayVector vector, int index, boolean nullable, Calendar calendar) { | ||
| if (nullable) { | ||
| return new NullableDateConsumer(vector, index, calendar); | ||
| } else { | ||
|
|
@@ -45,21 +61,21 @@ public static JdbcConsumer<DateMilliVector> createConsumer( | |
| /** | ||
| * Nullable consumer for date. | ||
| */ | ||
| static class NullableDateConsumer extends BaseConsumer<DateMilliVector> { | ||
| static class NullableDateConsumer extends BaseConsumer<DateDayVector> { | ||
|
|
||
| protected final Calendar calendar; | ||
|
|
||
| /** | ||
| * Instantiate a DateConsumer. | ||
| */ | ||
| public NullableDateConsumer(DateMilliVector vector, int index) { | ||
| public NullableDateConsumer(DateDayVector vector, int index) { | ||
| this(vector, index, /* calendar */null); | ||
| } | ||
|
|
||
| /** | ||
| * Instantiate a DateConsumer. | ||
| */ | ||
| public NullableDateConsumer(DateMilliVector vector, int index, Calendar calendar) { | ||
| public NullableDateConsumer(DateDayVector vector, int index, Calendar calendar) { | ||
| super(vector, index); | ||
| this.calendar = calendar; | ||
| } | ||
|
|
@@ -69,7 +85,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, date.getTime()); | ||
| int day = (int) TimeUnit.MILLISECONDS.toDays(date.getTime()); | ||
| if (day < 0 || day > MAX_DAY) { | ||
| throw new IllegalArgumentException("Day overflow: " + day); | ||
| } | ||
| vector.setSafe(currentIndex, day); | ||
| } | ||
| currentIndex++; | ||
| } | ||
|
|
@@ -78,21 +98,21 @@ public void consume(ResultSet resultSet) throws SQLException { | |
| /** | ||
| * Non-nullable consumer for date. | ||
| */ | ||
| static class NonNullableDateConsumer extends BaseConsumer<DateMilliVector> { | ||
| static class NonNullableDateConsumer extends BaseConsumer<DateDayVector> { | ||
|
|
||
| protected final Calendar calendar; | ||
|
|
||
| /** | ||
| * Instantiate a DateConsumer. | ||
| */ | ||
| public NonNullableDateConsumer(DateMilliVector vector, int index) { | ||
| public NonNullableDateConsumer(DateDayVector vector, int index) { | ||
| this(vector, index, /* calendar */null); | ||
| } | ||
|
|
||
| /** | ||
| * Instantiate a DateConsumer. | ||
| */ | ||
| public NonNullableDateConsumer(DateMilliVector vector, int index, Calendar calendar) { | ||
| public NonNullableDateConsumer(DateDayVector vector, int index, Calendar calendar) { | ||
| super(vector, index); | ||
| this.calendar = calendar; | ||
| } | ||
|
|
@@ -101,7 +121,11 @@ public NonNullableDateConsumer(DateMilliVector vector, int index, Calendar calen | |
| public void consume(ResultSet resultSet) throws SQLException { | ||
| Date date = calendar == null ? resultSet.getDate(columnIndexInResultSet) : | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this might be legacy, but
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure. I agree with you that java.sql.Date is better. |
||
| resultSet.getDate(columnIndexInResultSet, calendar); | ||
| vector.setSafe(currentIndex, date.getTime()); | ||
| int day = (int) TimeUnit.MILLISECONDS.toDays(date.getTime()); | ||
| if (day < 0 || day > MAX_DAY) { | ||
| throw new IllegalArgumentException("Day overflow: " + day); | ||
| } | ||
| vector.setSafe(currentIndex, day); | ||
| currentIndex++; | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should also change line 213?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch. Thank you.