Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,15 @@ public static LocalDateTime localTimeFromTime64Integer(int precision, long value
int nanoSeconds = 0;
if (precision > 0) {
int factor = BinaryStreamReader.BASES[precision];
nanoSeconds = Math.abs((int) (value % factor)); // nanoseconds are stored separately and only positive values accepted
nanoSeconds = (int) (value % factor);
value /= factor;

if (nanoSeconds < 0) {
nanoSeconds += factor;
value--;
}
if (nanoSeconds > 0L) {
nanoSeconds *= BASES[9 - precision];
}

}

return LocalDateTime.ofEpochSecond(value, nanoSeconds, ZoneOffset.UTC);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,4 +513,24 @@ public void testDurationToTimeStringIgnoresDefaultLocale() {
java.util.Locale.setDefault(original);
}
}

@DataProvider(name = "time64Values")
public Object[][] time64ValuesProvider() {
return new Object[][]{
{3, -3723456L, LocalDateTime.of(1969, 12, 31, 22, 57, 56, 544_000_000), Duration.parse("PT-1H-2M-3.456S")},
{9, -3723456789123L, LocalDateTime.of(1969, 12, 31, 22, 57, 56, 543_210_877), Duration.parse("PT-1H-2M-3.456789123S")},
{1, -5L, LocalDateTime.of(1969, 12, 31, 23, 59, 59, 500_000_000), Duration.parse("PT-0.5S")},
{3, -3723000L, LocalDateTime.of(1969, 12, 31, 22, 57, 57, 0), Duration.parse("PT-1H-2M-3S")},
{0, -3723L, LocalDateTime.of(1969, 12, 31, 22, 57, 57, 0), Duration.parse("PT-1H-2M-3S")},
{3, 3723456L, LocalDateTime.of(1970, 1, 1, 1, 2, 3, 456_000_000), Duration.parse("PT1H2M3.456S")},
};
}

@Test(groups = {"unit"}, dataProvider = "time64Values")
public void testLocalTimeFromTime64Integer(int precision, long rawValue, LocalDateTime expectedDateTime, Duration expectedDuration) {
LocalDateTime actualDateTime = DataTypeUtils.localTimeFromTime64Integer(precision, rawValue);
assertEquals(actualDateTime, expectedDateTime);
assertEquals(DataTypeUtils.localDateTimeToDuration(actualDateTime), expectedDuration);
assertEquals(actualDateTime.toInstant(ZoneOffset.UTC), DataTypeUtils.instantFromTime64Integer(precision, rawValue));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2053,14 +2053,14 @@ public static Object[][] testTimeData() {
{"Time64(3)","00:01:00.123", LocalDateTime.parse("1970-01-01T00:01:00.123")},
{"Time64(6)","00:01:00.123456", LocalDateTime.parse("1970-01-01T00:01:00.123456")},
{"Time64(9)","00:01:00.123456789", LocalDateTime.parse("1970-01-01T00:01:00.123456789")},
{"Time64","-00:01:00.123", LocalDateTime.parse("1969-12-31T23:59:00.123")},
{"Time64(3)","-00:01:00.123", LocalDateTime.parse("1969-12-31T23:59:00.123")},
{"Time64(6)","-00:01:00.123456", LocalDateTime.parse("1969-12-31T23:59:00.123456")},
{"Time64(9)","-00:01:00.123456789", LocalDateTime.parse("1969-12-31T23:59:00.123456789")},
{"Time64","-999:59:59.999", LocalDateTime.parse("1969-11-20T08:00:01.999")},
{"Time64(3)","-999:59:59.999", LocalDateTime.parse("1969-11-20T08:00:01.999")},
{"Time64(6)","-999:59:59.999999", LocalDateTime.parse("1969-11-20T08:00:01.999999")},
{"Time64(9)","-999:59:59.999999999", LocalDateTime.parse("1969-11-20T08:00:01.999999999")},
{"Time64","-00:01:00.123", LocalDateTime.parse("1969-12-31T23:58:59.877")},
{"Time64(3)","-00:01:00.123", LocalDateTime.parse("1969-12-31T23:58:59.877")},
{"Time64(6)","-00:01:00.123456", LocalDateTime.parse("1969-12-31T23:58:59.876544")},
{"Time64(9)","-00:01:00.123456789", LocalDateTime.parse("1969-12-31T23:58:59.876543211")},
{"Time64","-999:59:59.999", LocalDateTime.parse("1969-11-20T08:00:00.001")},
{"Time64(3)","-999:59:59.999", LocalDateTime.parse("1969-11-20T08:00:00.001")},
{"Time64(6)","-999:59:59.999999", LocalDateTime.parse("1969-11-20T08:00:00.000001")},
{"Time64(9)","-999:59:59.999999999", LocalDateTime.parse("1969-11-20T08:00:00.000000001")},
{"Time64","999:59:59.999", LocalDateTime.parse("1970-02-11T15:59:59.999")},
{"Time64(3)","999:59:59.999", LocalDateTime.parse("1970-02-11T15:59:59.999")},
{"Time64(6)","999:59:59.999999", LocalDateTime.parse("1970-02-11T15:59:59.999999")},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ final class ReaderKnownIssues {
// valid conversions return wrong values
KnownIssue.of(WRONG_VALUE, "getBigInteger converts large values through long")
.accessors(BIG_INTEGER).types(FLOATS + "|Decimal\\(38, 0\\)"),
KnownIssue.of(WRONG_VALUE, "negative Time64 values with a fraction are one second off")
.accessors(DURATION, TEMPORAL_AMOUNT, INSTANT, LOCAL_DATE_TIME).types("Time64\\(\\d\\)"),
KnownIssue.of(WRONG_VALUE, "IPv4-mapped IPv6 values are read as 4 byte IPv4 addresses")
.accessors(BYTE_ARRAY).types("IPv6").readersOnly(),
KnownIssue.of(UNEXPECTED_EXCEPTION, "getBigDecimal parses the enum name as a number")
Expand Down
6 changes: 6 additions & 0 deletions history/latest/3419.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
## Bug Fixes

- **[client-v2]** Fixed reading negative `Time64` values with a non-zero fractional part.
`DataTypeUtils.localTimeFromTime64Integer` now uses floor division so `getDuration`, `getLocalDateTime`, and
`getInstant` preserve the negative fraction instead of adding it to the truncated seconds.
(https://github.com/ClickHouse/clickhouse-java/issues/3419)
Original file line number Diff line number Diff line change
Expand Up @@ -1305,8 +1305,8 @@ public void testTimeTypes() throws SQLException {
LocalDateTime negativeTime = rs.getObject("time", LocalDateTime.class);
assertEquals(negativeTime.toEpochSecond(ZoneOffset.UTC), -(TimeUnit.HOURS.toSeconds(999) + TimeUnit.MINUTES.toSeconds(59) + 59));
LocalDateTime negativeTime64 = rs.getObject("time64", LocalDateTime.class);
assertEquals(negativeTime64.toEpochSecond(ZoneOffset.UTC), -(TimeUnit.HOURS.toSeconds(999) + TimeUnit.MINUTES.toSeconds(59) + 59), "value " + negativeTime64);
assertEquals(negativeTime64.getNano(), 999_999_999); // nanoseconds are stored separately and only positive values accepted
assertEquals(negativeTime64.toEpochSecond(ZoneOffset.UTC), -(TimeUnit.HOURS.toSeconds(999) + TimeUnit.MINUTES.toSeconds(59) + 59) - 1, "value " + negativeTime64);
assertEquals(negativeTime64.getNano(), 1);

// Positive values
assertTrue(rs.next());
Expand Down