Testrunner.tcl enhancements: (1) Attempt to build the SQLite tcl extension
[sqlite.git] / test / bestindexD.test
blobb06d6b4270da05d39bf6672d40557b12876b831a
1 # 2024-08-03
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 #***********************************************************************
11
14 set testdir [file dirname $argv0]
15 source $testdir/tester.tcl
16 set testprefix bestindexD
18 ifcapable !vtab {
19   finish_test
20   return
23 register_tcl_module db
25 proc vtab_command {method args} {
26   switch -- $method {
27     xConnect {
28       return "CREATE TABLE t1(a PRIMARY KEY, b, c) WITHOUT ROWID"
29     }
31     xBestIndex {
32       set hdl [lindex $args 0]
33       set ::colUsed [$hdl mask]
35       set cost 1000000
36       set used ""
38       set cons 0
39       foreach c [$hdl constraints] {
40         set cost [expr $cost/10]
41         append used " use $cons"
42         incr cons
43       }
45       return "cost $cost rows $cost $used"
46     }
47   }
49   return {}
52 do_execsql_test 1.0 {
53   CREATE VIRTUAL TABLE x1 USING tcl(vtab_command);
55   CREATE TABLE t2(a, b);
56 } {}
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} {
64   set ::colUsed ""
65   execsql $sql
66   set got $::colUsed
68   set expect 0
69   db eval "EXPLAIN $sql" x {
70     if {$x(opcode)=="VColumn"} {
71       set expect [expr $expect | (1<<$x(p2))]
72     }
73   }
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=?)
91 finish_test