sqlite-vec: SELECT WITH IN broken.

I have a query

def chunks_for(prompt, k:1024, facets:{}, limit:64, rerank:true)
    embedding = local_embedding_for(prompt)
    
    stmts = [
      "SELECT id, distance FROM embeddings WHERE embedding MATCH ? AND k = ?"
    ] 
    
    if facets.size > 0
      chunk_ids = chunk_ids_for_facets(facets)
      stmts.push "AND id IN(#{ chunk_ids.join(',') })"
    end

    stmts.push "ORDER BY distance"

    query =
      stmts.join(' ')
      
    rows =
      @vec0.execute(
        query,
        [embedding.pack("f*"), k]
      ) 
      
    rows =
      rows.sort do |a, b|
        b.last <=> a.last # ORDER BY distance DESC
      end
      
    ids =
      rows.map{|row| row.first}
        
    chunks =
      @chunks.where(id: ids).to_a
  
    if rerank
      reranked = rerank(prompt, chunks)
      chunks = reranked
    end
    
    chunks = chunks.first(limit)
    
    return chunks
  end


but, running this seems to always return the results of the IN query in that order. meaning, this seems to mess up the query planner. instead of returning matched rows that also are in a set of IDs, it returns only the set of IDS, in that exact order.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 17 (6 by maintainers)

Most upvoted comments

sqlite-vec will apply metadata filters before performing the KNN query, so something seems off here.

I’m trying to reproduce what you’re seeing on my end, and I’m having trouble:

.echo on
.bail on

.load dist/vec0
.mode box
.header on

create virtual table if not exists rag using vec0(
  id integer primary key,
  embedding float[1],
  subreddit text
);

insert into rag(id, embedding, subreddit) values 
  (1, '[1]', 'r/science'),
  (2, '[2]', 'r/science'),
  (3, '[3]', 'r/humor'),
  (4, '[4]', 'r/science'),
  (5, '[5]', 'r/science'),
  (6, '[6]', 'r/science'),
  (7, '[7]', 'r/science'),
  (8, '[8]', 'r/science'),
  (9, '[9]', 'r/humor'),
  (10, '[10]', 'r/science'),
  (11, '[11]', 'r/humor'),
  (12, '[12]', 'r/science'),
  (13, '[13]', 'r/humor');

SELECT id, subreddit, distance
FROM rag 
WHERE embedding MATCH '[0]' 
  AND k = 5
  AND subreddit = 'r/humor';
/*
┌────┬───────────┬──────────┐
│ id │ subreddit │ distance │
├────┼───────────┼──────────┤
│ 3  │ r/humor   │ 3.0      │
│ 9  │ r/humor   │ 9.0      │
│ 11 │ r/humor   │ 11.0     │
│ 13 │ r/humor   │ 13.0     │
└────┴───────────┴──────────┘
*/


SELECT id, subreddit, distance
FROM rag 
WHERE embedding MATCH '[0]' 
  AND k = 5
  AND subreddit = 'r/science';
/*
┌────┬───────────┬──────────┐
│ id │ subreddit │ distance │
├────┼───────────┼──────────┤
│ 1  │ r/science │ 1.0      │
│ 2  │ r/science │ 2.0      │
│ 4  │ r/science │ 4.0      │
│ 5  │ r/science │ 5.0      │
│ 6  │ r/science │ 6.0      │
└────┴───────────┴──────────┘
*/

(removed some of the other metadata columns that weren’t needed)

Could you try running an EXPLAIN QUERY PLAN on your KNN SQL query? the output should love something like this:

SCAN rag VIRTUAL TABLE INDEX 0:3{___}___&Aa_

It’s a bit hard to read, but I can use that to debug this further.

Alternatively if you’re ok sharing the database itself I can try to debug that too (feel free to email it to me)

I’d also try to confirm a few other things:

  • The filter clause you’re applying (subreddit = 'r/science') has values in the table (ex run `select id from rag where subreddit = ‘r/science’ limit 5) to ensure they exist in the table)
  • Increase the values of k to see if that effects results (it shouldnt, if it does that indicates a bug on sqlite-vec’s side)
  • Ensure there’s no other JOINs other other WHERE clauses in the KNN query