Skip to content

Geocoder produces PG::SyntaxError on rails 4.1 for count queries #630

Closed
@mswart

Description

@mswart

After upgrade to rails 4.1 Geocoder fails to count the number of results when searching by distance. I have reproduced it with a very simple example:

> Place.all.near([0.05, 0.1], 10, units: :km).count
SELECT COUNT(places.*, 6371.0 * 2 * ASIN(SQRT(POWER(SIN((0.05 - places.latitude) * PI() / 180 / 2), 2) + COS(0.05 * PI() / 180) * COS(places.latitude * PI() / 180) * POWER(SIN((0.1 - places.longitude) * PI() / 180 / 2), 2))) AS distance, CAST(DEGREES(ATAN2( RADIANS(places.longitude - 0.1), RADIANS(places.latitude - 0.05))) + 360 AS decimal) % 360 AS bearing) FROM "places"  WHERE (places.latitude BETWEEN -0.039932160591873045 AND 0.13993216059187305 AND places.longitude BETWEEN 0.010067805164487686 AND 0.18993219483551232 AND 6371.0 * 2 * ASIN(SQRT(POWER(SIN((0.05 - places.latitude) * PI() / 180 / 2), 2) + COS(0.05 * PI() / 180) * COS(places.latitude * PI() / 180) * POWER(SIN((0.1 - places.longitude) * PI() / 180 / 2), 2))) <= 10)
PG::SyntaxError: ERROR:  syntax error at or near "AS"
LINE 1: ...((0.1 - places.longitude) * PI() / 180 / 2), 2))) AS distanc...

In Rails 4.0.4 was

SELECT COUNT(*) FROM "places" WHERE (places.latitude BETWEEN -0.039932160591873045 AND 0.13993216059187305 AND places.longitude BETWEEN 0.010067805164487686 AND 0.18993219483551232 AND 6371.0 * 2 * ASIN(SQRT(POWER(SIN((0.05 - places.latitude) * PI() / 180 / 2), 2) + COS(0.05 * PI() / 180) * COS(places.latitude * PI() / 180) * POWER(SIN((0.1 - places.longitude) * PI() / 180 / 2), 2))) <= 10)
=> 0

Normal query work still in 4.1.0:

> Place.all.near([0.05, 0.1], 10, units: :km)
SELECT places.*, 6371.0 * 2 * ASIN(SQRT(POWER(SIN((0.05 - places.latitude) * PI() / 180 / 2), 2) + COS(0.05 * PI() / 180) * COS(places.latitude * PI() / 180) * POWER(SIN((0.1 - places.longitude) * PI() / 180 / 2), 2))) AS distance, CAST(DEGREES(ATAN2( RADIANS(places.longitude - 0.1), RADIANS(places.latitude - 0.05))) + 360 AS decimal) % 360 AS bearing FROM "places"  WHERE (places.latitude BETWEEN -0.039932160591873045 AND 0.13993216059187305 AND places.longitude BETWEEN 0.010067805164487686 AND 0.18993219483551232 AND 6371.0 * 2 * ASIN(SQRT(POWER(SIN((0.05 - places.latitude) * PI() / 180 / 2), 2) + COS(0.05 * PI() / 180) * COS(places.latitude * PI() / 180) * POWER(SIN((0.1 - places.longitude) * PI() / 180 / 2), 2))) <= 10)  ORDER BY distance ASC
=> #<ActiveRecord::Relation []>

Model:

class Place < ActiveRecord::Base
  reverse_geocoded_by :latitude, :longitude
end

Schema:

ActiveRecord::Schema.define(version: 20140409132943) do
  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "places", force: true do |t|
    t.datetime "created_at"
    t.datetime "updated_at"
    t.float    "latitude"
    t.float    "longitude"
  end
end

Tested with
ruby 2.1
rails 4.1.0 (worked with rails 4.0.4)
geocoder 1.1.9 and master @2a29613acda2468e23056921cc257252d2128ea8
postgresql 9.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions