Skip to content

Commit db817f0

Browse files
feat: implement surgical funcformat exclusion for aggregate functions in TypeCast contexts
Co-Authored-By: Dan Lynch <[email protected]>
1 parent ec863f5 commit db817f0

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@ export class V13ToV14Transformer {
172172
return false;
173173
}
174174

175+
if (this.isInTypeCastContext(context) && this.isAggregateFunction(context)) {
176+
return false;
177+
}
178+
175179
return true;
176180
}
177181

@@ -224,6 +228,41 @@ export class V13ToV14Transformer {
224228
);
225229
}
226230

231+
private isInTypeCastContext(context: TransformerContext): boolean {
232+
const path = context.path || [];
233+
return path.some((node: any) =>
234+
node && typeof node === 'object' && 'TypeCast' in node
235+
);
236+
}
237+
238+
private isInInsertContext(context: TransformerContext): boolean {
239+
const path = context.path || [];
240+
return path.some((node: any) =>
241+
node && typeof node === 'object' && 'InsertStmt' in node
242+
);
243+
}
244+
245+
private isInUpdateContext(context: TransformerContext): boolean {
246+
const path = context.path || [];
247+
return path.some((node: any) =>
248+
node && typeof node === 'object' && 'UpdateStmt' in node
249+
);
250+
}
251+
252+
private isAggregateFunction(context: TransformerContext): boolean {
253+
if (context.currentNode && 'FuncCall' in context.currentNode) {
254+
const funcCall = context.currentNode.FuncCall;
255+
if (funcCall?.funcname && Array.isArray(funcCall.funcname)) {
256+
const lastName = funcCall.funcname[funcCall.funcname.length - 1];
257+
if (lastName && 'String' in lastName) {
258+
const name = lastName.String.str || lastName.String.sval;
259+
return ['avg', 'sum', 'count', 'min', 'max', 'stddev', 'variance'].includes(name);
260+
}
261+
}
262+
}
263+
return false;
264+
}
265+
227266
CallStmt(node: PG13.CallStmt, context: TransformerContext): any {
228267
const result: any = { ...node };
229268

0 commit comments

Comments
 (0)