xref: /sqlite-3.40.0/test/descidx1.test (revision 0b2f3160)
1# 2005 December 21
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 script is descending indices.
13#
14# $Id: descidx1.test,v 1.2 2005/12/21 18:36:46 drh Exp $
15#
16
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
19
20# This procedure sets the value of the file-format in file 'test.db'
21# to $newval. Also, the schema cookie is incremented.
22#
23proc set_file_format {newval} {
24  set bt [btree_open test.db 10 0]
25  btree_begin_transaction $bt
26  set meta [btree_get_meta $bt]
27  lset meta 2 $newval                    ;# File format
28  lset meta 1 [expr [lindex $meta 1]+1]  ;# Schema cookie
29  eval "btree_update_meta $bt $meta"
30  btree_commit $bt
31  btree_close $bt
32}
33
34# This procedure returns the value of the file-format in file 'test.db'.
35#
36proc get_file_format {{fname test.db}} {
37  set bt [btree_open $fname 10 0]
38  set meta [btree_get_meta $bt]
39  btree_close $bt
40  lindex $meta 2
41}
42
43# Verify that the file format jumps to (at least) 4 as soon as a
44# descending index is created.
45#
46do_test descidx1-1.1 {
47  execsql {
48    CREATE TABLE t1(a,b);
49    CREATE INDEX i1 ON t1(b ASC);
50  }
51  get_file_format
52} {1}
53do_test descidx1-1.2 {
54  execsql {
55    CREATE INDEX i2 ON t1(a DESC);
56  }
57  get_file_format
58} {4}
59
60# Put some information in the table and verify that the descending
61# index actually works.
62#
63do_test descidx1-2.1 {
64  execsql {
65    INSERT INTO t1 VALUES(1,1);
66    INSERT INTO t1 VALUES(2,2);
67    INSERT INTO t1 SELECT a+2, a+2 FROM t1;
68    INSERT INTO t1 SELECT a+4, a+4 FROM t1;
69    SELECT b FROM t1 WHERE a>3 AND a<7;
70  }
71} {6 5 4}
72do_test descidx1-2.2 {
73  execsql {
74    SELECT a FROM t1 WHERE b>3 AND b<7;
75  }
76} {4 5 6}
77do_test descidx1-2.3 {
78  execsql {
79    SELECT b FROM t1 WHERE a>=3 AND a<7;
80  }
81} {6 5 4 3}
82do_test descidx1-2.4 {
83  execsql {
84    SELECT b FROM t1 WHERE a>3 AND a<=7;
85  }
86} {7 6 5 4}
87do_test descidx1-2.5 {
88  execsql {
89    SELECT b FROM t1 WHERE a>=3 AND a<=7;
90  }
91} {7 6 5 4 3}
92do_test descidx1-2.6 {
93  execsql {
94    SELECT a FROM t1 WHERE b>=3 AND b<=7;
95  }
96} {3 4 5 6 7}
97
98# This procedure executes the SQL.  Then it checks to see if the OP_Sort
99# opcode was executed.  If an OP_Sort did occur, then "sort" is appended
100# to the result.  If no OP_Sort happened, then "nosort" is appended.
101#
102# This procedure is used to check to make sure sorting is or is not
103# occurring as expected.
104#
105proc cksort {sql} {
106  set ::sqlite_sort_count 0
107  set data [execsql $sql]
108  if {$::sqlite_sort_count} {set x sort} {set x nosort}
109  lappend data $x
110  return $data
111}
112
113# Test sorting using a descending index.
114#
115do_test descidx1-3.1 {
116  cksort {SELECT a FROM t1 ORDER BY a}
117} {1 2 3 4 5 6 7 8 nosort}
118do_test descidx1-3.2 {
119  cksort {SELECT a FROM t1 ORDER BY a ASC}
120} {1 2 3 4 5 6 7 8 nosort}
121do_test descidx1-3.3 {
122  cksort {SELECT a FROM t1 ORDER BY a DESC}
123} {8 7 6 5 4 3 2 1 nosort}
124do_test descidx1-3.4 {
125  cksort {SELECT b FROM t1 ORDER BY a}
126} {1 2 3 4 5 6 7 8 nosort}
127do_test descidx1-3.5 {
128  cksort {SELECT b FROM t1 ORDER BY a ASC}
129} {1 2 3 4 5 6 7 8 nosort}
130do_test descidx1-3.6 {
131  cksort {SELECT b FROM t1 ORDER BY a DESC}
132} {8 7 6 5 4 3 2 1 nosort}
133do_test descidx1-3.7 {
134  cksort {SELECT a FROM t1 ORDER BY b}
135} {1 2 3 4 5 6 7 8 nosort}
136do_test descidx1-3.8 {
137  cksort {SELECT a FROM t1 ORDER BY b ASC}
138} {1 2 3 4 5 6 7 8 nosort}
139do_test descidx1-3.9 {
140  cksort {SELECT a FROM t1 ORDER BY b DESC}
141} {8 7 6 5 4 3 2 1 nosort}
142do_test descidx1-3.10 {
143  cksort {SELECT b FROM t1 ORDER BY b}
144} {1 2 3 4 5 6 7 8 nosort}
145do_test descidx1-3.11 {
146  cksort {SELECT b FROM t1 ORDER BY b ASC}
147} {1 2 3 4 5 6 7 8 nosort}
148do_test descidx1-3.12 {
149  cksort {SELECT b FROM t1 ORDER BY b DESC}
150} {8 7 6 5 4 3 2 1 nosort}
151
152do_test descidx1-3.21 {
153  cksort {SELECT a FROM t1 WHERE a>3 AND a<8 ORDER BY a}
154} {4 5 6 7 nosort}
155do_test descidx1-3.22 {
156  cksort {SELECT a FROM t1 WHERE a>3 AND a<8 ORDER BY a ASC}
157} {4 5 6 7 nosort}
158do_test descidx1-3.23 {
159  cksort {SELECT a FROM t1 WHERE a>3 AND a<8 ORDER BY a DESC}
160} {7 6 5 4 nosort}
161do_test descidx1-3.24 {
162  cksort {SELECT b FROM t1 WHERE a>3 AND a<8 ORDER BY a}
163} {4 5 6 7 nosort}
164do_test descidx1-3.25 {
165  cksort {SELECT b FROM t1 WHERE a>3 AND a<8 ORDER BY a ASC}
166} {4 5 6 7 nosort}
167do_test descidx1-3.26 {
168  cksort {SELECT b FROM t1 WHERE a>3 AND a<8 ORDER BY a DESC}
169} {7 6 5 4 nosort}
170
171# Create a table with indices that are descending on some terms and
172# ascending on others.
173#
174do_test descidx1-4.1 {
175  execsql {
176    CREATE TABLE t2(a INT, b TEXT, c BLOB, d REAL);
177    CREATE INDEX i3 ON t2(a ASC, b DESC, c ASC);
178    CREATE INDEX i4 ON t2(b DESC, a ASC, d DESC);
179    INSERT INTO t2 VALUES(1,'one',x'31',1.0);
180    INSERT INTO t2 VALUES(2,'two',x'3232',2.0);
181    INSERT INTO t2 VALUES(3,'three',x'333333',3.0);
182    INSERT INTO t2 VALUES(4,'four',x'34343434',4.0);
183    INSERT INTO t2 VALUES(5,'five',x'3535353535',5.0);
184    INSERT INTO t2 VALUES(6,'six',x'363636363636',6.0);
185    INSERT INTO t2 VALUES(2,'two',x'323232',2.1);
186    INSERT INTO t2 VALUES(2,'zwei',x'3232',2.2);
187    INSERT INTO t2 VALUES(2,NULL,NULL,2.3);
188    SELECT count(*) FROM t2;
189  }
190} {9}
191do_test descidx1-4.2 {
192  execsql {
193    SELECT d FROM t2 ORDER BY a;
194  }
195} {1.0 2.2 2.0 2.1 2.3 3.0 4.0 5.0 6.0}
196do_test descidx1-4.3 {
197  execsql {
198    SELECT d FROM t2 WHERE a>=2;
199  }
200} {2.2 2.0 2.1 2.3 3.0 4.0 5.0 6.0}
201do_test descidx1-4.4 {
202  execsql {
203    SELECT d FROM t2 WHERE a>2;
204  }
205} {3.0 4.0 5.0 6.0}
206do_test descidx1-4.5 {
207  execsql {
208    SELECT d FROM t2 WHERE a=2 AND b>'two';
209  }
210} {2.2}
211do_test descidx1-4.6 {
212  execsql {
213    SELECT d FROM t2 WHERE a=2 AND b>='two';
214  }
215} {2.2 2.0 2.1}
216do_test descidx1-4.7 {
217  execsql {
218    SELECT d FROM t2 WHERE a=2 AND b<'two';
219  }
220} {}
221do_test descidx1-4.8 {
222  execsql {
223    SELECT d FROM t2 WHERE a=2 AND b<='two';
224  }
225} {2.0 2.1}
226
227do_test descidx1-5.1 {
228  execsql {
229    CREATE TABLE t3(a,b,c,d);
230    CREATE INDEX t3i1 ON t3(a DESC, b ASC, c DESC, d ASC);
231    INSERT INTO t3 VALUES(0,0,0,0);
232    INSERT INTO t3 VALUES(0,0,0,1);
233    INSERT INTO t3 VALUES(0,0,1,0);
234    INSERT INTO t3 VALUES(0,0,1,1);
235    INSERT INTO t3 VALUES(0,1,0,0);
236    INSERT INTO t3 VALUES(0,1,0,1);
237    INSERT INTO t3 VALUES(0,1,1,0);
238    INSERT INTO t3 VALUES(0,1,1,1);
239    INSERT INTO t3 VALUES(1,0,0,0);
240    INSERT INTO t3 VALUES(1,0,0,1);
241    INSERT INTO t3 VALUES(1,0,1,0);
242    INSERT INTO t3 VALUES(1,0,1,1);
243    INSERT INTO t3 VALUES(1,1,0,0);
244    INSERT INTO t3 VALUES(1,1,0,1);
245    INSERT INTO t3 VALUES(1,1,1,0);
246    INSERT INTO t3 VALUES(1,1,1,1);
247    SELECT count(*) FROM t3;
248  }
249} {16}
250do_test descidx1-5.2 {
251  cksort {
252    SELECT a||b||c||d FROM t3 ORDER BY a,b,c,d;
253  }
254} {0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 sort}
255do_test descidx1-5.3 {
256  cksort {
257    SELECT a||b||c||d FROM t3 ORDER BY a DESC, b ASC, c DESC, d ASC;
258  }
259} {1010 1011 1000 1001 1110 1111 1100 1101 0010 0011 0000 0001 0110 0111 0100 0101 nosort}
260do_test descidx1-5.4 {
261  cksort {
262    SELECT a||b||c||d FROM t3 ORDER BY a ASC, b DESC, c ASC, d DESC;
263  }
264} {0101 0100 0111 0110 0001 0000 0011 0010 1101 1100 1111 1110 1001 1000 1011 1010 nosort}
265do_test descidx1-5.5 {
266  cksort {
267    SELECT a||b||c FROM t3 WHERE d=0 ORDER BY a DESC, b ASC, c DESC
268  }
269} {101 100 111 110 001 000 011 010 nosort}
270do_test descidx1-5.6 {
271  cksort {
272    SELECT a||b||c FROM t3 WHERE d=0 ORDER BY a ASC, b DESC, c ASC
273  }
274} {010 011 000 001 110 111 100 101 nosort}
275do_test descidx1-5.7 {
276  cksort {
277    SELECT a||b||c FROM t3 WHERE d=0 ORDER BY a ASC, b DESC, c DESC
278  }
279} {011 010 001 000 111 110 101 100 sort}
280do_test descidx1-5.8 {
281  cksort {
282    SELECT a||b||c FROM t3 WHERE d=0 ORDER BY a ASC, b ASC, c ASC
283  }
284} {000 001 010 011 100 101 110 111 sort}
285do_test descidx1-5.9 {
286  cksort {
287    SELECT a||b||c FROM t3 WHERE d=0 ORDER BY a DESC, b DESC, c ASC
288  }
289} {110 111 100 101 010 011 000 001 sort}
290
291finish_test
292