Timm0 commented on code in PR #29973:
URL: https://github.com/apache/flink/pull/29973#discussion_r4600776738


##########
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:
   I added a short sentence what `failIfExists` means in that context.



##########
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:
   Done.



-- 
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]

Reply via email to