Skip to content

Commit e194057

Browse files
authored
Fix for using the new sorting with $replaceRoot (#219)
2 parents 1c07eda + 4e363eb commit e194057

File tree

7 files changed

+90
-184
lines changed

7 files changed

+90
-184
lines changed

lib/make/makeAggregatePipeline.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,14 @@ function makeAggregatePipeline(ast, context = {}) {
598598
pipeline,
599599
(p) => !!p.$project
600600
);
601+
const replaceRootAfterPreviousProject = pipeline.findIndex(
602+
(step, index) => {
603+
if (index < previousProjectIndex) {
604+
return false;
605+
}
606+
return !!step.$replaceRoot;
607+
}
608+
);
601609

602610
// previous project stage found
603611
if (previousProjectIndex > -1) {
@@ -669,13 +677,24 @@ function makeAggregatePipeline(ast, context = {}) {
669677
for (const key of unsetKeys) {
670678
projectObj[key] = 1;
671679
}
680+
let replaceRoot = null;
681+
if (
682+
replaceRootAfterPreviousProject > previousProjectIndex
683+
) {
684+
replaceRoot = pipeline[replaceRootAfterPreviousProject];
685+
pipeline.splice(replaceRootAfterPreviousProject, 1);
686+
}
672687
pipeline.splice(previousProjectIndex, 1);
673688
pipeline.push({$project: projectObj});
674689
pipeline.push({$sort: sortObj});
675690
pipeline.push({$unset: unsetKeys});
691+
676692
if (removePrefixes.length > 0) {
677693
pipeline.push({$unset: removePrefixes});
678694
}
695+
if (replaceRoot) {
696+
pipeline.push(replaceRoot);
697+
}
679698
} else {
680699
pipeline.push({$sort: sortObj});
681700
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@synatic/noql",
3-
"version": "4.2.4",
3+
"version": "4.2.5",
44
"description": "Convert SQL statements to mongo queries or aggregates",
55
"main": "index.js",
66
"files": [

test/bug-fix-tests/bug-fix.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,5 +1308,30 @@
13081308
},
13091309
"sort-after-project": {
13101310
"case-1": {}
1311+
},
1312+
"invalid-sort-order": {
1313+
"case-1": {
1314+
"expectedResults": [
1315+
{
1316+
"RecordId": "recordId",
1317+
"AncestorInstanceId": null,
1318+
"AncestorRecordId": null,
1319+
"AncestorRecordNumber": null,
1320+
"ChecklistStatusId": "ChecklistStatusId",
1321+
"CurrentDv": "2021-08-28T06:57:40.097Z",
1322+
"InsertDv": "2021-08-28T06:54:50.200Z",
1323+
"InstanceId": "InstanceId",
1324+
"IsInUseByOtherRecords": 0,
1325+
"ModuleId": "ModuleId",
1326+
"ProcessFlowId": "ProcessFlowId",
1327+
"RecordNumber": 1,
1328+
"RecordStatus": "active",
1329+
"SQ": 12345,
1330+
"CreatedByUserId": "admin",
1331+
"LatestModifiedByUserId": "admin",
1332+
"DeletedByUserId": null
1333+
}
1334+
]
1335+
}
13111336
}
13121337
}

test/bug-fix-tests/bug-fix.test.js

Lines changed: 13 additions & 172 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ describe('bug-fixes', function () {
8383
// const res = await queryResultTester({
8484
// queryString: queryString,
8585
// casePath: 'bugfix.current-date.case2',
86-
// mode: 'write',
86+
// mode,
8787
// });
8888
// const correctHcp = [
8989
// {
@@ -387,7 +387,7 @@ describe('bug-fixes', function () {
387387
await queryResultTester({
388388
queryString: queryString,
389389
casePath: 'bugfix.ntile.case1',
390-
mode: 'write',
390+
mode,
391391
outputPipeline: true,
392392
});
393393
});
@@ -1159,7 +1159,7 @@ describe('bug-fixes', function () {
11591159
await queryResultTester({
11601160
queryString: queryString,
11611161
casePath: 'deeply-nested-divide.case1',
1162-
mode: 'write',
1162+
mode,
11631163
outputPipeline: false,
11641164
});
11651165
});
@@ -1189,7 +1189,7 @@ describe('bug-fixes', function () {
11891189
const {pipeline} = await queryResultTester({
11901190
queryString: queryString,
11911191
casePath: 'and-or-nor.case-1',
1192-
mode: 'write',
1192+
mode,
11931193
outputPipeline: false,
11941194
skipDbQuery: true,
11951195
optimizeJoins: true,
@@ -2117,7 +2117,7 @@ describe('bug-fixes', function () {
21172117
await queryResultTester({
21182118
queryString: queryString,
21192119
casePath: 'nin.case-2',
2120-
mode: 'write',
2120+
mode,
21212121
outputPipeline: true,
21222122
skipDbQuery: true,
21232123
optimizeJoins: false,
@@ -2199,7 +2199,7 @@ describe('bug-fixes', function () {
21992199
await queryResultTester({
22002200
queryString: queryString,
22012201
casePath: 'nested-case.case-1',
2202-
mode: 'write',
2202+
mode,
22032203
outputPipeline: false,
22042204
skipDbQuery: true,
22052205
optimizeJoins: false,
@@ -2257,7 +2257,7 @@ describe('bug-fixes', function () {
22572257
await queryResultTester({
22582258
queryString: queryString,
22592259
casePath: 'nested-case.case-2',
2260-
mode: 'write',
2260+
mode,
22612261
outputPipeline: false,
22622262
skipDbQuery: false,
22632263
optimizeJoins: false,
@@ -3077,181 +3077,22 @@ from (select RecordId,
30773077
CreatedByUserId,
30783078
LatestModifiedByUserId,
30793079
DeletedByUserId
3080-
from \`global-list-module-records--vbfr-std-glb-module-record\`
3080+
from \`global-list-module-records-vbfr-std-glb-module-record\`
30813081
where RecordStatus in ('active')
3082-
and InstanceId in ('19F881AA-94BA-4F9A-8E04-C37B172AF652' )
3082+
and InstanceId in ('InstanceId' )
30833083
) as records
30843084
where 1=1
30853085
order by CurrentDv asc , SQ asc`;
30863086
await queryResultTester({
30873087
queryString: sql,
3088-
casePath: 'nested-case.case-2',
3089-
mode: 'write',
3088+
casePath: 'invalid-sort-order.case-1',
3089+
mode,
30903090
outputPipeline: false,
3091-
skipDbQuery: true,
3091+
expectZeroResults: false,
3092+
skipDbQuery: false,
30923093
optimizeJoins: false,
30933094
unsetId: true,
3094-
schemas: {
3095-
// 'global-list-module-records--vbfr-std-glb-module-record': {
3096-
// type: 'object',
3097-
// properties: {
3098-
// _id: {
3099-
// type: 'string',
3100-
// format: 'mongoid',
3101-
// },
3102-
// RecordId: {
3103-
// type: 'string',
3104-
// stringLength: 36,
3105-
// },
3106-
// AncestorInstanceId: {
3107-
// type: 'null',
3108-
// },
3109-
// AncestorRecordId: {
3110-
// type: 'null',
3111-
// },
3112-
// AncestorRecordNumber: {
3113-
// type: 'null',
3114-
// },
3115-
// AuditSQ: {
3116-
// type: 'null',
3117-
// },
3118-
// ChecklistStatusId: {
3119-
// type: ['null', 'string'],
3120-
// stringLength: 36,
3121-
// },
3122-
// CreatedByUserId: {
3123-
// type: 'string',
3124-
// stringLength: 36,
3125-
// },
3126-
// CurrentDv: {
3127-
// type: 'string',
3128-
// format: 'date-time',
3129-
// },
3130-
// DeletedByUserId: {
3131-
// type: 'null',
3132-
// },
3133-
// InsertDv: {
3134-
// type: 'string',
3135-
// format: 'date-time',
3136-
// },
3137-
// InstanceId: {
3138-
// type: 'string',
3139-
// stringLength: 36,
3140-
// },
3141-
// IsInUseByOtherRecords: {
3142-
// type: 'integer',
3143-
// },
3144-
// LatestModifiedByUserId: {
3145-
// type: 'string',
3146-
// stringLength: 36,
3147-
// },
3148-
// ModuleId: {
3149-
// type: 'string',
3150-
// stringLength: 36,
3151-
// },
3152-
// ProcessFlowId: {
3153-
// type: 'string',
3154-
// stringLength: 36,
3155-
// },
3156-
// RecordNumber: {
3157-
// type: 'integer',
3158-
// },
3159-
// RecordStatus: {
3160-
// type: 'string',
3161-
// stringLength: 6,
3162-
// },
3163-
// SQ: {
3164-
// type: 'integer',
3165-
// },
3166-
// _dateUpdated: {
3167-
// type: 'string',
3168-
// format: 'date-time',
3169-
// },
3170-
// },
3171-
// },
3172-
},
31733095
});
3174-
const aggr = makeMongoAggregate(sql);
3175-
assert.deepStrictEqual(
3176-
aggr,
3177-
{
3178-
pipeline: [
3179-
{
3180-
$match: {
3181-
$and: [
3182-
{
3183-
RecordStatus: {
3184-
$in: ['active'],
3185-
},
3186-
},
3187-
{
3188-
InstanceId: {
3189-
$in: [
3190-
'19F881AA-94BA-4F9A-8E04-C37B172AF652',
3191-
],
3192-
},
3193-
},
3194-
],
3195-
},
3196-
},
3197-
{
3198-
$project: {
3199-
RecordId: '$RecordId',
3200-
AncestorInstanceId: '$AncestorInstanceId',
3201-
AncestorRecordId: '$AncestorRecordId',
3202-
AncestorRecordNumber: '$AncestorRecordNumber',
3203-
ChecklistStatusId: '$ChecklistStatusId',
3204-
CurrentDv: '$CurrentDv',
3205-
InsertDv: '$InsertDv',
3206-
InstanceId: '$InstanceId',
3207-
IsInUseByOtherRecords: '$IsInUseByOtherRecords',
3208-
ModuleId: '$ModuleId',
3209-
ProcessFlowId: '$ProcessFlowId',
3210-
RecordNumber: '$RecordNumber',
3211-
RecordStatus: '$RecordStatus',
3212-
SQ: '$SQ',
3213-
CreatedByUserId: '$CreatedByUserId',
3214-
LatestModifiedByUserId:
3215-
'$LatestModifiedByUserId',
3216-
DeletedByUserId: '$DeletedByUserId',
3217-
},
3218-
},
3219-
{
3220-
$match: {
3221-
$expr: {
3222-
$eq: [1, 1],
3223-
},
3224-
},
3225-
},
3226-
{
3227-
$replaceRoot: {
3228-
newRoot: '$records',
3229-
},
3230-
},
3231-
{
3232-
$project: {
3233-
records: '$$ROOT',
3234-
CurrentDv: 1,
3235-
SQ: 1,
3236-
},
3237-
},
3238-
{
3239-
$sort: {
3240-
CurrentDv: 1,
3241-
SQ: 1,
3242-
},
3243-
},
3244-
{
3245-
$unset: ['CurrentDv', 'SQ'],
3246-
},
3247-
],
3248-
collections: [
3249-
'global-list-module-records--vbfr-std-glb-module-record',
3250-
],
3251-
type: 'aggregate',
3252-
},
3253-
'Invalid sort order'
3254-
);
32553096
});
32563097
});
32573098
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[
2+
{
3+
"RecordId": "recordId",
4+
"AncestorInstanceId": null,
5+
"AncestorRecordId": null,
6+
"AncestorRecordNumber": null,
7+
"ChecklistStatusId": "ChecklistStatusId",
8+
"CurrentDv": "2021-08-28T06:57:40.097Z",
9+
"InsertDv": "2021-08-28T06:54:50.200Z",
10+
"InstanceId": "InstanceId",
11+
"IsInUseByOtherRecords": 0,
12+
"ModuleId": "ModuleId",
13+
"ProcessFlowId": "ProcessFlowId",
14+
"RecordNumber": 1,
15+
"RecordStatus": "active",
16+
"SQ": 12345,
17+
"CreatedByUserId": "admin",
18+
"LatestModifiedByUserId": "admin",
19+
"DeletedByUserId": null
20+
}
21+
]

test/joins/joins.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ describe('joins', function () {
207207
const {pipeline, results} = await queryResultTester({
208208
queryString,
209209
casePath: 'left-join.no-alias.case-1',
210-
mode: 'write',
210+
mode,
211211
skipDbQuery: false,
212212
});
213213
const lookup = pipeline.find((p) => !!p.$lookup);
@@ -695,7 +695,7 @@ describe('joins', function () {
695695
const {pipeline} = await queryResultTester({
696696
queryString,
697697
casePath: 'optimize.explicit',
698-
mode: 'write',
698+
mode,
699699
unsetId: false,
700700
});
701701

@@ -708,7 +708,7 @@ describe('joins', function () {
708708
const {pipeline} = await queryResultTester({
709709
queryString,
710710
casePath: 'optimize.explicit',
711-
mode: 'write',
711+
mode,
712712
});
713713

714714
assert.deepStrictEqual(pipeline, expectedPipeline);

0 commit comments

Comments
 (0)