@@ -362,8 +362,8 @@ public void createPartitions(Identifier identifier, List<Map<String, String>> pa
362
362
String dataFilePath =
363
363
hmsTable .getParameters ().containsKey (DATA_FILE_PATH_DIRECTORY .key ())
364
364
? sd .getLocation ()
365
- + "/"
366
- + hmsTable .getParameters ().get (DATA_FILE_PATH_DIRECTORY .key ())
365
+ + "/"
366
+ + hmsTable .getParameters ().get (DATA_FILE_PATH_DIRECTORY .key ())
367
367
: sd .getLocation ();
368
368
List <Partition > hivePartitions = new ArrayList <>();
369
369
for (Map <String , String > partitionSpec : partitions ) {
@@ -373,7 +373,7 @@ public void createPartitions(Identifier identifier, List<Map<String, String>> pa
373
373
dataFilePath
374
374
+ "/"
375
375
+ PartitionPathUtils .generatePartitionPath (
376
- new LinkedHashMap <>(partitionSpec )));
376
+ new LinkedHashMap <>(partitionSpec )));
377
377
hivePartition .setDbName (identifier .getDatabaseName ());
378
378
hivePartition .setTableName (identifier .getTableName ());
379
379
hivePartition .setValues (new ArrayList <>(partitionSpec .values ()));
@@ -692,9 +692,9 @@ private TableSchema loadTableSchema(Identifier identifier, Table table)
692
692
throws TableNotExistException {
693
693
if (isPaimonTable (table )) {
694
694
return tableSchemaInFileSystem (
695
- getTableLocation (identifier , table ),
696
- identifier .getBranchNameOrDefault ())
697
- .orElseThrow (() -> new TableNotExistException (identifier ));
695
+ getTableLocation (identifier , table ),
696
+ identifier .getBranchNameOrDefault ())
697
+ .orElseThrow (() -> new HmsTableNotExistInFsException (identifier ));
698
698
}
699
699
700
700
if (!formatTableDisabled ()) {
@@ -705,7 +705,7 @@ private TableSchema loadTableSchema(Identifier identifier, Table table)
705
705
}
706
706
}
707
707
708
- throw new TableNotExistException (identifier );
708
+ throw new HmsTableNotExistInFsException (identifier );
709
709
}
710
710
711
711
@ Override
@@ -888,6 +888,31 @@ private boolean usingExternalTable(Map<String, String> tableOptions) {
888
888
|| "TRUE" .equalsIgnoreCase (externalPropValue );
889
889
}
890
890
891
+ @ Override
892
+ public void dropTable (Identifier identifier , boolean ignoreIfNotExists )
893
+ throws TableNotExistException {
894
+ checkNotBranch (identifier , "dropTable" );
895
+ checkNotSystemTable (identifier , "dropTable" );
896
+
897
+ try {
898
+ getTable (identifier );
899
+ } catch (TableNotExistException e ) {
900
+ if (e instanceof HmsTableNotExistInFsException ) {
901
+ LOG .warn (
902
+ "Table {}.{} exists in Hive metastore, but not exist in file system. will try to drop it in Hive metastore only." ,
903
+ identifier .getDatabaseName (),
904
+ identifier .getTableName ());
905
+ dropHmsTableQuietly (identifier );
906
+ }
907
+ if (ignoreIfNotExists ) {
908
+ return ;
909
+ }
910
+ throw new TableNotExistException (identifier );
911
+ }
912
+
913
+ dropTableImpl (identifier );
914
+ }
915
+
891
916
@ Override
892
917
protected void dropTableImpl (Identifier identifier ) {
893
918
try {
@@ -1080,9 +1105,9 @@ protected void alterTableImpl(Identifier identifier, List<SchemaChange> changes)
1080
1105
// first commit changes to underlying files
1081
1106
schema = runWithLock (identifier , () -> schemaManager .commitChanges (changes ));
1082
1107
} catch (TableNotExistException
1083
- | ColumnAlreadyExistException
1084
- | ColumnNotExistException
1085
- | RuntimeException e ) {
1108
+ | ColumnAlreadyExistException
1109
+ | ColumnNotExistException
1110
+ | RuntimeException e ) {
1086
1111
throw e ;
1087
1112
} catch (Exception e ) {
1088
1113
throw new RuntimeException ("Failed to alter table " + identifier .getFullName (), e );
@@ -1648,4 +1673,47 @@ public int getBatchGetTableSize() {
1648
1673
return DEFAULT_TABLE_BATCH_SIZE ;
1649
1674
}
1650
1675
}
1651
- }
1676
+
1677
+ private void dropHmsTableQuietly (Identifier identifier ) throws TableNotExistException {
1678
+ try {
1679
+ boolean externalTable = isExternalTable (getHmsTable (identifier ));
1680
+ clients .execute (
1681
+ client ->
1682
+ client .dropTable (
1683
+ identifier .getDatabaseName (),
1684
+ identifier .getTableName (),
1685
+ !externalTable ,
1686
+ false ,
1687
+ true ));
1688
+ } catch (InterruptedException e ) {
1689
+ Thread .currentThread ().interrupt ();
1690
+ LOG .warn (
1691
+ "Interrupted in call to drop hms table {} which does not exist in file system" ,
1692
+ identifier .getFullName (),
1693
+ e );
1694
+ } catch (Exception e ) {
1695
+ LOG .warn (
1696
+ "Failed to drop hms table {} which does not exist in file system" ,
1697
+ identifier .getFullName (),
1698
+ e );
1699
+ }
1700
+ }
1701
+
1702
+ /**
1703
+ * Exception for trying to operate on a table which exists in Hive metastore but doesn't exist
1704
+ * in file system.
1705
+ */
1706
+ public static class HmsTableNotExistInFsException extends TableNotExistException {
1707
+
1708
+ protected static final String MSG =
1709
+ "Table %s exists in Hive metastore but does not exist in file system." ;
1710
+
1711
+ public HmsTableNotExistInFsException (Identifier identifier ) {
1712
+ this (identifier , null );
1713
+ }
1714
+
1715
+ public HmsTableNotExistInFsException (Identifier identifier , Throwable cause ) {
1716
+ super (MSG , identifier , cause );
1717
+ }
1718
+ }
1719
+ }
0 commit comments