raminqaf commented on code in PR #29050:
URL: https://github.com/apache/flink/pull/29050#discussion_r3895821384
##########
flink-core/src/main/java/org/apache/flink/types/variant/VariantBuilder.java:
##########
@@ -68,6 +69,9 @@ public interface VariantBuilder {
/** Create a variant from a LocalDateTime. */
Variant of(LocalDateTime localDateTime);
+ /** Create a variant from a LocalTime. */
Review Comment:
Should we mention that we truncate to micro?
##########
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:
Try this case
```java
LocalDateTime nanoLocalDateTime2 = LocalDateTime.of(2300, 1, 1, 0,
0, 0, 1);
Variant dateTimeVariant2 = builder.of(nanoLocalDateTime2);
```
This will overflow. We should handle the error better
--
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]