/** * Strip comments from a sql statement, tracking when the statement contains a string literal. * * @param statement the input string * @return a stripped statement */ public static String removeComments(String statement) { if (statement == null) { return null; } Iterator<String> iterator = Splitter.on("\n").omitEmptyStrings().split(statement).iterator(); int[] startQuote = {-1}; StringBuilder ret = new StringBuilder(statement.length()); while (iterator.hasNext()) { String lineWithComments = iterator.next(); String lineNoComments = removeComments(lineWithComments, startQuote); ret.append(lineNoComments); if (iterator.hasNext() && !lineNoComments.isEmpty()) { ret.append("\n"); } } return ret.toString(); }
/** * check that statement is unchanged after stripping */ private void assertUnchanged(String statement) { assertEquals("statement should not have been affected by stripping commnents", statement, removeComments(statement)); } }
public static ExecuteStatementOperation newExecuteStatementOperation(HiveSession parentSession, String statement, Map<String, String> confOverlay, boolean runAsync, long queryTimeout) throws HiveSQLException { String cleanStatement = HiveStringUtils.removeComments(statement); String[] tokens = cleanStatement.trim().split("\\s+"); CommandProcessor processor = null; try { processor = CommandProcessorFactory.getForHiveCommand(tokens, parentSession.getHiveConf()); } catch (SQLException e) { throw new HiveSQLException(e.getMessage(), e.getSQLState(), e); } if (processor == null) { // runAsync, queryTimeout makes sense only for a SQLOperation // Pass the original statement to SQLOperation as sql parser can remove comments by itself return new SQLOperation(parentSession, statement, confOverlay, runAsync, queryTimeout); } return new HiveCommandOperation(parentSession, cleanStatement, processor, confOverlay); } }
@Test public void testLinesEndingWithComments() { int[] escape = {-1}; assertEquals("show tables;", removeComments("show tables;",escape)); assertEquals("show tables;", removeComments("show tables; --comments",escape)); assertEquals("show tables;", removeComments("show tables; -------comments",escape)); assertEquals("show tables;", removeComments("show tables; -------comments;one;two;three;;;;",escape)); assertEquals("show", removeComments("show-- tables; -------comments",escape)); assertEquals("show", removeComments("show --tables; -------comments",escape)); assertEquals("s", removeComments("s--how --tables; -------comments",escape)); assertEquals("", removeComments("-- show tables; -------comments",escape)); assertEquals("\"show tables\"", removeComments("\"show tables\" --comments",escape)); assertEquals("\"show --comments tables\"", removeComments("\"show --comments tables\" --comments",escape)); assertEquals("\"'show --comments' tables\"", removeComments("\"'show --comments' tables\" --comments",escape)); assertEquals("'show --comments tables'", removeComments("'show --comments tables' --comments",escape)); assertEquals("'\"show --comments tables\"'", removeComments("'\"show --comments tables\"' --comments",escape)); }
@Test public void testLinesEndingWithComments() { int[] escape = {-1}; assertEquals("show tables;", removeComments("show tables;",escape)); assertEquals("show tables;", removeComments("show tables; --comments",escape)); assertEquals("show tables;", removeComments("show tables; -------comments",escape)); assertEquals("show tables;", removeComments("show tables; -------comments;one;two;three;;;;",escape)); assertEquals("show", removeComments("show-- tables; -------comments",escape)); assertEquals("show", removeComments("show --tables; -------comments",escape)); assertEquals("s", removeComments("s--how --tables; -------comments",escape)); assertEquals("", removeComments("-- show tables; -------comments",escape)); assertEquals("\"show tables\"", removeComments("\"show tables\" --comments",escape)); assertEquals("\"show --comments tables\"", removeComments("\"show --comments tables\" --comments",escape)); assertEquals("\"'show --comments' tables\"", removeComments("\"'show --comments' tables\" --comments",escape)); assertEquals("'show --comments tables'", removeComments("'show --comments tables' --comments",escape)); assertEquals("'\"show --comments tables\"'", removeComments("'\"show --comments tables\"' --comments",escape)); }
public String handleMultiLineCmd(String line) throws IOException { int[] startQuote = {-1}; line = HiveStringUtils.removeComments(line, startQuote); Character mask = (System.getProperty("jline.terminal", "").equals("jline.UnsupportedTerminal")) ? null : jline.console.ConsoleReader.NULL_MASK; break; extra = HiveStringUtils.removeComments(extra, startQuote); if (extra != null && !extra.isEmpty()) { line += "\n" + extra;
@Test public void testStripComments() throws Exception { assertNull(removeComments(null)); assertUnchanged("foo"); assertUnchanged("select 1"); assertUnchanged("insert into foo (values('-----')"); assertUnchanged("insert into foo (values('abc\n\'xyz')"); assertUnchanged("create database if not exists testDB; set hive.cli.print.current.db=true;use\ntestDB;\nuse default;drop if exists testDB;"); assertEquals("foo", removeComments("foo\n")); assertEquals("foo", removeComments("\nfoo")); assertEquals("foo", removeComments("\n\nfoo\n\n")); assertEquals("insert into foo (values('-----')", removeComments("--comment\ninsert into foo (values('-----')")); assertEquals("insert into foo (values('----''-')", removeComments("--comment\ninsert into foo (values('----''-')")); assertEquals("insert into foo (values(\"----''-\")", removeComments("--comment\ninsert into foo (values(\"----''-\")")); assertEquals("insert into foo (values(\"----\"\"-\")", removeComments("--comment\ninsert into foo (values(\"----\"\"-\")")); assertEquals("insert into foo (values('-\n--\n--')", removeComments("--comment\ninsert into foo (values('-\n--\n--')")); assertEquals("insert into foo (values('-\n--\n--')", removeComments("--comment\n\ninsert into foo (values('-\n--\n--')")); assertEquals("insert into foo (values(\"-\n--\n--\")", removeComments("--comment\n\ninsert into foo (values(\"-\n--\n--\")")); assertEquals("insert into foo (values(\"-\n--\n--\")", removeComments("\n\n--comment\n\ninsert into foo (values(\"-\n--\n--\")\n\n")); assertEquals("insert into foo (values('abc');\ninsert into foo (values('def');", removeComments( "insert into foo (values('abc');\n--comment\ninsert into foo (values('def');")); }
String cmd_trimmed = HiveStringUtils.removeComments(cmd).trim(); String[] tokens = tokenizeCmd(cmd_trimmed); int ret = 0;
/** * Strip comments from a sql statement, tracking when the statement contains a string literal. * * @param statement the input string * @return a stripped statement */ public static String removeComments(String statement) { if (statement == null) { return null; } Iterator<String> iterator = Splitter.on("\n").omitEmptyStrings().split(statement).iterator(); int[] startQuote = {-1}; StringBuilder ret = new StringBuilder(statement.length()); while (iterator.hasNext()) { String lineWithComments = iterator.next(); String lineNoComments = removeComments(lineWithComments, startQuote); ret.append(lineNoComments); if (iterator.hasNext() && !lineNoComments.isEmpty()) { ret.append("\n"); } } return ret.toString(); }
public static ExecuteStatementOperation newExecuteStatementOperation(HiveSession parentSession, String statement, Map<String, String> confOverlay, boolean runAsync, long queryTimeout) throws HiveSQLException { String cleanStatement = HiveStringUtils.removeComments(statement); String[] tokens = cleanStatement.trim().split("\\s+"); CommandProcessor processor = null; try { processor = CommandProcessorFactory.getForHiveCommand(tokens, parentSession.getHiveConf()); } catch (SQLException e) { throw new HiveSQLException(e.getMessage(), e.getSQLState(), e); } if (processor == null) { // runAsync, queryTimeout makes sense only for a SQLOperation // Pass the original statement to SQLOperation as sql parser can remove comments by itself return new SQLOperation(parentSession, statement, confOverlay, runAsync, queryTimeout); } return new HiveCommandOperation(parentSession, cleanStatement, processor, confOverlay); } }
public String handleMultiLineCmd(String line) throws IOException { int[] startQuote = {-1}; line = HiveStringUtils.removeComments(line, startQuote); Character mask = (System.getProperty("jline.terminal", "").equals("jline.UnsupportedTerminal")) ? null : jline.console.ConsoleReader.NULL_MASK; break; extra = HiveStringUtils.removeComments(extra, startQuote); if (extra != null && !extra.isEmpty()) { line += "\n" + extra;
String cmd_trimmed = HiveStringUtils.removeComments(cmd).trim(); String[] tokens = tokenizeCmd(cmd_trimmed); int ret = 0;