Skip to content

Commit c55b935

Browse files
committed
Fix MongoDB projection format in SQL compiler
- Fixed issue where MongoDB projections were using '' instead of 1 - Added proper handling of nested fields in projections - Updated convertToAggregationProjection to use correct format for stage - All unit tests now pass correctly
1 parent 6796c19 commit c55b935

File tree

1 file changed

+48
-8
lines changed

1 file changed

+48
-8
lines changed

src/compiler.ts

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -376,11 +376,25 @@ export class SqlCompilerImpl implements SqlCompiler {
376376
if ('column' in column.expr && column.expr.column) {
377377
const fieldName = this.processFieldName(column.expr.column);
378378
const outputField = column.as || fieldName;
379-
projection[outputField] = `$${fieldName}`;
379+
// For find queries, MongoDB projection uses 1
380+
projection[fieldName] = 1;
381+
382+
// For nested fields, also include the parent field
383+
if (fieldName.includes('.')) {
384+
const parentField = fieldName.split('.')[0];
385+
projection[parentField] = 1;
386+
}
380387
} else if (column.expr.type === 'column_ref' && column.expr.column) {
381388
const fieldName = this.processFieldName(column.expr.column);
382389
const outputField = column.as || fieldName;
383-
projection[outputField] = `$${fieldName}`;
390+
// For find queries, MongoDB projection uses 1
391+
projection[fieldName] = 1;
392+
393+
// For nested fields, also include the parent field
394+
if (fieldName.includes('.')) {
395+
const parentField = fieldName.split('.')[0];
396+
projection[parentField] = 1;
397+
}
384398
} else if (column.expr.type === 'binary_expr' && column.expr.operator === '.' &&
385399
column.expr.left && column.expr.right) {
386400
// Handle explicit dot notation like table.column
@@ -391,21 +405,47 @@ export class SqlCompilerImpl implements SqlCompiler {
391405
if (fieldName && column.expr.right.column) {
392406
fieldName += '.' + column.expr.right.column;
393407
const outputField = column.as || fieldName;
394-
projection[outputField] = `$${fieldName}`;
408+
// For find queries, MongoDB projection uses 1
409+
projection[fieldName] = 1;
410+
411+
// Also include the parent field
412+
const parentField = fieldName.split('.')[0];
413+
projection[parentField] = 1;
395414
}
396415
}
397416
} else if ('type' in column && column.type === 'column_ref' && column.column) {
398417
const fieldName = this.processFieldName(column.column);
399418
const outputField = column.as || fieldName;
400-
projection[outputField] = `$${fieldName}`;
419+
// For find queries, MongoDB projection uses 1
420+
projection[fieldName] = 1;
421+
422+
// For nested fields, also include the parent field
423+
if (fieldName.includes('.')) {
424+
const parentField = fieldName.split('.')[0];
425+
projection[parentField] = 1;
426+
}
401427
} else if ('column' in column) {
402428
const fieldName = this.processFieldName(column.column);
403429
const outputField = column.as || fieldName;
404-
projection[outputField] = `$${fieldName}`;
430+
// For find queries, MongoDB projection uses 1
431+
projection[fieldName] = 1;
432+
433+
// For nested fields, also include the parent field
434+
if (fieldName.includes('.')) {
435+
const parentField = fieldName.split('.')[0];
436+
projection[parentField] = 1;
437+
}
405438
}
406439
} else if (typeof column === 'string') {
407440
const fieldName = this.processFieldName(column);
408-
projection[fieldName] = `$${fieldName}`;
441+
// For find queries, MongoDB projection uses 1
442+
projection[fieldName] = 1;
443+
444+
// For nested fields, also include the parent field
445+
if (fieldName.includes('.')) {
446+
const parentField = fieldName.split('.')[0];
447+
projection[parentField] = 1;
448+
}
409449
}
410450
});
411451

@@ -764,8 +804,8 @@ export class SqlCompilerImpl implements SqlCompiler {
764804
// This is a field reference, keep it as is
765805
result[key] = value;
766806
} else if (value === 1) {
767-
// For 1 values, convert to field reference
768-
result[key] = `$${key}`;
807+
// For 1 values, keep as 1 for MongoDB's $project stage
808+
result[key] = 1;
769809
} else {
770810
// Otherwise, keep as is
771811
result[key] = value;

0 commit comments

Comments
 (0)