From 7cd4b8ad82d0bccaafae193f546a9f5b59bee307 Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Sun, 22 Nov 2026 03:45:19 +0000 Subject: [PATCH 1/2] test: ignore PG18 arg_location/payload_location/conninfo_location in AST comparisons libpg_query 18-latest (Normalize: take constant locations from the parser) adds DefElem.arg_location, NotifyStmt.payload_location and CreateSubscriptionStmt/AlterSubscriptionStmt.conninfo_location. They are source offsets like location, so round-trip comparisons must strip them. --- packages/deparser/scripts/clean-utils.ts | 3 +++ packages/plpgsql-deparser/test-utils/index.ts | 3 +++ packages/plpgsql-parser/__tests__/round-trip.test.ts | 3 +++ packages/transform-ast/test-utils/clean-tree.ts | 8 +++++++- packages/transform/__tests__/round-trip-core.test.ts | 3 +++ packages/transform/src/round-trip-core.ts | 3 +++ 6 files changed, 22 insertions(+), 1 deletion(-) diff --git a/packages/deparser/scripts/clean-utils.ts b/packages/deparser/scripts/clean-utils.ts index 4d32ebaa..f0dae2ca 100644 --- a/packages/deparser/scripts/clean-utils.ts +++ b/packages/deparser/scripts/clean-utils.ts @@ -64,6 +64,9 @@ export const cleanTree = (tree: any) => { stmt_len: noop, stmt_location: noop, location: noop, + arg_location: noop, + payload_location: noop, + conninfo_location: noop, rexpr_list_start: noop, rexpr_list_end: noop, list_start: noop, diff --git a/packages/plpgsql-deparser/test-utils/index.ts b/packages/plpgsql-deparser/test-utils/index.ts index c66b106b..e4cf69b5 100644 --- a/packages/plpgsql-deparser/test-utils/index.ts +++ b/packages/plpgsql-deparser/test-utils/index.ts @@ -248,6 +248,9 @@ export const transform = (obj: any, props: any): any => { const cleanProps: Record = { lineno: noop, location: noop, + arg_location: noop, + payload_location: noop, + conninfo_location: noop, stmt_len: noop, stmt_location: noop, // varno values are assigned based on position in datums array and can change diff --git a/packages/plpgsql-parser/__tests__/round-trip.test.ts b/packages/plpgsql-parser/__tests__/round-trip.test.ts index 5436977f..d067fd47 100644 --- a/packages/plpgsql-parser/__tests__/round-trip.test.ts +++ b/packages/plpgsql-parser/__tests__/round-trip.test.ts @@ -65,6 +65,9 @@ const cleanSqlTree = (tree: any) => { stmt_len: noop, stmt_location: noop, location: noop, + arg_location: noop, + payload_location: noop, + conninfo_location: noop, }); }; diff --git a/packages/transform-ast/test-utils/clean-tree.ts b/packages/transform-ast/test-utils/clean-tree.ts index 6b6a3779..4ebfbbf1 100644 --- a/packages/transform-ast/test-utils/clean-tree.ts +++ b/packages/transform-ast/test-utils/clean-tree.ts @@ -69,6 +69,9 @@ export const cleanTree = (tree: any) => { stmt_len: noop, stmt_location: noop, location: removeUndefined, + arg_location: noop, + payload_location: noop, + conninfo_location: noop, typemod: removeUndefined, rexpr_list_start: noop, rexpr_list_end: noop, @@ -80,7 +83,10 @@ export const cleanTree = (tree: any) => { export const cleanTreeWithStmt = (tree: any) => { return transform(tree, { stmt_location: noop, - location: noop + location: noop, + arg_location: noop, + payload_location: noop, + conninfo_location: noop }); }; \ No newline at end of file diff --git a/packages/transform/__tests__/round-trip-core.test.ts b/packages/transform/__tests__/round-trip-core.test.ts index c6726e71..db076cac 100644 --- a/packages/transform/__tests__/round-trip-core.test.ts +++ b/packages/transform/__tests__/round-trip-core.test.ts @@ -103,9 +103,12 @@ describe('firstDifference', () => { describe('cleanTree preset (upstream-compatible)', () => { it('exposes the upstream volatile key set', () => { expect([...CLEAN_TREE_VOLATILE_KEYS].sort()).toEqual([ + 'arg_location', + 'conninfo_location', 'list_end', 'list_start', 'location', + 'payload_location', 'rexpr_list_end', 'rexpr_list_start', 'stmt_len', diff --git a/packages/transform/src/round-trip-core.ts b/packages/transform/src/round-trip-core.ts index b1b7d109..0f45d4d5 100644 --- a/packages/transform/src/round-trip-core.ts +++ b/packages/transform/src/round-trip-core.ts @@ -105,6 +105,9 @@ export const CLEAN_TREE_VOLATILE_KEYS: ReadonlySet = new Set([ 'stmt_len', 'stmt_location', 'location', + 'arg_location', + 'payload_location', + 'conninfo_location', 'rexpr_list_start', 'rexpr_list_end', 'list_start', From 725b65a268db00dc7d81ae89842b4a841509e5dd Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Sun, 22 Nov 2026 03:45:19 +0000 Subject: [PATCH 2/2] fix(plpgsql-deparser): keep CONSTANT, NOT NULL and defaults on record declarations PG18 libpg_query serializes PLpgSQL_rec with isconst/notnull/default_val/datatype (record, composite and unresolved custom types). Emit them like PLpgSQL_var and walk PLpgSQL_rec datatype/default_val in walkPlpgsqlAst. --- .../__tests__/deparser-fixes.test.ts | 43 +++++++++++++++++++ .../plpgsql-deparser/src/plpgsql-deparser.ts | 25 +++++++++-- packages/plpgsql-deparser/src/types.ts | 4 ++ .../traverse/__tests__/plpgsql-rec.test.ts | 30 +++++++++++++ packages/traverse/src/plpgsql.ts | 11 ++++- 5 files changed, 109 insertions(+), 4 deletions(-) create mode 100644 packages/traverse/__tests__/plpgsql-rec.test.ts diff --git a/packages/plpgsql-deparser/__tests__/deparser-fixes.test.ts b/packages/plpgsql-deparser/__tests__/deparser-fixes.test.ts index 56f8aaa8..c6a825f1 100644 --- a/packages/plpgsql-deparser/__tests__/deparser-fixes.test.ts +++ b/packages/plpgsql-deparser/__tests__/deparser-fixes.test.ts @@ -1322,6 +1322,49 @@ END$$`; }); }); + describe('record-typed declarations', () => { + // PG18 libpg_query serializes record datums (record, composite and unresolved + // custom types) with isconst/notnull/default_val like scalar variables. + const buildRec = (rec: Record): PLpgSQLParseResult => ({ + plpgsql_funcs: [ + { + PLpgSQL_function: { + datums: [{ PLpgSQL_rec: { refname: 'x', dno: 0, lineno: 2, ...rec } } as any], + action: { PLpgSQL_stmt_block: { body: [] } }, + }, + }, + ], + }); + + it('should keep the default value of a record declaration', () => { + const deparsed = deparseSync( + buildRec({ + datatype: { PLpgSQL_type: { typname: 'record' } }, + default_val: { PLpgSQL_expr: { query: 'row(1,2,3)', parseMode: 2 } }, + }) + ); + + expect(deparsed).toContain('x record := row(1,2,3);'); + }); + + it('should keep CONSTANT and NOT NULL on a custom-typed record declaration', () => { + const deparsed = deparseSync( + buildRec({ + isconst: true, + notnull: true, + datatype: { PLpgSQL_type: { typname: 'int_nn' } }, + default_val: { PLpgSQL_expr: { query: '42', parseMode: 2 } }, + }) + ); + + expect(deparsed).toContain('x CONSTANT int_nn NOT NULL := 42;'); + }); + + it('should fall back to RECORD when no datatype is present', () => { + expect(deparseSync(buildRec({}))).toContain('x RECORD;'); + }); + }); + describe('uppercase %ROWTYPE/%TYPE references', () => { it('should not quote uppercase %ROWTYPE and %TYPE references', async () => { const sql = `CREATE FUNCTION test_rowtype_case() RETURNS void diff --git a/packages/plpgsql-deparser/src/plpgsql-deparser.ts b/packages/plpgsql-deparser/src/plpgsql-deparser.ts index 2bd28dff..5ab45aa4 100644 --- a/packages/plpgsql-deparser/src/plpgsql-deparser.ts +++ b/packages/plpgsql-deparser/src/plpgsql-deparser.ts @@ -833,11 +833,30 @@ export class PLpgSQLDeparser { * Deparse a record declaration */ private deparseRec(rec: PLpgSQL_rec, context: PLpgSQLDeparserContext): string { - const datatype = (rec as { datatype?: PLpgSQLTypeNode }).datatype; + const kw = this.keyword; + const parts: string[] = [rec.refname]; + + if (rec.isconst) { + parts.push(kw('CONSTANT')); + } + + const datatype = rec.datatype; if (datatype && 'PLpgSQL_type' in datatype && datatype.PLpgSQL_type.typname) { - return `${rec.refname} ${this.deparseType(datatype)}`; + parts.push(this.deparseType(datatype)); + } else { + parts.push(kw('RECORD')); + } + + if (rec.notnull) { + parts.push(kw('NOT NULL')); } - return `${rec.refname} ${this.keyword('RECORD')}`; + + if (rec.default_val) { + parts.push(':='); + parts.push(this.deparseExpr(rec.default_val)); + } + + return parts.join(' '); } /** diff --git a/packages/plpgsql-deparser/src/types.ts b/packages/plpgsql-deparser/src/types.ts index 71bb692b..0d3f5965 100644 --- a/packages/plpgsql-deparser/src/types.ts +++ b/packages/plpgsql-deparser/src/types.ts @@ -54,6 +54,10 @@ export interface PLpgSQL_rec { refname: string; dno?: number; lineno?: number; + datatype?: PLpgSQLTypeNode; + default_val?: PLpgSQLExprNode; + isconst?: boolean; + notnull?: boolean; } export interface PLpgSQL_row { diff --git a/packages/traverse/__tests__/plpgsql-rec.test.ts b/packages/traverse/__tests__/plpgsql-rec.test.ts new file mode 100644 index 00000000..b6f0b906 --- /dev/null +++ b/packages/traverse/__tests__/plpgsql-rec.test.ts @@ -0,0 +1,30 @@ +import { walkPlpgsqlAst } from '../src'; + +describe('walkPlpgsqlAst PLpgSQL_rec', () => { + it('visits the datatype and default value of a record declaration', () => { + const rec = { + PLpgSQL_rec: { + refname: 'x', + dno: 0, + datatype: { PLpgSQL_type: { typname: 'record' } }, + default_val: { PLpgSQL_expr: { query: 'row(1,2,3)', parseMode: 2 } }, + }, + }; + const visited: string[] = []; + + walkPlpgsqlAst( + rec, + { + PLpgSQL_type: (path) => { + visited.push(`type:${path.node.typname}`); + }, + PLpgSQL_expr: (path) => { + visited.push(`expr:${path.node.query}`); + }, + }, + { walkSqlExpressions: false } + ); + + expect(visited).toEqual(['type:record', 'expr:row(1,2,3)']); + }); +}); diff --git a/packages/traverse/src/plpgsql.ts b/packages/traverse/src/plpgsql.ts index b6cd7ca6..5579e13b 100644 --- a/packages/traverse/src/plpgsql.ts +++ b/packages/traverse/src/plpgsql.ts @@ -231,6 +231,16 @@ export function walkPlpgsqlAst( break; } + case 'PLpgSQL_rec': { + if (nodeData.datatype) { + walkPlpgsqlAst(nodeData.datatype, cb, opts, parentPath, [...parentPath.keyPath, 'datatype']); + } + if (nodeData.default_val) { + walkPlpgsqlAst(nodeData.default_val, cb, opts, parentPath, [...parentPath.keyPath, 'default_val']); + } + break; + } + case 'PLpgSQL_var': { if (nodeData.datatype) { walkPlpgsqlAst(nodeData.datatype, cb, opts, parentPath, [...parentPath.keyPath, 'datatype']); @@ -611,7 +621,6 @@ export function walkPlpgsqlAst( } // Nodes with no children to traverse - case 'PLpgSQL_rec': case 'PLpgSQL_row': case 'PLpgSQL_recfield': case 'PLpgSQL_type':