jbonofre commented on code in PR #1541:
URL: https://github.com/apache/arrow-java/pull/1541#discussion_r4756270057
##########
flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcTimeVectorAccessor.java:
##########
@@ -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();
Review 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.
##########
flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcTime.java:
##########
@@ -32,74 +26,73 @@
* this#toString.
*/
public class ArrowFlightJdbcTime extends Time {
- private static final List<String> LEADING_ZEROES = ImmutableList.of("", "0",
"00");
-
- // Desired length of the millisecond portion should be 3
- private static final int DESIRED_MILLIS_LENGTH = 3;
+ private static final int NANOS_PER_SECOND = 1_000_000_000;
- // Millis of the date time object.
- private final int millisReprValue;
+ // Fraction of the second in nanoseconds. java.sql.Time itself only holds
milliseconds.
+ private final int nanos;
/**
* Constructs this object based on epoch millis.
*
* @param milliseconds milliseconds representing Time.
*/
public ArrowFlightJdbcTime(final long milliseconds) {
+ this(milliseconds, (int)
TimeUnit.MILLISECONDS.toNanos(Math.floorMod(milliseconds, 1000L)));
+ }
+
+ /**
+ * Constructs this object based on epoch millis and the full fraction of the
second.
+ *
+ * @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) {
super(milliseconds);
- millisReprValue = getMillisReprValue(milliseconds);
+ if (nanos < 0 || nanos >= NANOS_PER_SECOND) {
+ throw new IllegalArgumentException("nanos must be between 0 and
999999999: " + nanos);
+ }
+ this.nanos = nanos;
}
@VisibleForTesting
ArrowFlightJdbcTime(final LocalTime time) {
// Although the constructor is deprecated, this is the exact same code as
// Time#valueOf(LocalTime)
super(time.getHour(), time.getMinute(), time.getSecond());
- millisReprValue = time.get(ChronoField.MILLI_OF_SECOND);
+ nanos = time.getNano();
}
- private int getMillisReprValue(long milliseconds) {
- // Extract the millisecond part from epoch nano day
- if (milliseconds >= MILLIS_PER_DAY) {
- // Convert to Epoch Day
- milliseconds %= MILLIS_PER_DAY;
- } else if (milliseconds < 0) {
- // LocalTime#ofNanoDay only accepts positive values
- milliseconds -= ((milliseconds / MILLIS_PER_DAY) - 1) * MILLIS_PER_DAY;
- }
- return LocalTime.ofNanoOfDay(TimeUnit.MILLISECONDS.toNanos(milliseconds))
- .get(ChronoField.MILLI_OF_SECOND);
+ /** Returns the time with the full fraction of the second, not only whole
seconds. */
+ @Override
+ public LocalTime toLocalTime() {
+ return super.toLocalTime().withNano(nanos);
}
+ /**
+ * Formats the time as {@code hh:mm:ss}, followed by 3, 6 or 9 fractional
digits if the fraction
+ * of the second is not zero.
+ */
@Override
public String toString() {
final StringBuilder time = new StringBuilder().append(super.toString());
- if (millisReprValue > 0) {
- final String millisString = Integer.toString(millisReprValue);
-
- // dot to separate the fractional seconds
- time.append(".");
-
- final int millisLength = millisString.length();
- if (millisLength < DESIRED_MILLIS_LENGTH) {
- // add necessary leading zeroes
- time.append(LEADING_ZEROES.get(DESIRED_MILLIS_LENGTH - millisLength));
- }
- 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);
Review 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?
```java
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:
```java
@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);
}
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]