diff --git a/plugin/trino-hive/src/main/java/io/trino/plugin/hive/metastore/SemiTransactionalHiveMetastore.java b/plugin/trino-hive/src/main/java/io/trino/plugin/hive/metastore/SemiTransactionalHiveMetastore.java index 4445a374964b..7d79a6fa7ff7 100644 --- a/plugin/trino-hive/src/main/java/io/trino/plugin/hive/metastore/SemiTransactionalHiveMetastore.java +++ b/plugin/trino-hive/src/main/java/io/trino/plugin/hive/metastore/SemiTransactionalHiveMetastore.java @@ -469,29 +469,33 @@ private static boolean isCreatedBy(Database database, String queryId) } public synchronized void dropDatabase(ConnectorSession session, String schemaName) + { + setExclusive((delegate, hdfsEnvironment) -> { + boolean deleteData = shouldDeleteDatabaseData(session, schemaName); + delegate.dropDatabase(schemaName, deleteData); + }); + } + + public boolean shouldDeleteDatabaseData(ConnectorSession session, String schemaName) { Optional location = delegate.getDatabase(schemaName) .orElseThrow(() -> new SchemaNotFoundException(schemaName)) .getLocation() .map(Path::new); - setExclusive((delegate, hdfsEnvironment) -> { - // If we see files in the schema location, don't delete it. - // If we see no files, request deletion. - // If we fail to check the schema location, behave according to fallback. - boolean deleteData = location.map(path -> { - try { - return !hdfsEnvironment.getFileSystem(new HdfsContext(session), path) - .listLocatedStatus(path).hasNext(); - } - catch (IOException | RuntimeException e) { - log.warn(e, "Could not check schema directory '%s'", path); - return deleteSchemaLocationsFallback; - } - }).orElse(deleteSchemaLocationsFallback); - - delegate.dropDatabase(schemaName, deleteData); - }); + // If we see files in the schema location, don't delete it. + // If we see no files, request deletion. + // If we fail to check the schema location, behave according to fallback. + return location.map(path -> { + try { + return !hdfsEnvironment.getFileSystem(new HdfsContext(session), path) + .listLocatedStatus(path).hasNext(); + } + catch (IOException | RuntimeException e) { + log.warn(e, "Could not check schema directory '%s'", path); + return deleteSchemaLocationsFallback; + } + }).orElse(deleteSchemaLocationsFallback); } public synchronized void renameDatabase(String source, String target)