You are viewing a plain text version of this content. The canonical link for it is here.
Posted to gitbox@hive.apache.org by GitBox <gi...@apache.org> on 2020/08/26 05:28:09 UTC

[GitHub] [hive] adesh-rao commented on a change in pull request #1419: HIVE-22782: Consolidate metastore call to fetch constraints

adesh-rao commented on a change in pull request #1419:
URL: https://github.com/apache/hive/pull/1419#discussion_r477042339



##########
File path: standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/client/TestGetAllTableConstraints.java
##########
@@ -0,0 +1,145 @@
+package org.apache.hadoop.hive.metastore.client;
+
+import org.apache.hadoop.hive.metastore.IMetaStoreClient;
+import org.apache.hadoop.hive.metastore.MetaStoreTestUtils;
+import org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest;
+import org.apache.hadoop.hive.metastore.api.AllTableConstraintsRequest;
+import org.apache.hadoop.hive.metastore.api.Catalog;
+import org.apache.hadoop.hive.metastore.api.Database;
+import org.apache.hadoop.hive.metastore.api.NoSuchObjectException;
+import org.apache.hadoop.hive.metastore.api.PrimaryKeysRequest;
+import org.apache.hadoop.hive.metastore.api.SQLAllTableConstraints;
+import org.apache.hadoop.hive.metastore.api.Table;
+import org.apache.hadoop.hive.metastore.client.builder.CatalogBuilder;
+import org.apache.hadoop.hive.metastore.client.builder.DatabaseBuilder;
+import org.apache.hadoop.hive.metastore.client.builder.TableBuilder;
+import org.apache.hadoop.hive.metastore.minihms.AbstractMetaStoreService;
+import org.apache.thrift.TException;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import static org.apache.hadoop.hive.metastore.Warehouse.DEFAULT_DATABASE_NAME;
+
+@RunWith(Parameterized.class)
+@Category(MetastoreCheckinTest.class)
+public class TestGetAllTableConstraints extends MetaStoreClientTest {
+  private static final String OTHER_DATABASE = "test_constraints_other_database";
+  private static final String OTHER_CATALOG = "test_constraints_other_catalog";
+  private static final String DATABASE_IN_OTHER_CATALOG = "test_constraints_database_in_other_catalog";
+  private final AbstractMetaStoreService metaStore;
+  private IMetaStoreClient client;
+  private Table[] testTables = new Table[3];
+  private Database inOtherCatalog;
+
+  public TestGetAllTableConstraints(String name, AbstractMetaStoreService metaStore) throws Exception {
+    this.metaStore = metaStore;
+  }
+  @Before
+  public void setUp() throws Exception {
+    // Get new client
+    client = metaStore.getClient();
+
+    // Clean up the database
+    client.dropDatabase(OTHER_DATABASE, true, true, true);
+    // Drop every table in the default database
+    for(String tableName : client.getAllTables(DEFAULT_DATABASE_NAME)) {
+      client.dropTable(DEFAULT_DATABASE_NAME, tableName, true, true, true);
+    }
+
+    client.dropDatabase(OTHER_CATALOG, DATABASE_IN_OTHER_CATALOG, true, true, true);
+    try {
+      client.dropCatalog(OTHER_CATALOG);
+    } catch (NoSuchObjectException e) {
+      // NOP
+    }
+
+    // Clean up trash
+    metaStore.cleanWarehouseDirs();
+
+    new DatabaseBuilder().setName(OTHER_DATABASE).create(client, metaStore.getConf());
+
+    Catalog cat = new CatalogBuilder()
+        .setName(OTHER_CATALOG)
+        .setLocation(MetaStoreTestUtils.getTestWarehouseDir(OTHER_CATALOG))
+        .build();
+    client.createCatalog(cat);
+
+    // For this one don't specify a location to make sure it gets put in the catalog directory
+    inOtherCatalog = new DatabaseBuilder()
+        .setName(DATABASE_IN_OTHER_CATALOG)
+        .setCatalogName(OTHER_CATALOG)
+        .create(client, metaStore.getConf());
+
+    testTables[0] =
+        new TableBuilder()
+            .setTableName("test_table_1")
+            .addCol("col1", "int")
+            .addCol("col2", "varchar(32)")
+            .create(client, metaStore.getConf());
+
+    testTables[1] =
+        new TableBuilder()
+            .setDbName(OTHER_DATABASE)
+            .setTableName("test_table_2")
+            .addCol("col1", "int")
+            .addCol("col2", "varchar(32)")
+            .create(client, metaStore.getConf());
+
+    testTables[2] =
+        new TableBuilder()
+            .inDb(inOtherCatalog)
+            .setTableName("test_table_3")
+            .addCol("col1", "int")
+            .addCol("col2", "varchar(32)")
+            .create(client, metaStore.getConf());
+
+    // Reload tables from the MetaStore
+    for(int i=0; i < testTables.length; i++) {
+      testTables[i] = client.getTable(testTables[i].getCatName(), testTables[i].getDbName(),
+          testTables[i].getTableName());
+    }
+  }
+
+  @After
+  public void tearDown() throws Exception {
+    try {
+      if (client != null) {
+        try {
+          client.close();
+        } catch (Exception e) {
+          // HIVE-19729: Shallow the exceptions based on the discussion in the Jira
+        }
+      }
+    } finally {
+      client = null;
+    }
+  }
+
+
+  @Test
+  public void NoConstraints() throws TException{
+    Table table = testTables[0];
+
+    // Make sure get on a table with no key returns empty list
+    PrimaryKeysRequest rqst =
+        new PrimaryKeysRequest(table.getDbName(), table.getTableName());
+    AllTableConstraintsRequest request = new AllTableConstraintsRequest(table.getDbName(),table.getTableName());
+
+    request.setCatName(table.getCatName());
+    rqst.setCatName(table.getCatName());
+    SQLAllTableConstraints fetched = client.getAllTableConstraints(request);
+
+    Assert.assertTrue(fetched.getCheckConstraints().isEmpty());
+    Assert.assertTrue(fetched.getForeignKeys().isEmpty());
+    Assert.assertTrue(fetched.getDefaultConstraints().isEmpty());
+    Assert.assertTrue(fetched.getNotNullConstraints().isEmpty());
+    Assert.assertTrue(fetched.getPrimaryKeys().isEmpty());
+    Assert.assertTrue(fetched.getUniqueConstraints().isEmpty());
+  }
+

Review comment:
       Also add tests for 
   1) all non-empty constraints
   2) few constraints are present
   3) more than 1 constraints are present of a single type on one table.
   4) having multiple tables and then fetching the constraints

##########
File path: standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/ObjectStore.java
##########
@@ -11286,6 +11287,20 @@ private String getPrimaryKeyConstraintName(String catName, String dbName, String
     return notNullConstraints;
   }
 
+  @Override
+  public SQLAllTableConstraints getAllTableConstraints(String catName, String db_name, String tbl_name)
+      throws MetaException {
+    debugLog("Get all table constraints for the table - " + catName + "."+ db_name+"."+tbl_name + " in class ObjectStore.java");
+    SQLAllTableConstraints sqlAllTableConstraints = new SQLAllTableConstraints();

Review comment:
       Let's have a followup jira to have overall one db call to fetch all constraints instead of having one for each type of constraint.




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

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



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org