pkumarsinha commented on code in PR #4807:
URL: https://github.com/apache/hive/pull/4807#discussion_r1389724695


##########
ql/src/test/org/apache/hadoop/hive/ql/exec/TestGetPartitionAuthWithBatches.java:
##########
@@ -0,0 +1,274 @@
+/*
+ * 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.hadoop.hive.ql.exec;
+
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.metastore.HiveMetaStoreClient;
+
+import org.apache.hadoop.hive.metastore.api.GetPartitionsPsWithAuthRequest;
+import org.apache.hadoop.hive.metastore.api.MetaException;
+import org.apache.hadoop.hive.metastore.api.Table;
+import org.apache.hadoop.hive.ql.metadata.Hive;
+import org.apache.hadoop.hive.ql.metadata.HiveException;
+import org.apache.hadoop.hive.ql.metadata.Partition;
+import org.apache.hadoop.hive.ql.session.SessionState;
+import org.junit.Assert;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.mockito.ArgumentCaptor;
+
+import java.util.Arrays;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+public class TestGetPartitionAuthWithBatches {
+
+    private final String catName = "hive";
+    private final String dbName = "default";
+    private final String tableName = "test_partition_batch_with_auth";
+    private static HiveConf hiveConf;
+    private static HiveMetaStoreClient msc;
+    private static Hive hive;
+    private Table table;
+
+    @BeforeClass
+    public static void setupClass() throws HiveException {
+        hiveConf = new HiveConf(TestGetPartitionAuthWithBatches.class);
+        hiveConf.set("hive.security.authorization.enabled", "true");
+        
hiveConf.set("hive.security.authorization.manager","org.apache.hadoop.hive.ql.security.authorization.DefaultHiveAuthorizationProvider");
+        hive = Hive.get();
+        SessionState.start(hiveConf);
+        try {
+            msc = new HiveMetaStoreClient(hiveConf);
+        } catch (MetaException e) {
+            throw new HiveException(e);
+        }
+    }
+
+    @Before
+    public void before() throws Exception {
+        PartitionUtil.createPartitionedTable(msc, catName, dbName, tableName);
+        table = msc.getTable(catName, dbName, tableName);
+        PartitionUtil.addPartitions(msc, dbName, tableName, 
table.getSd().getLocation(), hiveConf);
+    }
+
+    @After
+    public void after() throws Exception {
+        PartitionUtil.cleanUpTableQuietly(msc, catName, dbName, tableName);
+    }
+
+    @Test
+    public void testNumberOfPartitionsRetrieved() throws HiveException {
+        List<String> numParts = hive.getPartitionNames(dbName, tableName, 
(short)-1);
+        Assert.assertEquals(numParts.size(), 30);
+        List<Partition> partitions = hive.getPartitionsAuthByNames(new 
org.apache.hadoop.hive.ql.metadata.Table(table),
+                numParts.subList(0,5), "username", new 
ArrayList<>(Arrays.asList("Grp1", "Grp2")));
+        Assert.assertEquals(partitions.size(), 5);
+    }
+
+    /**
+     * Tests the number of partitions recieved from the HMS
+     *
+     * @throws Exception
+     */
+    @Test
+    public void testGetPartitionsAPI() throws Exception {
+        List<org.apache.hadoop.hive.ql.metadata.Partition> part = 
hive.getPartitions(hive.getTable(dbName, tableName));
+        Assert.assertEquals(part.size(), 30);
+    }
+
+    @Test
+    public void testGetPartitionsAPI2() throws Exception {
+        List<org.apache.hadoop.hive.ql.metadata.Partition> part = 
hive.getPartitions(hive.getTable(dbName, tableName), new HashMap() , (short) 
-1);
+        Assert.assertEquals(part.size(), 30);
+    }
+
+    @Test
+    public void testGetPartitionsAPI2limit() throws Exception {
+        List<org.apache.hadoop.hive.ql.metadata.Partition> part = 
hive.getPartitions(hive.getTable(dbName, tableName), new HashMap() , (short) 1);
+        Assert.assertEquals(part.size(), 1);
+    }
+
+    /**
+     * Tests the number of times Hive.getPartitions calls are executed with 
total number of
+     * partitions to be added are equally divisible by batch size
+     *
+     * @throws Exception
+     */
+    @Test
+    public void testNumberOfGetPartitionCalls() throws Exception {
+        HiveMetaStoreClient spyMSC = spy(msc);
+        hive.setMSC(spyMSC);
+        // test with a batch size of 10 and decaying factor of 2
+        hive.getAllPartitionsInBatches(hive.getTable(dbName, tableName),10, 2, 
0, null, true, "username", new ArrayList<>(Arrays.asList("Grp1", "Grp2")));
+        ArgumentCaptor<GetPartitionsPsWithAuthRequest> req = 
ArgumentCaptor.forClass(GetPartitionsPsWithAuthRequest.class);
+        // there should be 3 calls to get partitions
+        verify(spyMSC, 
times(3)).listPartitionsWithAuthInfoRequest(req.capture());
+        req.getAllValues().forEach(part-> 
Assert.assertEquals(part.getPartNames().size(),10));
+    }
+
+    @Test
+    public void testNumberOfGetPartitionCalls2() throws Exception {
+        HiveMetaStoreClient spyMSC = spy(msc);
+        hive.setMSC(spyMSC);
+        // test with a batch size of 10 and decaying factor of 2
+        hive.getAllPartitionsInBatches(hive.getTable(dbName, tableName),10, 2, 
0, new HashMap(), true, "username", new ArrayList<>(Arrays.asList("Grp1", 
"Grp2")));

Review Comment:
   Do you need to do explicit type to ArrayList?



##########
ql/src/java/org/apache/hadoop/hive/ql/metadata/Hive.java:
##########
@@ -4385,6 +4419,33 @@ public List<Partition> getPartitionsByNames(Table tbl, 
List<String> partNames, b
     return partitions;
   }
 
+  public List<Partition> getPartitionsAuthByNames(Table tbl, List<String> 
partNames, String userName,
+      List<String> groupNames) throws HiveException {
+    if (!tbl.isPartitioned()) {
+      throw new HiveException(ErrorMsg.TABLE_NOT_PARTITIONED, 
tbl.getTableName());
+    }
+    GetPartitionsPsWithAuthRequest req = new GetPartitionsPsWithAuthRequest();
+    req.setTblName(tbl.getTableName());
+    req.setDbName(tbl.getDbName());
+    req.setUserName(userName);
+    req.setGroupNames(groupNames);
+    req.setPartNames(partNames);
+
+    List<org.apache.hadoop.hive.metastore.api.Partition> tParts;
+    try {
+      GetPartitionsPsWithAuthResponse res = 
getMSC().listPartitionsWithAuthInfoRequest(req);
+      tParts = res.getPartitions();
+    } catch (Exception e) {
+      LOG.error("Failed getPartitions", e);

Review Comment:
   Log looks redundant as I will be logged upstream anyway. 



##########
ql/src/test/org/apache/hadoop/hive/ql/exec/TestGetPartitionAuthWithBatches.java:
##########
@@ -0,0 +1,274 @@
+/*
+ * 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.hadoop.hive.ql.exec;
+
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.metastore.HiveMetaStoreClient;
+
+import org.apache.hadoop.hive.metastore.api.GetPartitionsPsWithAuthRequest;
+import org.apache.hadoop.hive.metastore.api.MetaException;
+import org.apache.hadoop.hive.metastore.api.Table;
+import org.apache.hadoop.hive.ql.metadata.Hive;
+import org.apache.hadoop.hive.ql.metadata.HiveException;
+import org.apache.hadoop.hive.ql.metadata.Partition;
+import org.apache.hadoop.hive.ql.session.SessionState;
+import org.junit.Assert;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.mockito.ArgumentCaptor;
+
+import java.util.Arrays;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+public class TestGetPartitionAuthWithBatches {
+
+    private final String catName = "hive";
+    private final String dbName = "default";
+    private final String tableName = "test_partition_batch_with_auth";
+    private static HiveConf hiveConf;
+    private static HiveMetaStoreClient msc;
+    private static Hive hive;
+    private Table table;
+
+    @BeforeClass
+    public static void setupClass() throws HiveException {
+        hiveConf = new HiveConf(TestGetPartitionAuthWithBatches.class);
+        hiveConf.set("hive.security.authorization.enabled", "true");
+        
hiveConf.set("hive.security.authorization.manager","org.apache.hadoop.hive.ql.security.authorization.DefaultHiveAuthorizationProvider");
+        hive = Hive.get();
+        SessionState.start(hiveConf);
+        try {
+            msc = new HiveMetaStoreClient(hiveConf);
+        } catch (MetaException e) {
+            throw new HiveException(e);
+        }
+    }
+
+    @Before
+    public void before() throws Exception {
+        PartitionUtil.createPartitionedTable(msc, catName, dbName, tableName);
+        table = msc.getTable(catName, dbName, tableName);
+        PartitionUtil.addPartitions(msc, dbName, tableName, 
table.getSd().getLocation(), hiveConf);
+    }
+
+    @After
+    public void after() throws Exception {
+        PartitionUtil.cleanUpTableQuietly(msc, catName, dbName, tableName);
+    }
+
+    @Test
+    public void testNumberOfPartitionsRetrieved() throws HiveException {
+        List<String> numParts = hive.getPartitionNames(dbName, tableName, 
(short)-1);
+        Assert.assertEquals(numParts.size(), 30);
+        List<Partition> partitions = hive.getPartitionsAuthByNames(new 
org.apache.hadoop.hive.ql.metadata.Table(table),
+                numParts.subList(0,5), "username", new 
ArrayList<>(Arrays.asList("Grp1", "Grp2")));
+        Assert.assertEquals(partitions.size(), 5);
+    }
+
+    /**
+     * Tests the number of partitions recieved from the HMS
+     *
+     * @throws Exception
+     */
+    @Test
+    public void testGetPartitionsAPI() throws Exception {
+        List<org.apache.hadoop.hive.ql.metadata.Partition> part = 
hive.getPartitions(hive.getTable(dbName, tableName));
+        Assert.assertEquals(part.size(), 30);

Review Comment:
   You may want to define const for partition count



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to