Skip to content

Remove 'fetch' part in 'join fetch' clause during generation of count query #2686

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 @@ -87,6 +87,7 @@ public abstract class QueryUtils {
private static final String SIMPLE_COUNT_VALUE = "$2";
private static final String COMPLEX_COUNT_VALUE = "$3 $6";
private static final String COMPLEX_COUNT_LAST_VALUE = "$6";
private static final String FETCH_COMING_AFTER_JOIN_PART = "(?iu)(?<=join)(\\s*fetch\\s*)";
private static final String ORDER_BY_PART = "(?iu)\\s+order\\s+by\\s+.*";

private static final Pattern ALIAS_MATCH;
Expand Down Expand Up @@ -601,6 +602,8 @@ public static String createCountQueryFor(String originalQuery, @Nullable String
countQuery = matcher.replaceFirst(String.format(COUNT_REPLACEMENT_TEMPLATE, countProjection));
}

countQuery = countQuery.replaceAll(FETCH_COMING_AFTER_JOIN_PART, " ");

return countQuery.replaceFirst(ORDER_BY_PART, "");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
* @author Grégoire Druant
* @author Mohammad Hewedy
* @author Greg Turnquist
* @author Vladislav Yukharin
*/
class DefaultQueryUtilsUnitTests {

Expand Down Expand Up @@ -224,6 +225,13 @@ void doesNotPrefixSortsIfFunction() {
.isThrownBy(() -> applySorting("select p from Person p", sort, "p"));
}

@Test // GH-2348
void removesFetchPartInJoinFetchClauseInGeneratedCountQueryIfPresent() {

assertCountQuery("select u from User u left outer join fetch u.roles r left outer JOIN FETCH u.accounts a",
"select count(u) from User u left outer join u.roles r left outer JOIN u.accounts a");
}

@Test // DATAJPA-377
void removesOrderByInGeneratedCountQueryFromOriginalQueryIfPresent() {

Expand Down