Skip to content

Commit 848a6cf

Browse files
dplewisflovilmart
authored andcommitted
Distinct support for null (#4559)
* distinct support for null * better testing
1 parent cac14bc commit 848a6cf

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

spec/ParseQuery.Aggregate.spec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,30 @@ describe('Parse.Query Aggregate testing', () => {
589589
}).catch(done.fail);
590590
});
591591

592+
it('distinct null field', (done) => {
593+
const options = Object.assign({}, masterKeyOptions, {
594+
body: { distinct: 'distinctField' }
595+
});
596+
const user1 = new Parse.User();
597+
user1.setUsername('distinct_1');
598+
user1.setPassword('password');
599+
user1.set('distinctField', 'one');
600+
601+
const user2 = new Parse.User();
602+
user2.setUsername('distinct_2');
603+
user2.setPassword('password');
604+
user2.set('distinctField', null);
605+
user1.signUp().then(() => {
606+
return user2.signUp();
607+
}).then(() => {
608+
return rp.get(Parse.serverURL + '/aggregate/_User', options);
609+
}).then((resp) => {
610+
expect(resp.results.length).toEqual(1);
611+
expect(resp.results).toEqual(['one']);
612+
done();
613+
}).catch(done.fail);
614+
});
615+
592616
it('does not return sensitive hidden properties', (done) => {
593617
const options = Object.assign({}, masterKeyOptions, {
594618
body: {

src/Adapters/Storage/Mongo/MongoStorageAdapter.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -511,13 +511,16 @@ export class MongoStorageAdapter implements StorageAdapter {
511511
}
512512
return this._adaptiveCollection(className)
513513
.then(collection => collection.distinct(fieldName, transformWhere(className, query, schema)))
514-
.then(objects => objects.map(object => {
515-
if (isPointerField) {
516-
const field = fieldName.substring(3);
517-
return transformPointerString(schema, field, object);
518-
}
519-
return mongoObjectToParseObject(className, object, schema);
520-
}));
514+
.then(objects => {
515+
objects = objects.filter((obj) => obj != null);
516+
return objects.map(object => {
517+
if (isPointerField) {
518+
const field = fieldName.substring(3);
519+
return transformPointerString(schema, field, object);
520+
}
521+
return mongoObjectToParseObject(className, object, schema);
522+
});
523+
});
521524
}
522525

523526
aggregate(className: string, schema: any, pipeline: any, readPreference: ?string) {

src/Adapters/Storage/Postgres/PostgresStorageAdapter.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,7 +1464,8 @@ export class PostgresStorageAdapter implements StorageAdapter {
14641464
debug('distinct', className, query);
14651465
let field = fieldName;
14661466
let column = fieldName;
1467-
if (fieldName.indexOf('.') >= 0) {
1467+
const isNested = fieldName.indexOf('.') >= 0;
1468+
if (isNested) {
14681469
field = transformDotFieldToComponents(fieldName).join('->');
14691470
column = fieldName.split('.')[0];
14701471
}
@@ -1480,7 +1481,10 @@ export class PostgresStorageAdapter implements StorageAdapter {
14801481

14811482
const wherePattern = where.pattern.length > 0 ? `WHERE ${where.pattern}` : '';
14821483
const transformer = isArrayField ? 'jsonb_array_elements' : 'ON';
1483-
const qs = `SELECT DISTINCT ${transformer}($1:raw) $2:raw FROM $3:name ${wherePattern}`;
1484+
let qs = `SELECT DISTINCT ${transformer}($1:name) $2:name FROM $3:name ${wherePattern}`;
1485+
if (isNested) {
1486+
qs = `SELECT DISTINCT ${transformer}($1:raw) $2:raw FROM $3:name ${wherePattern}`;
1487+
}
14841488
debug(qs, values);
14851489
return this._client.any(qs, values)
14861490
.catch((error) => {
@@ -1490,7 +1494,7 @@ export class PostgresStorageAdapter implements StorageAdapter {
14901494
throw error;
14911495
})
14921496
.then((results) => {
1493-
if (fieldName.indexOf('.') === -1) {
1497+
if (!isNested) {
14941498
results = results.filter((object) => object[field] !== null);
14951499
return results.map(object => {
14961500
if (!isPointerField) {

0 commit comments

Comments
 (0)