Skip to content

Commit 411d226

Browse files
Drop AS in count.
1 parent d3c2a9b commit 411d226

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/HqlQueryTransformer.java

+12
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.ArrayList;
2121
import java.util.Collections;
2222
import java.util.List;
23+
import java.util.stream.Collectors;
2324

2425
import org.antlr.v4.runtime.ParserRuleContext;
2526
import org.springframework.data.domain.Sort;
@@ -357,6 +358,17 @@ public List<JpaQueryParsingToken> visitSelectClause(HqlParser.SelectClauseContex
357358

358359
if (ctx.DISTINCT() != null) {
359360

361+
List<JpaQueryParsingToken> target = new ArrayList<>(selectionListTokens.size());
362+
for(int i=0;i<selectionListTokens.size();i++) {
363+
JpaQueryParsingToken token = selectionListTokens.get(i);
364+
if(token.getToken().equalsIgnoreCase("as")) {
365+
i++;
366+
continue;
367+
}
368+
target.add(token);
369+
}
370+
selectionListTokens = target;
371+
360372
if (selectionListTokens.stream().anyMatch(hqlToken -> hqlToken.getToken().contains("new"))) {
361373
// constructor
362374
tokens.add(new JpaQueryParsingToken(() -> primaryFromAlias));

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/query/HqlQueryTransformerTests.java

+12
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
* Verify that HQL queries are properly transformed through the {@link JpaQueryEnhancer} and the {@link HqlQueryParser}.
3535
*
3636
* @author Greg Turnquist
37+
* @author Christoph Strobl
3738
*/
3839
class HqlQueryTransformerTests {
3940

@@ -1031,6 +1032,17 @@ void aliasesShouldNotOverlapWithSortProperties() {
10311032
"SELECT t3 FROM Test3 t3 JOIN t3.test2 x WHERE x.id = :test2Id order by t3.testDuplicateColumnName desc");
10321033
}
10331034

1035+
@Test // GH-3269
1036+
void createsCountQueryUsingAliasCorrectly() {
1037+
1038+
assertCountQuery("select distinct 1 as x from Employee","select count(distinct 1) from Employee AS __");
1039+
assertCountQuery("SELECT DISTINCT abc AS x FROM T","SELECT count(DISTINCT abc) FROM T AS __");
1040+
assertCountQuery("select distinct a as x, b as y from Employee","select count(distinct a , b) from Employee AS __");
1041+
assertCountQuery("select distinct sum(amount) as x from Employee GROUP BY n","select count(distinct sum(amount)) from Employee AS __ GROUP BY n");
1042+
assertCountQuery("select distinct a, b, sum(amount) as c, d from Employee GROUP BY n","select count(distinct a, b, sum(amount) , d) from Employee AS __ GROUP BY n");
1043+
assertCountQuery("select distinct a, count(b) as c from Employee GROUP BY n","select count(distinct a, count(b)) from Employee AS __ GROUP BY n");
1044+
}
1045+
10341046
private void assertCountQuery(String originalQuery, String countQuery) {
10351047
assertThat(createCountQueryFor(originalQuery)).isEqualTo(countQuery);
10361048
}

0 commit comments

Comments
 (0)