raminqaf commented on code in PR #29101: URL: https://github.com/apache/flink/pull/29101#discussion_r3933503958
########## flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/functions/casting/UuidToBinaryCastRule.java: ########## @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.table.planner.functions.casting; + +import org.apache.flink.table.types.logical.LogicalType; +import org.apache.flink.table.types.logical.LogicalTypeRoot; +import org.apache.flink.table.types.logical.VarBinaryType; +import org.apache.flink.table.types.logical.utils.LogicalTypeChecks; + +/** + * {@link LogicalTypeRoot#UUID} to binary cast rule. + * + * <p>A {@code UUID} is stored as its 16-byte big-endian encoding, so the cast returns those bytes + * unchanged. Only {@code BINARY(16)} and {@code BYTES} are supported targets: any other width would + * pad or trim the value and thereby corrupt it. + * + * <p>The input is already the internal {@code byte[]}, so the generated expression is the input + * term itself, with no transformation. Example generated code: + * + * <pre> + * result$0 = uuid$0; + * </pre> + */ +class UuidToBinaryCastRule extends AbstractExpressionCodeGeneratorCastRule<byte[], byte[]> { + + static final UuidToBinaryCastRule INSTANCE = new UuidToBinaryCastRule(); + + private UuidToBinaryCastRule() { + super( + CastRulePredicate.builder() + .predicate( + (input, target) -> + input.is(LogicalTypeRoot.UUID) && isSupportedTarget(target)) + .build()); + } + + private static boolean isSupportedTarget(LogicalType target) { + if (target.is(LogicalTypeRoot.BINARY)) { + return LogicalTypeChecks.getLength(target) == 16; + } + if (target.is(LogicalTypeRoot.VARBINARY)) { + return LogicalTypeChecks.getLength(target) == VarBinaryType.MAX_LENGTH; Review Comment: https://github.com/apache/flink/pull/29101#discussion_r3933499611 ########## flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/casting/CastRulesTest.java: ########## @@ -1571,6 +1584,50 @@ Stream<CastTestSpecBuilder> testCases() { .fromCase(BITMAP(), DEFAULT_BITMAP, DEFAULT_BITMAP.toBytes()) .fromCase(BITMAP(), Bitmap.empty(), Bitmap.empty().toBytes()) .fromCase(BITMAP(), null, null), + // UUID cast rules. A UUID renders as its canonical lower-case 8-4-4-4-12 form and + // maps to its 16-byte big-endian encoding. + CastTestSpecBuilder.testCastTo(STRING()) + .fromCase(UUID(), UUID_BYTES, fromString(UUID_STRING)) + .fromCase(UUID(), null, null), + // a bounded character target trims, and CHAR pads to its fixed width + CastTestSpecBuilder.testCastTo(VARCHAR(8)) + .fromCase(UUID(), UUID_BYTES, fromString("550e8400")), + CastTestSpecBuilder.testCastTo(CHAR(38)) + .fromCase(UUID(), UUID_BYTES, fromString(UUID_STRING + " ")), + CastTestSpecBuilder.testCastTo(BINARY(16)) + .fromCase(UUID(), UUID_BYTES, UUID_BYTES) + .fromCase(UUID(), null, null), + CastTestSpecBuilder.testCastTo(BYTES()) + .fromCase(UUID(), UUID_BYTES, UUID_BYTES) + .fromCase(UUID(), null, null), + // a UUID is parsed leniently from a string, following PostgreSQL conventions + CastTestSpecBuilder.testCastTo(UUID()) + .fromCase(STRING(), fromString(UUID_STRING), UUID_BYTES) + .fromCase(STRING(), fromString(UUID_STRING.toUpperCase()), UUID_BYTES) + .fromCase(STRING(), fromString("{" + UUID_STRING + "}"), UUID_BYTES) + .fromCase(STRING(), fromString(UUID_STRING.replace("-", "")), UUID_BYTES) + // a hyphen may follow any group of four digits, PostgreSQL style + .fromCase( + STRING(), + fromString("550e-8400-e29b-41d4-a716-4466-5544-0000"), + UUID_BYTES) + .fromCase(STRING(), null, null) + // a malformed value fails, and yields null for TRY_CAST + .fail(STRING(), fromString("not-a-uuid"), TableRuntimeException.class) + .fail(STRING(), fromString("550e8400"), TableRuntimeException.class) + // too many hexadecimal digits + .fail(STRING(), fromString(UUID_STRING + "00"), TableRuntimeException.class) + // a hyphen inside a four-digit group is rejected + .fail( + STRING(), + fromString("5-50e8400e29b41d4a716446655440000"), + TableRuntimeException.class), Review Comment: Added error messages -- 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]
