diff --git ql/src/java/org/apache/hadoop/hive/ql/metadata/Hive.java ql/src/java/org/apache/hadoop/hive/ql/metadata/Hive.java
index cceea019c2..4ddf20265a 100644
--- ql/src/java/org/apache/hadoop/hive/ql/metadata/Hive.java
+++ ql/src/java/org/apache/hadoop/hive/ql/metadata/Hive.java
@@ -1402,12 +1402,56 @@ public Table getTable(final String dbName, final String tableName,
/**
* Get all tables for the specified database.
* @param dbName
- * @return List of table names
+ * @return List of all tables
* @throws HiveException
*/
public List
getAllTableObjects(String dbName) throws HiveException {
+ return getTableObjects(dbName, ".*", null);
+ }
+
+ /**
+ * Get all virtual view names for the specified database.
+ * @param dbName
+ * @return List of virtual view table names
+ * @throws HiveException
+ */
+ public List getAllVirtualViews(String dbName) throws HiveException {
+ return getTablesByType(dbName, ".*", TableType.VIRTUAL_VIEW);
+ }
+
+ /**
+ * Get all virtual views for the specified database.
+ * @param dbName
+ * @return List of virtual view table objects
+ * @throws HiveException
+ */
+ public List
getAllVirtualViewObjects(String dbName) throws HiveException {
+ return getTableObjects(dbName, ".*", TableType.VIRTUAL_VIEW);
+ }
+
+ /**
+ * Get all materialized view names for the specified database.
+ * @param dbName
+ * @return List of materialized view table names
+ * @throws HiveException
+ */
+ public List getAllMaterializedViews(String dbName) throws HiveException {
+ return getTablesByType(dbName, ".*", TableType.MATERIALIZED_VIEW);
+ }
+
+ /**
+ * Get all materialized views for the specified database.
+ * @param dbName
+ * @return List of materialized view table objects
+ * @throws HiveException
+ */
+ public List
getTableObjects(String dbName, String pattern, TableType tableType) throws HiveException {
try {
- return Lists.transform(getMSC().getTableObjectsByName(dbName, getMSC().getAllTables(dbName)),
+ return Lists.transform(getMSC().getTableObjectsByName(dbName, getTablesByType(dbName, pattern, tableType)),
new com.google.common.base.Function() {
@Override
public Table apply(org.apache.hadoop.hive.metastore.api.Table table) {
@@ -1510,7 +1554,7 @@ public Table apply(org.apache.hadoop.hive.metastore.api.Table table) {
List result = new ArrayList<>();
for (String dbName : getMSC().getAllDatabases()) {
// From metastore (for security)
- List tables = getMSC().getAllTables(dbName);
+ List tables = getAllMaterializedViews(dbName);
// Cached views (includes all)
Collection cachedViews =
HiveMaterializedViewsRegistry.get().getRewritingMaterializedViews(dbName);
diff --git ql/src/java/org/apache/hadoop/hive/ql/metadata/HiveMaterializedViewsRegistry.java ql/src/java/org/apache/hadoop/hive/ql/metadata/HiveMaterializedViewsRegistry.java
index ba46ff76e7..51b6ef58fc 100644
--- ql/src/java/org/apache/hadoop/hive/ql/metadata/HiveMaterializedViewsRegistry.java
+++ ql/src/java/org/apache/hadoop/hive/ql/metadata/HiveMaterializedViewsRegistry.java
@@ -121,32 +121,28 @@ public static HiveMaterializedViewsRegistry get() {
* it does.
*/
public void init(final Hive db) {
- try {
- List
tables = new ArrayList
();
- for (String dbName : db.getAllDatabases()) {
- // TODO: We should enhance metastore API such that it returns only
- // materialized views instead of all tables
- tables.addAll(db.getAllTableObjects(dbName));
- }
- pool.submit(new Loader(tables));
- } catch (HiveException e) {
- LOG.error("Problem connecting to the metastore when initializing the view registry");
- }
+ pool.submit(new Loader(db));
}
private class Loader implements Runnable {
- private final List
tables;
+ private final Hive db;
- private Loader(List
tables) {
- this.tables = tables;
+ private Loader(Hive db) {
+ this.db = db;
}
@Override
public void run() {
- for (Table table : tables) {
- if (table.isMaterializedView()) {
- addMaterializedView(table);
+ try {
+ List
materializedViews = new ArrayList
();
+ for (String dbName : db.getAllDatabases()) {
+ materializedViews.addAll(db.getAllMaterializedViewObjects(dbName));
+ }
+ for (Table mv : materializedViews) {
+ addMaterializedView(mv);
}
+ } catch (HiveException e) {
+ LOG.error("Problem connecting to the metastore when initializing the view registry");
}
}
}
diff --git standalone-metastore/src/main/java/org/apache/hadoop/hive/metastore/MetaStoreDirectSql.java standalone-metastore/src/main/java/org/apache/hadoop/hive/metastore/MetaStoreDirectSql.java
index d9155c4c35..db60ff29a8 100644
--- standalone-metastore/src/main/java/org/apache/hadoop/hive/metastore/MetaStoreDirectSql.java
+++ standalone-metastore/src/main/java/org/apache/hadoop/hive/metastore/MetaStoreDirectSql.java
@@ -383,6 +383,39 @@ public Database getDatabase(String dbName) throws MetaException{
}
/**
+ * Get table names by using direct SQL queries.
+ *
+ * @param dbName Metastore database namme
+ * @param tableType Table type, or null if we want to get all tables
+ * @return list of table names
+ */
+ public List getTables(String db_name, TableType tableType) throws MetaException {
+ List ret = new ArrayList();
+ String queryText = "SELECT " + TBLS + ".\"TBL_NAME\","
+ + " FROM " + TBLS + " "
+ + " INNER JOIN " + DBS + " ON " + TBLS + ".\"DB_ID\" = " + DBS + ".\"DB_ID\" "
+ + " WHERE " + DBS + ".\"NAME\" = ? "
+ + (tableType == null ? "" : "AND " + TBLS + ".\"TBL_TYPE\" = ? ") ;
+
+ List pms = new ArrayList();
+ pms.add(db_name);
+ if (tableType != null) {
+ pms.add(tableType.toString());
+ }
+
+ Query> queryParams = pm.newQuery("javax.jdo.query.SQL", queryText);
+ List