commit e5f9a2fb84781d1ec6622ce30e22999b4bca14dd Author: Vihang Karajgaonkar Date: Mon May 8 17:17:26 2017 -0700 HIVE-14389 : Beeline should not output query and prompt to stdout diff --git beeline/src/java/org/apache/hive/beeline/BeeLine.java beeline/src/java/org/apache/hive/beeline/BeeLine.java index a589f337381258a96425f9cae2e57eea72f70bb6..1aed085f93420c8fa4fb3da62b608adb41eb589c 100644 --- beeline/src/java/org/apache/hive/beeline/BeeLine.java +++ beeline/src/java/org/apache/hive/beeline/BeeLine.java @@ -141,6 +141,7 @@ private OutputFile recordOutputFile = null; private PrintStream outputStream = new PrintStream(System.out, true); private PrintStream errorStream = new PrintStream(System.err, true); + private InputStream inputStream = System.in; private ConsoleReader consoleReader; private List batch = null; private final Reflector reflector = new Reflector(this); @@ -1223,10 +1224,10 @@ public ConsoleReader initializeConsoleReader(InputStream inputStream) throws IOE // by appending a newline to the end of inputstream InputStream inputStreamAppendedNewline = new SequenceInputStream(inputStream, new ByteArrayInputStream((new String("\n")).getBytes())); - consoleReader = new ConsoleReader(inputStreamAppendedNewline, getOutputStream()); + consoleReader = new ConsoleReader(inputStreamAppendedNewline, getErrorStream()); consoleReader.setCopyPasteDetection(true); // jline will detect if is regular character } else { - consoleReader = new ConsoleReader(); + consoleReader = new ConsoleReader(getInputStream(), getErrorStream()); } //disable the expandEvents for the purpose of backward compatibility @@ -2404,6 +2405,10 @@ PrintStream getErrorStream() { return errorStream; } + InputStream getInputStream() { + return inputStream; + } + ConsoleReader getConsoleReader() { return consoleReader; } diff --git beeline/src/test/org/apache/hive/beeline/cli/TestHiveCli.java beeline/src/test/org/apache/hive/beeline/cli/TestHiveCli.java index d306e29f5a9b8c57182a70a1428300209c720a2b..8eaa68308f00b98f2dc7af21362032ba961e08d6 100644 --- beeline/src/test/org/apache/hive/beeline/cli/TestHiveCli.java +++ beeline/src/test/org/apache/hive/beeline/cli/TestHiveCli.java @@ -112,7 +112,7 @@ public void testCmd() { @Test public void testSetPromptValue() { - verifyCMD("set hive.cli.prompt=MYCLI;SHOW\nTABLES;", "MYCLI> ", os, null, + verifyCMD("set hive.cli.prompt=MYCLI;SHOW\nTABLES;", "MYCLI> ", errS, null, ERRNO_OK, true); } @@ -213,21 +213,21 @@ public void testErrOutput() { 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_OTHER, true); + + "use default;drop if exists testDB;", "hive (testDB)>", errS, 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_OTHER, true); + "hive (testDB)>", errS, 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_OTHER, true); + + "use default;drop if exists testDB;", "hive (testDB)>", errS, null, ERRNO_OTHER, true); } @Test diff --git itests/hive-unit/src/test/java/org/apache/hive/beeline/TestBeeLineWithArgs.java itests/hive-unit/src/test/java/org/apache/hive/beeline/TestBeeLineWithArgs.java index 75f46eca045e7d74f21ace943226fbd42c13209d..56150d5eee8c07c4a8e1d304029b493b3bb7833a 100644 --- itests/hive-unit/src/test/java/org/apache/hive/beeline/TestBeeLineWithArgs.java +++ itests/hive-unit/src/test/java/org/apache/hive/beeline/TestBeeLineWithArgs.java @@ -1082,4 +1082,33 @@ public void testBackslashInLiteral() throws Throwable { argList.add("--outputformat=tsv2"); testScriptFile(SCRIPT_TEXT, argList, EXPECTED_PATTERN, true); } + + @Test + public void testRedirectOutputStream() throws Throwable { + String SCRIPT_TEXT = "create table if not exists new_table_cities(col1 string);\n " + + "insert into new_table_cities values (\"palo alto\");\n"; + List argList = getBaseArgs(miniHS2.getBaseJdbcURL()); + argList.add("--outputformat=tsv2"); + argList.add("--showHeader=false"); + // stderr should contain query text + testScriptFile(SCRIPT_TEXT, argList, OutStream.ERR, "cities", true); + SCRIPT_TEXT = "select * from new_table_cities;\n"; + // stderr should contain query text + testScriptFile(SCRIPT_TEXT, argList, OutStream.ERR, "cities", true); + // stderr should not contain data + testScriptFile(SCRIPT_TEXT, argList, OutStream.ERR, "palo alto", false); + //stdout should contain the data + testScriptFile(SCRIPT_TEXT, argList, OutStream.OUT, "palo alto", true); + //stdout should not contain query text + testScriptFile(SCRIPT_TEXT, argList, OutStream.OUT, "cities", false); + + // stderr should contain query text + testCommandEnclosedQuery(SCRIPT_TEXT, "cities", true, argList, OutStream.ERR); + // stderr should not contain data + testCommandEnclosedQuery(SCRIPT_TEXT, "palo alto", false, argList, OutStream.ERR); + //stdout should contain the data + testCommandEnclosedQuery(SCRIPT_TEXT, "palo alto", true, argList, OutStream.OUT); + //stdout should not contain query text + testCommandEnclosedQuery(SCRIPT_TEXT, "cities", false, argList, OutStream.OUT); + } }