Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/ext/vec1/tool/vec1bench_vibe2db.py # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / ext / vec1 / tool / vec1bench_vibe2db.py` 91 lines · 2389 bytes Raw Blame History 1 2 import h5py 3 import sqlite3 4 import numpy as np 5 import os 6 import sys 7 8 def sqlite_type_for(dtype): 9 # Everything goes in as BLOB; dtype is recorded separately 10 return "BLOB" 11 12 def sanitize(name): 13 # HDF5 paths like "/train" → "train" 14 return name.strip("/").replace("/", "_") 15 16 def main(h5_path, sqlite_path): 17 if os.path.exists(sqlite_path): 18 os.remove(sqlite_path) 19 20 conn = sqlite3.connect(sqlite_path) 21 cur = conn.cursor() 22 23 with h5py.File(h5_path, "r") as f: 24 25 dm = "unknown" 26 cur.execute("CREATE TABLE meta(field TEXT, value ANY)") 27 if "distance" in f.attrs: 28 dm = f.attrs["distance"] 29 if isinstance(dm, bytes): 30 dm = dm.decode("utf-8") 31 cur.execute("INSERT INTO meta VALUES('distance', ?)", (dm,)) 32 33 print("VIBE distance metric:", dm) 34 35 def visit(name, obj): 36 if not isinstance(obj, h5py.Dataset): 37 return 38 39 40 41 data = obj[()] 42 table = sanitize(name) 43 44 print(f"Importing {name} → table '{table}' shape={data.shape} dtype={data.dtype}") 45 46 # Create table 47 cur.execute(f""" 48 CREATE TABLE {table} ( 49 id INTEGER PRIMARY KEY, 50 vec BLOB NOT NULL 51 ) 52 """) 53 54 # Metadata table (created once) 55 cur.execute(""" 56 CREATE TABLE IF NOT EXISTS __meta__ ( 57 table_name TEXT PRIMARY KEY, 58 dtype TEXT, 59 shape TEXT 60 ) 61 """) 62 63 cur.execute( 64 "INSERT INTO __meta__ VALUES (?, ?, ?)", 65 (table, str(data.dtype), str(data.shape)) 66 ) 67 68 # Insert rows 69 if data.ndim == 1: 70 rows = [(i, data[i].tobytes()) for i in range(data.shape[0])] 71 else: 72 rows = [(i, data[i].tobytes()) for i in range(data.shape[0])] 73 74 cur.executemany( 75 f"INSERT INTO {table} (id, vec) VALUES (?, ?)", 76 rows 77 ) 78 79 conn.commit() 80 81 f.visititems(visit) 82 83 conn.close() 84 print("Done.") 85 86 if __name__ == "__main__": 87 if len(sys.argv) != 3: 88 print("usage: hdf5_to_sqlite.py input.hdf5 output.sqlite") 89 sys.exit(1) 90 91 main(sys.argv[1], sys.argv[2])