-
Notifications
You must be signed in to change notification settings - Fork 21.3k
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
Incorrect SQL generated for .last when ordering uses an SQL function #8919
Conversation
@@ -1226,4 +1226,11 @@ | |||
|
|||
*Aaron Patterson* | |||
|
|||
* Fix for reverting sorting order when SQL function is used in order clause | |||
This was causing incorrect SQL generation when oder is not just a simple |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you mean « order » here ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, thanks.
Fixed.
only outside of parentheses. When splitting on any commas this causes generation of incorrect SQL if any SQL functions are used inside sort order. I.e for "order by max(t.field1, t.field2)" DESC would be inserted twice
I tried to find if somebody has already fixed it before implementing myself. Apparently I did not search good enough. |
@vanyak looks good. Could you apply the tests cases from the other two patches on you pull request and see if all work with your fix? |
actually i think they won't pass: my solution from #8741: |
@joxxoxo looks like you are right. There are actually some more cases when it won't work. Unfortunately Ruby does not support variable-length lookbehind assertions so the only way would be to use split like you did. |
@vanyak alright, thanks for reporting back. |
I bumped into a problem with using COALESCE in sorting but it's relevant to any function like min or max.
When I have an order defined as
COALESCE(t.field1, t.field2)
, all works great until .last is used. ActiveRecord was generating invalid SQLselect whatever from t ORDER BY COALESCE(t.field1 DESC, t.field2 DESC) LIMIT 1
.