This project is archived and is in readonly mode.

#1207 ✓stale
Sascha Konietzke

PostgreSQLAdapter: Postgres Error while finding when using a function with two parameters in order clause

Reported by Sascha Konietzke | October 13th, 2008 @ 11:18 AM | in 3.x

I am using Postgis, which provides functions for calculating distances in the database. One is ST_Distance(point1, point2). When using associations, order by AND limit (or will_paginate's paginate method) an error is thrown because the postgresql adapter splits the order clause at each ",".

For :order => "ST_distance(placemarks.geo, other.geo)" this leads to the following query:

: SELECT * FROM (SELECT DISTINCT ON ("placemarks".id) "placemarks".id, ST_distance(placemarks.geo AS alias_0, other.geo) AS alias_1 FROM "placemarks" ...

throwing an ActiveRecord::StatementInvalid error.

The logic to parse the order clause is wrong. It is probably very hard to use regexs to parse the list and not split functions, so I thought using an array for multiple order by clauses would be a good idea.

I slightly modified the Postgres Adapter with a patch which seems to solve this problem for me.

What do you think, should this be a general convention for rails to allow :order => ["column1", "column2"] instead of :order => "column1, column2" as this solves parsing problems?

Comments and changes to this ticket

  • Pratik

    Pratik January 18th, 2009 @ 06:56 AM

    • Assigned user set to “Tarmo Tänav”
  • jay

    jay March 17th, 2009 @ 03:48 PM

    The old Trac-based dev site has a similar issue posted along with a patch that may help:

    http://dev.rubyonrails.org/ticke...

    I ran across a related issue yesterday when using PostgreSQL 8.3's NULLS FIRST/LAST clause in ordering. The aforementioned patch may help as it contains a small parser that looks for parenthesis and the like in function calls and also handles the NULLS FIRST/LAST issue, so I'll give it a shot and see how it goes.

  • jay

    jay March 17th, 2009 @ 07:08 PM

    Okay, I've applied the aforementioned patch courtesy of gravatite against the master head and attached the diff. I had a go at a couple of tests, but frankly, not being overly familiar with ActiveRecord's tests, I don't know how complete or useful they are. They're there, though, and at least the patch doesn't appear to break any existing tests.

  • Ruy Asan

    Ruy Asan June 20th, 2009 @ 11:57 PM

    I also have ran into this problem when trying to use postgresql's tsearch module for fulltext search.

    In general, any use of functions as order-by clauses will trigger this. Letting :order take arrays is going to be by far the best solution i think.

  • Jeremy Kemper

    Jeremy Kemper May 4th, 2010 @ 06:48 PM

    • Milestone changed from 2.x to 3.x
  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 05:04 PM

    • State changed from “new” to “open”
    • Importance changed from “” to “”

    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.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 05:04 PM

    • State changed from “open” to “stale”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Referenced by

Pages