Timm0 commented on code in PR #30207: URL: https://github.com/apache/flink/pull/30207#discussion_r4752898431
########## flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/internal/materializedtable/MaterializedTableEvolutionImpl.java: ########## @@ -0,0 +1,192 @@ +/* + * 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.internal.materializedtable; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.annotation.VisibleForTesting; +import org.apache.flink.configuration.ConfigOption; +import org.apache.flink.configuration.ConfigurationUtils; +import org.apache.flink.table.api.ExplainDetail; +import org.apache.flink.table.api.ExplainFormat; +import org.apache.flink.table.api.MaterializedTable; +import org.apache.flink.table.api.MaterializedTableEvolution; +import org.apache.flink.table.api.Schema; +import org.apache.flink.table.api.Table; +import org.apache.flink.table.api.ValidationException; +import org.apache.flink.table.api.internal.TableEnvironmentInternal; +import org.apache.flink.table.api.internal.TableImpl; +import org.apache.flink.table.catalog.ResolvedCatalogMaterializedTable; +import org.apache.flink.table.factories.FactoryUtil; +import org.apache.flink.table.operations.QueryOperation; +import org.apache.flink.table.operations.materializedtable.AlterMaterializedTableChangeOperation; +import org.apache.flink.table.operations.materializedtable.MaterializedTableOperationFactory; +import org.apache.flink.util.Preconditions; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.annotation.Nullable; + +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +import static org.apache.flink.table.operations.materializedtable.MaterializedTableDefinitionUtils.parseQuery; +import static org.apache.flink.table.operations.materializedtable.MaterializedTableDefinitionUtils.serializeQuery; + +/** Implementation of {@link MaterializedTableEvolution}. */ +@Internal +final class MaterializedTableEvolutionImpl implements MaterializedTableEvolution { + + private static final Logger LOG = LoggerFactory.getLogger(MaterializedTableEvolutionImpl.class); + + private final TableEnvironmentInternal tableEnvironment; + private final MaterializedTableImpl materializedTable; + private @Nullable String newSerializedQuery; + + /** The last edit of each option: its new value, or empty for a reset. */ + private final Map<String, Optional<String>> optionEdits = new LinkedHashMap<>(); + + MaterializedTableEvolutionImpl( + TableEnvironmentInternal tableEnvironment, MaterializedTableImpl materializedTable) { + this.tableEnvironment = tableEnvironment; + this.materializedTable = materializedTable; + } + + @Override + public MaterializedTableEvolution query(Table query) { + Preconditions.checkNotNull(query, "Query must not be null."); + if (((TableImpl) query).getTableEnvironment() != tableEnvironment) { + throw new ValidationException( + "Only tables from the same TableEnvironment can be used as the query of a materialized table."); + } + newSerializedQuery = + serializeQuery(query.getQueryOperation(), tableEnvironment.getCatalogManager()); + return this; + } + + @Override + public MaterializedTableEvolution query(String query) { + Preconditions.checkNotNull(query, "Query must not be null."); + return query(tableEnvironment.sqlQuery(query)); + } + + @Override + public MaterializedTableEvolution option(String key, String value) { + Preconditions.checkNotNull(key, "Key must not be null."); + Preconditions.checkNotNull(value, "Value must not be null."); + optionEdits.put(key, Optional.of(value)); + return this; + } + + @Override + public <T> MaterializedTableEvolution option(ConfigOption<T> configOption, T value) { + Preconditions.checkNotNull(configOption, "Config option must not be null."); + Preconditions.checkNotNull(value, "Value must not be null."); + return option(configOption.key(), ConfigurationUtils.convertValue(value, String.class)); + } + + @Override + public MaterializedTableEvolution resetOption(String key) { + Preconditions.checkNotNull(key, "Key must not be null."); + if (key.equals(FactoryUtil.CONNECTOR.key())) { + throw new ValidationException( + "The option 'connector' of a materialized table cannot be reset."); + } + optionEdits.put(key, Optional.empty()); + return this; + } + + @Override + public MaterializedTable execute() { + final AlterMaterializedTableChangeOperation operation = buildOperation(); + if (operation.getTableChanges().isEmpty()) { + LOG.info( + "Materialized table '{}' was not altered, the changes leave its definition as it is.", + materializedTable.getIdentifier().asSummaryString()); + } else { + tableEnvironment.executeInternal(operation); + } + return MaterializedTableImpl.of(tableEnvironment, materializedTable.getIdentifier()); + } + + @Override + public String explain(ExplainFormat format, ExplainDetail... extraDetails) { Review Comment: Done. I left out `resetOption` though. The plan doesn't print sink options, so pinning that would need an option that changes the plan's shape, and it would pin a documented limitation rather than intended behavior. Happy to add it if you think it's worth it. -- 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]
