You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kylin.apache.org by bi...@apache.org on 2018/03/13 00:27:55 UTC
[kylin] 04/05: KYLIN-3119 Fix bugs in the function 'massageSql' of
'QueryUtil.java'
This is an automated email from the ASF dual-hosted git repository.
billyliu pushed a commit to branch 2.3.x
in repository https://gitbox.apache.org/repos/asf/kylin.git
commit 348e73d762e0eee7165c1b70984e0f8d4586eab9
Author: pengjianhua <35...@qq.com>
AuthorDate: Tue Mar 13 08:22:10 2018 +0800
KYLIN-3119 Fix bugs in the function 'massageSql' of 'QueryUtil.java'
---
.../org/apache/kylin/query/util/QueryUtil.java | 29 +++++++-----
.../org/apache/kylin/query/util/QueryUtilTest.java | 52 ++++++++++++++++++++++
2 files changed, 70 insertions(+), 11 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 4de6a2c..0552895 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 class QueryUtil {
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 class QueryUtil {
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 01c2f7f..4d766b9 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 class QueryUtilTest extends LocalFileMetadataTestCase {
"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
--
To stop receiving notification emails like this one, please contact
billyliu@apache.org.