Skip to content

Parser: Fix ON CONFLICT after INSERT ... SELECT - #3079

Open
syuya2036 wants to merge 2 commits into
apache:mainfrom
syuya2036:fix-insert-select-on-conflict
Open

syuya2036 wants to merge 2 commits into
apache:mainfrom
syuya2036:fix-insert-select-on-conflict

Conversation

@syuya2036

@syuya2036 syuya2036 commented Dec 1, 2026 •

Copy link
Copy Markdown

Fixes #1987.

INSERT INTO t SELECT 1 ON CONFLICT DO NOTHING;
INSERT INTO t SELECT 1 ON DUPLICATE KEY UPDATE a = 1;

The parser treated ON as an implicit alias for 1 and then failed at CONFLICT or DUPLICATE. Reserve ON in the default and SQLite column-alias lists so the INSERT parser can read both clauses. Explicit aliases still work.

The regression tests cover PostgreSQL, Generic, SQLite, and MySQL. PostgreSQL INSERT syntax

I used Codex to investigate the bug and draft the fix, regression tests, and PR description.

@LucaCappelletti94 LucaCappelletti94 added bug Something isn't working waiting on reviewer PR needs to be reviewed labels Dec 1, 2026
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.65%. Comparing base (540e398) to head (320d5d1).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3079   +/-   ##
=======================================
  Coverage   83.64%   83.65%           
=======================================
  Files          42       42           
  Lines       35833    35836    +3     
  Branches    35833    35836    +3     
=======================================
+ Hits        29974    29977    +3     
  Misses       2418     2418           
  Partials     3441     3441           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@LucaCappelletti94 LucaCappelletti94 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reserving ON in keywords lets is_select_item_alias reject it for every dialect that uses the default list.

     Keyword::RETURNING,
     Keyword::VALUES,
+    Keyword::ON,
     // Reserved only as a column alias in the `SELECT` clause

Then, SQLite overrides is_column_alias with its own list, so it needs the same entry to keep INSERT INTO t SELECT 1 ON CONFLICT DO NOTHING parsing.

                 | Keyword::RETURNING
                 | Keyword::VALUES
+                | Keyword::ON
                 | Keyword::FROM

Comment thread src/parser/mod.rs Outdated
Comment on lines +14722 to +14724
if !explicit && kw == &Keyword::ON && parser.peek_keyword(Keyword::CONFLICT) {
return false;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could drop this lookahead and reserve ON as an implicit column alias instead. Special-casing ON CONFLICT leaves the same failure for MySQL, where INSERT INTO t SELECT 1 ON DUPLICATE KEY UPDATE a = 1 still fails on this branch with Expected: end of statement, found: DUPLICATE although MySQL 8.4 runs it. PostgreSQL 17, SQLite 3.51 and MySQL 8.4 all reject SELECT 1 ON, and with the reservation every existing test still passes.

Suggested change
if !explicit && kw == &Keyword::ON && parser.peek_keyword(Keyword::CONFLICT) {
return false;
}

Comment thread tests/sqlparser_common.rs Outdated
Comment on lines +23855 to +23856
let generic = TestedDialects::new(vec![Box::new(GenericDialect {})]);
generic.one_statement_parses_to("SELECT 1 ON", "SELECT 1 AS ON");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could replace the SELECT 1 ON pin with the MySQL statement. The pin asserts an alias that all three engines reject, and the MySQL statement fails without the reservation.

Suggested change
let generic = TestedDialects::new(vec![Box::new(GenericDialect {})]);
generic.one_statement_parses_to("SELECT 1 ON", "SELECT 1 AS ON");
let mysql_and_generic =
TestedDialects::new(vec![Box::new(MySqlDialect {}), Box::new(GenericDialect {})]);
mysql_and_generic.verified_stmt("INSERT INTO t SELECT 1 ON DUPLICATE KEY UPDATE a = 1");

@LucaCappelletti94 LucaCappelletti94 added waiting on contributor The review needs further refinements by its author and removed waiting on reviewer PR needs to be reviewed labels Dec 1, 2026
Suggested-by: LucaCappelletti94

Generated-by: OpenAI Codex
@github-actions github-actions Bot added the MySQL label Dec 1, 2026
@syuya2036

Copy link
Copy Markdown
Author

Thanks, @LucaCappelletti94 !
I pushed the changes you suggested and added the MySQL regression test.
Could you take another look when you have a chance?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working MySQL PostgreSQL SQLite waiting on contributor The review needs further refinements by its author

Projects

None yet

Development

Successfully merging this pull request may close these issues.

INSERT from SELECT cannot be parsed with an ON CONFLICT clause

3 participants