First public contribution.
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 # initialised 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}
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}
176 do_test collate4-1.1.23 {
177 cksort {SELECT a FROM collate4t4 ORDER BY a COLLATE NOCASE}
179 do_test collate4-1.1.24 {
180 cksort {SELECT a FROM collate4t4 ORDER BY a COLLATE TEXT}
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}
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}
276 do_test collate4-1.2.16 {
277 cksort {SELECT a FROM collate4t3 ORDER BY a COLLATE nocase}
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 CREATE TABLE collate4t1(a COLLATE NOCASE);
325 CREATE TABLE collate4t2(b COLLATE TEXT);
327 INSERT INTO collate4t1 VALUES('a');
328 INSERT INTO collate4t1 VALUES('A');
329 INSERT INTO collate4t1 VALUES('b');
330 INSERT INTO collate4t1 VALUES('B');
331 INSERT INTO collate4t1 VALUES('c');
332 INSERT INTO collate4t1 VALUES('C');
333 INSERT INTO collate4t1 VALUES('d');
334 INSERT INTO collate4t1 VALUES('D');
335 INSERT INTO collate4t1 VALUES('e');
336 INSERT INTO collate4t1 VALUES('D');
338 INSERT INTO collate4t2 VALUES('A');
339 INSERT INTO collate4t2 VALUES('Z');
342 do_test collate4-2.1.1 {
344 SELECT * FROM collate4t2, collate4t1 WHERE a = b;
347 do_test collate4-2.1.2 {
349 CREATE INDEX collate4i1 ON collate4t1(a);
352 SELECT * FROM collate4t2, collate4t1 WHERE a = b;
355 do_test collate4-2.1.3 {
357 SELECT * FROM collate4t2, collate4t1 WHERE b = a;
360 do_test collate4-2.1.4 {
362 DROP INDEX collate4i1;
363 CREATE INDEX collate4i1 ON collate4t1(a COLLATE TEXT);
366 SELECT * FROM collate4t2, collate4t1 WHERE a = b;
369 do_test collate4-2.1.5 {
371 SELECT * FROM collate4t2, collate4t1 WHERE b = a;
375 do_test collate4-2.1.6 {
377 SELECT a FROM collate4t1 WHERE a IN (SELECT * FROM collate4t2);
380 do_test collate4-2.1.7 {
382 DROP INDEX collate4i1;
383 CREATE INDEX collate4i1 ON collate4t1(a);
386 SELECT a FROM collate4t1 WHERE a IN (SELECT * FROM collate4t2);
389 do_test collate4-2.1.8 {
391 SELECT a FROM collate4t1 WHERE a IN ('z', 'a');
394 do_test collate4-2.1.9 {
396 DROP INDEX collate4i1;
397 CREATE INDEX collate4i1 ON collate4t1(a COLLATE TEXT);
400 SELECT a FROM collate4t1 WHERE a IN ('z', 'a');
404 do_test collate4-2.1.10 {
406 DROP TABLE collate4t1;
407 DROP TABLE collate4t2;
411 do_test collate4-2.2.0 {
413 CREATE TABLE collate4t1(a COLLATE nocase, b COLLATE text, c);
414 CREATE TABLE collate4t2(a COLLATE nocase, b COLLATE text, c COLLATE TEXT);
416 INSERT INTO collate4t1 VALUES('0', '0', '0');
417 INSERT INTO collate4t1 VALUES('0', '0', '1');
418 INSERT INTO collate4t1 VALUES('0', '1', '0');
419 INSERT INTO collate4t1 VALUES('0', '1', '1');
420 INSERT INTO collate4t1 VALUES('1', '0', '0');
421 INSERT INTO collate4t1 VALUES('1', '0', '1');
422 INSERT INTO collate4t1 VALUES('1', '1', '0');
423 INSERT INTO collate4t1 VALUES('1', '1', '1');
424 insert into collate4t2 SELECT * FROM collate4t1;
427 do_test collate4-2.2.1 {
429 SELECT * FROM collate4t2 NATURAL JOIN collate4t1;
431 } {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}
432 do_test collate4-2.2.1b {
434 CREATE INDEX collate4i1 ON collate4t1(a, b, c);
437 SELECT * FROM collate4t2 NATURAL JOIN collate4t1;
439 } {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}
440 do_test collate4-2.2.2 {
442 DROP INDEX collate4i1;
443 CREATE INDEX collate4i1 ON collate4t1(a, b, c COLLATE text);
446 SELECT * FROM collate4t2 NATURAL JOIN collate4t1;
448 } {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}
450 do_test collate4-2.2.10 {
452 DROP TABLE collate4t1;
453 DROP TABLE collate4t2;
458 # These tests - collate4-3.* verify that indices that implement
459 # UNIQUE and PRIMARY KEY constraints operate correctly with user
460 # defined collation sequences.
462 do_test collate4-3.0 {
464 CREATE TABLE collate4t1(a PRIMARY KEY COLLATE NOCASE);
467 do_test collate4-3.1 {
469 INSERT INTO collate4t1 VALUES('abc');
470 INSERT INTO collate4t1 VALUES('ABC');
472 } {1 {column a is not unique}}
473 do_test collate4-3.2 {
475 SELECT * FROM collate4t1;
478 do_test collate4-3.3 {
480 INSERT INTO collate4t1 SELECT upper(a) FROM collate4t1;
482 } {1 {column a is not unique}}
483 do_test collate4-3.4 {
485 INSERT INTO collate4t1 VALUES(1);
486 UPDATE collate4t1 SET a = 'abc';
488 } {1 {column a is not unique}}
489 do_test collate4-3.5 {
491 DROP TABLE collate4t1;
492 CREATE TABLE collate4t1(a COLLATE NOCASE UNIQUE);
495 do_test collate4-3.6 {
497 INSERT INTO collate4t1 VALUES('abc');
498 INSERT INTO collate4t1 VALUES('ABC');
500 } {1 {column a is not unique}}
501 do_test collate4-3.7 {
503 SELECT * FROM collate4t1;
506 do_test collate4-3.8 {
508 INSERT INTO collate4t1 SELECT upper(a) FROM collate4t1;
510 } {1 {column a is not unique}}
511 do_test collate4-3.9 {
513 INSERT INTO collate4t1 VALUES(1);
514 UPDATE collate4t1 SET a = 'abc';
516 } {1 {column a is not unique}}
517 do_test collate4-3.10 {
519 DROP TABLE collate4t1;
520 CREATE TABLE collate4t1(a);
521 CREATE UNIQUE INDEX collate4i1 ON collate4t1(a COLLATE NOCASE);
524 do_test collate4-3.11 {
526 INSERT INTO collate4t1 VALUES('abc');
527 INSERT INTO collate4t1 VALUES('ABC');
529 } {1 {column a is not unique}}
530 do_test collate4-3.12 {
532 SELECT * FROM collate4t1;
535 do_test collate4-3.13 {
537 INSERT INTO collate4t1 SELECT upper(a) FROM collate4t1;
539 } {1 {column a is not unique}}
540 do_test collate4-3.14 {
542 INSERT INTO collate4t1 VALUES(1);
543 UPDATE collate4t1 SET a = 'abc';
545 } {1 {column a is not unique}}
547 do_test collate4-3.15 {
549 DROP TABLE collate4t1;
553 # Mimic the SQLite 2 collation type NUMERIC.
554 db collate numeric numeric_collate
555 proc numeric_collate {lhs rhs} {
556 if {$lhs == $rhs} {return 0}
557 return [expr ($lhs>$rhs)?1:-1]
561 # These tests - collate4-4.* check that min() and max() only ever
562 # use indices constructed with built-in collation type numeric.
564 # CHANGED: min() and max() now use the collation type. If there
565 # is an indice that can be used, it is used.
567 do_test collate4-4.0 {
569 CREATE TABLE collate4t1(a COLLATE TEXT);
570 INSERT INTO collate4t1 VALUES('2');
571 INSERT INTO collate4t1 VALUES('10');
572 INSERT INTO collate4t1 VALUES('20');
573 INSERT INTO collate4t1 VALUES('104');
576 do_test collate4-4.1 {
578 SELECT max(a) FROM collate4t1
581 do_test collate4-4.2 {
583 SELECT min(a) FROM collate4t1
586 do_test collate4-4.3 {
587 # Test that the index with collation type TEXT is used.
589 CREATE INDEX collate4i1 ON collate4t1(a);
592 SELECT min(a) FROM collate4t1;
595 do_test collate4-4.4 {
597 SELECT max(a) FROM collate4t1;
600 do_test collate4-4.5 {
601 # Test that the index with collation type NUMERIC is not used.
603 DROP INDEX collate4i1;
604 CREATE INDEX collate4i1 ON collate4t1(a COLLATE NUMERIC);
607 SELECT min(a) FROM collate4t1;
610 do_test collate4-4.6 {
612 SELECT max(a) FROM collate4t1;
615 do_test collate4-4.7 {
617 DROP TABLE collate4t1;
621 # Also test the scalar min() and max() functions.
623 do_test collate4-4.8 {
625 CREATE TABLE collate4t1(a COLLATE TEXT, b COLLATE NUMERIC);
626 INSERT INTO collate4t1 VALUES('11', '101');
627 INSERT INTO collate4t1 VALUES('101', '11')
630 do_test collate4-4.9 {
632 SELECT max(a, b) FROM collate4t1;
635 do_test collate4-4.10 {
637 SELECT max(b, a) FROM collate4t1;
640 do_test collate4-4.11 {
642 SELECT max(a, '101') FROM collate4t1;
645 do_test collate4-4.12 {
647 SELECT max('101', a) FROM collate4t1;
650 do_test collate4-4.13 {
652 SELECT max(b, '101') FROM collate4t1;
655 do_test collate4-4.14 {
657 SELECT max('101', b) FROM collate4t1;
661 do_test collate4-4.15 {
663 DROP TABLE collate4t1;
668 # These tests - collate4.6.* - ensure that implict INTEGER PRIMARY KEY
669 # indices do not confuse collation sequences.
671 # These indices are never used for sorting in SQLite. And you can't
672 # create another index on an INTEGER PRIMARY KEY column, so we don't have
674 # (Revised 2004-Nov-22): The ROWID can be used for sorting now.
676 do_test collate4-6.0 {
678 CREATE TABLE collate4t1(a INTEGER PRIMARY KEY);
679 INSERT INTO collate4t1 VALUES(101);
680 INSERT INTO collate4t1 VALUES(10);
681 INSERT INTO collate4t1 VALUES(15);
684 do_test collate4-6.1 {
686 SELECT * FROM collate4t1 ORDER BY 1;
689 do_test collate4-6.2 {
691 SELECT * FROM collate4t1 ORDER BY oid;
694 do_test collate4-6.3 {
696 SELECT * FROM collate4t1 ORDER BY oid||'' COLLATE TEXT;