-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved Parquet specific methods out of DateTimeUtils (#4819)
- Loading branch information
1 parent
a61a8ef
commit df45e1c
Showing
9 changed files
with
137 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
extensions/parquet/table/src/main/java/io/deephaven/parquet/table/util/TransferUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package io.deephaven.parquet.table.util; | ||
|
||
import io.deephaven.time.DateTimeUtils; | ||
import io.deephaven.util.QueryConstants; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.ZoneOffset; | ||
|
||
/** | ||
* Internal library with utility methods for converting data between Deephaven and Parquet. | ||
*/ | ||
public class TransferUtils { | ||
/** | ||
* Returns nanoseconds from the Epoch for a {@link LocalDateTime} value in UTC timezone. | ||
* | ||
* @param localDateTime the local date time to compute the Epoch offset for | ||
* @return nanoseconds since Epoch, or a NULL_LONG value if the local date time is null | ||
*/ | ||
public static long epochNanosUTC(@Nullable final LocalDateTime localDateTime) { | ||
if (localDateTime == null) { | ||
return QueryConstants.NULL_LONG; | ||
} | ||
return DateTimeUtils.secondsToNanos(localDateTime.toEpochSecond(ZoneOffset.UTC)) | ||
+ localDateTime.toLocalTime().getNano(); | ||
} | ||
|
||
/** | ||
* Converts nanoseconds from the Epoch to a {@link LocalDateTime} in UTC timezone. | ||
* | ||
* @param nanos nanoseconds since Epoch | ||
* @return {@code null} if the input is {@link QueryConstants#NULL_LONG}; otherwise the input nanoseconds from the | ||
* Epoch converted to a {@link LocalDateTime} in UTC timezone | ||
*/ | ||
public static @Nullable LocalDateTime epochNanosToLocalDateTimeUTC(final long nanos) { | ||
return nanos == QueryConstants.NULL_LONG ? null | ||
: LocalDateTime.ofEpochSecond(nanos / 1_000_000_000L, (int) (nanos % 1_000_000_000L), ZoneOffset.UTC); | ||
} | ||
|
||
/** | ||
* Converts microseconds from the Epoch to a {@link LocalDateTime} in UTC timezone. | ||
* | ||
* @param micros microseconds since Epoch | ||
* @return {@code null} if the input is {@link QueryConstants#NULL_LONG}; otherwise the input microseconds from the | ||
* Epoch converted to a {@link LocalDateTime} in UTC timezone | ||
*/ | ||
public static @Nullable LocalDateTime epochMicrosToLocalDateTimeUTC(final long micros) { | ||
return micros == QueryConstants.NULL_LONG ? null | ||
: LocalDateTime.ofEpochSecond(micros / 1_000_000L, (int) ((micros % 1_000_000L) * DateTimeUtils.MICRO), | ||
ZoneOffset.UTC); | ||
} | ||
|
||
/** | ||
* Converts milliseconds from the Epoch to a {@link LocalDateTime} in UTC timezone. | ||
* | ||
* @param millis milliseconds since Epoch | ||
* @return {@code null} if the input is {@link QueryConstants#NULL_LONG}; otherwise the input milliseconds from the | ||
* Epoch converted to a {@link LocalDateTime} in UTC timezone | ||
*/ | ||
public static @Nullable LocalDateTime epochMillisToLocalDateTimeUTC(final long millis) { | ||
return millis == QueryConstants.NULL_LONG ? null | ||
: LocalDateTime.ofEpochSecond(millis / 1_000L, (int) ((millis % 1_000L) * DateTimeUtils.MILLI), | ||
ZoneOffset.UTC); | ||
} | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
extensions/parquet/table/src/test/java/io/deephaven/parquet/table/TestTransferUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* Copyright (c) 2016-2022 Deephaven Data Labs and Patent Pending | ||
*/ | ||
package io.deephaven.parquet.table; | ||
|
||
import io.deephaven.parquet.table.util.TransferUtils; | ||
import io.deephaven.time.DateTimeUtils; | ||
import io.deephaven.util.QueryConstants; | ||
import junit.framework.TestCase; | ||
import org.junit.Test; | ||
|
||
import java.time.Instant; | ||
import java.time.LocalDateTime; | ||
import java.time.ZoneId; | ||
|
||
final public class TestTransferUtils { | ||
|
||
@Test | ||
public void testEpochNanosUTC() { | ||
final long nanos = 123456789123456789L; | ||
final Instant dt2 = Instant.ofEpochSecond(0, nanos); | ||
final LocalDateTime ldt = LocalDateTime.ofInstant(dt2, ZoneId.of("UTC")); | ||
TestCase.assertEquals(nanos, TransferUtils.epochNanosUTC(ldt)); | ||
TestCase.assertEquals(QueryConstants.NULL_LONG, TransferUtils.epochNanosUTC(null)); | ||
} | ||
|
||
@Test | ||
public void testEpochNanosTo() { | ||
final long nanos = 123456789123456789L; | ||
final Instant dt2 = Instant.ofEpochSecond(0, nanos); | ||
final LocalDateTime ldt = LocalDateTime.ofInstant(dt2, ZoneId.of("UTC")); | ||
TestCase.assertEquals(ldt, TransferUtils.epochNanosToLocalDateTimeUTC(nanos)); | ||
TestCase.assertNull(TransferUtils.epochNanosToLocalDateTimeUTC(QueryConstants.NULL_LONG)); | ||
} | ||
|
||
@Test | ||
public void testEpochMicrosTo() { | ||
long nanos = 123456789123456789L; | ||
final long micros = DateTimeUtils.nanosToMicros(nanos); | ||
nanos = DateTimeUtils.microsToNanos(micros); | ||
final Instant dt2 = Instant.ofEpochSecond(0, nanos); | ||
final LocalDateTime ldt = LocalDateTime.ofInstant(dt2, ZoneId.of("UTC")); | ||
TestCase.assertEquals(ldt, TransferUtils.epochMicrosToLocalDateTimeUTC(micros)); | ||
TestCase.assertNull(TransferUtils.epochMicrosToLocalDateTimeUTC(QueryConstants.NULL_LONG)); | ||
} | ||
|
||
@Test | ||
public void testEpochMillisTo() { | ||
long nanos = 123456789123456789L; | ||
final long millis = DateTimeUtils.nanosToMillis(nanos); | ||
nanos = DateTimeUtils.millisToNanos(millis); | ||
final Instant dt2 = Instant.ofEpochSecond(0, nanos); | ||
final LocalDateTime ldt = LocalDateTime.ofInstant(dt2, ZoneId.of("UTC")); | ||
TestCase.assertEquals(ldt, TransferUtils.epochMillisToLocalDateTimeUTC(millis)); | ||
TestCase.assertNull(TransferUtils.epochMillisToLocalDateTimeUTC(QueryConstants.NULL_LONG)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters