Commit Graph

720 Commits

Author SHA1 Message Date
pedrocarlo
d5e365def4 add test 2025-09-30 15:26:13 -03:00
pedrocarlo
ddfe56bbb9 fix substr handling with utf-8 and blobs 2025-09-30 13:38:32 -03:00
pedrocarlo
642679889a simplify exec_trim code + only pattern match on whitespace char 2025-09-30 11:09:47 -03:00
Jussi Saurio
64ce33bd5c Move resolution of tableid/rootpage inside MvCursor constructor 2025-09-30 17:04:37 +03:00
Jussi Saurio
a52dbb7842 Handle table ID / rootpages properly for both checkpointed and non-checkpointed tables
Table ID is an opaque identifier that is only meaningful to the MV store.
Each checkpointed MVCC table corresponds to a single B-tree on the pager,
which naturally has a root page.

We cannot use root page as the MVCC table ID directly because:
- We assign table IDs during MVCC commit, but
- we commit pages to the pager only during checkpoint
which means the root page is not easily knowable ahead of time.

Hence, we:

- store the mapping between table id and btree rootpage
- sqlite_schema rows will have a negative rootpage column if the
  table has not been checkpointed yet.
2025-09-30 16:53:12 +03:00
Jussi Saurio
35b584f050 Merge 'core: change root_page to i64' from Pere Diaz Bou
Closes #3454
2025-09-30 12:50:23 +03:00
Pekka Enberg
1b991156f3 Merge 'core/vdbe: Fix BEGIN after BEGIN CONCURRENT check' from Pekka Enberg
We're supposed to error out only when "is_begin" is true.

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

Closes #3450
2025-09-30 11:34:40 +03:00
Pekka Enberg
9b83fe7abf core/vdbe: Fix BEGIN after BEGIN CONCURRENT check
We're supposed to error out only when "is_begin" is true.
2025-09-30 10:55:13 +03:00
Jussi Saurio
9681377c51 Merge 'sum() can throw integer overflow' from Duy Dang
close #3311

Closes #3416
2025-09-30 10:38:37 +03:00
Jussi Saurio
594bdce999 Merge 'sum should identify if there is num in strings/prefix of strings' from Pavan Nambi
closes #3285
maybe adding seperate func for it is stupid but i kept running into
issues with not closing some random `}` and it got annoying real quick
so i just moved tht into its own func. - and as i am using same logic in
3 places i think it's ok.

Closes #3412
2025-09-30 10:37:17 +03:00
Jussi Saurio
dc1861d806 Assert we have the only strong reference instead of falling back to COW 2025-09-30 10:12:20 +03:00
Jussi Saurio
6bff9e53e5 Fix index bookkeeping in DROP COLUMN
See #3448 which this issue closes.
2025-09-30 10:00:16 +03:00
Diego Reis
c9421e034d fix(3306): substr scalar should also work with non-text values 2025-09-29 21:42:21 -03:00
Pere Diaz Bou
0f631101df core: change page idx type from usize to i64
MVCC is like the annoying younger cousin (I know because I was him) that
needs to be treated differently. MVCC requires us to use root_pages that
might not be allocated yet, and the plan is to use negative root_pages
for that case. Therefore, we need i64 in order to fit this change.
2025-09-29 18:38:43 +02:00
Preston Thorpe
da599a1fb8 Merge 'quoting fix' from Nikita Sivukhin
This PR moves part of string normalization to the parser layer.
Now, we dequote and unescape values in the parser, but we still need to
lowercase them for proper ignore-case comparison logic in the planner.
The reason to not lowercase in the parser is following:
1. SQLite (and tursodb) have ident->string conversion rule and by
lowercasing value early we will loose original representation
2. Some things like column names are preserve the case right now and we
better to not change this behaviour.

Closes #3344
2025-09-29 12:33:42 -04:00
Pekka Enberg
1f86400cec Merge 'core/vdbe: Wrap Program::n_change with AtomicI64' from Pekka Enberg
Closes #3424
2025-09-29 18:11:18 +03:00
Pekka Enberg
5f9287304b core/vdbe: Wrap Program::n_change with AtomicI64 2025-09-29 17:09:33 +03:00
Nikita Sivukhin
a142c59de4 use explicit null if it set instead of column default value 2025-09-29 16:28:09 +04:00
Nikita Sivukhin
86a95e813d Merge branch 'main' into quoting-fix-attempt-2 2025-09-29 10:58:51 +04:00
Duy Dang
8562737d77 sum() can throw integer overflow 2025-09-29 00:27:59 +07:00
Pavan-Nambi
074a363c30 sum should identify if there is num in strings/prefix of strings 2025-09-28 17:23:55 +05:30
Pekka Enberg
5ff0044961 Merge 'length shall not count when it sees nullc' from Pavan Nambi
fixes #3317

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

Closes #3356
2025-09-27 16:50:50 +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
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
Pavan-Nambi
fdabbed539 length shall not count when it sees nullc 2025-09-26 15:07:33 +05:30
Nikita Sivukhin
ae24d637a8 adjust edge-cases 2025-09-26 13:01:49 +04:00
Nikita Sivukhin
12b89fd2f1 do not use Name::new 2025-09-26 13:01:49 +04:00
Nikita Sivukhin
fdf8ca88fd introduce exact(...) function - because enum variant will disappear 2025-09-26 13:01:49 +04:00
Nikita Sivukhin
2f4d76ec6d remove pattern matching over Name::Quoted 2025-09-26 13:01:49 +04: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
Pekka Enberg
9c9b525f8a Merge 'core: Wrap Connection::mv_tx with RwLock' from Pekka Enberg
Reviewed-by: Pere Diaz Bou <pere-altea@homail.com>

Closes #3332
2025-09-25 20:47:03 +03:00
Pere Diaz Bou
91cff65e44 Merge 'Autoincrement' from Pavan Nambi
fixes #1976
and #1605
```zsh
turso> DROP TABLE IF EXISTS t;
CREATE TABLE t (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT
);
turso> INSERT INTO t (name) VALUES ('A'); SELECT * FROM sqlite_sequence;
┌──────┬─────┐
│ name │ seq │
├──────┼─────┤
│ t    │   1 │
└──────┴─────┘
turso> DROP TABLE IF EXISTS t;
CREATE TABLE t (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT
);
turso> INSERT INTO t (name) VALUES ('A'); SELECT * FROM sqlite_sequence;
┌──────┬─────┐
│ name │ seq │
├──────┼─────┤
│ t    │   1 │
└──────┴─────┘
turso> INSERT INTO t (name) VALUES ('A'); SELECT * FROM sqlite_sequence;
┌──────┬─────┐
│ name │ seq │
├──────┼─────┤
│ t    │   2 │
└──────┴─────┘
turso>
```

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

Closes #2983
2025-09-25 18:57:24 +02:00
Pekka Enberg
fa1e986dca core: Wrap Connection::mv_tx with RwLock 2025-09-25 12:47:43 +03: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
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
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
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
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
rajajisai
b9ef9d6c63 Ignore null values in group concat 2025-09-24 18:11:02 -04:00
Levy A.
5dfd67b118 feat: add CAST to fuzzer 2025-09-24 18:06:55 -03:00
Pekka Enberg
60e9d1a1c4 core: Wrap Connection::is_nested_stmt in AtomicBool 2025-09-24 19:30:31 +03:00
Pavan-Nambi
8ce506fded cleanup remove unnecessary comments and correct spelling 2025-09-24 19:11:48 +05:30
Pavan-Nambi
49d5141f2d Merge remote-tracking branch 'origin/main' into cdc_fail_autoincrement 2025-09-24 18:06:02 +05:30