gyfora commented on code in PR #28837:
URL: https://github.com/apache/flink/pull/28837#discussion_r3776205170


##########
flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/input/deserializer/PojoToRowDataDeserializer.java:
##########
@@ -0,0 +1,330 @@
+/*
+ * 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.api.input.deserializer;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.typeutils.TypeSerializer;
+import org.apache.flink.api.common.typeutils.TypeSerializerSnapshot;
+import 
org.apache.flink.api.java.typeutils.runtime.PojoDeserializerCompatibilitySnapshot;
+import org.apache.flink.api.java.typeutils.runtime.PojoSerializerSnapshot;
+import org.apache.flink.core.memory.DataInputView;
+import org.apache.flink.core.memory.DataOutputView;
+import 
org.apache.flink.state.api.schema.SerializerSnapshotToLogicalTypeConverter;
+import org.apache.flink.table.data.GenericRowData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.types.logical.LogicalType;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.AbstractMap;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * A {@link TypeSerializer} that reads the POJO binary format written by {@link
+ * org.apache.flink.api.java.typeutils.runtime.PojoSerializer} and produces 
{@link GenericRowData}.
+ *
+ * <p>This deserializer does <em>not</em> require the user POJO class to be on 
the classpath. It
+ * mirrors the exact binary protocol of {@code PojoSerializer}:
+ *
+ * <pre>{@code
+ * 1 byte: flags (bitmask)
+ *   0x01 IS_NULL            → value is null, return null
+ *   0x02 NO_SUBCLASS        → exact POJO class: read numFields × (isNull 
boolean + field bytes)
+ *   0x08 IS_TAGGED_SUBCLASS → 1 byte subclass tag; delegate to registered 
subclass deserializer
+ *   0x04 IS_SUBCLASS        → UTF class name (must be read); Kryo not 
supported → throws IOException
+ * }</pre>
+ *
+ * <p>Use {@link #create(PojoSerializerSnapshot)} to build an instance from a 
savepoint snapshot.
+ */
+@Internal
+public final class PojoToRowDataDeserializer extends TypeSerializer<RowData> {
+
+    private static final long serialVersionUID = 1L;
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(PojoToRowDataDeserializer.class);
+
+    // Mirrors constants in PojoSerializer
+    static final int IS_NULL = 0x01;
+    static final int NO_SUBCLASS = 0x02;
+    static final int IS_SUBCLASS = 0x04;
+    static final int IS_TAGGED_SUBCLASS = 0x08;
+
+    private final int numFields;
+    private final TypeSerializer<?>[] fieldDeserializers;
+    private final LogicalType[] fieldTypes;
+    private final String[] fieldNames;
+    private final List<PojoToRowDataDeserializer> 
registeredSubclassDeserializers;
+
+    /**
+     * Builds a {@link PojoToRowDataDeserializer} from a {@link 
PojoSerializerSnapshot}.
+     *
+     * <p>For each field:
+     *
+     * <ul>
+     *   <li>If the field snapshot is itself a {@link PojoSerializerSnapshot}, 
this method recurses
+     *       to build a nested {@link PojoToRowDataDeserializer}.
+     *   <li>For all other field types, the field's original serializer is 
restored via {@link
+     *       TypeSerializerSnapshot#restoreSerializer()}.
+     * </ul>
+     *
+     * <p>Registered subclasses are handled by building a deserializer for 
each registered subclass
+     * snapshot in order (matching the tag index used in the binary format).
+     *
+     * @throws IllegalStateException if a required field serializer snapshot 
is absent
+     */
+    public static PojoToRowDataDeserializer create(PojoSerializerSnapshot<?> 
snapshot) {
+        List<AbstractMap.SimpleEntry<String, TypeSerializerSnapshot<?>>> 
fieldEntries =
+                snapshot.getFieldSnapshotEntries();
+
+        List<TypeSerializer<?>> fieldDeserializerList = new 
ArrayList<>(fieldEntries.size());
+        List<LogicalType> fieldTypeList = new ArrayList<>(fieldEntries.size());
+        List<String> fieldNameList = new ArrayList<>(fieldEntries.size());
+
+        for (AbstractMap.SimpleEntry<String, TypeSerializerSnapshot<?>> entry 
: fieldEntries) {
+            String fieldName = entry.getKey();
+            TypeSerializerSnapshot<?> fieldSnapshot = entry.getValue();
+
+            if (fieldSnapshot == null) {

Review Comment:
   Good catch, added enum support



-- 
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]

Reply via email to