manner commented on code in PR #29050:
URL: https://github.com/apache/flink/pull/29050#discussion_r3906185385
##########
flink-core/src/test/java/org/apache/flink/types/variant/BinaryVariantTest.java:
##########
@@ -99,10 +100,51 @@ void testScalarVariant() {
assertThat(builder.of(localDate).getDate()).isEqualTo(localDate);
assertThat(builder.of(localDate).get()).isEqualTo(localDate);
+ LocalTime localTime = LocalTime.now().truncatedTo(ChronoUnit.MICROS);
+ assertThat(builder.of(localTime).getTime()).isEqualTo(localTime);
+ assertThat(builder.of(localTime).get()).isEqualTo(localTime);
+
assertThat(builder.ofNull().get()).isEqualTo(null);
assertThat(builder.ofNull().isNull()).isTrue();
}
+ @Test
+ void testNanosecondPrecisionVariant() {
+ // Microsecond-precision values keep using the compact
TIMESTAMP/TIMESTAMP_LTZ encoding,
+ // matching the pre-existing on-wire format.
+ Instant microInstant = Instant.now().truncatedTo(ChronoUnit.MICROS);
+
assertThat(builder.of(microInstant).getType()).isEqualTo(Variant.Type.TIMESTAMP_LTZ);
+
assertThat(builder.of(microInstant).getInstant()).isEqualTo(microInstant);
+
+ LocalDateTime microLocalDateTime =
LocalDateTime.now().truncatedTo(ChronoUnit.MICROS);
+
assertThat(builder.of(microLocalDateTime).getType()).isEqualTo(Variant.Type.TIMESTAMP);
+
assertThat(builder.of(microLocalDateTime).getDateTime()).isEqualTo(microLocalDateTime);
+
+ // Sub-microsecond precision must switch to the nanosecond-precision
encoding rather than
+ // silently truncating.
+ Instant nanoInstant =
Instant.now().truncatedTo(ChronoUnit.NANOS).plusNanos(123);
+ Variant instantVariant = builder.of(nanoInstant);
+
assertThat(instantVariant.getType()).isEqualTo(Variant.Type.TIMESTAMP_LTZ_NS);
+ assertThat(instantVariant.getInstantNanos()).isEqualTo(nanoInstant);
+ assertThat(instantVariant.get()).isEqualTo(nanoInstant);
+
assertThatThrownBy(instantVariant::getInstant).isInstanceOf(VariantTypeException.class);
+
+ LocalDateTime nanoLocalDateTime =
LocalDateTime.now().withNano(123456789);
+ Variant dateTimeVariant = builder.of(nanoLocalDateTime);
+
assertThat(dateTimeVariant.getType()).isEqualTo(Variant.Type.TIMESTAMP_NS);
+
assertThat(dateTimeVariant.getDateTimeNanos()).isEqualTo(nanoLocalDateTime);
+ assertThat(dateTimeVariant.get()).isEqualTo(nanoLocalDateTime);
+
assertThatThrownBy(dateTimeVariant::getDateTime).isInstanceOf(VariantTypeException.class);
Review Comment:
Actually the same error will currently happen when using a microsecond
timestamp such as
`LocalDateTime nanoLocalDateTime = LocalDateTime.of(2300, 1, 1, 0, 0, 0, 0);`
`Instant.until()`s function for calculating microseconds between two
instants just uses the function for calculating nanoseconds between two
instants and then divides the result by 1000. This then also results in an
overflow.
<img width="534" height="164" alt="image"
src="https://github.com/user-attachments/assets/15f8f3eb-bd85-41fe-b28a-27ffb01cff4b"
/>
So this was a pre-existing bug, that only surfaced now.
--
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]