raminqaf commented on code in PR #29311: URL: https://github.com/apache/flink/pull/29311#discussion_r4153455076
########## flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/functions/casting/PrimitiveToVariantCastRule.java: ########## @@ -0,0 +1,175 @@ +/* + * 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.runtime.functions.VariantCastUtils; +import org.apache.flink.table.types.logical.LogicalType; +import org.apache.flink.table.types.logical.LogicalTypeRoot; +import org.apache.flink.table.types.logical.utils.LogicalTypeChecks; +import org.apache.flink.types.variant.Variant; + +import static org.apache.flink.table.planner.functions.casting.CastRuleUtils.staticCall; + +/** + * Cast rule from a primitive type to {@link LogicalTypeRoot#VARIANT}. + * + * <p>The value keeps the kind of its SQL type. For example, a {@code BIGINT} is stored as a {@code + * BIGINT}, even when it would fit a smaller kind. A {@code NaN} or infinite {@code FLOAT} or {@code + * DOUBLE} is stored as is, although {@code PARSE_JSON} rejects it. Some types can fail for some + * values, see {@link #canFail}. + */ +class PrimitiveToVariantCastRule extends AbstractExpressionCodeGeneratorCastRule<Object, Variant> { + + static final PrimitiveToVariantCastRule INSTANCE = new PrimitiveToVariantCastRule(); + + /** A character takes up to 4 bytes in UTF-8, which a declared length counts as one. */ + private static final int MAX_UTF8_BYTES_PER_CHAR = 4; + + private PrimitiveToVariantCastRule() { + super( + CastRulePredicate.builder() + .predicate( + (input, target) -> + target.is(LogicalTypeRoot.VARIANT) + && isSupportedSource(input)) + .build()); + } + + private static boolean isSupportedSource(LogicalType inputType) { + switch (inputType.getTypeRoot()) { + case BOOLEAN: + case TINYINT: + case SMALLINT: + case INTEGER: + case BIGINT: + case FLOAT: + case DOUBLE: + case DECIMAL: + case CHAR: + case VARCHAR: + case BINARY: + case VARBINARY: + case DATE: + case TIME_WITHOUT_TIME_ZONE: + case TIMESTAMP_WITHOUT_TIME_ZONE: + case TIMESTAMP_WITH_LOCAL_TIME_ZONE: + case UUID: + return true; + default: + return false; + } + } + + /** + * Returns whether a value of the input type can fail the cast, so that {@code TRY_CAST} returns + * {@code NULL} for it instead of failing. Two kinds of types can fail: + * + * <ul> + * <li>A {@code TIMESTAMP(p)} or {@code TIMESTAMP_LTZ(p)} with {@code p > 6} is stored with + * nanoseconds, which only cover 1677-09-21 to 2262-04-11. + * <li>A string or binary value over {@link VariantCastUtils#MAX_PAYLOAD_BYTES} does not fit + * into a {@code VARIANT}. Only a type whose declared length allows such a value can fail. + * </ul> + * + * <p>Every other type never fails. + */ + @Override + public boolean canFail(LogicalType inputLogicalType, LogicalType targetLogicalType) { Review Comment: Done. The `canFail` Javadoc now says that the check trusts the declared length, and that `TRY_CAST` fails for a longer value from a source. -- 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]
