diff --git a/core/src/main/java/org/apache/iceberg/BaseMetastoreCatalog.java b/core/src/main/java/org/apache/iceberg/BaseMetastoreCatalog.java index e960fe2b63e0..29068df380a9 100644 --- a/core/src/main/java/org/apache/iceberg/BaseMetastoreCatalog.java +++ b/core/src/main/java/org/apache/iceberg/BaseMetastoreCatalog.java @@ -113,7 +113,7 @@ private Table loadMetadataTable(TableIdentifier identifier) { } } - private boolean isValidMetadataIdentifier(TableIdentifier identifier) { + protected boolean isValidMetadataIdentifier(TableIdentifier identifier) { return MetadataTableType.from(identifier.name()) != null && isValidIdentifier(TableIdentifier.of(identifier.namespace().levels())); } diff --git a/hive-metastore/src/main/java/org/apache/iceberg/hive/HiveCatalog.java b/hive-metastore/src/main/java/org/apache/iceberg/hive/HiveCatalog.java index 1cf738d736cb..9fd7c6f2eeb0 100644 --- a/hive-metastore/src/main/java/org/apache/iceberg/hive/HiveCatalog.java +++ b/hive-metastore/src/main/java/org/apache/iceberg/hive/HiveCatalog.java @@ -412,6 +412,43 @@ private void validateTableIsIcebergTableOrView( } } + /** + * Check whether table or metadata table exists. + * + *
Note: If a hive table with the same identifier exists in catalog, this method will return
+ * {@code false}.
+ *
+ * @param identifier a table identifier
+ * @return true if the table exists, false otherwise
+ */
+ @Override
+ public boolean tableExists(TableIdentifier identifier) {
+ TableIdentifier baseTableIdentifier = identifier;
+ if (!isValidIdentifier(identifier)) {
+ if (!isValidMetadataIdentifier(identifier)) {
+ return false;
+ } else {
+ baseTableIdentifier = TableIdentifier.of(identifier.namespace().levels());
+ }
+ }
+
+ String database = baseTableIdentifier.namespace().level(0);
+ String tableName = baseTableIdentifier.name();
+ try {
+ Table table = clients.run(client -> client.getTable(database, tableName));
+ HiveOperationsBase.validateTableIsIceberg(table, fullTableName(name, baseTableIdentifier));
+ return true;
+ } catch (NoSuchTableException | NoSuchObjectException e) {
+ return false;
+ } catch (TException e) {
+ throw new RuntimeException("Failed to check table existence of " + baseTableIdentifier, e);
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ throw new RuntimeException(
+ "Interrupted in call to check table existence of " + baseTableIdentifier, e);
+ }
+ }
+
@Override
public void createNamespace(Namespace namespace, Map