Skip to content

Support for Distinct Query #4195

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
wants to merge 6 commits into from
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
106 changes: 106 additions & 0 deletions spec/ParseQuery.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
'use strict';

const Parse = require('parse/node');
const rp = require('request-promise');

describe('Parse.Query testing', () => {
it("basic query", function(done) {
Expand Down Expand Up @@ -3007,4 +3008,109 @@ describe('Parse.Query testing', () => {
done();
}, done.fail);
});

it('distinct query', function(done) {
const score1 = new TestObject({score: 10});
const score2 = new TestObject({score: 10});
const score3 = new TestObject({score: 10});
const score4 = new TestObject({score: 20});
Parse.Object.saveAll([score1, score2, score3, score4]).then(() => {
const distinct = 'score';
return rp.post({
url: Parse.serverURL + "/classes/TestObject",
json: { distinct, "_method": "GET" },
headers: {
'X-Parse-Application-Id': Parse.applicationId,
'X-Parse-Javascript-Key': Parse.javaScriptKey
}
});
}).then((response) => {
expect(response.results.length).toBe(2);
expect(response.results.indexOf(10) > -1).toBe(true);
expect(response.results.indexOf(20) > -1).toBe(true);
done();
}).catch(done.fail);
});

it('distinct nested', (done) => {
const sender1 = { group: 'A' };
const sender2 = { group: 'A' };
const sender3 = { group: 'B' };
const obj1 = new TestObject({ sender: sender1 });
const obj2 = new TestObject({ sender: sender2 });
const obj3 = new TestObject({ sender: sender3 });
Parse.Object.saveAll([obj1, obj2, obj3]).then(() => {
const distinct = 'sender.group';
return rp.post({
url: Parse.serverURL + "/classes/TestObject",
json: { distinct, "_method": "GET" },
headers: {
'X-Parse-Application-Id': Parse.applicationId,
'X-Parse-Javascript-Key': Parse.javaScriptKey
}
});
}).then((response) => {
expect(response.results.length).toBe(2);
expect(response.results.indexOf('A') > -1).toBe(true);
expect(response.results.indexOf('B') > -1).toBe(true);
done();
}).catch(done.fail);
});

it('distinct class does not exist return empty', (done) => {
const distinct = 'unknown';
rp.post({
url: Parse.serverURL + "/classes/UnknownDistinct",
json: { distinct, "_method": "GET" },
headers: {
'X-Parse-Application-Id': Parse.applicationId,
'X-Parse-Javascript-Key': Parse.javaScriptKey
}
}).then((response) => {
expect(response.results.length).toBe(0);
done();
}).catch(done.fail);
});

it('distinct field does not exist return empty', function(done) {
const score = new TestObject({score: 10});
score.save().then(() => {
const distinct = 'unknown';
return rp.post({
url: Parse.serverURL + "/classes/TestObject",
json: { distinct, "_method": "GET" },
headers: {
'X-Parse-Application-Id': Parse.applicationId,
'X-Parse-Javascript-Key': Parse.javaScriptKey
}
});
}).then((response) => {
expect(response.results.length).toBe(0);
done();
}).catch(done.fail);
});

it('distinct array', function(done) {
const size1 = new TestObject({size: ['S', 'M']});
const size2 = new TestObject({size: ['M', 'L']});
const size3 = new TestObject({size: ['S']});
const size4 = new TestObject({size: ['S']});
Parse.Object.saveAll([size1, size2, size3, size4]).then(() => {
const distinct = 'size';
return rp.post({
url: Parse.serverURL + "/classes/TestObject",
json: { distinct, "_method": "GET" },
headers: {
'X-Parse-Application-Id': Parse.applicationId,
'X-Parse-Javascript-Key': Parse.javaScriptKey
}
});
}).then((response) => {
expect(response.results.length).toBe(3);
expect(response.results.indexOf('S') > -1).toBe(true);
expect(response.results.indexOf('M') > -1).toBe(true);
expect(response.results.indexOf('L') > -1).toBe(true);
done();
}).catch(done.fail);
});
});
4 changes: 4 additions & 0 deletions src/Adapters/Storage/Mongo/MongoCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ export default class MongoCollection {
return findOperation.toArray();
}

distinct(field, query) {
return this._mongoCollection.distinct(field, query);
}

count(query, { skip, limit, sort, maxTimeMS, readPreference } = {}) {
const countOperation = this._mongoCollection.count(query, { skip, limit, sort, maxTimeMS, readPreference });

Expand Down
7 changes: 7 additions & 0 deletions src/Adapters/Storage/Mongo/MongoStorageAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,13 @@ export class MongoStorageAdapter {
}));
}

// Finds unique values.
distinct(className, schema, query, fieldName) {
schema = convertParseSchemaToMongoSchema(schema);
return this._adaptiveCollection(className)
.then(collection => collection.distinct(fieldName, transformWhere(className, query, schema)));
}

_parseReadPreference(readPreference) {
if (readPreference) {
switch (readPreference) {
Expand Down
33 changes: 33 additions & 0 deletions src/Adapters/Storage/Postgres/PostgresStorageAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -1366,6 +1366,39 @@ export class PostgresStorageAdapter {
});
}

// Finds unique values.
distinct(className, schema, query, fieldName) {
debug('distinct', className, query);
let field = fieldName;
let column = fieldName;
if (fieldName.indexOf('.') >= 0) {
field = transformDotFieldToComponents(fieldName).join('->');
column = fieldName.split('.')[0];
}
const isArrayField = schema.fields
&& schema.fields[fieldName]
&& schema.fields[fieldName].type === 'Array';
const values = [field, column, className];
const where = buildWhereClause({ schema, query, index: 4 });
values.push(...where.values);

const wherePattern = where.pattern.length > 0 ? `WHERE ${where.pattern}` : '';
let qs = `SELECT DISTINCT ON ($1:raw) $2:raw FROM $3:name ${wherePattern}`;
if (isArrayField) {
qs = `SELECT distinct jsonb_array_elements($1:raw) as $2:raw FROM $3:name ${wherePattern}`;
}
debug(qs, values);
return this._client.any(qs, values)
.catch(() => [])
.then((results) => {
if (fieldName.indexOf('.') === -1) {
return results.map(object => object[field]);
}
const child = fieldName.split('.')[1];
return results.map(object => object[column][child]);
});
}

performInitialization({ VolatileClassesSchemas }) {
debug('performInitialization');
const promises = VolatileClassesSchemas.map((schema) => {
Expand Down
7 changes: 7 additions & 0 deletions src/Controllers/DatabaseController.js
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,7 @@ DatabaseController.prototype.find = function(className, query, {
limit,
acl,
sort = {},
distinct,
count,
keys,
op,
Expand Down Expand Up @@ -853,6 +854,12 @@ DatabaseController.prototype.find = function(className, query, {
} else {
return this.adapter.count(className, schema, query, readPreference);
}
} else if (distinct) {
if (!classExists) {
return [];
} else {
return this.adapter.distinct(className, schema, query, distinct);
}
} else {
if (!classExists) {
return [];
Expand Down
1 change: 1 addition & 0 deletions src/RestQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ function RestQuery(config, auth, className, restWhere = {}, restOptions = {}, cl
case 'count':
this.doCount = true;
break;
case 'distinct':
case 'skip':
case 'limit':
case 'readPreference':
Expand Down
5 changes: 4 additions & 1 deletion src/Routers/ClassesRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export class ClassesRouter extends PromiseRouter {

static optionsFromBody(body) {
const allowConstraints = ['skip', 'limit', 'order', 'count', 'keys',
'include', 'redirectClassNameForKey', 'where'];
'include', 'redirectClassNameForKey', 'where', 'distinct'];

for (const key of Object.keys(body)) {
if (allowConstraints.indexOf(key) === -1) {
Expand Down Expand Up @@ -131,6 +131,9 @@ export class ClassesRouter extends PromiseRouter {
if (body.include) {
options.include = String(body.include);
}
if (body.distinct) {
options.distinct = String(body.distinct);
}
return options;
}

Expand Down