Skip to content

Commit

Permalink
Core: Fix HadoopCatalog drop table behavior (apache#3097)
Browse files Browse the repository at this point in the history
Dropping a table should return false if it does not exist.

Co-authored-by: adityasingh <[email protected]>
  • Loading branch information
itachi-sharingan and adityasingh authored Sep 13, 2021
1 parent 9641ec8 commit 9156464
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
24 changes: 11 additions & 13 deletions core/src/main/java/org/apache/iceberg/hadoop/HadoopCatalog.java
Original file line number Diff line number Diff line change
Expand Up @@ -252,21 +252,19 @@ public boolean dropTable(TableIdentifier identifier, boolean purge) {

Path tablePath = new Path(defaultWarehouseLocation(identifier));
TableOperations ops = newTableOps(identifier);
TableMetadata lastMetadata;
if (purge && ops.current() != null) {
lastMetadata = ops.current();
} else {
lastMetadata = null;
}

TableMetadata lastMetadata = ops.current();
try {
if (purge && lastMetadata != null) {
// Since the data files and the metadata files may store in different locations,
// so it has to call dropTableData to force delete the data file.
CatalogUtil.dropTableData(ops.io(), lastMetadata);
if (lastMetadata == null) {
LOG.debug("Not an iceberg table: {}", identifier);
return false;
} else {
if (purge) {
// Since the data files and the metadata files may store in different locations,
// so it has to call dropTableData to force delete the data file.
CatalogUtil.dropTableData(ops.io(), lastMetadata);
}
return fs.delete(tablePath, true /* recursive */);
}
fs.delete(tablePath, true /* recursive */);
return true;
} catch (IOException e) {
throw new RuntimeIOException(e, "Failed to delete file: %s", tablePath);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,24 @@ public void testDropTable() throws Exception {
Assert.assertFalse(fs.isDirectory(new Path(metaLocation)));
}

@Test
public void testDropNonIcebergTable() throws Exception {
Configuration conf = new Configuration();
String warehousePath = temp.newFolder().getAbsolutePath();
HadoopCatalog catalog = new HadoopCatalog(conf, warehousePath);
TableIdentifier testTable = TableIdentifier.of("db", "ns1", "ns2", "tbl");
String metaLocation = catalog.defaultWarehouseLocation(testTable);
//testing with non existent directory
Assert.assertFalse(catalog.dropTable(testTable));

FileSystem fs = Util.getFs(new Path(metaLocation), conf);
fs.mkdirs(new Path(metaLocation));
Assert.assertTrue(fs.isDirectory(new Path(metaLocation)));

Assert.assertFalse(catalog.dropTable(testTable));
Assert.assertTrue(fs.isDirectory(new Path(metaLocation)));
}

@Test
public void testRenameTable() throws Exception {
Configuration conf = new Configuration();
Expand Down

0 comments on commit 9156464

Please sign in to comment.