Skip to content

Commit

Permalink
minor, refine sql comment removing
Browse files Browse the repository at this point in the history
  • Loading branch information
rogercloud authored and liyang-kylin committed Jan 26, 2018
1 parent 60caf61 commit 7d5fb85
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
17 changes: 4 additions & 13 deletions query/src/main/java/org/apache/kylin/query/util/QueryUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,23 +142,14 @@ public static boolean isSelectStatement(String sql) {

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]", "/\\*.*?\\*/" };

for (int i = 0; i < commentPatterns.length; i++) {
String commentPattern = commentPatterns[i];
Pattern pattern = Pattern.compile(commentPattern);
Matcher matcher = pattern.matcher(sql1);

while (matcher.find()) {
if (matcher.start() == 0) {
sql1 = sql1.substring(matcher.end()).trim();
} else {
sql1 = (sql1.substring(0, matcher.start()) + sql1.substring(matcher.end())).trim();
}
matcher = pattern.matcher(sql1);
}
sql1 = sql1.replaceAll(commentPatterns[i], "");
}

sql1 = sql1.trim();

return sql1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,23 @@ public void testRemoveCommentInSql() {
}

{
String sqlWithComment = "/* comment \n select count(*) from kylin_sales; \n */ " + originSql;
String sqlWithComment = "/* comment1/comment2 */ " + originSql;
Assert.assertEquals(originSql, QueryUtil.removeCommentInSql(sqlWithComment));
}

{
String sqlWithComment = "/* comment1 * comment2 */ " + originSql;
Assert.assertEquals(originSql, QueryUtil.removeCommentInSql(sqlWithComment));
}

{
String sqlWithComment = "/* comment1 * comment2 */ /* comment3 / comment4 */ -- comment 5\n" + originSql;
Assert.assertEquals(originSql, QueryUtil.removeCommentInSql(sqlWithComment));
}

{
String sqlWithComment = "/* comment1 * comment2 */ -- comment 5\n" + originSql + "/* comment3 / comment4 */";
Assert.assertEquals(originSql, QueryUtil.removeCommentInSql(sqlWithComment));
}
}
}

0 comments on commit 7d5fb85

Please sign in to comment.