yanand0909 commented on code in PR #26385: URL: https://github.com/apache/flink/pull/26385#discussion_r2124904617
########## flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/ModelDescriptor.java: ########## @@ -0,0 +1,234 @@ +/* + * 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. */ + Map<String, String> getOptions() { + return Map.copyOf(modelOptions); + } + + /** Get the unresolved input schema of the model. */ + Optional<Schema> getInputSchema() { + return Optional.ofNullable(inputSchema); + } + + /** Get the unresolved output schema of the model. */ + Optional<Schema> getOutputSchema() { + return Optional.ofNullable(outputSchema); + } + + /** Get 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); Review Comment: Hi Sergey, thanks for pointing out the discrepancy in FLIP. I have update the flip to be in consistency of what we want. To make you understand it better we want to keep schema nullable to be able to drive the schemas from data source in future similar to what we are doing in CREATE TABLE AS for table but we have still not thought through how to implement that for model so we don't have any immediate use case for that but for future use cases we want to keep these fields nullable. > We don't need to consider the API how we want to do it for models but at least we should already prepare ModelDescriptor for it. Thus making at least the model schemas optional This is what was suggested by timo while discussing if we want to make the schemas optional. -- 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]
