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 file is testing DISTINCT, UNION, INTERSECT and EXCEPT
14 # SELECT statements that use user-defined collation sequences. Also
15 # GROUP BY clauses that use user-defined collation sequences.
17 # $Id: collate5.test,v 1.7 2008/09/16 11:58:20 drh Exp $
19 set testdir [file dirname $argv0]
20 source $testdir/tester.tcl
24 # Tests are organised as follows:
25 # collate5-1.* - DISTINCT
26 # collate5-2.* - Compound SELECT
27 # collate5-3.* - ORDER BY on compound SELECT
28 # collate5-4.* - GROUP BY
30 # Create the collation sequence 'TEXT', purely for asthetic reasons. The
31 # test cases in this script could just as easily use BINARY.
32 db collate TEXT [list string compare]
34 # Mimic the SQLite 2 collation type NUMERIC.
35 db collate numeric numeric_collate
36 proc numeric_collate {lhs rhs} {
37 if {$lhs == $rhs} {return 0}
38 return [expr ($lhs>$rhs)?1:-1]
42 # These tests - collate5-1.* - focus on the DISTINCT keyword.
44 do_test collate5-1.0 {
46 CREATE TABLE collate5t1(a COLLATE nocase, b COLLATE text);
48 INSERT INTO collate5t1 VALUES('a', 'apple');
49 INSERT INTO collate5t1 VALUES('A', 'Apple');
50 INSERT INTO collate5t1 VALUES('b', 'banana');
51 INSERT INTO collate5t1 VALUES('B', 'banana');
52 INSERT INTO collate5t1 VALUES('n', NULL);
53 INSERT INTO collate5t1 VALUES('N', NULL);
56 do_test collate5-1.1 {
58 SELECT DISTINCT a FROM collate5t1;
61 do_test collate5-1.2 {
63 SELECT DISTINCT b FROM collate5t1;
65 } {apple Apple banana {}}
66 do_test collate5-1.3 {
68 SELECT DISTINCT a, b FROM collate5t1;
70 } {a apple A Apple b banana n {}}
74 do_test collate5-1.11 {
76 CREATE TABLE tkt3376(a COLLATE nocase PRIMARY KEY);
77 INSERT INTO tkt3376 VALUES('abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz');
78 INSERT INTO tkt3376 VALUES('ABXYZ012234567890123456789ABXYZ012234567890123456789ABXYZ012234567890123456789ABXYZ012234567890123456789ABXYZ012234567890123456789ABXYZ012234567890123456789ABXYZ012234567890123456789');
79 SELECT DISTINCT a FROM tkt3376;
81 } {abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz ABXYZ012234567890123456789ABXYZ012234567890123456789ABXYZ012234567890123456789ABXYZ012234567890123456789ABXYZ012234567890123456789ABXYZ012234567890123456789ABXYZ012234567890123456789}
82 do_test collate5-1.12 {
85 PRAGMA encoding=UTF16le;
86 CREATE TABLE tkt3376(a COLLATE nocase PRIMARY KEY);
87 INSERT INTO tkt3376 VALUES('abc');
88 INSERT INTO tkt3376 VALUES('ABX');
89 SELECT DISTINCT a FROM tkt3376;
94 # The remainder of this file tests compound SELECT statements.
95 # Omit it if the library is compiled such that they are omitted.
103 # Tests named collate5-2.* focus on UNION, EXCEPT and INTERSECT
104 # queries that use user-defined collation sequences.
106 # collate5-2.1.* - UNION
107 # collate5-2.2.* - INTERSECT
108 # collate5-2.3.* - EXCEPT
110 do_test collate5-2.0 {
112 CREATE TABLE collate5t2(a COLLATE text, b COLLATE nocase);
114 INSERT INTO collate5t2 VALUES('a', 'apple');
115 INSERT INTO collate5t2 VALUES('A', 'apple');
116 INSERT INTO collate5t2 VALUES('b', 'banana');
117 INSERT INTO collate5t2 VALUES('B', 'Banana');
121 do_test collate5-2.1.1 {
123 SELECT a FROM collate5t1 UNION select a FROM collate5t2;
126 do_test collate5-2.1.2 {
128 SELECT a FROM collate5t2 UNION select a FROM collate5t1;
131 do_test collate5-2.1.3 {
133 SELECT a, b FROM collate5t1 UNION select a, b FROM collate5t2;
135 } {A Apple A apple B Banana b banana N {}}
136 do_test collate5-2.1.4 {
138 SELECT a, b FROM collate5t2 UNION select a, b FROM collate5t1;
140 } {A Apple B banana N {} a apple b banana n {}}
142 do_test collate5-2.2.1 {
144 SELECT a FROM collate5t1 EXCEPT select a FROM collate5t2;
147 do_test collate5-2.2.2 {
149 SELECT a FROM collate5t2 EXCEPT select a FROM collate5t1 WHERE a != 'a';
152 do_test collate5-2.2.3 {
154 SELECT a, b FROM collate5t1 EXCEPT select a, b FROM collate5t2;
157 do_test collate5-2.2.4 {
159 SELECT a, b FROM collate5t2 EXCEPT select a, b FROM collate5t1
164 do_test collate5-2.3.1 {
166 SELECT a FROM collate5t1 INTERSECT select a FROM collate5t2;
169 do_test collate5-2.3.2 {
171 SELECT a FROM collate5t2 INTERSECT select a FROM collate5t1 WHERE a != 'a';
174 do_test collate5-2.3.3 {
176 SELECT a, b FROM collate5t1 INTERSECT select a, b FROM collate5t2;
179 do_test collate5-2.3.4 {
181 SELECT a, b FROM collate5t2 INTERSECT select a, b FROM collate5t1;
183 } {A apple B Banana a apple b banana}
186 # This test ensures performs a UNION operation with a bunch of different
187 # length records. The goal is to test that the logic that compares records
188 # for the compound SELECT operators works with record lengths that lie
189 # either side of the troublesome 256 and 65536 byte marks.
192 0 1 2 3 4 5 6 7 8 9 \
193 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 \
194 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 \
195 65520 65521 65522 65523 65524 65525 65526 65527 65528 65529 65530 \
196 65531 65532 65533 65534 65535 65536 65537 65538 65539 65540 65541 \
197 65542 65543 65544 65545 65546 65547 65548 65549 65550 65551 ]
198 do_test collate5-2.4.0 {
201 CREATE TABLE collate5t3(a, b);
204 execsql "INSERT INTO collate5t3 VALUES($ii, '[string repeat a $ii]');"
206 expr [llength [execsql {
208 SELECT * FROM collate5t3 UNION SELECT * FROM collate5t3;
211 do_test collate5-2.4.1 {
212 execsql {DROP TABLE collate5t3;}
217 # These tests - collate5-3.* - focus on compound SELECT queries that
218 # feature ORDER BY clauses.
220 do_test collate5-3.0 {
222 SELECT a FROM collate5t1 UNION ALL SELECT a FROM collate5t2 ORDER BY 1;
224 } {/[aA] [aA] [aA] [aA] [bB] [bB] [bB] [bB] [nN] [nN]/}
225 do_test collate5-3.1 {
227 SELECT a FROM collate5t2 UNION ALL SELECT a FROM collate5t1 ORDER BY 1;
229 } {A A B B N a a b b n}
230 do_test collate5-3.2 {
232 SELECT a FROM collate5t1 UNION ALL SELECT a FROM collate5t2
233 ORDER BY 1 COLLATE TEXT;
235 } {A A B B N a a b b n}
237 do_test collate5-3.3 {
239 CREATE TABLE collate5t_cn(a COLLATE NUMERIC);
240 CREATE TABLE collate5t_ct(a COLLATE TEXT);
241 INSERT INTO collate5t_cn VALUES('1');
242 INSERT INTO collate5t_cn VALUES('11');
243 INSERT INTO collate5t_cn VALUES('101');
244 INSERT INTO collate5t_ct SELECT * FROM collate5t_cn;
247 do_test collate5-3.4 {
249 SELECT a FROM collate5t_cn INTERSECT SELECT a FROM collate5t_ct ORDER BY 1;
252 do_test collate5-3.5 {
254 SELECT a FROM collate5t_ct INTERSECT SELECT a FROM collate5t_cn ORDER BY 1;
258 do_test collate5-3.20 {
260 DROP TABLE collate5t_cn;
261 DROP TABLE collate5t_ct;
262 DROP TABLE collate5t1;
263 DROP TABLE collate5t2;
267 do_test collate5-4.0 {
269 CREATE TABLE collate5t1(a COLLATE NOCASE, b COLLATE NUMERIC);
270 INSERT INTO collate5t1 VALUES('a', '1');
271 INSERT INTO collate5t1 VALUES('A', '1.0');
272 INSERT INTO collate5t1 VALUES('b', '2');
273 INSERT INTO collate5t1 VALUES('B', '3');
276 do_test collate5-4.1 {
277 string tolower [execsql {
278 SELECT a, count(*) FROM collate5t1 GROUP BY a;
281 do_test collate5-4.2 {
283 SELECT a, b, count(*) FROM collate5t1 GROUP BY a, b ORDER BY a, b;
285 } {/[aA] 1(.0)? 2 [bB] 2 1 [bB] 3 1/}
286 do_test collate5-4.3 {
288 DROP TABLE collate5t1;