Group aggregation supports multiple columns for postgres (#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
This commit is contained in:
Siddharth Ramesh
2020-03-09 21:48:39 +05:30
committed by GitHub
parent f69545939b
commit ef17dc4382
2 changed files with 61 additions and 21 deletions

View File

@@ -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();
@@ -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]);

View File

@@ -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`;