diff --git itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java index 912c6c3..b271d65 100644 --- itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java +++ itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java @@ -163,5 +163,46 @@ public void testURIDatabaseName() throws Exception{ stmt.execute(" drop table if exists table_in_non_default_schema"); expected = stmt.execute("DROP DATABASE "+ dbName); stmt.close(); + + hs2Conn = DriverManager.getConnection(jdbcUri+"default",System.getProperty("user.name"),"bar"); + stmt = hs2Conn .createStatement(); + res = stmt.executeQuery("show tables"); + testTableExists = false; + while (res.next()) { + assertNotNull("table name is null in result set", res.getString(1)); + if (tableInNonDefaultSchema.equalsIgnoreCase(res.getString(1))) { + testTableExists = true; + } + } + + // test URI with no dbName + hs2Conn = DriverManager.getConnection(jdbcUri, System.getProperty("user.name"),"bar"); + verifyCurrentDB("default", hs2Conn); + hs2Conn.close(); + + hs2Conn = DriverManager.getConnection(jdbcUri + ";", System.getProperty("user.name"),"bar"); + verifyCurrentDB("default", hs2Conn); + hs2Conn.close(); + + hs2Conn = DriverManager.getConnection(jdbcUri + ";/foo=bar;foo1=bar1", System.getProperty("user.name"),"bar"); + verifyCurrentDB("default", hs2Conn); + hs2Conn.close(); + } + + /** + * verify that the current db is the one expected. first create table as .tab and then + * describe that table to check if is the current database + * @param expectedDbName + * @param hs2Conn + * @throws Exception + */ + private void verifyCurrentDB(String expectedDbName, Connection hs2Conn) throws Exception { + String verifyTab = "miniHS2DbVerificationTable"; + Statement stmt = hs2Conn.createStatement(); + stmt.execute("DROP TABLE IF EXISTS " + expectedDbName + "." + verifyTab); + stmt.execute("CREATE TABLE " + expectedDbName + "." + verifyTab + "(id INT)"); + stmt.execute("DESCRIBE " + verifyTab); + stmt.execute("DROP TABLE IF EXISTS " + expectedDbName + "." + verifyTab); + stmt.close(); } } diff --git jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java index 0915171..aabb5cb 100644 --- jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java +++ jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java @@ -297,8 +297,10 @@ private void configureConnection(String dbName) throws SQLException { for (Entry hiveVar : hiveVarMap.entrySet()) { stmt.execute("set hivevar:" + hiveVar.getKey() + "=" + hiveVar.getValue()); } - if(dbName!=null) - stmt.execute("use "+dbName); + // if the client is setting a non-default db, then switch the database + if (!Utils.DEFAULT_DATABASE.equalsIgnoreCase(dbName)) { + stmt.execute("use " + dbName); + } stmt.close(); } } diff --git jdbc/src/java/org/apache/hive/jdbc/Utils.java jdbc/src/java/org/apache/hive/jdbc/Utils.java index 913dc46..47c38e7 100644 --- jdbc/src/java/org/apache/hive/jdbc/Utils.java +++ jdbc/src/java/org/apache/hive/jdbc/Utils.java @@ -229,17 +229,16 @@ public static JdbcConnectionParams parseURL(String uri) throws IllegalArgumentEx // dbname and session settings String sessVars = jdbcURI.getPath(); - if ((sessVars == null) || sessVars.isEmpty()) { - connParams.setDbName(DEFAULT_DATABASE); - } else { + if ((sessVars != null) && !sessVars.isEmpty()) { + String dbName = ""; // removing leading '/' returned by getPath() sessVars = sessVars.substring(1); if (!sessVars.contains(";")) { // only dbname is provided - connParams.setDbName(sessVars); + dbName = sessVars; } else { // we have dbname followed by session parameters - connParams.setDbName(sessVars.substring(0, sessVars.indexOf(';'))); + dbName = sessVars.substring(0, sessVars.indexOf(';')); sessVars = sessVars.substring(sessVars.indexOf(';')+1); if (sessVars != null) { Matcher sessMatcher = pattern.matcher(sessVars); @@ -248,6 +247,9 @@ public static JdbcConnectionParams parseURL(String uri) throws IllegalArgumentEx } } } + if (!dbName.isEmpty()) { + connParams.setDbName(dbName); + } } // parse hive conf settings