sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
sqlite / ext / vec1 / tool / vec1bench_addcols.tcl
| 1 | |
| 2 | # |
| 3 | # This script edits a vec1bench database to add constant dimensions to |
| 4 | # the data, query and (if any) training vectors in the db. This should |
| 5 | # not change the results of any query, but did at one point confound |
| 6 | # the OPQ training code. |
| 7 | # |
| 8 | |
| 9 | package require sqlite3 |
| 10 | |
| 11 | set DEFAULT(--ncol) 1 |
| 12 | set DEFAULT(--zero) 0 |
| 13 | |
| 14 | proc usage {} { |
| 15 | global DEFAULT |
| 16 | puts stderr "usage: $::argv0 ?OPTIONS? DATABASE" |
| 17 | puts stderr "options:" |
| 18 | foreach o [array names DEFAULT] { |
| 19 | set display $DEFAULT($o) |
| 20 | if { [string is integer $display]==0 } { set display "\"$display\"" } |
| 21 | puts stderr " [format {% -20s} $o](default: $display)" |
| 22 | } |
| 23 | exit 1 |
| 24 | } |
| 25 | |
| 26 | proc process_args {lArg} { |
| 27 | global C |
| 28 | global DEFAULT |
| 29 | array set C [array get DEFAULT] |
| 30 | |
| 31 | for {set i 0} {$i < [expr [llength $lArg]-1]} {incr i} { |
| 32 | set a [lindex $lArg $i] |
| 33 | if {[string range $a 0 0]!="-"} break |
| 34 | if {[string range $a 1 1]!="-"} { set a "-$a" } |
| 35 | |
| 36 | unset -nocomplain m |
| 37 | foreach o [array names C] { |
| 38 | if {[string match ${a}* $o]} { |
| 39 | if {[info exists m]} usage |
| 40 | set m $o |
| 41 | } |
| 42 | } |
| 43 | if {[info exists m]==0} usage |
| 44 | |
| 45 | incr i |
| 46 | set C($m) [lindex $lArg $i] |
| 47 | } |
| 48 | |
| 49 | lrange $lArg $i end |
| 50 | } |
| 51 | |
| 52 | |
| 53 | # Check that the __meta__ table looks ok. This command checks that there |
| 54 | # are "train", "test", "neighbors" and "distances" tables, with types |
| 55 | # float32, float32, int64 and float64, respectively, in the db. If not, |
| 56 | # it prints out and error and exits. |
| 57 | # |
| 58 | proc check_db_looks_ok {} { |
| 59 | db eval { |
| 60 | WITH expect(nm, tp) AS ( |
| 61 | SELECT 'train', 'float32' UNION ALL |
| 62 | SELECT 'test', 'float32' UNION ALL |
| 63 | SELECT 'neighbors', 'int64' UNION ALL |
| 64 | SELECT 'neighbors', 'int32' |
| 65 | ), |
| 66 | have(nm, tp) AS ( |
| 67 | SELECT table_name, dtype |
| 68 | FROM __meta__ |
| 69 | WHERE table_name IN ('train', 'test', 'neighbors') |
| 70 | ) |
| 71 | |
| 72 | SELECT (count(*)==3) as 'ok' FROM have WHERE (nm, tp) IN ( |
| 73 | SELECT nm, tp FROM expect |
| 74 | ); |
| 75 | } { |
| 76 | if {$ok==0} { |
| 77 | puts stderr "database does not look right. bailing out..." |
| 78 | exit -1 |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | |
| 84 | # Process command line arguments and open the database. And load the |
| 85 | # vec1.so dynamic extension(s). |
| 86 | # |
| 87 | set lExt [process_args $argv] |
| 88 | if {[llength $lExt]!=1} usage |
| 89 | set C(dbname) [lindex $lExt 0] |
| 90 | |
| 91 | proc main { } { |
| 92 | global C |
| 93 | |
| 94 | sqlite3 db $C(dbname) |
| 95 | |
| 96 | check_db_looks_ok |
| 97 | |
| 98 | set nCol $C(--ncol) |
| 99 | set zstr [string repeat 00000000 $nCol] |
| 100 | |
| 101 | db transaction { |
| 102 | foreach tbl {test train learn} { |
| 103 | if {$C(--zero)} { |
| 104 | catch { db eval "UPDATE $tbl SET vec = unhex( hex(vec) || '$zstr')" } |
| 105 | } else { |
| 106 | catch { db eval " |
| 107 | UPDATE $tbl SET vec = unhex( hex(vec) || substr(hex(vec), 1, 8*$nCol) ) |
| 108 | " } |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | if {$C(--zero)} { |
| 113 | puts "Appended $nCol 0.0 floats to all vectors" |
| 114 | } else { |
| 115 | puts "Appended duplicate of first $nCol floats to all vectors" |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | main |
| 120 | exit |
| 121 |