Repository navigation
feat(dialect): add ON DUPLICATE KEY UPDATE - #408
Open
alexskinner wants to merge 1 commit into
Open
alexskinner wants to merge 1 commit into
alexskinner wants to merge 1 commit into
Conversation
cursor
Bot
force-pushed
the
upstream/dialect-upsert
branch
3 times, most recently
from
November 21, 2026 19:08
e669d57 to
3a5e67e
Compare
KKould
force-pushed
the
upstream/dialect-upsert
branch
2 times, most recently
from
November 21, 2026 20:27
fcf3170 to
8598266
Compare
Codecov Report❌ Patch coverage is
❌ Your patch check has failed because the patch coverage (86.43%) is below the target coverage (90.00%). You can increase the patch coverage or adjust the target coverage. Additional details and impacted files@@ Coverage Diff @@
## main #408 +/- ##
==========================================
- Coverage 92.99% 92.91% -0.09%
==========================================
Files 275 275
Lines 39243 39707 +464
==========================================
+ Hits 36495 36892 +397
- Misses 2748 2815 +67
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
cursor
Bot
force-pushed
the
upstream/dialect-upsert
branch
from
November 22, 2026 11:02
8598266 to
532333a
Compare
Contributor
Author
Add an optional ON DUPLICATE KEY UPDATE clause to ast::Insert, with the assignments and the row alias of INSERT ... AS alias. The generic parser lowers the clause only after SqlParser::with_on_duplicate_key_update(); otherwise it keeps rejecting it with the same error as before, so GenericSqlDialect is unchanged. The SQLite parser never produces it, and ON CONFLICT stays rejected everywhere. When a statement carries the clause, INSERT ... VALUES and INSERT ... SELECT update the row a primary key, or a UNIQUE key without NULLs, conflicts with instead of inserting. A row of the same statement counts as existing once written. Assignments read the existing row by column name and the proposed row through VALUES(col) or the row alias; an assignment may move the primary key, and moving onto an existing key fails as a duplicate. A row the assignments leave unchanged is not rewritten. Add SqlDialect::upsert_affected_rows(inserted, updated), defaulting to 1 for every row, which sets the affected rows of each inserted, changed and unchanged row. It is resolved once at bind time and kept on the insert operator; DynDialect forwards it to its current dialect. A unit test in src/dialect/mod.rs covers the default rejection, both forms of the proposed row and the hook. Co-authored-by: Alex Skinner <alexskinner@users.noreply.github.com>
cursor
Bot
force-pushed
the
upstream/dialect-upsert
branch
from
November 22, 2026 17:37
532333a to
8e9feba
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.
Part of #403.
Reworked on top of #410: the clause is now part of the KiteSQL AST and the dialect's parser decides whether to accept it.
What
ast::Insertgainson_duplicate_key_update: Option<ast::OnDuplicateKeyUpdate>, which holds the assignments and the optional row alias ofINSERT ... AS alias.SqlParser::with_on_duplicate_key_update()makes the generic parser lowerON DUPLICATE KEY UPDATEinto that field. Without it the clause is rejected with the same error as today (INSERT ON DUPLICATE KEY UPDATE is not supported), soGenericSqlDialectis unchanged and the existingtests/slt/insert.sltcase for that error is untouched. The SQLite parser never produces the clause, andON CONFLICTstays rejected everywhere.INSERT ... VALUESandINSERT ... SELECTupdate the row a primary key, or aUNIQUEkey with no NULLs in it, conflicts with instead of inserting. A row written earlier in the same statement counts as existing.VALUES(col)or the row alias, as inINSERT ... AS new ON DUPLICATE KEY UPDATE v = new.v. Column aliases on the row alias are rejected by the parser.Database::run, the statement's writes are then rolled back, as with any failed write.SqlDialecthook,upsert_affected_rows(inserted, updated) -> u64, defaulting to 1 for every row. It sets the affected-row count the statement reports for each inserted, changed and unchanged row. The binder resolves the three values once and stores them on the insert operator, so the executor never calls into the dialect.DynDialectforwards it.docs/features.md.Why
Upsert is the most common write pattern in applications written for MySQL-compatible databases.
ON DUPLICATE KEY UPDATEis that syntax, and the affected-row convention there is 1 for an insert, 2 for a change and 0 for a no-op. With #410 a dialect owns its syntax, so a dialect crate turns the clause on in its ownparseand picks the count through the hook, while core keeps one dialect-neutral upsert path. See #403.Testing
src/dialect/mod.rsruns the same upserts (primary-key conflict, unique-key conflict,VALUES(col), row alias, unchanged row) against the generic dialect, which still rejects the clause, and a test-only dialect whoseparsecallsSqlParser::with_on_duplicate_key_update(), with the default and with dialect-chosen affected rows.make test(656 passed, 2 ignored),make test-async(601 passed),make test-sltandmake test-slt-async(198 files each),make fmtand all fourmake clippyinvocations pass.Notes for review
src/dialect/mod.rs,src/parser/genericanddocs/features.mdas the session PR, so whichever lands second needs a trivial rebase.SqlDialecthook, following feat: KiteSQL AST and SQLite dialect #410's "the dialect rejects syntax it does not support" rule. Only the affected-row count, which is semantics, stays a hook.InsertOperatorgainson_duplicate_key_update: Option<OnDuplicateKeyUpdate>. For a plain insert it isNone, and the executor runs the same path as before.