@@ -454,9 +454,41 @@ export class V16ToV17Transformer {
454454 const result : any = { } ;
455455
456456 if ( node . names !== undefined ) {
457- result . names = Array . isArray ( node . names )
457+ let names = Array . isArray ( node . names )
458458 ? node . names . map ( item => this . transform ( item as any , context ) )
459459 : this . transform ( node . names as any , context ) ;
460+
461+ // Add pg_catalog prefix for JSON types in CREATE TABLE contexts
462+ if ( Array . isArray ( names ) && names . length === 1 ) {
463+ const firstElement = names [ 0 ] ;
464+ if ( firstElement && typeof firstElement === 'object' && 'String' in firstElement ) {
465+ const typeNameStr = firstElement . String . str || firstElement . String . sval ;
466+ if ( typeNameStr === 'json' ) {
467+ const hasCreateStmt = context . parentNodeTypes . includes ( 'CreateStmt' ) ;
468+ const hasCompositeTypeStmt = context . parentNodeTypes . includes ( 'CompositeTypeStmt' ) ;
469+ const hasRangeFunction = context . parentNodeTypes . includes ( 'RangeFunction' ) ;
470+ const hasCreateDomainStmt = context . parentNodeTypes . includes ( 'CreateDomainStmt' ) ;
471+ const hasColumnDef = context . parentNodeTypes . includes ( 'ColumnDef' ) ;
472+ if ( ( hasCreateStmt || hasCompositeTypeStmt || hasRangeFunction ) && hasColumnDef ) {
473+ const pgCatalogElement = {
474+ String : {
475+ sval : 'pg_catalog'
476+ }
477+ } ;
478+ names = [ pgCatalogElement , firstElement ] ;
479+ } else if ( hasCreateDomainStmt ) {
480+ const pgCatalogElement = {
481+ String : {
482+ sval : 'pg_catalog'
483+ }
484+ } ;
485+ names = [ pgCatalogElement , firstElement ] ;
486+ }
487+ }
488+ }
489+ }
490+
491+ result . names = names ;
460492 }
461493
462494 if ( node . typeOid !== undefined ) {
@@ -557,7 +589,31 @@ export class V16ToV17Transformer {
557589 result . arg = this . transform ( node . arg as any , context ) ;
558590 }
559591 if ( node . typeName !== undefined ) {
560- result . typeName = this . transform ( node . typeName as any , context ) ;
592+ let typeName = this . transform ( node . typeName as any , context ) ;
593+
594+ // Add pg_catalog prefix for JSON types in simple SELECT contexts
595+ if ( typeName && typeName . names && Array . isArray ( typeName . names ) && typeName . names . length === 1 ) {
596+ const firstElement = typeName . names [ 0 ] ;
597+ if ( firstElement && typeof firstElement === 'object' && 'String' in firstElement ) {
598+ const typeNameStr = firstElement . String . str || firstElement . String . sval ;
599+ if ( typeNameStr === 'json' ) {
600+ const hasSelectStmt = context . parentNodeTypes . includes ( 'SelectStmt' ) ;
601+ const hasResTarget = context . parentNodeTypes . includes ( 'ResTarget' ) ;
602+ const hasList = context . parentNodeTypes . includes ( 'List' ) ;
603+ const hasA_Expr = context . parentNodeTypes . includes ( 'A_Expr' ) ;
604+ if ( ( hasSelectStmt && hasResTarget ) || ( hasSelectStmt && hasList ) || hasA_Expr ) {
605+ const pgCatalogElement = {
606+ String : {
607+ sval : 'pg_catalog'
608+ }
609+ } ;
610+ typeName . names = [ pgCatalogElement , firstElement ] ;
611+ }
612+ }
613+ }
614+ }
615+
616+ result . typeName = typeName ;
561617 }
562618 if ( node . location !== undefined ) {
563619 result . location = node . location ;
0 commit comments