Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

PostgreSQL Adapter Breaks on Complex 'Order By' Clauses

#2622

The distinct and add_order_by_for_association_limiting methods in postgresql_adaptor.rb assume that incoming 'order by' clauses can be parsed simply by splitting on commas. This fails whenever a more complex expression is passed involving multiple columns with one or more function calls having multiple arguments.

For example,


Post.find(:all, :include => [:author, :comments], :conditions => "authors.name = 'David'", :order => "COALESCE(UPPER(posts.title), 'No Title') DESC, posts.id", :limit => 2, :offset => 1)

will result in broken SQL as ActiveRecord tries to associate aliases with all the "columns" that it finds including the arguments to COALESCE which it will see as two columns instead of one column that is modified by a function.

Attached is a patch that fixes the problem through an improved parsing of the column expressions along with an update to the test_limited_eager_with_multiple_order_columns unit test so that it also tests for this case.

Reported by John Weathers · May 8th, 2009 @ 03:29 AM

State: stale
Milestone: 2.3.10
Assigned to: Tarmo Tänav Tarmo Tänav
Importance: High

Activity

  1. jay
    jay

    I believe this may be a dupe of ticket #1207 and that the issue itself was originally posted in the old Trac system here: http://dev.rubyonrails.org/ticke...

    May 11th, 2009 @ 03:38 PM

  2. John Weathers
    John Weathers

    This issue does appear to overlap with ticket #1207.

    Has that issue received feedback from Rails core and moved further along the path towards being committed to Rails?

    I have applied for feedback for my patch, and the result of that feedback is in the following attachment which includes tests that focus on the column parsing - including one that breaks with the patch attached to #1207, but not with my updated patch.

    May 27th, 2009 @ 09:29 PM

  3. Michael Koziarski
    Michael Koziarski
    • Assigned user set to Tarmo Tänav

    assigning to tarmo to take a look. He's had the most experience in the postgresql adapter of late

    June 1st, 2009 @ 01:06 AM

  4. Tarmo Tänav
    Tarmo Tänav

    The patch looks generally good, I've got a couple of suggestions though. Firstly use "token << char" not "token += char" as the former modifies an existing string instead of generating a new one on each iteration. And the cases for '"' and "'" can be combined easily.

    Also, it would be good to have specific tests for the parse_columns method covering a variety of ORDER BY cases that it's now supposed to support.

    Thanks for your work

    June 1st, 2009 @ 02:43 AM

  5. John Weathers
    John Weathers

    OK. I've take care of replacing the "+=" method call with "<<" in constructing the token string. I've also combined the single and double quote cases. Finally, I've added some additional tests to 'order_by_clause_parsing_test_postgresql.rb' that test for specific ORDER BY cases.

    I'm attaching the updated patch file.

    June 2nd, 2009 @ 08:32 PM

  6. Michael Koziarski
    Michael Koziarski
    • Milestone changed from 2.x to 2.3.3

    The only thing which jumps out at me is that couldn't we avoid some of this expense for the really simple cases? e.g. if there's no ( or ) in the string, just split on ','?

    June 8th, 2009 @ 08:56 AM

  7. Tarmo Tänav
    Tarmo Tänav

    Good point, indeed it is quite a bit less expensive for the common case to just split if there are no characters that the complex case is looking for. I'll make the change.

    June 8th, 2009 @ 09:56 AM

  8. Michael Koziarski
    Michael Koziarski
    • Milestone changed from 2.3.3 to 2.3.4

    Moving to 2.3.4 rather than 2.3.3 as this isn't a regression.

    June 9th, 2009 @ 08:45 AM

  9. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.3.4 to 2.3.6

    [milestone:id#50064 bulk edit command]

    September 11th, 2009 @ 11:04 PM

  10. Rizwan Reza
    Rizwan Reza
    • Tag changed from associations, columns, distinct, limit, order, parse, patch, postgresql to associations, bugmash, columns, distinct, limit, order, parse, patch, postgresql

    May 16th, 2010 @ 02:41 AM

  11. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.3.6 to 2.3.7

    May 23rd, 2010 @ 05:54 PM

  12. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.3.7 to 2.3.8

    May 24th, 2010 @ 09:40 AM

  13. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.3.8 to 2.3.9

    May 25th, 2010 @ 11:45 PM

  14. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.3.9 to 2.3.10
    • Importance changed from to High

    August 30th, 2010 @ 02:28 AM

  15. rails
    rails
    • State changed from new to open

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 26th, 2011 @ 12:00 AM

  16. rails
    rails
    • State changed from open to stale

    February 26th, 2011 @ 12:00 AM