Skip to content

GH-2341 - Fix missing DISTINCT in count query bug. #2389

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
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 @@ -146,7 +146,7 @@ public abstract class QueryUtils {
builder.append(IDENTIFIER_GROUP);
builder.append("(.*)");

COUNT_MATCH = compile(builder.toString(), CASE_INSENSITIVE);
COUNT_MATCH = compile(builder.toString(), CASE_INSENSITIVE | DOTALL);

Map<PersistentAttributeType, Class<? extends Annotation>> persistentAttributeTypes = new HashMap<>();
persistentAttributeTypes.put(ONE_TO_ONE, OneToOne.class);
Expand Down Expand Up @@ -482,7 +482,8 @@ public static String createCountQueryFor(String originalQuery, @Nullable String
boolean useVariable = StringUtils.hasText(variable) //
&& !variable.startsWith(" new") //
&& !variable.startsWith("count(") //
&& !variable.contains(","); //
&& !variable.contains(",") //
&& !variable.contains("*");

String complexCountValue = matcher.matches() && StringUtils.hasText(matcher.group(COMPLEX_COUNT_FIRST_INDEX))
? COMPLEX_COUNT_VALUE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,11 @@ void createCountQuerySupportsWhitespaceCharacters() {
" where user.age = 18\n ");
}

@Test // GH-2341
void createCountQueryStarCharacterConverted() {
assertThat(createCountQueryFor("select * from User user")).isEqualTo("select count(user) from User user");
}

@Test
void createCountQuerySupportsLineBreaksInSelectClause() {

Expand Down Expand Up @@ -511,6 +516,28 @@ void findProjectionClauseWithIncludedFrom() {
assertThat(QueryUtils.getProjection("select x, frommage, y from t")).isEqualTo("x, frommage, y");
}

@Test // GH-2341
void countProjectionDistrinctQueryIncludesNewLineAfterFromAndBeforeJoin() {
String originalQuery = "SELECT DISTINCT entity1\nFROM Entity1 entity1\nLEFT JOIN Entity2 entity2 ON entity1.key = entity2.key";

assertCountQuery(originalQuery,
"select count(DISTINCT entity1) FROM Entity1 entity1\nLEFT JOIN Entity2 entity2 ON entity1.key = entity2.key");
}

@Test
void countProjectionDistinctQueryIncludesNewLineAfterEntity() {
String originalQuery = "SELECT DISTINCT entity1\nFROM Entity1 entity1 LEFT JOIN Entity2 entity2 ON entity1.key = entity2.key";
assertCountQuery(originalQuery,
"select count(DISTINCT entity1) FROM Entity1 entity1 LEFT JOIN Entity2 entity2 ON entity1.key = entity2.key");
}

@Test
void countProjectionDistinctQueryIncludesNewLineAfterEntityAndBeforeWhere() {
String originalQuery = "SELECT DISTINCT entity1\nFROM Entity1 entity1 LEFT JOIN Entity2 entity2 ON entity1.key = entity2.key\nwhere entity1.id = 1799";
assertCountQuery(originalQuery,
"select count(DISTINCT entity1) FROM Entity1 entity1 LEFT JOIN Entity2 entity2 ON entity1.key = entity2.key\nwhere entity1.id = 1799");
}

private static void assertCountQuery(String originalQuery, String countQuery) {
assertThat(createCountQueryFor(originalQuery)).isEqualTo(countQuery);
}
Expand Down