Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#11317] fix(core): reject listing schemas under a parent for non-hierarchical catalogs (#11319)

roryqi8537e3a03998
diff --git a/catalogs/catalog-hive/src/test/java/org/apache/gravitino/catalog/hive/integration/test/CatalogHive2IT.java b/catalogs/catalog-hive/src/test/java/org/apache/gravitino/catalog/hive/integration/test/CatalogHive2IT.java
index 800ae44e22..413ff82f58 100644
--- a/catalogs/catalog-hive/src/test/java/org/apache/gravitino/catalog/hive/integration/test/CatalogHive2IT.java
+++ b/catalogs/catalog-hive/src/test/java/org/apache/gravitino/catalog/hive/integration/test/CatalogHive2IT.java
@@ -771,6 +771,14 @@ public class CatalogHive2IT extends BaseIT {
Assertions.assertEquals(1, tables.length);
}
+ @Test
+ public void testListSchemasWithParentSchemaUnsupported() {
+ // Hive does not support hierarchical (nested) schemas, so listing schemas under a parent
+ // schema must be rejected instead of returning the catalog's flat schemas (see issue #11317).
+ SupportsSchemas schemas = catalog.asSchemas();
+ assertThrows(UnsupportedOperationException.class, () -> schemas.listSchemas(schemaName));
+ }
+
@Test
public void testFunctions() throws InterruptedException {
// test list functions in a schema which not created by Gravitino
diff --git a/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/IcebergCatalogCapability.java b/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/IcebergCatalogCapability.java
index 2bac6ed4db..15f82042c0 100644
--- a/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/IcebergCatalogCapability.java
+++ b/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/IcebergCatalogCapability.java
@@ -42,6 +42,12 @@ public class IcebergCatalogCapability implements Capability {
return CapabilityResult.unsupported("Iceberg does not support column default value.");
}
+ @Override
+ public CapabilityResult supportsHierarchicalSchema() {
+ // Iceberg namespaces can be multi-level, so Iceberg supports hierarchical (nested) schemas.
+ return CapabilityResult.SUPPORTED;
+ }
+
/**
* Validates the schema name specification for Iceberg.
*
diff --git a/catalogs/catalog-lakehouse-iceberg/src/test/java/org/apache/gravitino/catalog/lakehouse/iceberg/TestIcebergCatalogCapability.java b/catalogs/catalog-lakehouse-iceberg/src/test/java/org/apache/gravitino/catalog/lakehouse/iceberg/TestIcebergCatalogCapability.java
index 0172f2fb2a..20810b2a04 100644
--- a/catalogs/catalog-lakehouse-iceberg/src/test/java/org/apache/gravitino/catalog/lakehouse/iceberg/TestIcebergCatalogCapability.java
+++ b/catalogs/catalog-lakehouse-iceberg/src/test/java/org/apache/gravitino/catalog/lakehouse/iceberg/TestIcebergCatalogCapability.java
@@ -91,6 +91,13 @@ public class TestIcebergCatalogCapability {
Assertions.assertTrue(result.supported());
}
+ @Test
+ public void testSupportsHierarchicalSchema() {
+ IcebergCatalogCapability capability = new IcebergCatalogCapability(":");
+
+ Assertions.assertTrue(capability.supportsHierarchicalSchema().supported());
+ }
+
@Test
public void testCustomSeparatorSlash() {
IcebergCatalogCapability capability = new IcebergCatalogCapability("/");
diff --git a/core/src/main/java/org/apache/gravitino/catalog/SchemaNormalizeDispatcher.java b/core/src/main/java/org/apache/gravitino/catalog/SchemaNormalizeDispatcher.java
index 25460bb5d0..b4d55e6cb9 100644
--- a/core/src/main/java/org/apache/gravitino/catalog/SchemaNormalizeDispatcher.java
+++ b/core/src/main/java/org/apache/gravitino/catalog/SchemaNormalizeDispatcher.java
@@ -45,6 +45,18 @@ public class SchemaNormalizeDispatcher implements SchemaDispatcher {
@Override
public NameIdentifier[] listSchemas(Namespace namespace) throws NoSuchCatalogException {
+ // A namespace with more than two levels ([metalake, catalog]) carries a parentSchema, i.e. the
+ // caller is listing sub-schemas under a parent schema. Catalogs that do not support
+ // hierarchical schemas have no sub-schemas, so we must reject the request instead of returning
+ // the catalog's flat schemas as if they were children of the parent schema (see issue #11317).
+ if (namespace.length() > 2 && !supportsHierarchicalSchema(namespace)) {
+ throw new UnsupportedOperationException(
+ String.format(
+ "The catalog [%s] does not support hierarchical schemas, so listing schemas under a "
+ + "parent schema is not supported.",
+ NameIdentifier.of(namespace.levels())));
+ }
+
NameIdentifier[] identifiers = dispatcher.listSchemas(namespace);
// The constraints of the name spec may be more strict than underlying catalog,
// and for compatibility reasons, we only apply case-sensitive capabilities here.
@@ -84,6 +96,11 @@ public class SchemaNormalizeDispatcher implements SchemaDispatcher {
return dispatcher.dropSchema(normalizeNameIdentifier(ident), cascade);
}
+ private boolean supportsHierarchicalSchema(Namespace namespace) {
+ Capability capabilities = getCapability(NameIdentifier.of(namespace.levels()), catalogManager);
+ return capabilities.supportsHierarchicalSchema().supported();
+ }
+
private NameIdentifier normalizeNameIdentifier(NameIdentifier schemaIdent) {
Capability capabilities = getCapability(schemaIdent, catalogManager);
return applyCapabilities(schemaIdent, Capability.Scope.SCHEMA, capabilities);
diff --git a/core/src/main/java/org/apache/gravitino/connector/capability/Capability.java b/core/src/main/java/org/apache/gravitino/connector/capability/Capability.java
index ceaecce2cf..f787a16d4f 100644
--- a/core/src/main/java/org/apache/gravitino/connector/capability/Capability.java
+++ b/core/src/main/java/org/apache/gravitino/connector/capability/Capability.java
@@ -97,6 +97,19 @@ public interface Capability {
return DEFAULT.managedStorage(scope);
}
+ /**
+ * Check if the catalog supports hierarchical (nested) schemas. A hierarchical schema is a schema
+ * that can contain sub-schemas, addressed by a logical name using the configured external
+ * separator (e.g. {@code "A:B:C"}). Catalogs that only support flat schemas (such as Hive or JDBC
+ * catalogs) should keep the default unsupported result so that listing schemas under a parent
+ * schema is rejected instead of returning misleading data.
+ *
+ * @return The capability of the hierarchical schema support.
+ */
+ default CapabilityResult supportsHierarchicalSchema() {
+ return DEFAULT.supportsHierarchicalSchema();
+ }
+
/** The default implementation of the capability. */
class DefaultCapability implements Capability {
@@ -153,5 +166,11 @@ public interface Capability {
return CapabilityResult.unsupported(
String.format("The %s entity is not fully managed by Gravitino.", scope));
}
+
+ @Override
+ public CapabilityResult supportsHierarchicalSchema() {
+ return CapabilityResult.unsupported(
+ "The catalog does not support hierarchical (nested) schemas.");
+ }
}
}
diff --git a/core/src/test/java/org/apache/gravitino/catalog/TestSchemaNormalizeDispatcher.java b/core/src/test/java/org/apache/gravitino/catalog/TestSchemaNormalizeDispatcher.java
index e4fef5487e..fb438402b3 100644
--- a/core/src/test/java/org/apache/gravitino/catalog/TestSchemaNormalizeDispatcher.java
+++ b/core/src/test/java/org/apache/gravitino/catalog/TestSchemaNormalizeDispatcher.java
@@ -71,6 +71,21 @@ public class TestSchemaNormalizeDispatcher extends TestOperationDispatcher {
Assertions.assertFalse(schemaNormalizeDispatcher.schemaExists(schemaIdent));
}
+ @Test
+ public void testListSchemasUnderParentSchemaUnsupported() {
+ // The test catalog does not support hierarchical schemas, so listing schemas under a parent
+ // schema (a namespace deeper than [metalake, catalog]) must be rejected instead of returning
+ // the catalog's flat schemas (see issue #11317).
+ Namespace nestedNamespace = Namespace.of(metalake, catalog, "parent");
+ UnsupportedOperationException exception =
+ Assertions.assertThrows(
+ UnsupportedOperationException.class,
+ () -> schemaNormalizeDispatcher.listSchemas(nestedNamespace));
+ Assertions.assertTrue(
+ exception.getMessage().contains("does not support hierarchical schemas"),
+ exception.getMessage());
+ }
+
@Test
public void testNameSpec() {
NameIdentifier schemaIdent1 =