Commit Graph

4785 Commits

Author SHA1 Message Date
Jussi Saurio
360b1fcdae Fix bug: op_vopen should replace cursor slot, not add new one 2025-05-27 10:52:36 +03:00
Jussi Saurio
b72b99c973 Merge 'feature: INSERT INTO <table> SELECT' from Pedro Muniz
Closes #1528 .
- Modified `translate_select` so that the caller can define if the
statement is top-level statement or a subquery.
- Refactored `translate_insert` to offload the translation of multi-row
VALUES and SELECT statements to `translate_select`
- I did not try to change much of `populate_column_registers` as I did
not want to break `translate_virtual_table_insert`. Ideally, I would
want to unite this remaining logic folding `populate_column_registers`
into `populate_columns_multiple_rows` and the
`translate_virtual_table_insert` into `translate_insert`. But, I think
this may be best suited for a separate PR.
## TODO
- ~Tests~ - *Done*
- ~Need to emit a temp table when we are selecting and inserting into
the Same Table -
https://github.com/sqlite/sqlite/blob/master/src/insert.c#L1369~ -
*Done*
- Optimization when table have the exact same schema - open an Issue
about it
- Virtual Tables do not benefit yet from this feature - open an Issue
about it

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

Closes #1566
2025-05-27 10:50:26 +03:00
Jussi Saurio
1c4b5c0333 Merge 'Clear test db's WAL too in clone_test_db.sh' from Jussi Saurio
Before #1570 , we never read anything from the on-disk WAL at startup,
so the `test_wal_frame_count()` test assertion `frame_count == 0`would
always pass, regardless of whether there was anything in the WAL or not
Now that the actual bug is fixed, the current test isn't idempotent
because the `clone_test_db.sh` command does not clear the wal, just the
db file

Closes #1581
2025-05-27 10:04:02 +03:00
Jussi Saurio
7e16c235af Clear test db's WAL too in clone_test_db.sh 2025-05-26 22:00:39 +03:00
Jussi Saurio
4395b7cf4a Merge 'Small cleanups to pager/wal/vdbe - mostly naming' from Jussi Saurio
- Instead of using a confusing `CheckpointStatus` for many different
things, introduce the following statuses:
    * `PagerCacheflushStatus` - cacheflush can result in either:
       - the WAL being written to disk and fsynced
       - but also a checkpoint to the main DB file, and fsyncing the
main DB file
      Reflect this in the type.
    * `WalFsyncStatus`
      - previously `CheckpointStatus` was also used for this, even
though fsyncing the WAL doesn't checkpoint.
    * `CheckpointStatus`/`CheckpointResult` is now used only for actual
checkpointing.
- Rename `HaltState` to `CommitState` (program.halt_state ->
program.commit_state)
   * `HaltState`'s only variant was `HaltState::Checkpointing` but it
wasn't necessarily reflective of checkpoint being in progress.
   * Now the variants are `CommitState::Ready` and
`CommitState::Committing` and `commit_state` is
      non-optional in `ProgramState`
- Make `wal` a non-optional property in `Pager`
  * This gets rid of a lot of `if let Some(...)` boilerplate
  * For ephemeral indexes, provide a `DummyWAL` implementation that does
nothing.
- Rename `program.halt()` to `program.commit_txn()`
- Add some documentation comments to structs and functions

Reviewed-by: Preston Thorpe (@PThorpe92)

Closes #1578
2025-05-26 21:38:02 +03:00
Pekka Enberg
bc25ef0fba Merge 'bindings/javascript: API enhancements' from Diego Reis
This PR:
- Fix compatibility for variadic functions;
- Adds pragma, transaction, close, and pluck methods;
- Add variables to get, iterate, and all methods

Closes #1576
2025-05-26 20:46:10 +03:00
Pekka Enberg
11f7526a73 Merge 'github: Migrate workflows to Blacksmith runners' from blacksmith-sh[bot]
To whomever may be reviewing this PR,
**[Blacksmith](https://www.blacksmith.sh/) is the fastest way to run
your GitHub Actions.**
## What does this PR change?
This PR has been automatically generated by a team member in your GitHub
organization using Blacksmith's [Migration
Wizard](https://docs.blacksmith.sh/introduction/quickstart), or MigWiz
for short. This PR changes the following:
1. Updates your selected workflows to use Blacksmith's `runs-on:
blacksmith-4vcpu-ubuntu-2204`. Learn more about the [different instances
available to choose from](https://docs.blacksmith.sh/blacksmith-
runners/config).
2. Updates your `actions/cache`, `actions/setup-*` to use Blacksmith's
`useblacksmith/cache`, `useblacksmith/setup-*`. Learn more about
[Blacksmith's cache action](https://docs.blacksmith.sh/blacksmith-
caching/dependencies-actions).
## FAQ
- Is this free? The first 3,000 minutes per month are free.
- Who uses Blacksmith? Clerk, Ashby, VEED, and 300+ others.
- What's the catch? There is none. Merge this thing already.

Closes #1579
2025-05-26 20:44:40 +03:00
Pekka Enberg
400fe6a4fe github: Switch release workflow to use Github Actions
...the file is auto-generated by `cargo-dist` and won't run if edited manually.
2025-05-26 20:28:48 +03:00
blacksmith-sh[bot]
c08b6a8957 Migrate workflows to Blacksmith 2025-05-26 16:28:46 +00:00
Diego Reis
bce4ac45db bind/js: Remove broken try/catch 2025-05-26 12:49:47 -03:00
Diego Reis
c2efab35e0 bind/js: Refactor tests 2025-05-26 12:48:51 -03:00
Diego Reis
7dc69c9c39 bindings/js: Add extension loading 2025-05-26 12:25:43 -03:00
Jussi Saurio
3ba9f2ab97 Small cleanups to pager/wal/vdbe - mostly naming
- Instead of using a confusing CheckpointStatus for many different things,
  introduce the following statuses:
    * PagerCacheflushStatus - cacheflush can result in either:
      - the WAL being written to disk and fsynced
      - but also a checkpoint to the main BD file, and fsyncing the main DB file

      Reflect this in the type.
    * WalFsyncStatus - previously CheckpointStatus was also used for this, even
      though fsyncing the WAL doesn't checkpoint.
    * CheckpointStatus/CheckpointResult is now used only for actual checkpointing.

- Rename HaltState to CommitState (program.halt_state -> program.commit_state)
- Make WAL a non-optional property in Pager
  * This gets rid of a lot of if let Some(...) boilerplate
  * For ephemeral indexes, provide a DummyWAL implementation that does nothing.
- Rename program.halt() to program.commit_txn()
- Add some documentation comments to structs and functions
2025-05-26 10:37:34 +03:00
pedrocarlo
1410e57112 correct union result_row or yield emission + test 2025-05-26 01:06:26 -03:00
pedrocarlo
ee93316c46 fix num_values detection + emitting correct column for temp_table + tests 2025-05-25 19:15:28 -03:00
pedrocarlo
e3fd1e589e support using a INSERT SELECT that references the same table in both statements 2025-05-25 19:15:28 -03:00
pedrocarlo
90e3c8483d tests with compound select 2025-05-25 19:15:28 -03:00
pedrocarlo
c86e7542ec simple smoke tests 2025-05-25 19:13:40 -03:00
pedrocarlo
72c1f2f582 fix rebase issues and make code compile by cloning query type. Adjust the compound select behavior with insert 2025-05-25 19:13:40 -03:00
pedrocarlo
c8144340a0 adjust proper ordering for value insert 2025-05-25 19:12:30 -03:00
pedrocarlo
810211b3d1 passing incorrect number of values to virtual table insert 2025-05-25 19:12:30 -03:00
pedrocarlo
4bcfc8ca60 create separate function to populate multiple columns in a multi-row VALUES clause or in an INSERT INTO <table> SELECT. Virtual Table insert is broken, need to fix it still 2025-05-25 19:12:30 -03:00
pedrocarlo
bb7da39c72 remove assumption that translate_select is always called from a top-level context + adjust insert to use translate_select when needed 2025-05-25 19:12:30 -03:00
pedrocarlo
fd9e0db5cc pass the owned ast to translate_insert + remove assumption of a list of values in populate_columns_insert 2025-05-25 19:02:17 -03:00
pedrocarlo
15ffdd3e51 modify translate_select to return number of result columns 2025-05-25 19:02:17 -03:00
Diego Reis
dd029b3d37 fix clippy 2025-05-25 16:34:31 -03:00
Diego Reis
60b78b3566 bind/js: Partially implements pragma
Some pragmas may return more than one value, which would
break the current logic. So this cause should be handled in the future
2025-05-25 15:56:28 -03:00
Jussi Saurio
be89809335 Merge 'Add PThorpe92 to codeowners file for extensions + go bindings' from Preston Thorpe
Reviewed-by: Jussi Saurio <jussi.saurio@gmail.com>

Closes #1573
2025-05-25 21:43:00 +03:00
Jussi Saurio
41dfb2fd5e Merge 'UNION' from Jussi Saurio
```sql

limbo> select * from products where id between 1 and 3
UNION ALL
select * from products where id between 2 and 4;
┌───┬─────────┬──────┐
│ 1 │ hat     │ 79.0 │
├───┼─────────┼──────┤
│ 2 │ cap     │ 82.0 │
├───┼─────────┼──────┤
│ 3 │ shirt   │ 18.0 │
├───┼─────────┼──────┤
│ 2 │ cap     │ 82.0 │
├───┼─────────┼──────┤
│ 3 │ shirt   │ 18.0 │
├───┼─────────┼──────┤
│ 4 │ sweater │ 25.0 │
└───┴─────────┴──────┘
limbo> select * from products where id between 1 and 3
UNION
select * from products where id between 2 and 4;
┌───┬─────────┬──────┐
│ 1 │ hat     │ 79.0 │
├───┼─────────┼──────┤
│ 2 │ cap     │ 82.0 │
├───┼─────────┼──────┤
│ 3 │ shirt   │ 18.0 │
├───┼─────────┼──────┤
│ 4 │ sweater │ 25.0 │
└───┴─────────┴──────┘
limbo>
```
Similarly as UNION ALL (#1541 ), supports LIMIT but not OFFSET or ORDER
BY.
Augments `compound_select_fuzz()` to work with both UNION and UNION ALL

Closes #1545
2025-05-25 21:40:35 +03:00
Jussi Saurio
d5623e752b make compound_select_fuzz() test more likely to generate duplicate rows 2025-05-25 21:27:45 +03:00
Jussi Saurio
07fa3a9668 Rename SelectQueryType to QueryDestination 2025-05-25 21:23:04 +03:00
Jussi Saurio
d893a55c55 UNION 2025-05-25 21:23:04 +03:00
Jussi Saurio
75cab791b7 Merge 'Refactor: add stable internal_id property to TableReference' from Jussi Saurio
Closes #1557
Currently our "table id"/"table no"/"table idx" references always use
the direct index of the `TableReference` in the plan, e.g. in
`SelectPlan::table_references`. For example:
```rust
Expr::Column { table: 0, column: 3, .. }
```
refers to the 0'th table in the `table_references` list.
This is a fragile approach because it assumes the table_references list
is stable for the lifetime of the query processing. This has so far been
the case, but there exist certain query transformations, e.g. subquery
unnesting, that may fold new table references from a subquery (which has
its own table ref list) into the table reference list of the parent.
If such a transformation is made, then potentially all of the
Expr::Column references to tables will become invalid. Consider this
example:
```sql
-- Assume tables: users(id, age), orders(user_id, amount)

-- Get total amount spent per user on orders over $100
SELECT u.id, sub.total
FROM users u JOIN
     (SELECT user_id, SUM(amount) as total
      FROM orders o
      WHERE o.amount > 100
      GROUP BY o.user_id) sub
WHERE u.id = sub.user_id

-- Before subquery unnesting:
-- Main query table_references: [users, sub]
-- u.id refers to table 0, column 0
-- sub.total refers to table 1, column 1
--
-- Subquery table_references: [orders]
-- o.user_id refers to table 0, column 0
-- o.amount refers to table 0, column 1
--
-- After unnesting and folding subquery tables into main query,
-- the query might look like this:

SELECT u.id, SUM(o.amount) as total
FROM users u JOIN orders o ON u.id = o.user_id
WHERE o.amount > 100
GROUP BY u.id;

-- Main query table_references: [users, orders]
-- u.id refers to table index 0 (correct)
-- o.amount refers to table index 0 (incorrect, should be 1)
-- o.user_id refers to table index 0 (incorrect, should be 1)
```
We could ofc traverse every expression in the subquery and rewrite the
table indexes to be correct, but if we instead use stable identifiers
for each table reference, then all the column references will continue
to be correct.
Hence, this PR introduces a `TableInternalId` used in `TableReference`
as well as `Expr::Column` and `Expr::Rowid` so that this kind of query
transformations can happen with less pain. I used a separate newtype
struct for `TableInternalId` because it made the refactor a lot easier
due to not having to spend time thinking which `usize` is what.
---
Potential follow-up: `join_order` can be removed from `SelectPlan`
because the `table_references` vec can simply be sorted after join
reordering, because the `Expr::Column` references will continue to be
valid.

Reviewed-by: Preston Thorpe (@PThorpe92)

Closes #1561
2025-05-25 21:21:22 +03:00
Diego Reis
1ff454853b bind/js: Add close metho to Database 2025-05-25 14:47:04 -03:00
Diego Reis
f1018d97a4 bind/js: Add source attribute to Statement 2025-05-25 14:43:58 -03:00
PThorpe92
72d82abb80 Add PThorpe92 to codeowners file for extensions + go bindings 2025-05-25 13:29:05 -04:00
Jussi Saurio
7c07c09300 Add stable internal_id property to TableReference
Currently our "table id"/"table no"/"table idx" references always
use the direct index of the `TableReference` in the plan, e.g. in
`SelectPlan::table_references`. For example:

```rust
Expr::Column { table: 0, column: 3, .. }
```

refers to the 0'th table in the `table_references` list.

This is a fragile approach because it assumes the table_references
list is stable for the lifetime of the query processing. This has so
far been the case, but there exist certain query transformations,
e.g. subquery unnesting, that may fold new table references from
a subquery (which has its own table ref list) into the table reference
list of the parent.

If such a transformation is made, then potentially all of the Expr::Column
references to tables will become invalid. Consider this example:

```sql
-- Assume tables: users(id, age), orders(user_id, amount)

-- Get total amount spent per user on orders over $100
SELECT u.id, sub.total
FROM users u JOIN
     (SELECT user_id, SUM(amount) as total
      FROM orders o
      WHERE o.amount > 100
      GROUP BY o.user_id) sub
WHERE u.id = sub.user_id

-- Before subquery unnesting:
-- Main query table_references: [users, sub]
-- u.id refers to table 0, column 0
-- sub.total refers to table 1, column 1
--
-- Subquery table_references: [orders]
-- o.user_id refers to table 0, column 0
-- o.amount refers to table 0, column 1
--
-- After unnesting and folding subquery tables into main query,
-- the query might look like this:

SELECT u.id, SUM(o.amount) as total
FROM users u JOIN orders o ON u.id = o.user_id
WHERE o.amount > 100
GROUP BY u.id;

-- Main query table_references: [users, orders]
-- u.id refers to table index 0 (correct)
-- o.amount refers to table index 0 (incorrect, should be 1)
-- o.user_id refers to table index 0 (incorrect, should be 1)
```

We could ofc traverse every expression in the subquery and rewrite
the table indexes to be correct, but if we instead use stable identifiers
for each table reference, then all the column references will continue
to be correct.

Hence, this PR introduces a `TableInternalId` used in `TableReference`
as well as `Expr::Column` and `Expr::Rowid` so that this kind of query
transformations can happen with less pain.
2025-05-25 20:26:17 +03:00
Jussi Saurio
b7d2173e99 Merge 'Fix off-by-one error in max_frame after WAL load' from Jussi Saurio
🤦

Reviewed-by: Pere Diaz Bou <pere-altea@homail.com>
Reviewed-by: Preston Thorpe (@PThorpe92)

Closes #1572
2025-05-25 20:25:50 +03:00
Jussi Saurio
b5ac095716 Fix off-by-one error in max_frame after WAL load 2025-05-25 19:34:51 +03:00
Diego Reis
376adbb10a bind/js: Add support to variadic functions 2025-05-25 12:22:46 -03:00
Diego Reis
e463def54d wip: first wrapper version 2025-05-25 12:22:46 -03:00
Jussi Saurio
f388bc571e Merge 'xConnect for virtual tables to query core db connection' from Preston Thorpe
Re-Opening #1076 because it had bit-rotted to a point of no return.
However it has improved. Now with Weak references and no incrementing Rc
strong counts.
This also includes a better test extension that returns info about the
other tables in the schema.
![image](https://github.com/user-
attachments/assets/4292dc9c-121e-4ba2-8a51-4533bbcf2afd)
(theme doesn't show rows column)

Closes #1366
2025-05-25 14:37:38 +03:00
Jussi Saurio
621ae60ab5 Merge 'Reconstruct WAL frame cache when WAL is opened' from Jussi Saurio
Fixes #1567
Probably also fixes #1485
Currently we are simply unable to read any WAL frames from disk once a
fresh process w/ Limbo is opened, since we never try to read anything
from disk unless we already have it in our in-memory frame cache.
This commit implements a crude way of reading entire WAL into memory as
a single buffer and reconstructing the frame cache.

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

Closes #1570
2025-05-25 14:35:47 +03:00
Jussi Saurio
385c0d8987 clippy stfu part 2: electric boogaloo 2025-05-25 10:32:23 +03:00
Jussi Saurio
2df01f0b6f clippy stfu 2025-05-25 10:29:30 +03:00
Jussi Saurio
6254246541 use tempfile in test 2025-05-25 10:25:52 +03:00
Jussi Saurio
64ef3f1343 simplify condition 2025-05-25 10:22:46 +03:00
Jussi Saurio
20e65c0125 bump max_loops to 100k 2025-05-25 10:21:41 +03:00
Pere Diaz Bou
a91f8aee78 Merge 'set non-shared cache by default' from Pere Diaz Bou
Shared cache requires more locking mechasnisms. We still have multi
threading issues not related to shared cache so it is wise to first fix
those and then once they are fixed, we can incrementally add shared
cache back with locking in place.

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

Closes #1568
2025-05-25 08:50:21 +02:00
PThorpe92
954df3f837 Fix csv test assertion 2025-05-24 17:33:01 -04:00