raminqaf commented on code in PR #29311: URL: https://github.com/apache/flink/pull/29311#discussion_r4152607883
########## flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/functions/casting/PrimitiveToVariantCastRule.java: ########## @@ -0,0 +1,149 @@ +/* + * 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; + +/** + * Primitive type to {@link LogicalTypeRoot#VARIANT} cast rule. + * + * <p>The value keeps the kind of its SQL type, so a {@code BIGINT} is stored as a {@code BIGINT} + * even when it would fit a smaller one. Unlike {@code PARSE_JSON}, a {@code NaN} or infinite {@code + * FLOAT} or {@code DOUBLE} is accepted, since a {@code VARIANT} is not limited to what JSON can + * express. A timestamp keeps its declared precision, so a {@code TIMESTAMP(p)} with {@code p} above + * 6 is stored with nanoseconds. That kind only covers 1677-09-21 to 2262-04-11, so such a cast + * fails for a value outside that range. Every other cast never fails. + */ +class PrimitiveToVariantCastRule extends AbstractExpressionCodeGeneratorCastRule<Object, Variant> { + + static final PrimitiveToVariantCastRule INSTANCE = new PrimitiveToVariantCastRule(); + + /** The highest precision a VARIANT stores with microseconds, above it nanoseconds are used. */ + private static final int MAX_MICROS_PRECISION = 6; + + 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; + } + } + + @Override + public boolean canFail(LogicalType inputLogicalType, LogicalType targetLogicalType) { Review Comment: Good catch, thanks. `canFail` now returns true for CHAR and VARCHAR when 4 bytes per character can exceed 16,777,211 bytes, and for BINARY and VARBINARY when the length can. So `VARCHAR(100)` stays non-failing, while `STRING` and `BYTES` are fallible. -- 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]
