This is an automated email from the ASF dual-hosted git repository.

mbod pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hive.git


The following commit(s) were added to refs/heads/master by this push:
     new aa09f42  HIVE-26036: Fix NPE caused by getMTable() in ObjectStore 
(#3104) (Wechar Yu, reviewed by Marton Bod)
aa09f42 is described below

commit aa09f429edb632ad8bfcce3515458f17449b11cc
Author: Wechar Yu <[email protected]>
AuthorDate: Thu Mar 24 16:46:35 2022 +0800

    HIVE-26036: Fix NPE caused by getMTable() in ObjectStore (#3104) (Wechar 
Yu, reviewed by Marton Bod)
---
 .../apache/hadoop/hive/metastore/ObjectStore.java  | 27 +++++++++++
 .../hadoop/hive/metastore/TestObjectStore.java     | 55 ++++++++++++++++++++++
 2 files changed, 82 insertions(+)

diff --git 
a/standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/ObjectStore.java
 
b/standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/ObjectStore.java
index 52a16f3..9b7d28b 100644
--- 
a/standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/ObjectStore.java
+++ 
b/standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/ObjectStore.java
@@ -2583,6 +2583,11 @@ public class ObjectStore implements RawStore, 
Configurable {
       List<MTablePrivilege> tabGrants = null;
       List<MTableColumnPrivilege> tabColumnGrants = null;
       MTable table = this.getMTable(catName, dbName, tblName);
+      if (table == null) {
+        throw new InvalidObjectException("Unable to add partitions because "
+            + TableName.getQualified(catName, dbName, tblName) +
+            " does not exist");
+      }
       if 
("TRUE".equalsIgnoreCase(table.getParameters().get("PARTITION_LEVEL_PRIVILEGE")))
 {
         tabGrants = this.listAllTableGrants(catName, dbName, tblName);
         tabColumnGrants = this.listTableAllColumnGrants(catName, dbName, 
tblName);
@@ -2651,6 +2656,11 @@ public class ObjectStore implements RawStore, 
Configurable {
       List<MTablePrivilege> tabGrants = null;
       List<MTableColumnPrivilege> tabColumnGrants = null;
       MTable table = this.getMTable(catName, dbName, tblName);
+      if (table == null) {
+        throw new InvalidObjectException("Unable to add partitions because "
+            + TableName.getQualified(catName, dbName, tblName) +
+            " does not exist");
+      }
       if 
("TRUE".equalsIgnoreCase(table.getParameters().get("PARTITION_LEVEL_PRIVILEGE")))
 {
         tabGrants = this.listAllTableGrants(catName, dbName, tblName);
         tabColumnGrants = this.listTableAllColumnGrants(catName, dbName, 
tblName);
@@ -2711,6 +2721,11 @@ public class ObjectStore implements RawStore, 
Configurable {
       openTransaction();
       String catName = part.isSetCatName() ? part.getCatName() : 
getDefaultCatalog(conf);
       MTable table = this.getMTable(catName, part.getDbName(), 
part.getTableName());
+      if (table == null) {
+        throw new InvalidObjectException("Unable to add partition because "
+            + TableName.getQualified(catName, part.getDbName(), 
part.getTableName()) +
+            " does not exist");
+      }
       List<MTablePrivilege> tabGrants = null;
       List<MTableColumnPrivilege> tabColumnGrants = null;
       if 
("TRUE".equalsIgnoreCase(table.getParameters().get("PARTITION_LEVEL_PRIVILEGE")))
 {
@@ -2773,6 +2788,11 @@ public class ObjectStore implements RawStore, 
Configurable {
     try {
       openTransaction();
       MTable table = this.getMTable(catName, dbName, tableName);
+      if (table == null) {
+        throw new NoSuchObjectException("Unable to get partition because "
+            + TableName.getQualified(catName, dbName, tableName) +
+            " does not exist");
+      }
       MPartition mpart = getMPartition(catName, dbName, tableName, part_vals, 
table);
       part = convertToPart(mpart, false);
       committed = commitTransaction();
@@ -5168,6 +5188,10 @@ public class ObjectStore implements RawStore, 
Configurable {
       openTransaction();
 
       MTable table = this.getMTable(catName, dbName, tblName);
+      if (table == null) {
+        throw new NoSuchObjectException(
+            TableName.getQualified(catName, dbName, tblName) + " table not 
found");
+      }
       List<String> partNames = new ArrayList<>();
       for (List<String> partVal : part_vals) {
         partNames.add(
@@ -10022,6 +10046,9 @@ public class ObjectStore implements RawStore, 
Configurable {
     Boolean isCompliant = null;
     if (writeIdList != null) {
       MTable table = this.getMTable(catName, dbName, tableName);
+      if (table == null) {
+        throw new NoSuchObjectException(TableName.getQualified(catName, 
dbName, tableName) + " table not found");
+      }
       isCompliant = !TxnUtils.isTransactionalTable(table.getParameters())
         || (areTxnStatsSupported && isCurrentStatsValidForTheQuery(table, 
writeIdList, false));
     }
diff --git 
a/standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/TestObjectStore.java
 
b/standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/TestObjectStore.java
index 3766082..d4d81ed 100644
--- 
a/standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/TestObjectStore.java
+++ 
b/standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/TestObjectStore.java
@@ -51,6 +51,8 @@ import 
org.apache.hadoop.hive.metastore.api.NotificationEventRequest;
 import org.apache.hadoop.hive.metastore.api.NotificationEventResponse;
 import org.apache.hadoop.hive.metastore.api.Package;
 import org.apache.hadoop.hive.metastore.api.Partition;
+import org.apache.hadoop.hive.metastore.api.PartitionListComposingSpec;
+import org.apache.hadoop.hive.metastore.api.PartitionSpec;
 import org.apache.hadoop.hive.metastore.api.PrincipalType;
 import org.apache.hadoop.hive.metastore.api.PrivilegeBag;
 import org.apache.hadoop.hive.metastore.api.PrivilegeGrantInfo;
@@ -76,6 +78,7 @@ import org.apache.hadoop.hive.metastore.metrics.Metrics;
 import org.apache.hadoop.hive.metastore.metrics.MetricsConstants;
 import org.apache.hadoop.hive.metastore.model.MNotificationLog;
 import org.apache.hadoop.hive.metastore.model.MNotificationNextId;
+import org.apache.hadoop.hive.metastore.partition.spec.PartitionSpecProxy;
 import org.apache.hadoop.hive.metastore.utils.MetaStoreServerUtils;
 import org.junit.Assert;
 import org.junit.Assume;
@@ -349,6 +352,12 @@ public class TestObjectStore {
     objectStore.dropDatabase(db1.getCatalogName(), DB1);
   }
 
+  @Test (expected = NoSuchObjectException.class)
+  public void testTableOpsWhenTableDoesNotExist() throws 
NoSuchObjectException, MetaException {
+    List<String> colNames = Arrays.asList("c0", "c1");
+    objectStore.getTableColumnStatistics(DEFAULT_CATALOG_NAME, DB1, 
"not_existed_table", colNames, ENGINE, "");
+  }
+
   private StorageDescriptor createFakeSd(String location) {
     return new StorageDescriptor(null, location, null, null, false, 0,
         new SerDeInfo("SerDeName", "serializationLib", null), null, null, 
null);
@@ -439,6 +448,52 @@ public class TestObjectStore {
     }
   }
 
+  @Test
+  public void testPartitionOpsWhenTableDoesNotExist() throws 
InvalidObjectException, MetaException {
+    List<String> value1 = Arrays.asList("US", "CA");
+    StorageDescriptor sd1 = createFakeSd("location1");
+    HashMap<String, String> partitionParams = new HashMap<>();
+    partitionParams.put("PARTITION_LEVEL_PRIVILEGE", "true");
+    Partition part1 = new Partition(value1, DB1, "not_existed_table", 111, 
111, sd1, partitionParams);
+    try {
+      objectStore.addPartition(part1);
+    } catch (InvalidObjectException e) {
+      // expected
+    }
+    try {
+      objectStore.getPartition(DEFAULT_CATALOG_NAME, DB1, "not_existed_table", 
value1);
+    } catch (NoSuchObjectException e) {
+      // expected
+    }
+
+    List<String> value2 = Arrays.asList("US", "MA");
+    StorageDescriptor sd2 = createFakeSd("location2");
+    Partition part2 = new Partition(value2, DB1, "not_existed_table", 222, 
222, sd2, partitionParams);
+    List<Partition> parts = Arrays.asList(part1, part2);
+    try {
+      objectStore.addPartitions(DEFAULT_CATALOG_NAME, DB1, 
"not_existed_table", parts);
+    } catch (InvalidObjectException e) {
+      // expected
+    }
+
+    PartitionSpec partitionSpec1 = new PartitionSpec(DB1, "not_existed_table", 
"location1");
+    partitionSpec1.setPartitionList(new PartitionListComposingSpec(parts));
+    PartitionSpecProxy partitionSpecProxy = 
PartitionSpecProxy.Factory.get(Arrays.asList(partitionSpec1));
+    try {
+      objectStore.addPartitions(DEFAULT_CATALOG_NAME, DB1, 
"not_existed_table", partitionSpecProxy, true);
+    } catch (InvalidObjectException e) {
+      // expected
+    }
+
+    List<List<String>> part_vals = Arrays.asList(Arrays.asList("US", "GA"), 
Arrays.asList("US", "WA"));
+    try {
+      objectStore.alterPartitions(DEFAULT_CATALOG_NAME, DB1, 
"not_existed_table", part_vals, parts, 0, "");
+    } catch (MetaException e) {
+      // expected
+      Assert.assertTrue(e.getCause() instanceof NoSuchObjectException);
+    }
+  }
+
   /**
    * Test the concurrent drop of same partition would leak transaction.
    * https://issues.apache.org/jira/browse/HIVE-16839

Reply via email to