Fix a problem causing the recovery extension to use excessive memory and CPU time...
[sqlite.git] / test / bestindexC.test
blob476ea39faa02eff23dd472f53af81558929039f1
1 # 2024-04-26
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 bestindexC
18 ifcapable !vtab {
19   finish_test
20   return
23 register_tcl_module db
25 proc vtab_command {lVal method args} {
26   switch -- $method {
27     xConnect {
28       return "CREATE TABLE t1(a)"
29     }
31     xBestIndex {
32       set hdl [lindex $args 0]
33       set clist [$hdl constraints]
34       set orderby [$hdl orderby]
36       set idxstr [list]
37       set res [list]
39       set idx 0
40       foreach c $clist {
41         array set a $c
42         if {$a(usable)==0} continue
43         if {$a(op)=="limit" && ![info exists ::do_not_use_limit]} { 
44           lappend idxstr limit
45           lappend res omit $idx
46         }
47         if {$a(op)=="offset" && ![info exists ::do_not_use_offset]} { 
48           lappend idxstr offset
49           lappend res omit $idx
50         }
51         incr idx
52       }
54       return "cost 1000000 rows 1000000 idxnum 0 idxstr {$idxstr} $res"
55     }
57     xFilter {
58       set idxstr [lindex $args 1]
59       set LIMIT ""
60       foreach a $idxstr b [lindex $args 2] {
61         set x($a) $b
62       }
64       if {![info exists x(limit)]} { set x(limit) -1 }
65       if {![info exists x(offset)]} { set x(offset) -1 }
66       set LIMIT " LIMIT $x(limit) OFFSET $x(offset)"
68       set idx 1
69       foreach v $lVal {
70         lappend lRow "($idx, '$v')"
71         incr idx
72       }
74       return [list sql "
75         SELECT * FROM ( VALUES [join $lRow ,]) $LIMIT
76       "]
77     }
78   }
80   return {}
83 do_execsql_test 1.0 {
84   CREATE VIRTUAL TABLE x1 USING tcl(vtab_command "a b c d e f");
85   CREATE VIRTUAL TABLE x2 USING tcl(vtab_command "A B C D E F a b");
86 } {}
88 do_execsql_test 1.1 {
89   CREATE TEMP TABLE t_unionall AS 
90     SELECT * FROM x1 UNION ALL SELECT * FROM x2;
92   CREATE TEMP TABLE t_intersect AS 
93     SELECT * FROM x1 INTERSECT SELECT * FROM x2;
95   CREATE TEMP TABLE t_union AS 
96     SELECT * FROM x1 UNION SELECT * FROM x2;
98   CREATE TEMP TABLE t_except AS 
99     SELECT * FROM x1 EXCEPT SELECT * FROM x2;
102 foreach {tn limit} {
103   1 "LIMIT 8" 
104   2 "LIMIT 4" 
105   3 "LIMIT 4 OFFSET 2" 
106   4 "LIMIT 8 OFFSET 4" 
107 } {
109   foreach {op tbl} {
110     "UNION ALL" t_unionall
111     "UNION"     t_union
112     "INTERSECT" t_intersect
113     "EXCEPT"    t_except
114   } {
116     set expect [execsql "SELECT * FROM $tbl $limit"]
117     do_execsql_test 1.2.$tbl.$tn "SELECT * FROM (
118       SELECT * FROM x1 $op SELECT * FROM x2
119     ) $limit" $expect
121   }
125 #-------------------------------------------------------------------------
126 reset_db
127 register_tcl_module db
129 do_execsql_test 2.0 {
130   CREATE VIRTUAL TABLE x1 USING tcl(vtab_command "a b c d e f");
131   CREATE VIRTUAL TABLE x2 USING tcl(vtab_command "a b e f");
132 } {}
134 do_execsql_test 2.1 {
135   SELECT * FROM x1 
136     EXCEPT
137   SELECT * FROM x2
138   LIMIT 3
139 } {c d}
141 #-------------------------------------------------------------------------
142 reset_db
143 register_tcl_module db
144 do_execsql_test 3.0 {
145   CREATE VIRTUAL TABLE y1 USING tcl(vtab_command "1 2 3 4 5 6 7 8 9 10");
146 } {}
148 do_execsql_test 3.1 {
149   SELECT * FROM y1 WHERE a = COALESCE('8', a) LIMIT 3
150 } {8}
152 do_execsql_test 3.2 {
153   SELECT * FROM y1 WHERE a = '2' LIMIT 3
154 } {2}
156 load_static_extension db series
157 do_execsql_test 3.3 {
158   SELECT * FROM generate_series(1, 5) WHERE value = (value & 14) LIMIT 3
159 } {2 4}
161 do_execsql_test 3.4 {
162   SELECT value FROM generate_series(1,10) WHERE value>2 LIMIT 4 OFFSET 1;
163 } {4 5 6 7}
165 set ::do_not_use_limit 1
166 do_execsql_test 3.5 {
167   SELECT * FROM y1 LIMIT 5 OFFSET 3
168 } {4 5 6 7 8}
169 unset ::do_not_use_limit
170 set ::do_not_use_offset 1
171 do_execsql_test 3.6 {
172   SELECT * FROM y1 LIMIT 5 OFFSET 3
173 } {4 5 6 7 8}
174 unset ::do_not_use_offset
178 finish_test