Skip to content

fix(plpgsql-deparser): preserve ALIAS FOR declarations#308

Closed
pyramation wants to merge 1 commit into
mainfrom
fix/plpgsql-alias-for
Closed

fix(plpgsql-deparser): preserve ALIAS FOR declarations#308
pyramation wants to merge 1 commit into
mainfrom
fix/plpgsql-alias-for

Conversation

@pyramation

Copy link
Copy Markdown
Collaborator

Summary

Closes the last known PL/pgSQL round-trip gap: ALIAS FOR declarations were dropped entirely (the alias lives only in the PL/pgSQL compiler's namespace, which libpg_query never serialized), so deparsed bodies referenced undeclared names:

DECLARE arg ALIAS FOR $1;      -- input
BEGIN RETURN arg + 1; END      -- output: no declaration, `arg` is undefined

The fork (constructive-io/libpg_query 17-constructive + 18-constructive) now records aliases from the grammar's ALIAS production and emits them on PLpgSQL_function:

"aliases":[{"PLpgSQL_alias":{"name":"arg","varno":0,"lineno":2}}]

This PR teaches plpgsql-deparser to consume that:

  • new PLpgSQL_alias type ({name, varno, lineno}) on PLpgSQL_function.aliases
  • deparseDeclareSection emits name ALIAS FOR <datums[varno].refname>; interleaved with variable declarations by source lineno (variables win ties), so an alias of a local variable follows its target's declaration
  • fixtures 61–63: ALIAS FOR $1, named-param + local-var aliases (interleaving), ALIAS FOR old/new in triggers; snapshot tests in deparser-fixes.test.ts

⚠️ Merge order: the new fixtures require @libpg-query/parser ≥ 17.8.1 (with the aliases field) — CI will fail on the ALIAS fixtures until that's published from the updated 17-constructive pin. All suites pass locally against the locally-built WASM (deparser 713, transform 1126, plpgsql-deparser 109, plpgsql-parser 296, plpgsql-parse 57, parse 68).

Link to Devin session: https://app.devin.ai/sessions/eeb8deba0c04475091df703c88877302
Requested by: @pyramation

…metadata

The parser (libpg_query 17-constructive) now serializes ALIAS declarations
as an aliases array on PLpgSQL_function ({name, varno, lineno}); previously
the alias name existed only in the compiler namespace and was dropped, so
deparsed bodies referenced undeclared names. Alias declarations are
interleaved with variable declarations by source lineno (variables first on
ties) so aliases of local variables follow their target's declaration.

Fixtures 61-63 cover positional-param, named-param/local-var, and OLD/NEW
trigger aliases. Requires @libpg-query/parser >= 17.8.1 (aliases field).
@pyramation pyramation self-assigned this Jul 21, 2026
@devin-ai-integration

Copy link
Copy Markdown
Contributor

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment, CI, and merge conflict monitoring

@devin-ai-integration

Copy link
Copy Markdown
Contributor

Superseded by #312, which was rebuilt fresh on current PG18 main and merged. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant