4 # The author disclaims copyright to this source code. In place of
5 # a legal notice, here is a blessing:
7 # May you do good and not evil.
8 # May you find forgiveness for yourself and forgive others.
9 # May you share freely, never taking more than you give.
11 #***********************************************************************
12 # This file implements regression tests for SQLite library. The
13 # focus of this script is page cache subsystem.
15 # $Id: collate4.test,v 1.9 2008/01/05 17:39:30 danielk1977 Exp $
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
20 db collate TEXT text_collate
21 proc text_collate {a b} {
22 return [string compare $a $b]
25 # Do an SQL statement. Append the search count to the end of the result.
28 set ::sqlite_search_count 0
29 return [concat [execsql $sql] $::sqlite_search_count]
32 # This procedure executes the SQL. Then it checks the generated program
33 # for the SQL and appends a "nosort" to the result if the program contains the
34 # SortCallback opcode. If the program does not contain the SortCallback
35 # opcode it appends "sort"
38 set ::sqlite_sort_count 0
39 set data [execsql $sql]
40 if {$::sqlite_sort_count} {set x sort} {set x nosort}
46 # Test cases are organized roughly as follows:
48 # collate4-1.* ORDER BY.
49 # collate4-2.* WHERE clauses.
50 # collate4-3.* constraints (primary key, unique).
51 # collate4-4.* simple min() or max() queries.
52 # collate4-5.* REINDEX command
53 # collate4-6.* INTEGER PRIMARY KEY indices.
57 # These tests - collate4-1.* - check that indices are correctly
58 # selected or not selected to implement ORDER BY clauses when
59 # user defined collation sequences are involved.
61 # Because these tests also exercise all the different ways indices
62 # can be created, they also serve to verify that indices are correctly
63 # initialized with user-defined collation sequences when they are
66 # Tests named collate4-1.1.* use indices with a single column. Tests
67 # collate4-1.2.* use indices with two columns.
69 do_test collate4-1.1.0 {
71 CREATE TABLE collate4t1(a COLLATE NOCASE, b COLLATE TEXT);
72 INSERT INTO collate4t1 VALUES( 'a', 'a' );
73 INSERT INTO collate4t1 VALUES( 'b', 'b' );
74 INSERT INTO collate4t1 VALUES( NULL, NULL );
75 INSERT INTO collate4t1 VALUES( 'B', 'B' );
76 INSERT INTO collate4t1 VALUES( 'A', 'A' );
77 CREATE INDEX collate4i1 ON collate4t1(a);
78 CREATE INDEX collate4i2 ON collate4t1(b);
81 do_test collate4-1.1.1 {
82 cksort {SELECT a FROM collate4t1 ORDER BY a}
84 do_test collate4-1.1.2 {
85 cksort {SELECT a FROM collate4t1 ORDER BY a COLLATE NOCASE}
87 do_test collate4-1.1.3 {
88 cksort {SELECT a FROM collate4t1 ORDER BY a COLLATE TEXT}
90 do_test collate4-1.1.4 {
91 cksort {SELECT b FROM collate4t1 ORDER BY b}
93 do_test collate4-1.1.5 {
94 cksort {SELECT b FROM collate4t1 ORDER BY b COLLATE TEXT}
96 do_test collate4-1.1.6 {
97 cksort {SELECT b FROM collate4t1 ORDER BY b COLLATE NOCASE, rowid}
100 do_test collate4-1.1.7 {
102 CREATE TABLE collate4t2(
103 a PRIMARY KEY COLLATE NOCASE,
104 b UNIQUE COLLATE TEXT
106 INSERT INTO collate4t2 VALUES( 'a', 'a' );
107 INSERT INTO collate4t2 VALUES( NULL, NULL );
108 INSERT INTO collate4t2 VALUES( 'B', 'B' );
111 do_test collate4-1.1.8 {
112 cksort {SELECT a FROM collate4t2 ORDER BY a}
114 do_test collate4-1.1.9 {
115 cksort {SELECT a FROM collate4t2 ORDER BY a COLLATE NOCASE}
117 do_test collate4-1.1.10 {
118 cksort {SELECT a FROM collate4t2 ORDER BY a COLLATE TEXT}
120 do_test collate4-1.1.11 {
121 cksort {SELECT b FROM collate4t2 ORDER BY b}
123 do_test collate4-1.1.12 {
124 cksort {SELECT b FROM collate4t2 ORDER BY b COLLATE TEXT}
126 do_test collate4-1.1.13 {
127 cksort {SELECT b FROM collate4t2 ORDER BY b COLLATE NOCASE}
130 do_test collate4-1.1.14 {
132 CREATE TABLE collate4t3(
135 UNIQUE(a), PRIMARY KEY(b)
137 INSERT INTO collate4t3 VALUES( 'a', 'a' );
138 INSERT INTO collate4t3 VALUES( NULL, NULL );
139 INSERT INTO collate4t3 VALUES( 'B', 'B' );
142 do_test collate4-1.1.15 {
143 cksort {SELECT a FROM collate4t3 ORDER BY a}
145 do_test collate4-1.1.16 {
146 cksort {SELECT a FROM collate4t3 ORDER BY a COLLATE NOCASE}
148 do_test collate4-1.1.17 {
149 cksort {SELECT a FROM collate4t3 ORDER BY a COLLATE TEXT}
151 do_test collate4-1.1.18 {
152 cksort {SELECT b FROM collate4t3 ORDER BY b}
154 do_test collate4-1.1.19 {
155 cksort {SELECT b FROM collate4t3 ORDER BY b COLLATE TEXT}
157 do_test collate4-1.1.20 {
158 cksort {SELECT b FROM collate4t3 ORDER BY b COLLATE NOCASE}
161 do_test collate4-1.1.21 {
163 CREATE TABLE collate4t4(a COLLATE NOCASE, b COLLATE TEXT);
164 INSERT INTO collate4t4 VALUES( 'a', 'a' );
165 INSERT INTO collate4t4 VALUES( 'b', 'b' );
166 INSERT INTO collate4t4 VALUES( NULL, NULL );
167 INSERT INTO collate4t4 VALUES( 'B', 'B' );
168 INSERT INTO collate4t4 VALUES( 'A', 'A' );
169 CREATE INDEX collate4i3 ON collate4t4(a COLLATE TEXT);
170 CREATE INDEX collate4i4 ON collate4t4(b COLLATE NOCASE);
173 do_test collate4-1.1.22 {
174 cksort {SELECT a FROM collate4t4 ORDER BY a, rowid}
176 do_test collate4-1.1.23 {
177 cksort {SELECT a FROM collate4t4 ORDER BY a COLLATE NOCASE, rowid}
179 do_test collate4-1.1.24 {
180 cksort {SELECT a FROM collate4t4 ORDER BY a COLLATE TEXT, rowid}
181 } {{} A B a b nosort}
182 do_test collate4-1.1.25 {
183 cksort {SELECT b FROM collate4t4 ORDER BY b}
185 do_test collate4-1.1.26 {
186 cksort {SELECT b FROM collate4t4 ORDER BY b COLLATE TEXT}
188 do_test collate4-1.1.27 {
189 cksort {SELECT b FROM collate4t4 ORDER BY b COLLATE NOCASE}
190 } {{} a A b B nosort}
192 do_test collate4-1.1.30 {
194 DROP TABLE collate4t1;
195 DROP TABLE collate4t2;
196 DROP TABLE collate4t3;
197 DROP TABLE collate4t4;
201 do_test collate4-1.2.0 {
203 CREATE TABLE collate4t1(a COLLATE NOCASE, b COLLATE TEXT);
204 INSERT INTO collate4t1 VALUES( 'a', 'a' );
205 INSERT INTO collate4t1 VALUES( 'b', 'b' );
206 INSERT INTO collate4t1 VALUES( NULL, NULL );
207 INSERT INTO collate4t1 VALUES( 'B', 'B' );
208 INSERT INTO collate4t1 VALUES( 'A', 'A' );
209 CREATE INDEX collate4i1 ON collate4t1(a, b);
212 do_test collate4-1.2.1 {
213 cksort {SELECT a FROM collate4t1 ORDER BY a}
214 } {{} A a B b nosort}
215 do_test collate4-1.2.2 {
216 cksort {SELECT a FROM collate4t1 ORDER BY a COLLATE nocase}
217 } {{} A a B b nosort}
218 do_test collate4-1.2.3 {
219 cksort {SELECT a FROM collate4t1 ORDER BY a COLLATE text}
221 do_test collate4-1.2.4 {
222 cksort {SELECT a FROM collate4t1 ORDER BY a, b}
223 } {{} A a B b nosort}
224 do_test collate4-1.2.5 {
225 cksort {SELECT a FROM collate4t1 ORDER BY a, b COLLATE nocase, rowid}
227 do_test collate4-1.2.6 {
228 cksort {SELECT a FROM collate4t1 ORDER BY a, b COLLATE text}
229 } {{} A a B b nosort}
231 do_test collate4-1.2.7 {
233 CREATE TABLE collate4t2(
238 INSERT INTO collate4t2 VALUES( 'a', 'a' );
239 INSERT INTO collate4t2 VALUES( NULL, NULL );
240 INSERT INTO collate4t2 VALUES( 'B', 'B' );
243 do_test collate4-1.2.8 {
244 cksort {SELECT a FROM collate4t2 ORDER BY a}
246 do_test collate4-1.2.9 {
247 cksort {SELECT a FROM collate4t2 ORDER BY a COLLATE nocase}
249 do_test collate4-1.2.10 {
250 cksort {SELECT a FROM collate4t2 ORDER BY a COLLATE text}
252 do_test collate4-1.2.11 {
253 cksort {SELECT a FROM collate4t2 ORDER BY a, b}
255 do_test collate4-1.2.12 {
256 cksort {SELECT a FROM collate4t2 ORDER BY a, b COLLATE nocase}
258 do_test collate4-1.2.13 {
259 cksort {SELECT a FROM collate4t2 ORDER BY a, b COLLATE text}
262 do_test collate4-1.2.14 {
264 CREATE TABLE collate4t3(a COLLATE NOCASE, b COLLATE TEXT);
265 INSERT INTO collate4t3 VALUES( 'a', 'a' );
266 INSERT INTO collate4t3 VALUES( 'b', 'b' );
267 INSERT INTO collate4t3 VALUES( NULL, NULL );
268 INSERT INTO collate4t3 VALUES( 'B', 'B' );
269 INSERT INTO collate4t3 VALUES( 'A', 'A' );
270 CREATE INDEX collate4i2 ON collate4t3(a COLLATE TEXT, b COLLATE NOCASE);
273 do_test collate4-1.2.15 {
274 cksort {SELECT a FROM collate4t3 ORDER BY a, rowid}
276 do_test collate4-1.2.16 {
277 cksort {SELECT a FROM collate4t3 ORDER BY a COLLATE nocase, rowid}
279 do_test collate4-1.2.17 {
280 cksort {SELECT a FROM collate4t3 ORDER BY a COLLATE text}
281 } {{} A B a b nosort}
282 do_test collate4-1.2.18 {
283 cksort {SELECT a FROM collate4t3 ORDER BY a COLLATE text, b}
285 do_test collate4-1.2.19 {
286 cksort {SELECT a FROM collate4t3 ORDER BY a COLLATE text, b COLLATE nocase}
287 } {{} A B a b nosort}
288 do_test collate4-1.2.20 {
289 cksort {SELECT a FROM collate4t3 ORDER BY a COLLATE text, b COLLATE text}
291 do_test collate4-1.2.21 {
292 cksort {SELECT a FROM collate4t3 ORDER BY a COLLATE text DESC}
293 } {b a B A {} nosort}
294 do_test collate4-1.2.22 {
295 cksort {SELECT a FROM collate4t3 ORDER BY a COLLATE text DESC, b}
297 do_test collate4-1.2.23 {
298 cksort {SELECT a FROM collate4t3
299 ORDER BY a COLLATE text DESC, b COLLATE nocase}
301 do_test collate4-1.2.24 {
302 cksort {SELECT a FROM collate4t3
303 ORDER BY a COLLATE text DESC, b COLLATE nocase DESC}
304 } {b a B A {} nosort}
306 do_test collate4-1.2.25 {
308 DROP TABLE collate4t1;
309 DROP TABLE collate4t2;
310 DROP TABLE collate4t3;
315 # These tests - collate4-2.* - check that indices are correctly
316 # selected or not selected to implement WHERE clauses when user
317 # defined collation sequences are involved.
319 # Indices may optimise WHERE clauses using <, >, <=, >=, = or IN
322 do_test collate4-2.1.0 {
324 PRAGMA automatic_index=OFF;
325 CREATE TABLE collate4t1(a COLLATE NOCASE);
326 CREATE TABLE collate4t2(b COLLATE TEXT);
328 INSERT INTO collate4t1 VALUES('a');
329 INSERT INTO collate4t1 VALUES('A');
330 INSERT INTO collate4t1 VALUES('b');
331 INSERT INTO collate4t1 VALUES('B');
332 INSERT INTO collate4t1 VALUES('c');
333 INSERT INTO collate4t1 VALUES('C');
334 INSERT INTO collate4t1 VALUES('d');
335 INSERT INTO collate4t1 VALUES('D');
336 INSERT INTO collate4t1 VALUES('e');
337 INSERT INTO collate4t1 VALUES('D');
339 INSERT INTO collate4t2 VALUES('A');
340 INSERT INTO collate4t2 VALUES('Z');
343 do_test collate4-2.1.1 {
345 SELECT * FROM collate4t2, collate4t1 WHERE a = b;
348 do_test collate4-2.1.2 {
350 CREATE INDEX collate4i1 ON collate4t1(a);
353 SELECT * FROM collate4t2, collate4t1 WHERE a = b;
356 do_test collate4-2.1.3 {
358 SELECT * FROM collate4t2, collate4t1 WHERE b = a;
361 do_test collate4-2.1.4 {
363 DROP INDEX collate4i1;
364 CREATE INDEX collate4i1 ON collate4t1(a COLLATE TEXT);
367 SELECT * FROM collate4t2, collate4t1 WHERE a = b
368 ORDER BY collate4t2.rowid, collate4t1.rowid
371 do_test collate4-2.1.5 {
373 SELECT * FROM collate4t2, collate4t1 WHERE b = a;
377 do_test collate4-2.1.6 {
379 SELECT a FROM collate4t1 WHERE a IN (SELECT * FROM collate4t2)
383 do_test collate4-2.1.7 {
385 DROP INDEX collate4i1;
386 CREATE INDEX collate4i1 ON collate4t1(a);
389 SELECT a FROM collate4t1 WHERE a IN (SELECT * FROM collate4t2)
393 do_test collate4-2.1.8 {
395 SELECT a FROM collate4t1 WHERE a IN ('z', 'a');
398 do_test collate4-2.1.9 {
400 DROP INDEX collate4i1;
401 CREATE INDEX collate4i1 ON collate4t1(a COLLATE TEXT);
404 SELECT a FROM collate4t1 WHERE a IN ('z', 'a') ORDER BY rowid;
408 do_test collate4-2.1.10 {
410 DROP TABLE collate4t1;
411 DROP TABLE collate4t2;
415 do_test collate4-2.2.0 {
417 CREATE TABLE collate4t1(a COLLATE nocase, b COLLATE text, c);
418 CREATE TABLE collate4t2(a COLLATE nocase, b COLLATE text, c COLLATE TEXT);
420 INSERT INTO collate4t1 VALUES('0', '0', '0');
421 INSERT INTO collate4t1 VALUES('0', '0', '1');
422 INSERT INTO collate4t1 VALUES('0', '1', '0');
423 INSERT INTO collate4t1 VALUES('0', '1', '1');
424 INSERT INTO collate4t1 VALUES('1', '0', '0');
425 INSERT INTO collate4t1 VALUES('1', '0', '1');
426 INSERT INTO collate4t1 VALUES('1', '1', '0');
427 INSERT INTO collate4t1 VALUES('1', '1', '1');
428 insert into collate4t2 SELECT * FROM collate4t1;
431 do_test collate4-2.2.1 {
433 SELECT * FROM collate4t2 NOT INDEXED NATURAL JOIN collate4t1 NOT INDEXED;
435 } {0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1 63}
436 do_test collate4-2.2.1b {
438 CREATE INDEX collate4i1 ON collate4t1(a, b, c);
441 SELECT * FROM collate4t2 NATURAL JOIN collate4t1;
443 } {0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1 29}
444 do_test collate4-2.2.2 {
446 DROP INDEX collate4i1;
447 CREATE INDEX collate4i1 ON collate4t1(a, b, c COLLATE text);
450 SELECT * FROM collate4t2 NATURAL JOIN collate4t1;
452 } {0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1 22}
454 do_test collate4-2.2.10 {
456 DROP TABLE collate4t1;
457 DROP TABLE collate4t2;
462 # These tests - collate4-3.* verify that indices that implement
463 # UNIQUE and PRIMARY KEY constraints operate correctly with user
464 # defined collation sequences.
466 do_test collate4-3.0 {
468 CREATE TABLE collate4t1(a PRIMARY KEY COLLATE NOCASE);
471 do_test collate4-3.1 {
473 INSERT INTO collate4t1 VALUES('abc');
474 INSERT INTO collate4t1 VALUES('ABC');
476 } {1 {UNIQUE constraint failed: collate4t1.a}}
477 do_test collate4-3.2 {
479 SELECT * FROM collate4t1;
482 do_test collate4-3.3 {
484 INSERT INTO collate4t1 SELECT upper(a) FROM collate4t1;
486 } {1 {UNIQUE constraint failed: collate4t1.a}}
487 do_test collate4-3.4 {
489 INSERT INTO collate4t1 VALUES(1);
490 UPDATE collate4t1 SET a = 'abc';
492 } {1 {UNIQUE constraint failed: collate4t1.a}}
493 do_test collate4-3.5 {
495 DROP TABLE collate4t1;
496 CREATE TABLE collate4t1(a COLLATE NOCASE UNIQUE);
499 do_test collate4-3.6 {
501 INSERT INTO collate4t1 VALUES('abc');
502 INSERT INTO collate4t1 VALUES('ABC');
504 } {1 {UNIQUE constraint failed: collate4t1.a}}
505 do_test collate4-3.7 {
507 SELECT * FROM collate4t1;
510 do_test collate4-3.8 {
512 INSERT INTO collate4t1 SELECT upper(a) FROM collate4t1;
514 } {1 {UNIQUE constraint failed: collate4t1.a}}
515 do_test collate4-3.9 {
517 INSERT INTO collate4t1 VALUES(1);
518 UPDATE collate4t1 SET a = 'abc';
520 } {1 {UNIQUE constraint failed: collate4t1.a}}
521 do_test collate4-3.10 {
523 DROP TABLE collate4t1;
524 CREATE TABLE collate4t1(a);
525 CREATE UNIQUE INDEX collate4i1 ON collate4t1(a COLLATE NOCASE);
528 do_test collate4-3.11 {
530 INSERT INTO collate4t1 VALUES('abc');
531 INSERT INTO collate4t1 VALUES('ABC');
533 } {1 {UNIQUE constraint failed: collate4t1.a}}
534 do_test collate4-3.12 {
536 SELECT * FROM collate4t1;
539 do_test collate4-3.13 {
541 INSERT INTO collate4t1 SELECT upper(a) FROM collate4t1;
543 } {1 {UNIQUE constraint failed: collate4t1.a}}
544 do_test collate4-3.14 {
546 INSERT INTO collate4t1 VALUES(1);
547 UPDATE collate4t1 SET a = 'abc';
549 } {1 {UNIQUE constraint failed: collate4t1.a}}
551 do_test collate4-3.15 {
553 DROP TABLE collate4t1;
557 # Mimic the SQLite 2 collation type NUMERIC.
558 db collate numeric numeric_collate
559 proc numeric_collate {lhs rhs} {
560 if {$lhs == $rhs} {return 0}
561 return [expr ($lhs>$rhs)?1:-1]
565 # These tests - collate4-4.* check that min() and max() only ever
566 # use indices constructed with built-in collation type numeric.
568 # CHANGED: min() and max() now use the collation type. If there
569 # is an indice that can be used, it is used.
571 do_test collate4-4.0 {
573 CREATE TABLE collate4t1(a COLLATE TEXT);
574 INSERT INTO collate4t1 VALUES('2');
575 INSERT INTO collate4t1 VALUES('10');
576 INSERT INTO collate4t1 VALUES('20');
577 INSERT INTO collate4t1 VALUES('104');
580 do_test collate4-4.1 {
582 SELECT max(a) FROM collate4t1
585 do_test collate4-4.2 {
587 SELECT min(a) FROM collate4t1
590 do_test collate4-4.3 {
591 # Test that the index with collation type TEXT is used.
593 CREATE INDEX collate4i1 ON collate4t1(a);
596 SELECT min(a) FROM collate4t1;
599 do_test collate4-4.4 {
601 SELECT max(a) FROM collate4t1;
604 do_test collate4-4.5 {
605 # Test that the index with collation type NUMERIC is not used.
607 DROP INDEX collate4i1;
608 CREATE INDEX collate4i1 ON collate4t1(a COLLATE NUMERIC);
611 SELECT min(a) FROM collate4t1;
614 do_test collate4-4.6 {
616 SELECT max(a) FROM collate4t1;
619 do_test collate4-4.7 {
621 DROP TABLE collate4t1;
625 # Also test the scalar min() and max() functions.
627 do_test collate4-4.8 {
629 CREATE TABLE collate4t1(a COLLATE TEXT, b COLLATE NUMERIC);
630 INSERT INTO collate4t1 VALUES('11', '101');
631 INSERT INTO collate4t1 VALUES('101', '11')
634 do_test collate4-4.9 {
636 SELECT max(a, b) FROM collate4t1;
639 do_test collate4-4.10 {
641 SELECT max(b, a) FROM collate4t1;
644 do_test collate4-4.11 {
646 SELECT max(a, '101') FROM collate4t1;
649 do_test collate4-4.12 {
651 SELECT max('101', a) FROM collate4t1;
654 do_test collate4-4.13 {
656 SELECT max(b, '101') FROM collate4t1;
659 do_test collate4-4.14 {
661 SELECT max('101', b) FROM collate4t1;
665 do_test collate4-4.15 {
667 DROP TABLE collate4t1;
672 # These tests - collate4.6.* - ensure that implict INTEGER PRIMARY KEY
673 # indices do not confuse collation sequences.
675 # These indices are never used for sorting in SQLite. And you can't
676 # create another index on an INTEGER PRIMARY KEY column, so we don't have
678 # (Revised 2004-Nov-22): The ROWID can be used for sorting now.
680 do_test collate4-6.0 {
682 CREATE TABLE collate4t1(a INTEGER PRIMARY KEY);
683 INSERT INTO collate4t1 VALUES(101);
684 INSERT INTO collate4t1 VALUES(10);
685 INSERT INTO collate4t1 VALUES(15);
688 do_test collate4-6.1 {
690 SELECT * FROM collate4t1 ORDER BY 1;
693 do_test collate4-6.2 {
695 SELECT * FROM collate4t1 ORDER BY oid;
698 do_test collate4-6.3 {
700 SELECT * FROM collate4t1 ORDER BY oid||'' COLLATE TEXT;