fix(postgres): parse ON CONFLICT after a FROM-less INSERT ... SELECT#2672
Open
skyf0l wants to merge 1 commit into
Open
fix(postgres): parse ON CONFLICT after a FROM-less INSERT ... SELECT#2672skyf0l wants to merge 1 commit into
skyf0l wants to merge 1 commit into
Conversation
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
In the Postgres dialect,
INSERT … SELECT … ON CONFLICT …is reported as anUnparsable sectionwhenever theSELECTsource has noFROMclause. The same statement parses fine with aVALUESsource, or with aSELECTthat does have aFROMclause — so the upsert grammar is correct; the bare select target list simply doesn't stop atON CONFLICTand greedily consumes theONtoken.This is valid PostgreSQL: an
INSERTsource may be aquery(aSELECT), andON CONFLICTapplies to all three source forms (DEFAULT VALUES,VALUES,query). See the INSERT synopsis.Reproduction
A common real-world case is a writable CTE inserting unnested rows:
Root cause
The Postgres
SelectClauseSegmentparses its target list inParseMode::GreedyOnceStartedwith an inlineterminatorslist (FROM,WHERE,LIMIT,ORDER BY, …) that does not includeON CONFLICT. With noFROMclause to terminate at, the greedy target list swallows the trailingONand the statement becomes unparsable. When aFROMclause is present the select clause terminates there, which is why the bug only affects FROM-less selects.Fix
Add
ON CONFLICTas a terminator of the Postgres select target list. The two-keyword sequence is intentional so it does not affectSELECT DISTINCT ON (…)orJOIN … ON ….Ref::keyword("LIMIT").to_matchable(), Ref::keyword("OVERLAPS").to_matchable(), Ref::new("SetOperatorSegment").to_matchable(), Sequence::new(vec![ Ref::keyword("WITH").to_matchable(), Ref::keyword("NO").optional().to_matchable(), Ref::keyword("DATA").to_matchable(), ]) .to_matchable(), Ref::new("WithCheckOptionSegment").to_matchable(), + Sequence::new(vec![ + Ref::keyword("ON").to_matchable(), + Ref::keyword("CONFLICT").to_matchable(), + ]) + .to_matchable(), ]; this.parse_mode(ParseMode::GreedyOnceStarted);(
crates/lib-dialects/src/postgres.rs, in theSelectClauseSegmentreplace_grammarterminators.)Tests
Added a sqruff-native fixture covering
DO NOTHING,DO UPDATE, and the writable-CTE form:crates/lib-dialects/test/fixtures/dialects/postgres/sqruff/insert_select_on_conflict.sqlcrates/lib-dialects/test/fixtures/dialects/postgres/sqruff/insert_select_on_conflict.yml(generated viaenv UPDATE_EXPECT=1 cargo test)The generated parse tree contains real
insert_statement/conflict_target/conflict_action/with_compound_statementnodes and nounparsablesegments.cargo test -p sqruff-lib-dialectspasses (all dialect fixtures, 0 failed), and no existing fixtures changed — the grammar tweak doesn't perturb any other parse tree.Manually re-verified after the fix:
INSERT … SELECT … ON CONFLICT DO NOTHING,… DO UPDATE, and the writable-CTEINSERT … SELECT unnest(…) … ON CONFLICTformSELECT DISTINCT ON (a) a, b FROM t,SELECT a.x FROM a JOIN b ON a.id = b.id,INSERT … VALUES … ON CONFLICT,INSERT … SELECT … FROM … ON CONFLICT, plainSELECT 1, 2,SELECT a b FROM t