snuyanzin commented on code in PR #26385:
URL: https://github.com/apache/flink/pull/26385#discussion_r2031879610


##########
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/ModelDescriptor.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.api;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.configuration.ConfigOption;
+import org.apache.flink.configuration.ConfigurationUtils;
+import org.apache.flink.table.catalog.CatalogModel;
+import org.apache.flink.table.factories.FactoryUtil;
+import org.apache.flink.table.utils.EncodingUtils;
+import org.apache.flink.util.Preconditions;
+
+import javax.annotation.Nullable;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.stream.Collectors;
+
+/**
+ * Describes a {@link CatalogModel} representing a model.
+ *
+ * <p>A {@link ModelDescriptor} is a template for creating a {@link 
CatalogModel} instance. It
+ * closely resembles the "CREATE MODEL" SQL DDL statement, containing input 
schema, output schema,
+ * and other characteristics.
+ *
+ * <p>This can be used to register a Model in the Table API.
+ */
+@PublicEvolving
+public class ModelDescriptor {
+    private final @Nullable Schema inputSchema;
+    private final @Nullable Schema outputSchema;
+    private final Map<String, String> modelOptions;
+    private final @Nullable String comment;
+
+    protected ModelDescriptor(
+            @Nullable Schema inputSchema,
+            @Nullable Schema outputSchema,
+            Map<String, String> modelOptions,
+            @Nullable String comment) {
+        this.inputSchema = inputSchema;
+        this.outputSchema = outputSchema;
+        this.modelOptions = modelOptions;
+        this.comment = comment;
+    }
+
+    /** Converts this descriptor into a {@link CatalogModel}. */
+    public CatalogModel toCatalogModel() {
+        final Schema inputSchema =
+                getInputSchema()
+                        .orElseThrow(
+                                () ->
+                                        new ValidationException(
+                                                "Input schema missing in 
ModelDescriptor. Input schema cannot be null."));
+        final Schema outputSchema =
+                getOutputSchema()
+                        .orElseThrow(
+                                () ->
+                                        new ValidationException(
+                                                "Output schema missing in 
ModelDescriptor. Output schema cannot be null."));
+        return CatalogModel.of(inputSchema, outputSchema, modelOptions, 
comment);
+    }
+
+    /** Converts this immutable instance into a mutable {@link Builder}. */
+    public Builder toBuilder() {
+        return new Builder(this);
+    }
+
+    /**
+     * Returns a map of string-based model options.
+     *
+     * @return options of the model.
+     */
+    Map<String, String> getOptions() {
+        return modelOptions;
+    }
+
+    /**
+     * Get the unresolved input schema of the model.
+     *
+     * @return unresolved input schema of the model.
+     */
+    Optional<Schema> getInputSchema() {
+        return Optional.ofNullable(inputSchema);
+    }
+
+    /**
+     * Get the unresolved output schema of the model.
+     *
+     * @return unresolved output schema of the model.
+     */
+    Optional<Schema> getOutputSchema() {
+        return Optional.ofNullable(outputSchema);
+    }
+
+    /**
+     * Get comment of the model.
+     *
+     * @return comment of the model.
+     */
+    Optional<String> getComment() {
+        return Optional.ofNullable(comment);
+    }
+
+    /**
+     * Creates a new {@link Builder} for the model with the given provider 
option.
+     *
+     * @param provider string value of provider for the model.
+     */
+    public static Builder forProvider(String provider) {
+        Preconditions.checkNotNull(provider, "Model descriptors require a 
provider value.");
+        final Builder descriptorBuilder = new Builder();
+        descriptorBuilder.option(FactoryUtil.MODEL_PROVIDER, provider);
+        return descriptorBuilder;
+    }
+
+    @Override
+    public String toString() {
+        final String serializedOptions =
+                modelOptions.entrySet().stream()
+                        .map(
+                                entry ->
+                                        String.format(
+                                                "  '%s' = '%s'",
+                                                
EncodingUtils.escapeSingleQuotes(entry.getKey()),
+                                                
EncodingUtils.escapeSingleQuotes(entry.getValue())))
+                        .collect(Collectors.joining(String.format(",%n")));
+
+        return String.format(
+                "%s%n%s%nCOMMENT '%s'%nWITH (%n%s%n)",
+                inputSchema, outputSchema, comment != null ? comment : "", 
serializedOptions);
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
+        ModelDescriptor that = (ModelDescriptor) o;
+        return Objects.equals(inputSchema, that.inputSchema)
+                && Objects.equals(outputSchema, that.outputSchema)
+                && modelOptions.equals(that.modelOptions)
+                && Objects.equals(comment, that.comment);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(inputSchema, outputSchema, modelOptions, comment);
+    }
+
+    /** Builder for {@link ModelDescriptor}. */
+    @PublicEvolving
+    public static class Builder {
+        private @Nullable Schema inputSchema;
+        private @Nullable Schema outputSchema;
+        private final Map<String, String> modelOptions;
+        private @Nullable String comment;
+
+        protected Builder() {

Review Comment:
   is there a reason to have it `protected` rather than `private`?



-- 
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: issues-unsubscr...@flink.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to