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 # Test the virtual table interface. In particular the xBestIndex
15 set testdir [file dirname $argv0]
16 source $testdir/tester.tcl
17 set testprefix bestindex4
24 #-------------------------------------------------------------------------
25 # Virtual table callback for a virtual table named $tbl.
27 # The table created is:
29 # "CREATE TABLE t1 (id, host, class)"
31 # The virtual table supports == operators on a subset of its columns. The
32 # exact subset depends on the value of bitmask paramater $param.
34 # 0x01 - == on "id" supported
35 # 0x02 - == on "host" supported
36 # 0x04 - == on "class" supported
38 # $param also supports the following bits:
40 # 0x08 - ignore the "usable" flag (malfunction)
44 proc vtab_cmd {param method args} {
47 return "CREATE TABLE t1(id TEXT, host TEXT, class TEXT)"
51 set hdl [lindex $args 0]
52 set clist [$hdl constraints]
53 set orderby [$hdl orderby]
61 for {set i 0} {$i < [llength $clist]} {incr i} {
63 array set C [lindex $clist $i]
64 if { ($C(usable) || ($param & 0x08))
65 && $C(op)=="eq" && ($param & 1<<$C(column))
74 set score [expr $score / [llength $ret]]
76 lappend ret cost $score rows $score
87 register_tcl_module db
89 for {set param1 0} {$param1<16} {incr param1} {
90 for {set param2 0} {$param2<16} {incr param2} {
92 register_tcl_module db
93 do_execsql_test 1.$param1.$param2.1 "
94 CREATE VIRTUAL TABLE t1 USING tcl('vtab_cmd $param1');
95 CREATE VIRTUAL TABLE t2 USING tcl('vtab_cmd $param2');
99 2 "select t1.id as ID from t1, t2 where t1.id=t2.host and t2.class='xx'"
101 select t1.id as ID from t1, t2 where t2.class ='xx' and t2.id = t1.host
104 select t1.id as ID from t1, t2 where t1.host = t2.id and t2. class ='xx'
108 if {($param1 & 0x08)==0 && ($param2 & 0x08)==0} {
110 do_execsql_test 1.$param1.$param2.$tn.a $sql {}
113 do_test 1.$param1.$param2.$tn.b {
123 #-------------------------------------------------------------------------
124 # Test that a parameter passed to a table-valued function cannot be
125 # used to drive an index. i.e. that in the following:
127 # SELECT * FROM tbl, vtab(tbl.x);
129 # The implicit constraint "tbl.x = vtab.hidden" is not optimized using
133 register_tcl_module db
134 proc vtab_command {method args} {
137 return "CREATE TABLE t1(a, b, c, d HIDDEN)"
141 set hdl [lindex $args 0]
142 set clist [$hdl constraints]
143 set orderby [$hdl orderby]
146 if {[llength $clist]!=1} { error "unexpected constraint list" }
147 catch { array unset C }
148 array set C [lindex $clist 0]
150 return [list omit 0 idxnum 555 rows 10 cost 100]
152 return [list cost 100000000]
160 do_execsql_test 2.0 {
161 CREATE VIRTUAL TABLE x1 USING tcl(vtab_command);
162 CREATE TABLE t1 (x INT PRIMARY KEY);
166 SELECT * FROM t1, x1 WHERE x1.d=t1.x;
169 |--SCAN x1 VIRTUAL TABLE INDEX 0:
170 `--SEARCH t1 USING COVERING INDEX sqlite_autoindex_t1_1 (x=?)
174 SELECT * FROM t1, x1(t1.x)
178 `--SCAN x1 VIRTUAL TABLE INDEX 555: