Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
21 changes: 19 additions & 2 deletions lib/make/makeAggregatePipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const $json = require('@synatic/json-magic');
const projectIsSimple = require('../projectIsSimple');
const lodash = require('lodash');
const projectIsRoot = require('../projectIsRoot');
const $check = require('check-types');

exports.makeAggregatePipeline = makeAggregatePipeline;
exports.stripJoinHints = stripJoinHints;
Expand Down Expand Up @@ -264,12 +265,18 @@ function makeAggregatePipeline(ast, context = {}) {
$json.set(preprocessedWhere, subQueryQuery.path + '$expr', {
$eq: [{$size: `$${tempTableField}`}, 0],
});
$json.remove(preprocessedWhere, subQueryQuery.path + '$$$SubQuery$$$');
$json.remove(
preprocessedWhere,
subQueryQuery.path + '$$$SubQuery$$$'
);
} else if (subQueryQuery.operator === 'IN') {
$json.set(preprocessedWhere, subQueryQuery.path + '$expr', {
$gt: [{$size: `$${tempTableField}`}, 0],
});
$json.remove(preprocessedWhere, subQueryQuery.path + '$$$SubQuery$$$');
$json.remove(
preprocessedWhere,
subQueryQuery.path + '$$$SubQuery$$$'
);
} else {
throw new Error(
`Sub query operations not supported: ${subQueryQuery.operator}`
Expand Down Expand Up @@ -569,6 +576,16 @@ function makeAggregatePipeline(ast, context = {}) {
}

// check if sortKeys exists, otherwise insert the source before the last project
if (
!pipeline.findLastIndex ||
!$check.function(pipeline.findLastIndex)
) {
console.log(
`findLastIndex was not a function, typeof pipeline: ${typeof pipeline}, ${pipeline}, ${JSON.stringify(
pipeline
)}`
);
}
const previousProjectIndex = pipeline.findLastIndex(
(p) => !!p.$project
);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@synatic/noql",
"version": "4.2.2",
"version": "4.2.3",
"description": "Convert SQL statements to mongo queries or aggregates",
"main": "index.js",
"files": [
Expand Down
31 changes: 4 additions & 27 deletions test/bug-fix-tests/bug-fix.json
Original file line number Diff line number Diff line change
Expand Up @@ -1277,33 +1277,10 @@
"nested-case": {
"case-1": {},
"case-2": {
"expectedResults": [
{
"id": 1,
"item": "almonds",
"category": "nut"
},
{
"id": 2,
"item": "pecans",
"category": "nut"
},
{
"id": 3,
"item": "almonds",
"category": "nut"
},
{
"id": 4,
"item": "almonds",
"category": "nut"
},
{
"id": 5,
"item": "potatoes",
"category": "starch"
}
]
"expectedResults": []
}
},
"sort-after-project": {
"case-1": {}
}
}
261 changes: 226 additions & 35 deletions test/bug-fix-tests/bug-fix.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2342,34 +2342,30 @@ describe('bug-fixes', function () {
{
pipeline: [
{
"$project": {
"u": "$$ROOT"
}
$project: {
u: '$$ROOT',
},
},
{
"$project": {
"full_name": {
"$toLower": "$u.name"
$project: {
full_name: {
$toLower: '$u.name',
},
"u.first_name": 1
}
'u.first_name': 1,
},
},
{
"$sort": {
"full_name": 1,
"u.first_name": 1
}
$sort: {
full_name: 1,
'u.first_name': 1,
},
},
{
"$unset": [
"u.first_name"
]
$unset: ['u.first_name'],
},
{
"$unset": [
"u"
]
}
$unset: ['u'],
},
],
collections: ['users'],
type: 'aggregate',
Expand All @@ -2391,29 +2387,27 @@ describe('bug-fixes', function () {
{
pipeline: [
{
"$project": {
"u": "$$ROOT"
}
$project: {
u: '$$ROOT',
},
},
{
"$project": {
"u.full_name": {
"$toLower": "$u.name"
$project: {
'u.full_name': {
$toLower: '$u.name',
},
"u.first_name": 1
}
'u.first_name': 1,
},
},
{
"$sort": {
"u.full_name": 1,
"u.first_name": 1
}
$sort: {
'u.full_name': 1,
'u.first_name': 1,
},
},
{
"$unset": [
"u.first_name"
]
}
$unset: ['u.first_name'],
},
],
collections: ['users'],
type: 'aggregate',
Expand Down Expand Up @@ -3062,5 +3056,202 @@ describe('bug-fixes', function () {
'Invalid sort order'
);
});

it('should successfully call findLastIndex if pipeline is empty', async () => {
const sql = `
select records as \`$$ROOT\`
from (select RecordId,
AncestorInstanceId,
AncestorRecordId,
AncestorRecordNumber,
ChecklistStatusId,
CurrentDv,
InsertDv,
InstanceId,
IsInUseByOtherRecords,
ModuleId,
ProcessFlowId,
RecordNumber,
RecordStatus,
SQ,
CreatedByUserId,
LatestModifiedByUserId,
DeletedByUserId
from \`global-list-module-records--vbfr-std-glb-module-record\`
where RecordStatus in ('active')
and InstanceId in ('19F881AA-94BA-4F9A-8E04-C37B172AF652' )
) as records
where 1=1
order by CurrentDv asc , SQ asc`;
await queryResultTester({
queryString: sql,
casePath: 'nested-case.case-2',
mode: 'write',
outputPipeline: false,
skipDbQuery: true,
optimizeJoins: false,
unsetId: true,
schemas: {
// 'global-list-module-records--vbfr-std-glb-module-record': {
// type: 'object',
// properties: {
// _id: {
// type: 'string',
// format: 'mongoid',
// },
// RecordId: {
// type: 'string',
// stringLength: 36,
// },
// AncestorInstanceId: {
// type: 'null',
// },
// AncestorRecordId: {
// type: 'null',
// },
// AncestorRecordNumber: {
// type: 'null',
// },
// AuditSQ: {
// type: 'null',
// },
// ChecklistStatusId: {
// type: ['null', 'string'],
// stringLength: 36,
// },
// CreatedByUserId: {
// type: 'string',
// stringLength: 36,
// },
// CurrentDv: {
// type: 'string',
// format: 'date-time',
// },
// DeletedByUserId: {
// type: 'null',
// },
// InsertDv: {
// type: 'string',
// format: 'date-time',
// },
// InstanceId: {
// type: 'string',
// stringLength: 36,
// },
// IsInUseByOtherRecords: {
// type: 'integer',
// },
// LatestModifiedByUserId: {
// type: 'string',
// stringLength: 36,
// },
// ModuleId: {
// type: 'string',
// stringLength: 36,
// },
// ProcessFlowId: {
// type: 'string',
// stringLength: 36,
// },
// RecordNumber: {
// type: 'integer',
// },
// RecordStatus: {
// type: 'string',
// stringLength: 6,
// },
// SQ: {
// type: 'integer',
// },
// _dateUpdated: {
// type: 'string',
// format: 'date-time',
// },
// },
// },
},
});
const aggr = makeMongoAggregate(sql);
assert.deepStrictEqual(
aggr,
{
pipeline: [
{
$match: {
$and: [
{
RecordStatus: {
$in: ['active'],
},
},
{
InstanceId: {
$in: [
'19F881AA-94BA-4F9A-8E04-C37B172AF652',
],
},
},
],
},
},
{
$project: {
RecordId: '$RecordId',
AncestorInstanceId: '$AncestorInstanceId',
AncestorRecordId: '$AncestorRecordId',
AncestorRecordNumber: '$AncestorRecordNumber',
ChecklistStatusId: '$ChecklistStatusId',
CurrentDv: '$CurrentDv',
InsertDv: '$InsertDv',
InstanceId: '$InstanceId',
IsInUseByOtherRecords: '$IsInUseByOtherRecords',
ModuleId: '$ModuleId',
ProcessFlowId: '$ProcessFlowId',
RecordNumber: '$RecordNumber',
RecordStatus: '$RecordStatus',
SQ: '$SQ',
CreatedByUserId: '$CreatedByUserId',
LatestModifiedByUserId:
'$LatestModifiedByUserId',
DeletedByUserId: '$DeletedByUserId',
},
},
{
$match: {
$expr: {
$eq: [1, 1],
},
},
},
{
$replaceRoot: {
newRoot: '$records',
},
},
{
$project: {
records: '$$ROOT',
CurrentDv: 1,
SQ: 1,
},
},
{
$sort: {
CurrentDv: 1,
SQ: 1,
},
},
{
$unset: ['CurrentDv', 'SQ'],
},
],
collections: [
'global-list-module-records--vbfr-std-glb-module-record',
],
type: 'aggregate',
},
'Invalid sort order'
);
});
});
});
Loading