diff --git itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniMr.java itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniMr.java index 56c8905..e459c4e 100644 --- itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniMr.java +++ itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniMr.java @@ -23,10 +23,12 @@ import static org.junit.Assert.assertTrue; import java.sql.Connection; +import java.sql.DatabaseMetaData; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; +import java.sql.Types; import java.util.HashMap; import java.util.Map; @@ -226,6 +228,46 @@ public void testTempTable() throws Exception { " where value = '" + resultVal + "'"; verifyResult(queryStr, resultVal, 2); + + // Test getTables() + DatabaseMetaData md = hs2Conn.getMetaData(); + assertTrue(md.getConnection() == hs2Conn); + + ResultSet rs = md.getTables(null, null, tempTableName, null); + boolean foundTable = false; + while (rs.next()) { + String tableName = rs.getString(3); + if (tableName.equalsIgnoreCase(tempTableName)) { + assertFalse("Table not found yet", foundTable); + foundTable = true; + } + } + assertTrue("Found temp table", foundTable); + + // Test getTables() with no table name pattern + rs = md.getTables(null, null, null, null); + foundTable = false; + while (rs.next()) { + String tableName = rs.getString(3); + if (tableName.equalsIgnoreCase(tempTableName)) { + assertFalse("Table not found yet", foundTable); + foundTable = true; + } + } + assertTrue("Found temp table", foundTable); + + // Test getColumns() + rs = md.getColumns(null, null, tempTableName, null); + assertTrue("First row", rs.next()); + assertTrue(rs.getString(3).equalsIgnoreCase(tempTableName)); + assertTrue(rs.getString(4).equalsIgnoreCase("key")); + assertEquals(Types.VARCHAR, rs.getInt(5)); + + assertTrue("Second row", rs.next()); + assertTrue(rs.getString(3).equalsIgnoreCase(tempTableName)); + assertTrue(rs.getString(4).equalsIgnoreCase("value")); + assertEquals(Types.VARCHAR, rs.getInt(5)); + // A second connection should not be able to see the table Connection conn2 = DriverManager.getConnection(miniHS2.getJdbcURL(dbName), System.getProperty("user.name"), "bar"); diff --git metastore/src/java/org/apache/hadoop/hive/metastore/HiveMetaStoreClient.java metastore/src/java/org/apache/hadoop/hive/metastore/HiveMetaStoreClient.java index 4285c94..c119423 100644 --- metastore/src/java/org/apache/hadoop/hive/metastore/HiveMetaStoreClient.java +++ metastore/src/java/org/apache/hadoop/hive/metastore/HiveMetaStoreClient.java @@ -1575,7 +1575,7 @@ protected PrincipalPrivilegeSet deepCopy(PrincipalPrivilegeSet pps) { return copy; } - private List deepCopyFieldSchemas(List schemas) { + protected List deepCopyFieldSchemas(List schemas) { List copy = null; if (schemas != null) { copy = new ArrayList(); 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 ce5b284..183133c 100644 --- ql/src/java/org/apache/hadoop/hive/ql/metadata/SessionHiveMetaStoreClient.java +++ ql/src/java/org/apache/hadoop/hive/ql/metadata/SessionHiveMetaStoreClient.java @@ -59,6 +59,7 @@ import org.apache.hadoop.hive.metastore.api.PrincipalPrivilegeSet; import org.apache.hadoop.hive.metastore.api.TableStatsRequest; import org.apache.hadoop.hive.metastore.api.UnknownDBException; +import org.apache.hadoop.hive.metastore.api.UnknownTableException; import org.apache.hadoop.hive.ql.session.SessionState; import org.apache.hadoop.hive.ql.stats.StatsUtils; import org.apache.thrift.TException; @@ -211,7 +212,6 @@ protected void drop_table_with_environment_context(String dbname, String name, return tables; } - @Override public boolean tableExists(String databaseName, String tableName) throws MetaException, TException, UnknownDBException { @@ -226,6 +226,20 @@ public boolean tableExists(String databaseName, String tableName) throws MetaExc } @Override + public List getSchema(String dbName, String tableName) + throws MetaException, TException, UnknownTableException, + UnknownDBException { + // First check temp tables + org.apache.hadoop.hive.metastore.api.Table table = getTempTable(dbName, tableName); + if (table != null) { + return deepCopyFieldSchemas(table.getSd().getCols()); + } + + // Try underlying client + return super.getSchema(dbName, tableName); + } + + @Override public void alter_table(String dbname, String tbl_name, org.apache.hadoop.hive.metastore.api.Table new_tbl, boolean cascade) throws InvalidOperationException, MetaException, TException { org.apache.hadoop.hive.metastore.api.Table old_tbl = getTempTable(dbname, tbl_name);