Commit Graph

178 Commits

Author SHA1 Message Date
PThorpe92
d7c3256a5a Update vtab derive macro to pass readonly const to module impl 2025-07-23 16:59:18 -04:00
PThorpe92
e7ce3efb3f Remove default trait impl from csv extension 2025-07-23 16:50:57 -04:00
PThorpe92
eff455fb03 Add READONLY const property to virtual table module trait 2025-07-23 16:44:04 -04:00
Jussi Saurio
022f679fab chore: make every CREATE TABLE stmt in entire repo have 1 space after tbl name
`BTreeTable::to_sql` makes us incompatible with SQLite by losing e.g. the original whitespace provided during the CREATE TABLE command.

For now let's fix our tests by regex-replacing every CREATE TABLE in
the entire repo to have exactly 1 space after the table name in the
CREATE TABLE statement.
2025-07-22 11:35:21 +03:00
Nils Koch
f032de3d63 chore: link advapi32 on windows 2025-07-16 19:34:42 +01:00
Pekka Enberg
f72ceaf177 Merge 'extensions/vtab: fix i32 being passed as i64 across FFI boundary' from Jussi Saurio
as nilskch points out in #1807, Rust 1.88.0 is stricter about alignment
checks.
because rust integers default to `i32`, we were casting a pointer to an
`i32` as a pointer to an `i64` causing a panic when dereferenced due to
misalignment as rust expects it to be 8 byte aligned.

Reviewed-by: Preston Thorpe (@PThorpe92)

Closes #2064
2025-07-16 08:28:24 +03:00
Piotr Rzysko
30ae6538ee Treat table-valued functions as tables
With this change, the following two queries are considered equivalent:
```sql
SELECT value FROM generate_series(5, 50);
SELECT value FROM generate_series WHERE start = 5 AND stop = 50;
```
Arguments passed in parentheses to the virtual table name are now
matched to hidden columns.

Column references are still not supported as table-valued function
arguments. The only difference is that previously, a query like:
```sql
SELECT one.value, series.value
FROM (SELECT 1 AS value) one, generate_series(one.value, 3) series;
```
would cause a panic. Now, it returns a proper error message instead.

Adding support for column references is more nuanced for two main
reasons:
- We need to ensure that in joins where a TVF depends on other tables,
those other tables are processed first. For example, in:
```sql
SELECT one.value, series.value
FROM generate_series(one.value, 3) series, (SELECT 1 AS value) one;
```
the one table must be processed by the top-level loop, and series must
be nested.
- For outer joins involving TVFs, the arguments must be treated as ON
predicates, not WHERE predicates.
2025-07-14 07:16:53 +02:00
Piotr Rzysko
631d62e9f8 Implement best_index for table-valued functions
In SQLite, the field equivalent to `constraint_usage` (`aConstraintUsage`
from `sqlite3_index_info`) is used to request arguments that are later
passed to the `xFilter` method. In Limbo, this behavior applies to
virtual tables, but not to table-valued functions. Currently, TVFs have
dedicated handling that passes all function arguments to the filter
method and doesn't use information provided in the `constraint_usage`
field.

This commit is a step toward unifying the handling of virtual tables and
TVFs.
2025-07-14 07:16:53 +02:00
Piotr Rzysko
000d70f1f3 Propagate info about hidden columns 2025-07-14 07:16:53 +02:00
Piotr Rzysko
37b180dc8c Support multiple tables in kv_store extension 2025-07-14 06:24:29 +02:00
Jussi Saurio
ab3001ac37 extensions/vtab: remove unnecessary as *const as *mut trickery 2025-07-12 20:04:12 +03:00
Jussi Saurio
4aa2c846bb extensions/vtab: fix i32 being passed as i64 across FFI boundary
as nilskch points out in #1807, Rust 1.88.0 is stricter about
alignment.

because rust integers default to `i32`, we were casting a pointer
to an `i32` as a pointer to an `i64` causing a panic when dereferenced
due to misalignment as rust expects it to be 8 byte aligned.
2025-07-12 19:54:48 +03:00
Nils Koch
1a91966c7e fix clippy errors for rust 1.88.0 (manual fix) 2025-07-12 18:58:55 +03:00
Nils Koch
828d4f5016 fix clippy errors for rust 1.88.0 (auto fix) 2025-07-12 18:58:41 +03:00
Pekka Enberg
60191e7c7b Move series extension to core
It's part of upstream SQLite too.
2025-06-30 10:29:34 +03:00
Pekka Enberg
39fd84f297 Move time extension to core 2025-06-30 10:01:03 +03:00
Pekka Enberg
12131babae Move UUID extension to core
We want to bundle the UUID extension by default so move the code to core.
2025-06-30 09:54:13 +03:00
Pekka Enberg
eb0de4066b Rename limbo_ext crate to turso_ext 2025-06-29 12:14:08 +03:00
Pekka Enberg
eec994386b Rename limbo_macros to turso_macros 2025-06-29 12:00:17 +03:00
Pekka Enberg
53ba3ff926 Rename limbo_core crate to turso_core 2025-06-29 09:59:17 +03:00
Nils Koch
2827b86917 chore: fix clippy warnings 2025-06-23 19:52:13 +01:00
Pekka Enberg
90c1e3fc06 Switch Connection to use Arc instead of Rc
Connection needs to be Arc so that bindings can wrap it with `Mutex` for
multi-threading.
2025-06-16 10:43:19 +03:00
PThorpe92
954df3f837 Fix csv test assertion 2025-05-24 17:33:01 -04:00
PThorpe92
e53e30e06d Fix tests in csv extension to adapt to new API 2025-05-24 17:16:45 -04:00
PThorpe92
ef28906be3 Update extensions README with example for xConnect 2025-05-24 17:10:26 -04:00
PThorpe92
cf163f2dc0 Prevent double free in ext connection 2025-05-24 16:49:52 -04:00
PThorpe92
1cacbf1f0d Close statements in extension tests, and use mut pointers for stmt 2025-05-24 16:45:25 -04:00
PThorpe92
d63f9d8cff Make sure all resources are cleaned up properly in xconnect 2025-05-24 16:38:33 -04:00
PThorpe92
a4ed464ec4 Add some traces for errors in xconnect 2025-05-24 15:44:06 -04:00
PThorpe92
0decafbbc1 Use transparent struct in public api wrapper for vtab connect 2025-05-24 15:32:14 -04:00
PThorpe92
2e4343402e Add null checks to prevent double frees in vtab connections 2025-05-24 15:20:09 -04:00
PThorpe92
faa12987b4 Add test case to table stats extension 2025-05-24 14:49:59 -04:00
PThorpe92
4142d813c0 Change method name to bind_at to better reflect args in ext Statement 2025-05-24 14:49:58 -04:00
PThorpe92
58e1d5a4f8 Add additional test vtable extension for querying core 2025-05-24 14:49:58 -04:00
PThorpe92
d11ef6b9c5 Add execute method to xConnect db interface for vtables 2025-05-24 14:49:58 -04:00
PThorpe92
c2ec6caae1 Finish integrating xConnect into vtable open api 2025-05-24 14:49:58 -04:00
PThorpe92
cbd7245677 Update Vtable open method to accept core db connection 2025-05-24 14:49:58 -04:00
PThorpe92
2c784070f1 Impl Default for ext Value 2025-05-24 14:49:58 -04:00
PThorpe92
d51614a4fd Create extern functions to support vtab xConnect in core/ext 2025-05-24 14:49:57 -04:00
Piotr Rzysko
ad9d044a04 Add CSV extension 2025-05-21 09:22:59 +02:00
Piotr Rzysko
9c1dca72db Introduce VTable
This allows storing table arguments parsed in the VTabModule::create
method.
2025-05-21 08:33:17 +02:00
Piotr Rzysko
ea0ec6a216 Remove VTabModule methods delegating directly to VTabCursor 2025-05-21 08:29:20 +02: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
Jussi Saurio
4e05023bd3 Merge branch 'main' into ext-static-feature 2025-05-03 19:18:28 +03:00
PThorpe92
245e7f94f6 Store packed field on ConstraintInfo to optimize planning for vfilter 2025-04-17 14:01:45 -04:00
PThorpe92
95a2fdc096 Fix array from ptr in bestindex ffi method in proc macro 2025-04-17 14:01:45 -04:00
PThorpe92
e17fd7edc4 Add comments and address PR review 2025-04-17 14:01:44 -04:00
PThorpe92
528a9b6c7e Clean up allocations in main loop and fix ext tests 2025-04-17 14:01:44 -04:00
PThorpe92
de27c2fe4c Properly handle pushing predicates for query optimization from xBestIndex 2025-04-17 14:01:37 -04:00
PThorpe92
853af16946 Implement xBestIndex for virtual table api to improve query planning 2025-04-17 13:53:27 -04:00