interplane.Get Omega
Read in your language

cortex

aien-dev/cortex

Brought by drakestapleton

branch main Branches Tags History

cortex / tests / fts / run.sh

206 lines · 13756 bytes Raw Blame History
1#!/bin/sh
2# tests/fts/run.sh : differential tests of om/uni_fts.om, om/flog.om, om/fts.om, om/like.om
3# against SQLite 3.45.0 (sqlite3.c of libsqlite3-sys 0.28.0, built with the -D flags of its
4# build.rs and gcc -O3 like the cc crate inside cortex-rs) and glibc libm, plus negative controls.
5#
6# usage: sh tests/fts/run.sh DB DB: a copy of a cortex-rs database (opened read only)
7# env: OMC (pinned compiler), SQLITE_SRC (dir with sqlite3.c/sqlite3.h), WORK (scratch dir),
8# FTS_EVIDENCE (log dir, default evidence/fts; check.sh points it into $CHECK_DIR)
9# exit 0 only when every differential is bit-identical AND every negative control is detected.
10set -u
11HERE=$(cd "$(dirname "$0")" && pwd)
12ROOT=$(cd "$HERE/../.." && pwd)
13DB=${1:?usage: run.sh DB}
14OMC=$(sh "$ROOT/tools/omc.sh") || exit 2
15SQLITE_SRC=${SQLITE_SRC:-$HOME/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/libsqlite3-sys-0.28.0/sqlite3}
16WORK=${WORK:-$ROOT/build/fts-work}
17EV=${FTS_EVIDENCE:-$ROOT/evidence/fts}
18N="nice -n 19 ionice -c3"
19mkdir -p "$WORK" "$EV"
20LOG="$EV/run.log"
21: > "$LOG"
22say() { printf '%s\n' "$*" | tee -a "$LOG"; }
23fail=0
24check() { # name, expect(0=same,1=differ), file_a, file_b
25 if cmp -s "$3" "$4"; then r=same; else r=differ; fi
26 if { [ "$2" = 0 ] && [ $r = same ]; } || { [ "$2" = 1 ] && [ $r = differ ]; }; then
27 say "PASS $1 ($r)"
28 else
29 say "FAIL $1 ($r)"; fail=1
30 fi
31}
32say "date $(date -u +%FT%TZ) host $(uname -m)"
33say "omc $(sha256sum "$OMC" | cut -c1-64)"
34say "sqlite3.c $(sha256sum "$SQLITE_SRC/sqlite3.c" | cut -c1-64)"
35say "libm $(sha256sum "$(readlink -f /lib/aarch64-linux-gnu/libm.so.6)" | cut -c1-64)"
36say "db $(sha256sum "$DB" | cut -c1-64)"
37say "gcc $(gcc --version | head -1)"
38for f in uni_fts flog fts like; do say "om/$f.om $(sha256sum "$ROOT/om/$f.om" | cut -c1-64) fns=$(grep -c '^fn ' "$ROOT/om/$f.om")"; done
39
40# ---- oracle ----
41FLAGS="-DSQLITE_CORE -DSQLITE_DEFAULT_FOREIGN_KEYS=1 -DSQLITE_ENABLE_API_ARMOR -DSQLITE_ENABLE_COLUMN_METADATA -DSQLITE_ENABLE_DBSTAT_VTAB -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_FTS5 -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_LOAD_EXTENSION=1 -DSQLITE_ENABLE_MEMORY_MANAGEMENT -DSQLITE_ENABLE_RTREE -DSQLITE_ENABLE_STAT2 -DSQLITE_ENABLE_STAT4 -DSQLITE_SOUNDEX -DSQLITE_THREADSAFE=1 -DSQLITE_USE_URI -DHAVE_USLEEP=1 -D_POSIX_THREAD_SAFE_FUNCTIONS -DHAVE_ISNAN -DHAVE_LOCALTIME_R"
42if [ ! -x "$WORK/oracle" ] || [ "$HERE/oracle.c" -nt "$WORK/oracle" ]; then
43 [ -f "$WORK/sqlite3.o" ] || $N gcc -O3 -fPIC -ffunction-sections -fdata-sections $FLAGS -I"$SQLITE_SRC" -c "$SQLITE_SRC/sqlite3.c" -o "$WORK/sqlite3.o" || exit 2
44 $N gcc -O2 -I"$SQLITE_SRC" -c "$HERE/oracle.c" -o "$WORK/oracle.o" && gcc -o "$WORK/oracle" "$WORK/oracle.o" "$WORK/sqlite3.o" -lm -lpthread -ldl || exit 2
45fi
46O="$WORK/oracle"
47$O plan "$DB" | tee -a "$LOG"
48
49# ---- build harnesses (library files concatenated in build order + harness) ----
50build() { # name, extra files...
51 name=$1; shift
52 cat "$@" > "$WORK/$name.om"
53 $N "$OMC" < "$WORK/$name.om" > "$WORK/$name" 2> "$WORK/$name.err"; rc=$?
54 if [ $rc -ne 0 ] || [ -s "$WORK/$name.err" ]; then say "FAIL compile $name rc=$rc $(cat "$WORK/$name.err")"; exit 3; fi
55 chmod +x "$WORK/$name"
56}
57LIB="$ROOT/om/uni_fts.om $ROOT/om/flog.om $ROOT/om/fts.om $ROOT/om/like.om $HERE/tlib.om $HERE/tdump.om"
58build t_flog $LIB "$HERE/t_flog.om"
59build t_tok $LIB "$HERE/t_tok.om"
60build t_query $LIB "$HERE/t_query.om"
61build t_like $LIB "$HERE/t_like.om"
62say "compiled: t_flog t_tok t_query t_like"
63
64# ---- inputs ----
65$O dump "$DB" "$WORK/dump.bin"
66$O tok "$DB" "$WORK/tok.txt"
67python3 "$HERE/gen_tokf.py" "$WORK/tokf.bin" >> "$LOG"
68$O tokf "$WORK/tokf.bin" "$WORK/tokf.txt"
69$O log "$WORK/log.bin" 1000000 3000 4200 2>> "$LOG"
70$O fma "$WORK/fma.bin" 2000000
71python3 "$HERE/gen_queries.py" "$DB" "$WORK/q1.bin" 2400 20261026 >> "$LOG"
72python3 "$HERE/gen_queries.py" "$DB" "$WORK/q2.bin" 3000 99 >> "$LOG"
73python3 "$HERE/gen_like.py" "$DB" "$WORK/like.bin" 600 >> "$LOG"
74rm -f "$WORK/mut.db"; $O mutate "$DB" "$WORK/mut.db" "$WORK/ops.txt"
75say "ops $(grep -c '^D' "$WORK/ops.txt") deletes $(grep -c '^P' "$WORK/ops.txt") re-inserts"
76
77# ---- differentials ----
78if $N "$WORK/t_flog" l "$WORK/log.bin" > "$WORK/flog.txt"; then say "PASS flog vs glibc log: $(cat "$WORK/flog.txt")"; else say "FAIL flog vs glibc log: $(head -3 "$WORK/flog.txt")"; fail=1; fi
79if $N "$WORK/t_flog" f "$WORK/fma.bin" > "$WORK/fma.txt"; then say "PASS flog_fma vs FMADD: $(cat "$WORK/fma.txt")"; else say "FAIL flog_fma vs FMADD: $(head -3 "$WORK/fma.txt")"; fail=1; fi
80$N "$WORK/t_tok" "$WORK/dump.bin" > "$WORK/tok_om.txt"; check "tokenizer, all entities_fts rows" 0 "$WORK/tok_om.txt" "$WORK/tok.txt"
81$N "$WORK/t_tok" "$WORK/tokf.bin" f > "$WORK/tokf_om.txt"; check "tokenizer, every code point + invalid UTF-8" 0 "$WORK/tokf_om.txt" "$WORK/tokf.txt"
82$O query "$DB" "$WORK/q1.bin" "$WORK/q1_or.txt"
83$N "$WORK/t_query" "$WORK/dump.bin" "$WORK/q1.bin" > "$WORK/q1_om.txt" 2> "$WORK/q1.time"
84check "search corpus 1 (2400 queries x 5 spaces x 4 limits)" 0 "$WORK/q1_om.txt" "$WORK/q1_or.txt"
85say "timing q1: $(cat "$WORK/q1.time")"
86$O query "$DB" "$WORK/q2.bin" "$WORK/q2_or.txt"
87$N "$WORK/t_query" "$WORK/dump.bin" "$WORK/q2.bin" > "$WORK/q2_om.txt" 2> "$WORK/q2.time"
88check "search corpus 2 (3000 queries x 5 spaces x 4 limits)" 0 "$WORK/q2_om.txt" "$WORK/q2_or.txt"
89say "timing q2: $(cat "$WORK/q2.time")"
90$O query "$WORK/mut.db" "$WORK/q1.bin" "$WORK/qm_or.txt"
91$N "$WORK/t_query" "$WORK/dump.bin" "$WORK/q1.bin" "$WORK/ops.txt" > "$WORK/qm_om.txt" 2> "$WORK/qm.time"
92check "search after delete/re-insert + token-pool compaction" 0 "$WORK/qm_om.txt" "$WORK/qm_or.txt"
93say "timing after delete/re-insert: $(cat "$WORK/qm.time")"
94# the same searches with the plan of fts_search forced (1 scan of the space's rows, 2 term lists,
95# 3 every row as SQLite runs it, 4 chosen but never the term-count loop that OR-of-terms queries
96# take), before and after the delete/re-insert ops: each must give the oracle's answers, and the
97# incremental index must equal a rebuild (else an icheck= line differs)
98for rt in 1 2 3 4; do
99 $N "$WORK/t_query" "$WORK/dump.bin" "$WORK/q1.bin" - $rt > "$WORK/q1_r$rt.txt" 2> "$WORK/q1_r$rt.time"
100 check "search corpus 1, plan $rt forced" 0 "$WORK/q1_r$rt.txt" "$WORK/q1_or.txt"
101 $N "$WORK/t_query" "$WORK/dump.bin" "$WORK/q1.bin" "$WORK/ops.txt" $rt > "$WORK/qm_r$rt.txt" 2> "$WORK/qm_r$rt.time"
102 check "search after delete/re-insert, plan $rt forced" 0 "$WORK/qm_r$rt.txt" "$WORK/qm_or.txt"
103done
104$O like "$DB" "$WORK/like.bin" "$WORK/like_or.txt"
105$N "$WORK/t_like" "$WORK/dump.bin" "$WORK/like.bin" > "$WORK/like_om.txt"
106check "LIKE (600 patterns x every entity)" 0 "$WORK/like_om.txt" "$WORK/like_or.txt"
107# latency: the 200 most frequent title words as single-term queries (x 5 spaces x 4 limits)
108python3 - "$DB" "$WORK/lat.bin" <<'EOF'
109import collections, re, sqlite3, struct, sys
110con = sqlite3.connect('file:%s?mode=ro' % sys.argv[1], uri=True)
111c = collections.Counter(w.lower() for (t,) in con.execute('SELECT canonical_name FROM entities_fts') for w in re.findall(r'[A-Za-z]{3,}', t))
112open(sys.argv[2], 'wb').write(b''.join(struct.pack('<I', len(w)) + w.encode() for w, _ in c.most_common(200)))
113EOF
114$O query "$DB" "$WORK/lat.bin" "$WORK/lat_or.txt"
115$N "$WORK/t_query" "$WORK/dump.bin" "$WORK/lat.bin" > "$WORK/lat_om.txt" 2> "$WORK/lat.time"
116check "single-term latency corpus (200 frequent words)" 0 "$WORK/lat_om.txt" "$WORK/lat_or.txt"
117say "timing single-term: $(cat "$WORK/lat.time")"
118python3 - "$WORK" >> "$LOG" <<'EOF'
119import sys, collections
120w = sys.argv[1]
121for name in ('q1', 'q2'):
122 L = open('%s/%s_or.txt' % (w, name), 'rb').read().split(b'\n')[:-1]
123 st = collections.Counter(l.split()[3] for l in L)
124 nz = sum(1 for l in L if not l.split()[4].endswith(b'=0'))
125 rows = sum(int(l.split()[4][2:]) for l in L)
126 ties = sum(1 for l in L if len(set(x.split(b':')[1] for x in l.split()[5:])) < len(l.split()[5:]))
127 print('%s lines=%d ok=%d err=%d nonempty=%d rows=%d lines_with_tied_bm25=%d' % (name, len(L), st[b'ok'], st[b'err'], nz, rows, ties))
128L = open('%s/like_or.txt' % w).read().split('\n')[:-1]
129print('like patterns=%d err=%d matches=%d' % (len(L), sum(1 for l in L if ' err ' in l), sum(int(l.split()[2][2:]) for l in L)))
130EOF
131
132# ---- negative controls: each mutant must make its differential FAIL ----
133mutant() { # name, file, python replace expression old new, harness, run command...
134 name=$1; file=$2; old=$3; new=$4; harness=$5; shift 5
135 mkdir -p "$WORK/mut"; cp "$ROOT/om/uni_fts.om" "$ROOT/om/flog.om" "$ROOT/om/fts.om" "$ROOT/om/like.om" "$WORK/mut/"
136 python3 - "$WORK/mut/$file" "$old" "$new" <<'EOF'
137import sys
138p, old, new = sys.argv[1:4]
139s = open(p).read()
140if old.startswith('@qutx:'):
141 # change one number of the unicode table text: @qutx:INDEX, new = replacement value
142 i = int(old[6:])
143 a = s.index('text qutx "') + len('text qutx "'); b = s.index('"', a)
144 v = s[a:b].split(' ')
145 assert v[i] != new, 'mutation is a no-op'
146 v[i] = new
147 s = s[:a] + ' '.join(v) + s[b:]
148else:
149 assert s.count(old) >= 1, 'mutation anchor not found: ' + old
150 s = s.replace(old, new, 1)
151open(p, 'w').write(s)
152EOF
153 cat "$WORK/mut/uni_fts.om" "$WORK/mut/flog.om" "$WORK/mut/fts.om" "$WORK/mut/like.om" "$HERE/tlib.om" "$HERE/tdump.om" "$HERE/$harness.om" > "$WORK/mut/h.om"
154 $N "$OMC" < "$WORK/mut/h.om" > "$WORK/mut/h" 2> "$WORK/mut/h.err" || { say "FAIL negative control $name: mutant did not compile"; fail=1; return; }
155 chmod +x "$WORK/mut/h"
156 "$@"
157}
158# 1. bm25 k1 = 1.3 instead of 1.2 (K uses k1, the numerator uses k1+1)
159mutant "bm25 k1=1.3" fts.om "kk = mul add kk 0.25 bitcast f64 ulong 4608083138725491507" "kk = mul add kk 0.25 1.3" t_query \
160 sh -c "$N '$WORK/mut/h' '$WORK/dump.bin' '$WORK/q1.bin' > '$WORK/mut/q.txt' 2>/dev/null"
161check "negative control bm25 k1=1.3 detected" 1 "$WORK/mut/q.txt" "$WORK/q1_or.txt"
162# 2. bm25 without the fused last term (plain multiply-add)
163mutant "bm25 unfused tail" fts.om "s = flog_fma t get qidf i s" "s = add s mul t get qidf i" t_query \
164 sh -c "$N '$WORK/mut/h' '$WORK/dump.bin' '$WORK/q1.bin' > '$WORK/mut/q.txt' 2>/dev/null"
165check "negative control unfused bm25 tail detected" 1 "$WORK/mut/q.txt" "$WORK/q1_or.txt"
166# 3. one tokenizer table entry: aiOff[1] (index QUAO+1 = 4037) 2 -> 3, a case-fold offset
167mutant "aiOff entry" uni_fts.om "@qutx:4037" "3" t_tok \
168 sh -c "$N '$WORK/mut/h' '$WORK/tokf.bin' f > '$WORK/mut/t.txt' 2>/dev/null"
169check "negative control tokenizer table entry detected" 1 "$WORK/mut/t.txt" "$WORK/tokf.txt"
170# 4. log table: ln2lo replaced by 0
171mutant "log ln2lo" flog.om "d = flog_fma flog_t 1 kd d" "d = flog_fma 0.0 kd d" t_flog \
172 sh -c "$N '$WORK/mut/h' l '$WORK/log.bin' > '$WORK/mut/l.txt'; echo rc=\$? >> '$WORK/mut/l.txt'"
173if grep -q 'mismatches=0' "$WORK/mut/l.txt"; then say "FAIL negative control flog ln2lo not detected"; fail=1; else say "PASS negative control flog ln2lo detected ($(head -1 "$WORK/mut/l.txt"))"; fi
174# 5. LIKE without ASCII case folding
175mutant "LIKE case" like.om "if not and and eq fts_lower c fts_lower c2 lt c 128 lt c2 128" "if true" t_like \
176 sh -c "$N '$WORK/mut/h' '$WORK/dump.bin' '$WORK/like.bin' > '$WORK/mut/k.txt' 2>/dev/null"
177check "negative control LIKE case folding detected" 1 "$WORK/mut/k.txt" "$WORK/like_or.txt"
178# 6. tie order: unstable insertion (ties before earlier rows)
179mutant "tie order" fts.om "if le get qrb sub p 1 b
180break" "if lt get qrb sub p 1 b
181break" t_query \
182 sh -c "$N '$WORK/mut/h' '$WORK/dump.bin' '$WORK/q1.bin' > '$WORK/mut/q.txt' 2>/dev/null"
183check "negative control unstable tie order detected" 1 "$WORK/mut/q.txt" "$WORK/q1_or.txt"
184# 7-10. the incremental index: each skipped update must change an answer or the icheck line
185mutant "qdf not counted" fts.om "put qdf t uint add ulong get qdf t 1" "put qdf t uint ulong get qdf t" t_query \
186 sh -c "$N '$WORK/mut/h' '$WORK/dump.bin' '$WORK/q1.bin' '$WORK/ops.txt' > '$WORK/mut/q.txt' 2>/dev/null"
187check "negative control index: qdf not counted on put detected" 1 "$WORK/mut/q.txt" "$WORK/qm_or.txt"
188mutant "term node not linked" fts.om "put qlh t uint add k 1" "put qlv 1 get qlv 1" t_query \
189 sh -c "$N '$WORK/mut/h' '$WORK/dump.bin' '$WORK/q1.bin' '$WORK/ops.txt' > '$WORK/mut/q.txt' 2>/dev/null"
190check "negative control index: term node not linked detected" 1 "$WORK/mut/q.txt" "$WORK/qm_or.txt"
191mutant "group count not kept" fts.om "put qgc g uint add ulong get qgc g 1" "put qgc g get qgc g" t_query \
192 sh -c "$N '$WORK/mut/h' '$WORK/dump.bin' '$WORK/q1.bin' '$WORK/ops.txt' > '$WORK/mut/q.txt' 2>/dev/null"
193check "negative control index: group count not kept detected" 1 "$WORK/mut/q.txt" "$WORK/qm_or.txt"
194mutant "generation not bumped" fts.om "put qgen doc uint g" "put qgen doc get qgen doc" t_query \
195 sh -c "$N '$WORK/mut/h' '$WORK/dump.bin' '$WORK/q1.bin' '$WORK/ops.txt' > '$WORK/mut/q.txt' 2>/dev/null"
196check "negative control index: generation not bumped on delete detected" 1 "$WORK/mut/q.txt" "$WORK/qm_or.txt"
197# 11-12. the term-count loop counting each term once per row, and the LIKE byte search without case folding
198mutant "term counted once" fts.om "put qotc k add v 1" "put qotc k 1" t_query \
199 sh -c "$N '$WORK/mut/h' '$WORK/dump.bin' '$WORK/q1.bin' > '$WORK/mut/q.txt' 2>/dev/null"
200check "negative control term-count loop counting once per row detected" 1 "$WORK/mut/q.txt" "$WORK/q1_or.txt"
201mutant "LIKE literal case" like.om "b = add b 32" "b = add b 0" t_like \
202 sh -c "$N '$WORK/mut/h' '$WORK/dump.bin' '$WORK/like.bin' > '$WORK/mut/k.txt' 2>/dev/null"
203check "negative control LIKE byte search without case folding detected" 1 "$WORK/mut/k.txt" "$WORK/like_or.txt"
204
205if [ $fail -eq 0 ]; then say "RESULT PASS"; else say "RESULT FAIL"; fi
206exit $fail