Repository navigation
GH-1539: Read and bind TIME values at full precision as wall-clock time in Flight SQL JDBC #1541
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
base: main
Are you sure you want to change the base?
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 |
|---|---|---|
|
|
@@ -23,14 +23,15 @@ | |
| import java.sql.SQLException; | ||
| import java.sql.Time; | ||
| import java.sql.Timestamp; | ||
| import java.time.LocalDate; | ||
| import java.time.LocalTime; | ||
| import java.time.ZoneId; | ||
| import java.util.Calendar; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.function.IntSupplier; | ||
| import org.apache.arrow.driver.jdbc.ArrowFlightJdbcTime; | ||
| import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessor; | ||
| import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessorFactory; | ||
| import org.apache.arrow.driver.jdbc.utils.DateTimeUtils; | ||
| import org.apache.arrow.vector.TimeMicroVector; | ||
| import org.apache.arrow.vector.TimeMilliVector; | ||
| import org.apache.arrow.vector.TimeNanoVector; | ||
|
|
@@ -47,6 +48,8 @@ public class ArrowFlightJdbcTimeVectorAccessor extends ArrowFlightJdbcAccessor { | |
| private final TimeUnit timeUnit; | ||
| private final Holder holder; | ||
|
|
||
| private static final long NANOS_PER_DAY = TimeUnit.DAYS.toNanos(1); | ||
|
|
||
| /** | ||
| * Instantiate an accessor for a {@link TimeNanoVector}. | ||
| * | ||
|
|
@@ -136,21 +139,35 @@ public <T> T getObject(final Class<T> type) throws SQLException { | |
| return !type.isPrimitive() && wasNull ? null : type.cast(value); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the value as a {@link Time} holding the wall-clock time of the column in the time zone | ||
| * of the given calendar, or in the JVM default time zone if no calendar is given. TIME has no | ||
| * time zone, so the value read with the default calendar prints the same in any client zone. | ||
| * | ||
| * <p>{@link Time} is limited to milliseconds. The returned object keeps the full fraction for | ||
| * {@link Time#toString()} and {@link Time#toLocalTime()} only. | ||
| */ | ||
| @Override | ||
| public Time getTime(Calendar calendar) { | ||
| fillHolder(); | ||
| if (this.wasNull) { | ||
| LocalTime localTime = getLocalTime(); | ||
| if (localTime == null) { | ||
| return null; | ||
| } | ||
|
|
||
| long value = holder.value; | ||
| long milliseconds = this.timeUnit.toMillis(value); | ||
| ZoneId zone = calendar == null ? ZoneId.systemDefault() : calendar.getTimeZone().toZoneId(); | ||
| long milliseconds = localTime.atDate(LocalDate.EPOCH).atZone(zone).toInstant().toEpochMilli(); | ||
|
Comment on lines
+157
to
+158
Member
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. On your question in #1539: I don't think we need a legacy path here. I know the timestamp accessor keeps one in It is still a visible change: with a
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. Agreed, no legacy path then. I added a before/after example for a |
||
|
|
||
| return new ArrowFlightJdbcTime(DateTimeUtils.applyCalendarOffset(milliseconds, calendar)); | ||
| return new ArrowFlightJdbcTime(milliseconds, localTime.getNano()); | ||
| } | ||
|
|
||
| private LocalTime getLocalTime() { | ||
| return getTime(null).toLocalTime(); | ||
| fillHolder(); | ||
| if (this.wasNull) { | ||
| return null; | ||
| } | ||
|
|
||
| long nanoseconds = this.timeUnit.toNanos(holder.value); | ||
| return LocalTime.ofNanoOfDay(Math.floorMod(nanoseconds, NANOS_PER_DAY)); | ||
| } | ||
|
|
||
| private void fillHolder() { | ||
|
|
@@ -165,7 +182,9 @@ public Timestamp getTimestamp(Calendar calendar) { | |
| if (time == null) { | ||
| return null; | ||
| } | ||
| return new Timestamp(time.getTime()); | ||
| Timestamp timestamp = new Timestamp(time.getTime()); | ||
| timestamp.setNanos(time.toLocalTime().getNano()); | ||
| return timestamp; | ||
| } | ||
|
|
||
| protected static TimeUnit getTimeUnitForVector(ValueVector vector) { | ||
|
|
||
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.
I think the two parameters can allow conflicting nano settings.
Two ideas for fixing it:
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.
Good point. I went with the first idea: the constructor now throws
IllegalArgumentExceptionwhen the millisecond part ofnanosdiffers from the one ofmilliseconds, so a mismatch fails loudly rather than being silently rewritten. The check usesMath.floorMod, so values before the epoch work too.While there I also fixed the test-only
LocalTimeconstructor, which had the same problem the other way round: it kept the fraction innanosbut dropped it fromgetTime(). Tests cover both.