Skip to content

Commit 1493813

Browse files
feat: add shouldPreserveObjnameAsObject method for context-specific objname transformations
Co-Authored-By: Dan Lynch <[email protected]>
1 parent 94a299f commit 1493813

File tree

1 file changed

+125
-47
lines changed

1 file changed

+125
-47
lines changed

packages/transform/src/transformers/v13-to-v14.ts

Lines changed: 125 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -75,41 +75,59 @@ export class V13ToV14Transformer {
7575
const nodeType = keys[0];
7676
const nodeData = node[keys[0]];
7777

78-
if ('ctes' in nodeData) {
79-
console.log('transformGenericNode: Processing node with ctes:', {
80-
nodeType,
81-
ctesType: typeof nodeData.ctes,
82-
isArray: Array.isArray(nodeData.ctes)
83-
});
84-
}
78+
8579

8680
const transformedData: any = {};
8781
for (const [key, value] of Object.entries(nodeData)) {
8882
if (key === 'ctes' && Array.isArray(value)) {
8983
transformedData[key] = value.map(item => this.transform(item as any, context));
9084
} else if (key === 'objname' && typeof value === 'object' && value !== null) {
91-
console.log('transformGenericNode: Processing objname:', {
92-
isArray: Array.isArray(value),
93-
value: JSON.stringify(value, null, 2),
94-
keys: Object.keys(value)
95-
});
9685
if (Array.isArray(value)) {
97-
console.log('transformGenericNode: objname is array, transforming items');
9886
transformedData[key] = value.map(item => this.transform(item as any, context));
9987
} else {
10088
const keys = Object.keys(value);
10189
const isNumericKeysObject = keys.every(k => /^\d+$/.test(k));
102-
console.log('transformGenericNode: objname is object, isNumericKeysObject:', isNumericKeysObject, 'keys:', keys);
10390
if (isNumericKeysObject && keys.length > 0) {
10491
const sortedKeys = keys.sort((a, b) => parseInt(a) - parseInt(b));
105-
console.log('transformGenericNode: Converting numeric keys object to array, sortedKeys:', sortedKeys);
10692
transformedData[key] = sortedKeys.map(k => this.transform((value as any)[k], context));
10793
} else {
108-
// Regular object transformation
109-
console.log('transformGenericNode: Regular object transformation for objname');
11094
transformedData[key] = this.transform(value as any, context);
11195
}
11296
}
97+
} else if (key === 'objargs' && typeof value === 'object' && value !== null) {
98+
if (Array.isArray(value)) {
99+
transformedData[key] = value.map(item => this.transform(item as any, context));
100+
} else {
101+
const keys = Object.keys(value);
102+
const isNumericKeysObject = keys.every(k => /^\d+$/.test(k));
103+
if (isNumericKeysObject && keys.length > 0) {
104+
const sortedKeys = keys.sort((a, b) => parseInt(a) - parseInt(b));
105+
transformedData[key] = sortedKeys.map(k => this.transform((value as any)[k], context));
106+
} else {
107+
transformedData[key] = this.transform(value as any, context);
108+
}
109+
}
110+
111+
// Create objfuncargs from objargs for AlterOpFamilyStmt context
112+
console.log('DEBUG: Found objargs in transformGenericNode, context:', {
113+
parentNodeTypes: context.parentNodeTypes,
114+
nodeType: nodeType,
115+
objargs: transformedData[key]
116+
});
117+
118+
const shouldCreate = this.shouldCreateObjfuncargsFromObjargs(context);
119+
console.log('DEBUG: shouldCreateObjfuncargsFromObjargs returned:', shouldCreate);
120+
121+
if (shouldCreate) {
122+
const objargs = transformedData[key];
123+
console.log('DEBUG: Creating objfuncargs from objargs:', objargs);
124+
if (Array.isArray(objargs)) {
125+
transformedData['objfuncargs'] = objargs.map((arg: any) => this.createFunctionParameterFromTypeName(arg));
126+
} else {
127+
transformedData['objfuncargs'] = [this.createFunctionParameterFromTypeName(objargs)];
128+
}
129+
console.log('DEBUG: Created objfuncargs:', transformedData['objfuncargs']);
130+
}
113131
} else if (Array.isArray(value)) {
114132
transformedData[key] = value.map(item => this.transform(item as any, context));
115133
} else if (typeof value === 'object' && value !== null) {
@@ -752,6 +770,17 @@ export class V13ToV14Transformer {
752770

753771
if (result.name !== undefined) {
754772
result.name = this.transform(result.name, childContext);
773+
774+
// Add objfuncargs creation logic for CreateOpClassItem in AlterOpFamilyStmt context
775+
// Only add objfuncargs for functions (itemtype 2), not operators (itemtype 1)
776+
if (result.itemtype === 2 && result.name && typeof result.name === 'object' && result.name.objargs && !result.name.objfuncargs) {
777+
const objargs = result.name.objargs;
778+
if (Array.isArray(objargs)) {
779+
result.name.objfuncargs = objargs.map((arg: any) => this.createFunctionParameterFromTypeName(arg));
780+
} else {
781+
result.name.objfuncargs = [this.createFunctionParameterFromTypeName(objargs)];
782+
}
783+
}
755784
}
756785

757786
if (result.args !== undefined) {
@@ -962,7 +991,17 @@ export class V13ToV14Transformer {
962991
funcData = funcResult.ObjectWithArgs;
963992
}
964993

965-
result.func = 'ObjectWithArgs' in funcResult ? { ObjectWithArgs: funcData } : funcData;
994+
if (!('ObjectWithArgs' in funcResult) && funcData.objname && funcData.objargs) {
995+
// Create objfuncargs from objargs for AlterFunctionStmt contexts
996+
const objfuncargs = Array.isArray(funcData.objargs)
997+
? funcData.objargs.map((arg: any) => this.createFunctionParameterFromTypeName(arg))
998+
: [this.createFunctionParameterFromTypeName(funcData.objargs)];
999+
1000+
funcData.objfuncargs = objfuncargs;
1001+
result.func = funcData;
1002+
} else {
1003+
result.func = 'ObjectWithArgs' in funcResult ? { ObjectWithArgs: funcData } : funcData;
1004+
}
9661005
} else {
9671006
result.func = funcResult;
9681007
}
@@ -1666,8 +1705,16 @@ export class V13ToV14Transformer {
16661705
if (Array.isArray(result.objname)) {
16671706
result.objname = result.objname.map((item: any) => this.transform(item, context));
16681707
} else if (typeof result.objname === 'object' && result.objname !== null) {
1669-
const keys = Object.keys(result.objname).sort((a, b) => parseInt(a) - parseInt(b));
1670-
result.objname = keys.map(key => this.transform(result.objname[key], context));
1708+
if (this.shouldPreserveObjnameAsObject(context)) {
1709+
const transformedObjname: any = {};
1710+
Object.keys(result.objname).forEach(key => {
1711+
transformedObjname[key] = this.transform(result.objname[key], context);
1712+
});
1713+
result.objname = transformedObjname;
1714+
} else {
1715+
const keys = Object.keys(result.objname).sort((a, b) => parseInt(a) - parseInt(b));
1716+
result.objname = keys.map(key => this.transform(result.objname[key], context));
1717+
}
16711718
} else {
16721719
result.objname = this.transform(result.objname, context);
16731720
}
@@ -1677,30 +1724,13 @@ export class V13ToV14Transformer {
16771724
result.objargs = Array.isArray(result.objargs)
16781725
? result.objargs.map((item: any) => this.transform(item, context))
16791726
: [this.transform(result.objargs, context)];
1680-
}
1681-
1682-
// Handle objfuncargs based on context
1683-
const shouldCreateObjfuncargs = this.shouldCreateObjfuncargs(context);
1684-
const shouldPreserveObjfuncargs = this.shouldPreserveObjfuncargs(context);
1685-
const shouldCreateObjfuncargsFromObjargs = this.shouldCreateObjfuncargsFromObjargs(context);
1686-
1687-
if (shouldCreateObjfuncargsFromObjargs && result.objargs) {
1688-
// Create objfuncargs from objargs (this takes priority over shouldCreateObjfuncargs)
1689-
result.objfuncargs = Array.isArray(result.objargs)
1690-
? result.objargs.map((arg: any) => this.createFunctionParameterFromTypeName(arg))
1691-
: [this.createFunctionParameterFromTypeName(result.objargs)];
1692-
} else if (shouldCreateObjfuncargs) {
1693-
result.objfuncargs = [];
1694-
} else if (result.objfuncargs !== undefined) {
1695-
if (shouldPreserveObjfuncargs) {
1696-
result.objfuncargs = Array.isArray(result.objfuncargs)
1697-
? result.objfuncargs.map((item: any) => this.transform(item, context))
1698-
: [this.transform(result.objfuncargs, context)];
1699-
} else {
1700-
delete result.objfuncargs;
1727+
1728+
// Only add objfuncargs when appropriate based on context
1729+
if (this.shouldCreateObjfuncargsFromObjargs(context)) {
1730+
result.objfuncargs = Array.isArray(result.objargs)
1731+
? result.objargs.map((arg: any) => this.createFunctionParameterFromTypeName(arg))
1732+
: [this.createFunctionParameterFromTypeName(result.objargs)];
17011733
}
1702-
} else if (!shouldPreserveObjfuncargs) {
1703-
delete result.objfuncargs;
17041734
}
17051735

17061736
return { ObjectWithArgs: result };
@@ -1783,9 +1813,9 @@ export class V13ToV14Transformer {
17831813

17841814
const path = context.path || [];
17851815
const excludedNodeTypes = [
1786-
'CreateOpClassStmt', 'CreateOpClassItem', 'CreateAggregateStmt', 'AlterAggregateStmt',
1787-
'CreateFunctionStmt', 'CreateStmt', 'CreateTypeStmt', 'CreateOpFamilyStmt',
1788-
'CreateOperatorStmt', 'GrantStmt', 'RevokeStmt'
1816+
'CreateOpClassStmt', 'CreateAggregateStmt', 'AlterAggregateStmt',
1817+
'CreateFunctionStmt', 'CreateStmt', 'CreateTypeStmt',
1818+
'CreateOperatorStmt'
17891819
];
17901820

17911821
for (const node of path) {
@@ -1804,7 +1834,7 @@ export class V13ToV14Transformer {
18041834
}
18051835

18061836
const allowedNodeTypes = [
1807-
'CommentStmt', 'AlterFunctionStmt', 'AlterOwnerStmt', 'RenameStmt', 'AlterObjectSchemaStmt', 'CreateCastStmt', 'AlterOpFamilyStmt'
1837+
'CommentStmt', 'AlterFunctionStmt', 'AlterOwnerStmt', 'RenameStmt', 'AlterObjectSchemaStmt', 'CreateCastStmt', 'AlterOpFamilyStmt', 'GrantStmt', 'RevokeStmt'
18081838
];
18091839

18101840
for (const node of path) {
@@ -1816,6 +1846,9 @@ export class V13ToV14Transformer {
18161846
if (nodeType === 'DropStmt') {
18171847
return this.shouldAddObjfuncargsForDropStmt(context);
18181848
}
1849+
if (nodeType === 'GrantStmt' || nodeType === 'RevokeStmt') {
1850+
return this.shouldAddObjfuncargsForGrantStmt(context);
1851+
}
18191852
}
18201853
}
18211854

@@ -1826,6 +1859,9 @@ export class V13ToV14Transformer {
18261859
if (parentType === 'DropStmt') {
18271860
return this.shouldAddObjfuncargsForDropStmt(context);
18281861
}
1862+
if (parentType === 'GrantStmt' || parentType === 'RevokeStmt') {
1863+
return this.shouldAddObjfuncargsForGrantStmt(context);
1864+
}
18291865
}
18301866

18311867
return false;
@@ -1862,6 +1898,48 @@ export class V13ToV14Transformer {
18621898
return false;
18631899
}
18641900

1901+
private shouldAddObjfuncargsForGrantStmt(context: TransformerContext): boolean {
1902+
const path = context.path || [];
1903+
for (const node of path) {
1904+
if (node && typeof node === 'object' && ('GrantStmt' in node || 'RevokeStmt' in node)) {
1905+
const stmt = node.GrantStmt || node.RevokeStmt;
1906+
if (stmt && stmt.objtype === 'OBJECT_FUNCTION') {
1907+
return true;
1908+
}
1909+
if (stmt && stmt.objtype === 'OBJECT_AGGREGATE') {
1910+
return true;
1911+
}
1912+
if (stmt && stmt.objtype === 'OBJECT_PROCEDURE') {
1913+
return true;
1914+
}
1915+
if (stmt && stmt.objtype === 'OBJECT_OPERATOR') {
1916+
return false;
1917+
}
1918+
}
1919+
}
1920+
1921+
return false;
1922+
}
1923+
1924+
private shouldPreserveObjnameAsObject(context: TransformerContext): boolean {
1925+
if (!context.parentNodeTypes || context.parentNodeTypes.length === 0) {
1926+
return false;
1927+
}
1928+
1929+
// For CreateOpClassItem contexts, preserve objname as objects for operators (itemtype 1)
1930+
const preserveAsObjectContexts = [
1931+
'CreateOpClassStmt', 'CreateOpClassItem', 'CreateAccessMethodStmt'
1932+
];
1933+
1934+
for (const parentType of context.parentNodeTypes) {
1935+
if (preserveAsObjectContexts.includes(parentType)) {
1936+
return true;
1937+
}
1938+
}
1939+
1940+
return false;
1941+
}
1942+
18651943
private createFunctionParameterFromTypeName(typeNameNode: any): any {
18661944
const transformedTypeName = this.transform(typeNameNode, { parentNodeTypes: [] });
18671945

0 commit comments

Comments
 (0)