Skip to content

Commit d379514

Browse files
authored
Group aggregation supports multiple columns for postgres (parse-community#6483)
* 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
1 parent 864398b commit d379514

File tree

2 files changed

+61
-21
lines changed

2 files changed

+61
-21
lines changed

spec/ParseQuery.Aggregate.spec.js

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,32 @@ describe('Parse.Query Aggregate testing', () => {
225225
});
226226
});
227227

228+
it('group by multiple columns ', done => {
229+
const obj1 = new TestObject();
230+
const obj2 = new TestObject();
231+
const obj3 = new TestObject();
232+
const pipeline = [
233+
{
234+
group: {
235+
objectId: {
236+
score: '$score',
237+
views: '$views',
238+
},
239+
count: { $sum: 1 },
240+
},
241+
},
242+
];
243+
Parse.Object.saveAll([obj1, obj2, obj3])
244+
.then(() => {
245+
const query = new Parse.Query(TestObject);
246+
return query.aggregate(pipeline);
247+
})
248+
.then(results => {
249+
expect(results.length).toEqual(5);
250+
done();
251+
});
252+
});
253+
228254
it('group by date object', done => {
229255
const obj1 = new TestObject();
230256
const obj2 = new TestObject();
@@ -963,25 +989,29 @@ describe('Parse.Query Aggregate testing', () => {
963989
await Parse.Object.saveAll([obj1, obj2, obj3, obj4, obj5, obj6]);
964990

965991
expect(
966-
(await new Parse.Query('MyCollection').aggregate([
967-
{
968-
match: {
969-
language: { $in: [null, 'en'] },
992+
(
993+
await new Parse.Query('MyCollection').aggregate([
994+
{
995+
match: {
996+
language: { $in: [null, 'en'] },
997+
},
970998
},
971-
},
972-
]))
999+
])
1000+
)
9731001
.map(value => value.otherField)
9741002
.sort()
9751003
).toEqual([1, 2, 3, 4]);
9761004

9771005
expect(
978-
(await new Parse.Query('MyCollection').aggregate([
979-
{
980-
match: {
981-
$or: [{ language: 'en' }, { language: null }],
1006+
(
1007+
await new Parse.Query('MyCollection').aggregate([
1008+
{
1009+
match: {
1010+
$or: [{ language: 'en' }, { language: null }],
1011+
},
9821012
},
983-
},
984-
]))
1013+
])
1014+
)
9851015
.map(value => value.otherField)
9861016
.sort()
9871017
).toEqual([1, 2, 3, 4]);

src/Adapters/Storage/Postgres/PostgresStorageAdapter.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2219,20 +2219,30 @@ export class PostgresStorageAdapter implements StorageAdapter {
22192219
groupValues = value;
22202220
const groupByFields = [];
22212221
for (const alias in value) {
2222-
const operation = Object.keys(value[alias])[0];
2223-
const source = transformAggregateField(value[alias][operation]);
2224-
if (mongoAggregateToPostgres[operation]) {
2222+
if (typeof value[alias] === 'string' && value[alias]) {
2223+
const source = transformAggregateField(value[alias]);
22252224
if (!groupByFields.includes(`"${source}"`)) {
22262225
groupByFields.push(`"${source}"`);
22272226
}
2228-
columns.push(
2229-
`EXTRACT(${
2230-
mongoAggregateToPostgres[operation]
2231-
} FROM $${index}:name AT TIME ZONE 'UTC') AS $${index +
2232-
1}:name`
2233-
);
22342227
values.push(source, alias);
2228+
columns.push(`$${index}:name AS $${index + 1}:name`);
22352229
index += 2;
2230+
} else {
2231+
const operation = Object.keys(value[alias])[0];
2232+
const source = transformAggregateField(value[alias][operation]);
2233+
if (mongoAggregateToPostgres[operation]) {
2234+
if (!groupByFields.includes(`"${source}"`)) {
2235+
groupByFields.push(`"${source}"`);
2236+
}
2237+
columns.push(
2238+
`EXTRACT(${
2239+
mongoAggregateToPostgres[operation]
2240+
} FROM $${index}:name AT TIME ZONE 'UTC') AS $${index +
2241+
1}:name`
2242+
);
2243+
values.push(source, alias);
2244+
index += 2;
2245+
}
22362246
}
22372247
}
22382248
groupPattern = `GROUP BY $${index}:raw`;

0 commit comments

Comments
 (0)