-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Sorting doesn't work for field alias [DATAJPA-1061] #1404
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
Oliver Drotbohm commented Would you mind adding the complete stack trace? I suspect it's rather a persistence provider specific issue that might not allow aliases for properties that are not backed by a function expression. Generally speaking, sorting by alias is something you should use only as a last resort as it's absolutely not clear to clients where that |
Sergey Skryabin commented Log in attachment. Set<String> aliases = getOuterJoinAliases(query);
Set<String> functionAliases = getFunctionAliases(query);
for (Order order : sort) {
builder.append(getOrderClause(aliases, functionAliases, alias, order)).append(", ");
} Set<String> fieldAliases = getFieldAliases(query); is needed. The real test case is we have complex |
Grégoire Druant commented Hi, I have provided a pull request for this bug, as I needed a fix for the project I am working on. Here it is : #276 Regards |
Dario Seidl commented We are facing the same problem. I'm pretty sure that the other to comments are correct and this is a bug in the detection of the aliases in QueryUtils. As the code snippet from Sergey Skryabin shows, only join aliases and function aliases are detected. Furthermore the regex for the function aliases only works for exactly one pair of parenthesis. For example, we have a query with the following select statement: SELECT DISTINCT(event.id) as id,
event.name as name,
MIN(bundle.base_price_amount) as cheapestBundlePrice,
MIN(DATE(bundle.start)) as earliestBundleStart
FROM event event
LEFT JOIN bundle bundle ON event.id = bundle.event_id
... In this query, only The It would be great if GregDrt's pull request could be merged or reviewed. I believe it would fix the detection of aliases for fields, but not the detection of aliases for two or more functions |
Luca Garofalo commented Hello, we have a situation were for a native query the sort is not applied correctly, i'll try to explain below.
QueryUtils.applySorting is called also for Native SQL Query in Spring Data JPA. In native SQL there are many situation were the method applySorting fails.
SELECT
T1.ID aS C1,
T1.TYPE As C2,
T2.COL1 as C3,
SS.COL2 AS C4,
listagg(T1.CODE , ',') within group (order by T1.ID) as C5,
sum(T1.CNUM) as C6,
FROM TABLE1S S
JOIN TABLE2 T2 on T1.ID = T2.ID
JOIN TABLESS SS on SS.ID = T1.ID
GROUP BY T1.ID, T1.TYPE, T2.COL1, SS.COL2
Possible solution for that is to change the ORDER_BY Pattern regex in the QueryUtils.java file so that it doens't considere 'order by' written inside round brackets (this solve also the case where are nested query with sorting applied, if for any reason there should be a need to do so) :
private static final Pattern ORDER_BY = Pattern.compile(".*[^\\(]\\s+order\\s+by\\s+.*[^\\)].*", CASE_INSENSITIVE);
Moreover for the same query, the method QueryUtils.getFieldAliases extracts the following aliases: w[C3, C4, C5, C6, JOIN, C1, C2, on] when it sould be [C3, C4, C5, C6, C1, C2] To fix this you can change the FIELD_ALIAS_PATTERN regex ** like this:
builder = new StringBuilder();
builder.append("\\s[aA][sS]+\\s+(([\\w\\.]+))");
FIELD_ALIAS_PATTERN = compile(builder.toString()); I tryed all the QueryUtils Unit Tests and they run succesfully, should I provide a pull request for this? Thank you
|
Jens Schauder commented Luca Garofalo I think what you describe is DATAJPA-1406. If not and you don't find any other open issue describing your problem please create a new issue. |
Dario Seidl commented I have tried it and I can confirm now that the detection of aliases for fields is fixed, but not the detection of aliases for two or more functions. The builder = new StringBuilder();
// any function call including parameters within the brackets
builder.append("\\w+\\s*\\([\\w\\.,\\s'=]+\\)");
// the potential alias
builder.append("\\s+[as|AS]+\\s+(([\\w\\.]+))");
FUNCTION_PATTERN = compile(builder.toString()); which only considers a single pair of parenthesis, but not two or more. So, for example, the
SELECT DISTINCT(event.id) as id,
event.name as name,
event.top_event as topEvent,
event.ranking as ranking,
MIN(bundle.base_price_amount) as cheapestBundlePrice,
MIN(DATE(bundle.start)) as earliestBundleStart,
..
|
still happen with 2.3.4.RELEASE |
Looks like using a FUNCTION_PATTERN of Current pattern is: |
As per the comments, I don't think this bug was fixed for all cases. Could this issue be reopened? |
If there is still a problem please create a new issue. |
Sergey Skryabin opened DATAJPA-1061 and commented
doesn't work ("The state field path 'c.n' cannot be resolved to a valid type."). Although
works fine.
http://docs.spring.io/spring-data/jpa/docs/1.11.x/reference/html/#jpa.query-methods.sorting
says: The properties actually used within the Order instances of Sort need to match to your domain model, which means they need to resolve to either a property or an alias used within the query.
Affects: 1.11 GA (Ingalls)
Attachments:
Referenced from: pull request #276
Backported to: 2.1.10 (Lovelace SR10), 1.11.23 (Ingalls SR23)
3 votes, 9 watchers
The text was updated successfully, but these errors were encountered: