manuzhang commented on code in PR #14984:
URL: https://github.com/apache/iceberg/pull/14984#discussion_r3810028733


##########
spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/RollbackStagedTable.java:
##########
@@ -0,0 +1,150 @@
+/*
+ * 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.iceberg.spark;
+
+import java.util.Map;
+import java.util.Set;
+import java.util.function.Consumer;
+import java.util.function.Function;
+import org.apache.spark.sql.connector.catalog.Identifier;
+import org.apache.spark.sql.connector.catalog.StagedTable;
+import org.apache.spark.sql.connector.catalog.SupportsDeleteV2;
+import org.apache.spark.sql.connector.catalog.SupportsRead;
+import org.apache.spark.sql.connector.catalog.SupportsWrite;
+import org.apache.spark.sql.connector.catalog.Table;
+import org.apache.spark.sql.connector.catalog.TableCapability;
+import org.apache.spark.sql.connector.catalog.TableCatalog;
+import org.apache.spark.sql.connector.expressions.Transform;
+import org.apache.spark.sql.connector.expressions.filter.Predicate;
+import org.apache.spark.sql.connector.metric.CustomTaskMetric;
+import org.apache.spark.sql.connector.read.ScanBuilder;
+import org.apache.spark.sql.connector.write.LogicalWriteInfo;
+import org.apache.spark.sql.connector.write.WriteBuilder;
+import org.apache.spark.sql.types.StructType;
+import org.apache.spark.sql.util.CaseInsensitiveStringMap;
+
+/**
+ * An implementation of StagedTable that mimics the behavior of Spark's 
non-atomic CTAS and RTAS.
+ *
+ * <p>A Spark catalog can implement StagingTableCatalog to support atomic 
operations by producing
+ * StagedTable. But if a catalog implements StagingTableCatalog, Spark expects 
the catalog to be
+ * able to produce a StagedTable for any table loaded by the catalog. This 
assumption doesn't always
+ * work, as in the case of {@link SparkSessionCatalog}, which supports atomic 
operations can produce
+ * a StagedTable for Iceberg tables, but wraps the session catalog and cannot 
necessarily produce a
+ * working StagedTable implementation for tables that it loads.
+ *
+ * <p>The work-around is this class, which implements the StagedTable 
interface but does not have
+ * atomic behavior. Instead, the StagedTable interface is used to implement 
the behavior of the
+ * non-atomic SQL plans that will create a table, write, and will drop the 
table to roll back.
+ *
+ * <p>This StagedTable implements SupportsRead, SupportsWrite, and 
SupportsDelete by passing the
+ * calls to the real table. Implementing those interfaces is safe because 
Spark will not use them
+ * unless the table supports them and returns the corresponding capabilities 
from {@link
+ * #capabilities()}.
+ */
+public class RollbackStagedTable
+    implements StagedTable, SupportsRead, SupportsWrite, SupportsDeleteV2 {
+  private final TableCatalog catalog;
+  private final Identifier ident;
+  private final Table table;
+
+  public RollbackStagedTable(TableCatalog catalog, Identifier ident, Table 
table) {
+    this.catalog = catalog;
+    this.ident = ident;
+    this.table = table;
+  }
+
+  @Override
+  public void commitStagedChanges() {
+    // the changes have already been committed to the table at the end of the 
write
+  }
+
+  @Override
+  public void abortStagedChanges() {
+    // roll back changes by dropping the table
+    catalog.dropTable(ident);
+  }
+
+  // Java requires an explicit override to disambiguate the unrelated defaults 
inherited from
+  // StagedTable and TruncatableTable.
+  @Override
+  public CustomTaskMetric[] reportDriverMetrics() {

Review Comment:
   Fixed.



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to