3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #***********************************************************************
14 set testdir [file dirname $argv0]
15 source $testdir/tester.tcl
16 set testprefix bestindexD
23 register_tcl_module db
25 proc vtab_command {method args} {
28 return "CREATE TABLE t1(a PRIMARY KEY, b, c) WITHOUT ROWID"
32 set hdl [lindex $args 0]
33 set ::colUsed [$hdl mask]
39 foreach c [$hdl constraints] {
40 set cost [expr $cost/10]
41 append used " use $cons"
45 return "cost $cost rows $cost $used"
53 CREATE VIRTUAL TABLE x1 USING tcl(vtab_command);
55 CREATE TABLE t2(a, b);
58 # This proc assumes that there is only one use of a virtual table - x1 -
59 # in SQL statement $sql. It tests that the colUsed value passed to the
60 # xBestIndex method matches the actual columns used, which is ascertained
61 # by searching the compiled VM code for VColumn instructions.
63 proc do_colsused_test {tn sql} {
69 db eval "EXPLAIN $sql" x {
70 if {$x(opcode)=="VColumn"} {
71 set expect [expr $expect | (1<<$x(p2))]
75 uplevel [list do_test $tn.($expect/$got) [list expr ($expect & $got)] $expect]
78 do_colsused_test 1.1 { SELECT a FROM x1 }
79 do_colsused_test 1.2 { SELECT a,c FROM x1 }
80 do_colsused_test 1.3 { SELECT b FROM x1 }
81 do_colsused_test 1.4 { SELECT b FROM x1 WHERE c=? }
83 do_colsused_test 1.5 {
84 select 1 from t2 full join x1;
87 do_colsused_test 1.6 {
88 select 1 from x1 WHERE (b=? AND c=?) OR (b=? AND c=?)