Index: metastore/src/test/org/apache/hadoop/hive/metastore/TestHiveMetaStore.java =================================================================== --- metastore/src/test/org/apache/hadoop/hive/metastore/TestHiveMetaStore.java (revision 1130342) +++ metastore/src/test/org/apache/hadoop/hive/metastore/TestHiveMetaStore.java (working copy) @@ -716,6 +716,65 @@ (tbl2.getPartitionKeys() == null) || (tbl2.getPartitionKeys().size() == 0)); + //Test get_multi_table functionality, return ordering, and duplicate items in list + ArrayList tableNames = new ArrayList(); + tableNames.add(tblName2); + tableNames.add(tblName); + tableNames.add(tblName2); + List foundTables = client.client.get_multi_table(dbName, tableNames); + //foundTables.get(2) should be the same table as that specified by tblName2 + assertNotNull(foundTables.get(0)); + assertEquals(foundTables.get(0).getDbName(), dbName); + assertEquals(foundTables.get(0).getTableName(), tblName2); + assertEquals(foundTables.get(0).getSd().getCols().size(), typ1.getFields().size()); + assertEquals(foundTables.get(0).getSd().isCompressed(), false); + assertEquals(foundTables.get(0).getSd().getNumBuckets(), 1); + assertEquals(foundTables.get(0).getSd().getLocation(), tbl2.getSd().getLocation()); + assertNotNull(foundTables.get(0).getSd().getSerdeInfo()); + + //foundTables.get(1) should be the same table as that specified by tblName + assertNotNull(foundTables.get(1)); + assertEquals(foundTables.get(1).getDbName(), dbName); + assertEquals(foundTables.get(1).getTableName(), tblName); + assertEquals(foundTables.get(1).getSd().getCols().size(), typ1.getFields().size()); + assertEquals(foundTables.get(1).getSd().isCompressed(), false); + assertEquals(foundTables.get(1).getSd().getNumBuckets(), 1); + assertEquals(foundTables.get(1).getSd().getLocation(), tbl.getSd().getLocation()); + assertNotNull(foundTables.get(1).getSd().getSerdeInfo()); + + //foundTables.get(2) should be the same table as that specified by tblName2 + assertNotNull(foundTables.get(2)); + assertEquals(foundTables.get(2).getDbName(), dbName); + assertEquals(foundTables.get(2).getTableName(), tblName2); + assertEquals(foundTables.get(2).getSd().getCols().size(), typ1.getFields().size()); + assertEquals(foundTables.get(2).getSd().isCompressed(), false); + assertEquals(foundTables.get(2).getSd().getNumBuckets(), 1); + assertEquals(foundTables.get(2).getSd().getLocation(), tbl2.getSd().getLocation()); + assertNotNull(foundTables.get(2).getSd().getSerdeInfo()); + + //Negative test for get_multi_table + tableNames.add(1, "table_that_doesnt_exist"); + Exception nse = null; + try { + foundTables = client.client.get_multi_table(dbName, tableNames); + } catch (NoSuchObjectException e){ + nse = e; + } + assertNotNull(nse); + assertTrue("Table not found", nse.getMessage().contains("table_that_doesnt_exist")); + + tableNames = null; + nse = null; + try { + foundTables = client.client.get_multi_table(dbName, tableNames); + } catch (NoSuchObjectException e){ + nse = e; + } + assertNotNull(nse); + assertTrue("Table not found", nse.getMessage().contains("null no tables found")); + + + FileSystem fs = FileSystem.get((new Path(tbl.getSd().getLocation())).toUri(), hiveConf); client.dropTable(dbName, tblName); assertFalse(fs.exists(new Path(tbl.getSd().getLocation()))); Index: metastore/src/java/org/apache/hadoop/hive/metastore/RawStore.java =================================================================== --- metastore/src/java/org/apache/hadoop/hive/metastore/RawStore.java (revision 1130342) +++ metastore/src/java/org/apache/hadoop/hive/metastore/RawStore.java (working copy) @@ -114,6 +114,18 @@ public List getTables(String dbName, String pattern) throws MetaException; + /** + * @param dbname + * The name of the database from which to retrieve the tables + * @param tableNames + * The names of the tables to retrieve. + * @return A list of all tables that exist in the database whose names are in the list tableNames, + * returned in the same order specified by tableNames + * @throws MetaException + */ + public List
getMultiTables(String dbname, List tableNames) + throws MetaException; + public List getAllTables(String dbName) throws MetaException; public abstract List listPartitionNames(String db_name, Index: metastore/src/java/org/apache/hadoop/hive/metastore/HiveMetaStore.java =================================================================== --- metastore/src/java/org/apache/hadoop/hive/metastore/HiveMetaStore.java (revision 1130342) +++ metastore/src/java/org/apache/hadoop/hive/metastore/HiveMetaStore.java (working copy) @@ -516,6 +516,11 @@ return startFunction(function, " : db=" + db + " tbl=" + tbl); } + public String startMultiTableFunction(String function, String db, List tbls) { + String tableNames = join(tbls, ","); + return startFunction(function, " : db=" + db + " tbls=" + tableNames); + } + public String startPartitionFunction(String function, String db, String tbl, List partVals) { return startFunction(function, " : db=" + db + " tbl=" + tbl @@ -1134,6 +1139,60 @@ return t; } + /** + * Gets multiple tables from the hive metastore. + * @param dbname + * The name of the database in which the tables reside + * @param names + * The names of the tables to get + * + * @return A list of tables in the same order as the tables specified by the parameter "names" + * @throws MetaException + * @throws NoSuchObjectException + */ + public List
get_multi_table(final String dbname, final List names) + throws MetaException, NoSuchObjectException { + List
tables = null; + startMultiTableFunction("get_multi_table", dbname, names); + try { + tables = executeWithRetry(new Command>() { + @Override + public List
run(RawStore ms) throws Exception { + //If names is null, throw the same error that get_table would throw + if (names == null) + { + throw new NoSuchObjectException(dbname + ".null no tables found"); + } + List
foundTables = ms.getMultiTables(dbname, names); + //Throw an error if no tables were found + if (foundTables.isEmpty()) { + throw new NoSuchObjectException(dbname + " no tables found."); + } + //Throw an error if a requested table does not exist in the db + List foundTableNames = new ArrayList(foundTables.size()); + for (Table t : foundTables) { + foundTableNames.add(t.getTableName()); + } + for (String tableName : names) { + if (!foundTableNames.contains(tableName)) { + throw new NoSuchObjectException(dbname + "." + tableName + " table not found"); + } + } + return foundTables; + } + }); + } catch (NoSuchObjectException e) { + throw e; + } catch (MetaException e) { + throw e; + } catch (Exception e) { + throw new MetaException(e.getMessage()); + } finally { + endFunction("get_multi_table"); + } + return tables; + } + public boolean set_table_parameters(String dbname, String name, Map params) throws NoSuchObjectException, MetaException { endFunction(startTableFunction("set_table_parameters", dbname, name)); Index: metastore/src/java/org/apache/hadoop/hive/metastore/ObjectStore.java =================================================================== --- metastore/src/java/org/apache/hadoop/hive/metastore/ObjectStore.java (revision 1130342) +++ metastore/src/java/org/apache/hadoop/hive/metastore/ObjectStore.java (working copy) @@ -26,9 +26,9 @@ import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.Properties; import java.util.Set; -import java.util.Map.Entry; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; @@ -88,9 +88,9 @@ import org.apache.hadoop.hive.metastore.model.MTableColumnPrivilege; import org.apache.hadoop.hive.metastore.model.MTablePrivilege; import org.apache.hadoop.hive.metastore.model.MType; +import org.apache.hadoop.hive.metastore.parser.ExpressionTree.ANTLRNoCaseStringStream; import org.apache.hadoop.hive.metastore.parser.FilterLexer; import org.apache.hadoop.hive.metastore.parser.FilterParser; -import org.apache.hadoop.hive.metastore.parser.ExpressionTree.ANTLRNoCaseStringStream; import org.apache.hadoop.util.StringUtils; /** @@ -787,6 +787,46 @@ return mtbl; } + public List
getMultiTables(String db, List tbl_names) throws MetaException{ + List
tables = new ArrayList
(); + boolean committed = false; + try { + openTransaction(); + db = db.toLowerCase().trim(); + List lowered_tbl_names = new ArrayList(); + for (String t : tbl_names) { + lowered_tbl_names.add(t.toLowerCase().trim()); + } + Query query = pm.newQuery(MTable.class); + query.setFilter("database.name == db && tbl_names.contains(tableName)"); + query.declareParameters("java.lang.String db, java.util.Collection tbl_names"); + query.setOrdering("tableName ascending"); + Collection mtables = (Collection) query.execute(db, lowered_tbl_names); + List
foundTables = new ArrayList
(); + for (Iterator iter = mtables.iterator(); iter.hasNext();) { + foundTables.add(convertToTable((MTable) iter.next())); + } + //foundTableNames is sorted in ascending alphabetic order + List foundTableNames = new ArrayList(foundTables.size()); + for (Table t : foundTables) { + foundTableNames.add(t.getTableName()); + } + //Build a list of the tables found in the db in the order of the parameter "tbl_names". + for (String tableName : tbl_names) { + int nameIndex = Collections.binarySearch(foundTableNames, tableName); + if (nameIndex >= 0) { + tables.add(foundTables.get(nameIndex)); + } + } + committed = commitTransaction(); + } finally { + if (!committed) { + rollbackTransaction(); + } + } + return tables; + } + private Table convertToTable(MTable mtbl) throws MetaException { if (mtbl == null) { return null; Index: metastore/if/hive_metastore.thrift =================================================================== --- metastore/if/hive_metastore.thrift (revision 1130342) +++ metastore/if/hive_metastore.thrift (working copy) @@ -246,6 +246,8 @@ Table get_table(1:string dbname, 2:string tbl_name) throws (1:MetaException o1, 2:NoSuchObjectException o2) + list
get_multi_table(1:string dbname, 2:list tbl_names) throws (1:MetaException o1, 2:NoSuchObjectException o2) + // alter table applies to only future partitions not for existing partitions // * See notes on DDL_TIME void alter_table(1:string dbname, 2:string tbl_name, 3:Table new_tbl)