Timm0 commented on code in PR #30207: URL: https://github.com/apache/flink/pull/30207#discussion_r4752927766
########## 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) { + return tableEnvironment.explainInternal( + List.of(withRefreshQuery(buildOperation())), format, extraDetails); + } + + private AlterMaterializedTableChangeOperation withRefreshQuery( + AlterMaterializedTableChangeOperation operation) { + if (operation.getAsQueryOperation() != null) { + return operation; + } + final ResolvedCatalogMaterializedTable oldTable = operation.getOldTable(); + final QueryOperation storedQuery = + parseQuery(oldTable.getExpandedQuery(), tableEnvironment.getParser()); + return new AlterMaterializedTableChangeOperation( + materializedTable.getIdentifier(), + currentTable -> operation.getTableChanges(), + oldTable, + storedQuery); + } + + @VisibleForTesting + AlterMaterializedTableChangeOperation buildOperation() { + final ResolvedCatalogMaterializedTable storedTable = materializedTable.lookUp(); + final Map<String, String> options = new LinkedHashMap<>(storedTable.getOptions()); + optionEdits.forEach( + (key, value) -> + value.ifPresentOrElse( + newValue -> options.put(key, newValue), () -> options.remove(key))); + final EvolutionMaterializedTableMergeContext definition = + new EvolutionMaterializedTableMergeContext( + storedTable, + parseNewQuery(), + newSerializedQuery, + Collections.unmodifiableMap(options), + declaredSchema(storedTable), + tableEnvironment.getCatalogManager()); + return new MaterializedTableOperationFactory( + tableEnvironment.getCatalogManager(), tableEnvironment.getConfig()) + .alter(materializedTable.getIdentifier(), storedTable, definition); + } + + private @Nullable QueryOperation parseNewQuery() { + if (newSerializedQuery == null) { + return null; + } + return parseQuery(newSerializedQuery, tableEnvironment.getParser()); + } + + private @Nullable Schema declaredSchema(ResolvedCatalogMaterializedTable storedTable) { Review Comment: I'll fold it. FLINK-41401 applies the schema edits to the stored schema and passes the result as the declared schema to the same `CREATE OR ALTER` derivation, so a query change and schema edits stay one set of changes with one restart. Good catch on nullability. I feel SQL is a bit inconsistent here as well. `ALTER ... AS`, `CREATE OR ALTER` without a column list and a column list starting with a computed column all store an appended column as nullable. Only a column list starting with a physical column keeps the query's `NOT NULL` for unlisted columns, because only then does `hasSchemaDefinition` treat the list as a declared schema. So in FLINK-41401, columns the new query appends will always be stored as nullable, as FLIP-616 already states for `query()`, and primary key columns stay `NOT NULL`. -- 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]
