luoyuxia commented on code in PR #22995:
URL: https://github.com/apache/flink/pull/22995#discussion_r1266082678


##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/runtime/stream/sql/AtomicRtasITCase.java:
##########
@@ -0,0 +1,36 @@
+/*
+ * 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.planner.runtime.stream.sql;
+
+import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
+import org.apache.flink.table.api.EnvironmentSettings;
+import org.apache.flink.table.api.TableEnvironment;
+import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
+import org.apache.flink.table.planner.runtime.utils.AtomicRtasITCaseBase;
+
+/** Tests atomic rtas in stream mode. */
+public class AtomicRtasITCase extends AtomicRtasITCaseBase {
+
+    @Override
+    protected TableEnvironment getTableEnvironment() {
+        EnvironmentSettings settings = 
EnvironmentSettings.newInstance().inStreamingMode().build();
+        return StreamTableEnvironment.create(

Review Comment:
   Just wondering can we use `return TestingTableEnvironment.create(settings, 
null, TableConfig.getDefault());` 
   to keep it consistent with `AtomicRtasITCase` in batch mode?



##########
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/internal/TableEnvironmentImpl.java:
##########
@@ -833,79 +832,106 @@ public TableResultInternal 
executeInternal(List<ModifyOperation> operations) {
         return executeInternal(transformations, sinkIdentifierNames, 
jobStatusHookList);
     }
 
-    private ModifyOperation getOperation(ReplaceTableAsOperation 
rtasOperation) {
-        // rtas drop table first, then create
+    private ModifyOperation getModifyOperation(
+            ReplaceTableAsOperation rtasOperation, List<JobStatusHook> 
jobStatusHookList) {
         CreateTableOperation createTableOperation = 
rtasOperation.getCreateTableOperation();
         ObjectIdentifier tableIdentifier = 
createTableOperation.getTableIdentifier();
-        try {
-            catalogManager.dropTable(tableIdentifier, 
rtasOperation.isCreateOrReplace());
-        } catch (ValidationException e) {
-            if (String.format(
-                            "Table with identifier '%s' does not exist.",
-                            tableIdentifier.asSummaryString())
-                    .equals(e.getMessage())) {
-                throw new TableException(
-                        String.format(
-                                "The table %s to be replaced doesn't exist. "
-                                        + "You can try to use CREATE TABLE AS 
statement or "
-                                        + "CREATE OR REPLACE TABLE AS 
statement.",
-                                tableIdentifier));
-            } else {
-                throw e;
-            }
+        // First check if the replacedTable exists
+        Optional<ContextResolvedTable> replacedTable = 
catalogManager.getTable(tableIdentifier);
+        if (!rtasOperation.isCreateOrReplace() && !replacedTable.isPresent()) {
+            throw new TableException(
+                    String.format(
+                            "The table %s to be replaced doesn't exist. "
+                                    + "You can try to use CREATE TABLE AS 
statement or "
+                                    + "CREATE OR REPLACE TABLE AS statement.",
+                            tableIdentifier));
         }
+        Catalog catalog = 
catalogManager.getCatalog(tableIdentifier.getCatalogName()).orElse(null);
+        ResolvedCatalogTable catalogTable =
+                
catalogManager.resolveCatalogTable(createTableOperation.getCatalogTable());
+        Optional<DynamicTableSink> stagingDynamicTableSink =
+                getSupportsStagingDynamicTableSink(createTableOperation, 
catalog, catalogTable);
+        if (stagingDynamicTableSink.isPresent()) {
+            // use atomic rtas
+            DynamicTableSink dynamicTableSink = stagingDynamicTableSink.get();
+            SupportsStaging.StagingPurpose stagingPurpose =
+                    rtasOperation.isCreateOrReplace()
+                            ? 
SupportsStaging.StagingPurpose.CREATE_OR_REPLACE_TABLE_AS
+                            : SupportsStaging.StagingPurpose.REPLACE_TABLE_AS;
+
+            StagedTable stagedTable =
+                    ((SupportsStaging) dynamicTableSink)
+                            .applyStaging(new 
SinkStagingContext(stagingPurpose));
+            StagingSinkJobStatusHook stagingSinkJobStatusHook =
+                    new StagingSinkJobStatusHook(stagedTable);
+            jobStatusHookList.add(stagingSinkJobStatusHook);
+            return rtasOperation.toStagedSinkModifyOperation(
+                    tableIdentifier, catalogTable, catalog, dynamicTableSink);
+        }
+        // non-atomic rtas drop table first, then create
+        catalogManager.dropTable(tableIdentifier, 
rtasOperation.isCreateOrReplace());

Review Comment:
   Why always drop table? if `!replacedTable.isPresent()`, we don't need to 
drop so that we can save another metastore call.



##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/runtime/stream/sql/AtomicRtasITCase.java:
##########
@@ -0,0 +1,36 @@
+/*
+ * 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.planner.runtime.stream.sql;
+
+import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
+import org.apache.flink.table.api.EnvironmentSettings;
+import org.apache.flink.table.api.TableEnvironment;
+import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
+import org.apache.flink.table.planner.runtime.utils.AtomicRtasITCaseBase;
+
+/** Tests atomic rtas in stream mode. */
+public class AtomicRtasITCase extends AtomicRtasITCaseBase {
+
+    @Override
+    protected TableEnvironment getTableEnvironment() {
+        EnvironmentSettings settings = 
EnvironmentSettings.newInstance().inStreamingMode().build();
+        return StreamTableEnvironment.create(

Review Comment:
   Also, if we can, don't forget to update `AtomicCtasITCase`.



##########
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/internal/TableEnvironmentImpl.java:
##########
@@ -833,79 +832,106 @@ public TableResultInternal 
executeInternal(List<ModifyOperation> operations) {
         return executeInternal(transformations, sinkIdentifierNames, 
jobStatusHookList);
     }
 
-    private ModifyOperation getOperation(ReplaceTableAsOperation 
rtasOperation) {
-        // rtas drop table first, then create
+    private ModifyOperation getModifyOperation(
+            ReplaceTableAsOperation rtasOperation, List<JobStatusHook> 
jobStatusHookList) {
         CreateTableOperation createTableOperation = 
rtasOperation.getCreateTableOperation();
         ObjectIdentifier tableIdentifier = 
createTableOperation.getTableIdentifier();
-        try {
-            catalogManager.dropTable(tableIdentifier, 
rtasOperation.isCreateOrReplace());
-        } catch (ValidationException e) {
-            if (String.format(
-                            "Table with identifier '%s' does not exist.",
-                            tableIdentifier.asSummaryString())
-                    .equals(e.getMessage())) {
-                throw new TableException(
-                        String.format(
-                                "The table %s to be replaced doesn't exist. "
-                                        + "You can try to use CREATE TABLE AS 
statement or "
-                                        + "CREATE OR REPLACE TABLE AS 
statement.",
-                                tableIdentifier));
-            } else {
-                throw e;
-            }
+        // First check if the replacedTable exists
+        Optional<ContextResolvedTable> replacedTable = 
catalogManager.getTable(tableIdentifier);
+        if (!rtasOperation.isCreateOrReplace() && !replacedTable.isPresent()) {
+            throw new TableException(
+                    String.format(
+                            "The table %s to be replaced doesn't exist. "
+                                    + "You can try to use CREATE TABLE AS 
statement or "
+                                    + "CREATE OR REPLACE TABLE AS statement.",
+                            tableIdentifier));
         }
+        Catalog catalog = 
catalogManager.getCatalog(tableIdentifier.getCatalogName()).orElse(null);
+        ResolvedCatalogTable catalogTable =
+                
catalogManager.resolveCatalogTable(createTableOperation.getCatalogTable());
+        Optional<DynamicTableSink> stagingDynamicTableSink =
+                getSupportsStagingDynamicTableSink(createTableOperation, 
catalog, catalogTable);
+        if (stagingDynamicTableSink.isPresent()) {
+            // use atomic rtas
+            DynamicTableSink dynamicTableSink = stagingDynamicTableSink.get();
+            SupportsStaging.StagingPurpose stagingPurpose =
+                    rtasOperation.isCreateOrReplace()
+                            ? 
SupportsStaging.StagingPurpose.CREATE_OR_REPLACE_TABLE_AS
+                            : SupportsStaging.StagingPurpose.REPLACE_TABLE_AS;
+
+            StagedTable stagedTable =
+                    ((SupportsStaging) dynamicTableSink)
+                            .applyStaging(new 
SinkStagingContext(stagingPurpose));
+            StagingSinkJobStatusHook stagingSinkJobStatusHook =
+                    new StagingSinkJobStatusHook(stagedTable);
+            jobStatusHookList.add(stagingSinkJobStatusHook);
+            return rtasOperation.toStagedSinkModifyOperation(
+                    tableIdentifier, catalogTable, catalog, dynamicTableSink);
+        }
+        // non-atomic rtas drop table first, then create
+        catalogManager.dropTable(tableIdentifier, 
rtasOperation.isCreateOrReplace());
         executeInternal(createTableOperation);
         return rtasOperation.toSinkModifyOperation(catalogManager);
     }
 
     private ModifyOperation getModifyOperation(
             CreateTableASOperation ctasOperation, List<JobStatusHook> 
jobStatusHookList) {
         CreateTableOperation createTableOperation = 
ctasOperation.getCreateTableOperation();
-        if (tableConfig.get(TableConfigOptions.TABLE_CTAS_ATOMICITY_ENABLED)) {
-            ObjectIdentifier tableIdentifier = 
createTableOperation.getTableIdentifier();
-            Catalog catalog =
-                    
catalogManager.getCatalog(tableIdentifier.getCatalogName()).orElse(null);
-            ResolvedCatalogTable catalogTable =
-                    
catalogManager.resolveCatalogTable(createTableOperation.getCatalogTable());
+        ObjectIdentifier tableIdentifier = 
createTableOperation.getTableIdentifier();
+        Catalog catalog = 
catalogManager.getCatalog(tableIdentifier.getCatalogName()).orElse(null);

Review Comment:
   ```suggestion
           Catalog catalog =
                   
catalogManager.getCatalogOrThrowException(tableIdentifier.getCatalogName());
   ```



##########
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/internal/TableEnvironmentImpl.java:
##########
@@ -833,79 +832,106 @@ public TableResultInternal 
executeInternal(List<ModifyOperation> operations) {
         return executeInternal(transformations, sinkIdentifierNames, 
jobStatusHookList);
     }
 
-    private ModifyOperation getOperation(ReplaceTableAsOperation 
rtasOperation) {
-        // rtas drop table first, then create
+    private ModifyOperation getModifyOperation(
+            ReplaceTableAsOperation rtasOperation, List<JobStatusHook> 
jobStatusHookList) {
         CreateTableOperation createTableOperation = 
rtasOperation.getCreateTableOperation();
         ObjectIdentifier tableIdentifier = 
createTableOperation.getTableIdentifier();
-        try {
-            catalogManager.dropTable(tableIdentifier, 
rtasOperation.isCreateOrReplace());
-        } catch (ValidationException e) {
-            if (String.format(
-                            "Table with identifier '%s' does not exist.",
-                            tableIdentifier.asSummaryString())
-                    .equals(e.getMessage())) {
-                throw new TableException(
-                        String.format(
-                                "The table %s to be replaced doesn't exist. "
-                                        + "You can try to use CREATE TABLE AS 
statement or "
-                                        + "CREATE OR REPLACE TABLE AS 
statement.",
-                                tableIdentifier));
-            } else {
-                throw e;
-            }
+        // First check if the replacedTable exists
+        Optional<ContextResolvedTable> replacedTable = 
catalogManager.getTable(tableIdentifier);
+        if (!rtasOperation.isCreateOrReplace() && !replacedTable.isPresent()) {
+            throw new TableException(
+                    String.format(
+                            "The table %s to be replaced doesn't exist. "
+                                    + "You can try to use CREATE TABLE AS 
statement or "
+                                    + "CREATE OR REPLACE TABLE AS statement.",
+                            tableIdentifier));
         }
+        Catalog catalog = 
catalogManager.getCatalog(tableIdentifier.getCatalogName()).orElse(null);

Review Comment:
   ```suggestion
           Catalog catalog = 
catalogManager.getCatalogOrThrowException(tableIdentifier.getCatalogName());
   ```



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