Repository: tursodatabase/limbo (rust)

Issue:
Extension tests fail
Extension tests fail as follows:

```
limbo % make test-extensions
cargo build
   Compiling limbo_uuid v0.0.18 (/Users/penberg/src/penberg/limbo/extensions/uuid)
   Compiling limbo_time v0.0.18 (/Users/penberg/src/penberg/limbo/extensions/time)
   Compiling limbo_completion v0.0.18 (/Users/penberg/src/penberg/limbo/extensions/completion)
   Compiling limbo_core v0.0.18 (/Users/penberg/src/penberg/limbo/core)
   Compiling limbo v0.0.18 (/Users/penberg/src/penberg/limbo/bindings/rust)
   Compiling core_tester v0.0.18 (/Users/penberg/src/penberg/limbo/tests)
   Compiling limbo-wasm v0.0.18 (/Users/penberg/src/penberg/limbo/bindings/wasm)
   Compiling limbo-go v0.0.18 (/Users/penberg/src/penberg/limbo/bindings/go)
   Compiling limbo_cli v0.0.18 (/Users/penberg/src/penberg/limbo/cli)
   Compiling limbo_node v0.0.18 (/Users/penberg/src/penberg/limbo/bindings/javascript)
   Compiling limbo_stress v0.0.18 (/Users/penberg/src/penberg/limbo/stress)
   Compiling py-limbo v0.0.18 (/Users/penberg/src/penberg/limbo/bindings/python)
   Compiling limbo_sqlite3 v0.0.18 (/Users/penberg/src/penberg/limbo/sqlite3)
   Compiling limbo-java v0.0.18 (/Users/penberg/src/penberg/limbo/bindings/java)
   Compiling limbo_sim v0.0.18 (/Users/penberg/src/penberg/limbo/simulator)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 5.73s
cargo build --package limbo_regexp
   Compiling libc v0.2.171
   Compiling memchr v2.7.4
   Compiling cc v1.2.17
   Compiling syn v2.0.100
   Compiling aho-corasick v1.1.3
   Compiling getrandom v0.3.2
   Compiling libmimalloc-sys v0.1.40
   Compiling regex-automata v0.4.9
   Compiling limbo_macros v0.0.18 (/Users/penberg/src/penberg/limbo/macros)
   Compiling mimalloc v0.1.44
   Compiling regex v1.11.1
   Compiling limbo_ext v0.0.18 (/Users/penberg/src/penberg/limbo/extensions/core)
   Compiling limbo_regexp v0.0.18 (/Users/penberg/src/penberg/limbo/extensions/regexp)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 3.14s
./testing/cli_tests/extensions.py
Extension ./target/debug/liblimbo_regexp loaded successfully.
Testing: uuid functions are registered properly with ext loaded
Testing: scalar alias's are registered properly
Testing: median agg function returns null when ext not loaded
Testing: median agg function works
Testing: median agg function works with odd number of elements
Testing: test aggregate percentile function with 2 arguments works
Testing: test aggregate percentile function with 1 argument works
Testing: crypto_blake3 returns null when ext not loaded
Testing: blake3 should encrypt correctly
Testing: md5 should encrypt correctly
Testing: sha1 should encrypt correctly
Testing: sha256 should encrypt correctly
Testing: sha384 should encrypt correctly
Testing: sha512 should encrypt correctly
Testing: base32 should encode correctly
Testing: base32 should decode correctly
Testing: base64 should encode correctly
Testing: base64 should decode correctly
Testing: base85 should encode correctly
Testing: base85 should decode correctly
Testing: hex should encode correctly
Testing: hex should decode correctly
Testing: url should encode correctly
Testing: url should decode correctly
Testing: can create kv_store vtable
Testing: can insert into kv_store vtable
Test FAILED: Test failed
SQL: insert into t values ('hello', 'world');
Actual:
'× Corrupt database: Parse error: no such table: t'
make: *** [test-extensions] Error 1
```

Reverting 265457f175a5de2ac4b5dc4917ea694700d87029 fixes it. The weird thing is that the "no such table" error does not happen if I run the commands in Limbo shell.

Pull request:
Fix virtual table translation issues
closes #1240 

some idiot (read: me) had the init label of `CREATE VIRTUAL TABLE` translation completely in the wrong place :thinking: 



Patch:
diff --git a/core/translate/schema.rs b/core/translate/schema.rs
index a887bdef..29cf2964 100644
--- a/core/translate/schema.rs
+++ b/core/translate/schema.rs
@@ -462,7 +462,8 @@ pub fn translate_create_virtual_table(
         approx_num_insns: 40,
         approx_num_labels: 2,
     });
-
+    let init_label = program.emit_init();
+    let start_offset = program.offset();
     let module_name_reg = program.emit_string8_new_reg(module_name_str.clone());
     let table_name_reg = program.emit_string8_new_reg(table_name.clone());
 
@@ -520,8 +521,6 @@ pub fn translate_create_virtual_table(
         where_clause: parse_schema_where_clause,
     });
 
-    let init_label = program.emit_init();
-    let start_offset = program.offset();
     program.emit_halt();
     program.resolve_label(init_label, program.offset());
     program.emit_transaction(true);
diff --git a/core/util.rs b/core/util.rs
index 13b53bcb..f1769923 100644
--- a/core/util.rs
+++ b/core/util.rs
@@ -58,7 +58,7 @@ pub fn parse_schema_rows(
                         "table" => {
                             let root_page: i64 = row.get::<i64>(3)?;
                             let sql: &str = row.get::<&str>(4)?;
-                            if root_page == 0 && sql.to_lowercase().contains("virtual") {
+                            if root_page == 0 && sql.to_lowercase().contains("create virtual") {
                                 let name: &str = row.get::<&str>(1)?;
                                 let vtab = syms.vtabs.get(name).unwrap().clone();
                                 schema.add_virtual_table(vtab);
diff --git a/core/vdbe/execute.rs b/core/vdbe/execute.rs
index 31a64d49..3c511a0d 100644
--- a/core/vdbe/execute.rs
+++ b/core/vdbe/execute.rs
@@ -4110,7 +4110,7 @@ pub fn op_parse_schema(
     let conn = program.connection.upgrade();
     let conn = conn.as_ref().unwrap();
     let stmt = conn.prepare(format!(
-        "SELECT * FROM  sqlite_schema WHERE {}",
+        "SELECT * FROM sqlite_schema WHERE {}",
         where_clause
     ))?;
     let mut schema = conn.schema.write();


Test patch:
diff --git a/testing/cli_tests/extensions.py b/testing/cli_tests/extensions.py
index cb73aa76..d898908f 100755
--- a/testing/cli_tests/extensions.py
+++ b/testing/cli_tests/extensions.py
@@ -339,15 +339,16 @@ def test_series():
 def test_kv():
     ext_path = "target/debug/liblimbo_ext_tests"
     limbo = TestLimboShell()
+    # first, create a normal table to ensure no issues
+    limbo.execute_dot("CREATE TABLE other (a,b,c);")
+    limbo.execute_dot("INSERT INTO other values (23,32,23);")
     limbo.run_test_fn(
         "create virtual table t using kv_store;",
         lambda res: "Virtual table module not found: kv_store" in res,
     )
     limbo.execute_dot(f".load {ext_path}")
-    limbo.run_test_fn(
+    limbo.debug_print(
         "create virtual table t using kv_store;",
-        null,
-        "can create kv_store vtable",
     )
     limbo.run_test_fn(
         "insert into t values ('hello', 'world');",
diff --git a/testing/cli_tests/test_limbo_cli.py b/testing/cli_tests/test_limbo_cli.py
index 10e87869..8b6a6137 100755
--- a/testing/cli_tests/test_limbo_cli.py
+++ b/testing/cli_tests/test_limbo_cli.py
@@ -111,7 +111,6 @@ class TestLimboShell:
         if init_commands is None:
             # Default initialization
             init_commands = """
-.open :memory:
 CREATE TABLE users (id INTEGER PRIMARY KEY, first_name TEXT, last_name TEXT, age INTEGER);
 CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT, price INTEGER);
 INSERT INTO users VALUES (1, 'Alice', 'Smith', 30), (2, 'Bob', 'Johnson', 25),