Skip to content

Conversation

@pyramation
Copy link
Collaborator

feat(plpgsql-deparser): enable heterogeneous deparse for AST-based transformations

Summary

This PR enables AST-based transformations (e.g., schema renaming) to be properly reflected in deparsed PL/pgSQL function bodies. Previously, modifications to AST nodes in hydrated expressions were ignored because dehydrateQuery() returned the original string fields instead of deparsing the modified AST.

Key changes:

  • Added deparseExprNode() helper that wraps expression AST nodes in a SELECT statement, deparses, and strips the prefix
  • Updated dehydrateQuery() for sql-expr kind to always prefer deparsing the AST node
  • Updated dehydrateQuery() for assign kind to prefer deparsing targetExpr/valueExpr AST nodes when available
  • Updated hydrate-demo.test.ts to demonstrate proper AST-based modifications (instead of string field modifications)
  • Added tests for schema renaming across all hydrated expression kinds

⚠️ Breaking change: String-based modifications to query.target, query.value, and query.original are now ignored if AST nodes exist. Users should modify AST nodes directly for transformations.

Review & Testing Checklist for Human

  • Verify breaking change is acceptable: Previously, modifying string fields like query.original would work. Now AST modifications are preferred. Confirm this behavioral change is intentional and acceptable for downstream users.
  • Check dead code: The normalizeForComparison() function is defined but appears unused in the final implementation - should it be removed?
  • Test with real PL/pgSQL functions: The snapshot shows formatting differences (e.g., ::typeCAST(... AS type), multi-line OR conditions). Verify these don't break downstream consumers.
  • Verify type reference limitation: Type references in DECLARE sections (e.g., v_user "schema".users) use string typname fields, not AST nodes, so they won't be transformed. Confirm this limitation is documented/acceptable.

Recommended test plan:

  1. Run pnpm test in packages/plpgsql-deparser to verify all 26 tests pass
  2. Test schema renaming on a real PL/pgSQL function with various expression types
  3. Verify the deparsed output is valid SQL that can be executed

Notes

…ansformations

- Add deparseExprNode() helper to deparse expression AST nodes by wrapping in SELECT
- Update dehydrateQuery() for sql-expr kind to always prefer deparsed AST
- Update dehydrateQuery() for assign kind to prefer deparsed AST nodes
- Update hydrate-demo.test.ts to use AST-based modifications instead of string fields
- Add tests for schema renaming in hydrated expressions (sql-expr, assign, sql-stmt)

This enables AST-based transformations (e.g., schema renaming) to be properly
reflected in the deparsed PL/pgSQL function bodies. Previously, modifications
to AST nodes were ignored because dehydrateQuery() returned the original
string fields instead of deparsing the modified AST.
@devin-ai-integration
Copy link
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 and CI monitoring

@pyramation pyramation merged commit 22f53c7 into main Jan 5, 2026
14 checks passed
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.

2 participants