343 Commits

Author SHA1 Message Date
pedrocarlo
1db13889e3 Change Value::Text to use a Cow<'static, str> instead of Vec<u8> 2025-11-11 16:11:46 -03:00
RS2007
60cbc6d8ea migrating from_uri to database opts 2025-11-02 16:28:22 +05:30
Pekka Enberg
913b7ac600 core: Disable autovacuum by default
People have discovered various bugs in autovacuum so let's disable it by
default for now.
2025-11-02 12:09:21 +02:00
Pekka Enberg
0217588d13 Make mimalloc dependency optional 2025-11-01 11:43:32 +02:00
Nikita Sivukhin
35b96ae8d8 fix few places which needs to be hooked into new types 2025-10-28 11:27:35 +04:00
Jussi Saurio
9c87b20cb2 Merge 'Where clause subquery support' from Jussi Saurio
Closes #1282
# Support for WHERE clause subqueries
This PR implements support for subqueries that appear in the WHERE
clause of SELECT statements.
## What are those lol
1. **EXISTS subqueries**: `WHERE EXISTS (SELECT ...)`
2. **Row value subqueries**: `WHERE x = (SELECT ...)` or `WHERE (x, y) =
(SELECT ...)`. The latter are not yet supported - only the single-column
("scalar subquery") case is.
3. **IN subqueries**: `WHERE x IN (SELECT ...)` or `WHERE (x, y) IN
(SELECT ...)`
## Correlated vs Uncorrelated Subqueries
- **Uncorrelated subqueries** reference only their own tables and can be
evaluated once.
- **Correlated subqueries** reference columns from the outer query
(e.g., `WHERE EXISTS (SELECT * FROM t2 WHERE t2.id = t1.id)`) and must
be re-evaluated for each row of the outer query
## Implementation
### Planning
During query planning, the WHERE clause is walked to find subquery
expressions (`Expr::Exists`, `Expr::Subquery`, `Expr::InSelect`). Each
subquery is:
1. Assigned a unique internal ID
2. Compiled into its own `SelectPlan` with outer query tables provided
as available references
3. Replaced in the AST with an `Expr::SubqueryResult` node that
references the subquery with its internal ID
4. Stored in a `Vec<NonFromClauseSubquery>` on the `SelectPlan`
For IN subqueries, an ephemeral index is created to store the subquery
results; for other kinds, the results are stored in register(s).
### Translation
Before emitting bytecode, we need to determine when each subquery should
be evaluated:
- **Uncorrelated**: Evaluated once before opening any table cursors
- **Correlated**: Evaluated at the appropriate nested loop depth after
all referenced outer tables are in scope
This is calculated by examining which outer query tables the subquery
references and finding the right-most (innermost) loop that opens those
tables - using similar mechanisms that we use for figuring out when to
evaluate other `WhereTerm`s too.
### Code Generation
- **EXISTS**: Sets a register to 1 if any row is produced, 0 otherwise.
Has new `QueryDestination::ExistsSubqueryResult` variant.
- **IN**: Results stored in an ephemeral index and the index is probed.
- **RowValue**: Results stored in a range of registers. Has new
`QueryDestination::RowValueSubqueryResult` variant.
## Annoying details
### Which cursor to read from in a subquery?
Sometimes a query will use a covering index, i.e. skip opening the table
cursor at all if the index contains All The Needed Stuff.
Correlated subqueries reading columns from outer tables is a bit
problematic in this regard: with our current translation code, the
subquery doesn't know whether the outer query opened a table cursor,
index cursor, or both. So, for now, we try to find a table cursor first,
then fall back to finding any index cursor for that table.

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

Closes #3847
2025-10-28 06:36:55 +02:00
Jussi Saurio
c0c425b5d6 EXPLAIN: indent BeginSubrtn...Return blocks properly
WHERE clause subqueries use the BeginSubrtn instruction.

The corresponding closing instruction for BeginSubrtn is Return,
but Return is also used for other purposes, so we need to track pairs of
BeginSubrtn and Return that share the same 1st parameter (the subroutine register),
so that the EXPLAIN output for those subroutine contents is indented properly.
2025-10-27 13:42:00 +02:00
Nikita Sivukhin
8a80e8b743 rename custom modules to index_method like in postgresql 2025-10-27 13:18:18 +04:00
Nikita Sivukhin
299533b7b6 hide custom modules syntax behind --experimental-custom-modules flag 2025-10-27 12:29:05 +04:00
Nikita Sivukhin
7d423d358f avoid unnecessary time measures 2025-10-22 11:08:46 +04:00
Pavan-Nambi
aad3c00e57 clippy 2025-10-19 13:25:40 +05:30
Pavan-Nambi
055b38787d more keybindings 2025-10-19 13:22:35 +05:30
Pavan-Nambi
3658a94f06 fmt 2025-10-19 13:20:40 +05:30
Pavan-Nambi
ba2570d0b8 cli:scrolling and enable suggestion for wrong commands 2025-10-19 13:20:03 +05:30
Preston Thorpe
79e4176163 Merge 'cli: .tables and .indexes to show data from attached tables aswell' from Konstantinos Artopoulos
Closes #3545

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

Closes #3627
2025-10-16 11:42:29 -04:00
Pekka Enberg
9a1bd2112d Merge 'Run simulator under Miri' from Bob Peterson
This adds support for running the simulator under Miri to detect UB.
There are a few things to note about Miri and its limitations
- It has limited `libc` coverage, so it's not really possible to have
Miri help with `UringIO`/`UringFile` or `UnixIO`/`UnixFile`. That's a
big gap ☹️
- It **can** work for `GenericIO`/`GenericFile`, which only uses `std`
- It can't call external C libraries, so even using `sqlite` is out
(hence adding `--disable-integrity-check` to the simulator for Miri use)
- It runs on nightly, consequently there are a few new lints that don't
exist on turso's pinned version of rustc
Some questions I have about this MR
- I made `GenericFile::{lock_file,unlock_file}` noops so I could use
`GenericIO`. This isn't great, but if/when you update from Rust 1.88.0
to 1.89.0, `std::File::{lock,lock_shared,unlock}` will be stabilized and
available. Should I note that as a TODO or something?
- Previously, the sim runner shelled out to `git` to get stuff like the
current git hash and the repo directory. For Miri, that's out, and so is
`git2`. Unfortunately, `gix` is also out since it has a required
dependency that uses inline assembly, which Miri doesn't like. I wrote a
hacky shim that uses only std to look for `.git` and find the hash that
HEAD is pointing to. It doesn't deal with stuff like packed-refs or the
repo being a secondary one made with `git worktree`. I'm happy to
support that, but wanted to hear from maintainers before doing more
work.
Two UB occurrences I already found:
- `TursoRwLock::read` used `AtomicU64::compare_exchange_weak`, which is
(evidently) [allowed to spuriously fail](https://doc.rust-lang.org/std/s
ync/atomic/struct.AtomicU64.html#method.compare_exchange_weak) in
exchange for perf. Miri forces this behavior, which triggers trivial
read deadlocks even with zero readers/writers. I changed it to
`compare_exchange`, but I'm not an atomics expert.
- Uninitialized read in non-Unix
`core::storage::buffer_pool::arena::alloc`. This is a simple one,
resolved by using `std::alloc::alloc_zeroed` instead of
`std::alloc::alloc`
Moving forward, I'd be interested in potentially getting the tests to
run in Miri, too. `tokio` looks like a good example of a project with
partial coverage that runs it where they can. They have some extra test
config to allow as many as possible to run under Miri, with
appropriately scaled-down parameter values since Miri is super slow

Closes #3720
2025-10-14 09:26:55 +03:00
pedrocarlo
5b2cce946a do not reference workspace package by path 2025-10-13 21:07:15 -03:00
Bob Peterson
cd56f52bd6 Add cfg attributes for running under Miri 2025-10-13 14:54:16 -05:00
rajajisai
6980128a24 Ignore sqlite_sequence table when dumping tables in .clone 2025-10-09 22:50:18 -04:00
Konstantinos Artopoulos
435a472bca fix(cli): clippy error 2025-10-08 11:09:20 +03:00
Konstantinos Artopoulos
7b17c824fb feat(cli): .tables and .indexes dot commands should show tables and indexes from attached databases 2025-10-08 00:17:34 +03:00
Pekka Enberg
d93846c010 Merge 'Update man pages for encryption' from Avinash Sajjanshetty
Closes #3552
2025-10-03 09:52:06 +03:00
Avinash Sajjanshetty
44c387ff71 Update man pages for encryption 2025-10-03 12:18:55 +05:30
Pekka Enberg
f21230ed76 cli: Remove unused "--experimental-logical-log" option 2025-10-03 07:39:07 +03:00
Pekka Enberg
16540724aa Beta 2025-10-01 07:18:25 +03:00
Avinash Sajjanshetty
c8111f9555 Put encryption behind an opt in (runtime) flag 2025-09-30 18:29:18 +05:30
Glauber Costa
89956039be add manual page about materialized views 2025-09-29 15:49:37 -05:00
PThorpe92
714691748e Display nothing for .schema command when table not found 2025-09-25 19:34:11 -04: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
Pekka Enberg
cc70ec892b Merge 'Fix .schema command for empty databases' from Diego Reis
Fixes #3274

Closes #3342
2025-09-25 17:25:46 +03:00
Diego Reis
7a56c93b81 Makes clippy happy 2025-09-25 10:42:14 -03:00
Diego Reis
c945dee62b fix(3274): Command .schema should work for sqlite_schema 2025-09-25 10:38:45 -03:00
Pekka Enberg
fb39661809 core: Wrap Connection::metrics with RwLock 2025-09-25 14:07:39 +03:00
Glauber Costa
fbc3d0dbc3 Add built-in manual pages for Turso
In the hopes of doing a good job at teaching people what Turso can do,
I am adding built-in manual pages. When the CLI starts, it picks a
feature at random, and tells the user that the feature exists:

```
Turso v0.2.0-pre.8
Enter ".help" for usage hints.
Did you know that Turso supports Change Data Capture? Type .manual cdc to learn more.
This software is ALPHA, only use for development, testing, and experimentation.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database
```

There is a lot we can do to make this feature world class:
- we can automatically compile examples during compile time like
  rust-doc, to make sure examples used in the manuals always work
- we can implement scrolling and navigation
- we can document a lot more features

But for now, this is a start!
2025-09-24 11:29:24 -03:00
PThorpe92
0c54c2b255 Add turos_cli option to CLI DatabaseOpts 2025-09-22 11:28:19 -04:00
PThorpe92
cfa89c9ddb use cli_only feature in CLI Cargo.toml 2025-09-22 11:28:19 -04:00
Pere Diaz Bou
ff3c79d5d7 remove mvvmode and set logical log as default 2025-09-18 18:22:25 +02:00
Pere Diaz Bou
0eb2aa123d cli/app: add mvcc mode 2025-09-18 18:21:04 +02:00
Pere Diaz Bou
de8a975a0b core/mvcc: introduce MvccMode Logical Log 2025-09-18 18:21:04 +02:00
rajajisai
e605aff31b Merge branch 'main' into enc-page-1 2025-09-16 10:06:00 -04:00
rajajisai
89caa868f9 Encryption support for database header page 2025-09-16 10:04:30 -04:00
Pekka Enberg
ae25a0f088 Merge 'Implement Min/Max aggregators' from Glauber Costa
We have not implemented them before because they require the raw
elements to be kept. It is easy to see why in the following example:
```
current_min = 3;
insert(2) => current_min = 2 // can be done without state
delete(2) => needs to look at the state to determine new min!
```
The aggregator state was a very simple key-value structure. To
accomodate for min/max, we will make it into a more complex table, where
we can encode a more complex structure.
The key insight is that we can use a primary key composed of:
```
1) storage_id
2) zset_id,
3) element
```
The storage_id and zset_id are our previous key, except they are now
exploded to support a larger range of storage_id. With more bits
available in the storage_id, we can encode information about which
column we are storing. For aggregations in multiple columns, we will
need to keep a different list of values for min/max!
The element is just the values of the columns.
Because this is a primary key, the data will be sorted in the btree. We
can then just do a prefix search in the first two components of the key
and easily find the min/max when needed.
This new format is also adequate for joins. Joins will just have a new
storage_id which encodes two "columns" (left side, right side).

Closes #3143
2025-09-16 16:19:59 +03:00
Glauber Costa
3e9a5d93b5 hide internal tables from .schema 2025-09-15 22:30:48 -05:00
pedrocarlo
3c91ae206b move as many dependencies as possible to workspace to avoid multiple versions of the same dependency 2025-09-15 17:19:36 -03:00
TcMits
9dac467b40 support EXPLAIN QUERY PLAN 2025-09-12 19:58:45 +07:00
TcMits
830e10da8f resolve merge conflict 2025-09-11 18:13:29 +07:00
TcMits
b574b4bcea finish EXPLAIN 2025-09-11 18:04:59 +07:00
Jussi Saurio
7ac18a6952 Merge 'Remove some traces in super hot paths in btree' from Preston Thorpe
Particularly we were tracing `ImmutableRecord` / `BTreeKey` which would
then trace the bytes of records. These are super super hot paths and I
think we can probably remove even more to under debug assertions so we
dont eat those atomics/branches all the time.
This PR also introduces the `tracing_release` feature, which turns all
`trace!` and `debug!` macro invocations to noops at compile time, and
makes that feature available for all bindings.
it also removes the unused `lru` dependency, and cleans up the makefile
a bit

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

Closes #2995
2025-09-11 13:33:25 +03:00
TcMits
5caf9a2640 make it more safe + clippy 2025-09-11 00:14:38 +07:00
PThorpe92
ba1ed72ed8 Add tracing_release feature for benchmarks to compile tracing macros to noops 2025-09-10 09:56:12 -04:00