nk1506 commented on code in PR #9852:
URL: https://github.com/apache/iceberg/pull/9852#discussion_r1528242070


##########
hive-metastore/src/main/java/org/apache/iceberg/hive/HiveCatalog.java:
##########
@@ -222,53 +231,203 @@ public boolean dropTable(TableIdentifier identifier, 
boolean purge) {
 
   @Override
   public void renameTable(TableIdentifier from, TableIdentifier originalTo) {
-    if (!isValidIdentifier(from)) {
-      throw new NoSuchTableException("Invalid identifier: %s", from);
+    renameEntity(from, originalTo, "Table");
+  }
+
+  @Override
+  public boolean dropView(TableIdentifier identifier) {
+    if (!isValidIdentifier(identifier)) {
+      return false;
+    }
+
+    try {
+      String database = identifier.namespace().level(0);
+      String viewName = identifier.name();
+      Table table = clients.run(client -> client.getTable(database, viewName));
+      HiveOperationsBase.validateTableIsIcebergView(
+          table, CatalogUtil.fullTableName(name, identifier));
+
+      HiveViewOperations ops = (HiveViewOperations) newViewOps(identifier);
+      ViewMetadata lastViewMetadata = null;
+
+      try {
+        lastViewMetadata = ops.current();
+      } catch (NotFoundException e) {
+        LOG.warn(
+            "Failed to load table metadata for table: {}, continuing drop 
without purge",
+            identifier,
+            e);
+      }
+
+      clients.run(
+          client -> {
+            client.dropTable(database, viewName);
+            return null;
+          });
+
+      if (lastViewMetadata != null) {
+        CatalogUtil.dropViewMetaData(ops.io(), lastViewMetadata);
+      }
+
+      LOG.info("Dropped View: {}", identifier);
+      return true;
+
+    } catch (NoSuchViewException | NoSuchObjectException e) {
+      LOG.info("Skipping drop, View does not exist: {}", identifier, e);
+      return false;
+    } catch (TException e) {
+      throw new RuntimeException("Failed to drop " + identifier, e);
+    } catch (InterruptedException e) {
+      Thread.currentThread().interrupt();
+      throw new RuntimeException("Interrupted in call to dropView", e);
+    }
+  }
+
+  @Override
+  public List<TableIdentifier> listViews(Namespace namespace) {
+    Preconditions.checkArgument(
+        isValidateNamespace(namespace), "Missing database in namespace: %s", 
namespace);
+
+    try {
+      return listTablesByType(
+          namespace, TableType.VIRTUAL_VIEW, 
HiveOperationsBase.ICEBERG_VIEW_TYPE_VALUE);
+    } catch (UnknownDBException e) {
+      throw new NoSuchNamespaceException("Namespace does not exist: %s", 
namespace);
+
+    } catch (TException e) {
+      throw new RuntimeException("Failed to list all views under namespace " + 
namespace, e);
+
+    } catch (InterruptedException e) {
+      Thread.currentThread().interrupt();
+      throw new RuntimeException("Interrupted in call to listViews", e);
     }
+  }
+
+  private List<TableIdentifier> listTablesByType(
+      Namespace namespace, TableType tableType, String tableTypeProp)
+      throws TException, InterruptedException {
+    String database = namespace.level(0);
+    List<String> tableNames = clients.run(client -> client.getTables(database, 
"*", tableType));
+
+    // Retrieving the Table objects from HMS in batches to avoid OOM
+    List<TableIdentifier> filteredTableIdentifiers = Lists.newArrayList();
+    Iterable<List<String>> tableNameSets = Iterables.partition(tableNames, 
100);
+
+    for (List<String> tableNameSet : tableNameSets) {
+      filteredTableIdentifiers.addAll(filterIcebergTables(tableNameSet, 
namespace, tableTypeProp));
+    }
+
+    return filteredTableIdentifiers;
+  }
+
+  private List<TableIdentifier> filterIcebergTables(
+      List<String> tableNames, Namespace namespace, String tableTypeProp)
+      throws TException, InterruptedException {
+    List<Table> tableObjects =
+        clients.run(client -> client.getTableObjectsByName(namespace.level(0), 
tableNames));
+    return tableObjects.stream()
+        .filter(
+            table ->
+                table.getParameters() != null
+                    && tableTypeProp.equalsIgnoreCase(
+                        
table.getParameters().get(BaseMetastoreTableOperations.TABLE_TYPE_PROP)))
+        .map(table -> TableIdentifier.of(namespace, table.getTableName()))
+        .collect(Collectors.toList());
+  }
+
+  @Override
+  @SuppressWarnings("FormatStringAnnotation")
+  public void renameView(TableIdentifier from, TableIdentifier originalTo) {
+    if (!namespaceExists(originalTo.namespace())) {
+      throw new NoSuchNamespaceException(
+          "Cannot rename %s to %s. Namespace does not exist: %s",
+          from, originalTo, originalTo.namespace());
+    }
+    renameEntity(from, originalTo, "View");
+  }
 
-    TableIdentifier to = removeCatalogName(originalTo);
+  private void renameEntity(
+      TableIdentifier fromIdentifierEntity, TableIdentifier 
toIdentifierEntity, String entityType) {
+    if (!isValidIdentifier(fromIdentifierEntity)) {
+      throw new NoSuchViewException("Invalid identifier: %s", 
fromIdentifierEntity);
+    }
+
+    TableIdentifier to = removeCatalogName(toIdentifierEntity);
     Preconditions.checkArgument(isValidIdentifier(to), "Invalid identifier: 
%s", to);
 
     String toDatabase = to.namespace().level(0);
-    String fromDatabase = from.namespace().level(0);
-    String fromName = from.name();
+    String fromDatabase = fromIdentifierEntity.namespace().level(0);
+    String fromName = fromIdentifierEntity.name();
 
     try {
-      Table table = clients.run(client -> client.getTable(fromDatabase, 
fromName));
-      HiveOperationsBase.validateTableIsIceberg(table, fullTableName(name, 
from));
+      Table fromEntity = clients.run(client -> client.getTable(fromDatabase, 
fromName));
+      if (entityType.equalsIgnoreCase("Table")) {
+        HiveOperationsBase.validateTableIsIceberg(
+            fromEntity, CatalogUtil.fullTableName(name, fromIdentifierEntity));
+      } else {
+        HiveOperationsBase.validateTableIsIcebergView(
+            fromEntity, CatalogUtil.fullTableName(name, fromIdentifierEntity));
+      }
+
+      validateToTableForRename(fromIdentifierEntity, to);
 
-      table.setDbName(toDatabase);
-      table.setTableName(to.name());
+      fromEntity.setDbName(toDatabase);
+      fromEntity.setTableName(to.name());
 
       clients.run(
           client -> {
-            MetastoreUtil.alterTable(client, fromDatabase, fromName, table);
+            MetastoreUtil.alterTable(client, fromDatabase, fromName, 
fromEntity);
             return null;
           });
 
-      LOG.info("Renamed table from {}, to {}", from, to);
+      LOG.info("Renamed {} from {}, to {}", entityType, fromIdentifierEntity, 
to);
 
-    } catch (NoSuchObjectException e) {
-      throw new NoSuchTableException("Table does not exist: %s", from);
+    } catch (NoSuchObjectException | NoSuchViewException e) {
+      if (entityType.equalsIgnoreCase("Table")) {
+        throw new NoSuchTableException("Table does not exist: %s", 
fromIdentifierEntity);
+      } else {
+        throw new NoSuchViewException(
+            "Cannot rename %s to %s. View does not exist", 
fromIdentifierEntity, to);
+      }
 
     } catch (InvalidOperationException e) {
       if (e.getMessage() != null
           && e.getMessage().contains(String.format("new table %s already 
exists", to))) {
         throw new org.apache.iceberg.exceptions.AlreadyExistsException(
-            "Table already exists: %s", to);
+            "%s already exists: %s", entityType, to);
       } else {
-        throw new RuntimeException("Failed to rename " + from + " to " + to, 
e);
+        throw new RuntimeException("Failed to rename " + fromIdentifierEntity 
+ " to " + to, e);
       }
 
     } catch (TException e) {
-      throw new RuntimeException("Failed to rename " + from + " to " + to, e);
+      throw new RuntimeException("Failed to rename " + fromIdentifierEntity + 
" to " + to, e);
 
     } catch (InterruptedException e) {
       Thread.currentThread().interrupt();
       throw new RuntimeException("Interrupted in call to rename", e);
     }
   }
 
+  private void validateToTableForRename(TableIdentifier from, TableIdentifier 
to)

Review Comment:
   I have moved this to now renameContent as 
[here](https://github.com/apache/iceberg/pull/9852/files#diff-3fdd21d335b598f5354de287be6a8830d78e381c89274f0d6bf85c4c9966b8f0R386-R393)
 . 
   This was needed to throw error with explicit contentType in case of 
`renameView`. 
   
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to