Skip to content

HQL parser rejects HAVING without GROUP BY #3840

Closed

Description

@afrunt

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

Activity

  1. self-assigned this
    on Apr 14, 2025
  2. changed the title [-]HQL parser rejects HAVING without GROUP BY[/-] [+]EQL parser rejects HAVING without GROUP BY[/+] on Apr 14, 2025
  3. changed the title [-]EQL parser rejects HAVING without GROUP BY[/-] [+]HQL parser rejects HAVING without GROUP BY[/+] on Apr 14, 2025
  4. afrunt commented on Apr 14, 2025

    @afrunt
    Author

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

  5. mp911de commented on Apr 14, 2025

    @mp911de
    Member

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

  6. afrunt commented on Apr 14, 2025

    @afrunt
    Author

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

  7. mp911de commented on Apr 14, 2025

    @mp911de
    Member

    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).

  8. afrunt commented on Apr 14, 2025

    @afrunt
    Author

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

  9. mp911de commented on Apr 14, 2025

    @mp911de
    Member

    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.

  10. afrunt commented on Apr 14, 2025

    @afrunt
    Author

    @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

  11. added 2 commits that reference this issue on Apr 14, 2025
    fd914b7
    c239f73
  12. added a commit that references this issue on Apr 14, 2025
    3f0bd62
  13. added this to the 3.3.11 (2024.0.11) milestone on Apr 14, 2025
  14. mp911de commented on Apr 14, 2025

    @mp911de
    Member

    That's fixed now.

  15. afrunt commented on Apr 14, 2025

    @afrunt
    Author

    @mp911de incredible, thank you

  16. added a commit that references this issue on Apr 21, 2025
    a62996f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions