diff --git a/beeline/src/java/org/apache/hive/beeline/Commands.java b/beeline/src/java/org/apache/hive/beeline/Commands.java index a25e09b..975caaa 100644 --- a/beeline/src/java/org/apache/hive/beeline/Commands.java +++ b/beeline/src/java/org/apache/hive/beeline/Commands.java @@ -1151,7 +1151,7 @@ private boolean execute(String line, boolean call, boolean entireLineAsCommand) StringBuffer command = new StringBuffer(); boolean hasUnterminatedDoubleQuote = false; - boolean hasUntermindatedSingleQuote = false; + boolean hasUnterminatedSingleQuote = false; int lastSemiColonIndex = 0; char[] lineChars = line.toCharArray(); @@ -1162,25 +1162,29 @@ private boolean execute(String line, boolean call, boolean entireLineAsCommand) switch (lineChars[index]) { case '\'': if (!hasUnterminatedDoubleQuote && !wasPrevEscape) { - hasUntermindatedSingleQuote = !hasUntermindatedSingleQuote; + hasUnterminatedSingleQuote = !hasUnterminatedSingleQuote; } wasPrevEscape = false; break; case '\"': - if (!hasUntermindatedSingleQuote && !wasPrevEscape) { + if (!hasUnterminatedSingleQuote && !wasPrevEscape) { hasUnterminatedDoubleQuote = !hasUnterminatedDoubleQuote; } wasPrevEscape = false; break; case ';': - if (!hasUnterminatedDoubleQuote && !hasUntermindatedSingleQuote) { + if (!hasUnterminatedDoubleQuote && !hasUnterminatedSingleQuote) { addCmdPart(cmdList, command, line.substring(lastSemiColonIndex, index)); lastSemiColonIndex = index + 1; } wasPrevEscape = false; break; case '\\': - wasPrevEscape = true; + if (wasPrevEscape) { + wasPrevEscape = false; + } else { + wasPrevEscape = true; + } break; default: wasPrevEscape = false; diff --git a/itests/hive-unit/src/test/java/org/apache/hive/beeline/TestBeeLineWithArgs.java b/itests/hive-unit/src/test/java/org/apache/hive/beeline/TestBeeLineWithArgs.java index 49c1120..8c05e2c 100644 --- a/itests/hive-unit/src/test/java/org/apache/hive/beeline/TestBeeLineWithArgs.java +++ b/itests/hive-unit/src/test/java/org/apache/hive/beeline/TestBeeLineWithArgs.java @@ -903,4 +903,17 @@ public void testShowDbInPrompt() throws Throwable { testScriptFile( SCRIPT_TEXT, EXPECTED_PATTERN, true, argList); } + + /** + * Test that Beeline can handle \\ characters within a string literal. Either at the beginning, middle, or end of the + * literal. + */ + @Test + public void testBackslashAsLastCharacterInLiteral() throws Throwable { + String SCRIPT_TEXT = "select 'hello\\\\', '\\\\hello', 'hel\\\\lo' as literal;"; + final String EXPECTED_PATTERN = "hello\\\t\\hello\thel\\lo"; + List argList = getBaseArgs(miniHS2.getBaseJdbcURL()); + argList.add("--outputformat=tsv2"); + testScriptFile(SCRIPT_TEXT, EXPECTED_PATTERN, true, argList); + } }