Commit Graph

5154 Commits

Author SHA1 Message Date
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
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
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
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
Pekka Enberg
49f551e6c3 Merge 'Wrap Pager vacuum state in RwLock' from Pekka Enberg
Reviewed-by: Jussi Saurio <jussi.saurio@gmail.com>

Closes #3239
2025-09-22 13:44:22 +03:00
Jussi Saurio
6a20735fe0 mvcc: add blocking checkpoint lock
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.
2025-09-22 12:40:19 +03:00
TcMits
17c91c1fe2 resolve conflicts 2025-09-22 16:03:52 +07:00
Kacper Kołodziej
76f2e4e217 Enable checksum tests if checksum feature is on
These tests fail if checksum feature is turned off.
2025-09-22 10:46:49 +02:00
Pekka Enberg
37866e74e5 Merge 'core/io: Ensure callbacks are invoked once' from Pedro Muniz
Add a `Once` object to uphold this property. We cannot use the OnceLock
to dictate this, because if we set the OnceLock before actually calling
the callback, there is a moment in time where we will have an incorrect
transient state. This change ensures we atomically call the callback and
then set the OnceLock
Should fix #3217
Closes #3217

Closes #3237
2025-09-22 11:44:39 +03:00
Pekka Enberg
0144ea8059 Merge 'Support UNION queries in DBSP-based Materialized Views' from Glauber Costa
UNION queries, while useful on their own, are a cornerstone of recursive
CTEs.
This PR implements:
* the merge operator, required to merge both sides of a union query.
* the circuitry necessary to issue the Merge operator.
* extraction of tables mentioned in union and CTE expressions, so we can
correctly populate tables that contain them.

Closes #3234
2025-09-22 11:33:19 +03:00
Pekka Enberg
19849c7895 Merge 'DBSP: Return a parse error for a non-equality join' from Glauber Costa
We currently don't handle non equality, but end up just returning a
bogus result. Let's parse error.

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

Closes #3232
2025-09-22 11:32:43 +03:00
Pekka Enberg
6f258b37d9 core/storage: Wrap Pager vacuum state in RwLock 2025-09-22 10:34:58 +03:00
Jussi Saurio
c0fc2ad234 fix optimizer tests 2025-09-22 10:18:03 +03:00
Jussi Saurio
eada24b508 Store in-memory index definitions most-recently-seen-first
This solves an issue where an INSERT statement conflicts with
multiple indices. In that case, sqlite iterates the linked list
`pTab->pIndex` in order and handles the first conflict encountered.
The newest parsed index is always added to the head of the list.

To be compatible with this behavior, we also need to put the most
recently parsed index definition first in our indexes list for a given
table.
2025-09-22 10:11:50 +03:00
Pekka Enberg
f053b76518 core/storage: Move vacuum state machines to VacuumState 2025-09-22 09:37:40 +03:00
Pekka Enberg
6280cfc59d Merge branch 'main' into sync-improvements 2025-09-22 07:35:39 +03:00
Pekka Enberg
dae72a7418 Merge 'Remove some unnecessary unsafe impls' from Pedro Muniz
Reviewed-by: Jussi Saurio <jussi.saurio@gmail.com>

Closes #3236
2025-09-22 07:33:50 +03:00
Glauber Costa
2627ad44de support union statements in the DBSP circuit compiler 2025-09-21 21:00:27 -03:00
Glauber Costa
b419db489a Implement the DBSP merge operator
The Merge operator is a stateless operator that merges two deltas.
There are two modes: Distinct, where we merge together values that
are the same, and All, where we preserve all values. We use the rowid of
the hashable row to guarantee that: In Distinct mode, the rowid is set
to 0 in both sides. If they values are the same, they will hash to the
same thing. For All, the rowids are different.

The merge operator is used for the UNION statement, which is a
cornerstone of Recursive CTEs.
2025-09-21 21:00:27 -03:00
Glauber Costa
9f54f60d45 make sure that complex select statements are captured by MV populate
The population code extracts table information from the select statement
so it can populate the materialized view. But the code, as written
today, is naive. It doesn't capture table information correctly if there
is more than one select statement (such in the case of a union query).
2025-09-21 21:00:27 -03:00
pedrocarlo
ffeb26b24a only ever call callbacks once 2025-09-21 14:36:18 -03:00
PThorpe92
a1ca56620a Add SQLITE_CONSTRAINT_UNIQUE constraint to op_halt handling 2025-09-21 13:29:01 -04:00
PThorpe92
6fb4b03801 Fix UPSERT handling, properly rebuild indexes only based on what columns they touch 2025-09-21 13:28:36 -04:00
PThorpe92
0ea6e5714d Separate UPSERT behavior into preflight and commit state to prevent inserting idx before violating unique constraint 2025-09-21 13:27:50 -04:00
PThorpe92
e545e75e31 Emit Affinity instruction for unique index, and use no_constant_opt 2025-09-21 13:24:48 -04:00
PThorpe92
63177c42e4 Add SQLITE_CONSTRAINT_UNIQUE error constant 2025-09-21 13:22:31 -04:00
pedrocarlo
e5dfc942b1 remove some unnecessary unsafe impls 2025-09-21 13:29:59 -03:00
Glauber Costa
13260349b0 Return a parse error for a non-equality join
We currently don't handle non equality, but end up just returning a
bogus result. Let's parse error.
2025-09-20 20:35:10 -03:00
PThorpe92
03149bc92d Remove unused imports 2025-09-20 18:32:37 -04:00
PThorpe92
62ee68e4dd Fix INSERT/UPSERT to properly handle and/or reject partial indexes 2025-09-20 18:32:03 -04:00