feat(plpgsql-deparser): add hydratePlpgsqlAst for parsing embedded SQL expressions #254
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
feat(plpgsql-deparser): add hydratePlpgsqlAst for parsing embedded SQL expressions
Summary
Adds a new
hydratePlpgsqlAst()function that walks PL/pgSQL ASTs and parses the embedded SQL expression strings (PLpgSQL_expr.query) into full SQL AST nodes. This enables downstream tooling to work with structured SQL ASTs instead of opaque strings.The implementation uses a discriminated union type system with four expression kinds:
raw- fallback for unparseable expressionssql-expr- expressions parsed viaSELECT <expr>wrapper (parseMode 2)sql-stmt- full SQL statements (parseMode 0)assign- PL/pgSQL assignments split into target/value with parsed ASTs (parseMode 3)Key features:
scanSync) to safely split assignments on:=while respecting nestingrawkindisHydratedExpr()type guard andgetOriginalQuery()helperUpdates since last revision
Deparser pretty printing for CREATE FUNCTION - Added
context.isPretty()checks inCreateFunctionStmthandler to format function parameters andRETURNS TABLEcolumns on separate lines in pretty mode. This is a change to the corepgsql-deparserpackage.Workspace dependency -
plpgsql-deparsernow usesworkspace:*forpgsql-deparserdependency to ensure it uses the local workspace version during development.Creative lowercase naming - Demo uses realistic name transformations instead of
_MODIFIEDsuffixes:big_kitchen_sink→order_rollup_calculatorv_discount→v_rebate,v_tax→v_levy0→42Full CREATE FUNCTION support - The demo test parses the full SQL statement and outputs the complete
CREATE OR REPLACE FUNCTIONdeclaration includingRETURNS TABLEclause, parameters, and language specification.dehydratePlpgsqlAst()- Converts hydrated AST back to canonical string format for deparsing. Forassignkind, reconstructs the string fromtargetandvaluefields, enabling AST-level modifications to be reflected in the output.Review & Testing Checklist for Human
CreateFunctionStmtchanges affect ALL functions in pretty mode. Review the updated snapshots inpackages/deparser/__tests__/pretty/__snapshots__/to ensure the new multiline format is acceptable.workspace:*dependency inplpgsql-deparser/package.jsonshould be transformed to a version number during publish. Verify this works correctly with your publish workflow.END IF;placement, inconsistent whitespace). The PL/pgSQL body formatting is not as clean as the SQL wrapper.v_discountandv_taxassignments are renamed. Other references still use original names.Recommended test plan:
npm testinpackages/deparser- verify 7 pre-existing failures, no new failuresnpm testinpackages/plpgsql-deparser- all 23 tests should pass with 9 snapshotsNotes