@@ -402,6 +402,8 @@ export class SqlCompilerImpl implements SqlCompiler {
402402 private processFieldName ( fieldName : string ) : string {
403403 if ( ! fieldName ) return fieldName ;
404404
405+ console . log ( `Processing field name: "${ fieldName } "` ) ;
406+
405407 // First convert our placeholder format back to MongoDB dot notation
406408 // This transforms items__ARRAY_0__name => items.0.name
407409 let processed = fieldName . replace ( / _ _ A R R A Y _ ( \d + ) _ _ / g, '.$1.' ) ;
@@ -417,6 +419,12 @@ export class SqlCompilerImpl implements SqlCompiler {
417419 // This handles any direct [0] syntax that might have made it through the parser
418420 processed = processed . replace ( / \[ ( \d + ) \] / g, '.$1' ) ;
419421
422+ // Handle nested field access directly
423+ // MongoDB already uses dot notation for nested fields, so we can use it as is
424+ if ( processed . includes ( '.' ) ) {
425+ console . log ( `Using nested field in MongoDB filter: "${ processed } "` ) ;
426+ }
427+
420428 return processed ;
421429 }
422430
@@ -426,6 +434,8 @@ export class SqlCompilerImpl implements SqlCompiler {
426434 * address.zip might be parsed as table "address", column "zip"
427435 */
428436 private handleNestedFieldReferences ( ast : any ) : void {
437+ console . log ( 'Handling nested field references in AST' ) ;
438+
429439 // Handle column references in SELECT clause
430440 if ( ast . columns && Array . isArray ( ast . columns ) ) {
431441 ast . columns . forEach ( ( column : any ) => {
@@ -434,12 +444,16 @@ export class SqlCompilerImpl implements SqlCompiler {
434444 // This could be a nested field - convert table.column to a single column path
435445 column . expr . column = `${ column . expr . table } .${ column . expr . column } ` ;
436446 column . expr . table = null ;
447+ console . log ( `Converted SELECT column to nested field: ${ column . expr . column } ` ) ;
437448 }
438449 } ) ;
439450 }
440451
441452 // Handle conditions in WHERE clause
442453 this . processWhereClauseForNestedFields ( ast . where ) ;
454+
455+ // For debugging - show the resulting AST after transformation
456+ console . log ( 'AST after nested field handling:' , JSON . stringify ( ast ?. where , null , 2 ) ) ;
443457 }
444458
445459 /**
@@ -448,17 +462,28 @@ export class SqlCompilerImpl implements SqlCompiler {
448462 private processWhereClauseForNestedFields ( where : any ) : void {
449463 if ( ! where ) return ;
450464
465+ console . log ( 'Processing WHERE clause for nested fields:' , JSON . stringify ( where , null , 2 ) ) ;
466+
451467 if ( where . type === 'binary_expr' ) {
452468 // Process left and right sides recursively
453469 this . processWhereClauseForNestedFields ( where . left ) ;
454470 this . processWhereClauseForNestedFields ( where . right ) ;
455471
456472 // Handle column references in comparison expressions
457- if ( where . left && where . left . type === 'column_ref' &&
458- where . left . table && where . left . column ) {
459- // Convert table.column format to a nested field path
460- where . left . column = `${ where . left . table } .${ where . left . column } ` ;
461- where . left . table = null ;
473+ if ( where . left && where . left . type === 'column_ref' ) {
474+ console . log ( 'Processing column reference:' , JSON . stringify ( where . left , null , 2 ) ) ;
475+
476+ // Handle both direct dot notation in column name and table.column format
477+ if ( where . left . column && where . left . column . includes ( '.' ) ) {
478+ // Already has dot notation, just keep it
479+ console . log ( 'Column already has dot notation:' , where . left . column ) ;
480+ } else if ( where . left . table && where . left . column ) {
481+ // Convert table.column format to a nested field path
482+ console . log ( 'Converting table.column to nested path:' ,
483+ `${ where . left . table } .${ where . left . column } ` ) ;
484+ where . left . column = `${ where . left . table } .${ where . left . column } ` ;
485+ where . left . table = null ;
486+ }
462487 }
463488 } else if ( where . type === 'unary_expr' ) {
464489 // Process expression in unary operators
0 commit comments