diff --git cli/src/java/org/apache/hadoop/hive/cli/CliDriver.java cli/src/java/org/apache/hadoop/hive/cli/CliDriver.java index 874e5aa..a673790 100644 --- cli/src/java/org/apache/hadoop/hive/cli/CliDriver.java +++ cli/src/java/org/apache/hadoop/hive/cli/CliDriver.java @@ -421,7 +421,8 @@ public int processReader(BufferedReader r) throws IOException { while ((line = r.readLine()) != null) { // Skipping through comments - if (! line.startsWith("--")) { + line = removeComments(line); + if (line != null) { qsb.append(line + "\n"); } } @@ -768,7 +769,8 @@ private int executeDriver(CliSessionState ss, HiveConf conf, OptionsProcessor op if (!prefix.equals("")) { prefix += '\n'; } - if (line.trim().startsWith("--")) { + line = removeComments(line); + if (line == null) { continue; } if (line.trim().endsWith(";") && !line.trim().endsWith("\\;")) { @@ -871,4 +873,32 @@ public void setHiveVariables(Map hiveVariables) { SessionState.get().setHiveVariables(hiveVariables); } + private String removeComments(String line) { + boolean q1 = false; + boolean q2 = false; + for (int i = 0; i < line.length(); i++) { + switch (line.charAt(i)) { + case '\'': + if (i != 0 && line.charAt(i - 1) == '\\') { + break; + } + q1 = q1 ? false : true; + break; + case '\"': + if (i != 0 && line.charAt(i - 1) == '\\') { + break; + } + q2 = q2 ? false : true; + break; + case '-': + if (!q1 && !q2 && i != 0 && line.charAt(i - 1) == '-') { + String s = line.substring(0, i - 1); + return s.trim().isEmpty() ? null : s; + } + break; + } + } + + return line; + } } diff --git cli/src/test/org/apache/hadoop/hive/cli/TestCliDriverMethods.java cli/src/test/org/apache/hadoop/hive/cli/TestCliDriverMethods.java index bb1d865..cf6754f 100644 --- cli/src/test/org/apache/hadoop/hive/cli/TestCliDriverMethods.java +++ cli/src/test/org/apache/hadoop/hive/cli/TestCliDriverMethods.java @@ -35,6 +35,8 @@ import java.io.IOException; import java.io.PrintStream; import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.security.Permission; import java.util.ArrayList; import java.util.Arrays; @@ -57,6 +59,7 @@ import org.apache.hadoop.hive.ql.Driver; import org.apache.hadoop.hive.ql.processors.CommandProcessorResponse; import org.apache.hadoop.util.Shell; +import org.junit.Test; // Cannot call class TestCliDriver since that's the name of the generated @@ -315,6 +318,41 @@ public void testprocessInitFiles() throws Exception { } } + @Test + public void testRemoveComments() { + try { + CliDriver driver = new CliDriver(); + Method method = + CliDriver.class.getDeclaredMethod("removeComments", String.class); + method.setAccessible(true); + + assertEquals(method.invoke(driver, + "-- set abc=def;"), null); + assertEquals(method.invoke(driver, + " -- comments"), null); + assertEquals(method.invoke(driver, + "select -- comments"), "select "); + assertEquals(method.invoke(driver, + " replace('12345', '12', '--')"), " replace('12345', '12', '--')"); + assertEquals(method.invoke(driver, + " replace(\"12345\", \"12\", \"--\")"), + " replace(\"12345\", \"12\", \"--\")"); + assertEquals(method.invoke(driver, + " replace('12345', '12', '--') -- comments"), + " replace('12345', '12', '--') "); + assertEquals(method.invoke(driver, + "select -- commnets '-- comments'"), "select "); + assertEquals(method.invoke(driver, + " replace('12345', '12', '\\\'--\\\'')"), + " replace('12345', '12', '\\\'--\\\'')"); + assertEquals(method.invoke(driver, + " replace(\"12345\", \"12\", \"\\\"--\\\"\")"), + " replace(\"12345\", \"12\", \"\\\"--\\\"\")"); + } catch (Exception e) { + fail(e.getMessage()); + } + } + private static void setEnv(String key, String value) throws Exception { if (Shell.WINDOWS) setEnvWindows(key, value);