ajantha-bhat commented on code in PR #8907:
URL: https://github.com/apache/iceberg/pull/8907#discussion_r1374227697


##########
hive-metastore/src/test/java/org/apache/iceberg/hive/TestHiveViewCatalog.java:
##########
@@ -0,0 +1,197 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg.hive;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import java.util.Collections;
+import org.apache.iceberg.Transaction;
+import org.apache.iceberg.catalog.Catalog;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.exceptions.AlreadyExistsException;
+import org.apache.iceberg.view.ViewCatalogTests;
+import org.assertj.core.api.Assumptions;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+public class TestHiveViewCatalog extends ViewCatalogTests<HiveCatalog> {
+
+  private HiveMetastoreSetup hiveMetastoreSetup;
+
+  @BeforeEach
+  public void before() throws Exception {
+    hiveMetastoreSetup = new HiveMetastoreSetup(Collections.emptyMap());
+  }
+
+  @AfterEach
+  public void after() throws Exception {
+    hiveMetastoreSetup.stopMetastore();
+  }
+
+  @Override
+  protected HiveCatalog catalog() {
+    return hiveMetastoreSetup.catalog;
+  }
+
+  @Override
+  protected Catalog tableCatalog() {
+    return hiveMetastoreSetup.catalog;
+  }
+
+  @Override
+  protected boolean requiresNamespaceCreate() {
+    return true;
+  }
+
+  // Override few tests which are using AlreadyExistsException instead of 
NoSuchViewException

Review Comment:
   I have some of this problem for Nessie catalog extending `ViewCatalogTests`. 
Should we change ViewCatalogTests to accept multiple options like I did for 
Nessie 
(https://github.com/apache/iceberg/pull/8909/files#diff-9c8b69f86aec86aea82cd6675c1c24267d9a9d77d7715c2c792934cb954b328bR403-R409)
 or generalize the code to throw expected exception? 



##########
hive-metastore/src/main/java/org/apache/iceberg/hive/HiveCatalog.java:
##########
@@ -264,6 +279,138 @@ public void renameTable(TableIdentifier from, 
TableIdentifier originalTo) {
     }
   }
 
+  @Override
+  public boolean viewExists(TableIdentifier identifier) {
+    return HiveCatalogUtil.isTableWithTypeExists(clients, identifier, 
TableType.VIRTUAL_VIEW);
+  }
+
+  @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));
+      HiveCatalogUtil.validateTableIsIcebergView(table, fullTableName(name, 
identifier));
+      clients.run(
+          client -> {
+            client.dropTable(database, viewName);
+            return null;
+          });
+      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) {

Review Comment:
   Can we just pass TableType and reduce code duplication like I did for 
Nessie? 
   
https://github.com/apache/iceberg/pull/8909/files#diff-4abd6d90c69587709bc754be9ea305080395bad9ac778176a0e3ab2563ac8464R145
   
   Similar comment for all the view related APIs
   



##########
hive-metastore/src/test/java/org/apache/iceberg/hive/HiveMetastoreSetup.java:
##########
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg.hive;
+
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.metastore.HiveMetaStoreClient;
+import org.apache.iceberg.CatalogProperties;
+import org.apache.iceberg.CatalogUtil;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+
+/**
+ * Setup HiveMetastore. It does not create any database. All the tests should 
create a database
+ * accordingly. It should replace the existing setUp class {@link 
HiveMetastoreTest}
+ */
+class HiveMetastoreSetup {

Review Comment:
   We can directly call static methods (start/stop) from `HiveMetastoreTest` in 
before(). So, no need of this class?



##########
hive-metastore/src/main/java/org/apache/iceberg/hive/HiveViewOperations.java:
##########
@@ -0,0 +1,346 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg.hive;
+
+import java.util.Collections;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hive.common.StatsSetupConst;
+import org.apache.hadoop.hive.metastore.IMetaStoreClient;
+import org.apache.hadoop.hive.metastore.TableType;
+import org.apache.hadoop.hive.metastore.api.InvalidObjectException;
+import org.apache.hadoop.hive.metastore.api.NoSuchObjectException;
+import org.apache.hadoop.hive.metastore.api.SerDeInfo;
+import org.apache.hadoop.hive.metastore.api.StorageDescriptor;
+import org.apache.hadoop.hive.metastore.api.Table;
+import org.apache.iceberg.BaseMetastoreTableOperations;
+import org.apache.iceberg.ClientPool;
+import org.apache.iceberg.SchemaParser;
+import org.apache.iceberg.TableProperties;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.exceptions.AlreadyExistsException;
+import org.apache.iceberg.exceptions.CommitFailedException;
+import org.apache.iceberg.exceptions.CommitStateUnknownException;
+import org.apache.iceberg.exceptions.NoSuchViewException;
+import org.apache.iceberg.exceptions.ValidationException;
+import org.apache.iceberg.hadoop.ConfigProperties;
+import org.apache.iceberg.io.FileIO;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.apache.iceberg.view.BaseViewOperations;
+import org.apache.iceberg.view.ViewMetadata;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** Hive implementation of Iceberg ViewOperations. */
+final class HiveViewOperations extends BaseViewOperations {
+  private static final Logger LOG = 
LoggerFactory.getLogger(HiveViewOperations.class);
+
+  private final String fullName;
+  private final String database;
+  private final String tableName;
+  private final Configuration conf;
+  private final FileIO fileIO;
+  private final ClientPool<IMetaStoreClient, TException> metaClients;
+  private final long maxHiveTablePropertySize;
+  private final TableIdentifier identifier;
+
+  HiveViewOperations(
+      Configuration conf,
+      ClientPool<IMetaStoreClient, TException> metaClients,
+      FileIO fileIO,
+      String catalogName,
+      TableIdentifier tableIdentifier) {
+    this.identifier = tableIdentifier;
+    String dbName = tableIdentifier.namespace().level(0);
+    this.conf = conf;
+    this.metaClients = metaClients;
+    this.fileIO = fileIO;
+    this.fullName = catalogName + "." + dbName + "." + tableIdentifier.name();
+    this.database = dbName;
+    this.tableName = tableIdentifier.name();
+    this.maxHiveTablePropertySize =
+        conf.getLong(
+            HiveCatalogUtil.HIVE_TABLE_PROPERTY_MAX_SIZE,
+            HiveCatalogUtil.HIVE_TABLE_PROPERTY_MAX_SIZE_DEFAULT);
+  }
+
+  @Override
+  public ViewMetadata current() {
+    if (HiveCatalogUtil.isTableWithTypeExists(metaClients, identifier, 
TableType.EXTERNAL_TABLE)) {
+      throw new AlreadyExistsException(
+          "Table with same name already exists: %s.%s", database, tableName);
+    }
+    return super.current();
+  }
+
+  @Override
+  public void doRefresh() {
+    String metadataLocation = null;
+    try {
+      Table table = metaClients.run(client -> client.getTable(database, 
tableName));
+      HiveCatalogUtil.validateTableIsIcebergView(table, fullName);
+      metadataLocation =
+          
table.getParameters().get(BaseMetastoreTableOperations.METADATA_LOCATION_PROP);
+    } catch (NoSuchObjectException e) {
+      if (currentMetadataLocation() != null) {
+        throw new NoSuchViewException("View does not exist: %s.%s", database, 
tableName);
+      }
+    } catch (TException e) {
+      String errMsg =
+          String.format("Failed to get view info from metastore %s.%s", 
database, tableName);
+      throw new RuntimeException(errMsg, e);
+
+    } catch (InterruptedException e) {
+      Thread.currentThread().interrupt();
+      throw new RuntimeException("Interrupted during refresh", e);
+    }
+    refreshFromMetadataLocation(metadataLocation);
+  }
+
+  @SuppressWarnings("checkstyle:CyclomaticComplexity")
+  @Override
+  public void doCommit(ViewMetadata base, ViewMetadata metadata) {
+    boolean newTable = base == null;
+    String newMetadataLocation = writeNewMetadataIfRequired(metadata);
+    boolean keepHiveStats = conf.getBoolean(ConfigProperties.KEEP_HIVE_STATS, 
false);
+
+    boolean updateHiveTable = false;
+
+    try {
+
+      Table tbl = loadHmsTable();
+
+      if (tbl != null) {
+        // If we try to create the view but the metadata location is already 
set, then we had a
+        // concurrent commit
+        if (newTable
+            && 
tbl.getParameters().get(BaseMetastoreTableOperations.METADATA_LOCATION_PROP)
+                != null) {
+          throw new AlreadyExistsException("View already exists: %s.%s", 
database, tableName);
+        }
+
+        updateHiveTable = true;
+        LOG.debug("Committing existing view: {}", fullName);
+      } else {
+        tbl = newHmsTable(metadata);
+        LOG.debug("Committing new view: {}", fullName);
+      }
+
+      tbl.setSd(storageDescriptor(metadata)); // set to pickup any schema 
changes
+
+      String metadataLocation =
+          
tbl.getParameters().get(BaseMetastoreTableOperations.METADATA_LOCATION_PROP);
+      String baseMetadataLocation = base != null ? base.metadataFileLocation() 
: null;
+      if (!Objects.equals(baseMetadataLocation, metadataLocation)) {
+        throw new CommitFailedException(
+            "Base metadata location '%s' is not same as the current table 
metadata location '%s' for %s.%s",
+            baseMetadataLocation, metadataLocation, database, tableName);
+      }
+
+      setHmsTableParameters(newMetadataLocation, tbl, metadata);
+
+      if (!keepHiveStats) {
+        tbl.getParameters().remove(StatsSetupConst.COLUMN_STATS_ACCURATE);
+      }
+
+      try {
+        persistTable(tbl, updateHiveTable, baseMetadataLocation);
+
+      } catch (LockException le) {
+        throw new CommitStateUnknownException(

Review Comment:
   Can we extract all common code between `HiveTableOperations` and 
`HiveViewOperations` to a Util?



-- 
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