fix(plpgsql-deparser): re-insert INTO after DML RETURNING + skip implicit final RETURN - #301
Merged
Merged
Conversation
…plicit final RETURN
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.
Summary
Fixes two PL/pgSQL round-trip corruption bugs in
plpgsql-deparser:Bug 1 (critical):
INTOsilently dropped fromINSERT/UPDATE/DELETE ... RETURNING ... INTOlibpg_query strips
INTO <target>from the statement text and records it asexec.into/exec.target;deparseExecSql()re-inserts it viafindIntoInsertionPoint(). That scanner's depth-0INTOcheck matched theINTOofINSERT INTO s.t ...itself and returned-1("already present"), so the target clause was dropped:Fix in
findIntoInsertionPoint: when the first depth-0 command keyword isINSERT/UPDATE/DELETE/MERGE, appendINTO <target>at the end of the statement (after the RETURNING list) instead of running the SELECT clause-keyword scan (which would also misfire onDELETE FROM/UPDATE ... FROM). The scanner remains quote/comment/paren-depth aware, soINTOis never inserted inside a RETURNING subquery.Bug 2: implicit trailing bare
RETURNemitted (syntax error in trigger functions)libpg_query's PL/pgSQL compiler appends an implicit final
{"PLpgSQL_stmt_return":{}}(noexpr, nolineno) to every function body; the deparser printed it asRETURN;, which is invalid insideRETURNS triggerfunctions.Fix:
deparseFunctionnow callsstripImplicitFinalReturn(), which drops a trailing top-levelPLpgSQL_stmt_returnonly when it has noexpr, nolineno, and noretvarno >= 0. An explicit user-writtenRETURN;carries alinenoand is preserved.Tests
__fixtures__/plpgsql/plpgsql_deparser_fixes.sql+ regeneratedgenerated.json: INSERT/UPDATE/DELETERETURNING ... INTO,INTO STRICT, multi-column RETURNING, RETURNING with subquery, trigger fn with no final return, void fn with explicit trailingRETURN;, trigger fn ending inRETURN NEW.deparser-fixes.test.tsincluding an INTO/RETURNING keyword-count invariant across the round-trip.RETURN;.pgsql-cli/pgsql-typesreport "No tests found" — pre-existing, those packages have no test files).Context: found while making constructive-db's schema-rename transform use the parser/deparser path by default; Bug 1 silently corrupted 24 generated files and Bug 2 broke trigger deploys.
Link to Devin session: https://app.devin.ai/sessions/eeb8deba0c04475091df703c88877302
Requested by: @pyramation