twalthr commented on code in PR #29101:
URL: https://github.com/apache/flink/pull/29101#discussion_r3933020862
##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/logical/utils/LogicalTypeCasts.java:
##########
@@ -218,7 +219,7 @@ public final class LogicalTypeCasts {
castTo(VARCHAR)
.implicitFromFamily(CHARACTER_STRING)
.explicitFromFamily(PREDEFINED, CONSTRUCTED)
- .explicitFrom(RAW, NULL, STRUCTURED_TYPE, BITMAP, VARIANT)
+ .explicitFrom(RAW, NULL, STRUCTURED_TYPE, BITMAP, VARIANT,
UUID)
Review Comment:
Is this in sync with Calcite? No implicit cast from UUID? I'm fine with the
current, I just wanted to double check here.
##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/logical/utils/LogicalTypeCasts.java:
##########
@@ -679,6 +689,13 @@ private static boolean supportsCasting(
// BITMAP can only be cast to BYTES (unbounded VARBINARY), because
trimming or padding
// would corrupt the serialized bitmap data.
return allowExplicit && getLength(targetType) ==
VarBinaryType.MAX_LENGTH;
+ } else if (sourceRoot == UUID && targetRoot == BINARY) {
+ // A UUID is exactly 16 bytes, so it maps to BINARY(16). Any other
width would pad or
+ // trim the value and thereby corrupt it.
+ return allowExplicit && getLength(targetType) == 16;
+ } else if (sourceRoot == UUID && targetRoot == VARBINARY) {
+ // A UUID can only be cast to BYTES (unbounded VARBINARY), for the
same reason.
+ return allowExplicit && getLength(targetType) ==
VarBinaryType.MAX_LENGTH;
Review Comment:
Doesn't need to be max length right? I guess BITMAP is var-length which
makes it tricky, but UUID is not.
##########
flink-table/flink-table-common/src/test/java/org/apache/flink/table/types/LogicalTypeCastsTest.java:
##########
@@ -297,7 +298,31 @@ private static Stream<Arguments> testData() {
new VariantType(),
new MapType(new IntType(), new CharType()),
false,
- false));
+ false),
+ // UUID casts are explicit only, in both directions
+ Arguments.of(new UuidType(), VarCharType.STRING_TYPE, false,
true),
+ Arguments.of(new UuidType(), new CharType(), false, true),
+ Arguments.of(VarCharType.STRING_TYPE, new UuidType(), false,
true),
+ Arguments.of(new CharType(), new UuidType(), false, true),
+ // UUID maps to its 16-byte encoding: only BINARY(16) and
BYTES, any other width is
+ // rejected because it would pad or trim the value
+ Arguments.of(new UuidType(), new BinaryType(16), false, true),
+ Arguments.of(new UuidType(), new BinaryType(10), false, false),
+ Arguments.of(
+ new UuidType(), new
VarBinaryType(VarBinaryType.MAX_LENGTH), false, true),
+ Arguments.of(new UuidType(), new VarBinaryType(16), false,
false),
Review Comment:
wrong test?
##########
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:
see comment above.
##########
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:
We should also at least test against a substring of the error message to
ensure the error is correct.
--
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]