Skip to content

HQL query "where exists (subquery)" fails #2864

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
gregturn opened this issue Mar 15, 2023 · 3 comments
Closed

HQL query "where exists (subquery)" fails #2864

gregturn opened this issue Mar 15, 2023 · 3 comments
Assignees
Labels
in: query-parser Everything related to parsing JPQL or SQL type: bug A general bug

Comments

@gregturn
Copy link
Contributor

The following query:

select u 
from UserAccountEntity u
join fetch u.lossInspectorLimitConfiguration lil
join fetch u.companyTeam ct
where exists (
	select iu 
	from UserAccountEntity  iu
	join iu.roles u2r
	join u2r.role r
	join r.rights r2r
	join r2r.right rt
	where 
		rt.code = :rightCode
		and iu = u   
)
and ct.id = :teamId

...while valid according to the official HQL parser, doesn't pass validation with Spring Data JPA.

Screen Shot 2023-03-15 at 10 54 59 AM

@gregturn gregturn added type: bug A general bug in: query-parser Everything related to parsing JPQL or SQL labels Mar 15, 2023
@gregturn
Copy link
Contributor Author

Related: #2322 (comment)

@gregturn
Copy link
Contributor Author

Problem appears to be that RIGHT is commented out as a reserved word. (https://github.com/spring-projects/spring-data-jpa/blob/main/spring-data-jpa/src/main/antlr4/org/springframework/data/jpa/repository/query/Hql.g4#L776)

Need to uncomment it out so that it will roll up as an identifier and let the rest of the query parse.

gregturn added a commit that referenced this issue Mar 20, 2023
… parser.

RIGHT, LEFT, OUTER, INNER, and FULL are HQL tokens. This means they also need to be recognized as potential reserved words and thus possibly identifiers. This can show up if someone, for example, uses "right" as the name of a relationship in a JPA entity.

Resolves #2864.
gregturn added a commit that referenced this issue Mar 20, 2023
RIGHT, LEFT, OUTER, INNER, and FULL are HQL tokens. This means they also need to be recognized as potential reserved words and thus possibly identifiers. This can show up if someone, for example, uses "right" as the name of a relationship in a JPA entity.

Resolves #2864.
@gregturn
Copy link
Contributor Author

gregturn commented Mar 20, 2023

A query like this:

select u
from UserAccountEntity u
join fetch u.lossInspectorLimitConfiguration lil
join fetch u.companyTeam ct
where exists (
	select iu
	from UserAccountEntity  iu
	join iu.roles u2r
	join u2r.role r
	join r.rights r2r
	join r2r.right rt
	where
		rt.code = :rightCode
		and iu = u
)
and ct.id = :teamId

...will fail because RIGHT is recognized as a token but NOT as a potential identifier. Thus is breaks the query. By including it in the list of reserved words, which are then rolled up under identifier, it's possible for this query to work correctly.

Consequently, the other reserved words, which were originally commented out as well, should ALSO be uncommented and tested as well.

@gregturn gregturn self-assigned this Mar 20, 2023
gregturn added a commit that referenced this issue Mar 22, 2023
RIGHT, LEFT, OUTER, INNER, and FULL are HQL tokens. This means they also need to be recognized as potential reserved words and thus possibly identifiers. This can show up if someone, for example, uses "right" as the name of a relationship in a JPA entity.

Resolves #2864.
mp911de added a commit that referenced this issue Mar 28, 2023
Use textblocks where possible.

See #2864.
Original pull request: #2874.
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

Successfully merging a pull request may close this issue.

2 participants