diff --git a/jdbc/src/main/java/org/apache/zeppelin/jdbc/JDBCInterpreter.java b/jdbc/src/main/java/org/apache/zeppelin/jdbc/JDBCInterpreter.java index 1080c2bb7cd..a00c5458cf9 100644 --- a/jdbc/src/main/java/org/apache/zeppelin/jdbc/JDBCInterpreter.java +++ b/jdbc/src/main/java/org/apache/zeppelin/jdbc/JDBCInterpreter.java @@ -506,19 +506,36 @@ private boolean isDDLCommand(int updatedCount, int columnCount) throws SQLExcept protected ArrayList splitSqlQueries(String sql) { ArrayList queries = new ArrayList<>(); StringBuilder query = new StringBuilder(); - Character character; + char character; Boolean antiSlash = false; + Boolean multiLineComment = false; + Boolean singleLineComment = false; Boolean quoteString = false; Boolean doubleQuoteString = false; for (int item = 0; item < sql.length(); item++) { character = sql.charAt(item); - if (character.equals('\\')) { + if ((singleLineComment && (character == '\n' || item == sql.length() - 1)) + || (multiLineComment && character == '/' && sql.charAt(item - 1) == '*')) { + singleLineComment = false; + multiLineComment = false; + if (item == sql.length() - 1 && query.length() > 0) { + queries.add(StringUtils.trim(query.toString())); + } + continue; + } + + if (singleLineComment || multiLineComment) { + continue; + } + + if (character == '\\') { antiSlash = true; } - if (character.equals('\'')) { + + if (character == '\'') { if (antiSlash) { antiSlash = false; } else if (quoteString) { @@ -527,7 +544,8 @@ protected ArrayList splitSqlQueries(String sql) { quoteString = true; } } - if (character.equals('"')) { + + if (character == '"') { if (antiSlash) { antiSlash = false; } else if (doubleQuoteString) { @@ -537,16 +555,30 @@ protected ArrayList splitSqlQueries(String sql) { } } - if (character.equals(';') && !antiSlash && !quoteString && !doubleQuoteString) { - queries.add(query.toString()); + if (!quoteString && !doubleQuoteString && !multiLineComment && !singleLineComment + && sql.length() > item + 1) { + if (character == '-' && sql.charAt(item + 1) == '-') { + singleLineComment = true; + continue; + } + + if (character == '/' && sql.charAt(item + 1) == '*') { + multiLineComment = true; + continue; + } + } + + if (character == ';' && !antiSlash && !quoteString && !doubleQuoteString) { + queries.add(StringUtils.trim(query.toString())); query = new StringBuilder(); } else if (item == sql.length() - 1) { query.append(character); - queries.add(query.toString()); + queries.add(StringUtils.trim(query.toString())); } else { query.append(character); } } + return queries; } diff --git a/jdbc/src/test/java/org/apache/zeppelin/jdbc/JDBCInterpreterTest.java b/jdbc/src/test/java/org/apache/zeppelin/jdbc/JDBCInterpreterTest.java index 2e7e1a5116e..39c16ac4eee 100644 --- a/jdbc/src/test/java/org/apache/zeppelin/jdbc/JDBCInterpreterTest.java +++ b/jdbc/src/test/java/org/apache/zeppelin/jdbc/JDBCInterpreterTest.java @@ -448,4 +448,32 @@ public void testPrecodeWithAnotherPrefix() throws SQLException, IOException { assertEquals(InterpreterResult.Type.TABLE, interpreterResult.message().get(0).getType()); assertEquals("@TESTVARIABLE\n2\n", interpreterResult.message().get(0).getData()); } + + @Test + public void testExcludingComments() throws SQLException, IOException { + Properties properties = new Properties(); + properties.setProperty("common.max_count", "1000"); + properties.setProperty("common.max_retry", "3"); + properties.setProperty("default.driver", "org.h2.Driver"); + properties.setProperty("default.url", getJdbcConnection()); + properties.setProperty("default.user", ""); + properties.setProperty("default.password", ""); + JDBCInterpreter t = new JDBCInterpreter(properties); + t.open(); + + String sqlQuery = "/* ; */\n" + + "-- /* comment\n" + + "--select * from test_table\n" + + "select * from test_table; /* some comment ; */\n" + + "/*\n" + + "select * from test_table;\n" + + "*/\n" + + "-- a ; b\n" + + "select * from test_table WHERE ID = ';--';\n" + + "select * from test_table WHERE ID = '/*' -- test"; + + InterpreterResult interpreterResult = t.interpret(sqlQuery, interpreterContext); + assertEquals(InterpreterResult.Code.SUCCESS, interpreterResult.code()); + assertEquals(3, interpreterResult.message().size()); + } }