fix(plpgsql-deparser): support nested DECLARE blocks inside FOR loops#271
Merged
pyramation merged 1 commit intomainfrom Jan 7, 2026
Merged
fix(plpgsql-deparser): support nested DECLARE blocks inside FOR loops#271pyramation merged 1 commit intomainfrom
pyramation merged 1 commit intomainfrom
Conversation
Implements lineno-based scope tracking to correctly assign variables to their enclosing blocks. Variables declared inside a FOR loop body are now emitted in a nested DECLARE section before the inner BEGIN, preserving the correct semantics where variables are reinitialized on each loop iteration. Key changes: - Add buildBlockDatumMap() to assign datums to blocks based on lineno ranges - Add collectNestedBlockLinenos() to find all nested block linenos in the AST - Modify deparseDeclareSection() to accept included/excluded datum indices - Modify deparseBlock() to emit DECLARE sections for its assigned datums - Modify deparseFunction() to exclude nested block datums from top-level DECLARE This fixes the last remaining failing fixture (plpgsql_varprops-13.sql). All 190 fixtures now pass round-trip testing.
Contributor
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
fix(plpgsql-deparser): support nested DECLARE blocks inside FOR loops
Summary
Implements lineno-based scope tracking to correctly assign variables to their enclosing blocks. Previously, variables declared inside a FOR loop body were hoisted to the top-level DECLARE section, which changed semantics (variables should be reinitialized on each loop iteration).
The fix uses line numbers from the AST to determine which variables belong to which blocks:
lineno <= topLevelBlockLinenostay in the top-level DECLARElineno > topLevelBlockLinenoare assigned to the nearest nested block whoselinenois greater than the variable'slinenoThis fixes the last remaining failing fixture (
plpgsql_varprops-13.sql). All 190 fixtures now pass round-trip testing.Review & Testing Checklist for Human
Recommended test plan: Run the round-trip tests locally (
npm testinpackages/plpgsql-deparser) and manually inspect the output forplpgsql_varprops-13.sqlto verify the nested DECLARE appears in the correct position.Notes
topLevelBlockLinenois undefined (discovered during debugging withbig-function.sql)