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 #***********************************************************************
12 set testdir [file dirname $argv0]
13 source $testdir/tester.tcl
14 set testprefix bestindex2
21 #-------------------------------------------------------------------------
22 # Virtual table callback for table named $tbl, with the columns specified
23 # by list argument $cols. e.g. if the function is invoked as:
25 # vtab_cmd t1 {a b c} ...
27 # The table created is:
29 # "CREATE TABLE t1 (a, b, c)"
31 # The tables xBestIndex method behaves as if all possible combinations of
32 # "=" constraints (but no others) may be optimized. The cost of a full table
35 # "WHERE 1" "cost 1000000 rows 1000000"
37 # If one or more "=" constraints are in use, the cost and estimated number
38 # of rows returned are both is (11 - nCons)*1000, where nCons is the number
39 # of constraints used. e.g.
41 # "WHERE a=? AND b=?" -> "cost 900 rows 900"
42 # "WHERE c=? AND b<?" -> "cost 1000 rows 1000"
44 proc vtab_cmd {tbl cols method args} {
47 return "CREATE TABLE $tbl ([join $cols ,])"
50 foreach {clist orderby mask} $args {}
55 for {set i 0} {$i < [llength $clist]} {incr i} {
57 array set C [lindex $clist $i]
58 if {$C(op)=="eq" && $C(usable) && [lsearch $cons $C(column)]<0} {
60 lappend cons $C(column)
64 set nCons [llength $cons]
66 return "cost 1000000 rows 1000000"
68 set cost [expr (11-$nCons) * 1000]
69 set ret [concat $used "cost $cost rows $cost"]
72 foreach c $cons { lappend txt "[lindex $cols $c]=?" }
73 lappend ret idxstr "indexed([join $txt { AND }])"
82 register_tcl_module db
85 CREATE VIRTUAL TABLE t1 USING tcl("vtab_cmd t1 {a b}");
86 CREATE VIRTUAL TABLE t2 USING tcl("vtab_cmd t2 {c d}");
87 CREATE VIRTUAL TABLE t3 USING tcl("vtab_cmd t3 {e f}");
91 SELECT * FROM t1 WHERE a='abc'
92 } {SCAN TABLE t1 VIRTUAL TABLE INDEX 0:indexed(a=?)}
95 SELECT * FROM t1 WHERE a='abc' AND b='def'
96 } {SCAN TABLE t1 VIRTUAL TABLE INDEX 0:indexed(a=? AND b=?)}
99 SELECT * FROM t1 WHERE a='abc' AND a='def'
100 } {SCAN TABLE t1 VIRTUAL TABLE INDEX 0:indexed(a=?)}
103 SELECT * FROM t1,t2 WHERE c=a
106 |--SCAN TABLE t1 VIRTUAL TABLE INDEX 0:
107 `--SCAN TABLE t2 VIRTUAL TABLE INDEX 0:indexed(c=?)
111 SELECT * FROM t1, t2 CROSS JOIN t3 WHERE t2.c = +t1.b AND t3.e=t2.d
114 |--SCAN TABLE t1 VIRTUAL TABLE INDEX 0:
115 |--SCAN TABLE t2 VIRTUAL TABLE INDEX 0:indexed(c=?)
116 `--SCAN TABLE t3 VIRTUAL TABLE INDEX 0:indexed(e=?)
120 SELECT * FROM t1, t2, t3 WHERE t2.c = +t1.b AND t3.e = t2.d
123 |--SCAN TABLE t1 VIRTUAL TABLE INDEX 0:
124 |--SCAN TABLE t2 VIRTUAL TABLE INDEX 0:indexed(c=?)
125 `--SCAN TABLE t3 VIRTUAL TABLE INDEX 0:indexed(e=?)
128 do_execsql_test 1.7.1 {
129 CREATE TABLE x1(a, b);
132 SELECT * FROM x1 CROSS JOIN t1, t2, t3
133 WHERE t1.a = t2.c AND t1.b = t3.e
137 |--SCAN TABLE t1 VIRTUAL TABLE INDEX 0:
138 |--SCAN TABLE t2 VIRTUAL TABLE INDEX 0:indexed(c=?)
139 `--SCAN TABLE t3 VIRTUAL TABLE INDEX 0:indexed(e=?)