Skip to content

HQL parser rejects HAVING without GROUP BY #3840

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
afrunt opened this issue Apr 14, 2025 · 9 comments
Closed

HQL parser rejects HAVING without GROUP BY #3840

afrunt opened this issue Apr 14, 2025 · 9 comments
Assignees
Labels
type: bug A general bug

Comments

@afrunt
Copy link

afrunt commented Apr 14, 2025

HqlParser cannot parse the valid HQL query. Here is the query

            SELECT COUNT(f)
            FROM FooEntity f
            WHERE f.name IN ('Y', 'Basic', 'Remit') 
                        AND f.size = 10
            HAVING COUNT(f) > 0

Here is the example https://github.com/afrunt/eql-bug-check
(there are separate profiles for HQL and EQL
mvn clean package -U -P hql
mvn clean package -U -P eql
)
I will add similar upcoming issues to this project if any

NOTE: EqlParser works fine with this case

Caused by: org.springframework.data.jpa.repository.query.BadJpqlGrammarException: At 5:0 and token 'HAVING', mismatched input 'HAVING', expecting one of the following tokens: , ',', '/', '||', '*', AND, BY, DAY, EPOCH, EXCEPT, GROUP, HOUR, INTERSECT, MINUTE, MONTH, NANOSECOND, OR, ORDER, QUARTER, SECOND, UNION, WEEK, YEAR, '+', '-'; Bad HQL grammar [SELECT COUNT(f)
FROM FooEntity f
WHERE f.name IN ('Y', 'Basic', 'Remit')
AND f.size = 10
HAVING COUNT(f) > 0
]
at org.springframework.data.jpa.repository.query.BadJpqlGrammarErrorListener.syntaxError(BadJpqlGrammarErrorListener.java:53) ~[spring-data-jpa-3.4.5-SNAPSHOT.jar:3.4.5-SNAPSHOT]
at org.antlr.v4.runtime.ProxyErrorListener.syntaxError(ProxyErrorListener.java:41) ~[antlr4-runtime-4.13.0.jar:4.13.0]
at org.antlr.v4.runtime.Parser.notifyErrorListeners(Parser.java:544) ~[antlr4-runtime-4.13.0.jar:4.13.0]
at org.antlr.v4.runtime.DefaultErrorStrategy.reportInputMismatch(DefaultErrorStrategy.java:327) ~[antlr4-runtime-4.13.0.jar:4.13.0]
at org.antlr.v4.runtime.DefaultErrorStrategy.reportError(DefaultErrorStrategy.java:139) ~[antlr4-runtime-4.13.0.jar:4.13.0]
at org.springframework.data.jpa.repository.query.HqlParser.start(HqlParser.java:346) ~[spring-data-jpa-3.4.5-SNAPSHOT.jar:3.4.5-SNAPSHOT]
at org.springframework.data.jpa.repository.query.JpaQueryEnhancer.parse(JpaQueryEnhancer.java:101) ~[spring-data-jpa-3.4.5-SNAPSHOT.jar:3.4.5-SNAPSHOT]
at org.springframework.data.jpa.repository.query.JpaQueryEnhancer$HqlQueryParser.(JpaQueryEnhancer.java:288) ~[spring-data-jpa-3.4.5-SNAPSHOT.jar:3.4.5-SNAPSHOT]
at org.springframework.data.jpa.repository.query.JpaQueryEnhancer$HqlQueryParser.parseQuery(JpaQueryEnhancer.java:300) ~[spring-data-jpa-3.4.5-SNAPSHOT.jar:3.4.5-SNAPSHOT]
at org.springframework.data.jpa.repository.query.JpaQueryEnhancer.forHql(JpaQueryEnhancer.java:163) ~[spring-data-jpa-3.4.5-SNAPSHOT.jar:3.4.5-SNAPSHOT]
at org.springframework.data.jpa.repository.query.QueryEnhancerFactory.forQuery(QueryEnhancerFactory.java:68) ~[spring-data-jpa-3.4.5-SNAPSHOT.jar:3.4.5-SNAPSHOT]
at org.springframework.data.jpa.repository.query.StringQuery.(StringQuery.java:98) ~[spring-data-jpa-3.4.5-SNAPSHOT.jar:3.4.5-SNAPSHOT]
at org.springframework.data.jpa.repository.query.StringQuery.(StringQuery.java:77) ~[spring-data-jpa-3.4.5-SNAPSHOT.jar:3.4.5-SNAPSHOT]
at org.springframework.data.jpa.repository.query.ExpressionBasedStringQuery.(ExpressionBasedStringQuery.java:65) ~[spring-data-jpa-3.4.5-SNAPSHOT.jar:3.4.5-SNAPSHOT]
at org.springframework.data.jpa.repository.query.AbstractStringBasedJpaQuery.(AbstractStringBasedJpaQuery.java:84) ~[spring-data-jpa-3.4.5-SNAPSHOT.jar:3.4.5-SNAPSHOT]
at org.springframework.data.jpa.repository.query.SimpleJpaQuery.(SimpleJpaQuery.java:65) ~[spring-data-jpa-3.4.5-SNAPSHOT.jar:3.4.5-SNAPSHOT]
at org.springframework.data.jpa.repository.query.JpaQueryFactory.fromMethodWithQueryString(JpaQueryFactory.java:49) ~[spring-data-jpa-3.4.5-SNAPSHOT.jar:3.4.5-SNAPSHOT]
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$DeclaredQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:174) ~[spring-data-jpa-3.4.5-SNAPSHOT.jar:3.4.5-SNAPSHOT]
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:254) ~[spring-data-jpa-3.4.5-SNAPSHOT.jar:3.4.5-SNAPSHOT]
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:99) ~[spring-data-jpa-3.4.5-SNAPSHOT.jar:3.4.5-SNAPSHOT]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lookupQuery(QueryExecutorMethodInterceptor.java:116) ~[spring-data-commons-3.4.5-SNAPSHOT.jar:3.4.5-SNAPSHOT]
... 110 common frames omitted

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 14, 2025
@mp911de mp911de self-assigned this Apr 14, 2025
@mp911de mp911de changed the title HQL parser rejects HAVING without GROUP BY EQL parser rejects HAVING without GROUP BY Apr 14, 2025
@mp911de mp911de added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Apr 14, 2025
@afrunt afrunt changed the title EQL parser rejects HAVING without GROUP BY HQL parser rejects HAVING without GROUP BY Apr 14, 2025
@afrunt
Copy link
Author

afrunt commented Apr 14, 2025

@mp911de this time it is related to HQL, not EQL

@mp911de
Copy link
Member

mp911de commented Apr 14, 2025

All right, thanks for clarifying. I noticed the same reproducer repository and assumed a typo.

@afrunt
Copy link
Author

afrunt commented Apr 14, 2025

@mp911de I reworked the project and added two maven profiles to be able to check Hql and Eql for the same queries

@mp911de
Copy link
Member

mp911de commented Apr 14, 2025

FWIW, for those parser pure-syntax issues it should be fine to just have the offending query text, you don't need to craft new tests (maybe once we encounter some issue that terminates with a Hibernate stack trace).

@afrunt
Copy link
Author

afrunt commented Apr 14, 2025

@mp911de sure, it is more for myself. For better understanding. Also to make sure, that I'm reporting a real reproducible bug

@mp911de
Copy link
Member

mp911de commented Apr 14, 2025

HAVING COUNT(f) > 0 without GROUP BY is invalid HQL (testing against the Hibernate parser). In any case, the JPA spec allows absence of GROPUP BY so we should enable it.

@afrunt
Copy link
Author

afrunt commented Apr 14, 2025

@mp911de now I see. I didn't read the stacktrace precisely and missed the fact, that exception comes from Hibernate, when I'm trying to execute the query

mp911de added a commit that referenced this issue Apr 14, 2025

Verified

This commit was signed with the committer’s verified signature.
mp911de Mark Paluch
Closes #3840
mp911de added a commit that referenced this issue Apr 14, 2025

Verified

This commit was signed with the committer’s verified signature.
mp911de Mark Paluch
Closes #3840
mp911de added a commit that referenced this issue Apr 14, 2025

Verified

This commit was signed with the committer’s verified signature.
mp911de Mark Paluch
Closes #3840
@mp911de mp911de added this to the 3.3.11 (2024.0.11) milestone Apr 14, 2025
@mp911de
Copy link
Member

mp911de commented Apr 14, 2025

That's fixed now.

@afrunt
Copy link
Author

afrunt commented Apr 14, 2025

@mp911de incredible, thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants