kbendick commented on a change in pull request #3701:
URL: https://github.com/apache/iceberg/pull/3701#discussion_r767080509



##########
File path: 
spark/v3.2/spark/src/main/java/org/apache/iceberg/spark/SparkCatalog.java
##########
@@ -369,15 +370,53 @@ public void alterNamespace(String[] namespace, 
NamespaceChange... changes) throw
   }
 
   @Override
+  // Spark assumes that catalogs CASCADE by default. So we have to eagerly
+  // attempt to drop namespaces and tables, but the CASCADE keyword is still
+  // required to actually drop tables and namespaces as Spark will error out
+  // if any of the recursive deletes are non-empty and the user didn't specify
+  // cascades in their query.
   public boolean dropNamespace(String[] namespace) throws 
NoSuchNamespaceException {
     if (asNamespaceCatalog != null) {
+      Namespace asNamespace = Namespace.of(namespace);
+      boolean exists = namespaceExists(namespace);
+
+      // Spark only throws the catalyst version of `NoSuchNamespaceException` 
if the namespace
+      // does not exist AND the user did not specify `IF EXISTS` in their 
query.
+      //
+      // If the namespace does not exist, but listNamespaces didn't throw an 
exception,
+      // we know the user used IF EXISTS and can return false early.
+      List<Namespace> subNamespaces;
       try {
-        return asNamespaceCatalog.dropNamespace(Namespace.of(namespace));
+        subNamespaces = asNamespaceCatalog.listNamespaces(asNamespace);
       } catch (org.apache.iceberg.exceptions.NoSuchNamespaceException e) {
         throw new NoSuchNamespaceException(namespace);
       }
-    }
 
+      if (!exists && subNamespaces.size() == 0) {
+        return false;
+      }
+
+      // Recursively drop namespaces under the requested `namespace`
+      // so that the base case will delete the tables and then the namespace 
of those tables
+      // if the user used CASCADE. If the user did not use CASCADE, Spark will 
return false
+      // as soon as it encounters a non-empty namespace.
+      for (Namespace ns : subNamespaces) {
+        try {
+          boolean didDrop = dropNamespace(ns.levels());
+          if (!didDrop) {
+            return false;
+          }
+        } catch (NoSuchNamespaceException e) {
+          // Spark says this sub-namespace doesn't exist. This is unlikely to 
happen as we just
+          // got it from a listing, but it could have been concurrently 
removed.
+          // In either case, the result is the same.
+        }
+      }
+
+      // Base case
+      Arrays.stream(listTables(namespace)).forEach(this::dropTable);
+      return asNamespaceCatalog.dropNamespace(asNamespace);

Review comment:
       That's somewhat equivalent in this case, based on the way Spark 
implements it.
   
   There's no situation in which some tables are dropped and not others. We 
either:
   - dropped them all via `CASCADE`, in which case this final `dropNamespace` 
result will be true
   - droppped nothing, and we got an excepton which already bubbled up because 
of a `NosuchNamespaceException` or an exception from it being non-empty (which 
we're handling via recursive dropping as Spark will guard based on the presence 
of CASCADE or not).
   - dropped nothing, and this returns false becuse the user used `IF EXISTS` 
and the namespace didn't exist.




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