Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#1439] fix(hive): remove the data when drop hive table (#1558)

FANNGb80127e6878f
diff --git a/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveCatalogOperations.java b/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveCatalogOperations.java
index 5b60f8d8aa..099f47c831 100644
--- a/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveCatalogOperations.java
+++ b/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveCatalogOperations.java
@@ -854,18 +854,25 @@ public class HiveCatalogOperations implements CatalogOperations, SupportsSchemas
}
/**
- * Drops a table from the Hive Metastore.
+ * Drops a table from the Hive Metastore. Deletes the table and removes the directory associated
+ * with the table from the file system if the table is not EXTERNAL table. In case of an external
+ * table, only the associated metadata information is removed.
*
* @param tableIdent The identifier of the table to drop.
* @return true if the table is successfully dropped; false if the table does not exist.
*/
@Override
public boolean dropTable(NameIdentifier tableIdent) {
- return dropHiveTable(tableIdent, false, false);
+ if (isExternalTable(tableIdent)) {
+ return dropHiveTable(tableIdent, false, false);
+ } else {
+ return dropHiveTable(tableIdent, true, false);
+ }
}
/**
- * Purges a table from the Hive Metastore.
+ * Purges a table from the Hive Metastore. Completely purge the table skipping trash for managed
+ * table, external table aren't supported to purge table.
*
* @param tableIdent The identifier of the table to purge.
* @return true if the table is successfully purged; false if the table does not exist.
@@ -873,9 +880,7 @@ public class HiveCatalogOperations implements CatalogOperations, SupportsSchemas
*/
@Override
public boolean purgeTable(NameIdentifier tableIdent) throws UnsupportedOperationException {
- org.apache.hadoop.hive.metastore.api.Table hiveTable = loadHiveTable(tableIdent);
- boolean isExternal = EXTERNAL_TABLE.name().equalsIgnoreCase(hiveTable.getTableType());
- if (isExternal) {
+ if (isExternalTable(tableIdent)) {
throw new UnsupportedOperationException("Can't purge a external hive table");
} else {
return dropHiveTable(tableIdent, true, true);
@@ -886,6 +891,8 @@ public class HiveCatalogOperations implements CatalogOperations, SupportsSchemas
* Checks if the given namespace is a valid namespace for the Hive schema.
*
* @param tableIdent The namespace to validate.
+ * @param deleteData Whether to delete the table data.
+ * @param ifPurge Whether to purge the table.
* @return true if the namespace is valid; otherwise, false.
*/
private boolean dropHiveTable(NameIdentifier tableIdent, boolean deleteData, boolean ifPurge) {
@@ -943,4 +950,9 @@ public class HiveCatalogOperations implements CatalogOperations, SupportsSchemas
public PropertiesMetadata schemaPropertiesMetadata() throws UnsupportedOperationException {
return schemaPropertiesMetadata;
}
+
+ private boolean isExternalTable(NameIdentifier tableIdent) {
+ org.apache.hadoop.hive.metastore.api.Table hiveTable = loadHiveTable(tableIdent);
+ return EXTERNAL_TABLE.name().equalsIgnoreCase(hiveTable.getTableType());
+ }
}
diff --git a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/hive/CatalogHiveIT.java b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/hive/CatalogHiveIT.java
index eaa11b6dc6..5ef0ed8157 100644
--- a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/hive/CatalogHiveIT.java
+++ b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/hive/CatalogHiveIT.java
@@ -1126,6 +1126,58 @@ public class CatalogHiveIT extends AbstractIT {
NoSuchMetalakeException.class, () -> client.loadMetalake(NameIdentifier.of(metalakeName1)));
}
+ @Test
+ public void testDropHiveManagedTable() throws TException, InterruptedException, IOException {
+ ColumnDTO[] columns = createColumns();
+ catalog
+ .asTableCatalog()
+ .createTable(
+ NameIdentifier.of(metalakeName, catalogName, schemaName, tableName),
+ columns,
+ TABLE_COMMENT,
+ createProperties(),
+ new Transform[] {IdentityPartitioningDTO.of(columns[2].name())});
+ // Directly get table from hive metastore to check if the table is created successfully.
+ org.apache.hadoop.hive.metastore.api.Table hiveTab =
+ hiveClientPool.run(client -> client.getTable(schemaName, tableName));
+ checkTableReadWrite(hiveTab);
+ Assertions.assertEquals(MANAGED_TABLE.name(), hiveTab.getTableType());
+ Path tableDirectory = new Path(hiveTab.getSd().getLocation());
+ catalog
+ .asTableCatalog()
+ .dropTable(NameIdentifier.of(metalakeName, catalogName, schemaName, tableName));
+ Boolean existed = hiveClientPool.run(client -> client.tableExists(schemaName, tableName));
+ Assertions.assertFalse(existed, "The hive table should not exist");
+ Assertions.assertFalse(hdfs.exists(tableDirectory), "The table directory should not exist");
+ }
+
+ @Test
+ public void testDropHiveExternalTable() throws TException, InterruptedException, IOException {
+ ColumnDTO[] columns = createColumns();
+ catalog
+ .asTableCatalog()
+ .createTable(
+ NameIdentifier.of(metalakeName, catalogName, schemaName, tableName),
+ columns,
+ TABLE_COMMENT,
+ ImmutableMap.of(TABLE_TYPE, EXTERNAL_TABLE.name().toLowerCase(Locale.ROOT)),
+ new Transform[] {IdentityPartitioningDTO.of(columns[2].name())});
+ // Directly get table from hive metastore to check if the table is created successfully.
+ org.apache.hadoop.hive.metastore.api.Table hiveTab =
+ hiveClientPool.run(client -> client.getTable(schemaName, tableName));
+ checkTableReadWrite(hiveTab);
+ Assertions.assertEquals(EXTERNAL_TABLE.name(), hiveTab.getTableType());
+ catalog
+ .asTableCatalog()
+ .dropTable(NameIdentifier.of(metalakeName, catalogName, schemaName, tableName));
+
+ Boolean existed = hiveClientPool.run(client -> client.tableExists(schemaName, tableName));
+ Assertions.assertFalse(existed, "The table should be not exist");
+ Path tableDirectory = new Path(hiveTab.getSd().getLocation());
+ Assertions.assertTrue(
+ hdfs.listStatus(tableDirectory).length > 0, "The table should not be empty");
+ }
+
@Test
public void testPurgeHiveManagedTable() throws TException, InterruptedException, IOException {
ColumnDTO[] columns = createColumns();