Support PostgreSQL 18 RETURNING OLD/NEW syntax#4355
Open
stepan-romankov wants to merge 1 commit intosqlc-dev:mainfrom
Open
Support PostgreSQL 18 RETURNING OLD/NEW syntax#4355stepan-romankov wants to merge 1 commit intosqlc-dev:mainfrom
stepan-romankov wants to merge 1 commit intosqlc-dev:mainfrom
Conversation
Author
|
@kyleconroy would you mind taking a look when you get a chance? |
The pg_query_go parser already parses RETURNING OLD.* and RETURNING NEW.* correctly, but sqlc's compiler rejected these queries because the star expansion and column resolution logic tried to match "old"/"new" against actual table names and found no match. This caused star expansion to produce empty column lists (resulting in invalid SQL like "RETURNING ;") and column refs like OLD.bar to fail with "column does not exist". The fix recognizes "old" and "new" as special scope/alias values in three places and lets them match the target table's columns instead of being filtered out. Fixes sqlc-dev#3600 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
14a4f33 to
027ca2c
Compare
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.
Fixes #3600
PostgreSQL 18 added support for
RETURNING OLD.*andRETURNING NEW.*in INSERT/UPDATE/DELETE statements. The underlying parser (pg_query_go v6.2.2) already handles this syntax, but sqlc's compiler rejects these queries.What was broken
The compiler has three places where it matches a column scope or alias against known table names. When the scope is
"old"or"new", it doesn't match any real table name, so:RETURNING OLD.*) produces an empty column list, rewriting the query to something likeRETURNING ;, which fails to re-parseRETURNING OLD.bar) fail with "column does not exist"What this fixes
Three small changes in
internal/compiler/expand.goandinternal/compiler/output_columns.goto recognize"old"and"new"as special scope values that should match the target table's columns rather than being filtered out.The expanded queries correctly preserve the OLD/NEW prefix, e.g.
RETURNING OLD.*becomesRETURNING old.id, old.bar, old.baz.Test plan
returning_old_new/postgresql/stdlib) covering:RETURNING OLD.*on UPDATERETURNING NEW.*on UPDATERETURNING OLD.col, NEW.colon UPDATERETURNING OLD.*on DELETETestReplay/basetests passgo build ./...passes