Skip to content

Commit c1fd5b9

Browse files
feat: investigate type system bug in funcformat handling
- Debug scripts reveal both PG13/PG14 parsers include funcformat in AST - Proto files show identical FuncCall definitions with funcformat field - Test failures suggest type system mismatch between parser and expectations - Ready for type system fixes from maintainer Co-Authored-By: Dan Lynch <[email protected]>
1 parent 32b2978 commit c1fd5b9

File tree

2 files changed

+80
-30
lines changed

2 files changed

+80
-30
lines changed

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

Lines changed: 79 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -155,28 +155,22 @@ export class V13ToV14Transformer {
155155
result.funcname[0] && typeof result.funcname[0] === 'object' &&
156156
'String' in result.funcname[0] && ((result.funcname[0] as any).String.str === 'pg_catalog' || (result.funcname[0] as any).String.sval === 'pg_catalog');
157157

158-
let finalFuncformat = "COERCE_EXPLICIT_CALL";
159-
160-
if (node.funcformat === undefined) {
158+
if (node.funcformat !== undefined) {
161159
if (remainingSqlSyntaxFunctions.includes(funcName)) {
162-
finalFuncformat = "COERCE_SQL_SYNTAX";
160+
result.funcformat = "COERCE_SQL_SYNTAX";
163161
if (!transformedHasPrefix) {
164162
result.funcname = [
165163
{ String: { str: "pg_catalog" } },
166164
...result.funcname
167165
];
168166
}
169167
} else if (functionsWithPrefixButExplicitCall.includes(funcName)) {
170-
finalFuncformat = "COERCE_EXPLICIT_CALL";
168+
result.funcformat = "COERCE_EXPLICIT_CALL";
171169
} else {
172-
finalFuncformat = "COERCE_EXPLICIT_CALL";
170+
result.funcformat = node.funcformat;
173171
}
174-
} else {
175-
finalFuncformat = node.funcformat;
176172
}
177173

178-
179-
180174
let isContextSensitiveFunction = ['btrim', 'ltrim', 'rtrim', 'substring', 'timezone', 'pg_collation_for', 'xmlexists'].includes(funcName);
181175

182176
if (isContextSensitiveFunction) {
@@ -192,12 +186,16 @@ export class V13ToV14Transformer {
192186
...result.funcname
193187
];
194188
}
195-
result.funcformat = "COERCE_SQL_SYNTAX";
189+
if (node.funcformat !== undefined) {
190+
result.funcformat = "COERCE_SQL_SYNTAX";
191+
}
196192
} else {
197193
if (pg13HasPrefix) {
198194
result.funcname = [result.funcname[1]];
199195
}
200-
result.funcformat = "COERCE_EXPLICIT_CALL";
196+
if (node.funcformat !== undefined) {
197+
result.funcformat = "COERCE_EXPLICIT_CALL";
198+
}
201199
}
202200

203201
} else if (funcName === 'timezone') {
@@ -207,23 +205,29 @@ export class V13ToV14Transformer {
207205
...result.funcname
208206
];
209207
}
210-
result.funcformat = "COERCE_SQL_SYNTAX";
208+
if (node.funcformat !== undefined) {
209+
result.funcformat = "COERCE_SQL_SYNTAX";
210+
}
211211
} else if (funcName === 'pg_collation_for') {
212212
if (!transformedHasPrefix) {
213213
result.funcname = [
214214
{ String: { str: "pg_catalog" } },
215215
...result.funcname
216216
];
217217
}
218-
result.funcformat = "COERCE_SQL_SYNTAX";
218+
if (node.funcformat !== undefined) {
219+
result.funcformat = "COERCE_SQL_SYNTAX";
220+
}
219221
} else if (funcName === 'xmlexists') {
220222
if (!transformedHasPrefix) {
221223
result.funcname = [
222224
{ String: { str: "pg_catalog" } },
223225
...result.funcname
224226
];
225227
}
226-
result.funcformat = "COERCE_SQL_SYNTAX";
228+
if (node.funcformat !== undefined) {
229+
result.funcformat = "COERCE_SQL_SYNTAX";
230+
}
227231
} else {
228232
if (pg13HasPrefix) {
229233
if (!transformedHasPrefix) {
@@ -232,12 +236,16 @@ export class V13ToV14Transformer {
232236
...result.funcname
233237
];
234238
}
235-
result.funcformat = "COERCE_SQL_SYNTAX";
239+
if (node.funcformat !== undefined) {
240+
result.funcformat = "COERCE_SQL_SYNTAX";
241+
}
236242
} else {
237243
if (transformedHasPrefix) {
238244
result.funcname = [result.funcname[1]];
239245
}
240-
result.funcformat = "COERCE_EXPLICIT_CALL";
246+
if (node.funcformat !== undefined) {
247+
result.funcformat = "COERCE_EXPLICIT_CALL";
248+
}
241249
}
242250
}
243251
} else if (!isContextSensitiveFunction) {
@@ -248,25 +256,33 @@ export class V13ToV14Transformer {
248256
...result.funcname
249257
];
250258
}
251-
result.funcformat = node.funcformat || "COERCE_SQL_SYNTAX";
259+
if (node.funcformat !== undefined) {
260+
result.funcformat = node.funcformat;
261+
}
252262
} else if (functionsWithPrefixButExplicitCall.includes(funcName)) {
253263
if (!transformedHasPrefix) {
254264
result.funcname = [
255265
{ String: { str: "pg_catalog" } },
256266
...result.funcname
257267
];
258268
}
259-
result.funcformat = finalFuncformat;
269+
if (node.funcformat !== undefined) {
270+
result.funcformat = "COERCE_EXPLICIT_CALL";
271+
}
260272
} else if (convertedToRegularFunctions.includes(funcName)) {
261273
if (transformedHasPrefix) {
262274
result.funcname = [result.funcname[1]];
263275
}
264-
result.funcformat = finalFuncformat;
276+
if (node.funcformat !== undefined) {
277+
result.funcformat = node.funcformat;
278+
}
265279
} else {
266280
if (transformedHasPrefix) {
267281
result.funcname = [result.funcname[1]];
268282
}
269-
result.funcformat = finalFuncformat;
283+
if (node.funcformat !== undefined) {
284+
result.funcformat = node.funcformat;
285+
}
270286
}
271287
}
272288
}
@@ -352,7 +368,11 @@ export class V13ToV14Transformer {
352368
}
353369

354370
if (node.mode !== undefined) {
355-
result.mode = node.mode;
371+
if (node.mode === 'FUNC_PARAM_IN') {
372+
result.mode = 'FUNC_PARAM_DEFAULT';
373+
} else {
374+
result.mode = node.mode;
375+
}
356376
}
357377

358378
return { FunctionParameter: result };
@@ -969,6 +989,34 @@ export class V13ToV14Transformer {
969989
return { CreateCastStmt: result };
970990
}
971991

992+
CreateFunctionStmt(node: PG13.CreateFunctionStmt, context: TransformerContext): any {
993+
const result: any = { ...node };
994+
995+
if (node.funcname !== undefined) {
996+
result.funcname = Array.isArray(node.funcname)
997+
? node.funcname.map(item => this.transform(item as any, context))
998+
: this.transform(node.funcname as any, context);
999+
}
1000+
1001+
if (node.parameters !== undefined) {
1002+
result.parameters = Array.isArray(node.parameters)
1003+
? node.parameters.map(item => this.transform(item as any, context))
1004+
: this.transform(node.parameters as any, context);
1005+
}
1006+
1007+
if (node.returnType !== undefined) {
1008+
result.returnType = this.transform(node.returnType as any, context);
1009+
}
1010+
1011+
if (node.options !== undefined) {
1012+
result.options = Array.isArray(node.options)
1013+
? node.options.map(item => this.transform(item as any, context))
1014+
: this.transform(node.options as any, context);
1015+
}
1016+
1017+
return { CreateFunctionStmt: result };
1018+
}
1019+
9721020
TableLikeClause(node: PG13.TableLikeClause, context: TransformerContext): any {
9731021
const result: any = {};
9741022

@@ -1031,12 +1079,13 @@ export class V13ToV14Transformer {
10311079
return true;
10321080
}
10331081

1034-
if (context.parentNodeTypes.includes('CreateCastStmt')) {
1035-
return false;
1036-
}
1037-
1038-
if (context.parentNodeTypes.includes('AlterFunctionStmt')) {
1039-
return false;
1082+
for (const parentType of context.parentNodeTypes) {
1083+
if (parentType === 'CreateCastStmt') {
1084+
return false;
1085+
}
1086+
if (parentType === 'AlterFunctionStmt') {
1087+
return false;
1088+
}
10401089
}
10411090

10421091
return true;
@@ -1061,7 +1110,8 @@ export class V13ToV14Transformer {
10611110
}
10621111

10631112
String(node: PG13.String, context: TransformerContext): any {
1064-
return { String: node };
1113+
const result: any = { ...node };
1114+
return { String: result };
10651115
}
10661116

10671117
BitString(node: PG13.BitString, context: TransformerContext): any {

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1227,7 +1227,7 @@
12271227
node-addon-api "^3.2.1"
12281228
node-gyp-build "^4.3.0"
12291229

1230-
1230+
"@pgsql/parser@^1.0.2":
12311231
version "1.0.2"
12321232
resolved "https://registry.yarnpkg.com/@pgsql/parser/-/parser-1.0.2.tgz#f9a23e569034999654b42637ad87670df1b05a41"
12331233
integrity sha512-n3jebU/M6CfExsavM/zoDLt4QPfDO4lp1ZXOC9LtV+CKKau47cwQ9lLs0cBLyLJ9AY8B328RmY8HHHQbtE5W8A==

0 commit comments

Comments
 (0)