Skip to content

Correctly handle order by aliases. #2516

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 1 commit into from

Conversation

darinmanica
Copy link
Contributor

@darinmanica darinmanica commented May 1, 2022

Correctly handle order by aliases.
fixes #2260 related to c93aa25
fixes #2500
fixes #2518

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.

  • [✓] You have read the Spring Data contribution guidelines.
  • [✓] You use the code formatters provided here and have them applied to your changes. Don’t submit any formatting related changes.
  • [✓] You submit test cases (unit or integration tests) that back your changes.
  • [✓] You added yourself as author in the headers of the classes you touched. Amend the date range in the Apache license header if needed. For new types, add the license header (copy from another file and set the current year only).

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.
@gregturn
Copy link
Contributor

gregturn commented May 4, 2022

Thanks @darinmanica!

@gregturn
Copy link
Contributor

gregturn commented May 4, 2022

Merged to main

@gregturn gregturn closed this May 4, 2022
@gregturn
Copy link
Contributor

gregturn commented May 4, 2022

Backported to 2.7.x., 2.6.x, and 2.5.x.

@gregturn gregturn added this to the 3.0 M4 (2022.0.0) milestone May 4, 2022
@gregturn gregturn added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels May 4, 2022
@darinmanica darinmanica deleted the issue/gh-2260 branch May 5, 2022 11:51
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

Successfully merging this pull request may close these issues.

3 participants