Skip to content

Handle multiline subquery removal #2582

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
* @author Jędrzej Biedrzycki
* @author Darin Manica
* @author Simon Paradies
* @author Chris Fraser
*/
public abstract class QueryUtils {

Expand All @@ -103,7 +104,7 @@ public abstract class QueryUtils {
private static final Pattern ALIAS_MATCH;
private static final Pattern COUNT_MATCH;
private static final Pattern STARTS_WITH_PAREN = Pattern.compile("^\\s*\\(");
private static final Pattern PARENS_TO_REMOVE = Pattern.compile("(\\(.*\\bfrom\\b[^)]+\\))", CASE_INSENSITIVE);
private static final Pattern PARENS_TO_REMOVE = Pattern.compile("(\\(.*\\bfrom\\b[^)]+\\))", CASE_INSENSITIVE | DOTALL | MULTILINE);
private static final Pattern PROJECTION_CLAUSE = Pattern.compile("select\\s+(?:distinct\\s+)?(.+)\\s+from",
Pattern.CASE_INSENSITIVE);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
* @author Greg Turnquist
* @author Jędrzej Biedrzycki
* @author Darin Manica
* @author Chris Fraser
*/
class QueryUtilsUnitTests {

Expand Down Expand Up @@ -178,6 +179,56 @@ void testRemoveSubqueries() throws Exception {
.isEqualTo("(select u from User u where not exists ( ))");
}

@Test // GH-2581
void testRemoveMultilineSubqueries() {

assertThat(normalizeWhitespace(removeSubqueries("select u from User u\n"
+ " where not exists (\n"
+ " from User u2\n"
+ " )")))
.isEqualTo("select u from User u where not exists");
assertThat(normalizeWhitespace(removeSubqueries("(\n"
+ " select u from User u \n"
+ " where not exists (\n"
+ " from User u2\n"
+ " )\n"
+ ")")))
.isEqualTo("( select u from User u where not exists )");
assertThat(normalizeWhitespace(
removeSubqueries("select u from User u \n"
+ " where not exists (\n"
+ " from User u2 \n"
+ " where not exists (\n"
+ " from User u3\n"
+ " )\n"
+ " )")))
.isEqualTo("select u from User u where not exists");
assertThat(normalizeWhitespace(
removeSubqueries("select u from User u \n"
+ " where not exists (\n"
+ " (\n"
+ " from User u2 \n"
+ " where not exists (\n"
+ " from User u3\n"
+ " )\n"
+ " )\n"
+ " )")))
.isEqualTo("select u from User u where not exists ( )");
assertThat(normalizeWhitespace(
removeSubqueries("(\n"
+ " select u from User u \n"
+ " where not exists (\n"
+ " (\n"
+ " from User u2 \n"
+ " where not exists (\n"
+ " from User u3\n"
+ " )\n"
+ " )\n"
+ " )\n"
+ ")")))
.isEqualTo("( select u from User u where not exists ( ) )");
}

private String normalizeWhitespace(String s) {
Matcher matcher = MULTI_WHITESPACE.matcher(s);
if (matcher.find()) {
Expand Down