From 52290e72b3029eddc17f0a0482fd6c1a05840bb0 Mon Sep 17 00:00:00 2001 From: Aihua Xu Date: Wed, 21 Mar 2018 17:36:50 -0700 Subject: [PATCH] HIVE-19018: beeline -e now requires semicolon even when used with query from command line (Aihua Xu, reviewed by Yongzhi Chen) --- .../java/org/apache/hive/beeline/BeeLine.java | 2 ++ .../java/org/apache/hive/beeline/Commands.java | 1 - .../org/apache/hive/beeline/TestCommands.java | 16 ++++++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/beeline/src/java/org/apache/hive/beeline/BeeLine.java b/beeline/src/java/org/apache/hive/beeline/BeeLine.java index 402fadddde70..c6d009c19afa 100644 --- a/beeline/src/java/org/apache/hive/beeline/BeeLine.java +++ b/beeline/src/java/org/apache/hive/beeline/BeeLine.java @@ -795,6 +795,8 @@ int initArgs(String[] args) { int code = 0; if (cl.getOptionValues('e') != null) { commands = Arrays.asList(cl.getOptionValues('e')); + opts.setAllowMultiLineCommand(false); //When using -e, command is always a single line + } if (!commands.isEmpty() && getOpts().getScriptFile() != null) { diff --git a/beeline/src/java/org/apache/hive/beeline/Commands.java b/beeline/src/java/org/apache/hive/beeline/Commands.java index d5806a4a22f8..e46c0cfbe575 100644 --- a/beeline/src/java/org/apache/hive/beeline/Commands.java +++ b/beeline/src/java/org/apache/hive/beeline/Commands.java @@ -1072,7 +1072,6 @@ private boolean executeInternal(String sql, boolean call) { * Check if the input line is a multi-line command which needs to read further */ public String handleMultiLineCmd(String line) throws IOException { - //When using -e, console reader is not initialized and command is always a single line int[] startQuote = {-1}; line = HiveStringUtils.removeComments(line, startQuote); while (isMultiLine(line) && beeLine.getOpts().isAllowMultiLineCommand()) { diff --git a/beeline/src/test/org/apache/hive/beeline/TestCommands.java b/beeline/src/test/org/apache/hive/beeline/TestCommands.java index 8a51f9800e92..567ca25270cc 100644 --- a/beeline/src/test/org/apache/hive/beeline/TestCommands.java +++ b/beeline/src/test/org/apache/hive/beeline/TestCommands.java @@ -23,6 +23,8 @@ import static org.apache.hive.common.util.HiveStringUtils.removeComments; import static org.junit.Assert.assertEquals; +import java.io.IOException; + public class TestCommands { @Test @@ -43,5 +45,19 @@ public void testLinesEndingWithComments() { assertEquals("'show --comments tables'", removeComments("'show --comments tables' --comments",escape)); assertEquals("'\"show --comments tables\"'", removeComments("'\"show --comments tables\"' --comments",escape)); } + + /** + * Test the commands directly call from beeline. + * @throws IOException + */ + @Test + public void testBeelineCommands() throws IOException { + // avoid System.exit() call in beeline which causes JVM to exit and fails the test + System.setProperty(BeeLineOpts.PROPERTY_NAME_EXIT, "true"); + // Verify the command without ';' at the end also works fine + BeeLine.mainWithInputRedirection(new String[] {"-u", "jdbc:hive2://", "-e", "select 3"}, null); + BeeLine.mainWithInputRedirection( + new String[] {"-u", "jdbc:hive2://", "-e", "create table t1(x int); show tables"}, null); + } }