Skip to content

Commit

Permalink
[hotfix] Fix drop database not working in terminal (#3363)
Browse files Browse the repository at this point in the history
* Implement dropNamespace

* test namespace for SparkUnifiedCatalog

* Implement cascade or non-cascading drop namespace in base

* Restore private

* fix

* Remove redundant method

---------

Co-authored-by: big face cat <[email protected]>
  • Loading branch information
MarigWeizhi and huyuanfeng2018 authored Dec 31, 2024
1 parent 893ae22 commit 907973e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
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

0 comments on commit 907973e

Please sign in to comment.