Skip to content

Handle newlines in subquery detection #2557

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 Michał Pachucki
*/
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);
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 Michał Pachucki
*/
class QueryUtilsUnitTests {

Expand Down Expand Up @@ -710,6 +711,15 @@ void applySortingAccountsForNativeWindowFunction() {
"select * from (select * from user order by 1, 2, 3 desc limit 10) u order by u.active asc, age desc");
}

@Test
void applySortingAccountsForNewlinesInSubselect() {

Sort sort = Sort.by(Order.desc("age"));

assertThat(QueryUtils.applySorting("select u\nfrom user u\nwhere exists (select u2\nfrom user u2\n)\n", sort))
.isEqualTo("select u\nfrom user u\nwhere exists (select u2\nfrom user u2\n)\n order by u.age desc");
}

@Test // GH-2511
void countQueryUsesCorrectVariable() {

Expand Down