Commit Graph

5314 Commits

Author SHA1 Message Date
Pekka Enberg
8d9d2dad1d core/storage: Wrap WalFile::syncing with AtomicBool 2025-09-27 14:07:26 +03:00
Jussi Saurio
283fba2e0d use normalized table name 2025-09-27 09:53:11 +03:00
Jussi Saurio
67d320960d ALTER TABLE: prevent dropping/renaming column referenced in VIEW 2025-09-27 09:45:15 +03:00
Jussi Saurio
3137357092 ALTER TABLE: prevent dropping indexed column in VDBE layer 2025-09-27 09:45:15 +03:00
Jussi Saurio
085b92dc4e ALTER TABLE: prevent dropping indexed columns in translate layer 2025-09-27 09:45:15 +03:00
Jussi Saurio
a2d833c073 ALTER TABLE: add comment about things preventing drop column 2025-09-27 09:45:15 +03:00
Pekka Enberg
d796964a1e Merge 'support mixed integer and float expressions in the expr_compiler' from Glauber Costa
Fixes #3373

Closes #3387
2025-09-27 08:22:14 +03:00
Pekka Enberg
65b382b9e1 Merge 'Make MVCC code Send and Sync' from Pekka Enberg
Closes #3361
2025-09-27 08:21:01 +03:00
Pekka Enberg
92291ed736 Merge 'Fix offset variable handling' from Nikita Sivukhin
Before, db generated incorrect plan in case when offset parameter were
introduced as variable:
```
turso> EXPLAIN SELECT * FROM users LIMIT ? OFFSET ?;
addr  opcode             p1    p2    p3    p4             p5  comment
----  -----------------  ----  ----  ----  -------------  --  -------
0     Init               0     16    0                    0   Start at 16
1     Variable           1     1     0                    0   r[1]=parameter(1); OFFSET expr
2     MustBeInt          1     0     0                    0
3     Variable           2     2     0                    0   r[2]=parameter(2); OFFSET expr
4     MustBeInt          2     0     0                    0
5     OffsetLimit        1     3     2                    0   if r[1]>0 then r[3]=r[1]+max(0,r[2]) else r[3]=(-1)
6     OpenRead           0     2     0                    0   table=users, root=2, iDb=0
7     Rewind             0     15    0                    0   Rewind table users
8       Variable         2     2     0                    0   r[2]=parameter(2); OFFSET expr
9       MustBeInt        2     0     0                    0
10      IfPos            2     14    1                    0   r[2]>0 -> r[2]-=1, goto 14
11      Column           0     0     4                    0   r[4]=users.x
12      ResultRow        4     1     0                    0   output=r[4]
13      DecrJumpZero     1     15    0                    0   if (--r[1]==0) goto 15
14    Next               0     8     0                    0
15    Halt               0     0     0                    0
16    Transaction        0     1     1                    0   iDb=0 tx_mode=Read
17    Goto               0     1     0                    0
```

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

Closes #3360
2025-09-27 08:20:54 +03:00
Pekka Enberg
a932ac5450 Merge 'core: recover logical log on Database::connect ' from Pere Diaz Bou
If we open database and logical log is not empty we need to recover from
it. We also make sure a single recover executes concurrently and other
connections just wait for it to finish.
I also changed the fuzz tester to use `restart` instead of calling
`load_logical_log` manually to test this behaviour.

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

Closes #3359
2025-09-27 08:20:41 +03:00
Glauber Costa
d28022b491 support mixed integer and float expressions in the expr_compiler
Fixes #3373
2025-09-26 21:11:38 -03:00
Preston Thorpe
5dc30b5a68 Merge 'core/storage: Display page category for rowid integrity check failure' from Pekka Enberg
Let's add more hints to hunt down the reason for #2896.

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

Closes #3378
2025-09-26 19:07:45 -04:00
Preston Thorpe
e43184fb98 Merge 'translate: refactor arguments and centralize parameter context' from Preston Thorpe
This PR contains _no semantic changes_.
I made this cleanup on another branch when I was working on subqueries,
as the inconsistency with passing around `Schema` and `SymbolTable` had
been kinda bothering me. By adding `param_ctx` to the program, it
prevents accidentally resetting it to zero.

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

Closes #3382
2025-09-26 13:05:29 -04:00
PThorpe92
d9658070a9 Fix clippy warnings 2025-09-26 12:17:34 -04:00
PThorpe92
5fcc187434 translate: refactor arguments and centralize parameter context 2025-09-26 12:06:44 -04:00
Pekka Enberg
931cf2658e core/storage: Display page category for rowid integrity check failure
Let's add more hints to hunt down the reason for #2896.
2025-09-26 18:25:49 +03:00
Pere Diaz Bou
99adf73168 core/mvcc/logical-log: rename to needs_recovery 2025-09-26 16:59:57 +02:00
Nikita Sivukhin
f80650586a remove misleading comment 2025-09-26 18:06:20 +04:00
Nikita Sivukhin
63a9fa8c28 fix handling of offset parameter set through variable
- before the fix db generated following plan:

turso> EXPLAIN SELECT * FROM users LIMIT ? OFFSET ?;
addr  opcode             p1    p2    p3    p4             p5  comment
----  -----------------  ----  ----  ----  -------------  --  -------
0     Init               0     16    0                    0   Start at 16
1     Variable           1     1     0                    0   r[1]=parameter(1); OFFSET expr
2     MustBeInt          1     0     0                    0
3     Variable           2     2     0                    0   r[2]=parameter(2); OFFSET expr
4     MustBeInt          2     0     0                    0
5     OffsetLimit        1     3     2                    0   if r[1]>0 then r[3]=r[1]+max(0,r[2]) else r[3]=(-1)
6     OpenRead           0     2     0                    0   table=users, root=2, iDb=0
7     Rewind             0     15    0                    0   Rewind table users
8       Variable         2     2     0                    0   r[2]=parameter(2); OFFSET expr
9       MustBeInt        2     0     0                    0
10      IfPos            2     14    1                    0   r[2]>0 -> r[2]-=1, goto 14
11      Column           0     0     4                    0   r[4]=users.x
12      ResultRow        4     1     0                    0   output=r[4]
13      DecrJumpZero     1     15    0                    0   if (--r[1]==0) goto 15
14    Next               0     8     0                    0
15    Halt               0     0     0                    0
16    Transaction        0     1     1                    0   iDb=0 tx_mode=Read
17    Goto               0     1     0                    0

- the problem here is that Variable value is re-read at step 8 - which is wrong
2025-09-26 18:05:36 +04:00
Nikita Sivukhin
5b5379d078 propagate context to stringifier to properly derive column names 2025-09-26 17:40:41 +04:00
Nikita Sivukhin
52f3216211 fix avg aggregation
- ignore NULL rows as SQLite do
- emit NULL instead of NaN when no rows were aggregated
- adjust agg column alias name
2025-09-26 17:11:06 +04:00
Pere Diaz Bou
9e47cc3700 clippy 2025-09-26 14:16:11 +02:00
Pekka Enberg
1402e9841e core/mvcc: Wrap StreamingLogicalLogReader::buffer with RwLock 2025-09-26 14:19:16 +03:00
Pekka Enberg
96accef06c core/mvcc: Wrap header with RwLock 2025-09-26 14:10:18 +03:00
Pere Diaz Bou
59d3e37b9f fmt 2025-09-26 13:01:13 +02:00
Pere Diaz Bou
9c1d94a355 core/mvcc/logical-log: assert we don't call begin_load_tx twice 2025-09-26 12:59:13 +02:00
Pere Diaz Bou
4cdf293a2b core/mvcc/logical-log: fuzz test recover use db.restart 2025-09-26 12:56:58 +02:00
Pere Diaz Bou
ae994146af core/mvcc/logical-log: on mvcc restart clear DATABASE_MANAGER 2025-09-26 12:56:43 +02:00
Pere Diaz Bou
2a7abd82f7 core/lib: recover mvcc logical log if needed on connect 2025-09-26 12:47:52 +02:00
Pere Diaz Bou
83d8a7c775 core/mvcc/logical-log: lock recover logical log process 2025-09-26 12:47:31 +02:00
Pekka Enberg
506908e648 Merge 'translate: disallow creating/dropping internal tables' from Jussi Saurio
edit: we can't disallow 'turso_' prefix though, because turso-sync-
engine uses it
Closes #3313

Reviewed-by: Nikita Sivukhin (@sivukhin)

Closes #3338
2025-09-26 10:40:09 +03:00
Pekka Enberg
9461e22c06 Merge 'Improve DBSP view serialization' from Glauber Costa
Improve serialization for DBSP views.
The serialization code was written organically, without much forward
thinking about stability as we evolved the table and operator format.
Now that this is done, we are at at point where we can actually make it
suck less and take a considerable step towards making this production
ready.
We also add a simple version check (in the table name, because that is
much easier than reading contents in parse_schema_row) to prevent views
to be used if we had to do anything to evolve the format of the circuit
(including the operators)

Closes #3351
2025-09-26 09:18:45 +03:00
Jussi Saurio
cfa449a0c0 Merge 'Disallow multiple primary keys in table definition' from Jussi Saurio
Closes #3309

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

Closes #3340
2025-09-26 09:16:14 +03:00
Jussi Saurio
abb0c704af translate: disallow creating/dropping internal tables
we can't disallow 'turso_' prefix though, because turso-sync-engine
uses it
2025-09-26 09:15:32 +03:00
Jussi Saurio
00b69467f3 Merge 'Add CAST to fuzzer' from Levy A.
Adds `CAST` to the fuzzer while fixing some incompatibility bugs.

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

Closes #3314
2025-09-26 09:13:49 +03:00
Jussi Saurio
064cc04b69 Merge 'Fix CREATE INDEX with quoted identifiers' from Iaroslav Zeigerman
Discovered this one while working on #3322
It was a bit more elusive because the original error was essentially a
red herring:
```
turso> CREATE INDEX idx ON "t t" (`a a`);
  × unexpected token at SourceSpan { offset: SourceOffset(22), length: 1 }
   ╭────
 1 │ CREATE INDEX idx ON "t t" (`a a`);
   ·                       ┬
   ·                       ╰── here
   ╰────
  help: expected [TK_LP] but found TK_ID
```

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

Closes #3345
2025-09-26 09:13:30 +03:00
Jussi Saurio
c83ee9fc3e Merge 'Fix ungrouped aggregate with offset clause' from Preston Thorpe
closes #3300

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

Closes #3347
2025-09-26 09:13:16 +03:00
Jussi Saurio
252da9254a fix another incorrect test 2025-09-26 08:59:37 +03:00
Jussi Saurio
3170077952 Fix incorrect test 2025-09-26 08:59:37 +03:00
Jussi Saurio
6d6fc91da3 Disallow multiple primary keys in table definition 2025-09-26 08:59:36 +03:00
Glauber Costa
1b5e74060a make sure that we are able to prevent views from being corrupted
as we make changes to the way materialized views are generated (think
adding new operators, changing the id of existing operators, etc), we
will need to persist the topology of the circuit itself. This is a
change that I believe to be premature. For now, it is enough to reserve
the first operator id for it, and add a version number to the table
name. We can just detect that something changed, and ask the user to
drop the view. We can get away with it due to the fact that the views
are experimental.
2025-09-25 22:52:08 -03:00
Glauber Costa
3dc1dca5a8 use 128-bit hashes for the zset_id
We have used i64 before because that is the size of an integer in
SQLite. However, I believe that for large enough databases, the chances
of collision here are just too high. The effect of a collision is the
database silently returning incorrect data in the materialized view.

So now that everything else is working, we should move to i128.
2025-09-25 22:52:08 -03:00
Glauber Costa
b9011dfa16 Replace custom serialization with a saner version
The Materialized View code had custom serialization written so we could
move this code forward. Now that we have many operators and the views
work, replace it with something saner.

The main insight is that if we transform the AggregateState into Values
before the serialization, we are able to just use standard SQLite
serialization for the values. We then just have to add sizes, codes for
the functions, etc (which are also represented as Values).
2025-09-25 22:52:08 -03:00
PThorpe92
3a5b9dcf35 Fix UPDATE behavior to handle explicit rowid update on non rowid alias columns 2025-09-25 19:16:00 -04:00
PThorpe92
52e9bb8949 Create sentinel constant value for rowid to handle direct update of rowid col with no alias 2025-09-25 19:15:28 -04:00
PThorpe92
429a9e4bf0 Put comment back 2025-09-25 17:14:54 -04:00
PThorpe92
b7a431b79e Fix ungrouped aggregate with offset clause 2025-09-25 16:47:25 -04:00
Preston Thorpe
d44a1f8bca Merge 'Implement json_tree' from Mikaël Francoeur
This PR implements the `json_tree` table-valued function.
It's not 100% compatible with SQLite, because SQLite does some iffy
things with the `key` and `path` columns. I started a
[thread](https://www.sqlite.org/forum/forumpost/48f5763d8c) on their
forum and I linked it to the disabled tests in `json.test`.

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

Closes #3256
2025-09-25 16:45:44 -04:00
Iaroslav Zeigerman
b34e53d6c6 Fix CREATE INDEX with quoted identifiers 2025-09-25 12:55:06 -07:00
Pekka Enberg
ad9c12b094 Merge 'Make Connection Send' from Pekka Enberg
Reviewed-by: Pere Diaz Bou <pere-altea@homail.com>

Closes #3292
2025-09-25 21:04:49 +03:00