kylin-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] yiming187 closed pull request #107: kylin-3119
Date Tue, 13 Mar 2018 00:22:11 GMT
yiming187 closed pull request #107: kylin-3119
URL: https://github.com/apache/kylin/pull/107
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

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 4de6a2c337..055289578a 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
@@ -50,22 +50,29 @@ public static String massageSql(String sql, String project, int limit,
int offse
         ProjectManager projectManager = ProjectManager.getInstance(KylinConfig.getInstanceFromEnv());
         ProjectInstance projectInstance = projectManager.getProject(project);
         KylinConfig kylinConfig = projectInstance.getConfig();
-
+        sql = removeCommentInSql(sql);
         while (sql.endsWith(";"))
             sql = sql.substring(0, sql.length() - 1);
 
-        if (limit > 0 && !sql.toLowerCase().contains("limit")) {
-            sql += ("\nLIMIT " + limit);
-        }
+        String sql1=sql;
+        final String suffixPattern = "^.+?\\s(limit\\s\\d+)?\\s(offset\\s\\d+)?\\s*$";
+        sql = sql.replaceAll("\\s+", " ");
+        Pattern pattern = Pattern.compile(suffixPattern);
+        Matcher matcher = pattern.matcher(sql.toLowerCase() + "  ");
 
-        if (offset > 0 && !sql.toLowerCase().contains("offset")) {
-            sql += ("\nOFFSET " + offset);
+        if (matcher.find()) {
+            if (limit > 0 && matcher.group(1) == null) {
+                sql1 += ("\nLIMIT " + limit);
+            }
+            if (offset > 0 && matcher.group(2) == null) {
+                sql1 += ("\nOFFSET " + offset);
+            }
         }
 
         // https://issues.apache.org/jira/browse/KYLIN-2649
-        if (kylinConfig.getForceLimit() > 0 && !sql.toLowerCase().contains("limit")
-                && sql.toLowerCase().matches("^select\\s+\\*\\p{all}*")) {
-            sql += ("\nLIMIT " + kylinConfig.getForceLimit());
+        if (kylinConfig.getForceLimit() > 0 && limit <= 0 && matcher.group(1)
== null
+                && sql1.toLowerCase().matches("^select\\s+\\*\\p{all}*")) {
+            sql1 += ("\nLIMIT " + kylinConfig.getForceLimit());
         }
 
         // customizable SQL transformation
@@ -73,9 +80,9 @@ public static String massageSql(String sql, String project, int limit, int
offse
             initQueryTransformers();
         }
         for (IQueryTransformer t : queryTransformers) {
-            sql = t.transform(sql, project, defaultSchema);
+            sql1 = t.transform(sql1, project, defaultSchema);
         }
-        return sql;
+        return sql1;
     }
 
     private static void initQueryTransformers() {
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 01c2f7f113..4d766b9e33 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
@@ -51,6 +51,58 @@ public void testMassageSql() {
                     "select ( date '2001-09-28' + interval '2' month) from test_kylin_fact
group by ( date '2001-09-28' + interval '2' month)",
                     s);
         }
+        {
+            String sql = "select count(*) test_limit from test_kylin_fact where price >
10.0";
+            String s = QueryUtil.massageSql(sql, "default", 50000, 0, "DEFAULT");
+            Assert.assertEquals(
+                    "select count(*) test_limit from test_kylin_fact where price > 10.0\n"
+
+                            "LIMIT 50000",
+                    s);
+        }
+        {
+            String sql = "select count(*) test_offset from test_kylin_fact where price >
10.0";
+            String s = QueryUtil.massageSql(sql, "default", 0, 50, "DEFAULT");
+            Assert.assertEquals(
+                    "select count(*) test_offset from test_kylin_fact where price > 10.0\n"
+
+                            "OFFSET 50",
+                    s);
+        }
+        {
+            String sql = "select count(*) test_limit_and_offset from test_kylin_fact where
price > 10.0";
+            String s = QueryUtil.massageSql(sql, "default", 50000, 50, "DEFAULT");
+            Assert.assertEquals(
+                    "select count(*) test_limit_and_offset from test_kylin_fact where price
> 10.0\n" +
+                            "LIMIT 50000\nOFFSET 50",
+                    s);
+        }
+
+        {
+            String newLine = System.getProperty("line.separator");
+            String sql = "select count(*)     test_limit from " + newLine + "test_kylin_fact
where price > 10.0";
+            newLine = newLine.replace("\r", " ").replace("\n", newLine);
+            String s = QueryUtil.massageSql(sql, "default", 50000, 0, "DEFAULT");
+            Assert.assertEquals(
+                    "select count(*)     test_limit from " + newLine + "test_kylin_fact where
price > 10.0\nLIMIT 50000",
+                    s);
+        }
+        {
+            String newLine = System.getProperty("line.separator");
+            String sql = "select count(*)     test_offset from " + newLine + "test_kylin_fact
where price > 10.0";
+            newLine = newLine.replace("\r", " ").replace("\n", newLine);
+            String s = QueryUtil.massageSql(sql, "default", 50000, 0, "DEFAULT");
+            Assert.assertEquals(
+                    "select count(*)     test_offset from " + newLine + "test_kylin_fact
where price > 10.0\nLIMIT 50000",
+                    s);
+        }
+        {
+            String newLine = System.getProperty("line.separator");
+            String sql = "select count(*)     test_limit_and_offset from " + newLine + "test_kylin_fact
where price > 10.0";
+            newLine = newLine.replace("\r", " ").replace("\n", newLine);
+            String s = QueryUtil.massageSql(sql, "default", 50000, 0, "DEFAULT");
+            Assert.assertEquals(
+                    "select count(*)     test_limit_and_offset from " + newLine + "test_kylin_fact
where price > 10.0\nLIMIT 50000",
+                    s);
+        }
     }
 
     @Test


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message