lihaosky commented on code in PR #26641:
URL: https://github.com/apache/flink/pull/26641#discussion_r2131482648


##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/serde/ResolvedCatalogModelJsonDeserializer.java:
##########
@@ -0,0 +1,96 @@
+/*
+ * 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.plan.nodes.exec.serde;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.api.Schema;
+import org.apache.flink.table.catalog.CatalogModel;
+import org.apache.flink.table.catalog.DefaultResolvedCatalogModel;
+import org.apache.flink.table.catalog.ResolvedCatalogModel;
+import org.apache.flink.table.catalog.ResolvedSchema;
+
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser;
+import 
org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.DeserializationContext;
+import 
org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.deser.std.StdDeserializer;
+import 
org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ObjectNode;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.Map;
+
+import static 
org.apache.flink.table.planner.plan.nodes.exec.serde.CompiledPlanSerdeUtil.deserializeOptionalField;
+import static 
org.apache.flink.table.planner.plan.nodes.exec.serde.CompiledPlanSerdeUtil.traverse;
+import static 
org.apache.flink.table.planner.plan.nodes.exec.serde.ResolvedCatalogModelJsonSerializer.COMMENT;
+import static 
org.apache.flink.table.planner.plan.nodes.exec.serde.ResolvedCatalogModelJsonSerializer.INPUT_SCHEMA;
+import static 
org.apache.flink.table.planner.plan.nodes.exec.serde.ResolvedCatalogModelJsonSerializer.OUTPUT_SCHEMA;
+import static 
org.apache.flink.table.planner.plan.nodes.exec.serde.ResolvedCatalogTableJsonSerializer.OPTIONS;

Review Comment:
   `ResolvedCatalogModelJsonSerializer.OPTIONS`



##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/serde/ContextResolvedModelJsonDeserializer.java:
##########
@@ -0,0 +1,244 @@
+/*
+ * 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.plan.nodes.exec.serde;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.api.TableException;
+import org.apache.flink.table.api.config.TableConfigOptions;
+import org.apache.flink.table.catalog.CatalogManager;
+import org.apache.flink.table.catalog.Column;
+import org.apache.flink.table.catalog.ContextResolvedModel;
+import org.apache.flink.table.catalog.ObjectIdentifier;
+import org.apache.flink.table.catalog.ResolvedCatalogModel;
+import org.apache.flink.table.catalog.ResolvedSchema;
+
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonPointer;
+import 
org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.DeserializationContext;
+import 
org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.deser.std.StdDeserializer;
+import 
org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ObjectNode;
+
+import javax.annotation.Nullable;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+
+import static 
org.apache.flink.table.api.config.TableConfigOptions.PLAN_COMPILE_CATALOG_OBJECTS;
+import static 
org.apache.flink.table.api.config.TableConfigOptions.PLAN_RESTORE_CATALOG_OBJECTS;
+import static 
org.apache.flink.table.planner.plan.nodes.exec.serde.CompiledPlanSerdeUtil.deserializeOptionalField;
+import static 
org.apache.flink.table.planner.plan.nodes.exec.serde.CompiledPlanSerdeUtil.traverse;
+import static 
org.apache.flink.table.planner.plan.nodes.exec.serde.ContextResolvedModelJsonSerializer.FIELD_NAME_CATALOG_MODEL;
+import static 
org.apache.flink.table.planner.plan.nodes.exec.serde.ContextResolvedModelJsonSerializer.FIELD_NAME_IDENTIFIER;
+import static 
org.apache.flink.table.planner.plan.nodes.exec.serde.ResolvedCatalogModelJsonSerializer.OPTIONS;
+
+/**
+ * JSON deserializer for {@link ContextResolvedModel}.
+ *
+ * @see ContextResolvedModelJsonSerializer for the reverse operation
+ */
+@Internal
+public class ContextResolvedModelJsonDeserializer extends 
StdDeserializer<ContextResolvedModel> {
+
+    private static final long serialVersionUID = 1L;
+
+    private static final JsonPointer optionsPointer =
+            JsonPointer.compile("/" + FIELD_NAME_CATALOG_MODEL + "/" + 
OPTIONS);
+
+    public ContextResolvedModelJsonDeserializer() {
+        super(ContextResolvedModel.class);
+    }
+
+    @Override
+    public ContextResolvedModel deserialize(JsonParser jsonParser, 
DeserializationContext ctx)
+            throws IOException {
+        final TableConfigOptions.CatalogPlanRestore planRestoreOption =
+                
SerdeContext.get(ctx).getConfiguration().get(PLAN_RESTORE_CATALOG_OBJECTS);
+        final CatalogManager catalogManager =
+                SerdeContext.get(ctx).getFlinkContext().getCatalogManager();
+        final ObjectNode objectNode = jsonParser.readValueAsTree();
+
+        final ObjectIdentifier identifier =
+                ctx.readValue(
+                        traverse(objectNode.required(FIELD_NAME_IDENTIFIER), 
jsonParser.getCodec()),
+                        ObjectIdentifier.class);
+        final @Nullable ResolvedCatalogModel restoredModel =
+                deserializeOptionalField(
+                                objectNode,
+                                FIELD_NAME_CATALOG_MODEL,
+                                ResolvedCatalogModel.class,
+                                jsonParser.getCodec(),
+                                ctx)
+                        .orElse(null);
+
+        final Optional<ContextResolvedModel> contextResolvedModelFromCatalog =
+                catalogManager.getModel(identifier);
+
+        // If plan has no catalog model field or no options field,
+        // the model is permanent in the catalog and the option is plan all 
enforced, then fail
+        if ((objectNode.at(optionsPointer).isMissingNode()
+                && isPlanEnforced(planRestoreOption)
+                && contextResolvedModelFromCatalog
+                        .map(ContextResolvedModel::isPermanent)
+                        .orElse(false))) {
+            throw lookupDisabled(identifier);
+        }
+
+        // If we have a schema from the plan and from the catalog, we need to 
check they match.
+        if (restoredModel != null && 
contextResolvedModelFromCatalog.isPresent()) {
+            ContextResolvedModel modelFromCatalog = 
contextResolvedModelFromCatalog.get();
+            if (!areResolvedSchemasEqual(
+                    restoredModel.getResolvedInputSchema(),
+                    
modelFromCatalog.getResolvedModel().getResolvedInputSchema())) {
+                throw schemaNotMatching(
+                        identifier,
+                        "input schema",
+                        restoredModel.getResolvedInputSchema(),
+                        
modelFromCatalog.getResolvedModel().getResolvedInputSchema());
+            }
+            if (!areResolvedSchemasEqual(
+                    restoredModel.getResolvedOutputSchema(),
+                    
modelFromCatalog.getResolvedModel().getResolvedOutputSchema())) {
+                throw schemaNotMatching(
+                        identifier,
+                        "output schema",
+                        restoredModel.getResolvedInputSchema(),
+                        
modelFromCatalog.getResolvedModel().getResolvedInputSchema());
+            }
+        }
+
+        if (restoredModel == null || isLookupForced(planRestoreOption)) {
+            return contextResolvedModelFromCatalog.orElseThrow(
+                    () -> missingModelFromCatalog(identifier, 
isLookupForced(planRestoreOption)));
+        }
+
+        if (contextResolvedModelFromCatalog.isPresent()) {
+            // If no config map is present, then the ContextResolvedTable was 
serialized with
+            // SCHEMA, so we just need to return the catalog query result
+            if (objectNode.at(optionsPointer).isMissingNode()) {
+                return contextResolvedModelFromCatalog.get();
+            }
+
+            return contextResolvedModelFromCatalog
+                    .flatMap(ContextResolvedModel::getCatalog)
+                    .map(c -> ContextResolvedModel.permanent(identifier, c, 
restoredModel))
+                    .orElseGet(() -> 
ContextResolvedModel.temporary(identifier, restoredModel));
+        }
+
+        return ContextResolvedModel.temporary(identifier, restoredModel);
+    }
+
+    private boolean isPlanEnforced(TableConfigOptions.CatalogPlanRestore 
planRestoreOption) {
+        return planRestoreOption == 
TableConfigOptions.CatalogPlanRestore.ALL_ENFORCED;
+    }
+
+    private boolean isLookupForced(TableConfigOptions.CatalogPlanRestore 
planRestoreOption) {
+        return planRestoreOption == 
TableConfigOptions.CatalogPlanRestore.IDENTIFIER;
+    }
+
+    private boolean areResolvedSchemasEqual(
+            ResolvedSchema schemaFromPlan, ResolvedSchema schemaFromCatalog) {
+        // For schema equality we check:
+        //  * Columns size and order
+        //  * For each column: name, kind (class) and type
+        final List<Column> columnsFromPlan = schemaFromPlan.getColumns();
+        final List<Column> columnsFromCatalog = schemaFromCatalog.getColumns();
+
+        if (columnsFromPlan.size() != columnsFromCatalog.size()) {
+            return false;
+        }
+
+        for (int i = 0; i < columnsFromPlan.size(); i++) {
+            final Column columnFromPlan = columnsFromPlan.get(i);
+            final Column columnFromCatalog = columnsFromCatalog.get(i);
+            if (!Objects.equals(columnFromPlan.getName(), 
columnFromCatalog.getName())
+                    || !Objects.equals(columnFromPlan.getClass(), 
columnFromCatalog.getClass())
+                    || !Objects.equals(
+                            columnFromPlan.getDataType(), 
columnFromCatalog.getDataType())) {
+                return false;
+            }
+        }
+
+        return true;
+    }
+
+    static TableException schemaNotMatching(

Review Comment:
   Also almost similar to `ContextResolvedTableJsonDeserializer`. Move to util 
if you want



##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/serde/ResolvedCatalogModelJsonSerializer.java:
##########
@@ -0,0 +1,87 @@
+/*
+ * 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.plan.nodes.exec.serde;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.catalog.ResolvedCatalogModel;
+
+import 
org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator;
+import 
org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.SerializerProvider;
+import 
org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ser.std.StdSerializer;
+
+import java.io.IOException;
+
+/**
+ * JSON deserializer for {@link ResolvedCatalogModel}.
+ *
+ * @see ResolvedCatalogModelJsonDeserializer for the reverse operation
+ */
+@Internal
+public class ResolvedCatalogModelJsonSerializer extends 
StdSerializer<ResolvedCatalogModel> {
+
+    private static final long serialVersionUID = 1L;
+
+    static final String SERIALIZE_OPTIONS = "serialize_options";
+
+    static final String INPUT_SCHEMA = "inputSchema";
+    static final String OUTPUT_SCHEMA = "outputSchema";
+    static final String OPTIONS = "options";
+

Review Comment:
   nit: remove empty space



##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecMLPredictTableFunction.java:
##########
@@ -301,9 +337,9 @@ private Transformation<RowData> createAsyncModelPredict(
                             
createTransformationMeta(ML_PREDICT_TRANSFORMATION, config),
                             new AsyncWaitOperatorFactory<>(
                                     asyncFunc,
-                                    asyncLookupOptions.asyncTimeout,
-                                    asyncLookupOptions.asyncBufferCapacity,
-                                    asyncLookupOptions.asyncOutputMode),
+                                    asyncOptions.asyncTimeout,

Review Comment:
   Why don't we need these data shuffling logic in `createModelPredict`? Looks 
in lookup exec node, it's in sync transformation creation



##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/serde/ContextResolvedModelJsonDeserializer.java:
##########
@@ -0,0 +1,244 @@
+/*
+ * 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.plan.nodes.exec.serde;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.api.TableException;
+import org.apache.flink.table.api.config.TableConfigOptions;
+import org.apache.flink.table.catalog.CatalogManager;
+import org.apache.flink.table.catalog.Column;
+import org.apache.flink.table.catalog.ContextResolvedModel;
+import org.apache.flink.table.catalog.ObjectIdentifier;
+import org.apache.flink.table.catalog.ResolvedCatalogModel;
+import org.apache.flink.table.catalog.ResolvedSchema;
+
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonPointer;
+import 
org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.DeserializationContext;
+import 
org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.deser.std.StdDeserializer;
+import 
org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ObjectNode;
+
+import javax.annotation.Nullable;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+
+import static 
org.apache.flink.table.api.config.TableConfigOptions.PLAN_COMPILE_CATALOG_OBJECTS;
+import static 
org.apache.flink.table.api.config.TableConfigOptions.PLAN_RESTORE_CATALOG_OBJECTS;
+import static 
org.apache.flink.table.planner.plan.nodes.exec.serde.CompiledPlanSerdeUtil.deserializeOptionalField;
+import static 
org.apache.flink.table.planner.plan.nodes.exec.serde.CompiledPlanSerdeUtil.traverse;
+import static 
org.apache.flink.table.planner.plan.nodes.exec.serde.ContextResolvedModelJsonSerializer.FIELD_NAME_CATALOG_MODEL;
+import static 
org.apache.flink.table.planner.plan.nodes.exec.serde.ContextResolvedModelJsonSerializer.FIELD_NAME_IDENTIFIER;
+import static 
org.apache.flink.table.planner.plan.nodes.exec.serde.ResolvedCatalogModelJsonSerializer.OPTIONS;
+
+/**
+ * JSON deserializer for {@link ContextResolvedModel}.
+ *
+ * @see ContextResolvedModelJsonSerializer for the reverse operation
+ */
+@Internal
+public class ContextResolvedModelJsonDeserializer extends 
StdDeserializer<ContextResolvedModel> {
+
+    private static final long serialVersionUID = 1L;
+
+    private static final JsonPointer optionsPointer =
+            JsonPointer.compile("/" + FIELD_NAME_CATALOG_MODEL + "/" + 
OPTIONS);
+
+    public ContextResolvedModelJsonDeserializer() {
+        super(ContextResolvedModel.class);
+    }
+
+    @Override
+    public ContextResolvedModel deserialize(JsonParser jsonParser, 
DeserializationContext ctx)
+            throws IOException {
+        final TableConfigOptions.CatalogPlanRestore planRestoreOption =
+                
SerdeContext.get(ctx).getConfiguration().get(PLAN_RESTORE_CATALOG_OBJECTS);
+        final CatalogManager catalogManager =
+                SerdeContext.get(ctx).getFlinkContext().getCatalogManager();
+        final ObjectNode objectNode = jsonParser.readValueAsTree();
+
+        final ObjectIdentifier identifier =
+                ctx.readValue(
+                        traverse(objectNode.required(FIELD_NAME_IDENTIFIER), 
jsonParser.getCodec()),
+                        ObjectIdentifier.class);
+        final @Nullable ResolvedCatalogModel restoredModel =
+                deserializeOptionalField(
+                                objectNode,
+                                FIELD_NAME_CATALOG_MODEL,
+                                ResolvedCatalogModel.class,
+                                jsonParser.getCodec(),
+                                ctx)
+                        .orElse(null);
+
+        final Optional<ContextResolvedModel> contextResolvedModelFromCatalog =
+                catalogManager.getModel(identifier);
+
+        // If plan has no catalog model field or no options field,
+        // the model is permanent in the catalog and the option is plan all 
enforced, then fail
+        if ((objectNode.at(optionsPointer).isMissingNode()
+                && isPlanEnforced(planRestoreOption)
+                && contextResolvedModelFromCatalog
+                        .map(ContextResolvedModel::isPermanent)
+                        .orElse(false))) {
+            throw lookupDisabled(identifier);
+        }
+
+        // If we have a schema from the plan and from the catalog, we need to 
check they match.
+        if (restoredModel != null && 
contextResolvedModelFromCatalog.isPresent()) {
+            ContextResolvedModel modelFromCatalog = 
contextResolvedModelFromCatalog.get();
+            if (!areResolvedSchemasEqual(
+                    restoredModel.getResolvedInputSchema(),
+                    
modelFromCatalog.getResolvedModel().getResolvedInputSchema())) {
+                throw schemaNotMatching(
+                        identifier,
+                        "input schema",
+                        restoredModel.getResolvedInputSchema(),
+                        
modelFromCatalog.getResolvedModel().getResolvedInputSchema());
+            }
+            if (!areResolvedSchemasEqual(
+                    restoredModel.getResolvedOutputSchema(),
+                    
modelFromCatalog.getResolvedModel().getResolvedOutputSchema())) {
+                throw schemaNotMatching(
+                        identifier,
+                        "output schema",
+                        restoredModel.getResolvedInputSchema(),
+                        
modelFromCatalog.getResolvedModel().getResolvedInputSchema());
+            }
+        }
+
+        if (restoredModel == null || isLookupForced(planRestoreOption)) {
+            return contextResolvedModelFromCatalog.orElseThrow(
+                    () -> missingModelFromCatalog(identifier, 
isLookupForced(planRestoreOption)));
+        }
+
+        if (contextResolvedModelFromCatalog.isPresent()) {
+            // If no config map is present, then the ContextResolvedTable was 
serialized with
+            // SCHEMA, so we just need to return the catalog query result
+            if (objectNode.at(optionsPointer).isMissingNode()) {
+                return contextResolvedModelFromCatalog.get();
+            }
+
+            return contextResolvedModelFromCatalog
+                    .flatMap(ContextResolvedModel::getCatalog)
+                    .map(c -> ContextResolvedModel.permanent(identifier, c, 
restoredModel))
+                    .orElseGet(() -> 
ContextResolvedModel.temporary(identifier, restoredModel));
+        }
+
+        return ContextResolvedModel.temporary(identifier, restoredModel);
+    }
+
+    private boolean isPlanEnforced(TableConfigOptions.CatalogPlanRestore 
planRestoreOption) {
+        return planRestoreOption == 
TableConfigOptions.CatalogPlanRestore.ALL_ENFORCED;
+    }
+
+    private boolean isLookupForced(TableConfigOptions.CatalogPlanRestore 
planRestoreOption) {
+        return planRestoreOption == 
TableConfigOptions.CatalogPlanRestore.IDENTIFIER;
+    }
+
+    private boolean areResolvedSchemasEqual(

Review Comment:
   Same code as the one from `ContextResolvedTableJsonDeserializer` except last 
line? Put in a util class? 



##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/serde/ContextResolvedModelJsonDeserializer.java:
##########
@@ -0,0 +1,244 @@
+/*
+ * 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.plan.nodes.exec.serde;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.api.TableException;
+import org.apache.flink.table.api.config.TableConfigOptions;
+import org.apache.flink.table.catalog.CatalogManager;
+import org.apache.flink.table.catalog.Column;
+import org.apache.flink.table.catalog.ContextResolvedModel;
+import org.apache.flink.table.catalog.ObjectIdentifier;
+import org.apache.flink.table.catalog.ResolvedCatalogModel;
+import org.apache.flink.table.catalog.ResolvedSchema;
+
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonPointer;
+import 
org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.DeserializationContext;
+import 
org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.deser.std.StdDeserializer;
+import 
org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ObjectNode;
+
+import javax.annotation.Nullable;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+
+import static 
org.apache.flink.table.api.config.TableConfigOptions.PLAN_COMPILE_CATALOG_OBJECTS;
+import static 
org.apache.flink.table.api.config.TableConfigOptions.PLAN_RESTORE_CATALOG_OBJECTS;
+import static 
org.apache.flink.table.planner.plan.nodes.exec.serde.CompiledPlanSerdeUtil.deserializeOptionalField;
+import static 
org.apache.flink.table.planner.plan.nodes.exec.serde.CompiledPlanSerdeUtil.traverse;
+import static 
org.apache.flink.table.planner.plan.nodes.exec.serde.ContextResolvedModelJsonSerializer.FIELD_NAME_CATALOG_MODEL;
+import static 
org.apache.flink.table.planner.plan.nodes.exec.serde.ContextResolvedModelJsonSerializer.FIELD_NAME_IDENTIFIER;
+import static 
org.apache.flink.table.planner.plan.nodes.exec.serde.ResolvedCatalogModelJsonSerializer.OPTIONS;
+
+/**
+ * JSON deserializer for {@link ContextResolvedModel}.
+ *
+ * @see ContextResolvedModelJsonSerializer for the reverse operation
+ */
+@Internal
+public class ContextResolvedModelJsonDeserializer extends 
StdDeserializer<ContextResolvedModel> {
+
+    private static final long serialVersionUID = 1L;
+
+    private static final JsonPointer optionsPointer =
+            JsonPointer.compile("/" + FIELD_NAME_CATALOG_MODEL + "/" + 
OPTIONS);
+
+    public ContextResolvedModelJsonDeserializer() {
+        super(ContextResolvedModel.class);
+    }
+
+    @Override
+    public ContextResolvedModel deserialize(JsonParser jsonParser, 
DeserializationContext ctx)
+            throws IOException {
+        final TableConfigOptions.CatalogPlanRestore planRestoreOption =
+                
SerdeContext.get(ctx).getConfiguration().get(PLAN_RESTORE_CATALOG_OBJECTS);
+        final CatalogManager catalogManager =
+                SerdeContext.get(ctx).getFlinkContext().getCatalogManager();
+        final ObjectNode objectNode = jsonParser.readValueAsTree();
+
+        final ObjectIdentifier identifier =
+                ctx.readValue(
+                        traverse(objectNode.required(FIELD_NAME_IDENTIFIER), 
jsonParser.getCodec()),
+                        ObjectIdentifier.class);
+        final @Nullable ResolvedCatalogModel restoredModel =
+                deserializeOptionalField(
+                                objectNode,
+                                FIELD_NAME_CATALOG_MODEL,
+                                ResolvedCatalogModel.class,
+                                jsonParser.getCodec(),
+                                ctx)
+                        .orElse(null);
+
+        final Optional<ContextResolvedModel> contextResolvedModelFromCatalog =
+                catalogManager.getModel(identifier);
+
+        // If plan has no catalog model field or no options field,
+        // the model is permanent in the catalog and the option is plan all 
enforced, then fail
+        if ((objectNode.at(optionsPointer).isMissingNode()
+                && isPlanEnforced(planRestoreOption)
+                && contextResolvedModelFromCatalog
+                        .map(ContextResolvedModel::isPermanent)
+                        .orElse(false))) {
+            throw lookupDisabled(identifier);
+        }
+
+        // If we have a schema from the plan and from the catalog, we need to 
check they match.
+        if (restoredModel != null && 
contextResolvedModelFromCatalog.isPresent()) {
+            ContextResolvedModel modelFromCatalog = 
contextResolvedModelFromCatalog.get();
+            if (!areResolvedSchemasEqual(
+                    restoredModel.getResolvedInputSchema(),
+                    
modelFromCatalog.getResolvedModel().getResolvedInputSchema())) {
+                throw schemaNotMatching(
+                        identifier,
+                        "input schema",
+                        restoredModel.getResolvedInputSchema(),
+                        
modelFromCatalog.getResolvedModel().getResolvedInputSchema());
+            }
+            if (!areResolvedSchemasEqual(
+                    restoredModel.getResolvedOutputSchema(),
+                    
modelFromCatalog.getResolvedModel().getResolvedOutputSchema())) {
+                throw schemaNotMatching(
+                        identifier,
+                        "output schema",
+                        restoredModel.getResolvedInputSchema(),
+                        
modelFromCatalog.getResolvedModel().getResolvedInputSchema());
+            }
+        }
+
+        if (restoredModel == null || isLookupForced(planRestoreOption)) {
+            return contextResolvedModelFromCatalog.orElseThrow(
+                    () -> missingModelFromCatalog(identifier, 
isLookupForced(planRestoreOption)));
+        }
+
+        if (contextResolvedModelFromCatalog.isPresent()) {
+            // If no config map is present, then the ContextResolvedTable was 
serialized with
+            // SCHEMA, so we just need to return the catalog query result
+            if (objectNode.at(optionsPointer).isMissingNode()) {
+                return contextResolvedModelFromCatalog.get();
+            }
+
+            return contextResolvedModelFromCatalog
+                    .flatMap(ContextResolvedModel::getCatalog)
+                    .map(c -> ContextResolvedModel.permanent(identifier, c, 
restoredModel))
+                    .orElseGet(() -> 
ContextResolvedModel.temporary(identifier, restoredModel));
+        }
+
+        return ContextResolvedModel.temporary(identifier, restoredModel);
+    }
+
+    private boolean isPlanEnforced(TableConfigOptions.CatalogPlanRestore 
planRestoreOption) {
+        return planRestoreOption == 
TableConfigOptions.CatalogPlanRestore.ALL_ENFORCED;
+    }
+
+    private boolean isLookupForced(TableConfigOptions.CatalogPlanRestore 
planRestoreOption) {
+        return planRestoreOption == 
TableConfigOptions.CatalogPlanRestore.IDENTIFIER;
+    }
+
+    private boolean areResolvedSchemasEqual(
+            ResolvedSchema schemaFromPlan, ResolvedSchema schemaFromCatalog) {
+        // For schema equality we check:
+        //  * Columns size and order
+        //  * For each column: name, kind (class) and type
+        final List<Column> columnsFromPlan = schemaFromPlan.getColumns();
+        final List<Column> columnsFromCatalog = schemaFromCatalog.getColumns();
+
+        if (columnsFromPlan.size() != columnsFromCatalog.size()) {
+            return false;
+        }
+
+        for (int i = 0; i < columnsFromPlan.size(); i++) {
+            final Column columnFromPlan = columnsFromPlan.get(i);
+            final Column columnFromCatalog = columnsFromCatalog.get(i);
+            if (!Objects.equals(columnFromPlan.getName(), 
columnFromCatalog.getName())
+                    || !Objects.equals(columnFromPlan.getClass(), 
columnFromCatalog.getClass())
+                    || !Objects.equals(
+                            columnFromPlan.getDataType(), 
columnFromCatalog.getDataType())) {
+                return false;
+            }
+        }
+
+        return true;
+    }
+
+    static TableException schemaNotMatching(
+            ObjectIdentifier objectIdentifier,
+            String schemaType,
+            ResolvedSchema schemaFromPlan,
+            ResolvedSchema schemaFromCatalog) {
+        return new TableException(
+                String.format(
+                        "The %s of model '%s' from the persisted plan does not 
match the "
+                                + "model loaded from the catalog: '%s' != 
'%s'. "
+                                + "Make sure the model %s in the catalog is 
still identical.",
+                        schemaType,
+                        objectIdentifier.asSummaryString(),
+                        schemaFromPlan,
+                        schemaFromCatalog,
+                        schemaType));
+    }
+
+    static TableException lookupDisabled(ObjectIdentifier objectIdentifier) {
+        return new TableException(
+                String.format(
+                        "The persisted plan does not include all required 
catalog metadata for model '%s'. "
+                                + "However, lookup is disabled because option 
'%s' = '%s'. "
+                                + "Either enable the catalog lookup with '%s' 
= '%s' / '%s' or "
+                                + "regenerate the plan with '%s' = '%s'. "
+                                + "Make sure the model is not compiled as a 
temporary model.",
+                        objectIdentifier.asSummaryString(),
+                        PLAN_RESTORE_CATALOG_OBJECTS.key(),
+                        
TableConfigOptions.CatalogPlanRestore.ALL_ENFORCED.name(),
+                        PLAN_RESTORE_CATALOG_OBJECTS.key(),
+                        
TableConfigOptions.CatalogPlanRestore.IDENTIFIER.name(),
+                        TableConfigOptions.CatalogPlanRestore.ALL.name(),
+                        PLAN_COMPILE_CATALOG_OBJECTS.key(),
+                        TableConfigOptions.CatalogPlanCompilation.ALL.name()));
+    }
+
+    static TableException missingModelFromCatalog(

Review Comment:
   ditto



##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecMLPredictTableFunction.java:
##########
@@ -256,21 +292,21 @@ private Transformation<RowData> createAsyncModelPredict(
                                 config, classLoader, null, inputRowType),
                         InternalSerializers.create(modelOutputType),
                         false,
-                        asyncLookupOptions.asyncBufferCapacity);
-        if (asyncLookupOptions.asyncOutputMode == 
AsyncDataStream.OutputMode.UNORDERED) {
+                        asyncOptions.asyncBufferCapacity);
+        if (asyncOptions.asyncOutputMode == 
AsyncDataStream.OutputMode.UNORDERED) {
             // The input stream is insert-only.
             return ExecNodeUtil.createOneInputTransformation(
                     inputTransformation,
                     createTransformationMeta(ML_PREDICT_TRANSFORMATION, 
config),
                     new AsyncWaitOperatorFactory<>(
                             asyncFunc,
-                            asyncLookupOptions.asyncTimeout,
-                            asyncLookupOptions.asyncBufferCapacity,
-                            asyncLookupOptions.asyncOutputMode),
+                            asyncOptions.asyncTimeout,
+                            asyncOptions.asyncBufferCapacity,
+                            asyncOptions.asyncOutputMode),
                     InternalTypeInfo.of(getOutputType()),
                     inputTransformation.getParallelism(),
                     false);
-        } else if (asyncLookupOptions.asyncOutputMode == 
AsyncDataStream.OutputMode.ORDERED) {
+        } else if (asyncOptions.asyncOutputMode == 
AsyncDataStream.OutputMode.ORDERED) {

Review Comment:
   I'm a bit confused about the shuffle logic. Looks shuffle is only done when 
`upsertMaterialize` is true in lookup join exec node. And `upsertMaterialize` 
seems to be always false for lookup join. Why doesn't lookup need to do it for 
cdc? Also async is disabled when `upsertMaterialize` for lookup join



##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/serde/ContextResolvedModelJsonDeserializer.java:
##########
@@ -0,0 +1,244 @@
+/*
+ * 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.plan.nodes.exec.serde;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.api.TableException;
+import org.apache.flink.table.api.config.TableConfigOptions;
+import org.apache.flink.table.catalog.CatalogManager;
+import org.apache.flink.table.catalog.Column;
+import org.apache.flink.table.catalog.ContextResolvedModel;
+import org.apache.flink.table.catalog.ObjectIdentifier;
+import org.apache.flink.table.catalog.ResolvedCatalogModel;
+import org.apache.flink.table.catalog.ResolvedSchema;
+
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonPointer;
+import 
org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.DeserializationContext;
+import 
org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.deser.std.StdDeserializer;
+import 
org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ObjectNode;
+
+import javax.annotation.Nullable;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+
+import static 
org.apache.flink.table.api.config.TableConfigOptions.PLAN_COMPILE_CATALOG_OBJECTS;
+import static 
org.apache.flink.table.api.config.TableConfigOptions.PLAN_RESTORE_CATALOG_OBJECTS;
+import static 
org.apache.flink.table.planner.plan.nodes.exec.serde.CompiledPlanSerdeUtil.deserializeOptionalField;
+import static 
org.apache.flink.table.planner.plan.nodes.exec.serde.CompiledPlanSerdeUtil.traverse;
+import static 
org.apache.flink.table.planner.plan.nodes.exec.serde.ContextResolvedModelJsonSerializer.FIELD_NAME_CATALOG_MODEL;
+import static 
org.apache.flink.table.planner.plan.nodes.exec.serde.ContextResolvedModelJsonSerializer.FIELD_NAME_IDENTIFIER;
+import static 
org.apache.flink.table.planner.plan.nodes.exec.serde.ResolvedCatalogModelJsonSerializer.OPTIONS;
+
+/**
+ * JSON deserializer for {@link ContextResolvedModel}.
+ *
+ * @see ContextResolvedModelJsonSerializer for the reverse operation
+ */
+@Internal
+public class ContextResolvedModelJsonDeserializer extends 
StdDeserializer<ContextResolvedModel> {
+
+    private static final long serialVersionUID = 1L;
+
+    private static final JsonPointer optionsPointer =
+            JsonPointer.compile("/" + FIELD_NAME_CATALOG_MODEL + "/" + 
OPTIONS);
+
+    public ContextResolvedModelJsonDeserializer() {
+        super(ContextResolvedModel.class);
+    }
+
+    @Override
+    public ContextResolvedModel deserialize(JsonParser jsonParser, 
DeserializationContext ctx)
+            throws IOException {
+        final TableConfigOptions.CatalogPlanRestore planRestoreOption =
+                
SerdeContext.get(ctx).getConfiguration().get(PLAN_RESTORE_CATALOG_OBJECTS);
+        final CatalogManager catalogManager =
+                SerdeContext.get(ctx).getFlinkContext().getCatalogManager();
+        final ObjectNode objectNode = jsonParser.readValueAsTree();
+
+        final ObjectIdentifier identifier =
+                ctx.readValue(
+                        traverse(objectNode.required(FIELD_NAME_IDENTIFIER), 
jsonParser.getCodec()),
+                        ObjectIdentifier.class);
+        final @Nullable ResolvedCatalogModel restoredModel =
+                deserializeOptionalField(
+                                objectNode,
+                                FIELD_NAME_CATALOG_MODEL,
+                                ResolvedCatalogModel.class,
+                                jsonParser.getCodec(),
+                                ctx)
+                        .orElse(null);
+
+        final Optional<ContextResolvedModel> contextResolvedModelFromCatalog =
+                catalogManager.getModel(identifier);
+
+        // If plan has no catalog model field or no options field,
+        // the model is permanent in the catalog and the option is plan all 
enforced, then fail
+        if ((objectNode.at(optionsPointer).isMissingNode()
+                && isPlanEnforced(planRestoreOption)
+                && contextResolvedModelFromCatalog
+                        .map(ContextResolvedModel::isPermanent)
+                        .orElse(false))) {
+            throw lookupDisabled(identifier);
+        }
+
+        // If we have a schema from the plan and from the catalog, we need to 
check they match.
+        if (restoredModel != null && 
contextResolvedModelFromCatalog.isPresent()) {
+            ContextResolvedModel modelFromCatalog = 
contextResolvedModelFromCatalog.get();
+            if (!areResolvedSchemasEqual(
+                    restoredModel.getResolvedInputSchema(),
+                    
modelFromCatalog.getResolvedModel().getResolvedInputSchema())) {
+                throw schemaNotMatching(
+                        identifier,
+                        "input schema",
+                        restoredModel.getResolvedInputSchema(),
+                        
modelFromCatalog.getResolvedModel().getResolvedInputSchema());
+            }
+            if (!areResolvedSchemasEqual(
+                    restoredModel.getResolvedOutputSchema(),
+                    
modelFromCatalog.getResolvedModel().getResolvedOutputSchema())) {
+                throw schemaNotMatching(
+                        identifier,
+                        "output schema",
+                        restoredModel.getResolvedInputSchema(),
+                        
modelFromCatalog.getResolvedModel().getResolvedInputSchema());
+            }
+        }
+
+        if (restoredModel == null || isLookupForced(planRestoreOption)) {
+            return contextResolvedModelFromCatalog.orElseThrow(
+                    () -> missingModelFromCatalog(identifier, 
isLookupForced(planRestoreOption)));
+        }
+
+        if (contextResolvedModelFromCatalog.isPresent()) {
+            // If no config map is present, then the ContextResolvedTable was 
serialized with
+            // SCHEMA, so we just need to return the catalog query result
+            if (objectNode.at(optionsPointer).isMissingNode()) {
+                return contextResolvedModelFromCatalog.get();
+            }
+
+            return contextResolvedModelFromCatalog
+                    .flatMap(ContextResolvedModel::getCatalog)
+                    .map(c -> ContextResolvedModel.permanent(identifier, c, 
restoredModel))
+                    .orElseGet(() -> 
ContextResolvedModel.temporary(identifier, restoredModel));
+        }
+
+        return ContextResolvedModel.temporary(identifier, restoredModel);
+    }
+
+    private boolean isPlanEnforced(TableConfigOptions.CatalogPlanRestore 
planRestoreOption) {
+        return planRestoreOption == 
TableConfigOptions.CatalogPlanRestore.ALL_ENFORCED;
+    }
+
+    private boolean isLookupForced(TableConfigOptions.CatalogPlanRestore 
planRestoreOption) {
+        return planRestoreOption == 
TableConfigOptions.CatalogPlanRestore.IDENTIFIER;
+    }
+
+    private boolean areResolvedSchemasEqual(
+            ResolvedSchema schemaFromPlan, ResolvedSchema schemaFromCatalog) {
+        // For schema equality we check:
+        //  * Columns size and order
+        //  * For each column: name, kind (class) and type
+        final List<Column> columnsFromPlan = schemaFromPlan.getColumns();
+        final List<Column> columnsFromCatalog = schemaFromCatalog.getColumns();
+
+        if (columnsFromPlan.size() != columnsFromCatalog.size()) {
+            return false;
+        }
+
+        for (int i = 0; i < columnsFromPlan.size(); i++) {
+            final Column columnFromPlan = columnsFromPlan.get(i);
+            final Column columnFromCatalog = columnsFromCatalog.get(i);
+            if (!Objects.equals(columnFromPlan.getName(), 
columnFromCatalog.getName())
+                    || !Objects.equals(columnFromPlan.getClass(), 
columnFromCatalog.getClass())
+                    || !Objects.equals(
+                            columnFromPlan.getDataType(), 
columnFromCatalog.getDataType())) {
+                return false;
+            }
+        }
+
+        return true;
+    }
+
+    static TableException schemaNotMatching(
+            ObjectIdentifier objectIdentifier,
+            String schemaType,
+            ResolvedSchema schemaFromPlan,
+            ResolvedSchema schemaFromCatalog) {
+        return new TableException(
+                String.format(
+                        "The %s of model '%s' from the persisted plan does not 
match the "
+                                + "model loaded from the catalog: '%s' != 
'%s'. "
+                                + "Make sure the model %s in the catalog is 
still identical.",
+                        schemaType,
+                        objectIdentifier.asSummaryString(),
+                        schemaFromPlan,
+                        schemaFromCatalog,
+                        schemaType));
+    }
+
+    static TableException lookupDisabled(ObjectIdentifier objectIdentifier) {

Review Comment:
   ditto



##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/serde/ContextResolvedModelJsonDeserializer.java:
##########
@@ -0,0 +1,244 @@
+/*
+ * 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.plan.nodes.exec.serde;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.api.TableException;
+import org.apache.flink.table.api.config.TableConfigOptions;
+import org.apache.flink.table.catalog.CatalogManager;
+import org.apache.flink.table.catalog.Column;
+import org.apache.flink.table.catalog.ContextResolvedModel;
+import org.apache.flink.table.catalog.ObjectIdentifier;
+import org.apache.flink.table.catalog.ResolvedCatalogModel;
+import org.apache.flink.table.catalog.ResolvedSchema;
+
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonPointer;
+import 
org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.DeserializationContext;
+import 
org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.deser.std.StdDeserializer;
+import 
org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ObjectNode;
+
+import javax.annotation.Nullable;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+
+import static 
org.apache.flink.table.api.config.TableConfigOptions.PLAN_COMPILE_CATALOG_OBJECTS;
+import static 
org.apache.flink.table.api.config.TableConfigOptions.PLAN_RESTORE_CATALOG_OBJECTS;
+import static 
org.apache.flink.table.planner.plan.nodes.exec.serde.CompiledPlanSerdeUtil.deserializeOptionalField;
+import static 
org.apache.flink.table.planner.plan.nodes.exec.serde.CompiledPlanSerdeUtil.traverse;
+import static 
org.apache.flink.table.planner.plan.nodes.exec.serde.ContextResolvedModelJsonSerializer.FIELD_NAME_CATALOG_MODEL;
+import static 
org.apache.flink.table.planner.plan.nodes.exec.serde.ContextResolvedModelJsonSerializer.FIELD_NAME_IDENTIFIER;
+import static 
org.apache.flink.table.planner.plan.nodes.exec.serde.ResolvedCatalogModelJsonSerializer.OPTIONS;
+
+/**
+ * JSON deserializer for {@link ContextResolvedModel}.
+ *
+ * @see ContextResolvedModelJsonSerializer for the reverse operation
+ */
+@Internal
+public class ContextResolvedModelJsonDeserializer extends 
StdDeserializer<ContextResolvedModel> {
+
+    private static final long serialVersionUID = 1L;
+
+    private static final JsonPointer optionsPointer =
+            JsonPointer.compile("/" + FIELD_NAME_CATALOG_MODEL + "/" + 
OPTIONS);
+
+    public ContextResolvedModelJsonDeserializer() {
+        super(ContextResolvedModel.class);
+    }
+
+    @Override
+    public ContextResolvedModel deserialize(JsonParser jsonParser, 
DeserializationContext ctx)
+            throws IOException {
+        final TableConfigOptions.CatalogPlanRestore planRestoreOption =
+                
SerdeContext.get(ctx).getConfiguration().get(PLAN_RESTORE_CATALOG_OBJECTS);
+        final CatalogManager catalogManager =
+                SerdeContext.get(ctx).getFlinkContext().getCatalogManager();
+        final ObjectNode objectNode = jsonParser.readValueAsTree();
+
+        final ObjectIdentifier identifier =
+                ctx.readValue(
+                        traverse(objectNode.required(FIELD_NAME_IDENTIFIER), 
jsonParser.getCodec()),
+                        ObjectIdentifier.class);
+        final @Nullable ResolvedCatalogModel restoredModel =
+                deserializeOptionalField(
+                                objectNode,
+                                FIELD_NAME_CATALOG_MODEL,
+                                ResolvedCatalogModel.class,
+                                jsonParser.getCodec(),
+                                ctx)
+                        .orElse(null);
+
+        final Optional<ContextResolvedModel> contextResolvedModelFromCatalog =
+                catalogManager.getModel(identifier);
+
+        // If plan has no catalog model field or no options field,
+        // the model is permanent in the catalog and the option is plan all 
enforced, then fail
+        if ((objectNode.at(optionsPointer).isMissingNode()
+                && isPlanEnforced(planRestoreOption)
+                && contextResolvedModelFromCatalog
+                        .map(ContextResolvedModel::isPermanent)
+                        .orElse(false))) {
+            throw lookupDisabled(identifier);
+        }
+
+        // If we have a schema from the plan and from the catalog, we need to 
check they match.
+        if (restoredModel != null && 
contextResolvedModelFromCatalog.isPresent()) {
+            ContextResolvedModel modelFromCatalog = 
contextResolvedModelFromCatalog.get();
+            if (!areResolvedSchemasEqual(
+                    restoredModel.getResolvedInputSchema(),
+                    
modelFromCatalog.getResolvedModel().getResolvedInputSchema())) {
+                throw schemaNotMatching(
+                        identifier,
+                        "input schema",
+                        restoredModel.getResolvedInputSchema(),
+                        
modelFromCatalog.getResolvedModel().getResolvedInputSchema());
+            }
+            if (!areResolvedSchemasEqual(
+                    restoredModel.getResolvedOutputSchema(),
+                    
modelFromCatalog.getResolvedModel().getResolvedOutputSchema())) {
+                throw schemaNotMatching(
+                        identifier,
+                        "output schema",
+                        restoredModel.getResolvedInputSchema(),
+                        
modelFromCatalog.getResolvedModel().getResolvedInputSchema());
+            }
+        }
+
+        if (restoredModel == null || isLookupForced(planRestoreOption)) {
+            return contextResolvedModelFromCatalog.orElseThrow(
+                    () -> missingModelFromCatalog(identifier, 
isLookupForced(planRestoreOption)));
+        }
+
+        if (contextResolvedModelFromCatalog.isPresent()) {
+            // If no config map is present, then the ContextResolvedTable was 
serialized with

Review Comment:
   `ContextResolvedModel`



##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/serde/ResolvedCatalogModelJsonSerializer.java:
##########
@@ -0,0 +1,87 @@
+/*
+ * 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.plan.nodes.exec.serde;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.catalog.ResolvedCatalogModel;
+
+import 
org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator;
+import 
org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.SerializerProvider;
+import 
org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ser.std.StdSerializer;
+
+import java.io.IOException;
+
+/**
+ * JSON deserializer for {@link ResolvedCatalogModel}.
+ *
+ * @see ResolvedCatalogModelJsonDeserializer for the reverse operation
+ */
+@Internal
+public class ResolvedCatalogModelJsonSerializer extends 
StdSerializer<ResolvedCatalogModel> {
+
+    private static final long serialVersionUID = 1L;
+
+    static final String SERIALIZE_OPTIONS = "serialize_options";
+
+    static final String INPUT_SCHEMA = "inputSchema";
+    static final String OUTPUT_SCHEMA = "outputSchema";
+    static final String OPTIONS = "options";
+
+    static final String COMMENT = "comment";
+
+    public ResolvedCatalogModelJsonSerializer() {
+        super(ResolvedCatalogModel.class);
+    }
+
+    @Override
+    public void serialize(
+            ResolvedCatalogModel model,
+            JsonGenerator jsonGenerator,
+            SerializerProvider serializerProvider)
+            throws IOException {
+        boolean serializeOptions =
+                serializerProvider.getAttribute(SERIALIZE_OPTIONS) == null
+                        || (boolean) 
serializerProvider.getAttribute(SERIALIZE_OPTIONS);
+        serialize(model, serializeOptions, jsonGenerator, serializerProvider);
+    }
+
+    static void serialize(
+            ResolvedCatalogModel resolvedCatalogModel,
+            boolean serializeOptions,
+            JsonGenerator jsonGenerator,
+            SerializerProvider serializerProvider)
+            throws IOException {
+        jsonGenerator.writeStartObject();
+
+        serializerProvider.defaultSerializeField(
+                INPUT_SCHEMA, resolvedCatalogModel.getResolvedInputSchema(), 
jsonGenerator);
+        serializerProvider.defaultSerializeField(
+                OUTPUT_SCHEMA, resolvedCatalogModel.getResolvedOutputSchema(), 
jsonGenerator);
+        if (serializeOptions) {
+            serializerProvider.defaultSerializeField(
+                    OPTIONS, resolvedCatalogModel.getOptions(), jsonGenerator);

Review Comment:
   For table, there's a try/catch, curious why no catch here?



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