Repository: apache/gravitino (Java)

Issue:
[Bug report] Can not drop a catalog when its schemas were dropped externally
### Version

main branch

### Describe what's wrong

Can not drop a catalog when its schemas were dropped externally

### Error message and/or stacktrace

Catalog test has schemas, please drop them first or use force option

### How to reproduce

 - create a relational catalog
 - create a schema by Gravitino
 - drop the schema does not through Gravitino
 - disable the catalog then drop it

### Additional context

_No response_
[Bug report] Failed to drop clean PG catalog due to "public" schema.
### Version

main branch

### Describe what's wrong

Failed to drop clean catalog due to  PG "public" schema.

<img width="791" alt="image" src="https://github.com/user-attachments/assets/84503015-d935-4310-8209-3a87e3e81e95">
<img width="958" alt="image" src="https://github.com/user-attachments/assets/390f15d6-841b-4c2e-8204-cae1222f8a25">


### Error message and/or stacktrace

```
{
    "code": 1005,
    "type": "NonEmptyCatalogException",
    "message": "Failed to operate catalog(s) [pg6] operation [DROP] under metalake [pg_test2], reason [Catalog pg_test2.pg6 has schemas, please drop them first or use force option]",
    "stack": [
        "org.apache.gravitino.exceptions.NonEmptyCatalogException: Catalog pg_test2.pg6 has schemas, please drop them first or use force option",
        "\tat org.apache.gravitino.catalog.CatalogManager.dropCatalog(CatalogManager.java:655)",
        "\tat org.apache.gravitino.hook.CatalogHookDispatcher.dropCatalog(CatalogHookDispatcher.java:130)",
        "\tat org.apache.gravitino.catalog.CatalogNormalizeDispatcher.dropCatalog(CatalogNormalizeDispatcher.java:112)",
        "\tat org.apache.gravitino.listener.CatalogEventDispatcher.dropCatalog(CatalogEventDispatcher.java:169)",
        "\tat org.apache.gravitino.server.web.rest.CatalogOperations.lambda$dropCatalog$12(CatalogOperations.java:316)",
        "\tat org.apache.gravitino.lock.TreeLockUtils.doWithTreeLock(TreeLockUtils.java:49)",
        "\tat org.apache.gravitino.server.web.rest.CatalogOperations.lambda$dropCatalog$13(CatalogOperations.java:313)",
        "\tat java.base/java.security.AccessController.doPrivileged(AccessController.java:712)",
        "\tat java.base/javax.security.auth.Subject.doAs(Subject.java:439)",
        "\tat org.apache.gravitino.utils.PrincipalUtils.doAs(PrincipalUtils.java:39)",
        "\tat org.apache.gravitino.server.web.Utils.doAs(Utils.java:188)",
        "\tat org.apache.gravitino.server.web.rest.CatalogOperations.dropCatalog(CatalogOperations.java:308)",
        "\tat jdk.internal.reflect.GeneratedMethodAccessor130.invoke(Unknown Source)",
        "\tat java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)",
        "\tat java.base/java.lang.reflect.Method.invoke(Method.java:568)",
        "\tat org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.lambda$static$0(ResourceMethodInvocationHandlerFactory.java:52)",
        "\tat org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:146)",
        "\tat org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:189)",
        "\tat org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:176)",
        "\tat org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:93)",
        "\tat org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:478)",
        "\tat org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:400)",
        "\tat org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:81)",
        "\tat org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:256)",
        "\tat org.glassfish.jersey.internal.Errors$1.call(Errors.java:248)",
        "\tat org.glassfish.jersey.internal.Errors$1.call(Errors.java:244)",
        "\tat org.glassfish.jersey.internal.Errors.process(Errors.java:292)",
        "\tat org.glassfish.jersey.internal.Errors.process(Errors.java:274)",
        "\tat org.glassfish.jersey.internal.Errors.process(Errors.java:244)",
        "\tat org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:265)",
        "\tat org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:235)",
        "\tat org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:684)",
        "\tat org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:394)",
        "\tat org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:346)",
        "\tat org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:358)",
        "\tat org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:311)",
        "\tat org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:205)",
        "\tat org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:799)",
        "\tat org.eclipse.jetty.servlet.ServletHandler$ChainEnd.doFilter(ServletHandler.java:1656)",
        "\tat org.apache.gravitino.server.authentication.AuthenticationFilter.doFilter(AuthenticationFilter.java:86)",
        "\tat org.eclipse.jetty.servlet.FilterHolder.doFilter(FilterHolder.java:193)",
        "\tat org.eclipse.jetty.servlet.ServletHandler$Chain.doFilter(ServletHandler.java:1626)",
        "\tat org.apache.gravitino.server.web.VersioningFilter.doFilter(VersioningFilter.java:111)",
        "\tat org.eclipse.jetty.servlet.FilterHolder.doFilter(FilterHolder.java:193)",
        "\tat org.eclipse.jetty.servlet.ServletHandler$Chain.doFilter(ServletHandler.java:1626)",
        "\tat org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:552)",
        "\tat org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)",
        "\tat org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:600)",
        "\tat org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127)",
        "\tat org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:235)",
        "\tat org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1624)",
        "\tat org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:233)",
        "\tat org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1440)",
        "\tat org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:188)",
        "\tat org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:505)",
        "\tat org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1594)",
        "\tat org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:186)",
        "\tat org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1355)",
        "\tat org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)",
        "\tat org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:146)",
        "\tat org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127)",
        "\tat org.eclipse.jetty.server.Server.handle(Server.java:516)",
        "\tat org.eclipse.jetty.server.HttpChannel.lambda$handle$1(HttpChannel.java:487)",
        "\tat org.eclipse.jetty.server.HttpChannel.dispatch(HttpChannel.java:732)",
        "\tat org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:479)",
        "\tat org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:277)",
        "\tat org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:311)",
        "\tat org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:105)",
        "\tat org.eclipse.jetty.io.ChannelEndPoint$1.run(ChannelEndPoint.java:104)",
        "\tat org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.runTask(EatWhatYouKill.java:338)",
        "\tat org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:315)",
        "\tat org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.tryProduce(EatWhatYouKill.java:173)",
        "\tat org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:131)",
        "\tat org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:409)",
        "\tat org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:883)",
        "\tat org.eclipse.jetty.util.thread.QueuedThreadPool$Runner.run(QueuedThreadPool.java:1034)",
        "\tat java.base/java.lang.Thread.run(Thread.java:840)"
    ]
}
```

### How to reproduce

1. launch a new PG instance
2. create a  PG catalog  in gravitino
3. check the table is empty on UI
4. drop the PG catalog 

### Additional context

_No response_



Fix (patch):
diff --git a/core/src/main/java/org/apache/gravitino/catalog/CatalogManager.java b/core/src/main/java/org/apache/gravitino/catalog/CatalogManager.java
index 6f77bb46206..da79ff702e3 100644
--- a/core/src/main/java/org/apache/gravitino/catalog/CatalogManager.java
+++ b/core/src/main/java/org/apache/gravitino/catalog/CatalogManager.java
@@ -643,24 +643,25 @@ public boolean dropCatalog(NameIdentifier ident, boolean force)
             "Catalog %s is in use, please disable it first or use force option", ident);
       }
 
-      List<SchemaEntity> schemas =
-          store.list(
-              Namespace.of(ident.namespace().level(0), ident.name()),
-              SchemaEntity.class,
-              EntityType.SCHEMA);
+      Namespace schemaNamespace = Namespace.of(ident.namespace().level(0), ident.name());
+      CatalogWrapper catalogWrapper = loadCatalogAndWrap(ident);
+
+      List<SchemaEntity> schemaEntities =
+          store.list(schemaNamespace, SchemaEntity.class, EntityType.SCHEMA);
       CatalogEntity catalogEntity = store.get(ident, EntityType.CATALOG, CatalogEntity.class);
 
-      if (!schemas.isEmpty() && !force) {
-        // the Kafka catalog is special, it includes a default schema
-        if (!catalogEntity.getProvider().equals("kafka") || schemas.size() > 1) {
-          throw new NonEmptyCatalogException(
-              "Catalog %s has schemas, please drop them first or use force option", ident);
-        }
+      if (containsUserCreatedSchemas(schemaEntities, catalogEntity, catalogWrapper) && !force) {
+        throw new NonEmptyCatalogException(
+            "Catalog %s has schemas, please drop them first or use force option", ident);
       }
 
-      CatalogWrapper catalogWrapper = loadCatalogAndWrap(ident);
       if (includeManagedEntities(catalogEntity)) {
-        schemas.forEach(
+        // code reach here in two cases:
+        // 1. the catalog does not have available schemas
+        // 2. the catalog has available schemas, and force is true
+        // for case 1, the forEach block can drop them without any side effect
+        // for case 2, the forEach block will drop all managed sub-entities
+        schemaEntities.forEach(
             schema -> {
               try {
                 catalogWrapper.doWithSchemaOps(
@@ -677,11 +678,69 @@ public boolean dropCatalog(NameIdentifier ident, boolean force)
     } catch (NoSuchMetalakeException | NoSuchCatalogException ignored) {
       return false;
 
-    } catch (IOException e) {
+    } catch (GravitinoRuntimeException e) {
+      throw e;
+    } catch (Exception e) {
       throw new RuntimeException(e);
     }
   }
 
+  /**
+   * Check if the given list of schema entities contains any currently existing user-created
+   * schemas.
+   *
+   * <p>This method determines if there are valid user-created schemas by comparing the provided
+   * schema entities with the actual schemas currently existing in the external data source. It
+   * excludes:
+   *
+   * <ul>
+   *   <li>1. Automatically generated schemas (such as Kafka catalog's "default" schema or
+   *       JDBC-PostgreSQL catalog's "public" schema).
+   *   <li>2. Schemas that have been dropped externally but still exist in the entity store.
+   * </ul>
+   *
+   * @param schemaEntities The list of schema entities to check.
+   * @param catalogEntity The catalog entity to which the schemas belong.
+   * @param catalogWrapper The catalog wrapper for the catalog.
+   * @return True if the list of schema entities contains any valid user-created schemas, false
+   *     otherwise.
+   * @throws Exception If an error occurs while checking the schemas.
+   */
+  private boolean containsUserCreatedSchemas(
+      List<SchemaEntity> schemaEntities, CatalogEntity catalogEntity, CatalogWrapper catalogWrapper)
+      throws Exception {
+    if (schemaEntities.isEmpty()) {
+      return false;
+    }
+
+    if (schemaEntities.size() == 1) {
+      if ("kafka".equals(catalogEntity.getProvider())) {
+        return false;
+
+      } else if ("jdbc-postgresql".equals(catalogEntity.getProvider())) {
+        // PostgreSQL catalog includes the "public" schema, see
+        // https://github.com/apache/gravitino/issues/2314
+        return !schemaEntities.get(0).name().equals("public");
+      }
+    }
+
+    NameIdentifier[] allSchemas =
+        catalogWrapper.doWithSchemaOps(
+            schemaOps ->
+                schemaOps.listSchemas(
+                    Namespace.of(catalogEntity.namespace().level(0), catalogEntity.name())));
+    if (allSchemas.length == 0) {
+      return false;
+    }
+
+    Set<String> availableSchemaNames =
+        Arrays.stream(allSchemas).map(NameIdentifier::name).collect(Collectors.toSet());
+
+    // some schemas are dropped externally, but still exist in the entity store, those schemas are
+    // invalid
+    return schemaEntities.stream().map(SchemaEntity::name).anyMatch(availableSchemaNames::contains);
+  }
+
   private boolean includeManagedEntities(CatalogEntity catalogEntity) {
     return catalogEntity.getType().equals(FILESET);
   }