Commit Graph

5223 Commits

Author SHA1 Message Date
Pere Diaz Bou
334da8abbb core/mvcc/logical-log: refactor get log path in tests 2025-09-25 18:33:26 +02:00
Pekka Enberg
e80d8cd0c0 Merge 'translate: disable support for UPDATE ... ORDER BY' from Jussi Saurio
we haven't really tested this at all, and it clearly doesn't work, as
per #3315.
best to disable it for the time being, especially since vanilla SQLite
doesn't support this syntax unless you compile it with a flag.

Closes #3337
2025-09-25 17:24:15 +03:00
Pekka Enberg
f8130d051f Merge 'Fix JavaScript bindings' from Nikita Sivukhin
This PR makes all JS db packages to have uniform interface: `new
Database(...)` constructor with explicit `connect()` and `close()`
methods.
Also, this PR adds docstrings in the code and properly support few
better-sqlite options (readonly, fileMustExist, timeout)

Closes #3334
2025-09-25 17:24:03 +03:00
Jussi Saurio
c07a227560 Merge 'core/mvcc/logical-log: load logical log from disk' from Pere Diaz Bou
## Description
Read transactions from logical log and load mvcc store based on the
contents onto transaction 0, which is the special transaction where we
will load all row versions that can be read by all new transactions.
## Todo
- [x] Testing for multiple transactions
- [ ] Lock multiple loading of same logical-log at the same time
- [x] Add column_count to format because if not it is hard to judge from
record alone.
- [x] Trim buffer read
I was thinking to implement the rest on another pr to not increase load
to review this one.

Reviewed-by: Jussi Saurio <jussi.saurio@gmail.com>

Closes #3323
2025-09-25 16:26:34 +03:00
Pere Diaz Bou
8f3332377a core/mvcc/logical-log: fmt fmt fmt fmt 2025-09-25 13:33:16 +02:00
Pere Diaz Bou
b75eb9f09d core/mvcc/logical-log: drain consumed bytes on read_more_data 2025-09-25 13:31:03 +02:00
Pere Diaz Bou
7b131b0b0d core/mvcc/logical-log: test fuzz logical log read 2025-09-25 13:23:43 +02:00
Jussi Saurio
8f32817423 translate: disable support for UPDATE ... ORDER BY
we haven't really tested this at all, and it clearly doesn't work,
as per #3315.

best to disable it for the time being.
2025-09-25 13:54:39 +03:00
Pere Diaz Bou
dd0a71484d core/mvcc/logical-log: test insert 100 transactions 2025-09-25 12:40:05 +02:00
Pere Diaz Bou
8528f92248 core/mvcc/logical-log: fix is_eof 2025-09-25 12:39:48 +02:00
Pere Diaz Bou
654f9a9412 core/mvcc/logical-log: read column_count from immutable record 2025-09-25 12:27:30 +02:00
Jussi Saurio
3113822ceb Merge 'MVCC: support alter table' from Jussi Saurio
Changes ALTER TABLE operations to use only MV store and not go through
pager, because pager is only used during checkpoint.
- Exclusive transaction is required for DDL operations: trying to
execute one inside a `BEGIN CONCURRENT` transaction will return an
error. It's simply too complicated for now to try to make them
concurrently transactional.
- Not doing schema changes via pager means that in MV rollback, the
connection must rollback its private schema separately, since pager
rollback is not invoked.
- To simplify MVCC semantics, if any transaction committed a schema
change after a transaction started, it cannot commit and will abort with
`SchemaUpdated` error
- To mimic regular SQLite transaction behavior, if a transaction tries
to promote to exclusive transaction, it will fail with `Busy` error if
there were any committed transactions after the read transaction
started.

Reviewed-by: Pere Diaz Bou <pere-altea@homail.com>

Closes #3333
2025-09-25 12:25:22 +03:00
Jussi Saurio
3d38c5a101 mvcc: disallow promote to exclusive tx if another tx committed in between 2025-09-25 11:50:09 +03:00
Jussi Saurio
d4bcf2c15a mvcc: disallow BEGIN after BEGIN CONCURRENT 2025-09-25 11:50:09 +03:00
Jussi Saurio
1ff2e07404 mvcc: disallow BEGIN CONCURRENT after BEGIN 2025-09-25 11:50:09 +03:00
Jussi Saurio
7c86e7b9c4 merge conflicts with atomics PRs 2025-09-25 11:49:44 +03:00
Jussi Saurio
8f46721848 Merge 'Normalize target table name identifier on table or column rename' from Iaroslav Zeigerman
Closes #3320
Closes #3286
In addition to the problem reported in the ticket, 2 more issues were
identified:
1. Renaming a column for a table with a special character in its name
failed with
```
turso> CREATE TABLE `t t`(a);
turso> ALTER TABLE `t t` RENAME COLUMN a TO `a a`;

thread 'main' panicked at core/vdbe/execute.rs:7870:14:
table being renamed should be in schema
```
2. The renamed table in the `sql` column of `sqlite_schema` was not
reflected correctly after renaming:
```
turso>  select * from sqlite_schema;
┌───────┬──────┬──────────┬──────────┬──────────────────────┐
│ type  │ name │ tbl_name │ rootpage │ sql                  │
├───────┼──────┼──────────┼──────────┼──────────────────────┤
│ table │ t t  │ t t      │        2 │ CREATE TABLE t t (a) │
└───────┴──────┴──────────┴──────────┴──────────────────────┘
```
3. `sql` for indexes was not reflected correctly after renaming a column
that contains special characters:
```
turso> ALTER TABLE `t t` RENAME COLUMN `a a` TO `b b`;
turso> SELECT sql FROM sqlite_schema;

┌───────────────────────────────────┐
│ sql                               │
├───────────────────────────────────┤
│ CREATE TABLE `t t` (`b b`)        │
├───────────────────────────────────┤
│ CREATE INDEX idx ON `t t` (`a a`) │
├───────────────────────────────────┤
```

Reviewed-by: Jussi Saurio <jussi.saurio@gmail.com>

Closes #3322
2025-09-25 11:41:45 +03:00
Nikita Sivukhin
a938bdcf09 fix exec to run over multiple statements in the string 2025-09-25 12:03:52 +04:00
Pekka Enberg
b81cc1da1d Merge 'Fix: NULL values in group_concat()' from
Closes #3304.
group_concat() now ignores null values.

Reviewed-by: Jussi Saurio <jussi.saurio@gmail.com>

Closes #3321
2025-09-25 10:19:41 +03:00
Jussi Saurio
54a566967c Merge 'Fix result columns binding precedence' from Jussi Saurio
In e.g. `SELECT x AS y, y AS x FROM t ORDER BY x;`, the `x` in the
`ORDER BY` should reference t.y, which has been aliased as `x` for this
query. The same goes for GROUP BY, JOIN ON etc. but NOT for WHERE.
Previously we had wrong precedence in `bind_and_rewrite_expr`.
Closes #3281

Closes #3290
2025-09-25 09:26:24 +03:00
Jussi Saurio
ede2a882f1 mvcc: revert connection schema changes in mv rollback 2025-09-25 08:43:00 +03:00
Jussi Saurio
1626ef046b mvcc: abort tx if other tx made a schema change in between 2025-09-25 08:43:00 +03:00
Jussi Saurio
864fa379dd mvcc: require exclusive transaction for schema changes 2025-09-25 08:42:59 +03:00
Iaroslav Zeigerman
4799f23261 Fix: Normalize the target table name identifier when renaming a table / column 2025-09-24 22:23:04 -07:00
Jussi Saurio
49602f409f fixes after atomics merge conflicts 2025-09-25 08:16:52 +03:00
Jussi Saurio
3010dc0255 mvcc: add CheckpointStateMachine
performs a blocking, truncating checkpoint:

- when started, blocks any other transactions from starting
- writes all row versions to pager that havent already been
  checkpointed
- flushes them to wal
- checkpoints wal to db

in the case of added/deleted rows in table id 1 (sqlite schema),
also creates/destroys btrees accordingly
2025-09-25 08:12:13 +03:00
Jussi Saurio
429f305fe8 mvcc: add MvStore::get_next_table_id()
this assigns "synthetic" root page numbers instead of using the pager
or btree cursors to allocate pages. Actual page allocation will only
happen during checkpoint, since regular MVCC commit bypasses the
pager.
2025-09-25 08:12:13 +03:00
Jussi Saurio
b702af8ac0 mvcc: add truncate method to logical log 2025-09-25 08:12:13 +03:00
Jussi Saurio
8f33b31c3d mvcc: update global header on commit end 2025-09-25 08:12:13 +03:00
Jussi Saurio
7464d1c172 mvcc: remove tx from store when commit write set is empty
previously, this was leaking transaction IDs in the mv store.
2025-09-25 08:12:13 +03:00
Jussi Saurio
949e6dd728 mvcc: remove unused states and fields from CommitStateMachine
none of the pager commit related data and logic are used anymore,
so let's delete them.
2025-09-25 08:12:12 +03:00
Jussi Saurio
c18c44b032 fix: result columns have varying binding precedence
In e.g. `SELECT x AS y, y AS x FROM t ORDER BY x;`, the `x` in the
`ORDER BY` should reference t.y, which has been aliased as `x` for this
query. The same goes for GROUP BY, JOIN ON etc. but NOT for WHERE.

Previously we had wrong precedence in `bind_and_rewrite_expr`.
2025-09-25 08:07:37 +03:00
Pekka Enberg
0a78ea87d5 Merge 'core: Wrap Connection::attached_databases with RwLock' from Pekka Enberg
Closes #3310
2025-09-25 07:46:48 +03:00
Pere Diaz Bou
1478538cb9 core/mvcc/logical-log: load logical log from disk
Read transactions from logical log and load mvcc store based on the
contents onto transaction 0, which is the special transaction where we
will load all row versions that can be read by all new transactions.

- [ ] Testing for multiple transactions
- [ ] Lock multiple loading of same logical-log at the same time
- [ ] Add column_count to format because if not it is hard to judge from
record alone.
2025-09-25 02:15:28 +02:00
Pere Diaz Bou
74f0830920 Merge 'core/mvcc/logical-log: on disk format for logical log' from Pere Diaz Bou
This format is based on previous discussions:
1. Log header
```rust
/// Log's Header, this will be the 64 bytes in any logical log file.
/// Log header is 64 bytes at maximum, fields added must not exceed that size. If it doesn't exceed
/// it, any bytes missing will be padded with zeroes.
struct LogHeader {
    version: u8,
    salt: u64,
    encrypted: u8, // 0 is no
}
```
2. Transaction format:
* Transaction id
* Checksum u64
* Byte size of all rows combined
* Rows
* End marker (offset position after appending buffer)
3. Row format:
```rust
    /// Serialize a row_version into on disk format.
    /// Format of a "row" (maybe we could change the name because row is not general enough for
    /// future type of values):
    ///
    /// * table_id (root page) -> u64
    /// * row type -> u8
    ///
    /// (by row type)
    /// Delete:
    /// * Payload length -> u64
    /// * Rowid -> varint
    ///
    /// Insert:
    /// * Payload length -> u64
    /// * Data size -> varint
    /// * Rowid -> varint
    /// * Data -> [u8] (data size length)
    fn serialize(&self, buffer: &mut Vec<u8>, row_version: &RowVersion) {

```

Closes #3245
2025-09-25 00:39:03 +02:00
rajajisai
b9ef9d6c63 Ignore null values in group concat 2025-09-24 18:11:02 -04:00
Pekka Enberg
f8f23a2a15 core: Wrap Connection::attached_databases with RwLock 2025-09-24 20:15:58 +03:00
Pekka Enberg
03263a9977 core: Wrap Connection::data_sync_retry with AtomicBool 2025-09-24 19:30:31 +03:00
Pekka Enberg
60e9d1a1c4 core: Wrap Connection::is_nested_stmt in AtomicBool 2025-09-24 19:30:31 +03:00
Pekka Enberg
a50771fe38 core: Wrap Connection::query_only with AtomicBool 2025-09-24 19:23:13 +03:00
Pekka Enberg
465dba573d core: Wrap Connection::closed with AtomicBool 2025-09-24 19:15:13 +03:00
Pekka Enberg
84588c03a8 Merge 'core/mvcc: Wrap Transaction::database_header with RwLock' from Pekka Enberg
Closes #3296
2025-09-24 19:14:22 +03:00
Pekka Enberg
eaddf1030d Merge 'core: Wrap Connection::capture_data_changes in RwLock' from Pekka Enberg
Closes #3293
2025-09-24 19:14:15 +03:00
PThorpe92
47aa03997b fix pc issue in sequence test op 2025-09-24 08:26:34 -04:00
Pekka Enberg
b590b353eb core/mvcc: Wrap Transaction::database_header with RwLock 2025-09-24 15:19:00 +03:00
Pekka Enberg
41d26d807b core: Wrap Connection::capture_data_changes in RwLock 2025-09-24 11:32:05 +03:00
Pekka Enberg
c894dcf438 Merge 'Make some Connection fields atomic' from Pekka Enberg
...the quest for Send continues.

Closes #3288
2025-09-24 11:27:57 +03:00
Pekka Enberg
d0e15f9ac0 Merge 'Fix INSERT INTO t DEFAULT VALUES' from Jussi Saurio
Closes #3279

Closes #3291
2025-09-24 11:09:27 +03:00
Jussi Saurio
5c82b72e5f fix INSERT INTO t DEFAULT VALUES 2025-09-24 09:54:43 +03:00
Jussi Saurio
726bc24e78 Support referring to rowid as _rowid_ or oid 2025-09-24 09:17:28 +03:00