diff --git a/cli/src/java/org/apache/hadoop/hive/cli/CliDriver.java b/cli/src/java/org/apache/hadoop/hive/cli/CliDriver.java index 5364ca6..27fd66d 100644 --- a/cli/src/java/org/apache/hadoop/hive/cli/CliDriver.java +++ b/cli/src/java/org/apache/hadoop/hive/cli/CliDriver.java @@ -384,8 +384,11 @@ public void handle(Signal signal) { try { int lastRet = 0, ret = 0; + // we can not use "split" function directly as ";" may be quoted + List commands = splitSemiColon(line); + String command = ""; - for (String oneCmd : line.split(";")) { + for (String oneCmd : commands) { if (StringUtils.endsWith(oneCmd, "\\")) { command += StringUtils.chop(oneCmd) + ";"; @@ -415,6 +418,47 @@ public void handle(Signal signal) { } } } + + public static List splitSemiColon(String line) { + boolean insideSingleQuote = false; + boolean insideDoubleQuote = false; + boolean escape = false; + int beginIndex = 0; + List ret = new ArrayList<>(); + for (int index = 0; index < line.length(); index++) { + if (line.charAt(index) == '\'') { + // take a look to see if it is escaped + if (!escape) { + // flip the boolean variable + insideSingleQuote = !insideSingleQuote; + } + } else if (line.charAt(index) == '\"') { + // take a look to see if it is escaped + if (!escape) { + // flip the boolean variable + insideDoubleQuote = !insideDoubleQuote; + } + } else if (line.charAt(index) == ';') { + if (insideSingleQuote || insideDoubleQuote) { + // do not split + } else { + // split, do not include ; itself + ret.add(line.substring(beginIndex, index)); + beginIndex = index + 1; + } + } else { + // nothing to do + } + // set the escape + if (escape) { + escape = false; + } else if (line.charAt(index) == '\\') { + escape = true; + } + } + ret.add(line.substring(beginIndex)); + return ret; + } public int processReader(BufferedReader r) throws IOException { String line; diff --git a/itests/util/src/main/java/org/apache/hadoop/hive/ql/QTestUtil.java b/itests/util/src/main/java/org/apache/hadoop/hive/ql/QTestUtil.java index 5bf23e7..b365ae6 100644 --- a/itests/util/src/main/java/org/apache/hadoop/hive/ql/QTestUtil.java +++ b/itests/util/src/main/java/org/apache/hadoop/hive/ql/QTestUtil.java @@ -64,6 +64,7 @@ import java.util.regex.Pattern; import com.google.common.base.Preconditions; + import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; @@ -1347,7 +1348,7 @@ public int executeClient(String tname) { } private int executeClientInternal(String commands) { - String [] cmds = commands.split(";"); + List cmds = CliDriver.splitSemiColon(commands); int rc = 0; String command = ""; @@ -1470,9 +1471,9 @@ private String getCommand(String tname) { StringBuilder newCommands = new StringBuilder(commands.length()); int lastMatchEnd = 0; Matcher commentMatcher = Pattern.compile("^--.*$", Pattern.MULTILINE).matcher(commands); + // remove the comments while (commentMatcher.find()) { newCommands.append(commands.substring(lastMatchEnd, commentMatcher.start())); - newCommands.append(commentMatcher.group().replaceAll("(?