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/cortex/blob/main/tests/fts/oracle.c # cortex aien-dev/cortex Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `cortex / tests / fts / oracle.c` 388 lines ยท 17760 bytes Raw Blame History 1 /* 2 ** Verification oracle for the Omega FTS slice (tests/fts). NOT product code. 3 ** Links sqlite3.c from libsqlite3-sys-0.28.0 (SQLite 3.45.0), compiled with the 4 ** same -D flags libsqlite3-sys build.rs passes (see run.sh), so FTS5, bm25, 5 ** the unicode61 tokenizer and LIKE behave exactly as inside cortex-rs. 6 ** 7 ** Modes: 8 ** oracle plan DB EXPLAIN QUERY PLAN of the cortex-rs search SQL 9 ** oracle dump DB OUT binary dump of entities_fts rows + entities (LIKE subjects) 10 ** oracle tok DB OUT unicode61 token list of every entities_fts row 11 ** oracle query DB QFILE OUT run the exact cortex-rs FTS SQL for every query x space x limit 12 ** oracle like DB PFILE OUT canonical_name LIKE ?1 OR content LIKE ?1 bitmap per pattern 13 ** oracle log OUT N glibc log() on N inputs (see flogcases below) 14 */ 15 #include 16 #include 17 #include 18 #include 19 #include 20 #include 21 #include 22 #include "sqlite3.h" 23 24 static const char *SEARCH_SQL = 25 "SELECT e.id, e.space_id, e.space_slug, e.entity_type, e.canonical_name, e.content, e.aliases_json, e.metadata_json, e.confidence, e.revision, e.retracted, e.valid_from, e.valid_to, e.created_at,\n" 26 " COALESCE(bm25(entities_fts), 5.0) as bm25_rank\n" 27 " FROM entities e\n" 28 " JOIN entities_fts f ON f.id = e.id\n" 29 " WHERE entities_fts MATCH ?1 AND e.space_slug = ?2 AND e.retracted = 0\n" 30 " ORDER BY bm25_rank ASC\n" 31 " LIMIT ?3"; 32 33 static void die(const char *m, sqlite3 *db){ 34 fprintf(stderr, "oracle: %s: %s\n", m, db ? sqlite3_errmsg(db) : ""); 35 exit(2); 36 } 37 38 /* output files are created 0644 (subject to umask), never world-writable */ 39 static FILE *wopen(const char *path){ 40 int fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, 0644); 41 if( fd<0 ) return 0; 42 FILE *f = fdopen(fd, "w"); 43 if( !f ) close(fd); 44 return f; 45 } 46 47 static sqlite3 *opendb(const char *path){ 48 sqlite3 *db = 0; 49 if( sqlite3_open_v2(path, &db, SQLITE_OPEN_READONLY, 0) ) die("open", db); 50 return db; 51 } 52 53 static void put32(FILE *f, uint32_t v){ fwrite(&v, 4, 1, f); } 54 static void put64(FILE *f, uint64_t v){ fwrite(&v, 8, 1, f); } 55 static void putstr(FILE *f, sqlite3_stmt *st, int i){ 56 if( sqlite3_column_type(st, i)==SQLITE_NULL ){ put32(f, 0xFFFFFFFFu); return; } 57 const unsigned char *z = sqlite3_column_text(st, i); 58 int n = sqlite3_column_bytes(st, i); 59 put32(f, (uint32_t)n); fwrite(z, 1, n, f); 60 } 61 62 static int mode_plan(const char *dbp){ 63 sqlite3 *db = opendb(dbp); 64 const char *v[2] = { SEARCH_SQL, 0 }; 65 char buf[4096]; 66 snprintf(buf, sizeof buf, "EXPLAIN QUERY PLAN %s", SEARCH_SQL); 67 sqlite3_stmt *st; 68 if( sqlite3_prepare_v2(db, buf, -1, &st, 0) ) die("prepare plan", db); 69 sqlite3_bind_text(st, 1, "memory", -1, SQLITE_STATIC); 70 sqlite3_bind_text(st, 2, "aien-lessons", -1, SQLITE_STATIC); 71 sqlite3_bind_int64(st, 3, 12); 72 while( sqlite3_step(st)==SQLITE_ROW ){ 73 printf("%d %d %s\n", sqlite3_column_int(st,0), sqlite3_column_int(st,1), 74 (const char*)sqlite3_column_text(st,3)); 75 } 76 sqlite3_finalize(st); 77 (void)v; 78 printf("sqlite_version %s source_id %s\n", sqlite3_libversion(), sqlite3_sourceid()); 79 sqlite3_close(db); 80 return 0; 81 } 82 83 static int mode_dump(const char *dbp, const char *out){ 84 sqlite3 *db = opendb(dbp); 85 FILE *f = wopen(out); 86 sqlite3_stmt *st; 87 fwrite("FTSDUMP1", 1, 8, f); 88 if( sqlite3_prepare_v2(db, "SELECT count(*) FROM entities_fts", -1, &st, 0) ) die("count", db); 89 sqlite3_step(st); put32(f, (uint32_t)sqlite3_column_int64(st, 0)); sqlite3_finalize(st); 90 if( sqlite3_prepare_v2(db, 91 "SELECT f.rowid, f.id, f.canonical_name, f.content, f.space_slug, e.space_slug, e.retracted " 92 "FROM entities_fts f LEFT JOIN entities e ON e.id = f.id ORDER BY f.rowid", -1, &st, 0) ) die("fts", db); 93 while( sqlite3_step(st)==SQLITE_ROW ){ 94 put64(f, (uint64_t)sqlite3_column_int64(st, 0)); 95 putstr(f, st, 1); putstr(f, st, 2); putstr(f, st, 3); putstr(f, st, 4); putstr(f, st, 5); 96 unsigned char r = sqlite3_column_type(st,6)==SQLITE_NULL ? 2 : (unsigned char)(sqlite3_column_int64(st,6)!=0); 97 fwrite(&r, 1, 1, f); 98 } 99 sqlite3_finalize(st); 100 if( sqlite3_prepare_v2(db, "SELECT count(*) FROM entities", -1, &st, 0) ) die("count2", db); 101 sqlite3_step(st); put32(f, (uint32_t)sqlite3_column_int64(st, 0)); sqlite3_finalize(st); 102 if( sqlite3_prepare_v2(db, "SELECT rowid, canonical_name, content FROM entities ORDER BY rowid", -1, &st, 0) ) die("ent", db); 103 while( sqlite3_step(st)==SQLITE_ROW ){ 104 put64(f, (uint64_t)sqlite3_column_int64(st, 0)); 105 putstr(f, st, 1); putstr(f, st, 2); 106 } 107 sqlite3_finalize(st); 108 fclose(f); 109 sqlite3_close(db); 110 return 0; 111 } 112 113 /* ---- tokenizer via the fts5_api (the exact unicode61 code linked into SQLite) ---- */ 114 static fts5_api *get_api(sqlite3 *db){ 115 fts5_api *p = 0; 116 sqlite3_stmt *st; 117 if( sqlite3_prepare_v2(db, "SELECT fts5(?1)", -1, &st, 0) ) die("fts5()", db); 118 sqlite3_bind_pointer(st, 1, (void*)&p, "fts5_api_ptr", 0); 119 sqlite3_step(st); 120 sqlite3_finalize(st); 121 if( !p ) die("no fts5 api", db); 122 return p; 123 } 124 static FILE *gTokOut; 125 static int tokcb(void *ctx, int tflags, const char *pTok, int nTok, int s, int e){ 126 (void)ctx; (void)tflags; 127 fprintf(gTokOut, " %d:%d:", s, e); 128 if( nTok>32768 ) nTok = 32768; /* FTS5_MAX_TOKEN_SIZE: what the index and queries keep */ 129 for(int i=0;ixFindTokenizer(api, "unicode61", &ud, &tk) ) die("find tok", db); 137 if( tk.xCreate(ud, 0, 0, &t) ) die("create tok", db); 138 gTokOut = wopen(out); 139 sqlite3_stmt *st; 140 if( sqlite3_prepare_v2(db, "SELECT rowid, canonical_name, content FROM entities_fts ORDER BY rowid", -1, &st, 0) ) die("tok sel", db); 141 while( sqlite3_step(st)==SQLITE_ROW ){ 142 for(int c=1;c<=2;c++){ 143 const char *z = (const char*)sqlite3_column_text(st, c); 144 int n = sqlite3_column_bytes(st, c); 145 fprintf(gTokOut, "%lld c%d", (long long)sqlite3_column_int64(st,0), c); 146 if( z ) tk.xTokenize(t, 0, FTS5_TOKENIZE_DOCUMENT, z, n, tokcb); 147 fprintf(gTokOut, "\n"); 148 } 149 } 150 sqlite3_finalize(st); 151 /* docsize cross-check: sum of tokens recorded by fts5 itself */ 152 tk.xDelete(t); 153 fclose(gTokOut); 154 sqlite3_close(db); 155 return 0; 156 } 157 158 /* copy SRC to DST, then replay cortex-rs style FTS updates on DST: for FTS row number i (rowid 159 ** order) with i%3==0 "DELETE FROM entities_fts WHERE id=?" and, when also i%2==0, re-INSERT the 160 ** same values (new rowid). Writes "D i" / "P i rowid" lines to OPS. */ 161 static int mode_mutate(const char *src, const char *dst, const char *ops){ 162 sqlite3 *s = opendb(src), *d = 0; 163 if( sqlite3_open(dst, &d) ) die("open dst", d); 164 sqlite3_backup *b = sqlite3_backup_init(d, "main", s, "main"); 165 if( !b ) die("backup", d); 166 sqlite3_backup_step(b, -1); sqlite3_backup_finish(b); 167 sqlite3_close(s); 168 FILE *o = wopen(ops); 169 sqlite3_stmt *sel, *del, *ins; 170 if( sqlite3_prepare_v2(d, "SELECT id, canonical_name, content, space_slug FROM entities_fts ORDER BY rowid", -1, &sel, 0) ) die("sel", d); 171 if( sqlite3_prepare_v2(d, "DELETE FROM entities_fts WHERE id = ?1", -1, &del, 0) ) die("del", d); 172 if( sqlite3_prepare_v2(d, "INSERT INTO entities_fts (id, canonical_name, content, space_slug) VALUES (?1, ?2, ?3, ?4)", -1, &ins, 0) ) die("ins", d); 173 int n = 0, cap = 8192; 174 char **v = malloc(sizeof(char*)*4*cap); int *ln = malloc(sizeof(int)*4*cap); 175 while( sqlite3_step(sel)==SQLITE_ROW ){ 176 for(int c=0;c<4;c++){ int k = sqlite3_column_bytes(sel,c); v[n*4+c] = malloc(k+1); memcpy(v[n*4+c], sqlite3_column_text(sel,c), k); ln[n*4+c] = k; } 177 n++; 178 } 179 sqlite3_finalize(sel); 180 sqlite3_exec(d, "BEGIN", 0, 0, 0); 181 for(int i=0;ixFindTokenizer(api, "unicode61", &ud, &tk) ) die("find tok", db); 205 if( tk.xCreate(ud, 0, 0, &t) ) die("create tok", db); 206 long n; unsigned char *b = slurp(in, &n); 207 gTokOut = wopen(out); 208 long p = 0; long r = 0; 209 while( p + 4 <= n ){ 210 uint32_t len; memcpy(&len, b+p, 4); p += 4; 211 fprintf(gTokOut, "%ld c2", r); 212 tk.xTokenize(t, 0, FTS5_TOKENIZE_DOCUMENT, (const char*)b+p, (int)len, tokcb); 213 fprintf(gTokOut, "\n"); 214 p += len; r++; 215 } 216 fclose(gTokOut); 217 return 0; 218 } 219 220 /* ---- query / like input: records of u32 len + bytes ---- */ 221 static unsigned char *slurp(const char *p, long *pn){ 222 FILE *f = fopen(p, "rb"); if(!f){ perror(p); exit(2); } 223 fseek(f, 0, SEEK_END); long n = ftell(f); fseek(f, 0, SEEK_SET); 224 unsigned char *b = malloc(n+1); if( fread(b,1,n,f)!=(size_t)n ) exit(2); fclose(f); 225 *pn = n; return b; 226 } 227 228 static int mode_query(const char *dbp, const char *qf, const char *out){ 229 sqlite3 *db = opendb(dbp); 230 long n; unsigned char *b = slurp(qf, &n); 231 FILE *o = wopen(out); 232 sqlite3_stmt *st; 233 char sql[4096]; 234 snprintf(sql, sizeof sql, "%s", SEARCH_SQL); 235 /* append f.rowid is NOT done: we map e.id -> fts rowid through a second lookup so the 236 ** SQL text (and plan) is byte-identical to cortex-rs. */ 237 if( sqlite3_prepare_v2(db, sql, -1, &st, 0) ) die("prepare search", db); 238 sqlite3_stmt *rid; 239 if( sqlite3_prepare_v2(db, "SELECT rowid FROM entities_fts WHERE id = ?1", -1, &rid, 0) ) die("prep rid", db); 240 static const char *spaces[] = { "aien-lessons", "aien-sources", "atlas-memory", "survey-test-mask", "nope" }; 241 static const int limits[] = { 1, 5, 12, 50 }; 242 long p = 0; int qi = 0; 243 while( p + 4 <= n ){ 244 uint32_t len; memcpy(&len, b+p, 4); p += 4; 245 for(int s=0;s<5;s++) for(int l=0;l<4;l++){ 246 sqlite3_reset(st); 247 sqlite3_bind_text(st, 1, (const char*)b+p, (int)len, SQLITE_TRANSIENT); 248 sqlite3_bind_text(st, 2, spaces[s], -1, SQLITE_STATIC); 249 sqlite3_bind_int64(st, 3, limits[l]); 250 fprintf(o, "q%d s%d l%d", qi, s, limits[l]); 251 int rc, cnt = 0; 252 char line[200000]; int lp = 0; 253 while( (rc = sqlite3_step(st))==SQLITE_ROW ){ 254 double d = sqlite3_column_double(st, 14); 255 uint64_t bits; memcpy(&bits, &d, 8); 256 sqlite3_reset(rid); 257 sqlite3_bind_text(rid, 1, (const char*)sqlite3_column_text(st,0), sqlite3_column_bytes(st,0), SQLITE_TRANSIENT); 258 long long r = -1; 259 if( sqlite3_step(rid)==SQLITE_ROW ) r = sqlite3_column_int64(rid, 0); 260 if( lp < 190000 ) lp += snprintf(line+lp, sizeof(line)-lp, " %lld:%016llx", r, (unsigned long long)bits); 261 cnt++; 262 } 263 line[lp] = 0; 264 fprintf(o, " %s n=%d%s\n", rc==SQLITE_DONE ? "ok" : "err", cnt, line); 265 } 266 p += len; qi++; 267 } 268 fclose(o); 269 sqlite3_finalize(st); sqlite3_finalize(rid); 270 sqlite3_close(db); 271 return 0; 272 } 273 274 static int mode_like(const char *dbp, const char *pf, const char *out){ 275 sqlite3 *db = opendb(dbp); 276 long n; unsigned char *b = slurp(pf, &n); 277 FILE *o = wopen(out); 278 sqlite3_stmt *cntst, *st; 279 if( sqlite3_prepare_v2(db, "SELECT count(*) FROM entities", -1, &cntst, 0) ) die("c", db); 280 sqlite3_step(cntst); int nent = sqlite3_column_int(cntst, 0); sqlite3_finalize(cntst); 281 /* rowid -> index map */ 282 sqlite3_stmt *rs; long long *rows = malloc(sizeof(long long)*nent); int k = 0; 283 sqlite3_prepare_v2(db, "SELECT rowid FROM entities ORDER BY rowid", -1, &rs, 0); 284 while( sqlite3_step(rs)==SQLITE_ROW ) rows[k++] = sqlite3_column_int64(rs, 0); 285 sqlite3_finalize(rs); 286 if( sqlite3_prepare_v2(db, "SELECT rowid FROM entities WHERE canonical_name LIKE ?1 OR content LIKE ?1 ORDER BY rowid", -1, &st, 0) ) die("like", db); 287 unsigned char *bm = malloc((nent+7)/8); 288 long p = 0; int pi = 0; 289 while( p + 4 <= n ){ 290 uint32_t len; memcpy(&len, b+p, 4); p += 4; 291 sqlite3_reset(st); 292 sqlite3_bind_text(st, 1, (const char*)b+p, (int)len, SQLITE_TRANSIENT); 293 memset(bm, 0, (nent+7)/8); 294 int rc, cnt = 0, j = 0; 295 while( (rc = sqlite3_step(st))==SQLITE_ROW ){ 296 long long r = sqlite3_column_int64(st, 0); 297 while( j>3] |= (unsigned char)(1<<(j&7)); } 299 cnt++; 300 } 301 fprintf(o, "p%d %s n=%d ", pi, rc==SQLITE_DONE ? "ok" : "err", cnt); 302 for(int i=0;i<(nent+7)/8;i++) fprintf(o, "%02x", bm[i]); 303 fprintf(o, "\n"); 304 p += len; pi++; 305 } 306 fclose(o); 307 sqlite3_finalize(st); 308 sqlite3_close(db); 309 return 0; 310 } 311 312 /* glibc log over: every IDF argument for nRow in [lo,hi] (all nHit), then random bit 313 ** patterns, normals, [0.5,2), subnormals and near-1 values up to `target` cases, then 314 ** special values. Output: binary f64 input, f64 output, repeated. */ 315 static uint64_t rng = 0x9E3779B97F4A7C15ull; 316 static uint64_t xr(void){ rng ^= rng<<13; rng ^= rng>>7; rng ^= rng<<17; return rng; } 317 static int mode_log(const char *out, long target, long lo, long hi){ 318 FILE *o = wopen(out); 319 long cnt = 0; 320 for(long nRow=lo; nRow<=hi; nRow++){ 321 for(long nHit=0; nHit<=nRow; nHit++){ 322 double x = ((double)(nRow - nHit) + 0.5) / ((double)nHit + 0.5); 323 double y = log(x); 324 fwrite(&x, 8, 1, o); fwrite(&y, 8, 1, o); cnt++; 325 } 326 } 327 long base = cnt; 328 while( cnt < base + target ){ 329 uint64_t u = xr(); 330 double x; 331 switch( cnt % 4 ){ 332 case 0: memcpy(&x, &u, 8); break; /* any pattern */ 333 case 1: u = (u & 0x000FFFFFFFFFFFFFull) | 0x3FE0000000000000ull | ((u>>52)&1ull)<<52; memcpy(&x,&u,8); break; /* [0.5,2) */ 334 case 2: u = (u & 0x000FFFFFFFFFFFFFull) | ((uint64_t)(1 + (u>>53)%2046) << 52); memcpy(&x,&u,8); break; /* positive normal */ 335 default: u = (u & 0x000FFFFFFFFFFFFFull) | (((u>>52)&1ull)<<63) ; if( (cnt/4)%3==0 ) u = 0x3FF0000000000000ull + (int64_t)((int)(u%20001) - 10000); memcpy(&x,&u,8); break; /* subnormal / near 1 */ 336 } 337 double y = log(x); 338 fwrite(&x, 8, 1, o); fwrite(&y, 8, 1, o); cnt++; 339 } 340 /* specials */ 341 double sp[] = { 0.0, -0.0, 1.0, -1.0, INFINITY, -INFINITY, NAN, -NAN, 4.`9406564584124654e`-324, 1.`7976931348623157e308` }; 342 for(int i=0;i<10;i++){ double y = log(sp[i]); fwrite(&sp[i],8,1,o); fwrite(&y,8,1,o); cnt++; } 343 fclose(o); 344 fprintf(stderr, "log cases %ld\n", cnt); 345 return 0; 346 } 347 348 /* hardware fma reference for the Omega soft-fma: input u64 a,b,c -> fma bits */ 349 static int mode_fma(const char *out, long target){ 350 FILE *o = wopen(out); 351 for(long i=0;i=3 && !strcmp(argv[1],"plan") ) return mode_plan(argv[2]); 378 if( argc>=4 && !strcmp(argv[1],"dump") ) return mode_dump(argv[2], argv[3]); 379 if( argc>=5 && !strcmp(argv[1],"mutate") ) return mode_mutate(argv[2], argv[3], argv[4]); 380 if( argc>=4 && !strcmp(argv[1],"tokf") ) return mode_tokf(argv[2], argv[3]); 381 if( argc>=4 && !strcmp(argv[1],"tok") ) return mode_tok(argv[2], argv[3]); 382 if( argc>=5 && !strcmp(argv[1],"query") ) return mode_query(argv[2], argv[3], argv[4]); 383 if( argc>=5 && !strcmp(argv[1],"like") ) return mode_like(argv[2], argv[3], argv[4]); 384 if( argc>=6 && !strcmp(argv[1],"log") ) return mode_log(argv[2], atol(argv[3]), atol(argv[4]), atol(argv[5])); 385 if( argc>=4 && !strcmp(argv[1],"fma") ) return mode_fma(argv[2], atol(argv[3])); 386 fprintf(stderr, "usage: oracle plan|dump|tok|query|like|log|fma ...\n"); 387 return 2; 388 }