xxlaykxx commented on code in PR #1541:
URL: https://github.com/apache/arrow-java/pull/1541#discussion_r4757562254
##########
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:
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.
##########
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:
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.
--
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]