Skip to content
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

HQL query parser does not support all fields in extract() #3219

Closed
dfschneid opened this issue Nov 5, 2023 · 2 comments
Closed

HQL query parser does not support all fields in extract() #3219

dfschneid opened this issue Nov 5, 2023 · 2 comments
Assignees
Labels
in: query-parser Everything related to parsing JPQL or SQL type: bug A general bug

Comments

@dfschneid
Copy link

The HQL query parser does not seem to support all fields in the extract() function. According to the Hibernate user guide, fields such as day of week should be possible. However, this does not work in practice.

An example:

@Query("select extract(day of week from departureTime) AS day, sum(duration) as duration from JourneyEntity" +
        " group by extract(day of week from departureTime)")
List<Object[]> findJourneyDurationByDayOfWeek();

When running this query, I get the following stack trace, which apparently originates in the HQL parser of Spring Data JPA:

org.springframework.data.jpa.repository.query.BadJpqlGrammarException: Line 1:19 no viable alternative at input 'extract(dayof'; Bad JPQL grammar [select extract(day of week from departureTime) AS day, count(*) as count from JourneyEntity group by extract(day of week from departureTime)]
	at org.springframework.data.jpa.repository.query.BadJpqlGrammarErrorListener.syntaxError(BadJpqlGrammarErrorListener.java:39) ~[spring-data-jpa-3.1.5.jar:3.1.5]
	at org.antlr.v4.runtime.ProxyErrorListener.syntaxError(ProxyErrorListener.java:41) ~[antlr4-runtime-4.10.1.jar:4.10.1]
	at org.antlr.v4.runtime.Parser.notifyErrorListeners(Parser.java:543) ~[antlr4-runtime-4.10.1.jar:4.10.1]
	at org.antlr.v4.runtime.DefaultErrorStrategy.reportNoViableAlternative(DefaultErrorStrategy.java:310) ~[antlr4-runtime-4.10.1.jar:4.10.1]
	at org.antlr.v4.runtime.DefaultErrorStrategy.reportError(DefaultErrorStrategy.java:136) ~[antlr4-runtime-4.10.1.jar:4.10.1]
	at org.springframework.data.jpa.repository.query.HqlParser.expressionOrPredicate(HqlParser.java:9218) ~[spring-data-jpa-3.1.5.jar:3.1.5]
...

I am using Hibernate 6.2.13 and Spring Data JPA 3.1.5 (as defined by Spring Boot 3.1.5).

A quick glance at the grammars used for the Hibernate HqlParser and the Spring Data JPA HqlParser reveals a difference in the definition of extractFunction that seems to explain the behavior.

Allowing arbitrary expressions in the first argument position of the function also appears to me to be too general. Perhaps the grammar of the Hibernate HQL parser could be used for the first argument instead?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 5, 2023
@mp911de mp911de added type: bug A general bug in: query-parser Everything related to parsing JPQL or SQL and removed status: waiting-for-triage An issue we've not yet triaged labels Nov 6, 2023
@gregturn gregturn added this to the 3.2 GA (2023.1.0) milestone Nov 6, 2023
gregturn added a commit that referenced this issue Nov 6, 2023
HQL's extract function supports things like "day of week", "day of month", and "week of year". Extend support to these alternate expressions.

See #3219.
@gregturn
Copy link
Contributor

gregturn commented Nov 6, 2023

Resolved.

@gregturn gregturn closed this as completed Nov 6, 2023
gregturn added a commit that referenced this issue Nov 6, 2023
HQL's extract function supports things like "day of week", "day of month", and "week of year". Extend support to these alternate expressions.

See #3219.
@gregturn
Copy link
Contributor

gregturn commented Nov 6, 2023

Backported to 3.1.x.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: query-parser Everything related to parsing JPQL or SQL type: bug A general bug
Projects
None yet
Development

No branches or pull requests

4 participants