You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@zeppelin.apache.org by mo...@apache.org on 2017/04/08 22:50:14 UTC

zeppelin git commit: [ZEPPELIN-2279] excluded comments from SQL

Repository: zeppelin
Updated Branches:
  refs/heads/master 241fd0344 -> f9830a7d6


[ZEPPELIN-2279] excluded comments from SQL

### What is this PR for?
Exclusion comments (single-, multiline) from queries before execution. Comments don't need to execute  query and sometimes there are errors.

### What type of PR is it?
Bug Fix | Improvement

### What is the Jira issue?
https://issues.apache.org/jira/browse/ZEPPELIN-2279

### How should this be tested?
```
/* ; */
select 1;
-- text select 1
/* bla
bla
bla*/
select 1; -- text
```

### Questions:
* Does the licenses files need update? no
* Is there breaking changes for older versions? no
* Does this needs documentation? no

Author: Tinkoff DWH <ti...@gmail.com>

Closes #2158 from tinkoff-dwh/ZEPPELIN-2279 and squashes the following commits:

3f7496e [Tinkoff DWH] [ZEPPELIN-2279] fix conditions, common format
f48f7d6 [Tinkoff DWH] [ZEPPELIN-2279] improve test, revert  precode execution
2cb94fa [Tinkoff DWH] Merge remote-tracking branch 'origin/master' into ZEPPELIN-2279
6db3c46 [Tinkoff DWH] [ZEPPELIN-2279] excluded comments from SQL


Project: http://git-wip-us.apache.org/repos/asf/zeppelin/repo
Commit: http://git-wip-us.apache.org/repos/asf/zeppelin/commit/f9830a7d
Tree: http://git-wip-us.apache.org/repos/asf/zeppelin/tree/f9830a7d
Diff: http://git-wip-us.apache.org/repos/asf/zeppelin/diff/f9830a7d

Branch: refs/heads/master
Commit: f9830a7d64921c1f67aeb6ee179b574769e0a6f9
Parents: 241fd03
Author: Tinkoff DWH <ti...@gmail.com>
Authored: Wed Apr 5 12:32:44 2017 +0500
Committer: Lee moon soo <mo...@apache.org>
Committed: Sun Apr 9 07:50:09 2017 +0900

----------------------------------------------------------------------
 .../apache/zeppelin/jdbc/JDBCInterpreter.java   | 46 +++++++++++++++++---
 .../zeppelin/jdbc/JDBCInterpreterTest.java      | 28 ++++++++++++
 2 files changed, 67 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/zeppelin/blob/f9830a7d/jdbc/src/main/java/org/apache/zeppelin/jdbc/JDBCInterpreter.java
----------------------------------------------------------------------
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 5bf4063..2e35e81 100644
--- a/jdbc/src/main/java/org/apache/zeppelin/jdbc/JDBCInterpreter.java
+++ b/jdbc/src/main/java/org/apache/zeppelin/jdbc/JDBCInterpreter.java
@@ -508,19 +508,36 @@ public class JDBCInterpreter extends Interpreter {
   protected ArrayList<String> splitSqlQueries(String sql) {
     ArrayList<String> 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) {
@@ -529,7 +546,8 @@ public class JDBCInterpreter extends Interpreter {
           quoteString = true;
         }
       }
-      if (character.equals('"')) {
+
+      if (character == '"') {
         if (antiSlash) {
           antiSlash = false;
         } else if (doubleQuoteString) {
@@ -539,16 +557,30 @@ public class JDBCInterpreter extends Interpreter {
         }
       }
 
-      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;
   }
 

http://git-wip-us.apache.org/repos/asf/zeppelin/blob/f9830a7d/jdbc/src/test/java/org/apache/zeppelin/jdbc/JDBCInterpreterTest.java
----------------------------------------------------------------------
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 dc0463a..04365cc 100644
--- a/jdbc/src/test/java/org/apache/zeppelin/jdbc/JDBCInterpreterTest.java
+++ b/jdbc/src/test/java/org/apache/zeppelin/jdbc/JDBCInterpreterTest.java
@@ -450,4 +450,32 @@ public class JDBCInterpreterTest extends BasicJDBCTestCaseAdapter {
     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());
+  }
 }