diff --git ql/src/java/org/apache/hadoop/hive/ql/metadata/SessionHiveMetaStoreClient.java ql/src/java/org/apache/hadoop/hive/ql/metadata/SessionHiveMetaStoreClient.java index 410868cacf..5c41613e83 100644 --- ql/src/java/org/apache/hadoop/hive/ql/metadata/SessionHiveMetaStoreClient.java +++ ql/src/java/org/apache/hadoop/hive/ql/metadata/SessionHiveMetaStoreClient.java @@ -19,18 +19,18 @@ package org.apache.hadoop.hive.ql.metadata; import java.io.IOException; +import java.util.List; import java.util.ArrayList; -import java.util.Collections; +import java.util.Arrays; +import java.util.Map; +import java.util.Set; import java.util.HashMap; import java.util.HashSet; +import java.util.Collections; import java.util.Iterator; -import java.util.List; -import java.util.Map; import java.util.Map.Entry; -import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; - import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileStatus; import org.apache.hadoop.fs.FileSystem; @@ -74,6 +74,8 @@ import org.slf4j.LoggerFactory; import static org.apache.hadoop.hive.metastore.Warehouse.DEFAULT_CATALOG_NAME; +import static org.apache.hadoop.hive.metastore.Warehouse.makePartName; +import static org.apache.hadoop.hive.metastore.Warehouse.getCatalogQualifiedTableName; /** * todo: This need review re: thread safety. Various places (see callsers of @@ -573,7 +575,14 @@ private void createTempTable(org.apache.hadoop.hive.metastore.api.Table tbl, createTempTable(tbl); } - private org.apache.hadoop.hive.metastore.api.Table getTempTable(String dbName, String tableName) { + private org.apache.hadoop.hive.metastore.api.Table getTempTable(String dbName, String tableName) + throws MetaException { + if (dbName == null) { + throw new MetaException("Db name cannot be null"); + } + if (tableName == null) { + throw new MetaException("Table name cannot be null"); + } Map tables = getTempTablesForDatabase(dbName.toLowerCase(), tableName.toLowerCase()); if (tables != null) { @@ -914,30 +923,79 @@ private boolean deleteTempTableColumnStats(String dbName, String tableName, Stri pTree = t.getPartitionKeysSize() > 0 ? new PartitionTree(tTable) : null; } private void addPartition(Partition p) throws AlreadyExistsException, MetaException { - assertPartitioned(); pTree.addPartition(p); } private Partition getPartition(String partName) throws MetaException { - assertPartitioned(); + if (partName == null || partName.isEmpty()) { + throw new MetaException("Partition name cannot be null or empty"); + } return pTree.getPartition(partName); } + private Partition getPartition(List partVals) throws MetaException { + if (partVals == null) { + throw new MetaException("Partition values cannot be null"); + } + return pTree.getPartition(partVals); + } + private int addPartitions(List partitions) throws AlreadyExistsException, MetaException { - assertPartitioned(); return pTree.addPartitions(partitions); } + private List getPartitionsByNames(List partNames) throws MetaException { + if (partNames == null) { + throw new MetaException("Partition names cannot be null"); + } + List partitions = new ArrayList<>(); + for (String partName : partNames) { + Partition partition = getPartition(partName); + if (partition != null) { + partitions.add(partition); + } + } + return partitions; + } + private List getPartitions(List partialPartVals) throws MetaException { - assertPartitioned(); return pTree.getPartitions(partialPartVals); } - private void assertPartitioned() throws MetaException { - if(tTable.getPartitionKeysSize() <= 0) { - throw new MetaException(Warehouse.getQualifiedName(tTable) + " is not partitioned"); + private Partition getPartitionWithAuthInfo(List partionVals, String userName, List groupNames) + throws MetaException { + Partition partition = getPartition(partionVals); + if (partition == null) { + return null; } + return checkPrivilegesForPartition(partition, userName, groupNames) ? partition : null; + } + + private boolean checkPrivilegesForPartition(Partition partition, String userName, List groupNames) { + if ((userName == null || userName.isEmpty()) && (groupNames == null || groupNames.isEmpty())) { + return true; + } + PrincipalPrivilegeSet privileges = partition.getPrivileges(); + if (privileges == null) { + return false; + } + if (privileges.isSetUserPrivileges()) { + if (!privileges.getUserPrivileges().containsKey(userName)) { + return false; + } + } + if (privileges.isSetGroupPrivileges()) { + if (groupNames == null) { + return false; + } + for (String group : groupNames) { + if (!privileges.getGroupPrivileges().containsKey(group)) { + return false; + } + } + } + return true; } /** @@ -965,6 +1023,19 @@ private Partition getPartition(String partName) { return parts.get(partName); } + /** + * Get a partition matching the partition values. + * + * @param partVals partition values for this partition, must be in the same order as the + * partition keys of the table. + * @return the partition object, or if not found null. + * @throws MetaException + */ + private Partition getPartition(List partVals) throws MetaException { + String partName = makePartName(tTable.getPartitionKeys(), partVals); + return getPartition(partName); + } + private int addPartitions(List partitions) throws AlreadyExistsException, MetaException { int partitionsAdded = 0; @@ -1011,7 +1082,7 @@ private int addPartitions(List partitions) //(assume) not a temp table - Try underlying client return super.add_partition(partition); } - TempTable tt = getTempTable(table); + TempTable tt = getPartitionedTempTable(table); if(tt == null) { throw new IllegalStateException("TempTable not found for " + Warehouse.getQualifiedName(table)); @@ -1039,7 +1110,7 @@ public int add_partitions(List partitions) throws TException { // not a temp table - Try underlying client return super.add_partitions(partitions); } - TempTable tt = getTempTable(table); + TempTable tt = getPartitionedTempTable(table); if (tt == null) { throw new IllegalStateException("TempTable not found for" + table.getTableName()); @@ -1047,6 +1118,50 @@ public int add_partitions(List partitions) throws TException { return tt.addPartitions(deepCopyPartitions(partitions)); } + + @Override + public Partition getPartition(String catName, String dbName, String tblName, String name) throws TException { + org.apache.hadoop.hive.metastore.api.Table table = getTempTable(dbName, tblName); + if (table == null) { + return super.getPartition(catName, dbName, tblName, name); + } + assertPartitioned(table); + TempTable tt = getPartitionedTempTable(table); + if(tt == null) { + throw new IllegalStateException("TempTable not found for " + + getCatalogQualifiedTableName(table)); + } + Partition partition = tt.getPartition(name); + if (partition == null) { + throw new NoSuchObjectException("Partition with name " + name + " for table " + tblName + " in database " + + dbName + " is not found."); + } + + return deepCopy(partition); + } + + @Override + public Partition getPartition(String catName, String dbName, String tblName, + List partVals) throws TException { + org.apache.hadoop.hive.metastore.api.Table table = getTempTable(dbName, tblName); + if (table == null) { + return super.getPartition(catName, dbName, tblName, partVals); + } + assertPartitioned(table); + TempTable tt = getPartitionedTempTable(table); + if(tt == null) { + throw new IllegalStateException("TempTable not found for " + + getCatalogQualifiedTableName(table)); + } + Partition partition = tt.getPartition(partVals); + if (partition == null) { + throw new NoSuchObjectException("Partition with partition values " + + (partVals != null ? Arrays.toString(partVals.toArray()) : "null")+ + " for table " + tblName + " in database " + dbName + " is not found."); + } + return deepCopy(partition); + } + /** * @param partialPvals partition values, can be partial. This really means that missing values * are represented by empty str. @@ -1062,7 +1177,7 @@ public int add_partitions(List partitions) throws TException { return super.listPartitionsWithAuthInfo(dbName, tableName, partialPvals, maxParts, userName, groupNames); } - TempTable tt = getTempTable(table); + TempTable tt = getPartitionedTempTable(table); if(tt == null) { throw new IllegalStateException("TempTable not found for " + Warehouse.getQualifiedName(table)); @@ -1087,7 +1202,7 @@ public int add_partitions(List partitions) throws TException { //(assume) not a temp table - Try underlying client return super.listPartitionNames(dbName, tableName, maxParts); } - TempTable tt = getTempTable(table); + TempTable tt = getPartitionedTempTable(table); if(tt == null) { throw new IllegalStateException("TempTable not found for " + Warehouse.getQualifiedName(table)); @@ -1103,42 +1218,53 @@ public int add_partitions(List partitions) throws TException { return matchedParts; } - /** - * partNames are like "p=1/q=2" type strings. The values (RHS of =) are escaped. - */ + @Override - public List getPartitionsByNames(String db_name, String tblName, - List partNames) throws TException { - return getPartitionsByNames(db_name, tblName, partNames, false); + public List getPartitionsByNames(String catName, String dbName, String tblName, + List partNames, boolean getColStats) throws TException { + org.apache.hadoop.hive.metastore.api.Table table = getTempTable(dbName, tblName); + if (table == null) { + //(assume) not a temp table - Try underlying client + return super.getPartitionsByNames(catName, dbName, tblName, partNames, getColStats); + } + assertPartitioned(table); + TempTable tt = getPartitionedTempTable(table); + if (tt == null) { + throw new IllegalStateException("TempTable not found for " + + getCatalogQualifiedTableName(table)); + } + List partitions = tt.getPartitionsByNames(partNames); + + return deepCopyPartitions(partitions); } - /** - * partNames are like "p=1/q=2" type strings. The values (RHS of =) are escaped. - */ @Override - public List getPartitionsByNames(String db_name, String tblName, - List partNames, boolean getColStats) - throws TException { - org.apache.hadoop.hive.metastore.api.Table table = getTempTable(db_name, tblName); + public Partition getPartitionWithAuthInfo(String catName, String dbName, String tableName, + List pvals, String userName, + List groupNames) throws TException { + org.apache.hadoop.hive.metastore.api.Table table = getTempTable(dbName, tableName); if (table == null) { - //(assume) not a temp table - Try underlying client - return super.getPartitionsByNames(db_name, tblName, partNames, getColStats); + return super.getPartitionWithAuthInfo(catName, dbName, tableName, pvals, userName, groupNames); } - TempTable tt = getTempTable(table); - if(tt == null) { - throw new IllegalStateException("TempTable not found for " + tblName); + assertPartitioned(table); + TempTable tt = getPartitionedTempTable(table); + if (tt == null) { + throw new IllegalStateException("TempTable not found for " + + getCatalogQualifiedTableName(table)); } - List matchedParts = new ArrayList<>(); - for(String partName : partNames) { - Partition p = tt.getPartition(partName); - if(p != null) { - matchedParts.add(deepCopy(p)); - } + + Partition partition = tt.getPartitionWithAuthInfo(pvals, userName, groupNames); + if (partition == null) { + throw new NoSuchObjectException("Partition with partition values " + + (pvals != null ? Arrays.toString(pvals.toArray()) : "null") + + " for table " + tableName + " in database " + dbName + "and for user " + + userName + " and group names " + (groupNames != null ? Arrays.toString(groupNames.toArray()) : "null") + + " is not found."); } - return matchedParts; + return deepCopy(partition); } - private static TempTable getTempTable(org.apache.hadoop.hive.metastore.api.Table t) { + private static TempTable getPartitionedTempTable(org.apache.hadoop.hive.metastore.api.Table t) { String qualifiedTableName = Warehouse. getQualifiedName(t.getDbName().toLowerCase(), t.getTableName().toLowerCase()); SessionState ss = SessionState.get(); @@ -1175,4 +1301,10 @@ private static void createTempTable(org.apache.hadoop.hive.metastore.api.Table t throw new IllegalStateException("TempTable for " + qualifiedTableName + " already exists"); } } + + private void assertPartitioned(org.apache.hadoop.hive.metastore.api.Table table) throws MetaException { + if(table.getPartitionKeysSize() <= 0) { + throw new MetaException(getCatalogQualifiedTableName(table) + " is not partitioned"); + } + } } diff --git ql/src/test/org/apache/hadoop/hive/ql/metadata/TestSessionHiveMetastoreClientGetPartitionsTempTable.java ql/src/test/org/apache/hadoop/hive/ql/metadata/TestSessionHiveMetastoreClientGetPartitionsTempTable.java new file mode 100644 index 0000000000..387d138f62 --- /dev/null +++ ql/src/test/org/apache/hadoop/hive/ql/metadata/TestSessionHiveMetastoreClientGetPartitionsTempTable.java @@ -0,0 +1,162 @@ +/* + * 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.metadata; + +import com.google.common.collect.Lists; +import org.apache.hadoop.hive.conf.HiveConf; +import org.apache.hadoop.hive.metastore.IMetaStoreClient; +import org.apache.hadoop.hive.metastore.api.Partition; +import org.apache.hadoop.hive.metastore.api.Table; +import org.apache.hadoop.hive.metastore.api.*; +import org.apache.hadoop.hive.metastore.client.CustomIgnoreRule; +import org.apache.hadoop.hive.metastore.client.TestGetPartitions; +import org.apache.hadoop.hive.metastore.client.builder.PartitionBuilder; +import org.apache.hadoop.hive.metastore.client.builder.TableBuilder; +import org.apache.hadoop.hive.metastore.minihms.AbstractMetaStoreService; +import org.apache.hadoop.hive.ql.session.SessionState; +import org.apache.thrift.TException; +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static junit.framework.TestCase.assertTrue; +import static org.junit.Assert.assertNotNull; + +/** + * Test class for get partitions related methods on temporary tables. + */ +public class TestSessionHiveMetastoreClientGetPartitionsTempTable extends TestGetPartitions { + + private HiveConf conf; + + private static final String USER_NAME = "user0"; + private static final List GROUPS = Lists.newArrayList("group0", "group1"); + private static final String PART_PRIV = "PARTITION_LEVEL_PRIVILEGE"; + + public TestSessionHiveMetastoreClientGetPartitionsTempTable(String name, AbstractMetaStoreService metaStore) { + super(name, metaStore); + ignoreRule = new CustomIgnoreRule(); + } + + @Before public void setUp() throws Exception { + initHiveConf(); + SessionState.start(conf); + super.setClient(Hive.get(conf).getMSC()); + getClient().dropDatabase(DB_NAME, true, true, true); + } + + private void initHiveConf() throws HiveException { + conf = Hive.get().getConf(); + conf.setBoolVar(HiveConf.ConfVars.METASTORE_FASTPATH, true); + } + + @Override protected Table createTestTable(IMetaStoreClient client, String dbName, String tableName, + List partCols, boolean setPartitionLevelPrivileges) throws TException { + TableBuilder builder = + new TableBuilder().setDbName(dbName).setTableName(tableName).addCol("id", "int").addCol("name", "string") + .setTemporary(true); + + partCols.forEach(col -> builder.addPartCol(col, "string")); + Table table = builder.build(conf); + + if (setPartitionLevelPrivileges) { + table.putToParameters(PART_PRIV, "true"); + } + + client.createTable(table); + return table; + } + + @Override protected void addPartition(IMetaStoreClient client, Table table, List values) throws TException { + PartitionBuilder builder = new PartitionBuilder().inTable(table); + values.forEach(builder::addValue); + Partition partition = builder.build(conf); + if (table.getParameters().containsKey(PART_PRIV) && table.getParameters().get(PART_PRIV).equals("true")) { + PrincipalPrivilegeSet privileges = new PrincipalPrivilegeSet(); + Map> userPrivileges = new HashMap<>(); + userPrivileges.put(USER_NAME, new ArrayList<>()); + privileges.setUserPrivileges(userPrivileges); + + Map> groupPrivileges = new HashMap<>(); + GROUPS.forEach(g -> groupPrivileges.put(g, new ArrayList<>())); + privileges.setGroupPrivileges(groupPrivileges); + partition.setPrivileges(privileges); + } + client.add_partition(partition); + } + + @Test(expected = MetaException.class) @Override public void testGetPartitionsByNamesEmptyParts() throws Exception { + createTable4PartColsParts(getClient()); + getClient().getPartitionsByNames(DB_NAME, TABLE_NAME, Lists.newArrayList("", "")); + } + + @Test(expected = MetaException.class) @Override public void testGetPartitionsByNamesNullDbName() throws Exception { + super.testGetPartitionsByNamesNullDbName(); + } + + @Test(expected = MetaException.class) @Override public void testGetPartitionsByNamesNullTblName() throws Exception { + super.testGetPartitionsByNamesNullTblName(); + } + + @Test @Override public void testGetPartitionWithAuthInfo() throws Exception { + createTable3PartCols1PartAuthOn(getClient()); + Partition partition = getClient() + .getPartitionWithAuthInfo(DB_NAME, TABLE_NAME, Lists.newArrayList("1997", "05", "16"), USER_NAME, GROUPS); + assertNotNull(partition); + assertAuthInfoReturned(USER_NAME, GROUPS, partition); + } + + @Test @Override public void testGetPartitionWithAuthInfoEmptyUserGroup() throws Exception { + createTable3PartCols1PartAuthOn(getClient()); + Partition partition = getClient() + .getPartitionWithAuthInfo(DB_NAME, TABLE_NAME, Lists.newArrayList("1997", "05", "16"), "", + Lists.newArrayList()); + assertNotNull(partition); + assertAuthInfoReturned(USER_NAME, GROUPS, partition); + } + + @Test(expected = MetaException.class) @Override public void testGetPartitionWithAuthInfoNullDbName() + throws Exception { + super.testGetPartitionWithAuthInfoNullDbName(); + } + + @Test(expected = MetaException.class) @Override public void testGetPartitionWithAuthInfoNullTblName() + throws Exception { + super.testGetPartitionWithAuthInfoNullTblName(); + } + + @Test(expected = NoSuchObjectException.class) @Override public void testGetPartitionWithAuthInfoNullGroups() + throws Exception { + super.testGetPartitionWithAuthInfoNullGroups(); + } + + private void assertAuthInfoReturned(String userName, List groups, Partition partition) { + PrincipalPrivilegeSet privileges = partition.getPrivileges(); + assertNotNull(privileges); + assertTrue(privileges.getUserPrivileges().containsKey(userName)); + for (String group : groups) { + assertTrue(privileges.getGroupPrivileges().containsKey(group)); + } + } + +} diff --git standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/client/ConditionalIgnoreOnSessionHiveMetastoreClient.java standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/client/ConditionalIgnoreOnSessionHiveMetastoreClient.java new file mode 100644 index 0000000000..99039b08d0 --- /dev/null +++ standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/client/ConditionalIgnoreOnSessionHiveMetastoreClient.java @@ -0,0 +1,36 @@ +/* + * 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.metastore.client; + +import java.lang.annotation.Inherited; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * This annotation provides a way to skip the execution of seome tests, + * when a certain runtime criteria is met. It should be used together with {@link CustomIgnoreRule}. + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +@Inherited +public @interface ConditionalIgnoreOnSessionHiveMetastoreClient { + +} diff --git standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/client/CustomIgnoreRule.java standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/client/CustomIgnoreRule.java new file mode 100644 index 0000000000..c81db9412e --- /dev/null +++ standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/client/CustomIgnoreRule.java @@ -0,0 +1,45 @@ +/* + * 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.metastore.client; + +import org.junit.Assume; +import org.junit.rules.TestRule; +import org.junit.runner.Description; +import org.junit.runners.model.Statement; + +/** + * This rule checks the existance of {@link ConditionalIgnoreOnSessionHiveMetastoreClient} annotation. + * If the annotation is present, skips the execution of the test. + */ +public class CustomIgnoreRule implements TestRule { + @Override + public Statement apply(Statement statement, Description description) { + return new Statement() { + @Override + public void evaluate() throws Throwable { + ConditionalIgnoreOnSessionHiveMetastoreClient annotation = description.getAnnotation(ConditionalIgnoreOnSessionHiveMetastoreClient.class); + if (annotation != null) { + Assume.assumeTrue("Test is ignored", false); + } else { + statement.evaluate(); + } + } + }; + } +} diff --git standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/client/MetaStoreClientTest.java standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/client/MetaStoreClientTest.java index dc48fa8308..21603e29cb 100644 --- standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/client/MetaStoreClientTest.java +++ standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/client/MetaStoreClientTest.java @@ -22,7 +22,9 @@ import org.apache.hadoop.hive.metastore.minihms.AbstractMetaStoreService; import org.junit.AfterClass; import org.junit.BeforeClass; +import org.junit.Rule; import org.junit.experimental.categories.Category; +import org.junit.rules.TestRule; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.slf4j.Logger; @@ -47,6 +49,9 @@ // Then we should remove our own copy private static Set metaStoreServices = null; + @Rule + public TestRule ignoreRule; + @Parameterized.Parameters(name = "{0}") public static List getMetaStoreToTest() throws Exception { List result = MetaStoreFactoryForTests.getMetaStores(); diff --git standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/client/TestGetPartitions.java standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/client/TestGetPartitions.java index 4d7f7c1220..8ac0813f77 100644 --- standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/client/TestGetPartitions.java +++ standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/client/TestGetPartitions.java @@ -51,11 +51,11 @@ import org.junit.runner.RunWith; import org.junit.runners.Parameterized; -import static junit.framework.TestCase.assertNotNull; -import static junit.framework.TestCase.assertNull; -import static org.apache.hadoop.hive.metastore.Warehouse.DEFAULT_DATABASE_NAME; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import static org.junit.Assert.assertNull; /** * API tests for HMS client's getPartitions methods. @@ -64,10 +64,11 @@ @Category(MetastoreCheckinTest.class) public class TestGetPartitions extends MetaStoreClientTest { private AbstractMetaStoreService metaStore; + private IMetaStoreClient client; - private static final String DB_NAME = "testpartdb"; - private static final String TABLE_NAME = "testparttable"; + protected static final String DB_NAME = "testpartdb"; + protected static final String TABLE_NAME = "testparttable"; public TestGetPartitions(String name, AbstractMetaStoreService metaStore) { this.metaStore = metaStore; @@ -98,6 +99,14 @@ public void tearDown() throws Exception { } } + public IMetaStoreClient getClient() { + return client; + } + + public void setClient(IMetaStoreClient client) { + this.client = client; + } + private void createDB(String dbName) throws TException { new DatabaseBuilder(). setName(dbName). @@ -105,8 +114,8 @@ private void createDB(String dbName) throws TException { } - private Table createTestTable(IMetaStoreClient client, String dbName, String tableName, - List partCols, boolean setPartitionLevelPrivilages) + protected Table createTestTable(IMetaStoreClient client, String dbName, String tableName, + List partCols, boolean setPartitionLevelPrivilages) throws TException { TableBuilder builder = new TableBuilder() .setDbName(dbName) @@ -125,7 +134,7 @@ private Table createTestTable(IMetaStoreClient client, String dbName, String tab return table; } - private void addPartition(IMetaStoreClient client, Table table, List values) + protected void addPartition(IMetaStoreClient client, Table table, List values) throws TException { PartitionBuilder partitionBuilder = new PartitionBuilder().inTable(table); values.forEach(val -> partitionBuilder.addValue(val)); @@ -139,15 +148,15 @@ private void createTable3PartCols1PartGeneric(IMetaStoreClient client, boolean a addPartition(client, t, Lists.newArrayList("1997", "05", "16")); } - private void createTable3PartCols1Part(IMetaStoreClient client) throws TException { + protected void createTable3PartCols1Part(IMetaStoreClient client) throws TException { createTable3PartCols1PartGeneric(client, false); } - private void createTable3PartCols1PartAuthOn(IMetaStoreClient client) throws TException { + protected void createTable3PartCols1PartAuthOn(IMetaStoreClient client) throws TException { createTable3PartCols1PartGeneric(client, true); } - private List> createTable4PartColsParts(IMetaStoreClient client) throws + protected List> createTable4PartColsParts(IMetaStoreClient client) throws Exception { Table t = createTestTable(client, DB_NAME, TABLE_NAME, Lists.newArrayList("yyyy", "mm", "dd"), false); @@ -345,9 +354,7 @@ public void testGetPartitionsByNames() throws Exception { partitions = client.getPartitionsByNames(DB_NAME, TABLE_NAME, Lists.newArrayList("yyyy=2017/mm=11/dd=27", "yyyy=1999/mm=01/dd=02")); assertEquals(2, partitions.size()); - assertEquals(testValues.get(0), partitions.get(0).getValues()); - assertEquals(testValues.get(3), partitions.get(1).getValues()); - + partitions.forEach(p -> assertTrue(testValues.contains(p.getValues()))); partitions = client.getPartitionsByNames(DB_NAME, TABLE_NAME, Lists.newArrayList("yyyy=2017", "yyyy=1999/mm=01/dd=02")); @@ -525,6 +532,7 @@ public void testGetPartitionWithAuthInfoNullGroups() throws Exception { } @Test + @ConditionalIgnoreOnSessionHiveMetastoreClient public void otherCatalog() throws TException { String catName = "get_partition_catalog"; Catalog cat = new CatalogBuilder() @@ -578,18 +586,21 @@ public void otherCatalog() throws TException { } @Test(expected = NoSuchObjectException.class) + @ConditionalIgnoreOnSessionHiveMetastoreClient public void getPartitionBogusCatalog() throws TException { createTable3PartCols1Part(client); client.getPartition("bogus", DB_NAME, TABLE_NAME, Lists.newArrayList("1997", "05", "16")); } @Test(expected = NoSuchObjectException.class) + @ConditionalIgnoreOnSessionHiveMetastoreClient public void getPartitionByNameBogusCatalog() throws TException { createTable3PartCols1Part(client); client.getPartition("bogus", DB_NAME, TABLE_NAME, "yyyy=1997/mm=05/dd=16"); } @Test(expected = NoSuchObjectException.class) + @ConditionalIgnoreOnSessionHiveMetastoreClient public void getPartitionWithAuthBogusCatalog() throws TException { createTable3PartCols1PartAuthOn(client); client.getPartitionWithAuthInfo("bogus", DB_NAME, TABLE_NAME, @@ -597,6 +608,7 @@ public void getPartitionWithAuthBogusCatalog() throws TException { } @Test(expected = NoSuchObjectException.class) + @ConditionalIgnoreOnSessionHiveMetastoreClient public void getPartitionsByNamesBogusCatalog() throws TException { createTable3PartCols1Part(client); client.getPartitionsByNames("bogus", DB_NAME, TABLE_NAME,