Skip to content

Commit

Permalink
Use primary table alias for SQL count query derivation with DISTINCT …
Browse files Browse the repository at this point in the history
…queries.

We now use render COUNT(DISTINCT a.*) where 'a' is the primary table alias instead of COUNT(DISTINCT *).

Closes #3707
  • Loading branch information
mp911de committed Dec 10, 2024
1 parent 3cc84e8 commit 9f7c591
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
* @author Geoffrey Deremetz
* @author Yanming Zhou
* @author Christoph Strobl
* @author Mark Paluch
* @since 2.7.0
*/
public class JSqlParserQueryEnhancer implements QueryEnhancer {
Expand Down Expand Up @@ -119,7 +120,7 @@ public String applySorting(Sort sort, @Nullable String alias) {

Select selectStatement = parseSelectStatement(queryString);

if (selectStatement instanceof SetOperationList setOperationList) {
if (selectStatement instanceof SetOperationList setOperationList) {
return applySortingToSetOperationList(setOperationList, sort);
}

Expand Down Expand Up @@ -217,7 +218,7 @@ private Set<String> getJoinAliases(String query) {
}

Select selectStatement = (Select) statement;
if (selectStatement instanceof PlainSelect selectBody) {
if (selectStatement instanceof PlainSelect selectBody) {
return getJoinAliases(selectBody);
}

Expand Down Expand Up @@ -315,7 +316,7 @@ private String detectAlias(String query) {
* ValuesStatement has no alias
* SetOperation can have multiple alias for each operation item
*/
if (!(selectStatement instanceof PlainSelect selectBody)) {
if (!(selectStatement instanceof PlainSelect selectBody)) {
return null;
}

Expand Down Expand Up @@ -370,7 +371,7 @@ public String createCountQueryFor(@Nullable String countProjection) {
/*
We only support count queries for {@link PlainSelect}.
*/
if (!(selectStatement instanceof PlainSelect selectBody)) {
if (!(selectStatement instanceof PlainSelect selectBody)) {
return this.query.getQueryString();
}

Expand Down Expand Up @@ -413,7 +414,7 @@ public String getProjection() {

Select selectBody = selectStatement;

if (selectStatement instanceof SetOperationList setOperationList) {
if (selectStatement instanceof SetOperationList setOperationList) {

// using the first one since for setoperations the projection has to be the same
selectBody = setOperationList.getSelects().get(0);
Expand Down Expand Up @@ -491,7 +492,12 @@ private String countPropertyNameForSelection(List<SelectItem<?>> selectItems, bo
return column.getFullyQualifiedName();
}

return query.isNativeQuery() ? (distinct ? "*" : "1") : tableAlias == null ? "*" : tableAlias;
// TODO: We should not handle JPQL here...
if (!query.isNativeQuery()) {
return tableAlias == null ? "*" : tableAlias;
}

return distinct ? ((tableAlias != null ? tableAlias + "." : "") + "*") : "1";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ void shouldApplySorting() {
assertThat(sql).isEqualTo("SELECT e FROM Employee e ORDER BY e.foo ASC, e.bar ASC");
}

@Test // GH-3707
void countQueriesShouldConsiderPrimaryTableAlias() {

QueryEnhancer enhancer = createQueryEnhancer(DeclaredQuery.of("""
SELECT DISTINCT a.*, b.b1
FROM TableA a
JOIN TableB b ON a.b = b.b
LEFT JOIN TableC c ON b.c = c.c
ORDER BY b.b1, a.a1, a.a2
""", true));

String sql = enhancer.createCountQueryFor();

assertThat(sql).startsWith("SELECT count(DISTINCT a.*) FROM TableA a");
}

@Override
@ParameterizedTest // GH-2773
@MethodSource("jpqlCountQueries")
Expand Down

0 comments on commit 9f7c591

Please sign in to comment.