Commit graph

4321 commits

Author SHA1 Message Date
Piotr Rzysko
fdffbc9534 Ensure virtual table name uniqueness 2025-05-11 21:21:18 +02:00
Pekka Enberg
14ef25ebb8 Merge 'Add drop index' from Anton Harniakou
This commit adds suport for DROP INDEX.
Bytecode produced by this commit differs from SQLITE's bytecode, main
reason we don't do autovacuum or repacking of pages like SQLITE does.
Closes #1280

Closes #1444
2025-05-10 08:04:39 +03:00
Pekka Enberg
f2372e9aac Merge 'bindings/java: Remove disabled annotation for UPDATE and DELETE ' from Kim Seon Woo
## Changes
Since limbo now supports UPDATE and DELETE, I'm planning to implement
the rest of the features for integrating JDBC and limbo.
Since limbo's [total_changes() function seems to work differently in
compared to the sqlite's](https://discord.com/channels/12586588262579610
20/1321869557459058778/1368830400985563176), let's remove the
`@Disabled` annotation from
`execute_update_should_return_number_of_updated_elements` test  after
the issue is handled.
## Reference
https://github.com/tursodatabase/limbo/issues/615

Closes #1451
2025-05-10 08:02:13 +03:00
Pekka Enberg
73c0bd0737 Merge 'Refactor numeric literal' from meteorgan
Closes #1461
2025-05-10 08:00:58 +03:00
Pekka Enberg
be1621e099 Merge 'EXPLAIN should show a comment for the Insert opcode' from Anton Harniakou
After this commit EXPLAIN should show a comment for `Insert`.
```
limbo> explain insert into t (age, name, id) values (20, 'max', 1);
addr  opcode             p1    p2    p3    p4             p5  comment
----  -----------------  ----  ----  ----  -------------  --  -------
0     Init               0     9     0                    0   Start at 9
1     OpenWrite          0     2     0                    0
2     Integer            1     2     0                    0   r[2]=1
3     String8            0     3     0     max            0   r[3]='max'
4     Integer            20    4     0                    0   r[4]=20
5     NewRowId           0     1     0                    0
6     MakeRecord         2     3     5                    0   r[5]=mkrec(r[2..4])
7     Insert             0     5     1     t              0   intkey=r[1] data=r[5]
8     Halt               0     0     0                    0
9     Transaction        0     1     0                    0   write=true
10    Goto               0     1     0                    0

```

Closes #1452
2025-05-10 07:59:36 +03:00
Pekka Enberg
97396a553d Merge 'bindings/wasm: add types property for typescript setting' from 오병진
This pull request includes a small change to the
`bindings/wasm/package.json` file. The change adds a `types` field
pointing to the TypeScript declaration file for the package (`[bindings/
wasm/package.jsonR15](diffhunk://#diff-
1f41234b939ba148924b9bbfedd557853ebcd22351a9c300e568ce7af0291babR15)`).

Closes #1460
2025-05-10 07:59:03 +03:00
Pekka Enberg
a105c20f69 Merge 'Implement transaction support in Go adapter' from Jonathan Ness
This PR implements basic transaction support in the Limbo Go adapter by
adding the required methods to fulfill the `driver.Tx` interface.
## Changes
- Add `Begin()` method to `limboConn` to start a transaction
- Add `BeginTx()` method with context support and proper handling of
transaction options
- Implement `Commit()` method to commit transaction changes
- Implement `Rollback()` method with appropriate error handling
- Add transaction tests
## Implementation Details
- Uses the standard SQLite transaction commands (BEGIN, COMMIT,
ROLLBACK)
- Follows the same pattern as other SQL operations in the adapter
(prepare-execute-close)
- Maintains consistent locking and error handling patterns
## Limitations
- Currently, ROLLBACK operations will return an error as they're not yet
fully supported in the underlying Limbo implementation
- Only the default isolation level is supported; all other isolation
levels return `driver.ErrSkip`
- Read-only transactions are not supported and return `driver.ErrSkip`
## Testing
- Added basic transaction tests that verify BEGIN and COMMIT operations
- Adjusted tests to work with the current Limbo implementation
capabilities
These transaction methods enable the Go adapter to be used in
applications that require transaction support while providing clear
error messages when unsupported features are requested.  I'll add to it
when Limbo supports ROLLBACK and/or additional isolation levels.

Closes #1435
2025-05-10 07:58:29 +03:00
Pekka Enberg
97ad25c506 Merge 'Initial implementation of ALTER TABLE RENAME' from Levy A.
- [x] `ALTER TABLE _ RENAME TO _`

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

Closes #1456
2025-05-10 07:57:42 +03:00
Pekka Enberg
9bc1b73d67 Merge 'bindings/javascript: Improve compatibility with better-sqlite' from Diego Reis
Probably this is larger than it should, but this PR:
- Enhance error handling (instead of `unwraps`, napi's `GenericFailure`
with custom message)
- Add missing methods and properties for Database and Statements to turn
explicit what should be done
- Implement `all()` and `run()`* methods for `Statement`
- Bump napi version to the latest stable
- Implement `DatabaseStorage::sync()` for DatabaseFile
- Add conversion from js values to limbo values
\* `run()` isn't 100% compatible yet for two reasons:
 1. The function API isn't variadic -- I chatted with napi's maintainer
in Discord and he said that this isn't possible using only napi (in this
version), and we could normalize "[...] parameters at the JavaScript
layer before passing them to Rust". Something similar to [this](https://
github.com/rolldown/rolldown/blob/main/packages/rolldown/src/utils/bindi
ngify-input-options.ts), which I plan to do in another PR.
2. better-sqlite version returns a result object, which isn't currently
supported by core (AFAIK), I also plan to do this in another PR.

Closes #1464
2025-05-10 07:55:19 +03:00
Pekka Enberg
9bf3f1e90d Merge 'Add time.Time and bool data types support in Go adapter' from Jonathan Ness
PR #1442 added support for using time.Time as query parameters.
Scanning time.Time values from query results still fails:
`sql: Scan error on column index 4, name "mod_time": unsupported Scan,
storing driver.Value type string into type *time.Time`
This change modifies the `toGoValue` function to detect RFC3339
formatted datetime strings and convert them back to time.Time objects
when reading from the database. This provides complete roundtrip support
for time.Time values.
Also added boolean support by converting Go bool values to integers (0
for false, 1 for true) when binding parameters, following SQLite's
convention for representing boolean values.

Reviewed-by: Preston Thorpe (@PThorpe92)

Closes #1465
2025-05-10 07:54:10 +03:00
Pekka Enberg
0949e7d2f2 Merge 'bindings/go: Upgrade ebitengine/purego to allow for use with go 1.23.9' from Preston Thorpe
Go 1.23.9 introduced a change with to it's linker that caused a
`duplicate symbol` error with purego 1.82
this is the recommended fix per
https://github.com/golang/go/issues/73617

Closes #1466
2025-05-10 07:53:44 +03:00
PThorpe92
efd4767b6a
Bindings/Go: Upgrade ebitengine/purego to allow for use with go 1.23.9 2025-05-09 20:25:29 -04:00
jnesss
02d141e3ce add support for bool type 2025-05-09 09:01:24 -07:00
Jussi Saurio
9a5990f87e Merge 'Add tests for INSERT with specified column-name list' from Anton Harniakou
Let's add some missing tests for the INSERT statement.

Closes #1455
2025-05-09 08:59:39 +03:00
Jussi Saurio
bda6526d28 Merge 'GROUP BY: refactor logic to support cases where no sorting is needed' from Jussi Saurio
Right now we have the following problem with GROUP BY:
- it always allocates a sorter and sorts the input rows, even when the
rows are already sorted in the right order
This PR is a refactor supporting a future PR that introduces a new
version of the optimizer which does 1. join reordering and 2. sorting
elimination based on plan cost. The PR splits GROUP BY into multiple
subsections:
1. Initializing the sorter, if needed
2. Reading rows from the sorter, if needed
3. Doing the actual grouping (this is done regardless of whether sorting
is needed)
4. Emitting rows during grouping in a subroutine (this is done
regardless of whether sorting is needed)
For example, you might currently have the following pseudo-bytecode for
GROUP BY:
```
SorterOpen (groupby_sorter)
OpenRead (users)
Rewind (users)
   <read columns from users>
   SorterInsert (groupby_sorter)
Next (users)
SorterSort (groupby_sorter)
   <do grouping>
SorterNext (groupby_sorter)
ResultRow
```
This PR allows us to do the following in cases where the rows are
already sorted:
```
OpenRead (users)
Rewind (users)
  <read columns from users>
  <do grouping>
Next (users)
ResultRow
```
---
In fact this is where the vast majority of the changes in this PR come
from -- eliminating the implied assumption that sorting for GROUP BY is
always required. The PR does not change current behavior, i.e. sorting
is always done for GROUP BY, but it adds the _ability_ to not do sorting
if the planner so decides.
The most important changes to understand are these:
```rust
/// Enum representing the source for the rows processed during a GROUP BY.
/// In case sorting is needed (which is most of the time), the variant
/// [GroupByRowSource::Sorter] encodes the necessary information about that
/// sorter.
///
/// In case where the rows are already ordered, for example:
/// "SELECT indexed_col, count(1) FROM t GROUP BY indexed_col"
/// the rows are processed directly in the order they arrive from
/// the main query loop.
#[derive(Debug)]
pub enum GroupByRowSource {
    Sorter {
        /// Cursor opened for the pseudo table that GROUP BY reads rows from.
        pseudo_cursor: usize,
        /// The sorter opened for ensuring the rows are in GROUP BY order.
        sort_cursor: usize,
        /// Register holding the key used for sorting in the Sorter
        reg_sorter_key: usize,
        /// Number of columns in the GROUP BY sorter
        sorter_column_count: usize,
        /// In case some result columns of the SELECT query are equivalent to GROUP BY members,
        /// this mapping encodes their position.
        column_register_mapping: Vec<Option<usize>>,
    },
    MainLoop {
        /// If GROUP BY rows are read directly in the main loop, start_reg is the first register
        /// holding the value of a relevant column.
        start_reg_src: usize,
        /// The grouping columns for a group that is not yet finalized must be placed in new registers,
        /// so that they don't get overwritten by the next group's data.
        /// This is because the emission of a group that is "done" is made after a comparison between the "current" and "next" grouping
        /// columns returns nonequal. If we don't store the "current" group in a separate set of registers, the "next" group's data will
        /// overwrite the "current" group's columns and the wrong grouping column values will be emitted.
        /// Aggregation results do not require new registers as they are not at risk of being overwritten before a given group
        /// is processed.
        start_reg_dest: usize,
    },
}

/// Enum representing the source of the aggregate function arguments
/// emitted for a group by aggregation.
/// In the common case, the aggregate function arguments are first inserted
/// into a sorter in the main loop, and in the group by aggregation phase
/// we read the data from the sorter.
///
/// In the alternative case, no sorting is required for group by,
/// and the aggregate function arguments are retrieved directly from
/// registers allocated in the main loop.
pub enum GroupByAggArgumentSource<'a> {
    /// The aggregate function arguments are retrieved from a pseudo cursor
    /// which reads from the GROUP BY sorter.
    PseudoCursor {
        cursor_id: usize,
        col_start: usize,
        dest_reg_start: usize,
        aggregate: &'a Aggregate,
    },
    /// The aggregate function arguments are retrieved from a contiguous block of registers
    /// allocated in the main loop for that given aggregate function.
    Register {
        src_reg_start: usize,
        aggregate: &'a Aggregate,
    },
}
```

Closes #1438
2025-05-09 08:56:38 +03:00
jnesss
c0dd79adc2 Add time.Time support for scanning query results 2025-05-08 20:51:54 -07:00
Diego Reis
242f4d7cdc bind/js: Add tests and some fixes 2025-05-08 15:57:46 -03:00
Diego Reis
64874bca4e bind/js: Add method all() 2025-05-08 15:23:39 -03:00
Diego Reis
559263ce3c bind/js: Add execution of prepared statements with bindings 2025-05-08 15:16:19 -03:00
meteorgan
261adb5ed7 fix cargo fmt 2025-05-08 22:26:50 +08:00
meteorgan
a1f981a973 handle int64 overflow by f64 2025-05-08 22:22:55 +08:00
Diego Reis
0aa46154ab bind/js: Add conversion from js types to limbo types 2025-05-08 10:32:46 -03:00
Levy A.
007cdb7ce0 fix: clippy 2025-05-08 09:24:58 -03:00
Levy A.
023a116b0d feat: initial implementation of ALTER TABLE
only supporting renaming tables
2025-05-08 09:24:56 -03:00
Diego Reis
74f585d2e0 bind/js: Add missing methods for Database and Statement 2025-05-08 08:46:38 -03:00
Diego Reis
e4014d290f bind/js: Implement DatabaseStorage::sync 2025-05-08 08:42:13 -03:00
Diego Reis
259bfc04b7 bind/js: Improve error handling 2025-05-08 08:40:53 -03:00
Diego Reis
f0a08612b0 bind/js: Close Database connection when it is garbage collected 2025-05-08 08:36:27 -03:00
Diego Reis
787de5e2b3 bind/js: Comment out unused properties
Just left here so we don't lose track of which properties the objects should have
2025-05-08 08:34:48 -03:00
Diego Reis
25df20dc97 bind/js: Bump napi and napi-derive versions 2025-05-08 08:22:04 -03:00
meteorgan
ef3f004e30 refactor numeric literal 2025-05-08 18:37:17 +08:00
Jussi Saurio
37097e01ae GROUP BY: refactor logic to support cases where no sorting is needed 2025-05-08 12:39:26 +03:00
Jussi Saurio
ae2561dbca Merge 'Fix memory leak caused by unclosed virtual table cursors' from Piotr Rżysko
The following code reproduces the leak (memory usage increases over
time):
```rust
#[tokio::main]
async fn main() {
    let db = Builder::new_local(":memory:").build().await.unwrap();
    let conn = db.connect().unwrap();

    conn.execute("SELECT load_extension('./target/debug/liblimbo_series');", ())
        .await
        .unwrap();

    loop {
        conn.execute("SELECT * FROM generate_series(1,10,2);", ())
            .await
            .unwrap();
    }
}
```
After switching to the system allocator, the leak becomes detectable
with Valgrind:
```
32,000 bytes in 1,000 blocks are definitely lost in loss record 24 of 24
   at 0x538580F: malloc (vg_replace_malloc.c:446)
   by 0x62E15FA: alloc::alloc::alloc (alloc.rs:99)
   by 0x62E172C: alloc::alloc::Global::alloc_impl (alloc.rs:192)
   by 0x62E1530: allocate (alloc.rs:254)
   by 0x62E1530: alloc::alloc::exchange_malloc (alloc.rs:349)
   by 0x62E0271: new<limbo_series::GenerateSeriesCursor> (boxed.rs:257)
   by 0x62E0271: open_GenerateSeriesVTab (lib.rs:19)
   by 0x425D8FA: limbo_core::VirtualTable::open (lib.rs:732)
   by 0x4285DDA: limbo_core::vdbe::execute::op_vopen (execute.rs:890)
   by 0x42351E8: limbo_core::vdbe::Program::step (mod.rs:396)
   by 0x425C638: limbo_core::Statement::step (lib.rs:610)
   by 0x40DB238: limbo::Statement::execute::{{closure}} (lib.rs:181)
   by 0x40D9EAF: limbo::Connection::execute::{{closure}} (lib.rs:109)
   by 0x40D54A1: example::main::{{closure}} (example.rs:26)
```
Interestingly, when using mimalloc, neither Valgrind nor mimalloc’s
internal statistics report the leak.

Reviewed-by: Preston Thorpe (@PThorpe92)

Closes #1447
2025-05-08 10:48:23 +03:00
sunrabbit123
67dbd59e2d
fix(bindings/wasm): add types property for typescript setting
Signed-off-by: sunrabbit123 <qudwls185@naver.com>
2025-05-08 14:42:54 +09:00
Jonathan Ness
dd56d92b6a
Merge branch 'main' into feature/go-transactions 2025-05-07 12:48:20 -07:00
Pekka Enberg
2bd221e5db Merge 'Add embedded library support to Go adapter' from Jonathan Ness
This change enables the Go adapter to embed platform-specific libraries
and extract them at runtime, eliminating the need for users to set
LD_LIBRARY_PATH or other environment variables.
- Add embedded.go with core library extraction functionality
- Update limbo_unix.go and limbo_windows.go to use embedded libraries
- Add build_lib.sh script to generate platform-specific libraries
- Update README.md with documentation for the new feature
- Add .gitignore to prevent committing binary files
- Add test coverage for Vector operations (vector(), vector_extract(),
vector_distance_cos()) and sqlite core features
The implementation maintains backward compatibility with the traditional
library loading mechanism as a fallback. This approach is inspired by
projects like go-embed-python that use a similar technique for native
library distribution.
https://github.com/tursodatabase/limbo/issues/506

Reviewed-by: Preston Thorpe (@PThorpe92)

Closes #1434
2025-05-07 22:31:29 +03:00
Pekka Enberg
e7902541cd Merge 'Add time.Time support to Go driver parameter binding' from Jonathan Ness
### Problem
The Limbo Go driver currently throws an "unsupported type" error when
trying to bind `time.Time` values as query parameters. This requires
applications to manually convert datetime values to strings before
passing them to queries.
### Solution
Added `time.Time` support to the `buildArgs` function in `types.go`. The
implementation:
- Converts `time.Time` to RFC3339 format strings
- Uses the existing `textVal` type for storage
- Maintains compatibility with Limbo's datetime handling
### Example Usage
```go
// Previously failed with "unsupported type: time.Time"
now := time.Now()
db.Exec("INSERT INTO events (timestamp) VALUES (?)", now)

// Now works as expected
```
### Testing
I tested with various datetime operations:
- Parameter binding with time.Time
- Round-trip storage and retrieval
- Compatibility with existing datetime functions
Values are stored in standard ISO8601/RFC3339 format which I believe is
same as sqlite.

Reviewed-by: Preston Thorpe (@PThorpe92)

Closes #1442
2025-05-07 22:30:44 +03:00
Jussi Saurio
d592e8ec8a Merge 'Show explanation for the NewRowid opcode' from Anton Harniakou
After this commit explain will start showing a comment for NewRowid.
```
limbo> explain insert into t(id) values (1);
addr  opcode             p1    p2    p3    p4             p5  comment
----  -----------------  ----  ----  ----  -------------  --  -------
0     Init               0     17    0                    0   Start at 17
...
6     NewRowid           0     1     0                    0   r[1]=rowid
...
18    Goto               0     1     0                    0
```

Closes #1445
2025-05-07 09:15:46 +03:00
Jussi Saurio
57b16d5b2b Merge 'Add notion of join ordering to plan' from Jussi Saurio
This PR is an enabler for our (Coming Soon ™️ ) join reordering
optimizer -- simply adds the notion of a join order to the current query
execution. This PR does not do any join ordering -- the join order is
always the same as expressed in the SQL query.

Reviewed-by: Preston Thorpe (@PThorpe92)

Closes #1439
2025-05-07 08:55:13 +03:00
jnesss
a4c0f57f82 added embedded library usage notes 2025-05-06 21:43:18 -07:00
jnesss
548bcd4692 accept debug or release parameter - default to release 2025-05-06 20:44:02 -07:00
Anton Harniakou
d907f95716 Add tests for INSERT with specified column-name list 2025-05-06 11:31:41 +03:00
Piotr Rzysko
977b6b331a Fix memory leak caused by unclosed virtual table cursors
The following code reproduces the leak, with memory usage increasing
over time:

```
#[tokio::main]
async fn main() {
    let db = Builder::new_local(":memory:").build().await.unwrap();
    let conn = db.connect().unwrap();

    conn.execute("SELECT load_extension('./target/debug/liblimbo_series');", ())
        .await
        .unwrap();

    loop {
        conn.execute("SELECT * FROM generate_series(1,10,2);", ())
            .await
            .unwrap();
    }
}
```

After switching to the system allocator, the leak becomes detectable
with Valgrind:

```
32,000 bytes in 1,000 blocks are definitely lost in loss record 24 of 24
   at 0x538580F: malloc (vg_replace_malloc.c:446)
   by 0x62E15FA: alloc::alloc::alloc (alloc.rs:99)
   by 0x62E172C: alloc::alloc::Global::alloc_impl (alloc.rs:192)
   by 0x62E1530: allocate (alloc.rs:254)
   by 0x62E1530: alloc::alloc::exchange_malloc (alloc.rs:349)
   by 0x62E0271: new<limbo_series::GenerateSeriesCursor> (boxed.rs:257)
   by 0x62E0271: open_GenerateSeriesVTab (lib.rs:19)
   by 0x425D8FA: limbo_core::VirtualTable::open (lib.rs:732)
   by 0x4285DDA: limbo_core::vdbe::execute::op_vopen (execute.rs:890)
   by 0x42351E8: limbo_core::vdbe::Program::step (mod.rs:396)
   by 0x425C638: limbo_core::Statement::step (lib.rs:610)
   by 0x40DB238: limbo::Statement::execute::{{closure}} (lib.rs:181)
   by 0x40D9EAF: limbo::Connection::execute::{{closure}} (lib.rs:109)
   by 0x40D54A1: example::main::{{closure}} (example.rs:26)
```

Interestingly, when using mimalloc, neither Valgrind nor mimalloc’s
internal statistics report the leak.
2025-05-05 21:26:23 +02:00
Anton Harniakou
d74df2473e EXPLAIN should show a comment for the Insert opcode 2025-05-05 10:54:59 +03:00
김선우
aeca307c90 Add reason for the @Disabled 2025-05-05 15:04:48 +09:00
김선우
cb1c538a03 Remove @Disalbed annotation for basic UPDATE and DELETE 2025-05-05 14:51:52 +09:00
Anton Harniakou
a971bce353 Show explanation for the NewRowid opcode 2025-05-04 14:14:19 +03:00
Anton Harniakou
2432e0561e Fix clippy warnings 2025-05-04 13:02:54 +03:00
Anton Harniakou
9d31f5d848 Test DROP INDEX 2025-05-04 12:18:36 +03:00
Anton Harniakou
6c8eef2fac Support DROP INDEX
This commit adds suport for DROP INDEX.
Bytecode produced by this commit differs from SQLITE's bytecode, main
reason we don't do autovacuum or repacking of pages like SQLITE does.
2025-05-04 12:13:16 +03:00