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

ayushsaxena 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 7bb378ba197 HIVE-23935. Fetching primaryKey through beeline fails with 
NPE. (#4009).  (Ayush Saxena reviewed by Laszlo Bodor)
7bb378ba197 is described below

commit 7bb378ba1975710d14fcf73e0ae0dfa03f7a8976
Author: Ayush Saxena <ayushsax...@apache.org>
AuthorDate: Fri Feb 3 15:54:37 2023 +0530

    HIVE-23935. Fetching primaryKey through beeline fails with NPE. (#4009).  
(Ayush Saxena reviewed by Laszlo Bodor)
---
 .../apache/hadoop/hive/metastore/ObjectStore.java  | 14 ++++-----
 .../hadoop/hive/metastore/TestObjectStore.java     | 35 ++++++++++++++++++++++
 2 files changed, 42 insertions(+), 7 deletions(-)

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 0e8caf785a7..e11677dfe34 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
@@ -12052,22 +12052,22 @@ public class ObjectStore implements RawStore, 
Configurable {
   }
 
   private List<SQLPrimaryKey> getPrimaryKeysInternal(final String catName,
-                                                     final String 
db_name_input,
-                                                     final String 
tbl_name_input)
+                                                     final String dbNameInput,
+                                                     final String tblNameInput)
   throws MetaException, NoSuchObjectException {
-    final String db_name = normalizeIdentifier(db_name_input);
-    final String tbl_name = normalizeIdentifier(tbl_name_input);
-    return new GetListHelper<SQLPrimaryKey>(catName, db_name, tbl_name, true, 
true) {
+    final String dbName = dbNameInput != null ? 
normalizeIdentifier(dbNameInput) : null;
+    final String tblName = normalizeIdentifier(tblNameInput);
+    return new GetListHelper<SQLPrimaryKey>(catName, dbName, tblName, true, 
true) {
 
       @Override
       protected List<SQLPrimaryKey> 
getSqlResult(GetHelper<List<SQLPrimaryKey>> ctx) throws MetaException {
-        return directSql.getPrimaryKeys(catName, db_name, tbl_name);
+        return directSql.getPrimaryKeys(catName, dbName, tblName);
       }
 
       @Override
       protected List<SQLPrimaryKey> getJdoResult(
         GetHelper<List<SQLPrimaryKey>> ctx) throws MetaException, 
NoSuchObjectException {
-        return getPrimaryKeysViaJdo(catName, db_name, tbl_name);
+        return getPrimaryKeysViaJdo(catName, dbName, tblName);
       }
     }.run(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 0bcbdeb9a10..79036c2c22c 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
@@ -125,6 +125,7 @@ import static org.hamcrest.CoreMatchers.hasItems;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.core.Is.is;
 import static org.hamcrest.core.IsNot.not;
+import static org.junit.Assert.assertEquals;
 
 @Category(MetastoreUnitTest.class)
 public class TestObjectStore {
@@ -1261,6 +1262,40 @@ public class TestObjectStore {
     setAndCheckSSLProperties(true, "", "", "jks");
   }
 
+  /**
+   * Tests getPrimaryKeys() when db_name isn't specified.
+   */
+  @Test
+  public void testGetPrimaryKeys() throws Exception {
+    Database db1 =
+        new DatabaseBuilder().setName(DB1).setDescription("description")
+            .setLocation("locationurl").build(conf);
+    objectStore.createDatabase(db1);
+    StorageDescriptor sd1 = new StorageDescriptor(
+        ImmutableList.of(new FieldSchema("pk_col", "double", null)), 
"location",
+        null, null, false, 0,
+        new SerDeInfo("SerDeName", "serializationLib", null), null, null, 
null);
+    HashMap<String, String> params = new HashMap<>();
+    params.put("EXTERNAL", "false");
+    Table tbl1 =
+        new Table(TABLE1, DB1, "owner", 1, 2, 3, sd1, null, params, null, null,
+            "MANAGED_TABLE");
+    objectStore.createTable(tbl1);
+
+    SQLPrimaryKey pk =
+        new SQLPrimaryKey(DB1, TABLE1, "pk_col", 1, "pk_const_1", false, false,
+            false);
+    pk.setCatName(DEFAULT_CATALOG_NAME);
+    objectStore.addPrimaryKeys(ImmutableList.of(pk));
+
+    // Primary key retrieval should be success, even if db_name isn't 
specified.
+    assertEquals("pk_col",
+        objectStore.getPrimaryKeys(DEFAULT_CATALOG_NAME, null, TABLE1).get(0)
+            .getColumn_name());
+    objectStore.dropTable(DEFAULT_CATALOG_NAME, DB1, TABLE1);
+    objectStore.dropDatabase(db1.getCatalogName(), DB1);
+  }
+
   /**
    * Helper method for setting and checking the SSL configuration parameters.
    * @param useSSL whether or not SSL is enabled

Reply via email to