Commit Graph

5187 Commits

Author SHA1 Message Date
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
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
Pekka Enberg
042a8dd031 core: Wrap Connection::wal_auto_checkpoint_disabled with AtomicBool 2025-09-24 09:12:46 +03:00
Pekka Enberg
aa95cb24ea core: Wrap Connection::page_size with AtomicU16 2025-09-24 09:12:46 +03:00
Pekka Enberg
60d3a837b7 core: Wrap Connection::cache_size with AtomicI32 2025-09-24 09:12:46 +03:00
Pekka Enberg
0b6f535f4a core: Wrap Connection fields with AtomicI64 2025-09-24 09:12:46 +03:00
Jussi Saurio
73571d9c55 Merge 'Don't allow duplicate columns and get column type as passed ' from Pavan Nambi
fixes #3231
```zsh

❯ sqlite3
SQLite version 3.50.4 2025-07-30 19:33:53
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite>   CREATE TABLE t1 (a);
    ALTER TABLE t1 ADD COLUMN a;
Parse error: duplicate column name: a
sqlite>   ALTER TABLE t1 ADD COLUMN name varchar(255);
    SELECT sql FROM sqlite_schema WHERE name = 't1';
CREATE TABLE t1 (a, name varchar(255))
sqlite>
```
```zsh
turso>
turso>  CREATE TABLE t1 (a);
    ALTER TABLE t1 ADD COLUMN a;
  x Parse error: duplicate column name: a

turso>  ALTER TABLE t1 ADD COLUMN name varchar(255);
    SELECT sql FROM sqlite_schema WHERE name = 't1';
┌─────────────────────────────────────────┐
│ sql                                     │
├─────────────────────────────────────────┤
│ CREATE TABLE t1 (a, name varchar (255)) │
└─────────────────────────────────────────┘
turso>
```

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

Closes #3249
2025-09-24 09:04:08 +03:00
Jussi Saurio
75a989a215 Merge 'fix: CTE alias resolution in planner' from Mayank
Closes #3182

Reviewed-by: Preston Thorpe <preston@turso.tech>

Closes #3243
2025-09-24 09:01:45 +03:00
Pekka Enberg
248b38b6a9 Merge 'core: Wrap Connection::transaction_state with RwLock' from Pekka Enberg
Closes #3269
2025-09-24 08:44:40 +03:00
Jussi Saurio
d5de088abe Merge 'translate: implement Sequence opcode and fix sort order' from Preston Thorpe
This PR implements the `Sequence` and `SequenceTest` opcodes, although
does not yet add plumbing to emit the latter.
SQLite has two distinct mechanisms that determine the final row order
with aggregates:
Traversal order of GROUP BY, and ORDER BY tiebreaking. When ORDER BY
contains only aggregate expressions and/or constants, SQLite has no
extra tiebreak key, but when ORDER BY mixes aggregate and non-aggregate
terms, SQLite adds an implicit, stable row `sequence` so “ties” respect
the input order.
This PR also fixes an issue with a query like the following:
```sql
SELECT u.first_name, COUNT(*) AS c
FROM users u
JOIN orders o ON o.user_id = u.id
GROUP BY u.first_name
ORDER BY c DESC;
```
Because ORDER BY has only an aggregate (COUNT(*) DESC) and no non-
aggregate terms, SQLite traverses the group key (u.first_name) in DESC
order in this case, so ties on c naturally appear with group keys in
descending order.
Previously tursodb would return the group key sorted in ASC order,
because it was used in all cases as the default

Closes #3287
2025-09-24 08:38:08 +03:00
PThorpe92
58625b1c6d Use expr.is_constant instead of matching for literal directly 2025-09-23 23:08:04 -04:00
PThorpe92
376d2bf7b1 Add plumbing to add sequence column to stabilize tiebreakers in order+group by 2025-09-23 22:35:59 -04:00
PThorpe92
5afebc5f74 Add Sequence and SequenceTest opcode to explain 2025-09-23 22:34:33 -04:00
PThorpe92
3c8216caab Add Sequence and SequenceTest opcodes to vdbe and sorter 2025-09-23 22:34:13 -04:00
Pekka Enberg
f5d3962459 core: Wrap Connection::transaction_state with RwLock 2025-09-23 14:01:31 +03:00
Pekka Enberg
fa8065ca52 core: Wrap Connection::autocommit in AtomicBool 2025-09-23 13:18:49 +03:00
Pekka Enberg
1b6050338d core: Wrap Connection::database_schemas in RwLock 2025-09-23 11:50:43 +03:00
Pekka Enberg
233beeb8e7 Merge 'core: Wrap Connection::schema in RwLock' from Pekka Enberg
Closes #3261
2025-09-23 11:50:24 +03:00
Pekka Enberg
b94aa22499 core: Wrap Connection::schema in RwLock 2025-09-23 10:31:20 +03:00
Pavan-Nambi
59660f0c25 remove tests for column type spaces 2025-09-23 12:48:35 +05:30
Pekka Enberg
9d395a5a52 Merge 'Stop incrementing n_changes for idx delete' from Preston Thorpe
closes #3259
Previously we were emitting the following:
<img width="483" height="135" alt="image" src="https://github.com/user-
attachments/assets/e12100ed-5815-4619-829a-3230eb8c8f7f" />
After:
<img width="484" height="310" alt="image" src="https://github.com/user-
attachments/assets/28591f52-18b1-4060-8c92-7a3f7194fca0" />

Closes #3258
2025-09-23 07:30:41 +03:00
Pekka Enberg
b857f94fe4 Merge 'core: Wrap Connection::pager in RwLock' from Pekka Enberg
Closes #3247
2025-09-23 07:29:09 +03:00
PThorpe92
9238584a75 Stop incrementing n_changes for idx delete 2025-09-22 19:10:35 -04:00
PThorpe92
10662ee5c5 Fix error in test missing DatabaseOpts field 2025-09-22 11:28:20 -04:00
PThorpe92
8420b9be04 Disable runtime extension loading unless enabled 2025-09-22 11:28:19 -04:00
PThorpe92
4ac4aff30c Add a flag to DatabaseOpts, only for cli_only feature to enable rt extension loading 2025-09-22 11:28:19 -04:00
PThorpe92
d2c643da06 Add cli_only feature to core 2025-09-22 11:28:19 -04:00
Jussi Saurio
1d4b301f05 Merge 'mvcc: simplify StateMachine' from Jussi Saurio
`TransitionResult::Continue` is an internal implementation detail that
tells an invocation of `StateMachine::step()` to continue looping, but
it is of no use to upstream callers.
For this reason, just return an IOResult from StateMachine::step() which
simplifies the result handling.

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

Closes #3248
2025-09-22 17:03:16 +03:00
Pekka Enberg
aa454a6637 core: Wrap Connection::pager in RwLock 2025-09-22 17:02:08 +03:00
Pekka Enberg
69b2e86c9c Merge 'Fix busy handler' from Lâm Hoàng Phúc
@penberg i think it fixed #3144, but i got locked database error
```sh
Running write throughput benchmark with 5 threads, 1000 batch size, 1000 iterations, mode: Legacy
Database created at: write_throughput_test.db
Thread error 0: SQL execution failure: `database is locked`
Thread 1: 1000000 inserts in 514.45s (1943.82 inserts/sec)
Error: SqlExecutionFailure("database is locked")
```

Closes #3147
2025-09-22 16:43:51 +03:00
Jussi Saurio
4af49ef98c mvcc: simplify StateMachine
TransitionResult is an internal implementation detail that tells
an invocation of StateMachine::step() to continue looping, but it
is of no use to other callers.

For this reason, just return an IOResult from StateMachine::step()
which simplifies the result handling.
2025-09-22 16:37:31 +03:00
Jussi Saurio
5498816d0b Merge 'mvcc: add blocking checkpoint lock' from Jussi Saurio
This PR does not implement MVCC checkpoint yet, just adds a lock for it.
MVCC checkpoints are always TRUNCATE, plus they block all other
transactions. This guarantees that never need to let transactions read
from the SQLite WAL.
In MVCC, the checkpoint procedure is roughly as follows:
- Take the blocking_checkpoint_lock
- Write everything in the logical log to the pager, and from there
commit to the SQLite WAL.
- Immediately TRUNCATE checkpoint the WAL into the database file.
- Release the blocking_checkpoint_lock.

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

Closes #3244
2025-09-22 16:16:26 +03:00
Preston Thorpe
44dc4c9636 Merge 'translate/emitter: Implement partial indexes' from Preston Thorpe
This PR adds support for partial indexes, e.g. `CREATE INDEX` with a
provided predicate
```sql
CREATE UNIQUE INDEX idx_expensive ON products(sku) where price > 100;
```
The PR does not yet implement support for using the partial indexes in
the optimizer.

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

Closes #3228
2025-09-22 09:09:54 -04:00
Pavan-Nambi
8dc485e5f7 don't allow duplicate columns and get column type more precisely 2025-09-22 17:39:08 +05:30
Pekka Enberg
372daef656 core: Wrap Pager::io_ctx in RwLock 2025-09-22 15:00:29 +03:00
Pekka Enberg
2af98223ae Merge 'Enable checksum tests if checksum feature is on' from Kacper Kołodziej
These tests fail if checksum feature is turned off.

Closes #3242
2025-09-22 14:40:34 +03:00
Pere Diaz Bou
2f4426fc33 core/mvcc/logical-log: change order of Data size in InsertRow 2025-09-22 13:25:46 +02:00
Pere Diaz Bou
4cc88ee2bb core/mvcc/logical-log: rename Insert and Delete -> InsertRow and DeleteRow in LogRecordType 2025-09-22 13:21:54 +02:00