diff --git a/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java b/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java index 45aac5fbec..bd2e300a16 100644 --- a/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java +++ b/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java @@ -36,9 +36,7 @@ import org.apache.hive.service.cli.operation.ClassicTableTypeMapping.ClassicTableTypes; import org.apache.hive.service.cli.operation.HiveTableTypeMapping; import org.apache.hive.service.cli.operation.TableTypeMappingFactory.TableTypeMappings; -import org.junit.After; import org.junit.AfterClass; -import org.junit.Before; import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; @@ -93,6 +91,7 @@ * This class tests the JDBC API for HiveServer2 via an embedded HiveServer2 instance * */ +@SuppressWarnings("deprecation") public class TestJdbcDriver2 { private static final Logger LOG = LoggerFactory.getLogger(TestJdbcDriver2.class); private static final String driverName = "org.apache.hive.jdbc.HiveDriver"; @@ -1546,6 +1545,128 @@ public void testDatabaseMetaData() throws SQLException { assertTrue(meta.getDatabaseMajorVersion() > -1); assertTrue(meta.getDatabaseMinorVersion() > -1); } + + @Test + public void testDatabaseMetaDataGetTables() throws SQLException { + String dbName = "test_db_metadata_gettables"; + String tblName = "test_tbl"; + String tblNameExt = "test_tbl_ext"; + String viewName = "test_view"; + String viewNameMaterialized = "test_view_materialized"; + List tablesAndViews = new ArrayList(Arrays.asList(tblName, tblNameExt, viewName, viewNameMaterialized)); + Statement stmt = con.createStatement(); + DatabaseMetaData dbMetadata = con.getMetaData(); + ResultSet resultSet; + // create test database + stmt.execute("create database if not exists " + dbName); + // create table + stmt.execute("set " + ConfVars.HIVE_SUPPORT_CONCURRENCY.varname + "=true"); + stmt.execute("set " + ConfVars.HIVE_TXN_MANAGER.varname + "=org.apache.hadoop.hive.ql.lockmgr.DbTxnManager"); + stmt.execute("create transactional table if not exists " + dbName + "." + tblName + " (col1 int , col2 string)"); + // create external table + stmt.execute("create external table if not exists " + dbName + "." + tblNameExt + " (col1 int , col2 string)"); + // create view + stmt.execute("create view " + dbName + "." + viewName + " as select * from " + dbName + "." + tblName); + // create materialized view + stmt.execute("create materialized view " + dbName + "." + viewNameMaterialized + " as select * from " + dbName + "." + tblName); + // get all "TABLE" objects + resultSet = dbMetadata.getTables(null, dbName, null, new String[] { "TABLE" }); + int count = 0; + while (resultSet.next()) { + String tName = resultSet.getString("TABLE_NAME"); + assertTrue(tablesAndViews.contains(tName)); + count++; + } + assertEquals(count, 2); + // get all "VIEW" objects + resultSet = dbMetadata.getTables(null, dbName, null, new String[] { "VIEW" }); + count = 0; + while (resultSet.next()) { + String tName = resultSet.getString("TABLE_NAME"); + assertTrue(tablesAndViews.contains(tName)); + count++; + } + assertEquals(count, 1); + // get all "VIEW" objects + resultSet = dbMetadata.getTables(null, dbName, null, new String[] { "VIEW" }); + count = 0; + while (resultSet.next()) { + String tName = resultSet.getString("TABLE_NAME"); + assertTrue(tablesAndViews.contains(tName)); + count++; + } + // get all "MATERIALIZED_VIEW" objects + resultSet = dbMetadata.getTables(null, dbName, null, new String[] { "MATERIALIZED_VIEW" }); + count = 0; + while (resultSet.next()) { + String tName = resultSet.getString("TABLE_NAME"); + assertTrue(tablesAndViews.contains(tName)); + count++; + } + assertEquals(count, 1); + // get all "TABLE" and "VIEW" objects + resultSet = dbMetadata.getTables(null, dbName, null, new String[] { "TABLE", "VIEW", "MATERIALIZED_VIEW" }); + count = 0; + while (resultSet.next()) { + String tName = resultSet.getString("TABLE_NAME"); + assertTrue(tablesAndViews.contains(tName)); + count++; + } + assertEquals(count, 4); + // passing null for table types should return all tables and views + resultSet = dbMetadata.getTables(null, dbName, null, null); + count = 0; + while (resultSet.next()) { + String tName = resultSet.getString("TABLE_NAME"); + assertTrue(tablesAndViews.contains(tName)); + count++; + } + assertEquals(count, 3); + // passing EXTERNAL_TABLE should not return anything + resultSet = dbMetadata.getTables(null, dbName, null, new String[] { "EXTERNAL_TABLE" }); + count = 0; + while (resultSet.next()) { + count++; + } + assertEquals(count, 0); + // passing TABLE, EXTERNAL_TABLE should return all tables (EXTERNAL_TABLE will be ignored) + resultSet = dbMetadata.getTables(null, dbName, null, new String[] { "TABLE", "EXTERNAL_TABLE" }); + count = 0; + while (resultSet.next()) { + String tName = resultSet.getString("TABLE_NAME"); + assertTrue(tablesAndViews.contains(tName)); + count++; + } + assertEquals(count, 2); + // passing EXTERNAL TABLE should not return anything + resultSet = dbMetadata.getTables(null, dbName, null, new String[] { "EXTERNAL TABLE" }); + count = 0; + while (resultSet.next()) { + count++; + } + assertEquals(count, 0); + // passing MANAGED_TABLE should not return anything + resultSet = dbMetadata.getTables(null, dbName, null, new String[] { "MANAGED_TABLE" }); + count = 0; + while (resultSet.next()) { + count++; + } + assertEquals(count, 0); + // passing junk should not return anything + resultSet = dbMetadata.getTables(null, dbName, null, new String[] { "JUNK" }); + count = 0; + while (resultSet.next()) { + count++; + } + assertEquals(count, 0); + + // clean up + stmt.execute("drop view " + dbName + "." + viewName); + stmt.execute("drop view " + dbName + "." + viewNameMaterialized); + stmt.execute("drop table " + dbName + "." + tblName); + stmt.execute("drop table " + dbName + "." + tblNameExt); + stmt.execute("drop database " + dbName); + } @Test public void testClientInfo() throws SQLException { diff --git a/service/src/java/org/apache/hive/service/cli/operation/GetTablesOperation.java b/service/src/java/org/apache/hive/service/cli/operation/GetTablesOperation.java index 5ccdc94773..5e5197a918 100644 --- a/service/src/java/org/apache/hive/service/cli/operation/GetTablesOperation.java +++ b/service/src/java/org/apache/hive/service/cli/operation/GetTablesOperation.java @@ -24,6 +24,7 @@ import org.apache.hadoop.hive.conf.HiveConf; import org.apache.hadoop.hive.metastore.IMetaStoreClient; +import org.apache.hadoop.hive.metastore.TableType; import org.apache.hadoop.hive.metastore.api.TableMeta; import org.apache.hadoop.hive.ql.security.authorization.plugin.HiveOperationType; import org.apache.hadoop.hive.ql.security.authorization.plugin.HivePrivilegeObject; @@ -86,7 +87,23 @@ protected GetTablesOperation(HiveSession parentSession, if (tableTypes != null) { tableTypeList = new ArrayList(); for (String tableType : tableTypes) { - tableTypeList.addAll(Arrays.asList(tableTypeMapping.mapToHiveType(tableType.trim()))); + if (tableMappingStr.equalsIgnoreCase("HIVE")) { + tableTypeList.addAll(Arrays.asList(tableTypeMapping.mapToHiveType(tableType.trim()))); + } else { + // A table type passed from the client side can only be one of the values returned by + // {@link org.apache.hive.jdbc.HiveDatabaseMetaData#getTableTypes}, + // which returns: TABLE, VIEW, INDEX_TABLE + if (tableType.equalsIgnoreCase("TABLE")) { + tableTypeList.add(TableType.MANAGED_TABLE.toString()); + tableTypeList.add(TableType.EXTERNAL_TABLE.toString()); + } + if (tableType.equalsIgnoreCase("VIEW")) { + tableTypeList.add(TableType.VIRTUAL_VIEW.toString()); + } + if (tableType.equalsIgnoreCase("MATERIALIZED_VIEW")) { + tableTypeList.add(TableType.MATERIALIZED_VIEW.toString()); + } + } } } else { tableTypeList = null;