gaborgsomogyi commented on code in PR #27340: URL: https://github.com/apache/flink/pull/27340#discussion_r2840465166
########## flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/table/SavepointTypeInfoResolver.java: ########## @@ -0,0 +1,493 @@ +/* + * 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.state.table; + +import org.apache.flink.api.common.serialization.SerializerConfig; +import org.apache.flink.api.common.typeinfo.TypeInformation; +import org.apache.flink.api.common.typeinfo.utils.TypeUtils; +import org.apache.flink.api.common.typeutils.TypeSerializer; +import org.apache.flink.api.common.typeutils.TypeSerializerSnapshot; +import org.apache.flink.api.common.typeutils.base.MapSerializer; +import org.apache.flink.configuration.ConfigOption; +import org.apache.flink.configuration.Configuration; +import org.apache.flink.runtime.state.metainfo.StateMetaInfoSnapshot; +import org.apache.flink.table.types.logical.ArrayType; +import org.apache.flink.table.types.logical.LogicalType; +import org.apache.flink.table.types.logical.MapType; +import org.apache.flink.table.types.logical.RowType; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.math.BigDecimal; +import java.util.Map; +import java.util.Optional; + +import static org.apache.flink.state.table.SavepointConnectorOptions.FIELDS; +import static org.apache.flink.state.table.SavepointConnectorOptions.VALUE_CLASS; + +/** Resolver for TypeInformation from savepoint metadata and configuration. */ +public class SavepointTypeInfoResolver { + + private static final Logger LOG = LoggerFactory.getLogger(SavepointTypeInfoResolver.class); + + /** Context for type inference to determine what aspect of the type we need. */ + public enum InferenceContext { + /** Inferring the key type of keyed state (always primitive). */ + KEY, + /** Inferring the key type of a MAP state. */ + MAP_KEY, + /** Inferring the value type (behavior depends on logical type). */ + VALUE + } + + private final Map<String, StateMetaInfoSnapshot> preloadedStateMetadata; + private final SerializerConfig serializerConfig; + + public SavepointTypeInfoResolver( + Map<String, StateMetaInfoSnapshot> preloadedStateMetadata, + SerializerConfig serializerConfig) { + this.preloadedStateMetadata = preloadedStateMetadata; + this.serializerConfig = serializerConfig; + } + + /** + * Resolves TypeInformation for keyed state keys (primitive types only). + * + * <p>This is a simplified version of type resolution specifically for key types, which are + * always primitive and don't require complex metadata inference. + * + * @param options Configuration containing table options + * @param classOption Config option for explicit class specification + * @param typeInfoFactoryOption Config option for type factory specification + * @param rowField The row field containing name and LogicalType + * @return The resolved TypeInformation for the key + * @throws IllegalArgumentException If both class and factory options are specified + * @throws RuntimeException If type instantiation fails + */ + public TypeInformation<?> resolveKeyType( + Configuration options, + ConfigOption<String> classOption, + ConfigOption<String> typeInfoFactoryOption, + RowType.RowField rowField) { + try { + // Priority 1: Explicit configuration (backward compatibility) + TypeInformation<?> explicitTypeInfo = + getExplicitTypeInfo(options, classOption, typeInfoFactoryOption); + if (explicitTypeInfo != null) { + return explicitTypeInfo; + } + + // Priority 2: Simple primitive type inference from LogicalType + LogicalType logicalType = rowField.getType(); + String columnName = rowField.getName(); + return TypeInformation.of(getPrimitiveClass(logicalType, columnName)); + } catch (ReflectiveOperationException e) { + throw new RuntimeException(e); + } + } + + /** + * Resolves TypeSerializer for a table field using a three-tier priority system with direct + * serializer extraction for metadata inference. + * + * <h3>Three-Tier Priority System (Serializer-First)</h3> + * + * <ol> + * <li><strong>Priority 1: Explicit Configuration</strong> (Highest priority) <br> + * Uses user-specified class name or type factory from table options, then converts to + * serializer. + * <li><strong>Priority 2: Metadata Inference</strong> <br> + * Directly extracts serializers from preloaded savepoint metadata (NO TypeInformation + * conversion). + * <li><strong>Priority 3: LogicalType Fallback</strong> (Lowest priority) <br> + * Infers TypeInformation from table schema's LogicalType, then converts to serializer. + * </ol> + * + * <p>This approach eliminates TypeInformation extraction complexity for metadata inference, + * making it work with ANY serializer type (Avro, custom types, etc.). + * + * @param options Configuration containing table options + * @param classOption Config option for explicit class specification + * @param typeInfoFactoryOption Config option for type factory specification + * @param rowField The table field containing name and LogicalType + * @param inferStateType Whether to enable automatic type inference. If false, returns null when + * no explicit configuration is provided. + * @param context The inference context determining what type aspect to extract. + * @return The resolved TypeSerializer, or null if inferStateType is false and no explicit + * configuration is provided. + * @throws IllegalArgumentException If both class and factory options are specified + * @throws RuntimeException If serializer creation fails + */ + public TypeSerializer<?> resolveSerializer( + Configuration options, + ConfigOption<String> classOption, + ConfigOption<String> typeInfoFactoryOption, + RowType.RowField rowField, + boolean inferStateType, + InferenceContext context) { + try { + // Priority 1: Explicit configuration (backward compatibility) + TypeInformation<?> explicitTypeInfo = + getExplicitTypeInfo(options, classOption, typeInfoFactoryOption); + if (explicitTypeInfo != null) { + return explicitTypeInfo.createSerializer(serializerConfig); + } + + if (inferStateType) { Review Comment: Changed. -- 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]
