diff --git a/beeline/src/java/org/apache/hive/beeline/Commands.java b/beeline/src/java/org/apache/hive/beeline/Commands.java index 80703fffc234be2df56c6fca590b7d22821c455b..ebc80f2f4b84b195bb6200ee499df6775908458a 100644 --- a/beeline/src/java/org/apache/hive/beeline/Commands.java +++ b/beeline/src/java/org/apache/hive/beeline/Commands.java @@ -22,6 +22,7 @@ */ package org.apache.hive.beeline; +import com.google.common.annotations.VisibleForTesting; import org.apache.hadoop.hive.conf.HiveConf; import org.apache.hadoop.hive.conf.HiveVariableSource; import org.apache.hadoop.hive.conf.SystemVariables; @@ -1028,7 +1029,33 @@ private boolean executeInternal(String sql, boolean call) { return true; } + @VisibleForTesting + String removeComments(String line) { + if (line == null || line.isEmpty()) return line; + + StringBuilder builder = new StringBuilder(); + int escape = -1; + for (int index = 0; index < line.length(); index++) { + if (index < line.length() - 1 && line.charAt(index) == line.charAt(index + 1)) { + if (escape == -1 && line.charAt(index) == '-') return builder.toString().trim(); + } + + char letter = line.charAt(index); + if (letter == escape) { + escape = -1; // Turn escape off. + } else if (escape == -1 && (letter == '\'' || letter == '"')) { + escape = letter; // Turn escape on. + } + + builder.append(letter); + } + + return builder.toString().trim(); + } + public String handleMultiLineCmd(String line) throws IOException { + line = removeComments(line); + //When using -e, console reader is not initialized and command is a single line while (beeLine.getConsoleReader() != null && !(line.trim().endsWith(";")) && 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 new file mode 100644 index 0000000000000000000000000000000000000000..45b2ed78ff009a208bd54694a1a48cabea906b3a --- /dev/null +++ b/beeline/src/test/org/apache/hive/beeline/TestCommands.java @@ -0,0 +1,47 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hive.beeline; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class TestCommands { + + @Test + public void testLinesEndingWithComments() { + BeeLine beeline = new BeeLine(); + Commands commands = new Commands(beeline); + + assertEquals("show tables;", commands.removeComments("show tables;")); + assertEquals("show tables;", commands.removeComments("show tables; --comments")); + assertEquals("show tables;", commands.removeComments("show tables; -------comments")); + assertEquals("show tables;", commands.removeComments("show tables; -------comments;one;two;three;;;;")); + assertEquals("show", commands.removeComments("show-- tables; -------comments")); + assertEquals("show", commands.removeComments("show --tables; -------comments")); + assertEquals("s", commands.removeComments("s--how --tables; -------comments")); + assertEquals("", commands.removeComments("-- show tables; -------comments")); + + assertEquals("\"show tables\"", commands.removeComments("\"show tables\" --comments")); + assertEquals("\"show --comments tables\"", commands.removeComments("\"show --comments tables\" --comments")); + assertEquals("\"'show --comments' tables\"", commands.removeComments("\"'show --comments' tables\" --comments")); + assertEquals("'show --comments tables'", commands.removeComments("'show --comments tables' --comments")); + assertEquals("'\"show --comments tables\"'", commands.removeComments("'\"show --comments tables\"' --comments")); + } +}