Skip to content

Commit 6690253

Browse files
author
xiaohongbo
committed
[hive] Fix no longer possible to drop the hms table issue when table not in fs (apache#4853)
1 parent 4620f30 commit 6690253

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

paimon-core/src/main/java/org/apache/paimon/catalog/AbstractCatalog.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,14 @@ public void dropTable(Identifier identifier, boolean ignoreIfNotExists)
272272
try {
273273
getTable(identifier);
274274
} catch (TableNotExistException e) {
275+
if (!tableExistsInFileSystem(getTableLocation(identifier), DEFAULT_MAIN_BRANCH)) {
276+
dropTableImpl(identifier);
277+
}
275278
if (ignoreIfNotExists) {
276279
return;
280+
} else {
281+
throw new TableNotExistException(identifier);
277282
}
278-
throw new TableNotExistException(identifier);
279283
}
280284

281285
dropTableImpl(identifier);

paimon-hive/paimon-hive-catalog/src/test/java/org/apache/paimon/hive/HiveCatalogTest.java

+34
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
import org.junit.jupiter.api.io.TempDir;
4545

4646
import java.lang.reflect.Field;
47+
import java.nio.file.Path;
48+
import java.nio.file.Paths;
4749
import java.util.ArrayList;
4850
import java.util.Arrays;
4951
import java.util.Collections;
@@ -59,6 +61,7 @@
5961
import static org.apache.paimon.hive.HiveCatalog.TABLE_TYPE_PROP;
6062
import static org.assertj.core.api.Assertions.assertThat;
6163
import static org.assertj.core.api.Assertions.assertThatThrownBy;
64+
import static org.junit.Assert.assertThrows;
6265
import static org.junit.jupiter.api.Assertions.assertEquals;
6366
import static org.junit.jupiter.api.Assertions.fail;
6467

@@ -407,6 +410,37 @@ public void testListTables() throws Exception {
407410
catalog.dropDatabase(databaseName, true, true);
408411
}
409412

413+
@Test
414+
public void testDropTable() throws Exception {
415+
String databaseName = "drop_table_test_db";
416+
String tableName = "drop_table_test_table";
417+
catalog.dropDatabase(databaseName, true, true);
418+
catalog.createDatabase(databaseName, true);
419+
Identifier identifier = Identifier.create(databaseName, tableName);
420+
421+
// test ignore if exists
422+
catalog.createTable(
423+
identifier, Schema.newBuilder().column("col", DataTypes.INT()).build(), true);
424+
Path path = Paths.get(catalog.warehouse(), databaseName.concat(".db"), tableName);
425+
catalog.fileIO().delete(new org.apache.paimon.fs.Path(path.toString()), true);
426+
List<String> tables = catalog.listTables(databaseName);
427+
assertEquals(1, tables.size());
428+
catalog.dropTable(identifier, true);
429+
List<String> newTables = catalog.listTables(databaseName);
430+
assertEquals(0, newTables.size());
431+
432+
// test not ignore if exists
433+
catalog.createTable(
434+
identifier, Schema.newBuilder().column("col", DataTypes.INT()).build(), true);
435+
catalog.fileIO().delete(new org.apache.paimon.fs.Path(path.toString()), true);
436+
tables = catalog.listTables(databaseName);
437+
assertEquals(1, tables.size());
438+
assertThrows(
439+
Catalog.TableNotExistException.class, () -> catalog.dropTable(identifier, false));
440+
441+
catalog.dropDatabase(databaseName, true, true);
442+
}
443+
410444
@Override
411445
protected boolean supportsView() {
412446
return true;

paimon-spark/paimon-spark-ut/src/test/scala/org/apache/paimon/spark/sql/DDLWithHiveCatalogTestBase.scala

+23
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,29 @@ abstract class DDLWithHiveCatalogTestBase extends PaimonHiveTestBase {
621621
}
622622
}
623623

624+
test("Paimon DDL with hive catalog: drop table which location has been deleted") {
625+
Seq("paimon", sparkCatalogName, paimonHiveCatalogName).foreach {
626+
catalogName =>
627+
spark.sql(s"USE $catalogName")
628+
withDatabase("paimon_db") {
629+
spark.sql(s"CREATE DATABASE paimon_db")
630+
spark.sql(s"USE paimon_db")
631+
spark.sql("CREATE TABLE t USING paimon")
632+
val table = loadTable("paimon_db", "t")
633+
table.fileIO().delete(table.location(), true)
634+
if (catalogName.equals("paimon")) {
635+
// Filesystem catalog determines whether a table exists based on table location
636+
assert(spark.sql("SHOW TABLES").count() == 0)
637+
} else {
638+
// Hive catalog determines whether a table exists based on metadata in hms
639+
assert(spark.sql("SHOW TABLES").count() == 1)
640+
}
641+
spark.sql("DROP TABLE IF EXISTS t")
642+
assert(spark.sql("SHOW TABLES").count() == 0)
643+
}
644+
}
645+
}
646+
624647
def getDatabaseProp(dbName: String, propertyName: String): String = {
625648
spark
626649
.sql(s"DESC DATABASE EXTENDED $dbName")

0 commit comments

Comments
 (0)