mirror of
https://github.com/aljazceru/turso.git
synced 2026-01-15 06:04:19 +01:00
## What Rowsets are used in SQLite for two purposes: 1. for membership tests on a set of `i64`s, 2. for in-order iteration of a set of `i64`s, Both in cases where we can just use rowids (which are `i64`) instead of building an entire ephemeral btree from a table's contents. For example, in cases where a `DELETE FROM tbl WHERE ...` is performed on a table that has any `BEFORE DELETE` triggers, SQLite collects the table's rowids into a RowSet before actually performing the deletion. This is similar to how an UPDATE that modifies rowids (or the index used to iterate the UPDATE loop) will first collect the rows into an ephemeral index, and same with `INSERT INTO ... SELECT`. ## Details RowSet uses a "batch" concept where insertions of a given batch must be guaranteed by caller to contain no duplicates and will be pushed into a vector for O(1). When a new batch is started, the previous batch is folded into a `BTreeSet` so that membership tests can be performed in O(logn). As far as I can tell, the "in-order iteration" use case doesn't use this batch logic at all. ## AI disclosure This entire PR description was written by me - no AIs were harmed in the production of it. However, the code itself was mostly vibecoded using two agents in Cursor: - Composer 1: given the SQLite opcode documentation and rowset.c source code, and asked to implement the VDBE instructions and the RowSet module. - GPT-5: given the same SQLite docs and source code, and asked to review Composer 1's work and write feedback into a separate markdown file. This loop was run for roughly 4-5 iterations, where each time GPT-5's feedback was given to Composer 1, until GPT-5 found nothing to comment anymore. After this, I instructed Composer 1 to improve the documentation to be less stupid. After that, I made a manual editing pass over the runtime code to e.g. change boolean flags to a `RowSetMode` enum to make clearer that the rowset has two distinct mutually exclusive purposes (membership tests and in-order iteration), plus cleaned up some other dumb shit and added comments. I am still not sure if this saved time or not. Closes #3938