Skip to content

Commit

Permalink
Support CASCADE option for DROP SCHEMA in Hive
Browse files Browse the repository at this point in the history
  • Loading branch information
ebyhr committed Aug 22, 2023
1 parent 79931e7 commit f0536d1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -952,9 +952,36 @@ public void createSchema(ConnectorSession session, String schemaName, Map<String
}

@Override
public void dropSchema(ConnectorSession session, String schemaName)
public void dropSchema(ConnectorSession session, String schemaName, boolean cascade)
{
metastore.dropDatabase(session, schemaName);
if (cascade) {
// List all objects first because such operations after adding/dropping/altering tables/views in a transaction is disallowed
List<SchemaTableName> views = listViews(session, Optional.of(schemaName));
List<SchemaTableName> tables = listTables(session, Optional.of(schemaName)).stream()
.filter(table -> !views.contains(table))
.collect(toImmutableList());

for (SchemaTableName viewName : views) {
dropView(session, viewName);
}

for (SchemaTableName tableName : tables) {
ConnectorTableHandle table = getTableHandle(session, tableName);
if (table == null) {
log.debug("Table disappeared during DROP SCHEMA CASCADE: %s", tableName);
continue;
}
dropTable(session, table);
}

// Commit and then drop database with raw metastore because exclusive operation after dropping object is disallowed in SemiTransactionalHiveMetastore
metastore.commit();
boolean deleteData = metastore.shouldDeleteDatabaseData(session, schemaName);
metastore.unsafeGetRawHiveMetastoreClosure().dropDatabase(schemaName, deleteData);
}
else {
metastore.dropDatabase(session, schemaName);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,6 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
case SUPPORTS_TOPN_PUSHDOWN:
return false;

case SUPPORTS_DROP_SCHEMA_CASCADE:
return false;

case SUPPORTS_ADD_FIELD:
case SUPPORTS_DROP_FIELD:
case SUPPORTS_RENAME_FIELD:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2095,6 +2095,27 @@ public void testDropStatsPartitionedTable()
assertUpdate("DROP TABLE " + getFullyQualifiedTestTableName(tableName));
}

@Test
public void testUnsupportedDropSchemaCascadeWithNonHiveTable()
{
String schemaName = "test_unsupported_drop_schema_cascade_" + randomNameSuffix();
String icebergTableName = "test_dummy_iceberg_table" + randomNameSuffix();

hiveMinioDataLake.getHiveHadoop().runOnHive("CREATE DATABASE %2$s LOCATION 's3a://%1$s/%2$s'".formatted(bucketName, schemaName));
try {
hiveMinioDataLake.getHiveHadoop().runOnHive("CREATE TABLE " + schemaName + "." + icebergTableName + " TBLPROPERTIES ('table_type'='iceberg') AS SELECT 1 a");

assertQueryFails("DROP SCHEMA " + schemaName + " CASCADE", "\\QCannot query Iceberg table '%s.%s'".formatted(schemaName, icebergTableName));

assertThat(computeActual("SHOW SCHEMAS").getOnlyColumnAsSet()).contains(schemaName);
assertThat(computeActual("SHOW TABLES FROM " + schemaName).getOnlyColumnAsSet()).contains(icebergTableName);
assertThat(hiveMinioDataLake.getMinioClient().listObjects(bucketName, schemaName).stream()).isNotEmpty();
}
finally {
hiveMinioDataLake.getHiveHadoop().runOnHive("DROP DATABASE IF EXISTS " + schemaName + " CASCADE");
}
}

private void renamePartitionResourcesOutsideTrino(String tableName, String partitionColumn, String regionKey)
{
String partitionName = format("%s=%s", partitionColumn, regionKey);
Expand Down

0 comments on commit f0536d1

Please sign in to comment.