-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
NativeQuery with Pagination #2260
Comments
I suspect this is a bug in the underlying JPA implementation. Spring Data JPA uses Could you confirm that you seeing this behaviour as well if you use an |
Hello, I´ve tried many ways but entity manager did not worked for me cause native query has no option to pass sort into query. But after some investigation I´ve found a workaround with adding ORDER 1 at the end of query and now It is working fine. |
I'll try to tackle this issue |
The test cannot be reproduced using hsqldb - the syntax causing the bug is not supported. Closes spring-projects#2260
I have added a solution where I'm counting the number of occurrences of |
Fix alias detection bug occurring when using subqueries. Closes spring-projects#2260
I've changed the pattern to match subqueries. |
Hi, can this be worked? |
In several ways, it's possible to have various ORDER BY clauses, including SQL OVER (windowing) clauses. See #2260.
In several ways, it's possible to have various ORDER BY clauses, including SQL OVER (windowing) clauses. Need to improve handling of ORDER BY. See #2260.
In several ways, it's possible to have various ORDER BY clauses, including SQL OVER (windowing) clauses. Need to improve handling of ORDER BY. See #2260.
In several ways, it's possible to have various ORDER BY clauses, including SQL OVER (windowing) clauses. Need to improve handling of ORDER BY. See #2260.
In several ways, it's possible to have various ORDER BY clauses, including SQL OVER (windowing) clauses. Need to improve handling of ORDER BY. See #2260.
Thanks @TAndronicus! |
Hi @TAndronicus, you mentioned a bug concerning alias detection. After upgrading from Spring Boot 2.5.12 to 2.5.13 (which implicitely upgrades spring-data-jpa from 2.5.10 to 2.5.11), some of our JPA queries did not work anymore because of false aliases used in queries. The problem only seem to occur in functions using Example showing code which works with 2.5.10 of spring-data-jpa, but fails with 2.5.11:
The occurring error is as follows: As you can see in the error, the generated sql is finalized by "order by r2.name desc", but r2 is the alias used in the subquery, not the alias used for the whole query. Can you please take a look? I am unsure if we do something wrong, or if this is a new bug introduced with 2.5.11. With best regards, |
I can confirm, the change in alias detection broke quite a few things:
In my case, I have a field that ends with
|
I can confirm this behavior. We have the same error. |
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](#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. See #2260 (c93aa25), #2500, #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](#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. See #2260 (c93aa25), #2500, #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](#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. See #2260 (c93aa25), #2500, #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](#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. See #2260 (c93aa25), #2500, #2518.
@gregturn does not look like #2516 fixes this case: #2260 (comment) as there are no sub-queries yet alias detection is affected by the change in this ticket. |
fixes spring-projects#2260 Added a zero-width word boundary to the regex that identifies the from clause. This is used in alias detection for order by clauses.
Fixed on Backported to Thanks @darinmanica! |
Thanks to everyone else that helped tackle this issue with feedback and links. |
hi @gregturn, does this issue get fixed? I am using 2.7.1 but still seeing the sorting with wrong table |
I watched the same issue with 2.7.1, even if it should already be fixed |
@PAX523 please create a new issue and include a reproducer. |
@PAX523 @ronghuiye-agi There is an issue in #2563 where the alias is incorrectly detected in multi-line queries. I just submitted #2603 to address this. Does your query contain EOL characters, such as a java text block? |
Yes, that's the case! I cannot inspect the used EOL characters right now, but basically the query consists of Java's multi-line string feature: """
SELECT * FROM ...
WHERE ...
""" Thank you for working on it. Cheers PAX |
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/spring-data-jpa#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. See #2260 (c93aa25), #2500, #2518.
Hello, I am facing issue similiar with #1282.
I am trying to run this native query with spring-data on version
2.5.0
@Query(value = "SELECT dense_rank() OVER (ORDER BY rank DESC)," + "t.id, " + "FROM table t WHERE t.x > 0 ", nativeQuery = true) Page<Object[]> getT(Pageable pageable);
But I am getting SQLGrammarException because the spring-data engine will translate this query into
SELECT dense_rank() OVER (ORDER BY rank DESC),t.id FROM table t WHERE t.x > 0 , t.y desc limit 20
Where the problem is pretty obvious. There is ORDER BY clause missing, probably because of presence of ORDER BY clause in OVER subquery. Is this a known bug? And is it possible to bypass this somehow? I need to use whole pageable object with native query with this dense_rank() calling.
The text was updated successfully, but these errors were encountered: