raminqaf opened a new pull request, #29101: URL: https://github.com/apache/flink/pull/29101
## What is the purpose of the change This is a sub-task of [FLINK-40485](https://issues.apache.org/jira/browse/FLINK-40485) / FLIP-604 (Complete VARIANT Primitive Coverage with UUID and Timestamps). It adds explicit `CAST` and `TRY_CAST` support between the `UUID` type and character strings and binary. The `UUID` type was introduced in FLINK-40486 and its runtime in FLINK-40490. Until now a `UUID` could only be produced as a literal and round-tripped through sources and sinks. It could not be converted to or from any other type. This change makes `UUID` usable together with the string and binary types that carry it in practice. ## Brief change log - Add `UuidCastUtils` in `flink-table-runtime` with the runtime helpers: render a `UUID` to its canonical string, parse a string to the 16-byte encoding, and validate a 16-byte binary value. - Add four cast rules in `flink-table-planner` and register them in `CastRuleProvider`: `UuidToStringCastRule`, `UuidToBinaryCastRule`, `StringToUuidCastRule`, `BinaryToUuidCastRule`. - Route `UUID` casts through `LogicalTypeCasts` in `SqlCastFunction#canCastFrom`, in both directions. Calcite's own checker only allows a `UUID` to be cast from another `UUID`, so the type-level matrix has to govern it. - Extend `LogicalTypeCasts` with the `UUID` cast matrix: `UUID` to `CHAR`/`VARCHAR`, `UUID` to `BINARY(16)`/`BYTES` only, and `CHAR`/`VARCHAR`/`BINARY`/`VARBINARY` to `UUID`. ## Cast semantics All casts are explicit. There are no implicit coercions to or from `UUID`. `VARIANT` and `UUID` conversion is out of scope and handled by a separate issue. **`UUID` to `STRING`.** Renders the canonical lower-case 8-4-4-4-12 form. **`STRING` to `UUID`.** Parsed leniently, matching PostgreSQL's `uuid` input (`string_to_uuid` in `adt/uuid.c`). The digits may be upper or lower case, the value may be wrapped in a single pair of braces, and a single hyphen may follow any group of four digits. A malformed value fails `CAST` and returns `NULL` for `TRY_CAST`. | Input | Result | |-------------------------------------------|-------------------------------------| | `A0EEBC99-9C0B-4EF8-BB6D-6BB9BD380A11` | passes (upper case) | | `{a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11}` | passes (braces) | | `a0eebc999c0b4ef8bb6d6bb9bd380a11` | passes (no hyphens) | | `a0ee-bc99-9c0b-4ef8-bb6d-6bb9-bd38-0a11` | passes (a hyphen after every group) | | `{a0eebc99-9c0b4ef8-bb6d6bb9-bd380a11}` | passes (braces, hyphens at 8/16/24) | | `a0e-ebc999c0b4ef8bb6d6bb9bd380a11` | fails (hyphen inside a group) | | `-a0eebc999c0b4ef8bb6d6bb9bd380a11` | fails (leading hyphen) | | `a0eebc99--9c0b4ef8bb6d6bb9bd380a11` | fails (doubled hyphen) | | `{a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11` | fails (unmatched brace) | | `a0eebc999c0b4ef8bb6d6bb9bd380a1` | fails (31 digits) | | `g0eebc999c0b4ef8bb6d6bb9bd380a11` | fails (non-hex digit) | The five passing examples all decode to the same value `a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11`. **`UUID` to `BINARY(16)` / `BYTES`.** Produces the 16 big-endian bytes. Any other target width is rejected during validation, since padding or trimming would corrupt the value. **`BINARY` / `BYTES` to `UUID`.** Reinterprets the bytes as a `UUID`. The value has to be exactly 16 bytes, which is checked at runtime. A different length fails `CAST` and returns `NULL` for `TRY_CAST`. ## Verifying this change This change added tests and can be verified as follows: - `CastRulesTest` covers the exhaustive value and lenient-parse matrix at the cast-executor level. - `LogicalTypeCastsTest` covers the type-level cast matrix in both directions. - `CastFunctionITCase#uuidCasts` covers the end-to-end SQL and Table API paths, validation failures, and `TRY_CAST` returning `NULL`. ## Does this pull request potentially affect one of the following parts: - Dependencies (does it add or upgrade a dependency): no - The public API, i.e., is any changed class annotated with `@Public(Evolving)`: no - The serializers: no - The runtime per-record code paths (performance sensitive): no - Anything that affects deployment or recovery: JobManager (and its components), Checkpointing, Kubernetes/Yarn, ZooKeeper: no - The S3 file system connector: no ## Documentation - Does this pull request introduce a new feature? yes - If yes, how is the feature documented? The `UUID` type documentation, including casting, is tracked in FLINK-40494 and is not part of this PR. --- ##### Was generative AI tooling used to co-author this PR? - [X] Yes (please specify the tool below) Generated-by: Claude Code (Opus 4.8) -- 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]
