sqlite-vss icon indicating copy to clipboard operation
sqlite-vss copied to clipboard

`SqliteError: SQL logic error` when using with `better-sqlite3` under node

Open jdb8 opened this issue 11 months ago • 6 comments

Hi there! I'm trying to get something simple running using better-sqlite3 by following the instructions here: https://alexgarcia.xyz/sqlite-vss/nodejs.html

My code looks as follows:

import Database from 'better-sqlite3';
import * as sqlite_vss from 'sqlite-vss';

const db = new Database(':memory:');
sqlite_vss.load(db);

const version = db.prepare('select vss_version()').pluck().get();
console.log(version);

db.exec('create virtual table vss_demo using vss0(a(3));');

const stmt = db.prepare('INSERT INTO vss_demo VALUES (?)');
const selectStmt = db.prepare('SELECT * from vss_demo;');

const embedding1 = [0.1, 0.2, 0.3];
const embedding2 = [0.2, 0.3, 0.4];

stmt.run(JSON.stringify(embedding1));
stmt.run(JSON.stringify(embedding2));

console.log(selectStmt.all());

The select vss_version() call works, printing v0.1.1-alpha.20, which tells me that the extension is successfully installed and that better-sqlite3 is working.

However, when trying to insert two embeddings, on the second I get the following:

❯ node index.mjs
v0.1.1-alpha.20

file:///Users/jbateson/code/sqlite-vss-testing/index.mjs:19
stmt.run(JSON.stringify(embedding2));
     ^
SqliteError: SQL logic error
    at file:///Users/jbateson/code/sqlite-vss-testing/index.mjs:19:6
    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:61:12) {
  code: 'SQLITE_ERROR'
}

If I remove the second insertion call, and insert only one entry, I get no error but the select query seems to return nothing (which implies that the first insertion didn't work):

❯ node index.mjs
v0.1.1-alpha.20
[ { a: null } ]

I'm not sure if this is me doing something wrong - unfortunately the node docs only contain examples for insertions and not table creation or querying - but I've tried to also read https://alexgarcia.xyz/sqlite-vss/getting-started.html#introduction too and those examples give me a similar inscrutible "SQL logic error". I also can't spot any node testcases in this repo from which to copy from.

I've created a repro of this at https://github.com/jdb8/sqlite-vss-testing if that's helpful. Please let me know if you're unable to repro or have more questions!

jdb8 avatar Jul 18 '23 01:07 jdb8