Repository navigation
Conversation
…ock time Read TIME columns in the Flight SQL JDBC driver as wall-clock time with the full fraction of the column unit, instead of truncating to milliseconds and shifting by the client JVM timezone. Accept LocalTime as a parameter and convert bound TIME values to the unit of the parameter vector. Closes apache#1539. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This comment has been minimized.
This comment has been minimized.
| time.append(millisString); | ||
| if (nanos > 0) { | ||
| final int digits = nanos % 1_000_000 == 0 ? 3 : nanos % 1_000 == 0 ? 6 : 9; | ||
| time.append('.').append(String.format("%09d", nanos), 0, digits); |
There was a problem hiding this comment.
String.format uses the JVM default locale here, so locales with non-Latin digits get localized digits in the fraction. With -Duser.language=ar -Duser.country=EG, getString() on 12:34:56.123 returns 12:34:56..., which LocalTime.parse rejects. fa-IR behaves the same way. On main this returns 12:34:56.123, because it uses Integer.toString, so millisecond values regress too.
Could we pass Locale.ROOT, as IntervalStringUtils does?
time.append('.').append(String.format(Locale.ROOT, "%09d", nanos), 0, digits);This also needs import java.util.Locale;. We could add a regression test in ArrowFlightJdbcTimeTest, for example:
@Test
public void testPrintingFractionIgnoresDefaultLocale() {
final Locale defaultLocale = Locale.getDefault();
try {
Locale.setDefault(Locale.forLanguageTag("ar-EG"));
ArrowFlightJdbcTime time =
new ArrowFlightJdbcTime(LocalTime.of(hour, minute, second, 123_456_000));
assertThat(time.toString(), is("05:06:07.123456"));
} finally {
Locale.setDefault(defaultLocale);
}
}There was a problem hiding this comment.
Good catch — fixed in the latest push. The fraction is now formatted with Locale.ROOT, and I added the regression test with an ar-EG default locale.
| ZoneId zone = calendar == null ? ZoneId.systemDefault() : calendar.getTimeZone().toZoneId(); | ||
| long milliseconds = localTime.atDate(LocalDate.EPOCH).atZone(zone).toInstant().toEpochMilli(); |
There was a problem hiding this comment.
On your question in #1539: I don't think we need a legacy path here. I know the timestamp accessor keeps one in getTimestampWithOffset, but for TIME the shift is the bug being fixed, and keeping it would leave getTime wrong outside UTC.
It is still a visible change: with a Europe/Berlin JVM, getTime() for 12:34:56.123 goes from epoch millis 45296123 to 41696123. We should keep this in the PR description with a before/after example. Let me add a breaking-change label too.
There was a problem hiding this comment.
Agreed, no legacy path then. I added a before/after example for a Europe/Berlin JVM to the PR description and marked it as containing breaking changes. Thanks for adding the label.
- Format the fraction of ArrowFlightJdbcTime with Locale.ROOT so that it does not depend on the JVM default locale - Add a regression test with an ar-EG default locale Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
| }); | ||
| } | ||
|
|
||
| @Test |
There was a problem hiding this comment.
Consider a test like this to fully cover the calendar conversion in getTime.
@Test
public void testGetTimeWithCalendarInNonUtcDefaultZone() throws Exception {
// 12:34:56.789 = 45_296_789 ms of the day
final int millisOfDay = (int) (LocalTime.parse("12:34:56.789").toNanoOfDay() / 1_000_000);
try (TimeMilliVector milliVector =
new TimeMilliVector("", rootAllocatorTestExtension.getRootAllocator())) {
milliVector.setSafe(0, millisOfDay);
milliVector.setValueCount(1);
this.vector = milliVector;
final TimeZone defaultTz = TimeZone.getDefault();
try {
TimeZone.setDefault(TimeZone.getTimeZone("Europe/Berlin")); // UTC+1 on 1970-01-01
ArrowFlightJdbcTimeVectorAccessor accessor = accessorSupplier.supply(vector, () -> 0);
Calendar vancouver = Calendar.getInstance(TimeZone.getTimeZone(AMERICA_VANCOUVER)); // UTC-8
// JDBC: the calendar's zone gives the wall-clock time its instant.
// 12:34:56.789 in Vancouver = 20:34:56.789 UTC = 74_096_789 epoch ms.
// The old code also added the default zone's offset and gave 77_696_789 (21:34:56.789 UTC).
assertThat(accessor.getTime(vancouver).getTime(), is(74_096_789L));
assertThat(accessor.getTimestamp(vancouver).getTime(), is(74_096_789L));
// With no calendar, the default zone is used: 12:34:56.789 in Berlin = 11:34:56.789 UTC.
// The old code returned the raw 45_296_789, which prints as 13:34:56.789 in Berlin.
assertThat(accessor.getTime(null).getTime(), is(41_696_789L));
assertThat(accessor.getTime(null).toString(), is("12:34:56.789"));
} finally {
TimeZone.setDefault(defaultTz);
}
}
}
There was a problem hiding this comment.
Added as testGetTimeWithCalendarInNonUtcDefaultZone, thanks. It passes as written, including the getTimestamp(vancouver) assertion.
| * @param milliseconds milliseconds representing Time. | ||
| * @param nanos fraction of the second in nanoseconds, including the millisecond part. | ||
| */ | ||
| public ArrowFlightJdbcTime(final long milliseconds, final int nanos) { |
There was a problem hiding this comment.
I think the two parameters can allow conflicting nano settings.
// 45_296_500 ms = 12:34:56.500, but nanos says .123456789
ArrowFlightJdbcTime t = new ArrowFlightJdbcTime(45_296_500L, 123_456_789);
t.getTime(); // 45_296_500 -> .500 (from milliseconds)
t.toString(); // "12:34:56.123456789" -> .123 (hh:mm:ss from millis, fraction from nanos)
t.toLocalTime(); // 12:34:56.123456789 -> .123
new Timestamp(t.getTime()).getNanos(); // 500_000_000
t.equals(new Time(45_296_500L)); // true, because equals only compares millis
Two ideas for fixing it:
// 1. Reject values that disagree
if (Math.floorMod(milliseconds, 1000L) != nanos / 1_000_000) {
throw new IllegalArgumentException(...);
}
// 2. Let nanos decide, and rebuild the millis from it
super(Math.floorDiv(milliseconds, 1000L) * 1000L + nanos / 1_000_000);
There was a problem hiding this comment.
Good point. I went with the first idea: the constructor now throws IllegalArgumentException when the millisecond part of nanos differs from the one of milliseconds, so a mismatch fails loudly rather than being silently rewritten. The check uses Math.floorMod, so values before the epoch work too.
While there I also fixed the test-only LocalTime constructor, which had the same problem the other way round: it kept the fraction in nanos but dropped it from getTime(). Tests cover both.
Reject ArrowFlightJdbcTime values whose milliseconds and nanoseconds disagree, and cover the calendar conversion of getTime in a non-UTC default time zone. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
What's Changed
TIMEcolumns read through the Flight SQL JDBC driver were truncated to milliseconds and shifted by the client JVM timezone, andTIMEparameters could not be bound correctly. With the client inEurope/Berlin,12:34:56.123456from aTime(MICROSECOND, 64)column was returned as13:34:56.123.All changes are in
flight/flight-sql-jdbc-core.Reading (
ArrowFlightJdbcTimeVectorAccessor,ArrowFlightJdbcTime)LocalTimebuilt from the raw vector value in nanoseconds.getObject(i, LocalTime.class)returns the full fraction and returnsnullon a NULL row instead of throwingNullPointerException.getTime,getObjectandgetStringreturn the wall-clock time in any JVM default timezone.getTime(calendar)returns the wall-clock time in the timezone of the calendar.ArrowFlightJdbcTimecarries the nanosecond fraction:toStringprints 3, 6 or 9 fractional digits andtoLocalTime()returns the full fraction.java.sql.Timeitself is limited to milliseconds, sogetStringandgetObject(i, LocalTime.class)are the lossless paths.getTimestampkeeps the nanoseconds as well.Binding (
TimeAvaticaParameterConverter)PreparedStatement.setObject(i, LocalTime)is accepted. It failed before withBinding value of type OBJECT is not yet supported for expected Arrow type Time(MICROSECOND, 64).This contains breaking changes.
Behaviour changes to review
getTimewith no calendar now returns wall-clock time, so a client that compensates for the old timezone shift will see a different value. There is no legacy path likegetTimestampWithOffsetin the timestamp accessor: forTIMEthe shift is the bug being fixed. Example for the value12:34:56.123read withgetTime()in aEurope/BerlinJVM:getTime().getTime()getTime().toString()4529612313:34:56.1234169612312:34:56.123In a UTC JVM both values are unchanged.
The number of fractional digits follows the value, not the column unit:
12:34:56in a microsecond column still prints as12:34:56. This keeps the output for whole-second and millisecond values as it was.A stored value outside 0-24h is wrapped into the day instead of being rejected.
ArrowFlightJdbcTime.hashCodedepends only on the millisecond value, to stay consistent withequals.Testing
UTC,Europe/Berlin,America/VancouverandAsia/Kolkatadefault timezones, NULL rows, and bindingLocalTimeandjava.sql.Timeto all four time units.mvn -pl flight/flight-sql-jdbc-core -am verifyfor the driver tests: 1363 tests, 0 failures, 6 errors. The 6 errors are TLS connection tests that needtesting/data/flight/cert0.pem; thetestingsubmodule was not checked out locally. The style and static checks pass.The same unit problem exists for
TIMESTAMPparameters; it is tracked separately in #1540 and not changed here.AI assistance disclosure: this change and description were prepared with the help of an AI coding assistant; the checks above were run locally.
Closes #1539.
🤖 Generated with Claude Code