AHeise commented on code in PR #30207: URL: https://github.com/apache/flink/pull/30207#discussion_r4744954904
########## flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/MaterializedTableEvolution.java: ########## @@ -0,0 +1,99 @@ +/* + * 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; + +/** + * A pending change to an existing materialized table, returned by {@link + * MaterializedTable#alter()}. + * + * <p>Changes apply in call order to the table's definition as the catalog stores it when {@link + * #execute()} or {@link #explain(ExplainDetail...)} runs. + */ +@PublicEvolving +public interface MaterializedTableEvolution extends Explainable<MaterializedTableEvolution> { + + /** Review Comment: `query()` and `execute()` use CREATE OR ALTER semantics, but the FLIP describes `ALTER … AS` and ALTERs run one after another. FLIP line ~1495 even says a query change that changes columns "is rejected when the schema has computed (non-persisted) columns", and this PR accepts that case. I think CoA is the better behavior (the SQL error already points users to it), but could we record the deviation in the PR description or with a short note on the FLIP/ML, so it's clearly intentional? Minor: `query()` is where users will look for the evolution rule. Something like "The new query must keep every persisted column with its name, position and type, and may only append columns, which are stored as nullable" would help. Fine to fold into FLINK-41610 instead. ########## 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: `explain()` isn't exercised by any test. The query-change path goes straight to the planner branch that `ExplainTest#testExplainFullAlterMaterializedTable` already covers, but `withRefreshQuery` (the options-only case FLINK-41397 calls out) is new. Could you add one planner-side test for an options-only alter, e.g. in `DefaultMaterializedTableExecutorTest` asserting the plan's sink fields? Including a `resetOption` would also pin the documented "resets still apply in explain" behavior. ########## 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: Non-blocking: FLINK-41397 planned typed `MaterializedTableAlteration`s applied in call order, while this feeds one CoA merge context into a single diff. For FLINK-41401 (schema edits), will you chain the extracted ADD/MODIFY/DROP changes after the query step, or fold the edits into the declared schema? The second flips `schemaDeclared` and keeps a NOT NULL column the query appends NOT NULL, so nullability would depend on whether `addSchema` was called. -- 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]
