Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[hotfix] Fix drop database not working in terminal #3363

Merged
merged 10 commits into from
Dec 31, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,29 @@ public void alterNamespace(String[] namespace, NamespaceChange... changes) {
throw new UnsupportedOperationException("Cannot apply namespace change");
}

@Override
public boolean dropNamespace(String[] namespace) {
public boolean dropNamespace(String[] namespace, boolean cascade)
throws NoSuchNamespaceException {
String database = namespaceToDatabase(namespace);
if (!unifiedCatalog.databaseExists(database)) {
throw new NoSuchNamespaceException(namespace);
}
List<TableIDWithFormat> tables = unifiedCatalog.listTables(database);
if (!tables.isEmpty() && !cascade) {
throw new IllegalStateException("Namespace '" + database + "' is non empty.");
}

for (TableIDWithFormat id : tables) {
unifiedCatalog.dropTable(database, id.getIdentifier().getTableName(), true);
}
unifiedCatalog.dropDatabase(database);
return !unifiedCatalog.databaseExists(database);
}

@Override
public boolean dropNamespace(String[] namespace) throws NoSuchNamespaceException {
return dropNamespace(namespace, false);
}

@Override
public Identifier[] listTables(String[] namespace) throws NoSuchNamespaceException {
String database = namespaceToDatabase(namespace);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ public void testTableFormats(TableFormat format, boolean sessionCatalog) {
long count = sql(sqlText).count();
Assertions.assertEquals(expect, count);

// create and drop namespace
testNamespaceWithSql();

// visit sub tables.
testVisitSubTable(format, sessionCatalog);

Expand All @@ -108,6 +111,17 @@ public void testTableFormats(TableFormat format, boolean sessionCatalog) {
Assertions.assertFalse(unifiedCatalog().tableExists(target().database, target().table));
}

private void testNamespaceWithSql() {
// Use SparkTestBase::sql method to test SparkUnifiedCatalog instead of CommonUnifiedCatalog.
String createDatabase = "CREATE DATABASE test_unified_catalog";
sql(createDatabase);
Assertions.assertTrue(unifiedCatalog().databaseExists("test_unified_catalog"));

String dropDatabase = "DROP DATABASE test_unified_catalog";
sql(dropDatabase);
Assertions.assertFalse(unifiedCatalog().databaseExists("test_unified_catalog"));
}

private String pkDDL(TableFormat format) {
if (TableFormat.MIXED_HIVE.equals(format) || TableFormat.MIXED_ICEBERG.equals(format)) {
return ", primary key(id)";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.apache.spark.sql.catalyst.analysis.NoSuchFunctionException;
import org.apache.spark.sql.catalyst.analysis.NoSuchNamespaceException;
import org.apache.spark.sql.catalyst.analysis.NoSuchTableException;
import org.apache.spark.sql.catalyst.analysis.NonEmptyNamespaceException;
import org.apache.spark.sql.connector.catalog.FunctionCatalog;
import org.apache.spark.sql.connector.catalog.Identifier;
import org.apache.spark.sql.connector.catalog.SupportsNamespaces;
Expand Down Expand Up @@ -69,26 +68,6 @@ public UnboundFunction loadFunction(Identifier ident) throws NoSuchFunctionExcep
throw new NoSuchFunctionException(ident);
}

/**
* Drop a namespace from the catalog with cascade mode, recursively dropping all objects within
* the namespace if cascade is true.
*
* <p>If the catalog implementation does not support this operation, it may throw {@link
* UnsupportedOperationException}.
*
* @param namespace a multi-part namespace
* @param cascade When true, deletes all objects under the namespace
* @return true if the namespace was dropped
* @throws NoSuchNamespaceException If the namespace does not exist (optional)
* @throws NonEmptyNamespaceException If the namespace is non-empty and cascade is false
* @throws UnsupportedOperationException If drop is not a supported operation
*/
@Override
public boolean dropNamespace(String[] namespace, boolean cascade)
throws NoSuchNamespaceException, NonEmptyNamespaceException {
return false;
}

/**
* Load table metadata of a specific version by {@link Identifier identifier} from the catalog.
*
Expand Down
Loading