This is an automated email from the ASF dual-hosted git repository.

shaofengshi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/kylin.git


The following commit(s) were added to refs/heads/master by this push:
     new 9c3165a  KYLIN-3620 resolve "--" is treated as comment marker between 
singe quotes
9c3165a is described below

commit 9c3165a1e10a830b48463a23aa26419986b5abfa
Author: Lijun Cao <641507...@qq.com>
AuthorDate: Sun Oct 21 15:14:03 2018 +0800

    KYLIN-3620 resolve "--" is treated as comment marker between singe quotes
---
 .../org/apache/kylin/query/util/QueryUtil.java     |  2 +-
 .../org/apache/kylin/query/util/QueryUtilTest.java | 52 +++++++++++++++++++++-
 2 files changed, 51 insertions(+), 3 deletions(-)

diff --git a/query/src/main/java/org/apache/kylin/query/util/QueryUtil.java 
b/query/src/main/java/org/apache/kylin/query/util/QueryUtil.java
index 1e7498b..5aad5e0 100644
--- a/query/src/main/java/org/apache/kylin/query/util/QueryUtil.java
+++ b/query/src/main/java/org/apache/kylin/query/util/QueryUtil.java
@@ -184,7 +184,7 @@ public class QueryUtil {
 
     public static String removeCommentInSql(String sql1) {
         // match two patterns, one is "-- comment", the other is "/* comment 
*/"
-        final String[] commentPatterns = new String[] { "--[^\r\n]*", 
"/\\*[\\s\\S]*?\\*/" };
+        final String[] commentPatterns = new String[]{"--(?!.*\\*/).*?[\r\n]", 
"/\\*(.|\r|\n)*?\\*/"};
 
         for (int i = 0; i < commentPatterns.length; i++) {
             sql1 = sql1.replaceAll(commentPatterns[i], "");
diff --git a/query/src/test/java/org/apache/kylin/query/util/QueryUtilTest.java 
b/query/src/test/java/org/apache/kylin/query/util/QueryUtilTest.java
index bf6f836..0b04e6d 100644
--- a/query/src/test/java/org/apache/kylin/query/util/QueryUtilTest.java
+++ b/query/src/test/java/org/apache/kylin/query/util/QueryUtilTest.java
@@ -169,10 +169,10 @@ public class QueryUtilTest extends 
LocalFileMetadataTestCase {
     public void testRemoveCommentInSql() {
 
         String originSql = "select count(*) from test_kylin_fact where price > 
10.0";
+        String originSql2 = "select count(*) from test_kylin_fact where 
TEST_COLUMN != 'not--a comment'";
 
         {
             String sqlWithComment = "-- comment \n" + originSql;
-
             Assert.assertEquals(originSql, 
QueryUtil.removeCommentInSql(sqlWithComment));
         }
 
@@ -223,9 +223,57 @@ public class QueryUtilTest extends 
LocalFileMetadataTestCase {
         }
 
         {
-            String sqlWithComment = "/* comment1 * \ncomment2 */ -- comment 
3\n" + originSql + "-- comment 5";
+            String sqlWithComment = "/* comment1 * \ncomment2 */ -- comment 
3\n" + originSql + "-- comment 5\n";
             Assert.assertEquals(originSql, 
QueryUtil.removeCommentInSql(sqlWithComment));
         }
+
+        {
+            String sqlWithComment2 = "/* comment1 * \ncomment2 */ -- comment 
5\n" + originSql2 + "/* comment3 / comment4 */";
+            Assert.assertEquals(originSql2, 
QueryUtil.removeCommentInSql(sqlWithComment2));
+        }
+
+        {
+            String sqlWithComment2 = "/* comment1 * comment2 */ /* comment3 / 
comment4 */ -- comment 5\n" + originSql2;
+            Assert.assertEquals(originSql2, 
QueryUtil.removeCommentInSql(sqlWithComment2));
+        }
+
+        {
+            String sqlWithComment2 = "/* comment1 * comment2 */ " + originSql2;
+            Assert.assertEquals(originSql2, 
QueryUtil.removeCommentInSql(sqlWithComment2));
+        }
+
+        {
+            String sqlWithComment2 = "/* comment1/comment2 */ " + originSql2;
+            Assert.assertEquals(originSql2, 
QueryUtil.removeCommentInSql(sqlWithComment2));
+        }
+
+        {
+            String sqlWithComment2 = "-- \n -- comment \n" + originSql2;
+            Assert.assertEquals(originSql2, 
QueryUtil.removeCommentInSql(sqlWithComment2));
+        }
+
+        {
+            String sqlWithComment2 = "-- comment \n" + originSql2;
+            Assert.assertEquals(originSql2, 
QueryUtil.removeCommentInSql(sqlWithComment2));
+        }
+
+        {
+            String sqlWithComment2 = "-- comment \n -- comment\n" + originSql2;
+            Assert.assertEquals(originSql2, 
QueryUtil.removeCommentInSql(sqlWithComment2));
+        }
+
+        String content = "        --  One-line comment and /**range\n" +
+                "/*\n" +
+                "Multi-line comment\r\n" +
+                "--  Multi-line comment*/\n" +
+                "select price as " +
+                "/*\n" +
+                "Multi-line comment\r\n" +
+                "--  Multi-line comment*/\n" +
+                "revenue from /*One-line comment-- One-line comment*/ 
v_lineitem;";
+        String expectedContent = "select price as revenue from  v_lineitem;";
+        String trimmedContent = 
QueryUtil.removeCommentInSql(content).replaceAll("\n", "").trim();
+        Assert.assertEquals(trimmedContent, expectedContent);
     }
 
     @Test

Reply via email to