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 set hdl [lindex $args 0]
51 set clist [$hdl constraints]
52 set orderby [$hdl orderby]
58 for {set i 0} {$i < [llength $clist]} {incr i} {
60 array set C [lindex $clist $i]
61 if {$C(op)=="eq" && $C(usable) && [lsearch $cons $C(column)]<0} {
63 lappend cons $C(column)
67 set nCons [llength $cons]
69 return "cost 1000000 rows 1000000"
71 set cost [expr (11-$nCons) * 1000]
72 set ret [concat $used "cost $cost rows $cost"]
75 foreach c $cons { lappend txt "[lindex $cols $c]=?" }
76 lappend ret idxstr "indexed([join $txt { AND }])"
85 register_tcl_module db
88 CREATE VIRTUAL TABLE t1 USING tcl("vtab_cmd t1 {a b}");
89 CREATE VIRTUAL TABLE t2 USING tcl("vtab_cmd t2 {c d}");
90 CREATE VIRTUAL TABLE t3 USING tcl("vtab_cmd t3 {e f}");
94 SELECT * FROM t1 WHERE a='abc'
95 } {SCAN t1 VIRTUAL TABLE INDEX 0:indexed(a=?)}
98 SELECT * FROM t1 WHERE a='abc' AND b='def'
99 } {SCAN t1 VIRTUAL TABLE INDEX 0:indexed(a=? AND b=?)}
102 SELECT * FROM t1 WHERE a='abc' AND a='def'
103 } {SCAN t1 VIRTUAL TABLE INDEX 0:indexed(a=?)}
106 SELECT * FROM t1,t2 WHERE c=a
109 |--SCAN t1 VIRTUAL TABLE INDEX 0:
110 `--SCAN t2 VIRTUAL TABLE INDEX 0:indexed(c=?)
114 SELECT * FROM t1, t2 CROSS JOIN t3 WHERE t2.c = +t1.b AND t3.e=t2.d
117 |--SCAN t1 VIRTUAL TABLE INDEX 0:
118 |--SCAN t2 VIRTUAL TABLE INDEX 0:indexed(c=?)
119 `--SCAN t3 VIRTUAL TABLE INDEX 0:indexed(e=?)
123 SELECT * FROM t1, t2, t3 WHERE t2.c = +t1.b AND t3.e = t2.d
126 |--SCAN t1 VIRTUAL TABLE INDEX 0:
127 |--SCAN t2 VIRTUAL TABLE INDEX 0:indexed(c=?)
128 `--SCAN t3 VIRTUAL TABLE INDEX 0:indexed(e=?)
131 do_execsql_test 1.7.1 {
132 CREATE TABLE x1(a, b);
135 SELECT * FROM x1 CROSS JOIN t1, t2, t3
136 WHERE t1.a = t2.c AND t1.b = t3.e
140 |--SCAN t1 VIRTUAL TABLE INDEX 0:
141 |--SCAN t2 VIRTUAL TABLE INDEX 0:indexed(c=?)
142 `--SCAN t3 VIRTUAL TABLE INDEX 0:indexed(e=?)