[FLINK-40493][table] Support casting from VARIANT to UUID - #29163
Conversation
| Arguments.of(new UuidType(), new VariantType(), false, false), | ||
| Arguments.of(new VariantType(), new UuidType(), false, false)); | ||
| // a UUID does not cast to VARIANT | ||
| Arguments.of(new UuidType(), new VariantType(), false, false)); |
There was a problem hiding this comment.
I assume casting to its existing type is safe - does it warrant a test?
There was a problem hiding this comment.
There's an identity cast test in line 389: Arguments.of(new UuidType(), new UuidType(), true, true). Did you mean that one?
There was a problem hiding this comment.
For now this test is fine. For the future we will add support of CAST primitive dataTypes to Variant.
davidradl
left a comment
There was a problem hiding this comment.
Approve pending the CI completing and the small comments being addressed.
| * Encodes a {@code UUID} into the canonical 16-byte big-endian representation used to store a | ||
| * {@code UUID} value. | ||
| */ | ||
| public static byte[] toUuidBytes(UUID uuid) { |
There was a problem hiding this comment.
I think this piece of code exists in many places for UUID now. Could you double check if we can re-use this method more and replace the duplicates?
There was a problem hiding this comment.
In the current location only RexLiteralUtil in the planner can reuse it, to deduplicate it also in UuidUuidConverter it would have to move to flink-table-common.
| .fromCase(VARIANT(), null, null) | ||
| .fromCase( | ||
| VARIANT(), | ||
| Variant.newBuilder().of(UUID.fromString(UUID_STRING)), |
There was a problem hiding this comment.
Nit: extract UUID.fromString as static field and re-use
What is the purpose of the change
This pull request adds
CASTandTRY_CASTfrom aVARIANTholding a UUID (variant primitive code 20) to the nativeUUIDtype, completing UUID coverage forVARIANTunder FLIP-604.A
VARIANTwhose stored kind isUUIDcan now be cast to theUUIDtype, and it renders to a character string in the canonical lower-case 8-4-4-4-12 form like any other scalar kind.Consistent with the existing
VARIANT-cast semantics, a cast never reinterprets the stored kind, so the UUID variant casts only toUUID(and to a string).Brief change log
VariantCastUtils.toUuiddecodes a UUID-kind variant into the 16-byte big-endian encoding used internally for theUUIDtype,renderScalargains aUUIDcase producing the canonical string.VariantToPrimitiveCastRuleacceptsUUIDas a target and generates thetoUuidcall.LogicalTypeCastsdeclaresVARIANTas an explicit source forcastTo(UUID).data-types.md, bothcontentandcontent.zh) listsUUIDin the "stored kind → succeeds for" table forVARIANTcasts.Verifying this change
This change added tests and can be verified as follows:
CastRulesTest: added aVARIANT→UUIDcase (success and aTableRuntimeExceptionfor a non-UUID kind) and aVARIANT→STRINGcase asserting the canonical rendering.LogicalTypeCastsTest:VARIANT→UUIDis explicit-only castable;UUID→VARIANTstays unsupported.CastRuleProviderTest: theVARIANT→UUIDrule resolves.Does this pull request potentially affect one of the following parts:
@Public(Evolving): noDocumentation
Was generative AI tooling used to co-author this PR?
Generated-by: Claude Code (Opus 4.8)