Skip to content

Fix nearSphere queries when sorting by another key on Postgres #6026

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions spec/ParseQuery.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4868,4 +4868,34 @@ describe('Parse.Query testing', () => {
const results = await query.find();
equal(results[0].get('array').length, 105);
});

it('can perform nearSphere query with different sort key', async () => {
const obj1 = new Parse.Object('TestObject', {
name: 'c',
location: new Parse.GeoPoint(0, 0),
});
const obj2 = new Parse.Object('TestObject', {
name: 'b',
location: new Parse.GeoPoint(5, 5),
});
const obj3 = new Parse.Object('TestObject', {
name: 'a',
location: new Parse.GeoPoint(10, 10),
});

const distanceInKilometers = 1569 + 1; // 1569km is the approximate distance between {0, 0} and {10, 10}.
await Parse.Object.saveAll([obj1, obj2, obj3]);

const q = new Parse.Query(TestObject);
q.withinKilometers(
'location',
new Parse.GeoPoint(0, 0),
distanceInKilometers
);
q.ascending('name');

const results = await q.find();
equal(results.length, 3);
equal(results[0].get('name'), 'a');
});
});
4 changes: 0 additions & 4 deletions src/Adapters/Storage/Postgres/PostgresStorageAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -594,10 +594,6 @@ const buildWhereClause = ({ schema, query, index }): WhereClause => {
`ST_distance_sphere($${index}:name::geometry, POINT($${index +
1}, $${index + 2})::geometry) <= $${index + 3}`
);
sorts.push(
`ST_distance_sphere($${index}:name::geometry, POINT($${index +
1}, $${index + 2})::geometry) ASC`
);
values.push(fieldName, point.longitude, point.latitude, distanceInKM);
index += 4;
}
Expand Down