diff --git beeline/src/java/org/apache/hive/beeline/Commands.java beeline/src/java/org/apache/hive/beeline/Commands.java index 88a94d7..6885069 100644 --- beeline/src/java/org/apache/hive/beeline/Commands.java +++ beeline/src/java/org/apache/hive/beeline/Commands.java @@ -687,10 +687,8 @@ private boolean execute(String line, boolean call) { } } - String extra = beeLine.getConsoleReader().readLine(prompt.toString()); - if (!beeLine.isComment(extra)) { - line += " " + extra; - } + line = pruneCommentOut(line); // required for first query line + line += " " + pruneCommentOut(beeLine.getConsoleReader().readLine(prompt.toString())); } } catch (Exception e) { beeLine.handleException(e); @@ -771,6 +769,34 @@ private boolean execute(String line, boolean call) { return true; } + private String pruneCommentOut(String line) { + // SQL92 comment prefix is "--" + // beeline also supports shell-style "#" prefix + String queryLine = line.trim(); + String prunedQueryLine; + + int dashStartingAt = queryLine.indexOf("--"); + int hashStartingAt = queryLine.indexOf("#"); + + if (-1 == dashStartingAt && -1 == hashStartingAt) { + // no comment + prunedQueryLine = queryLine; + } else if (0 == dashStartingAt || 0 == hashStartingAt) { + // only comment + prunedQueryLine = ""; + } else if ((-1 != dashStartingAt && + -1 != hashStartingAt && dashStartingAt < hashStartingAt) || + (-1 != dashStartingAt && -1 == hashStartingAt)) { + // inline "--" comment + prunedQueryLine = queryLine.substring(0, dashStartingAt - 1); + } else { + // inline "#" comment + prunedQueryLine = queryLine.substring(0, hashStartingAt - 1); + } + + return prunedQueryLine; + } + public boolean quit(String line) { beeLine.setExit(true); 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 140c1bc..14235d9 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 @@ -469,4 +469,15 @@ public void testEmbeddedBeelineConnection() throws Throwable{ final String EXPECTED_PATTERN = "embedded_table"; testScriptFile(TEST_NAME, SCRIPT_TEXT, EXPECTED_PATTERN, true, argList); } + + @Test + public void testInlineCommentInQuery() throws Throwable { + List argList = getBaseArgs(JDBC_URL); + argList.add("--hivevar"); + argList.add("DUMMY_TBL=dummy"); + final String TEST_NAME = "testInlineCommentInQuery"; + final String SCRIPT_TEXT = "create -- COMMENT\ntable ${DUMMY_TBL} #COMMENT2\n (d int);\nshow tables;\n"; + final String EXPECTED_PATTERN = "dummy"; + testScriptFile(TEST_NAME, SCRIPT_TEXT, EXPECTED_PATTERN, true, argList); + } }