Skip to content

feat(dialect): add session transaction control - #409

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

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

Conversation

@alexskinner

@alexskinner alexskinner commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Part of #403.

Reworked on top of #410: the transaction control statements are now part of the KiteSQL AST, and the dialect's parser decides which of them it accepts.

What

  • The AST gains Statement::StartTransaction, Commit, Rollback and SetAutocommit(bool).
    • The generic parser lowers BEGIN / START TRANSACTION, COMMIT and ROLLBACK, and rejects transaction modes, savepoints and AND CHAIN.
    • SET autocommit = 0 | 1 (and ON/OFF, TRUE/FALSE) is lowered only after SqlParser::with_set_autocommit(). Otherwise it is rejected like any other SET, as today.
    • The SQLite parser lowers BEGIN [DEFERRED], COMMIT/END and ROLLBACK, so Session works with SqliteDialect too. Named transactions, IMMEDIATE/EXCLUSIVE and savepoints stay rejected.
  • Adds Database::session(), which returns a Session that runs those statements:
    • Between BEGIN and COMMIT/ROLLBACK, statements run in one transaction, as with Transaction::run. Outside a transaction, each statement commits on its own, as with Database::run.
    • COMMIT and ROLLBACK without an open transaction do nothing. Dropping the session rolls back an open transaction.
    • With autocommit off, a statement outside a transaction opens one, which stays open until COMMIT or ROLLBACK, and turning autocommit back on commits it.
    • DDL and ANALYZE still go through Database::ddl and Database::analyze, with the same errors as today. Database::run, Transaction::run and the other entry points reject transaction control statements and point to Database::session.
    • Session::run takes one statement at a time. autocommit() and in_transaction() report the session's state.
  • One SqlDialect hook, begin_commits_active_transaction(), defaulting to false. When true, BEGIN inside an open transaction commits it and opens a new one. By default such a BEGIN is rejected and the open transaction is left as it was. DynDialect forwards it.
  • Database::run and Transaction::run now delegate to a crate-private run_statements, which the session reuses for the statement it has already parsed. Neither public method changes behaviour.
  • Rustdoc on Session, the parser option and the hook, a line in the MVCC section and a line in the dialect section of docs/features.md.

Why

Drivers and connection pools send transaction control as SQL. Today that only works through the CLI shell, which special-cases those strings. A Session gives embedders the same thing through the library. Dialects also differ on the details: MySQL-compatible clients expect a nested BEGIN to commit implicitly and use SET autocommit = 0, while the standard behaviour is to reject both. With #410 a dialect crate opts into SET autocommit in its own parse and picks the nested BEGIN behaviour through the hook, without branches in core. See #403.

Testing

  • Two unit tests in src/db/session.rs, on the default test storage for both the sync and async builds: with the generic dialect a nested BEGIN and SET autocommit are rejected and the open transaction still commits; with a test-only dialect whose parse calls SqlParser::with_set_autocommit(), a nested BEGIN commits, and autocommit off opens a transaction that turning it back on commits.
  • The existing test_prepare_and_command_type_classification case for start transaction now checks that command_type rejects it with a pointer to Database::session, since the parser accepts it.
  • make test (657 passed, 2 ignored), make test-async (602 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, including the ON DUPLICATE KEY UPDATE one. It touches the same spots in src/dialect/mod.rs, src/parser/generic and docs/features.md as that PR, so whichever lands second needs a trivial rebase.
  • The SET autocommit 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 nested BEGIN behaviour, which is semantics, stays a hook.
  • Session is additive. Nothing that exists today goes through it, and with the default dialect the only new behaviour is the session itself.
  • A statement that fails inside a transaction leaves the transaction open with whatever that statement wrote, just like Transaction::run does today. ROLLBACK discards it. Statement-level rollback inside a transaction would need savepoint support in the storages, so I left it out of scope.
  • On single-writer storage (LMDB), writing through the Database on the same thread while a session holds a write transaction waits for that transaction. The rustdoc says to send every statement through the session while it has one open.
  • I didn't switch src/bin/shell.rs over to Session, to keep this PR to one concern. I'm happy to do that in a follow-up if you'd like.

@cursor
cursor Bot force-pushed the upstream/dialect-transactions branch 3 times, most recently from 061b159 to 8d34bae Compare November 21, 2026 19:08
@KKould
KKould force-pushed the upstream/dialect-transactions branch 2 times, most recently from 3d5bf40 to 2f4e0b7 Compare November 21, 2026 20:27
@codecov

codecov Bot commented Nov 21, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 75.52743% with 58 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.89%. Comparing base (2741570) to head (2f4e0b7).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/db/session.rs 75.23% 52 Missing ⚠️
src/dialect/mod.rs 50.00% 6 Missing ⚠️

❌ Your patch check has failed because the patch coverage (75.52%) 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     #409      +/-   ##
==========================================
- Coverage   92.98%   92.89%   -0.09%     
==========================================
  Files         274      276       +2     
  Lines       39013    39479     +466     
==========================================
+ Hits        36275    36674     +399     
- Misses       2738     2805      +67     
Flag Coverage Δ
rust 92.89% <75.52%> (-0.09%) ⬇️

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

Files with missing lines Coverage Δ
src/binder/parser.rs 90.44% <100.00%> (+0.33%) ⬆️
src/db.rs 94.19% <ø> (+0.66%) ⬆️
src/dialect/mod.rs 86.20% <50.00%> (ø)
src/db/session.rs 75.23% <75.23%> (ø)
🚀 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-transactions branch from 2f4e0b7 to c0b35f9 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.

Add StartTransaction, Commit, Rollback and SetAutocommit statements to the
KiteSQL AST. The generic parser lowers BEGIN or START TRANSACTION, COMMIT
and ROLLBACK, rejecting transaction modes, savepoints and AND CHAIN, and
lowers SET autocommit = 0 | 1 (or ON/OFF, TRUE/FALSE) only after
SqlParser::with_set_autocommit(); otherwise SET autocommit is rejected
like any other SET. The SQLite parser lowers BEGIN [DEFERRED], COMMIT/END
and ROLLBACK.

Add Database::session(), which returns a Session that runs these
statements. Between BEGIN and COMMIT or ROLLBACK, statements run in one
transaction, as with Transaction::run; outside one, each statement
commits on its own, as with Database::run. COMMIT and ROLLBACK without an
open transaction do nothing, and dropping the session rolls back an open
transaction. With autocommit off, a statement outside a transaction opens
one that stays open until COMMIT or ROLLBACK, and turning autocommit back
on commits it. DDL and ANALYZE still go through Database::ddl and
Database::analyze, and the other entry points reject transaction control
statements.

Add SqlDialect::begin_commits_active_transaction(), defaulting to false:
when true, BEGIN inside an open transaction commits it and opens a new
one; by default such a BEGIN is rejected and the open transaction is left
as it was. DynDialect forwards it to its current dialect.

Database::run and Transaction::run now delegate to run_statements, which
the session reuses for statements it has already parsed. Their behaviour
is unchanged. Unit tests in src/db/session.rs cover the default and a
test-only dialect that parses SET autocommit and turns the hook on.

Co-authored-by: Alex Skinner <alexskinner@users.noreply.github.com>
@cursor
cursor Bot force-pushed the upstream/dialect-transactions branch from c0b35f9 to 8a8bac6 Compare November 22, 2026 17:37
@alexskinner alexskinner changed the title feat(dialect): add session transaction control hooks feat(dialect): add session transaction control 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