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 # This file implements regression tests for SQLite library. The
12 # focus of this script is descending indices.
14 # $Id: descidx1.test,v 1.10 2008/03/19 00:21:31 drh Exp $
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
20 # Do not use a codec for tests in this file, as the database file is
21 # manipulated directly using tcl scripts (using the [hexio_write] command).
25 #db eval {PRAGMA legacy_file_format=OFF}
26 sqlite3_db_config db LEGACY_FILE_FORMAT 0
28 # This procedure sets the value of the file-format in file 'test.db'
29 # to $newval. Also, the schema cookie is incremented.
31 proc set_file_format {newval} {
32 hexio_write test.db 44 [hexio_render_int32 $newval]
33 set schemacookie [hexio_get_int [hexio_read test.db 40 4]]
35 hexio_write test.db 40 [hexio_render_int32 $schemacookie]
39 # This procedure returns the value of the file-format in file 'test.db'.
41 proc get_file_format {{fname test.db}} {
42 return [hexio_get_int [hexio_read $fname 44 4]]
46 # Verify that the file format starts as 4.
48 do_test descidx1-1.1 {
51 CREATE INDEX i1 ON t1(b ASC);
55 do_test descidx1-1.2 {
57 CREATE INDEX i2 ON t1(a DESC);
62 # Put some information in the table and verify that the descending
63 # index actually works.
65 do_test descidx1-2.1 {
67 INSERT INTO t1 VALUES(1,1);
68 INSERT INTO t1 VALUES(2,2);
69 INSERT INTO t1 SELECT a+2, a+2 FROM t1;
70 INSERT INTO t1 SELECT a+4, a+4 FROM t1;
71 SELECT b FROM t1 WHERE a>3 AND a<7;
74 do_test descidx1-2.2 {
76 SELECT a FROM t1 WHERE b>3 AND b<7;
79 do_test descidx1-2.3 {
81 SELECT b FROM t1 WHERE a>=3 AND a<7;
84 do_test descidx1-2.4 {
86 SELECT b FROM t1 WHERE a>3 AND a<=7;
89 do_test descidx1-2.5 {
91 SELECT b FROM t1 WHERE a>=3 AND a<=7;
94 do_test descidx1-2.6 {
96 SELECT a FROM t1 WHERE b>=3 AND b<=7;
100 # This procedure executes the SQL. Then it checks to see if the OP_Sort
101 # opcode was executed. If an OP_Sort did occur, then "sort" is appended
102 # to the result. If no OP_Sort happened, then "nosort" is appended.
104 # This procedure is used to check to make sure sorting is or is not
105 # occurring as expected.
108 set ::sqlite_sort_count 0
109 set data [execsql $sql]
110 if {$::sqlite_sort_count} {set x sort} {set x nosort}
115 # Test sorting using a descending index.
117 do_test descidx1-3.1 {
118 cksort {SELECT a FROM t1 ORDER BY a}
119 } {1 2 3 4 5 6 7 8 nosort}
120 do_test descidx1-3.2 {
121 cksort {SELECT a FROM t1 ORDER BY a ASC}
122 } {1 2 3 4 5 6 7 8 nosort}
123 do_test descidx1-3.3 {
124 cksort {SELECT a FROM t1 ORDER BY a DESC}
125 } {8 7 6 5 4 3 2 1 nosort}
126 do_test descidx1-3.4 {
127 cksort {SELECT b FROM t1 ORDER BY a}
128 } {1 2 3 4 5 6 7 8 nosort}
129 do_test descidx1-3.5 {
130 cksort {SELECT b FROM t1 ORDER BY a ASC}
131 } {1 2 3 4 5 6 7 8 nosort}
132 do_test descidx1-3.6 {
133 cksort {SELECT b FROM t1 ORDER BY a DESC}
134 } {8 7 6 5 4 3 2 1 nosort}
135 do_test descidx1-3.7 {
136 cksort {SELECT a FROM t1 ORDER BY b}
137 } {1 2 3 4 5 6 7 8 nosort}
138 do_test descidx1-3.8 {
139 cksort {SELECT a FROM t1 ORDER BY b ASC}
140 } {1 2 3 4 5 6 7 8 nosort}
141 do_test descidx1-3.9 {
142 cksort {SELECT a FROM t1 ORDER BY b DESC}
143 } {8 7 6 5 4 3 2 1 nosort}
144 do_test descidx1-3.10 {
145 cksort {SELECT b FROM t1 ORDER BY b}
146 } {1 2 3 4 5 6 7 8 nosort}
147 do_test descidx1-3.11 {
148 cksort {SELECT b FROM t1 ORDER BY b ASC}
149 } {1 2 3 4 5 6 7 8 nosort}
150 do_test descidx1-3.12 {
151 cksort {SELECT b FROM t1 ORDER BY b DESC}
152 } {8 7 6 5 4 3 2 1 nosort}
154 do_test descidx1-3.21 {
155 cksort {SELECT a FROM t1 WHERE a>3 AND a<8 ORDER BY a}
157 do_test descidx1-3.22 {
158 cksort {SELECT a FROM t1 WHERE a>3 AND a<8 ORDER BY a ASC}
160 do_test descidx1-3.23 {
161 cksort {SELECT a FROM t1 WHERE a>3 AND a<8 ORDER BY a DESC}
163 do_test descidx1-3.24 {
164 cksort {SELECT b FROM t1 WHERE a>3 AND a<8 ORDER BY a}
166 do_test descidx1-3.25 {
167 cksort {SELECT b FROM t1 WHERE a>3 AND a<8 ORDER BY a ASC}
169 do_test descidx1-3.26 {
170 cksort {SELECT b FROM t1 WHERE a>3 AND a<8 ORDER BY a DESC}
173 # Create a table with indices that are descending on some terms and
174 # ascending on others.
177 do_test descidx1-4.1 {
179 CREATE TABLE t2(a INT, b TEXT, c BLOB, d REAL);
180 CREATE INDEX i3 ON t2(a ASC, b DESC, c ASC);
181 CREATE INDEX i4 ON t2(b DESC, a ASC, d DESC);
182 INSERT INTO t2 VALUES(1,'one',x'31',1.0);
183 INSERT INTO t2 VALUES(2,'two',x'3232',2.0);
184 INSERT INTO t2 VALUES(3,'three',x'333333',3.0);
185 INSERT INTO t2 VALUES(4,'four',x'34343434',4.0);
186 INSERT INTO t2 VALUES(5,'five',x'3535353535',5.0);
187 INSERT INTO t2 VALUES(6,'six',x'363636363636',6.0);
188 INSERT INTO t2 VALUES(2,'two',x'323232',2.1);
189 INSERT INTO t2 VALUES(2,'zwei',x'3232',2.2);
190 INSERT INTO t2 VALUES(2,NULL,NULL,2.3);
191 SELECT count(*) FROM t2;
194 do_test descidx1-4.2 {
196 SELECT d FROM t2 ORDER BY a;
198 } {1.0 2.2 2.0 2.1 2.3 3.0 4.0 5.0 6.0}
199 do_test descidx1-4.3 {
201 SELECT d FROM t2 WHERE a>=2 ORDER BY a;
203 } {2.2 2.0 2.1 2.3 3.0 4.0 5.0 6.0}
204 do_test descidx1-4.4 {
206 SELECT d FROM t2 WHERE a>2 ORDER BY a;
209 do_test descidx1-4.5 {
211 SELECT d FROM t2 WHERE a=2 AND b>'two';
214 do_test descidx1-4.6 {
216 SELECT d FROM t2 WHERE a=2 AND b>='two';
219 do_test descidx1-4.7 {
221 SELECT d FROM t2 WHERE a=2 AND b<'two';
224 do_test descidx1-4.8 {
226 SELECT d FROM t2 WHERE a=2 AND b<='two';
231 do_test descidx1-5.1 {
233 CREATE TABLE t3(a,b,c,d);
234 CREATE INDEX t3i1 ON t3(a DESC, b ASC, c DESC, d ASC);
235 INSERT INTO t3 VALUES(0,0,0,0);
236 INSERT INTO t3 VALUES(0,0,0,1);
237 INSERT INTO t3 VALUES(0,0,1,0);
238 INSERT INTO t3 VALUES(0,0,1,1);
239 INSERT INTO t3 VALUES(0,1,0,0);
240 INSERT INTO t3 VALUES(0,1,0,1);
241 INSERT INTO t3 VALUES(0,1,1,0);
242 INSERT INTO t3 VALUES(0,1,1,1);
243 INSERT INTO t3 VALUES(1,0,0,0);
244 INSERT INTO t3 VALUES(1,0,0,1);
245 INSERT INTO t3 VALUES(1,0,1,0);
246 INSERT INTO t3 VALUES(1,0,1,1);
247 INSERT INTO t3 VALUES(1,1,0,0);
248 INSERT INTO t3 VALUES(1,1,0,1);
249 INSERT INTO t3 VALUES(1,1,1,0);
250 INSERT INTO t3 VALUES(1,1,1,1);
251 SELECT count(*) FROM t3;
254 do_test descidx1-5.2 {
256 SELECT a||b||c||d FROM t3 ORDER BY a,b,c,d;
258 } {0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 sort}
259 do_test descidx1-5.3 {
261 SELECT a||b||c||d FROM t3 ORDER BY a DESC, b ASC, c DESC, d ASC;
263 } {1010 1011 1000 1001 1110 1111 1100 1101 0010 0011 0000 0001 0110 0111 0100 0101 nosort}
264 do_test descidx1-5.4 {
266 SELECT a||b||c||d FROM t3 ORDER BY a ASC, b DESC, c ASC, d DESC;
268 } {0101 0100 0111 0110 0001 0000 0011 0010 1101 1100 1111 1110 1001 1000 1011 1010 nosort}
269 do_test descidx1-5.5 {
271 SELECT a||b||c FROM t3 WHERE d=0 ORDER BY a DESC, b ASC, c DESC
273 } {101 100 111 110 001 000 011 010 nosort}
274 do_test descidx1-5.6 {
276 SELECT a||b||c FROM t3 WHERE d=0 ORDER BY a ASC, b DESC, c ASC
278 } {010 011 000 001 110 111 100 101 nosort}
279 do_test descidx1-5.7 {
281 SELECT a||b||c FROM t3 WHERE d=0 ORDER BY a ASC, b DESC, c DESC
283 } {011 010 001 000 111 110 101 100 sort}
284 do_test descidx1-5.8 {
286 SELECT a||b||c FROM t3 WHERE d=0 ORDER BY a ASC, b ASC, c ASC
288 } {000 001 010 011 100 101 110 111 sort}
289 do_test descidx1-5.9 {
291 SELECT a||b||c FROM t3 WHERE d=0 ORDER BY a DESC, b DESC, c ASC
293 } {110 111 100 101 010 011 000 001 sort}
295 # Test the legacy_file_format pragma here because we have access to
296 # the get_file_format command.
298 ifcapable legacyformat {
299 do_test descidx1-6.1 {
301 forcedelete test.db test.db-journal
303 sqlite3_db_config db LEGACY_FILE_FORMAT
306 do_test descidx1-6.1 {
308 forcedelete test.db test.db-journal
310 sqlite3_db_config db LEGACY_FILE_FORMAT
313 do_test descidx1-6.2 {
314 sqlite3_db_config db LEGACY_FILE_FORMAT 1
315 sqlite3_db_config db LEGACY_FILE_FORMAT
317 do_test descidx1-6.3 {
319 CREATE TABLE t1(a,b,c);
324 # Verify that the file format is preserved across a vacuum.
325 do_test descidx1-6.3.1 {
330 do_test descidx1-6.4 {
332 forcedelete test.db test.db-journal
334 sqlite3_db_config db LEGACY_FILE_FORMAT 0
335 sqlite3_db_config db LEGACY_FILE_FORMAT
337 do_test descidx1-6.5 {
339 CREATE TABLE t1(a,b,c);
340 CREATE INDEX i1 ON t1(a ASC, b DESC, c ASC);
341 INSERT INTO t1 VALUES(1,2,3);
342 INSERT INTO t1 VALUES(1,1,0);
343 INSERT INTO t1 VALUES(1,2,1);
344 INSERT INTO t1 VALUES(1,3,4);
349 # Verify that the file format is preserved across a vacuum.
350 do_test descidx1-6.6 {
354 do_test descidx1-6.7 {
355 sqlite3_db_config db LEGACY_FILE_FORMAT 1