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

Support CASCADE option for DROP SCHEMA statement in Hive #18320

Merged
merged 1 commit into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
ebyhr marked this conversation as resolved.
Show resolved Hide resolved
// 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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a hive tables to ensure it is also no dropped ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ordering when dropping objects isn't ensured.

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