The first assert() added in [0ebc65481f4a3e79] is not necessarily true in a
[sqlite.git] / test / bestindex6.test
blob8926ec4a1110c854509447ee6c0d5991aa7a0287
1 # 2018-09-09
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
13 set testdir [file dirname $argv0]
14 source $testdir/tester.tcl
15 set testprefix bestindex6
17 ifcapable !vtab {
18   finish_test
19   return
22 register_tcl_module db
24 proc vtab_command {src method args} {
25   switch -- $method {
26     xConnect {
27       return [db one {SELECT sql FROM sqlite_master where name = $src}]
28     }
30     xBestIndex {
31       set hdl [lindex $args 0]
32       set clist [$hdl constraints]
33       set orderby [$hdl orderby]
34       set mask [$hdl mask]
36       set wlist 1
38       set iCons 0
39       set ret [list]
40       foreach cons $clist {
41         catch { array unset C }
42         array set C $cons
44         if {$C(usable)} {
45           set col [db one {
46             SELECT name FROM pragma_table_info($src) WHERE cid=$C(column)
47           }]
48           switch $C(op) {
49             isnull {
50               lappend wlist "$col IS NULL"
51               lappend ret omit $iCons
52             }
53             eq {
54               lappend wlist "$col = %$iCons%"
55               lappend ret omit $iCons
56             }
57           }
58         }
59         incr iCons
60       }
61       #puts "xBestIndex: $ret"
62       lappend ret idxStr [join $wlist " AND "]
63       return $ret
64     }
66     xFilter {
67       foreach {idxnum idxstr aa} $args {}
68       set map [list]
69       for {set iCons 0} {$iCons < [llength $aa]} {incr iCons} {
70         lappend map %$iCons% [lindex $aa $iCons]
71       }
72       set ret [list sql \
73           "SELECT rowid, * FROM $src WHERE [string map $map $idxstr]"
74       ]
75       # puts "xFilter: $ret"
76       return $ret
77     }
79   }
81   return {}
84 do_execsql_test 1.0 {
85   CREATE TABLE t1(id int, value text);
86   CREATE TABLE t2(ctx int, id int, value text); 
88   INSERT INTO t1 VALUES(1,'try');
89   INSERT INTO t2 VALUES(1,1,'good');
90   INSERT INTO t2 VALUES(2,2,'evil');
92   CREATE VIRTUAL TABLE vt1 USING tcl(vtab_command t1);
93   CREATE VIRTUAL TABLE vt2 USING tcl(vtab_command t2);
96 do_execsql_test 1.1 {
97   select * from t2 left join t1 on t1.id=t2.ctx where t1.value is null;
98 } {2 2 evil {} {}}
100 do_execsql_test 1.2 {
101   select * from vt2 left join vt1 on vt1.id=vt2.ctx where vt1.value is null; 
102 } {2 2 evil {} {}}
104 unset -nocomplain xxx
105 do_execsql_test 1.3 {
106   select * from vt2 left join vt1 on vt1.id=vt2.ctx where vt1.value is $xxx; 
107 } {2 2 evil {} {}}
109 do_execsql_test 1.4 {
110   select * from t2 left join vt1 on vt1.id=t2.ctx where vt1.value = 3
111 } {}
113 finish_test