Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/deparser/scripts/clean-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
43 changes: 43 additions & 0 deletions packages/plpgsql-deparser/__tests__/deparser-fixes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown>): 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
Expand Down
25 changes: 22 additions & 3 deletions packages/plpgsql-deparser/src/plpgsql-deparser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(' ');
}

/**
Expand Down
4 changes: 4 additions & 0 deletions packages/plpgsql-deparser/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions packages/plpgsql-deparser/test-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@ export const transform = (obj: any, props: any): any => {
const cleanProps: Record<string, any> = {
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
Expand Down
3 changes: 3 additions & 0 deletions packages/plpgsql-parser/__tests__/round-trip.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
};

Expand Down
8 changes: 7 additions & 1 deletion packages/transform-ast/test-utils/clean-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
});
};

3 changes: 3 additions & 0 deletions packages/transform/__tests__/round-trip-core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
3 changes: 3 additions & 0 deletions packages/transform/src/round-trip-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ export const CLEAN_TREE_VOLATILE_KEYS: ReadonlySet<string> = new Set([
'stmt_len',
'stmt_location',
'location',
'arg_location',
'payload_location',
'conninfo_location',
'rexpr_list_start',
'rexpr_list_end',
'list_start',
Expand Down
30 changes: 30 additions & 0 deletions packages/traverse/__tests__/plpgsql-rec.test.ts
Original file line number Diff line number Diff line change
@@ -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)']);
});
});
11 changes: 10 additions & 1 deletion packages/traverse/src/plpgsql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down Expand Up @@ -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':
Expand Down
Loading