-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Sorting doesn't work when using an alias on two or more functions #2322
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
Comments
I drafted this test case to check things out... @Test
void foo() {
String query = "SELECT MIN(bundle.base_price_amount) as cheapestBundlePrice, MIN(DATE(bundle.start)) as earliestBundleStart from example_table";
Sort sort = Sort.by("earliestBundleStart");
String fullQuery = applySorting(query, sort);
assertThat(fullQuery).isEqualTo(
"SELECT MIN(bundle.base_price_amount) as cheapestBundlePrice, MIN(DATE(bundle.start)) as earliestBundleStart from example_table order by earliestBundleStart asc");
} Funny thing is, it passed with no issue. But I put a breakpoint inside I tweaked But of course we know that this pattern is insufficient based on your hint of an AST instead of regex solution. It would indeed capture such patterns, but it won't ensure a proper grammar. Regex isn't qualified to handle stacking of symbols. And that's where I'm not yet sold on needing an AST with a lexical analyzer and a proper grammar. I have submitted #2399 to properly handle ignoring case on aliased fields. And it doesn't involve digging into the regex patterns. I'd frankly prefer solving as many of these aliasing issues as possible before undertaking a complete rewrite based on grammar parsing. Not sure how much bang we'd get for spending such a large buck. Let alone the new level of maintenance for anyone having to maintain it in the future. |
Thank you for looking into this. I tried your test case and indeed it passes. It seems the problem only appears on a slightly more complicated case:
In this case, |
In spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java Lines 265 to 271 in 8c4123f
earliestBundleStart is not in selectionAliases . That in turn seems to mess with the logic in getOrderClause , which decides to prepend the root alias (in this case event ) or not (based on some logic that I don't fully understand yet).
|
You want to revisit this in light of 1336809. |
Thanks for the pointer. I tried with the latest QueryUtils from that commit, unfortunately, it did not change anything about the test case posted above. |
Hi everyone, SELECT
t1.*,
t2join.t2_count AS t2Count,
t3join.t3_count AS t3Count
FROM table1 AS t1
LEFT JOIN (
SELECT t2.column2, count(*) AS t2_count
FROM table2 t2
GROUP BY t2.column2
) AS t2join ON t1.id = t2join.column2
LEFT JOIN (
SELECT t3.column3, count(*) AS t3_count
FROM table3 t3
GROUP BY t3.column3
) AS t3join ON t1.id = t3join.column3
WHERE t1.column1 = :value We call this with a Pageable which contains a It would be great if this could be fixed for the next release. |
No news that I know of. If you're constructing the Pageable manually, you can try this as a workaround: |
@darioseidl thanks for pointing out |
Ah, yes, that name doesn't sound too comforting, does it? What it means is that
(as it says here) As long as you hard-code the string and make sure that it is just a property or alias, it is still safe. It would be unsafe to accept user input and pass it to So |
it looks like there is a missing test case in commit mentioned by @gregturn , in my case i have such SQL query: and if I add sort by id: The commit mentioned by @gregturn does not cover a select in where nor a select in join. The issue happens after updating to spring boot 2.7.0 |
Ok I see, the QueryUtils.STARTS_WITH_PAREN does not handle newlines in subselects. I'll try to make a MR for that.
|
We are working on a parsing solution to better handle such situations than |
This looks like a candidate for #2863. |
If you check 39e12ea, you should see that aliasing is now properly handled in sorts, and that the query at the top is included as a test case. Feel free to check out Spring Data JPA |
I'm closing this as being resolved by the commit in the previous message. If this problem persists, feel free to open a new ticket. |
As per the comments on this closed issue, I'm opening a new issue, because sorting still doesn't work for aliases in all cases.
The regex patterns in
org.springframework.data.jpa.repository.query.QueryUtils
are not broad enough.There was also a question on SO about this here, where many people state that they are facing the same issue.
There are and have been several issues (at least #1404, #1724, #1919) with the regex patterns in QueryUtils. I'm wondering, isn't there any more reliable way to parse the queries than with a regex (possibly an AST that is used also to execute the query)?
Edit: here's another closely related issue: #2079
The text was updated successfully, but these errors were encountered: