raminqaf commented on code in PR #29973:
URL: https://github.com/apache/flink/pull/29973#discussion_r4598959596
##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/runtime/stream/sql/MaterializedTableTableApiITCase.java:
##########
@@ -895,6 +949,34 @@ void
testRedeployMaterializedTableWithMaterializeInto(@TempDir Path temporaryPat
assertThat(newTable.getDistribution()).isEmpty();
}
+ @Test
+ void testRedeployWithFailIfExistsIsRejectedAndLeavesRefreshJobRunning()
throws Exception {
+ usersShopsQuery().materializeInto("users_shops",
Duration.ofSeconds(30)).execute();
+ final ResolvedCatalogMaterializedTable oldTable = getUsersShops();
+ final JobID refreshJobId = getRefreshJobId();
+ waitUntilAllTasksAreRunning(clusterClient, refreshJobId);
+ final Set<JobID> jobsBefore = listJobIds();
+
+ assertThatThrownBy(
+ () ->
+ usersShopsQuery()
+ .materializeInto("users_shops",
Duration.ofMinutes(1))
+ .execute(CreateMode.failIfExists()))
+ .isInstanceOf(ValidationException.class)
+ .hasMessage(
+ "Could not execute CreateTable in path
`test_catalog`.`test_db`.`users_shops`");
+
+ final ResolvedCatalogMaterializedTable currentTable = getUsersShops();
+
assertThat(currentTable.getRefreshStatus()).isSameAs(RefreshStatus.ACTIVATED);
+
assertThat(currentTable.getResolvedSchema()).isEqualTo(oldTable.getResolvedSchema());
+
assertThat(currentTable.getExpandedQuery()).isEqualTo(oldTable.getExpandedQuery());
+ assertThat(currentTable.getDefinitionFreshness())
+ .isEqualTo(oldTable.getDefinitionFreshness());
+ assertThat(getRefreshJobId()).isEqualTo(refreshJobId);
Review Comment:
the stored entry should not change at all. So
`assertThat(getUsersShops()).isEqualTo(oldTable)` can replace the checks
##########
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/MaterializedPipeline.java:
##########
@@ -57,8 +57,17 @@ public interface MaterializedPipeline {
* options are reset and its distribution is dropped, as by a {@code
CREATE OR ALTER
* MATERIALIZED TABLE} without {@code WITH} and {@code DISTRIBUTED BY}
clauses.
*
+ * <p>Equivalent to {@code execute(CreateMode.createOrAlter())}.
+ *
* <p>Returns once the catalog entry is written and, if a continuous
refresh job is started,
* once that job has been submitted.
*/
MaterializedTable execute();
+
+ /**
+ * Submits this declaration in the given mode. With {@link
CreateMode#createOrAlter()}, this is
+ * {@link #execute()}. With {@link CreateMode#failIfExists()}, it behaves
as that mode
+ * describes.
Review Comment:
the last part of the docs is very vague
##########
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/internal/materializedtable/MaterializedPipelineImpl.java:
##########
@@ -90,37 +91,65 @@ public ObjectIdentifier getIdentifier() {
@Override
public MaterializedTable execute() {
- tableEnvironment.executeInternal(buildOperation());
+ return execute(CreateMode.createOrAlter());
+ }
+
+ @Override
+ public MaterializedTable execute(CreateMode mode) {
+ Preconditions.checkNotNull(mode, "Create mode must not be null.");
+ final Mode finalOperationMode = normalizeCreateMode(mode);
+ tableEnvironment.executeInternal(buildOperation(finalOperationMode));
return new MaterializedTableImpl(identifier);
}
- private Operation buildOperation() {
+ private Operation buildOperation(Mode factoryMode) {
final CatalogManager catalogManager =
tableEnvironment.getCatalogManager();
if (catalogManager.isTemporaryTable(identifier)) {
throw new ValidationException(
String.format(
- "Cannot create or alter materialized table %s,
because a temporary table or view with the same identifier exists and would
shadow it. Drop the temporary object first.",
+ "Cannot %s materialized table %s, because a
temporary table or view with the same identifier exists and would shadow it.
Drop the temporary object first.",
+ factoryMode == Mode.CREATE ? "create" : "create or
alter",
identifier.asSummaryString()));
}
- final Optional<ResolvedCatalogBaseTable<?>> existing =
- catalogManager.getCatalogBaseTable(identifier);
- if (existing.isPresent() && existing.get().getTableKind() ==
TableKind.TABLE) {
- throw new ValidationException(
- String.format(
- "Cannot create or alter materialized table %s,
because a regular table with the same identifier exists. Table#materializeInto
does not support regular table conversion.",
- identifier.asSummaryString()));
+
+ // Under CREATE the catalog rejects any permanent object at the path.
+ if (factoryMode == Mode.CREATE_OR_ALTER) {
+ final Optional<ResolvedCatalogBaseTable<?>> existing =
+ catalogManager.getCatalogBaseTable(identifier);
+ if (existing.isPresent() && existing.get().getTableKind() ==
TableKind.TABLE) {
+ throw new ValidationException(
+ String.format(
+ "Cannot create or alter materialized table %s,
because a regular table with the same identifier exists. Table#materializeInto
does not support regular table conversion.",
+ identifier.asSummaryString()));
+ }
}
// A query statement parses to exactly one query operation.
final QueryOperation storedQueryOperation =
(QueryOperation)
tableEnvironment.getParser().parse(serializedQuery).get(0);
return new MaterializedTableOperationFactory(catalogManager,
tableEnvironment.getConfig())
.build(
identifier,
- Mode.CREATE_OR_ALTER,
+ factoryMode,
new QueryMaterializedTableMergeContext(
storedQueryOperation, serializedQuery,
freshness));
}
+ private Mode normalizeCreateMode(CreateMode mode) {
Review Comment:
should we rename to
```suggestion
private Mode toFactoryMode(CreateMode mode) {
```
##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/runtime/stream/sql/MaterializedTableTableApiITCase.java:
##########
@@ -895,6 +949,34 @@ void
testRedeployMaterializedTableWithMaterializeInto(@TempDir Path temporaryPat
assertThat(newTable.getDistribution()).isEmpty();
}
+ @Test
+ void testRedeployWithFailIfExistsIsRejectedAndLeavesRefreshJobRunning()
throws Exception {
+ usersShopsQuery().materializeInto("users_shops",
Duration.ofSeconds(30)).execute();
+ final ResolvedCatalogMaterializedTable oldTable = getUsersShops();
+ final JobID refreshJobId = getRefreshJobId();
+ waitUntilAllTasksAreRunning(clusterClient, refreshJobId);
+ final Set<JobID> jobsBefore = listJobIds();
+
+ assertThatThrownBy(
+ () ->
+ usersShopsQuery()
+ .materializeInto("users_shops",
Duration.ofMinutes(1))
+ .execute(CreateMode.failIfExists()))
+ .isInstanceOf(ValidationException.class)
+ .hasMessage(
+ "Could not execute CreateTable in path
`test_catalog`.`test_db`.`users_shops`");
Review Comment:
```suggestion
"Could not execute CreateTable in path
`test_catalog`.`test_db`.`users_shops`")
.hasCauseInstanceOf(TableAlreadyExistException.class);
```
--
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]