Skip to content

Commit 7e43238

Browse files
committed
update
1 parent 3b639af commit 7e43238

5 files changed

Lines changed: 97 additions & 7 deletions

File tree

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -364,32 +364,37 @@ public void dropTable(Identifier identifier, boolean ignoreIfNotExists)
364364
checkNotBranch(identifier, "dropTable");
365365
checkNotSystemTable(identifier, "dropTable");
366366

367+
if (!tableExists(identifier)) {
368+
if (ignoreIfNotExists) {
369+
return;
370+
}
371+
throw new TableNotExistException(identifier);
372+
}
373+
367374
Set<Path> externalPaths = new HashSet<>();
368-
try {
375+
if (tableExistsInFileSystem(getTableLocation(identifier), DEFAULT_MAIN_BRANCH)) {
369376
Table table = getTable(identifier);
370377
if (table instanceof FileStoreTable) {
371378
FileStoreTable fileStoreTable = (FileStoreTable) table;
372379
List<Path> schemaExternalPaths =
373380
getSchemaExternalPaths(fileStoreTable.schemaManager().listAll());
374381
externalPaths.addAll(schemaExternalPaths);
375-
// get table branch external path
376382
List<String> branches = fileStoreTable.branchManager().branches();
377383
for (String branch : branches) {
378384
SchemaManager schemaManager =
379385
fileStoreTable.schemaManager().copyWithBranch(branch);
380386
externalPaths.addAll(getSchemaExternalPaths(schemaManager.listAll()));
381387
}
382388
}
383-
} catch (TableNotExistException e) {
384-
if (ignoreIfNotExists) {
385-
return;
386-
}
387-
throw new TableNotExistException(identifier);
388389
}
389390

390391
dropTableImpl(identifier, new ArrayList<>(externalPaths));
391392
}
392393

394+
protected boolean tableExists(Identifier identifier) {
395+
return tableExistsInFileSystem(getTableLocation(identifier), DEFAULT_MAIN_BRANCH);
396+
}
397+
393398
private List<Path> getSchemaExternalPaths(List<TableSchema> schemas) {
394399
if (schemas == null) {
395400
return Collections.emptyList();

paimon-core/src/main/java/org/apache/paimon/jdbc/JdbcCatalog.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,16 @@ protected TableSchema loadTableSchema(Identifier identifier) throws TableNotExis
451451
() -> new RuntimeException("There is no paimon table in " + tableLocation));
452452
}
453453

454+
@Override
455+
protected boolean tableExists(Identifier identifier) {
456+
return JdbcUtils.tableExists(
457+
connections,
458+
catalogKey,
459+
identifier.getDatabaseName(),
460+
identifier.getTableName())
461+
|| super.tableExists(identifier);
462+
}
463+
454464
@Override
455465
public boolean caseSensitive() {
456466
return false;

paimon-core/src/test/java/org/apache/paimon/jdbc/JdbcCatalogTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.paimon.catalog.CatalogContext;
2323
import org.apache.paimon.catalog.CatalogTestBase;
2424
import org.apache.paimon.catalog.Identifier;
25+
import org.apache.paimon.fs.Path;
2526
import org.apache.paimon.options.CatalogOptions;
2627
import org.apache.paimon.options.Options;
2728
import org.apache.paimon.schema.Schema;
@@ -86,6 +87,27 @@ private JdbcCatalog initCatalog(Map<String, String> props) {
8687
@Test
8788
public void testGetTable() throws Exception {}
8889

90+
@Test
91+
public void testDropTableWhenTablePathMissing() throws Exception {
92+
String databaseName = "test_db";
93+
String tableName = "new_table";
94+
catalog.createDatabase(databaseName, false);
95+
Identifier identifier = Identifier.create(databaseName, tableName);
96+
catalog.createTable(identifier, DEFAULT_TABLE_SCHEMA, false);
97+
98+
JdbcCatalog jdbcCatalog = (JdbcCatalog) catalog;
99+
Path path = jdbcCatalog.getTableLocation(identifier);
100+
jdbcCatalog.fileIO().deleteDirectoryQuietly(path);
101+
102+
assertThatThrownBy(() -> catalog.getTable(identifier))
103+
.isInstanceOf(RuntimeException.class)
104+
.hasMessage("There is no paimon table in " + path);
105+
assertThat(jdbcCatalog.listTables(databaseName)).contains(tableName);
106+
107+
jdbcCatalog.dropTable(identifier, false);
108+
assertThat(jdbcCatalog.listTables(databaseName)).doesNotContain(tableName);
109+
}
110+
89111
@Test
90112
public void testAcquireLockFail() throws SQLException, InterruptedException {
91113
String lockId = "jdbc.testDb.testTable";

paimon-hive/paimon-hive-catalog/src/main/java/org/apache/paimon/hive/HiveCatalog.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,27 @@ public List<String> listDatabases() {
303303
}
304304
}
305305

306+
@Override
307+
protected boolean tableExists(Identifier identifier) {
308+
try {
309+
boolean inHms =
310+
clients()
311+
.run(
312+
client ->
313+
client.tableExists(
314+
identifier.getDatabaseName(),
315+
identifier.getTableName()));
316+
return inHms || super.tableExists(identifier);
317+
} catch (TException e) {
318+
throw new RuntimeException(
319+
"Cannot determine if table " + identifier.getFullName() + " exists.", e);
320+
} catch (InterruptedException e) {
321+
Thread.currentThread().interrupt();
322+
throw new RuntimeException(
323+
"Interrupted in call to tableExists " + identifier.getFullName(), e);
324+
}
325+
}
326+
306327
@Override
307328
protected void createDatabaseImpl(String name, Map<String, String> properties) {
308329
try {

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.apache.paimon.catalog.CatalogTestBase;
2525
import org.apache.paimon.catalog.Identifier;
2626
import org.apache.paimon.client.ClientPool;
27+
import org.apache.paimon.fs.Path;
2728
import org.apache.paimon.options.CatalogOptions;
2829
import org.apache.paimon.options.Options;
2930
import org.apache.paimon.partition.Partition;
@@ -281,6 +282,37 @@ public void testAlterHiveTableParameters() {
281282
}
282283
}
283284

285+
@Test
286+
public void testDropTableWhenTablePathMissing() throws Exception {
287+
String databaseName = "test_db";
288+
String tableName = "new_table";
289+
catalog.createDatabase(databaseName, false);
290+
Identifier identifier = Identifier.create(databaseName, tableName);
291+
292+
Schema schema =
293+
new Schema(
294+
Lists.newArrayList(
295+
new DataField(0, "pk", DataTypes.INT()),
296+
new DataField(1, "col1", DataTypes.STRING()),
297+
new DataField(2, "col2", DataTypes.STRING())),
298+
Collections.emptyList(),
299+
Collections.emptyList(),
300+
new HashMap<>(),
301+
"");
302+
catalog.createTable(identifier, schema, false);
303+
304+
HiveCatalog hiveCatalog = (HiveCatalog) catalog;
305+
Path path = hiveCatalog.getTableLocation(identifier);
306+
hiveCatalog.fileIO().deleteDirectoryQuietly(path);
307+
308+
assertThatThrownBy(() -> hiveCatalog.getTable(identifier))
309+
.isInstanceOf(Catalog.TableNotExistException.class);
310+
assertThat(hiveCatalog.listTables(databaseName)).contains(tableName);
311+
312+
hiveCatalog.dropTable(identifier, false);
313+
assertThat(hiveCatalog.listTables(databaseName)).doesNotContain(tableName);
314+
}
315+
284316
@Test
285317
public void testListTablesLock() {
286318
try {

0 commit comments

Comments
 (0)