Skip to content

feat(dialect): add ON DUPLICATE KEY UPDATE - #408

Open
alexskinner wants to merge 1 commit into
KipData:mainfrom
alexskinner:upstream/dialect-upsert
Open

alexskinner wants to merge 1 commit into
KipData:mainfrom
alexskinner:upstream/dialect-upsert

Conversation

@alexskinner

@alexskinner alexskinner commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

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::Insert gains on_duplicate_key_update: Option<ast::OnDuplicateKeyUpdate>, which holds the assignments and the optional row alias of INSERT ... AS alias.
  • SqlParser::with_on_duplicate_key_update() makes the generic parser lower ON DUPLICATE KEY UPDATE into that field. Without it the clause is rejected with the same error as today (INSERT ON DUPLICATE KEY UPDATE is not supported), so GenericSqlDialect is unchanged and the existing tests/slt/insert.slt case for that error is untouched. The SQLite parser never produces the clause, 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 with no NULLs in it, conflicts with instead of inserting. A row written earlier in the same statement counts as existing.
    • Assignments read the existing row by plain column name. They read the row the statement tried to insert through VALUES(col) or the row alias, as in INSERT ... AS new ON DUPLICATE KEY UPDATE v = new.v. Column aliases on the row alias are rejected by the parser.
    • An assignment may change the primary key, which moves the row. Moving it onto a key that already exists fails as a duplicate primary key. Through Database::run, the statement's writes are then rolled back, as with any failed write.
    • A row the assignments leave unchanged isn't rewritten.
  • One SqlDialect hook, 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. DynDialect forwards it.
  • Rustdoc on the new AST type, the parser option and the hook, and the dialect section of docs/features.md.

Why

Upsert is the most common write pattern in applications written for MySQL-compatible databases. ON DUPLICATE KEY UPDATE is 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 own parse and picks the count through the hook, while core keeps one dialect-neutral upsert path. See #403.

Testing

  • One unit test in src/dialect/mod.rs runs 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 whose parse calls SqlParser::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-slt and make test-slt-async (198 files each), make fmt and all four make clippy invocations pass.

Notes for review

  • One commit on top of main, no new dependencies. It's independent of the other RFC: pluggable SQL dialects, so dialect-specific behaviour can live outside core #403 PRs. It touches the same spots in src/dialect/mod.rs, src/parser/generic and docs/features.md as the session PR, so whichever lands second needs a trivial rebase.
  • The syntax gate lives in the parser rather than in a SqlDialect hook, 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.
  • InsertOperator gains on_duplicate_key_update: Option<OnDuplicateKeyUpdate>. For a plain insert it is None, and the executor runs the same path as before.
  • The conflict check looks up the primary key first and then each unique index, and it updates the first conflicting row it finds. When several unique keys could match different rows, only one row is updated, which matches how other engines that support this clause behave.

@cursor
cursor Bot force-pushed the upstream/dialect-upsert branch 3 times, most recently from e669d57 to 3a5e67e Compare November 21, 2026 19:08
@KKould
KKould force-pushed the upstream/dialect-upsert branch 2 times, most recently from fcf3170 to 8598266 Compare November 21, 2026 20:27
@codecov

codecov Bot commented Nov 21, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 86.43411% with 70 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.91%. Comparing base (bb18cee) to head (8598266).

Files with missing lines Patch % Lines
src/binder/parser.rs 83.00% 34 Missing ⚠️
src/execution/dml/insert.rs 89.44% 17 Missing ⚠️
src/dialect/mod.rs 75.38% 16 Missing ⚠️
src/planner/operator/insert.rs 66.66% 2 Missing ⚠️
src/binder/mod.rs 95.65% 1 Missing ⚠️

❌ 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     
Flag Coverage Δ
rust 92.91% <86.43%> (-0.09%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/binder/insert.rs 98.83% <100.00%> (-0.04%) ⬇️
src/binder/select.rs 96.40% <100.00%> (+0.02%) ⬆️
src/orm/mod.rs 88.17% <100.00%> (+0.10%) ⬆️
src/planner/operator/mod.rs 93.46% <ø> (ø)
src/planner/operator/visitor.rs 99.16% <100.00%> (+0.02%) ⬆️
src/planner/operator/visitor_mut.rs 98.40% <100.00%> (+0.03%) ⬆️
src/binder/mod.rs 95.58% <95.65%> (+0.40%) ⬆️
src/planner/operator/insert.rs 80.00% <66.66%> (-10.00%) ⬇️
src/dialect/mod.rs 85.02% <75.38%> (-3.87%) ⬇️
src/execution/dml/insert.rs 88.34% <89.44%> (-0.82%) ⬇️
... and 1 more
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@cursor
cursor Bot force-pushed the upstream/dialect-upsert branch from 8598266 to 532333a Compare November 22, 2026 11:02
@alexskinner

Copy link
Copy Markdown
Contributor Author

Rebased onto current main (after #407). Still a single commit, no conflicts. The upsert path now uses Index::from_meta so it honours prefix indexes from #407.

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
cursor Bot force-pushed the upstream/dialect-upsert branch from 532333a to 8e9feba Compare November 22, 2026 17:37
@alexskinner alexskinner changed the title feat(dialect): add ON DUPLICATE KEY UPDATE hooks feat(dialect): add ON DUPLICATE KEY UPDATE Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants