Skip to content

Correctly handle order by aliases. #2515

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
wants to merge 2 commits into from

Conversation

darinmanica
Copy link
Contributor

Correctly handle order by aliases.

fixes #2260 related to c93aa25

In order to correctly identify aliases in the order by clause, we cannot process the from clauses left-to-right using regular expressions. These must be removed inner-to-outer. Commit c93aa25 resulted in a bug where the subquery would be incorrectly identified as the alias, as by the following query:

from Parent p join p.children c where not c.id not in (select c2.id from Child c2)

Passing in a Sort.by("name") would result in "order by c2.name" instead of "order by p.name". Thus, it was using the alias of the inner query instead of the outer query. This comment suggests removing the content of the inner query, with the caveat of the entire query being surrounded by parenthesis. This commit does exactly that, by removing the subquery before the alias is identified. It also handles the case when the entire query is surrounded by parenthesis. Unit tests illustrate this along with several examples of removing the subquery to correctly identify the alias for the order by clause.

fixes spring-projects#2260 related to c93aa25

In order to correctly identify aliases in the order by clause, we cannot process the from clauses left-to-right using regular expressions.  These must be removed inner-to-outer.  Commit c93aa25 resulted in a bug where the subquery would be incorrectly identified as the alias, as by the following query:

```
from Parent p join p.children c where not c.id not in (select c2.id from Child c2)
```

Passing in a Sort.by("name") would result in "order by c2.name" instead of "order by p.name".  Thus, it was using the alias of the inner query instead of the outer query.  [This comment](spring-projects#2260 (comment)) suggests removing the content of the inner query, with the caveat of the entire query being surrounded by parenthesis.  This commit does exactly that, by removing the subquery before the alias is identified.  It also handles the case when the entire query is surrounded by parenthesis.  Unit tests illustrate this along with several examples of removing the subquery to correctly identify the alias for the order by clause.
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 29, 2022
@darinmanica darinmanica marked this pull request as draft April 29, 2022 21:27
@darinmanica darinmanica closed this May 1, 2022
@darinmanica darinmanica deleted the issue/gh-2260 branch May 1, 2022 12:49
@gregturn gregturn added status: duplicate A duplicate of another issue and removed status: waiting-for-triage An issue we've not yet triaged labels May 4, 2022
@gregturn
Copy link
Contributor

gregturn commented May 4, 2022

Duplicates #2516.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate A duplicate of another issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants