[FLINK-40491][core] Extend BinaryVariant with TIME, TIMESTAMP_NS, TIMESTAMP_LTZ_NS primitives - #29050
[FLINK-40491][core] Extend BinaryVariant with TIME, TIMESTAMP_NS, TIMESTAMP_LTZ_NS primitives#29050manner wants to merge 6 commits into
Conversation
…ESTAMP_LTZ_NS primitives
| assertThat(dateTimeVariant.getType()).isEqualTo(Variant.Type.TIMESTAMP_NS); | ||
| assertThat(dateTimeVariant.getDateTimeNanos()).isEqualTo(nanoLocalDateTime); | ||
| assertThat(dateTimeVariant.get()).isEqualTo(nanoLocalDateTime); | ||
| assertThatThrownBy(dateTimeVariant::getDateTime).isInstanceOf(VariantTypeException.class); |
There was a problem hiding this comment.
Try this case
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
There was a problem hiding this comment.
Good catch! I'll add proper handling
There was a problem hiding this 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.

So this was a pre-existing bug, that only surfaced now.
| case DATE: | ||
| return readLong(value, pos + 1, 4); | ||
| case INT8: | ||
| case TIME: |
There was a problem hiding this comment.
is it TIME with micro/nanos or not?
There was a problem hiding this comment.
| case TIMESTAMP_LTZ_NS: | ||
| case TIMESTAMP_NS: |
There was a problem hiding this comment.
why do we think 8 bytes is enough here?
There was a problem hiding this comment.
The Variant specification states 8 bytes for nanosecond timestamps as well. The downside for using the same 8 bytes for the higher precision values is the smaller range of timestamps that can be used (+/- 292 years around unix epoch)

https://parquet.apache.org/docs/file-format/types/variantencoding/
There was a problem hiding this comment.
ok, thanks for clarification
I tend to think we need to explicitly mention such limitation in docs
There was a problem hiding this comment.
and by the way what will happen with timestamp_NS -300 years?
will it fail (user friendly message?)
or produce some wrong result?
There was a problem hiding this comment.
You're right, this should be properly documented. I think documentation will mostly happen in this ticket:
https://issues.apache.org/jira/browse/FLINK-40494
When converting goes wrong it will now throw a VariantTypeException with a helpful error message:
https://github.com/apache/flink/pull/29050/changes#diff-2a1f60d1bf5ca4585c1c2d08c51866702d17d70306ee16ce096b238377b82770R137-R148
There's a test for this here:
https://github.com/apache/flink/pull/29050/changes#diff-662023392a0949caa3a0814536613caf4fb2a17c1e6f3d7217e829bee0d50390R142-R154
|
after looking one more time the thing I didn't get I guess we should, or? |
What is the purpose of the change
The variant binary encoding spec (see Variant Encoding) defines primitive type codes 17-20 for
TIME,TIMESTAMP_LTZ_NS,TIMESTAMP_NS, andUUIDthat are currently missing in Flink.This PR adds support for codes 17-19 (
TIME,TIMESTAMP_LTZ_NS,TIMESTAMP_NS) toBinaryVariant, so that aLocalTimeand a nanosecond-precisionInstant/LocalDateTimecan be represented in aVariantwithout lossy truncation to microseconds.UUID(code 20) is intentionally out of scope here and will be added in a separate ticket.Brief change log
BinaryVariantUtilconstants for primitive codes 17-19, extendedgetType()/valueSize()/getLong()to handle them, and added aTIME_FORMATTERfor JSON renderingVariant.Type.TIME/TIMESTAMP_NS/TIMESTAMP_LTZ_NSand the correspondinggetTime()/getDateTimeNanos()/getInstantNanos()accessors to theVariantinterfaceVariantBuilder.of(LocalTime); madeof(Instant)/of(LocalDateTime)precision-aware so a value with no sub-microsecond component keeps using the existing compact micros encoding, and only switches to the new nanosecond encoding when the value actually needs itappendTime/appendTimestampNanos/appendTimestampLtzNanosinBinaryVariantInternalBuilder, and the matching read/get()/toJson()support inBinaryVariantVerifying this change
This change added tests and can be verified as follows:
get()dispatch tests for the new types (BinaryVariantTest#testScalarVariant)Instant/LocalDateTimepick the existingTIMESTAMP_LTZ/TIMESTAMPencoding for microsecond-aligned values and the newTIMESTAMP_LTZ_NS/TIMESTAMP_NSencoding otherwise, including that the mismatched accessor throwsVariantTypeException(BinaryVariantTest#testNanosecondPrecisionVariant)LocalTimesilently truncates below microsecond precision, sinceTIMEhas no nanosecond-precision counterpart in the variant spec (BinaryVariantTest#testTimeSubMicrosecondTruncation)TIME/TIMESTAMP_NS/TIMESTAMP_LTZ_NS(BinaryVariantTest#testToJsonScalar)Does this pull request potentially affect one of the following parts:
@Public(Evolving): yes (VariantandVariantBuilderare@PublicEvolving; this adds new enum constants and new interface methods)Documentation
Was generative AI tooling used to co-author this PR?
Generated-by: Claude Sonnet 5 (Claude Code)