DUE TO SPAM, SIGN-UP IS DISABLED. Goto Selfserve wiki signup and request an account.
| Discussion thread | https://lists.apache.org/thread/o9j5wt7cmsg4cwykmqtzx2t6f6ly14nb |
|---|---|
| Vote thread | https://lists.apache.org/thread/604nvjx47m0fdf2mcvbyrcqb97g9cc6n |
| JIRA | FLINK-40485 - Getting issue details... STATUS |
| Release | <Flink Version> |
Motivation
This FLIP closes two related gaps in Flink's type system. Both are about type completeness against the open, cross-engine data formats that Flink already interoperates with.
Gap 1: no native UUID type
UUIDs are one of the most common identifier shapes in streaming and lakehouse data. They appear as Kafka record keys, CDC primary keys, event and trace IDs, and dimension keys in lake tables. Flink has no native UUID type today. Users model UUIDs as CHAR(36) or BINARY(16). This has real costs:
No type safety. A UUID column is indistinguishable from any other string or binary column.
No format-native mapping. Parquet, Iceberg, Avro, and Arrow all define a native UUID type. Flink cannot read or write those columns as UUIDs, so it falls back to a lossy string or opaque-binary mapping.
Storage waste. The canonical string form is 36 bytes. The binary form is 16.
No defined ordering. Comparison and
ORDER BYon UUIDs is not specified.
Apache Calcite has supported a native UUID type since Calcite 1.39.0 (CALCITE-6738), which is the same release Flink adopted for FLIP-521. Flink currently ships Calcite 1.41.0. The type is already available in the planner and unused. Adding it to Flink's own type system requires no Calcite upgrade.
Gap 2: VARIANT does not cover the full Variant primitive set
FLIP-521 introduced the VARIANT type in Flink 2.1. Its binary layout follows the open Parquet Variant Encoding. Flink's BinaryVariant implements primitive type codes 0 through 16. The open Variant spec defines codes through 20:
Code | Variant primitive | Flink target type |
|---|---|---|
| time without time zone (us) |
|
| timestamp with time zone (ns) |
|
| timestamp without tz (ns) |
|
| uuid (16-byte big-endian) |
|
TIME(3) is the SQL landing type, the highest precision Flink SQL supports. This limit is specific to the SQL runtime. The flink-core Variant keeps the full micros: Variant.getTime() returns a LocalTime at precision 6, and the raw bytes are never truncated. The nanosecond timestamps (codes 18 and 19) have no such gap, since Flink SQL supports TIMESTAMP(9) and TIMESTAMP_LTZ(9).
Flink's BinaryVariantUtil.getType throws unknownPrimitiveTypeInVariant for any code above 16. The practical consequence is an interoperability hole: a VARIANT value written by Spark, Iceberg, or any Parquet writer that contains a TIME, a nanosecond timestamp, or a UUID cannot be read by Flink. This breaks the lakehouse round-trip that FLIP-521 set out to support.
Prior art
UUID support across engines splits into two camps: a native 16-byte type, or UUID modeled as a string. The split tracks whether the engine is storage-native or query-first.
Native 16-byte UUID type
Engine | Storage | Generator returns | Other UUID functions |
|---|---|---|---|
PostgreSQL | 16 bytes |
|
|
Trino, Presto | 16 bytes |
| explicit |
DuckDB | 16 bytes |
|
|
ClickHouse | 16 bytes |
|
|
Snowflake | 16 bytes, distinct native |
|
|
UUID as a string, no native type
Engine | Storage | Generator returns | Other UUID functions |
|---|---|---|---|
MySQL |
|
|
|
Spark SQL |
|
| none; UUID rides in VARIANT only |
Databricks |
|
| none; inherits Spark's type system |
BigQuery |
|
| none |
Takeaways
The 16-byte representation is unanimous among engines with a native type. This FLIP matches them and the Parquet, Iceberg, and Avro formats.
Snowflake is direct precedent for this FLIP's split. It pairs a native UUID type with a string-returning legacy generator, using the idiom
id UUID DEFAULT UUID_STRING(). This FLIP keeps the legacyUUID()as a string and adds typedUUID_V4()andUUID_V7(), so both styles are available. The_V4and_V7suffixes name the UUID version each function produces, so the algorithm is explicit at the call site, and the underscore form matches Flink's existing built-in naming such asPARSE_JSONandTO_TIMESTAMP_LTZ.Explicit-cast-only between
UUIDandVARCHARmatches Trino.Version-named generators are the common pattern in DuckDB, ClickHouse, and PostgreSQL 18. UUIDv7 is the current trend for time-ordered keys.
Spark and Databricks are string-only, and Spark originated the Variant spec Flink adopted. Adding a native UUID type and completing Variant code 20 puts Flink ahead of them on UUID.
UUID is not part of the SQL standard. Every design above is a vendor extension, so there is no conformance target to follow.
Public Interfaces
Part A: the UUID type
Logical type
A new UuidType in flink-table-common, backed by a fixed 16-byte value. It has no type parameters and follows the nullable pattern of the other extension scalars.
package org.apache.flink.table.types.logical;
/**
* Logical type of a universally unique identifier (UUID), stored as a 16-byte value.
*
* <p>The default conversion class is {@link java.util.UUID}. {@code byte[]} of length 16 and
* {@link String} in the canonical 8-4-4-4-12 form are also supported conversion classes.
*/
@PublicEvolving
public class UuidType extends LogicalType {
// serialized/SQL string: UUID
}
@PublicEvolving
public enum LogicalTypeRoot {
// ...
UUID(LogicalTypeFamily.EXTENSION);
}
@PublicEvolving
public final class DataTypes {
// ...
/** Data type of a 16-byte universally unique identifier (UUID). */
public static DataType UUID() {
return new AtomicDataType(new UuidType());
}
}
SQL and serialized string form:
UUID. Usable directly in DDL, for exampleevent_id UUID.Conversion classes:
java.util.UUID(default),byte[]of length 16, andStringin the canonical form.DataStream API: a
BasicTypeInfo.UUID_TYPE_INFOplus aUuidSerializer, mirroring how FLIP-521 exposed VARIANT.
Internal representation
The internal representation is a fixed 16-byte big-endian value. No new RowData accessor is required. UUIDs are read and written through the existing RowData.getBinary(pos) path, and code generation converts to and from java.util.UUID at the API boundary. This matches the Parquet Variant code 20 layout and Iceberg's UUID storage, and it gives a natural comparison order.
Type factory and parser
FlinkTypeFactorymapsUuidTypeto and from CalciteSqlTypeName.UUID.The SQL parser and
LogicalTypeParserrecognizeUUIDas a type keyword.
SQL literal
Calcite 1.41 already parses and represents a UUID literal, so this FLIP surfaces it through the Flink SQL parser:
UUID '123e4567-e89b-12d3-a456-426655440000'
Calcite parses this into a SqlUuidLiteral and lowers it to a UUID-typed RexLiteral via java.util.UUID.fromString. The literal accepts the standard hyphenated form and is case-insensitive. It does not accept the alternative input forms that CAST(string AS UUID) allows; those are a CAST-only convenience. A malformed literal is a parse-time error. Surfacing the literal requires exposing the existing Calcite production in flink-sql-parser.
Casting
UUID participates in explicit casts only. There are no implicit coercions to or from other types.
Cast | Behavior |
|---|---|
| Canonical lowercase 8-4-4-4-12 form. |
| Parses lenient input: canonical, upper-case, braces, omitted or extra hyphens (PostgreSQL-compatible). Error on malformed, |
| 16 big-endian bytes. |
| Requires exactly 16 bytes. |
| Writes Variant primitive code 20. Cast to VARIANT is not supported for any primitive type in Flink |
| Reads Variant code 20 |
CAST(string AS UUID) accepts lenient input, matching PostgreSQL. Besides the canonical form it accepts upper-case hex digits, the value wrapped in braces, some or all hyphens omitted, and extra hyphens added after any group of four digits. All of these parse to the same value:
A0EEBC99-9C0B-4EF8-BB6D-6BB9BD380A11
{a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11}
a0eebc999c0b4ef8bb6d6bb9bd380a11
a0ee-bc99-9c0b-4ef8-bb6d-6bb9-bd38-0a11
{a0eebc99-9c0b4ef8-bb6d6bb9-bd380a11}
Input that is not a valid UUID after normalization is an error under CAST and yields NULL under TRY_CAST. Output is always the canonical lowercase 8-4-4-4-12 form, so CAST(uuid AS STRING) never reproduces an alternative input form.
Comparison and ordering
UUID supports =, <>, <, >, <=, >=, ORDER BY, GROUP BY, and use as a join key. Ordering is unsigned big-endian byte comparison of the 16-byte value. This matches Iceberg and PostgreSQL UUID ordering.
Format support
Format support is future work and can land incrementally. The core of this FLIP is the UUID type, its casts, and the VARIANT primitive completion. Mapping UUID into each format (Parquet, Avro, JSON, CSV, Protobuf) is additive and independent, so a format can adopt the type after the type ships. Until a format maps UUID natively, it falls back to the CAST to STRING form.
Format | Mapping |
|---|---|
Parquet | Parquet logical type |
Avro | Avro |
JSON | Canonical string form. |
Protobuf | Represented as string, bytes, or a custom message |
Part B: completing VARIANT primitive coverage
Variant interface
The Variant.Type enum gains four values that map one-to-one to the Variant wire codes.
enum Type {
// ... existing values ...
TIME,
TIMESTAMP_NS,
TIMESTAMP_LTZ_NS,
UUID
}
New accessors on Variant:
/** Scalar value as LocalTime, if the variant type is TIME. */ LocalTime getTime() throws VariantTypeException; /** Scalar value as UUID, if the variant type is UUID. */ java.util.UUID getUuid() throws VariantTypeException;
Nanosecond timestamps reuse the existing getTimestamp() (LocalDateTime) and getInstant() (Instant) accessors. Both Java types already carry nanosecond precision. The new TIMESTAMP_NS and TIMESTAMP_LTZ_NS enum values only distinguish the wire code so that a value re-encodes at the precision it was read with.
VariantBuilder
/** Create a variant from a LocalTime. */ Variant of(LocalTime time); /** Create a variant from a UUID. */ Variant of(java.util.UUID uuid);
The existing of(Instant) and of(LocalDateTime) builders become precision-aware. A value with sub-microsecond precision writes the nanosecond code (18 or 19). A value at microsecond precision or coarser keeps writing the existing microsecond code (12 or 13). This preserves backward-compatible output for existing callers and compact encodings for microsecond data.
String and JSON representation
TIME: ISO local time.TIMESTAMP_NSandTIMESTAMP_LTZ_NS: ISO form with a nanosecond fraction.UUID: canonical 36-character string.
Casting between VARIANT and the new types
FLIP-521 already declared TIME, TIMESTAMP, and TIMESTAMP_LTZ in the VARIANT cast list. This FLIP adds UUID to that list and makes the four codes reachable. CAST(variant AS TIME | TIMESTAMP(9) | TIMESTAMP_LTZ(9) | UUID) and the reverse casts extend the existing VARIANT cast framework.
TIME requires one caveat. Flink stores TIME internally as an integer count of milliseconds within a day, effectively supporting precision 3, though the type declares up to 9. A Variant TIME value (code 17) stores microsecond precision, which is precision 6. When a Variant TIME value has sub-millisecond digits, CAST(variant AS TIME) truncates them, consistent with Flink's other narrowing casts such as TIMESTAMP(9) → TIMESTAMP(3) and CAST(string AS TIME). The same applies to the nanosecond timestamps: casting to a lower precision, e.g. CAST(variant AS TIMESTAMP(3)), truncates the extra digits. The full value is never lost from the VARIANT bytes and stays reachable through the flink-core Variant API.
Proposed Changes
No Calcite upgrade.
SqlTypeName.UUIDis present in the Calcite 1.41 that Flink already ships.Add
UuidType,LogicalTypeRoot.UUID,DataTypes.UUID(), the parser keyword, and theFlinkTypeFactorymapping.Add the UUID cast rules and comparison support in the planner.
Add the
UUID_V4()andUUID_V7()generator functions returning theUUIDtype, and expose the Calcite UUID literal throughflink-sql-parser.Internal representation is a 16-byte binary. Code generation converts to and from
java.util.UUID. Add the DataStreamTypeInformationand serializer.Add UUID support to the Parquet, Avro, JSON, and CSV formats.
VARIANT: add primitive codes 17 through 20 to
BinaryVariantUtil(getType,valueSize, header writers), extendVariant,BinaryVariant,BinaryVariantBuilder, and theVariantBuilder, and update the JSON and string rendering.VariantSerializerneeds no change. It serializes the raw metadata and value byte arrays, so the new codes ride along without a format change. This keeps serialized VARIANT state compatible.
Compatibility, Deprecation, and Migration Plan
UUID is a purely additive new type.
UUID()is unchanged, so no existing query breaks. The newUUID_V4()andUUID_V7()functions and theUUID '...'literal are additive. ExistingCHAR(36)andBINARY(16)models keep working. Adoption is opt-in through DDL andCAST.VARIANT changes are additive. A VARIANT written by an older Flink never contained codes 17 through 20, so old-to-new reads are unaffected.
Forward-compatibility caveat. A VARIANT produced by the new Flink that contains codes 17 through 20 cannot be read by a pre-FLIP Flink. It will throw
unknownPrimitiveTypeInVariant. The Variant version flag stays 1 because these codes are already part of the open Variant spec. As with the existing VARIANT contract, persisted bytes are a one-way door once a newer type code is written.State. The extended
VariantSerializerlayout is unchanged, so VARIANT state stays compatible.UuidSerializeris new.
Test Plan
Type:
DataTypesand parser round-trip, the full cast matrix (UUIDto and from string, binary, and VARIANT), comparison and ordering, and null handling.Generators:
UUID_V4()andUUID_V7()return theUUIDtype, produce the correct version nibble, are non-deterministic, andUUID_V7()carries a millisecond prefix that is monotonic within a task slot.Formats: Parquet, Avro, JSON, and CSV read and write round-trips, including reading a UUID column written by Spark or Iceberg.
VARIANT: decode Spark- and Parquet-produced variants that contain codes 17 through 20, encode from Flink
TIME,TIMESTAMP(9),TIMESTAMP_LTZ(9), andUUID, verify the JSON string rendering, and verify cast round-trips.Integration tests mirroring the FLIP-521 VARIANT ITCases.
Rejected Alternatives
A version argument on the generator, such as
UUID(7). Rejected. No engine parameterizes the UUID version by an integer argument. The form collides with the newUUIDtype name and theUUID '...'literal, the version would have to be a constant literal anyway, and returning a different type per argument value is unclear. Version-named functions are used instead.Two separate FLIPs. Rejected. Variant code 20 is a UUID and needs the UUID type to cast into. Shipping them together keeps the data-format alignment coherent and avoids a second design discussion for the same goal.
String-backed UUID as
CHAR(36). Rejected. It costs 36 bytes instead of 16, does not align with the native UUID types in Parquet, Iceberg, Avro, and Arrow, and leaves ordering undefined.Changing
UUID()to return the new type. Rejected. It is a breaking change for every query that relies onUUID()being a string.UUID_V4()provides the typed equivalent without breaking the legacy function.Modeling nanosecond timestamps by carrying precision on the existing
TIMESTAMPandTIMESTAMP_LTZVariant enum values instead of new entries. Rejected. Distinct enum values map one-to-one to the wire codes and keep the round-trip unambiguous.
[1] Parquet Variant Encoding: https://github.com/apache/parquet-format/blob/master/VariantEncoding.md
[2] FLIP-521: https://cwiki.apache.org/confluence/display/FLINK/FLIP-521
[3] CALCITE-6738 Support a UUID type natively: https://issues.apache.org/jira/browse/CALCITE-6738
[4] PostgreSQL UUID Type support: https://www.postgresql.org/docs/19/datatype-uuid.html