commit 99173578e904c18ab8446c1280a326f9e7a6c456 Author: Reuben Kuhnert Date: Wed Mar 30 06:47:22 2016 -0500 Fix for HIVE-12612 Change-Id: I426dcf9d5384a56ea0191ee7f39a1ec471f44b27 diff --git a/beeline/src/java/org/apache/hive/beeline/BeeLine.java b/beeline/src/java/org/apache/hive/beeline/BeeLine.java index 4ab6aa82fbf1b0308e7178f4527c70adacb20214..a4a955844c3ebbfed255df37e6b9da1894537740 100644 --- a/beeline/src/java/org/apache/hive/beeline/BeeLine.java +++ b/beeline/src/java/org/apache/hive/beeline/BeeLine.java @@ -953,26 +953,32 @@ private int executeFile(String fileName) { } private int execute(ConsoleReader reader, boolean exitOnError) { - String line; + int lastExecutionResult = ERRNO_OK; while (!exit) { try { // Execute one instruction; terminate on executing a script if there is an error // in silent mode, prevent the query and prompt being echoed back to terminal - line = (getOpts().isSilent() && getOpts().getScriptFile() != null) ? reader + String line = (getOpts().isSilent() && getOpts().getScriptFile() != null) ? reader .readLine(null, ConsoleReader.NULL_MASK) : reader.readLine(getPrompt()); // trim line - line = (line == null) ? null : line.trim(); + if (line != null) { + line = line.trim(); + } - if (!dispatch(line) && exitOnError) { - return ERRNO_OTHER; + if (!dispatch(line)) { + lastExecutionResult = ERRNO_OTHER; + if (exitOnError) break; + } else if (line != null) { + lastExecutionResult = ERRNO_OK; } + } catch (Throwable t) { handleException(t); return ERRNO_OTHER; } } - return ERRNO_OK; + return lastExecutionResult; } @Override diff --git a/beeline/src/test/org/apache/hive/beeline/cli/TestHiveCli.java b/beeline/src/test/org/apache/hive/beeline/cli/TestHiveCli.java index 275036ffe9fe98de98fb7e7c6d88bff39cf879e4..d306e29f5a9b8c57182a70a1428300209c720a2b 100644 --- a/beeline/src/test/org/apache/hive/beeline/cli/TestHiveCli.java +++ b/beeline/src/test/org/apache/hive/beeline/cli/TestHiveCli.java @@ -38,6 +38,7 @@ private static final Logger LOG = LoggerFactory.getLogger(TestHiveCli.class.getName()); private static final int ERRNO_OK = 0; private static final int ERRNO_ARGS = 1; + private static final int ERRNO_OTHER = 2; private final static String SOURCE_CONTEXT = "create table if not exists test.testSrcTbl(sc1 string);"; @@ -101,7 +102,7 @@ private void verifyCMD(String CMD, String keywords, OutputStream os, String[] op @Test public void testInValidCmd() { - verifyCMD("!lss\n", "Failed to execute lss", errS, null, ERRNO_OK, true); + verifyCMD("!lss\n", "Failed to execute lss", errS, null, ERRNO_OTHER, true); } @Test @@ -159,7 +160,7 @@ public void testSourceCmd2() { public void testSourceCmd3() { File f = generateTmpFile(SOURCE_CONTEXT4); verifyCMD("source " + f.getPath() + ";" + "desc testSrcTbl4;\nquit;\n", "src", os, - new String[] { "--database", "test" }, ERRNO_OK, true); + new String[] { "--database", "test" }, ERRNO_OTHER, true); f.delete(); } @@ -205,34 +206,34 @@ public void testVariablesForSource() { @Test public void testErrOutput() { verifyCMD("show tables;set system:xxx=5;set system:yyy=${system:xxx};\nlss;", - "cannot recognize input near 'lss' '' ''", errS, null, ERRNO_OK, true); + "cannot recognize input near 'lss' '' ''", errS, null, ERRNO_OTHER, true); } @Test public void testUseCurrentDB1() { verifyCMD( "create database if not exists testDB; set hive.cli.print.current.db=true;use testDB;\n" - + "use default;drop if exists testDB;", "hive (testDB)>", os, null, ERRNO_OK, true); + + "use default;drop if exists testDB;", "hive (testDB)>", os, null, ERRNO_OTHER, true); } @Test public void testUseCurrentDB2() { verifyCMD( "create database if not exists testDB; set hive.cli.print.current.db=true;use\ntestDB;\nuse default;drop if exists testDB;", - "hive (testDB)>", os, null, ERRNO_OK, true); + "hive (testDB)>", os, null, ERRNO_OTHER, true); } @Test public void testUseCurrentDB3() { verifyCMD( "create database if not exists testDB; set hive.cli.print.current.db=true;use testDB;\n" - + "use default;drop if exists testDB;", "hive (testDB)>", os, null, ERRNO_OK, true); + + "use default;drop if exists testDB;", "hive (testDB)>", os, null, ERRNO_OTHER, true); } @Test public void testUseInvalidDB() { verifyCMD("set hive.cli.print.current.db=true;use invalidDB;", - "hive (invalidDB)>", os, null, ERRNO_OK, false); + "hive (invalidDB)>", os, null, ERRNO_OTHER, false); } @Test