Skip to content
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

Group aggregation supports multiple columns for postgres #6483

Merged
merged 4 commits into from
Mar 9, 2020
Merged
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
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