Repository: hive Updated Branches: refs/heads/master 84f4e3a3b -> 267633a24
HIVE-16188: beeline should block the connection if given invalid database name. (Sahil Takiar, reviewed by Sergio Pena, Vihang Karajgaonkar, Pavas Garg) Project: http://git-wip-us.apache.org/repos/asf/hive/repo Commit: http://git-wip-us.apache.org/repos/asf/hive/commit/267633a2 Tree: http://git-wip-us.apache.org/repos/asf/hive/tree/267633a2 Diff: http://git-wip-us.apache.org/repos/asf/hive/diff/267633a2 Branch: refs/heads/master Commit: 267633a24461af41915e1aa0657c22beafdad84f Parents: 84f4e3a Author: Sahil Takiar <takiar.sa...@gmail.com> Authored: Mon Mar 27 15:58:11 2017 -0700 Committer: Sergio Pena <sergio.p...@cloudera.com> Committed: Mon Mar 27 16:04:28 2017 -0700 ---------------------------------------------------------------------- .../test/java/org/apache/hive/jdbc/TestJdbcDriver2.java | 9 ++++++++- .../java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java | 12 ++++++++++++ .../hive/service/cli/session/HiveSessionImpl.java | 7 +++++++ 3 files changed, 27 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/hive/blob/267633a2/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java ---------------------------------------------------------------------- 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 35aad6d..6e9223a 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 @@ -30,6 +30,7 @@ import org.apache.hadoop.hive.ql.exec.UDF; import org.apache.hadoop.hive.ql.processors.DfsProcessor; import org.apache.hive.common.util.HiveVersionInfo; import org.apache.hive.jdbc.Utils.JdbcConnectionParams; +import org.apache.hive.service.cli.HiveSQLException; import org.apache.hive.service.cli.operation.ClassicTableTypeMapping; import org.apache.hive.service.cli.operation.ClassicTableTypeMapping.ClassicTableTypes; import org.apache.hive.service.cli.operation.HiveTableTypeMapping; @@ -577,7 +578,7 @@ public class TestJdbcDriver2 { @Test public void testSetOnConnection() throws Exception { - Connection connection = getConnection("test?conf1=conf2;conf3=conf4#var1=var2;var3=var4"); + Connection connection = getConnection(testDbName + "?conf1=conf2;conf3=conf4#var1=var2;var3=var4"); try { verifyConfValue(connection, "conf1", "conf2"); verifyConfValue(connection, "conf3", "conf4"); @@ -2922,4 +2923,10 @@ public class TestJdbcDriver2 { assertEquals(rowCount, dataFileRowCount); stmt.execute("drop table " + tblName); } + + // Test that opening a JDBC connection to a non-existent database throws a HiveSQLException + @Test(expected = HiveSQLException.class) + public void testConnectInvalidDatabase() throws SQLException { + DriverManager.getConnection("jdbc:hive2:///databasedoesnotexist", "", ""); + } } http://git-wip-us.apache.org/repos/asf/hive/blob/267633a2/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java ---------------------------------------------------------------------- diff --git a/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java b/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java index 3780b4e..fc2cb08 100644 --- a/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java +++ b/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java @@ -112,6 +112,14 @@ public class TestJdbcWithMiniHS2 { stmt.execute("drop database if exists " + testDbName + " cascade"); stmt.execute("create database " + testDbName); stmt.close(); + + try { + openTestConnections(); + } catch (Exception e) { + System.out.println("Unable to open default connections to MiniHS2: " + e); + throw e; + } + // tables in test db createTestTables(conTestDb, testDbName); } @@ -183,6 +191,7 @@ public class TestJdbcWithMiniHS2 { HiveConf conf = new HiveConf(); startMiniHS2(conf); openDefaultConnections(); + openTestConnections(); } private static void startMiniHS2(HiveConf conf) throws Exception { @@ -208,6 +217,9 @@ public class TestJdbcWithMiniHS2 { private static void openDefaultConnections() throws Exception { conDefault = getConnection(); + } + + private static void openTestConnections() throws Exception { conTestDb = getConnection(testDbName); } http://git-wip-us.apache.org/repos/asf/hive/blob/267633a2/service/src/java/org/apache/hive/service/cli/session/HiveSessionImpl.java ---------------------------------------------------------------------- diff --git a/service/src/java/org/apache/hive/service/cli/session/HiveSessionImpl.java b/service/src/java/org/apache/hive/service/cli/session/HiveSessionImpl.java index fd74d55..418f453 100644 --- a/service/src/java/org/apache/hive/service/cli/session/HiveSessionImpl.java +++ b/service/src/java/org/apache/hive/service/cli/session/HiveSessionImpl.java @@ -275,6 +275,13 @@ public class HiveSessionImpl implements HiveSession { throw new HiveSQLException(e); } } else if (key.startsWith("use:")) { + try { + if (sessionHive.getDatabase(entry.getValue()) == null) { + throw new HiveSQLException("Database " + entry.getValue() + " does not exist"); + } + } catch (HiveException e) { + throw new HiveSQLException(e); + } SessionState.get().setCurrentDatabase(entry.getValue()); } else { sessionConf.verifyAndSet(key, entry.getValue());