Skip to content

Commit

Permalink
Group aggregation supports multiple columns for postgres (#6483)
Browse files Browse the repository at this point in the history
* Group aggregation supports multiple columns for postgres

* Group aggregation supports multiple columns for postgres

* Group aggregation supports multiple columns for postgres

* Group aggregation supports multiple columns for postgres
  • Loading branch information
srameshr committed Mar 9, 2020
1 parent f695459 commit ef17dc4
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 21 deletions.
54 changes: 42 additions & 12 deletions spec/ParseQuery.Aggregate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,32 @@ describe('Parse.Query Aggregate testing', () => {
});
});

it('group by multiple columns ', done => {
const obj1 = new TestObject();
const obj2 = new TestObject();
const obj3 = new TestObject();
const pipeline = [
{
group: {
objectId: {
score: '$score',
views: '$views',
},
count: { $sum: 1 },
},
},
];
Parse.Object.saveAll([obj1, obj2, obj3])
.then(() => {
const query = new Parse.Query(TestObject);
return query.aggregate(pipeline);
})
.then(results => {
expect(results.length).toEqual(5);
done();
});
});

it('group by date object', done => {
const obj1 = new TestObject();
const obj2 = new TestObject();
Expand Down Expand Up @@ -963,25 +989,29 @@ describe('Parse.Query Aggregate testing', () => {
await Parse.Object.saveAll([obj1, obj2, obj3, obj4, obj5, obj6]);

expect(
(await new Parse.Query('MyCollection').aggregate([
{
match: {
language: { $in: [null, 'en'] },
(
await new Parse.Query('MyCollection').aggregate([
{
match: {
language: { $in: [null, 'en'] },
},
},
},
]))
])
)
.map(value => value.otherField)
.sort()
).toEqual([1, 2, 3, 4]);

expect(
(await new Parse.Query('MyCollection').aggregate([
{
match: {
$or: [{ language: 'en' }, { language: null }],
(
await new Parse.Query('MyCollection').aggregate([
{
match: {
$or: [{ language: 'en' }, { language: null }],
},
},
},
]))
])
)
.map(value => value.otherField)
.sort()
).toEqual([1, 2, 3, 4]);
Expand Down
28 changes: 19 additions & 9 deletions src/Adapters/Storage/Postgres/PostgresStorageAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2219,20 +2219,30 @@ export class PostgresStorageAdapter implements StorageAdapter {
groupValues = value;
const groupByFields = [];
for (const alias in value) {
const operation = Object.keys(value[alias])[0];
const source = transformAggregateField(value[alias][operation]);
if (mongoAggregateToPostgres[operation]) {
if (typeof value[alias] === 'string' && value[alias]) {
const source = transformAggregateField(value[alias]);
if (!groupByFields.includes(`"${source}"`)) {
groupByFields.push(`"${source}"`);
}
columns.push(
`EXTRACT(${
mongoAggregateToPostgres[operation]
} FROM $${index}:name AT TIME ZONE 'UTC') AS $${index +
1}:name`
);
values.push(source, alias);
columns.push(`$${index}:name AS $${index + 1}:name`);
index += 2;
} else {
const operation = Object.keys(value[alias])[0];
const source = transformAggregateField(value[alias][operation]);
if (mongoAggregateToPostgres[operation]) {
if (!groupByFields.includes(`"${source}"`)) {
groupByFields.push(`"${source}"`);
}
columns.push(
`EXTRACT(${
mongoAggregateToPostgres[operation]
} FROM $${index}:name AT TIME ZONE 'UTC') AS $${index +
1}:name`
);
values.push(source, alias);
index += 2;
}
}
}
groupPattern = `GROUP BY $${index}:raw`;
Expand Down

0 comments on commit ef17dc4

Please sign in to comment.