Commit Graph

6501 Commits

Author SHA1 Message Date
Jussi Saurio
2b045ccfd8 btree: clear overflow pages when insert overwrites a cell 2025-07-24 13:44:11 +03:00
Jussi Saurio
025ea8808a Merge 'WAL insert: mark pages as dirty' from Nikita Sivukhin
WAL insert API introduced in the #2231 works incorrectly as it never
mark inserted pages as dirty.
This PR fixes this issue and also add simple fuzz test which fails
without fixes.

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

Closes #2245
2025-07-24 12:58:01 +03:00
Jussi Saurio
9a08c57b58 Merge 'make add dirty to change flag and also add page to the dirty list' from Nikita Sivukhin
Make `add_dirty` helper to set flag and add page to the dirt list. This
makes API safer as now its harder to do one thing and forget about
another (which can lead to DB corruption).

Reviewed-by: Pere Diaz Bou <pere-altea@homail.com>
Reviewed-by: Jussi Saurio <jussi.saurio@gmail.com>

Closes #2244
2025-07-24 12:22:33 +03:00
Nikita Sivukhin
edd6ef2d21 fix after rebase 2025-07-24 11:51:33 +04:00
Nikita Sivukhin
3d2a38eb88 add simple helper 2025-07-24 11:49:39 +04:00
Nikita Sivukhin
fb83862013 fix clippy 2025-07-24 11:49:39 +04:00
Nikita Sivukhin
4a80306705 fix wal insert frame raw API
- we need to properly mark pages as dirty after insertion
2025-07-24 11:49:39 +04:00
Nikita Sivukhin
435ca7fe7a add fuzz tests for raw WAL API 2025-07-24 11:49:39 +04:00
Nikita Sivukhin
d618463906 simplify add_dirty API 2025-07-24 11:29:01 +04:00
Jussi Saurio
2d3c9001ee Merge 'emit SetCookie after DropTable' from Glauber Costa
The SetCookie opcode is used, among other things, to notify the
transaction of schema changes. We are not issuing it on DropTable.
Without it, the transaction thinks the schema hasn't changed, and does
not update the schema of the connection back to the database.
SQLite will, of course, issue it:
35    DropTable      0     0     0     foo            0
36    SetCookie      0     1     2                    0
Unfortunately I don't have a unit test that breaks with this, because
the one that is supposed to break is having, let's put it this way,
bigger problems.

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

Closes #2249
2025-07-24 10:12:16 +03:00
Jussi Saurio
92a10f94d8 Merge 'Bail early for read-only virtual tables' from Preston Thorpe
This PR adds a const associated value on the VTabModule trait,
`READONLY` defaulted to `true`, so we can bail early when a write
operation is done on an invalid vtable.
This prevents extensions from having to implement `insert`,`update`,
`delete` just to return `Error::ReadOnly`, and prevents us from having
to step through `VUpdate` just to error out.

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

Closes #2247
2025-07-24 10:12:07 +03:00
Jussi Saurio
9b5ef02cfb Merge 'measure only the time it takes to open the actual connection' from Glauber Costa
The current code includes creating the database object, which is slow.
Unfortunately the same cannot be done on the standard SQLite.

Reviewed-by: Preston Thorpe (@PThorpe92)

Closes #2242
2025-07-24 10:09:00 +03:00
Jussi Saurio
dd30729f5e Merge 'Explicit rowid insert' from Nikita Sivukhin
This PR adds support for `INSERT` queries with explicit value for
`rowid` column (not thought rowid alias):
```
turso> create table t(x, y, z);
turso> insert into t(rowid, x, y, z) values (10, 1, 2, 3);
turso> select rowid, * from t;
┌───────┬───┬───┬───┐
│ rowid │ x │ y │ z │
├───────┼───┼───┼───┤
│    10 │ 1 │ 2 │ 3 │
└───────┴───┴───┴───┘
```

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

Closes #2239
2025-07-24 10:08:42 +03:00
Jussi Saurio
49b2bf4fdb Merge 'Deserialize keys only once when sorting immutable records' from Iaroslav Zeigerman
Before this update, the entire immutable record was **fully**
deserialized **every** time it was compared in the sorter.
This PR extends the sorter with incremental deserialization of record
keys, only when needed and only if they weren’t already deserialized in
a previous iteration.
I hate that we panic on failed deserialization in `cmp`, but
unfortunately, I can’t return `Result` as part of this interface.
Looking for feedback around a better way to handle this.
Alternatively, I could store the deserialization error as part of
`SortableImmutableRecord` and check it before returning the record in
`next`, thereby deferring the error handling. The downside of this
approach is that it complicates debugging, since the error will be
completely decoupled from the place where it occurs.

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

Closes #2207
2025-07-24 10:08:16 +03:00
Jussi Saurio
52b4c22be9 Merge 'fix: SUM returns correct float for mixed numeric/non-numeric types & return value on empty set' from Axel Tobieson Rova
# Fix SUM aggregate function for mixed types
Fixes #2133
The SUM aggregate function was returning incorrect results when
processing tables with mixed numeric and non-numeric values. According
to SQLite documentation:
> "If any input to sum() is neither an integer nor a NULL, then sum()
returns a floating point value"
[*](https://sqlite.org/lang_aggfunc.html)
Now both SQLite and Turso yield the same output of 44.0.
--
I modified `Sum` to increment only for numeric values, skipping non-
numeric values. However, if we have mixed numeric values or non-numeric
values, we return a float output. Added a flag to keep track of it.
as pointed out by @FHaggs , If there are no non-NULL input rows then
sum() returns NULL but total() returns 0.0. I decided to include it in
this PR as well. Empty was such a natural test case.

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

Closes #2182
2025-07-24 10:08:01 +03:00
Glauber Costa
2a2468026c emit SetCookie after DropTable
The SetCookie opcode is used, among other things, to notify the
transaction of schema changes. We are not issuing it on DropTable.

Without it, the transaction thinks the schema hasn't changed, and does
not update the schema of the connection back to the database.

SQLite will, of course, issue it:

35    DropTable      0     0     0     foo            0
36    SetCookie      0     1     2                    0

Unfortunately I don't have a unit test that breaks with this, because
the one that is supposed to break is having, let's put it this way,
bigger problems.
2025-07-23 19:34:41 -05:00
PThorpe92
3358e85889 Update py tests for new error msg 2025-07-23 17:05:46 -04:00
PThorpe92
d7c3256a5a Update vtab derive macro to pass readonly const to module impl 2025-07-23 16:59:18 -04:00
PThorpe92
b68539fc45 Make the ReadOnly error more generic 2025-07-23 16:58:22 -04:00
PThorpe92
0871a8c7f3 Bail early when we detect a readonly virtual table 2025-07-23 16:57:30 -04:00
PThorpe92
e7ce3efb3f Remove default trait impl from csv extension 2025-07-23 16:50:57 -04:00
PThorpe92
9c3f9426c3 Add readonly method for VirtualTable to bail early 2025-07-23 16:49:42 -04:00
PThorpe92
eff455fb03 Add READONLY const property to virtual table module trait 2025-07-23 16:44:04 -04:00
Iaroslav Zeigerman
1e51d23bd6 store the key deserialization error instead of panicking 2025-07-23 11:22:01 -07:00
Nikita Sivukhin
f4a40c43cd fix clippy 2025-07-23 20:19:00 +04:00
Nikita Sivukhin
30c7bef27b make add dirty to change flag and also add page to the dirty list 2025-07-23 20:06:49 +04:00
Glauber Costa
5a66ed8433 measure only the time it takes to open the actual connection
The current code includes creating the database object, which is slow.

Unfortunately the same cannot be done on the standard SQLite.
2025-07-23 08:41:36 -05:00
Nikita Sivukhin
001670c069 fix clippy 2025-07-23 16:00:24 +04:00
Nikita Sivukhin
a017baced7 small refactoring 2025-07-23 15:56:38 +04:00
Nikita Sivukhin
d3f3807ede fix cdc emit 2025-07-23 15:50:34 +04:00
Nikita Sivukhin
fd63128227 adjust behaviour in case when both rowid alias and rowid provided 2025-07-23 15:42:05 +04:00
Nikita Sivukhin
a4d114460a adjust vtable insert 2025-07-23 15:22:56 +04:00
Jussi Saurio
1e38202084 Merge 'WAL insert API' from Nikita Sivukhin
This PR implements missing raw WAL API from LibSQL for future use for
offline-sync feature:
1. `wal_insert_begin` - begin WAL session by opening WAL read/write
transaction
2. `wal_insert_end` - finish WAL session by closing WAL transaction
opened by `wal_insert_begin` call
3. `wal_insert_frame` - insert frame `frame_no` with raw content `frame`
(WAL frame included)
For now any schema changes will not be reflected after
`wal_insert_frame` because `turso-db` do not re-parse schema without
need. I will fix this in follow up PR.

Reviewed-by: Pekka Enberg <penberg@iki.fi>

Closes #2231
2025-07-23 14:08:15 +03:00
Nikita Sivukhin
0178b41b28 accept explicit "rowid" column name in the INSERT statement 2025-07-23 15:03:38 +04:00
Jussi Saurio
63f488a1cc Merge 'Pager: clear overflow cells when freeing page' from Jussi Saurio
## Background
The `balance_non_root` procedure can end up freeing a page if the pages
to be balanced can fit the required combined number of cells in less
pages, even if the page that triggered balancing is overfull. This can
then free the originally overfull pages, leaving a non-zero
`overflow_cells` on the in-mem representation of the page.
```rust
balance_non_root: page=305, overflow_cells=0
balance_non_root: page=304, overflow_cells=0
balance_non_root: page=302, overflow_cells=1
pre_edit_page(page=304, page_idx=0, new_cells=4, old_cells=1, cells_per_page_old=[1, 3, 9, 0, 0], cells_per_page_new=[4, 9, 9, 0, 0], cell_array_count=9)
edit_page start_old_cells=0 start_new_cells=0 number_new_cells=4 cell_array=9 end_old_cells=1 end_new_cells=4
pre_edit_page(page=305, page_idx=1, new_cells=4, old_cells=1, cells_per_page_old=[1, 3, 9, 0, 0], cells_per_page_new=[4, 9, 9, 0, 0], cell_array_count=9)
edit_page start_old_cells=2 start_new_cells=5 number_new_cells=4 cell_array=9 end_old_cells=3 end_new_cells=9
balance_non_root: sibling_count_new=2, sibling_count=3

// Custom assertion to demonstrate this:
thread 'main' panicked at core/storage/pager.rs:1127:29:
Pager::free_page: In memory page with id 302 has overflow cells
```
## Why is this a problem
Right now this is not an immediate problem, because we always allocate
brand new pages. However, in #2233 we begin to reuse pages from the
freelist for page allocation to improve performance and reduce database
size bloat. In that PR, the `balance_non_root` procedure will calculate
cell counts incorrectly in `edit_page()` and panic if: 1. a new
allocated page is taken from the freelist, 2. the page is still in
memory, and 3. and it still contains `overflow_cells`.
## Solution
Clear `page_contents.overflow_cells` when an in-memory page is freed.

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

Closes #2238
2025-07-23 13:38:31 +03:00
Jussi Saurio
ab92aabb70 Merge 'types: less noisy Debug implementation for ImmutableRecord' from Jussi Saurio
This PR truncates the Debug output of ImmutableRecord so that blobs and
texts only show the first 20 bytes or chars respectively. Debugging gets
much better when we don't print a huge blob or text a million times.

Closes #2237
2025-07-23 12:20:54 +03:00
Jussi Saurio
f98a9e8939 Pager: don't assume page is necessarily in memory anymore 2025-07-23 11:08:34 +03:00
Jussi Saurio
ecb5fce1bd Pager: clear overflow cells when freeing page 2025-07-23 10:58:10 +03:00
Jussi Saurio
ffd2299aa1 types: less noisy Debug implementation for ImmutableRecord 2025-07-23 10:56:41 +03:00
Nikita Sivukhin
60eaa11add hide new methods behind fs feature 2025-07-23 11:51:39 +04:00
Nikita Sivukhin
a85283a84f add trailing comma 2025-07-23 11:31:00 +04:00
Nikita Sivukhin
3c0af3e389 small adjustments 2025-07-23 11:31:00 +04:00
Nikita Sivukhin
2283a04aab add more tests 2025-07-23 11:31:00 +04:00
Nikita Sivukhin
73761a8983 rollback non-commited changes 2025-07-23 11:31:00 +04:00
Nikita Sivukhin
bf2bfbe978 fix clippy 2025-07-23 11:31:00 +04:00
Nikita Sivukhin
16763e1500 implement raw WAL write api 2025-07-23 11:30:59 +04:00
Nikita Sivukhin
bc09ea6e98 make end_write_txn/end_read_txn function non-failing 2025-07-23 11:30:29 +04:00
PThorpe92
cb42102a6e Merge 'silence clippy errors with features disabled' from Glauber Costa
When compiling with features disabled, there are lots of clippy
warnings. This PR silences them.
For the utils file, I am using a bit of a hammer and just allowing
unused stuff in the whole file. Due to the box of utilities nature of
this file, it'll always be the case that things will be unused depending
on the feature-set.

Reviewed-by: Preston Thorpe (@PThorpe92)

Closes #2236
2025-07-22 21:51:27 -04:00
PThorpe92
a13fc3515e Fix cargo fmt warning 2025-07-22 21:47:15 -04:00
Glauber Costa
a10d8d7f94 silence clippy errors with features disabled
When compiling with features disabled, there are lots of clippy
warnings. This PR silences them.

For the utils file, I am using a bit of a hammer and just allowing
unused stuff in the whole file. Due to the box of utilities nature of
this file, it'll always be the case that things will be unused depending
on the feature-set.
2025-07-22 20:37:45 -05:00