Commit Graph

4865 Commits

Author SHA1 Message Date
Anton Harniakou
45fe13f226 Implement .name property 2025-06-04 13:00:13 +03:00
Anton Harniakou
ac33dbb63a Add tests 2025-06-04 11:48:16 +03:00
Pekka Enberg
48e0441fae Update README.md 2025-05-30 09:45:41 +03:00
Jussi Saurio
819a6138d0 Merge 'Fix: aggregate regs must be initialized as NULL at the start' from Jussi Saurio
Again found when fuzzing nested where clause subqueries:
Aggregate registers need to be NULLed at the start because the same
registers might be reused on another invocation of a subquery, and if
they are not NULLed, the 2nd invocation of the same subquery will have
values left over from the first invocation.

Reviewed-by: Preston Thorpe (@PThorpe92)

Closes #1614
2025-05-30 09:39:37 +03:00
Jussi Saurio
5632a6046e Merge 'Fix: allow DeferredSeek on more than one cursor per program' from Jussi Saurio
Found while fuzzing nested subqueries. Since subqueries result in nested
plans, it quickly revealed that there can be multiple `DeferredSeek`
instructions issued for different cursors, but our `ProgramState` only
supported one at a time.

Closes #1610
2025-05-30 09:39:23 +03:00
Jussi Saurio
0d91971639 Merge 'Use tempfile in constraint test' from Jussi Saurio
Closes #1618
2025-05-30 09:38:29 +03:00
Jussi Saurio
d52a9a635c fix: use tempfile as db path in constraint.py 2025-05-29 21:03:10 +03:00
Jussi Saurio
d18ab34193 Fix: dont pollute testing.db in insert tests 2025-05-29 20:51:29 +03:00
Jussi Saurio
482eb4aa9a Merge 'Refactor: make clear distinction between 'joined tables' and 'tables referenced from outer query scopes'' from Jussi Saurio
**Beef:** we need to distinguish between references to tables in the
current query scope (CTEs, FROM clause) and references to tables from
outer query scopes (inside subqueries, or inside CTEs that refer to
previous CTEs). We don't want to consider 'tables from outside' in the
join order of a subquery, but we want the subquery to be able to
_reference_ those tables in e.g. its WHERE clause.
This PR -- or at least some sort of equivalent of it -- is a requirement
for #1595.
---
This PR replaces the `Vec<TableReference>` we use with new data
structures:
- TableReferences struct, which holds both:
     - joined_tables, and
     - outer_query_refs
- JoinedTable:
     - this is just a rename of the previous TableReference struct
- OuterQueryReference
     - this is to distinguish from JoinedTable those cases where
       e.g. a subquery refers to an outer query's table, or a CTE
       refers to a previous CTE.
Both JoinedTable and OuterQueryReference can be referred to by
expressions,
but only JoinedTables are considered for join ordering optimization and
so
forth.
These data structures are then used everywhere, which resulted in a lot
of changes.

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

Closes #1580
2025-05-29 20:45:33 +03:00
Jussi Saurio
f8257df77b Fix: aggregate regs must be initialized as NULL at the start 2025-05-29 18:44:53 +03:00
Pekka Enberg
5af837d50a Merge 'antithesis-tests: Don't fail tests on unique constraint violation' from Pekka Enberg
Unique constraint violation is not an error.

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

Closes #1612
2025-05-29 17:27:47 +03:00
Pere Diaz Bou
d8e636ec45 Merge 'Remove frame id from key' from Pere Diaz Bou
After reading sqlite a bit, it isn't needed because we have RWlock for
each table in the database file.

Closes #1569
2025-05-29 16:18:45 +02:00
Pekka Enberg
1962990506 antithesis-tests: Don't fail tests on unique constraint violation
Unique constraint violation is not an error.
2025-05-29 16:10:14 +03:00
Jussi Saurio
69133b3b2e Fix: allow DeferredSeek on more than one cursor per program 2025-05-29 16:05:47 +03:00
Pere Diaz Bou
968eeea75d Merge 'add stress test with 1 thread 10k iterations to ci' from Pere Diaz Bou
Add simple stress test run with 10k iteration to test `Delete`, `Update`
and `Insert` together.

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

Closes #1585
2025-05-29 14:16:56 +02:00
Pere Diaz Bou
dd15b7df7f remove dumb comment from pagecachekey 2025-05-29 14:12:16 +02:00
Pere Diaz Bou
93161e9fce remove lru size > 0 check on page cache fuzz 2025-05-29 14:12:16 +02:00
Pere Diaz Bou
37e834b092 remove unnecessary test 2025-05-29 14:12:16 +02:00
Pere Diaz Bou
44007075d9 remove frame_id from PageCacheKey
After reading sqlite a bit, it isn't needed because we have RWlock for
each table in the database file.
2025-05-29 14:12:16 +02:00
Pekka Enberg
1653bfb2b3 Merge 'Fix stress test to ignore unique constraint violation' from krishna sindhur
This should ignore unique constraint violation errors during the stress
test.

Closes #1606
2025-05-29 14:59:34 +03:00
krishna sindhur
4b66bcb2a7 fix: stress test will ignore unique constraint violation 2025-05-29 15:11:43 +05:30
Jussi Saurio
211b511189 Fix join optimizer tests 2025-05-29 11:44:56 +03:00
Jussi Saurio
cc405dea7e Use new TableReferences struct everywhere 2025-05-29 11:44:56 +03:00
Jussi Saurio
3d42f85c98 tests/python/writes: use tempfile instead of permanent file 2025-05-29 11:23:50 +03:00
Jussi Saurio
124b38a262 plan.rs: add new datastructures
- TableReferences struct, which holds both:
     - joined_tables, and
     - outer_query_refs

- JoinedTable:
     - this is just a rename of the previous TableReference struct

- OuterQueryReference
     - this is to distinguish from JoinedTable those cases where
       e.g. a subquery refers to an outer query's table, or a CTE
       refers to a previous CTE.

Both JoinedTable and OuterQueryReference can be referred to by expressions,
but only JoinedTables are considered for join ordering optimization and so
forth.

This commit does not compile.
2025-05-29 11:03:09 +03:00
Jussi Saurio
e59c1ac985 tests/compat: use tempfiles for all empty dbs the tests open 2025-05-29 11:01:37 +03:00
Jussi Saurio
7ededb8b97 Merge 'Fix ProgramBuilder::cursor_ref not having unique keys' from Jussi Saurio
Currently we have this:
`program.alloc_cursor_id(Option<String>, CursorType)`
where the `String` is the table's name or alias ('users' or 'u' in the
query).
This is problematic because this can happen:
`SELECT * FROM t WHERE EXISTS (SELECT * FROM t)`
There are two cursors, both with identifier 't'. This causes a bug where
the program will use the same cursor for both the main query and the
subquery, since they are keyed by 't'.
Instead introduce `CursorKey`, which is a combination of:
1. `TableInternalId`, and
2. index name (`Option<Arc<Index>>` -- in case of index cursors.)
This should provide key uniqueness for cursors:
`SELECT * FROM t WHERE EXISTS (SELECT * FROM t)`
here the first 't' will have a different `TableInternalId` than the
second `t`, so there is no clash.
---
These `CursorKey`s are only required when the program needs to retrieve
the cursor ID later:
`program.resolve_cursor_id(key)`
So, there are now two methods for allocating cursors:
`program.alloc_cursor_id_keyed(key, cursor_type); // needs to be
retrieved later with same key`
`program.alloc_cursor_id(cursor_type); // does not need to be retrieved
later`

Reviewed-by: Preston Thorpe (@PThorpe92)

Closes #1604
2025-05-29 10:53:24 +03:00
Jussi Saurio
592ba41137 Add assertion forbidding duplicate cursor keys 2025-05-29 01:04:45 +03:00
Jussi Saurio
77ce4780d9 Fix ProgramBuilder::cursor_ref not having unique keys
Currently we have this:

program.alloc_cursor_id(Option<String>, CursorType)`

where the String is the table's name or alias ('users' or 'u' in
the query).

This is problematic because this can happen:

`SELECT * FROM t WHERE EXISTS (SELECT * FROM t)`

There are two cursors, both with identifier 't'. This causes a bug
where the program will use the same cursor for both the main query
and the subquery, since they are keyed by 't'.

Instead introduce `CursorKey`, which is a combination of:

1. `TableInternalId`, and
2. index name (Option<String> -- in case of index cursors.

This should provide key uniqueness for cursors:

`SELECT * FROM t WHERE EXISTS (SELECT * FROM t)`

here the first 't' will have a different `TableInternalId` than the
second `t`, so there is no clash.
2025-05-29 00:59:24 +03:00
Jussi Saurio
85316d8419 Merge 'clear page cache on transaction failure' from Pere Diaz Bou
This is the first step towards rollback, since we still don't spill
pages with WAL, we can simply invalidate page cache in case of failure.

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

Closes #1599
2025-05-28 23:14:44 +03:00
Jussi Saurio
1d7824badd Merge 'Fix serialize() unreachable panic' from Krishna Vishal
Closes: https://github.com/tursodatabase/limbo/issues/1602
Fix unreachable panic when calling serialize on Value::Integer(0)
 by handling `SerialTypeKind` `ConstInt0` and `ConstInt1` in
`Record::serialize()`
Changed `test_serialize_integers` to reflect this change.

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

Closes #1603
2025-05-28 23:12:32 +03:00
krishvishal
fb1d53b0ec Fix test. off by one. 2025-05-29 00:22:38 +05:30
krishvishal
5b57efd894 A couple more tests to test this case. 2025-05-29 00:10:59 +05:30
krishvishal
e3bc78f7e4 Fix unreachable panic when calling serialize on Value::Integer(0)
by handling `SerialTypeKind` `ConstInt0` and `ConstInt1` in
`Record::serialize()`

- Changed `test_serialize_integers` to accomodate this change.
2025-05-29 00:08:37 +05:30
Jussi Saurio
35948c6dbb Merge 'Btree: fix cursor record state not being updated in insert_into_page()' from Jussi Saurio
overwrite_cell() requires that the cursor state is pointing to a valid
record, but this was not currently set properly.

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

Closes #1598
2025-05-28 17:10:44 +03:00
Pere Diaz Bou
28bd24b7d4 clear page cache on transaction failure
This is the first step towards rollback, since we still don't spill
pages with WAL, we can simply invalidate page cache in case of failure.
2025-05-28 15:54:28 +02:00
Jussi Saurio
dad1e6293b Btree: fix cursor record state not being updated in insert_into_page()
overwrite_cell() requires that the cursor state is pointing to a valid
record, but this was not currently set properly.
2025-05-28 16:54:00 +03:00
Pekka Enberg
0ae46e815c Limbo 0.0.21 2025-05-28 12:42:03 +03:00
Pekka Enberg
6112bd855c Update CHANGELOG.md 2025-05-28 12:41:54 +03:00
Pekka Enberg
6b93c9acdc Limbo 0.0.21-pre.2 2025-05-28 12:23:13 +03:00
Pekka Enberg
8870dc38aa github: Switch JavaScript publish to Github actions
With Blacksmith runners, it errors out as follows:

```
npm notice Publishing to https://registry.npmjs.org/ with tag next and public access
npm notice publish Signed provenance statement with source and build information from GitHub Actions
npm notice publish Provenance statement published to transparency log: https://search.sigstore.dev/?logIndex=221582002
npm error code E422
npm error 422 Unprocessable Entity - PUT https://registry.npmjs.org/@tursodatabase%2flimbo - Error verifying sigstore provenance bundle: Unsupported GitHub Actions runner environment: "self-hosted". Only "github-hosted" runners are supported when publishing with provenance.
npm error A complete log of this run can be found in: /home/runner/.npm/_logs/2025-05-28T08_42_29_830Z-debug-0.log
Error: Process completed with exit code 1.
```
2025-05-28 12:22:17 +03:00
Pekka Enberg
80d7571f3e Limbo 0.0.21-pre.1 2025-05-28 11:17:08 +03:00
Jussi Saurio
7ab243dc4e Merge 'Make WhereTerm::consumed a Cell<bool>' from Jussi Saurio
Currently in the main translation logic after planning and optimization,
we don't _really_ need to pass a `&mut Vec<WhereTerm>` around anymore,
except for the fact that virtual table constraint resolution is done ad-
hoc in `init_loop()`.
Even there, the only thing we mutate is `WhereTerm::consumed` which is a
boolean indicating that the term has been "used up" by the optimizer and
shouldn't be evaluated as a normal where clause condition anymore.
In the upcoming branch for WHERE clause subqueries, I want to store
immutable references to WHERE clause expressions in `Resolver`, but this
is unfortunately not possible if we still use the aforementioned mutable
references.
Hence, we can temporarily make `WhereTerm::consumed` a `Cell<bool>`
which allows us to pass an immutable reference to `init_loop()`, and the
`Cell` can be removed once the virtual table constraint resolution is
moved to an earlier part of the query processing pipeline.

Closes #1597
2025-05-28 11:14:40 +03:00
Jussi Saurio
2a7d675e71 Merge 'Use lifetimes in walk_expr() to guarantee that child expr has same lifetime as parent expr' from Jussi Saurio
Closes #1596
2025-05-28 11:14:10 +03:00
Jussi Saurio
73e806ad84 Make WhereTerm::consumed a Cell<bool>
Currently in the main translation logic after planning and optimization,
we don't _really_ need to pass a &mut Vec<WhereTerm> around anymore, except
for the fact that virtual table constraint resolution is done ad-hoc in
`init_loop()`. Even there, the only thing we mutate is `WhereTerm::consumed`
which is a boolean indicating that the term has been "used up" by the optimizer
and shouldn't be evaluated as a normal where clause condition anymore.

In the upcoming branch for WHERE clause subqueries, I want to store immutable
references to WHERE clause expressions in `Resolver`, but this is unfortunately
not possible if we still use the aforementioned mutable references.

Hence, we can temporarily make `WhereTerm::consumed` a `Cell<bool>` which allows
us to pass an immutable reference to `init_loop()`, and the `Cell` can be removed
once the virtual table constraint resolution is moved to an earlier part of the
query processing pipeline.
2025-05-28 11:02:39 +03:00
Jussi Saurio
51605ad2a4 Use lifetimes in walk_expr() to guarantee that child expr has same lifetime as parent expr 2025-05-28 10:56:30 +03:00
Jussi Saurio
7241e0503e Merge 'Fix LIMIT handling' from Jussi Saurio
Currently we have some usages of LIMIT where the actual limit counter is
initialized next to the DecrJumpZero instruction, and then
`program.mark_last_insn_constant()` is used to hoist the counter
initialization to the beginning of the program.
This is very fragile, and already FROM clause subquery handling works
around this with a hack (removed in this PR), and (upcoming) WHERE
clause subqueries would also run into problems because of this, because
the LIMIT might need to be initialized once for every iteration of the
subquery.
This PR removes those usages for LIMIT, and LIMIT processing is now more
intuitive:
- limit counter is now initialized at the start of the query processing
- a function init_limit() is extracted to do this for
select/update/delete

Closes #1591
2025-05-28 09:53:51 +03:00
Jussi Saurio
a9ae1af75c Fix: init_limit() in wrong place for Delete 2025-05-27 21:26:31 +03:00
Jussi Saurio
3c587b91b5 Add comment on init_limit() 2025-05-27 21:19:28 +03:00
Jussi Saurio
4e9d9a2470 Fix LIMIT handling
Currently we have some usages of LIMIT where the actual limit counter
is initialized next to the DecrJumpZero instruction, and then
`program.mark_last_insn_constant()` is used to hoist the counter
initialization to the beginning of the program.

This is very fragile, and already FROM clause subquery handling works
around this with a hack (removed in this PR), and (upcoming) WHERE clause
subqueries would also run into problems because of this, because the LIMIT
might need to be initialized once for every iteration of the subquery.

This PR removes those usages for LIMIT, and LIMIT processing is now more
intuitive:

- limit counter is now initialized at the start of the query processing
- a function init_limit() is extracted to do this for select/update/delete
2025-05-27 21:12:22 +03:00