diff options
Diffstat (limited to 'libraries/sqlite/unix/sqlite-3.5.1/test/select2.test')
-rw-r--r-- | libraries/sqlite/unix/sqlite-3.5.1/test/select2.test | 185 |
1 files changed, 185 insertions, 0 deletions
diff --git a/libraries/sqlite/unix/sqlite-3.5.1/test/select2.test b/libraries/sqlite/unix/sqlite-3.5.1/test/select2.test new file mode 100644 index 0000000..1c6a5c8 --- /dev/null +++ b/libraries/sqlite/unix/sqlite-3.5.1/test/select2.test | |||
@@ -0,0 +1,185 @@ | |||
1 | # 2001 September 15 | ||
2 | # | ||
3 | # The author disclaims copyright to this source code. In place of | ||
4 | # a legal notice, here is a blessing: | ||
5 | # | ||
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. | ||
9 | # | ||
10 | #*********************************************************************** | ||
11 | # This file implements regression tests for SQLite library. The | ||
12 | # focus of this file is testing the SELECT statement. | ||
13 | # | ||
14 | # $Id: select2.test,v 1.25 2005/07/21 03:15:01 drh Exp $ | ||
15 | |||
16 | set testdir [file dirname $argv0] | ||
17 | source $testdir/tester.tcl | ||
18 | |||
19 | # Create a table with some data | ||
20 | # | ||
21 | execsql {CREATE TABLE tbl1(f1 int, f2 int)} | ||
22 | execsql {BEGIN} | ||
23 | for {set i 0} {$i<=30} {incr i} { | ||
24 | execsql "INSERT INTO tbl1 VALUES([expr {$i%9}],[expr {$i%10}])" | ||
25 | } | ||
26 | execsql {COMMIT} | ||
27 | |||
28 | # Do a second query inside a first. | ||
29 | # | ||
30 | do_test select2-1.1 { | ||
31 | set sql {SELECT DISTINCT f1 FROM tbl1 ORDER BY f1} | ||
32 | set r {} | ||
33 | catch {unset data} | ||
34 | db eval $sql data { | ||
35 | set f1 $data(f1) | ||
36 | lappend r $f1: | ||
37 | set sql2 "SELECT f2 FROM tbl1 WHERE f1=$f1 ORDER BY f2" | ||
38 | db eval $sql2 d2 { | ||
39 | lappend r $d2(f2) | ||
40 | } | ||
41 | } | ||
42 | set r | ||
43 | } {0: 0 7 8 9 1: 0 1 8 9 2: 0 1 2 9 3: 0 1 2 3 4: 2 3 4 5: 3 4 5 6: 4 5 6 7: 5 6 7 8: 6 7 8} | ||
44 | |||
45 | do_test select2-1.2 { | ||
46 | set sql {SELECT DISTINCT f1 FROM tbl1 WHERE f1>3 AND f1<5} | ||
47 | set r {} | ||
48 | db eval $sql data { | ||
49 | set f1 $data(f1) | ||
50 | lappend r $f1: | ||
51 | set sql2 "SELECT f2 FROM tbl1 WHERE f1=$f1 ORDER BY f2" | ||
52 | db eval $sql2 d2 { | ||
53 | lappend r $d2(f2) | ||
54 | } | ||
55 | } | ||
56 | set r | ||
57 | } {4: 2 3 4} | ||
58 | |||
59 | # Create a largish table. Do this twice, once using the TCL cache and once | ||
60 | # without. Compare the performance to make sure things go faster with the | ||
61 | # cache turned on. | ||
62 | # | ||
63 | ifcapable tclvar { | ||
64 | do_test select2-2.0.1 { | ||
65 | set t1 [time { | ||
66 | execsql {CREATE TABLE tbl2(f1 int, f2 int, f3 int); BEGIN;} | ||
67 | for {set i 1} {$i<=30000} {incr i} { | ||
68 | set i2 [expr {$i*2}] | ||
69 | set i3 [expr {$i*3}] | ||
70 | db eval {INSERT INTO tbl2 VALUES($i,$i2,$i3)} | ||
71 | } | ||
72 | execsql {COMMIT} | ||
73 | }] | ||
74 | list | ||
75 | } {} | ||
76 | puts "time with cache: $::t1" | ||
77 | } | ||
78 | catch {execsql {DROP TABLE tbl2}} | ||
79 | do_test select2-2.0.2 { | ||
80 | set t2 [time { | ||
81 | execsql {CREATE TABLE tbl2(f1 int, f2 int, f3 int); BEGIN;} | ||
82 | for {set i 1} {$i<=30000} {incr i} { | ||
83 | set i2 [expr {$i*2}] | ||
84 | set i3 [expr {$i*3}] | ||
85 | execsql "INSERT INTO tbl2 VALUES($i,$i2,$i3)" | ||
86 | } | ||
87 | execsql {COMMIT} | ||
88 | }] | ||
89 | list | ||
90 | } {} | ||
91 | puts "time without cache: $t2" | ||
92 | ifcapable tclvar { | ||
93 | do_test select2-2.0.3 { | ||
94 | expr {[lindex $t1 0]<[lindex $t2 0]} | ||
95 | } 1 | ||
96 | } | ||
97 | |||
98 | do_test select2-2.1 { | ||
99 | execsql {SELECT count(*) FROM tbl2} | ||
100 | } {30000} | ||
101 | do_test select2-2.2 { | ||
102 | execsql {SELECT count(*) FROM tbl2 WHERE f2>1000} | ||
103 | } {29500} | ||
104 | |||
105 | do_test select2-3.1 { | ||
106 | execsql {SELECT f1 FROM tbl2 WHERE 1000=f2} | ||
107 | } {500} | ||
108 | |||
109 | do_test select2-3.2a { | ||
110 | execsql {CREATE INDEX idx1 ON tbl2(f2)} | ||
111 | } {} | ||
112 | do_test select2-3.2b { | ||
113 | execsql {SELECT f1 FROM tbl2 WHERE 1000=f2} | ||
114 | } {500} | ||
115 | do_test select2-3.2c { | ||
116 | execsql {SELECT f1 FROM tbl2 WHERE f2=1000} | ||
117 | } {500} | ||
118 | do_test select2-3.2d { | ||
119 | set sqlite_search_count 0 | ||
120 | btree_breakpoint | ||
121 | execsql {SELECT * FROM tbl2 WHERE 1000=f2} | ||
122 | set sqlite_search_count | ||
123 | } {3} | ||
124 | do_test select2-3.2e { | ||
125 | set sqlite_search_count 0 | ||
126 | execsql {SELECT * FROM tbl2 WHERE f2=1000} | ||
127 | set sqlite_search_count | ||
128 | } {3} | ||
129 | |||
130 | # Make sure queries run faster with an index than without | ||
131 | # | ||
132 | do_test select2-3.3 { | ||
133 | execsql {DROP INDEX idx1} | ||
134 | set sqlite_search_count 0 | ||
135 | execsql {SELECT f1 FROM tbl2 WHERE f2==2000} | ||
136 | set sqlite_search_count | ||
137 | } {29999} | ||
138 | |||
139 | # Make sure we can optimize functions in the WHERE clause that | ||
140 | # use fields from two or more different table. (Bug #6) | ||
141 | # | ||
142 | do_test select2-4.1 { | ||
143 | execsql { | ||
144 | CREATE TABLE aa(a); | ||
145 | CREATE TABLE bb(b); | ||
146 | INSERT INTO aa VALUES(1); | ||
147 | INSERT INTO aa VALUES(3); | ||
148 | INSERT INTO bb VALUES(2); | ||
149 | INSERT INTO bb VALUES(4); | ||
150 | SELECT * FROM aa, bb WHERE max(a,b)>2; | ||
151 | } | ||
152 | } {1 4 3 2 3 4} | ||
153 | do_test select2-4.2 { | ||
154 | execsql { | ||
155 | INSERT INTO bb VALUES(0); | ||
156 | SELECT * FROM aa, bb WHERE b; | ||
157 | } | ||
158 | } {1 2 1 4 3 2 3 4} | ||
159 | do_test select2-4.3 { | ||
160 | execsql { | ||
161 | SELECT * FROM aa, bb WHERE NOT b; | ||
162 | } | ||
163 | } {1 0 3 0} | ||
164 | do_test select2-4.4 { | ||
165 | execsql { | ||
166 | SELECT * FROM aa, bb WHERE min(a,b); | ||
167 | } | ||
168 | } {1 2 1 4 3 2 3 4} | ||
169 | do_test select2-4.5 { | ||
170 | execsql { | ||
171 | SELECT * FROM aa, bb WHERE NOT min(a,b); | ||
172 | } | ||
173 | } {1 0 3 0} | ||
174 | do_test select2-4.6 { | ||
175 | execsql { | ||
176 | SELECT * FROM aa, bb WHERE CASE WHEN a=b-1 THEN 1 END; | ||
177 | } | ||
178 | } {1 2 3 4} | ||
179 | do_test select2-4.7 { | ||
180 | execsql { | ||
181 | SELECT * FROM aa, bb WHERE CASE WHEN a=b-1 THEN 0 ELSE 1 END; | ||
182 | } | ||
183 | } {1 4 1 0 3 2 3 0} | ||
184 | |||
185 | finish_test | ||