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 DISTINCT queries using the skip-ahead
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
19 set testprefix distinct2
22 CREATE TABLE t1(x INTEGER PRIMARY KEY);
23 INSERT INTO t1 VALUES(0),(1),(2);
25 SELECT DISTINCT a.x AS aa, b.x AS bb
27 SELECT *, '|' FROM t2 ORDER BY aa, bb;
28 } {0 0 | 0 1 | 0 2 | 1 0 | 1 1 | 1 2 | 2 0 | 2 1 | 2 2 |}
32 SELECT DISTINCT a.x AS aa, b.x AS bb
34 WHERE a.x IN t1 AND b.x IN t1;
35 SELECT *, '|' FROM t2 ORDER BY aa, bb;
36 } {0 0 | 0 1 | 0 2 | 1 0 | 1 1 | 1 2 | 2 0 | 2 1 | 2 2 |}
38 CREATE TABLE t102 (i0 TEXT UNIQUE NOT NULL);
39 INSERT INTO t102 VALUES ('0'),('1'),('2');
44 JOIN t102 AS t4 ON (t2.i0 IN t102)
45 NATURAL JOIN t102 AS t3
46 JOIN t102 AS t1 ON (t0.i0 IN t102)
47 JOIN t102 AS t2 ON (t2.i0=+t0.i0 OR (t0.i0<>500 AND t2.i0=t1.i0));
48 SELECT *, '|' FROM t2 ORDER BY 1, 2, 3, 4, 5;
49 } {0 0 0 0 | 0 0 1 0 | 0 0 1 1 | 0 0 2 0 | 0 0 2 2 | 0 1 0 0 | 0 1 1 0 | 0 1 1 1 | 0 1 2 0 | 0 1 2 2 | 0 2 0 0 | 0 2 1 0 | 0 2 1 1 | 0 2 2 0 | 0 2 2 2 | 1 0 0 0 | 1 0 0 1 | 1 0 1 1 | 1 0 2 1 | 1 0 2 2 | 1 1 0 0 | 1 1 0 1 | 1 1 1 1 | 1 1 2 1 | 1 1 2 2 | 1 2 0 0 | 1 2 0 1 | 1 2 1 1 | 1 2 2 1 | 1 2 2 2 | 2 0 0 0 | 2 0 0 2 | 2 0 1 1 | 2 0 1 2 | 2 0 2 2 | 2 1 0 0 | 2 1 0 2 | 2 1 1 1 | 2 1 1 2 | 2 1 2 2 | 2 2 0 0 | 2 2 0 2 | 2 2 1 1 | 2 2 1 2 | 2 2 2 2 |}
52 CREATE TABLE t4(a,b,c,d,e,f,g,h,i,j);
53 INSERT INTO t4 VALUES(0,1,2,3,4,5,6,7,8,9);
54 INSERT INTO t4 SELECT * FROM t4;
55 INSERT INTO t4 SELECT * FROM t4;
56 CREATE INDEX t4x ON t4(c,d,e);
57 SELECT DISTINCT a,b,c FROM t4 WHERE a=0 AND b=1;
60 SELECT DISTINCT a,b,c,d FROM t4 WHERE a=0 AND b=1;
63 SELECT DISTINCT d,a,b,c FROM t4 WHERE a=0 AND b=1;
66 SELECT DISTINCT a,b,c,d,e FROM t4 WHERE a=0 AND b=1;
69 SELECT DISTINCT a,b,c,d,e,f FROM t4 WHERE a=0 AND b=1;
73 CREATE TABLE t5(a INT, b INT);
74 CREATE UNIQUE INDEX t5x ON t5(a+b);
75 INSERT INTO t5(a,b) VALUES(0,0),(1,0),(1,1),(0,3);
76 CREATE TEMP TABLE out AS SELECT DISTINCT a+b FROM t5;
77 SELECT * FROM out ORDER BY 1;
81 CREATE TABLE t6a(x INTEGER PRIMARY KEY);
82 INSERT INTO t6a VALUES(1);
83 CREATE TABLE t6b(y INTEGER PRIMARY KEY);
84 INSERT INTO t6b VALUES(2),(3);
85 SELECT DISTINCT x, x FROM t6a, t6b;
89 CREATE TABLE t7(a, b, c);
91 SELECT 1 UNION ALL SELECT i+1 FROM s WHERE (i+1)<200
93 INSERT INTO t7 SELECT i/100, i/50, i FROM s;
96 SELECT DISTINCT a, b FROM t7;
101 do_execsql_test 720 {
102 SELECT DISTINCT a, b+1 FROM t7;
107 do_execsql_test 730 {
108 CREATE INDEX i7 ON t7(a, b+1);
110 SELECT DISTINCT a, b+1 FROM t7;
116 do_execsql_test 800 {
117 CREATE TABLE t8(a, b, c);
119 SELECT 1 UNION ALL SELECT i+1 FROM s WHERE (i+1)<100
121 INSERT INTO t8 SELECT i/40, i/20, i/40 FROM s;
124 do_execsql_test 820 {
125 SELECT DISTINCT a, b, c FROM t8;
132 do_execsql_test 820 {
133 SELECT DISTINCT a, b, c FROM t8 WHERE b=3;
136 do_execsql_test 830 {
137 CREATE INDEX i8 ON t8(a, c);
139 SELECT DISTINCT a, b, c FROM t8 WHERE b=3;
142 do_execsql_test 900 {
144 INSERT INTO t9 VALUES
145 ('abcd'), ('Abcd'), ('aBcd'), ('ABcd'), ('abCd'), ('AbCd'), ('aBCd'),
146 ('ABCd'), ('abcD'), ('AbcD'), ('aBcD'), ('ABcD'), ('abCD'), ('AbCD'),
148 ('wxyz'), ('Wxyz'), ('wXyz'), ('WXyz'), ('wxYz'), ('WxYz'), ('wXYz'),
149 ('WXYz'), ('wxyZ'), ('WxyZ'), ('wXyZ'), ('WXyZ'), ('wxYZ'), ('WxYZ'),
153 do_execsql_test 910 {
154 SELECT DISTINCT v COLLATE NOCASE, v FROM t9 ORDER BY +v;
156 ABCD ABCD ABCd ABCd ABcD ABcD ABcd ABcd AbCD
157 AbCD AbCd AbCd AbcD AbcD Abcd Abcd
158 WXYZ WXYZ WXYz WXYz WXyZ WXyZ WXyz WXyz WxYZ
159 WxYZ WxYz WxYz WxyZ WxyZ Wxyz Wxyz
160 aBCD aBCD aBCd aBCd aBcD aBcD aBcd aBcd abCD
161 abCD abCd abCd abcD abcD abcd abcd
162 wXYZ wXYZ wXYz wXYz wXyZ wXyZ wXyz wXyz wxYZ
163 wxYZ wxYz wxYz wxyZ wxyZ wxyz wxyz
166 do_execsql_test 920 {
167 CREATE INDEX i9 ON t9(v COLLATE NOCASE, v);
170 SELECT DISTINCT v COLLATE NOCASE, v FROM t9 ORDER BY +v;
172 ABCD ABCD ABCd ABCd ABcD ABcD ABcd ABcd AbCD
173 AbCD AbCd AbCd AbcD AbcD Abcd Abcd
174 WXYZ WXYZ WXYz WXYz WXyZ WXyZ WXyz WXyz WxYZ
175 WxYZ WxYz WxYz WxyZ WxyZ Wxyz Wxyz
176 aBCD aBCD aBCd aBCd aBcD aBcD aBcd aBcd abCD
177 abCD abCd abCd abcD abcD abcd abcd
178 wXYZ wXYZ wXYz wXYz wXyZ wXyZ wXyz wXyz wxYZ
179 wxYZ wxYz wxYz wxyZ wxyZ wxyz wxyz
182 # Ticket https://sqlite.org/src/info/ef9318757b152e3a on 2017-11-21
183 # Incorrect result due to a skip-ahead-distinct optimization on a
184 # join where no rows of the inner loop appear in the result set.
188 do_execsql_test 1000 {
189 CREATE TABLE t1(a INTEGER PRIMARY KEY, b INTEGER);
190 CREATE INDEX t1b ON t1(b);
191 CREATE TABLE t2(x INTEGER PRIMARY KEY, y INTEGER);
192 CREATE INDEX t2y ON t2(y);
193 WITH RECURSIVE c(x) AS (VALUES(0) UNION ALL SELECT x+1 FROM c WHERE x<49)
194 INSERT INTO t1(b) SELECT x/10 - 1 FROM c;
195 WITH RECURSIVE c(x) AS (VALUES(-1) UNION ALL SELECT x+1 FROM c WHERE x<19)
196 INSERT INTO t2(x,y) SELECT x, 1 FROM c;
197 SELECT DISTINCT y FROM t1, t2 WHERE b=x AND b<>-1;
199 SELECT DISTINCT y FROM t1, t2 WHERE b=x AND b<>-1;
203 do_execsql_test 1010 {
204 CREATE TABLE t1(a INTEGER PRIMARY KEY, b INTEGER);
205 CREATE INDEX t1b ON t1(b);
206 CREATE TABLE t2(x INTEGER PRIMARY KEY, y INTEGER);
207 CREATE INDEX t2y ON t2(y);
208 WITH RECURSIVE c(x) AS (VALUES(0) UNION ALL SELECT x+1 FROM c WHERE x<49)
209 INSERT INTO t1(b) SELECT -(x/10 - 1) FROM c;
210 WITH RECURSIVE c(x) AS (VALUES(-1) UNION ALL SELECT x+1 FROM c WHERE x<19)
211 INSERT INTO t2(x,y) SELECT -x, 1 FROM c;
212 SELECT DISTINCT y FROM t1, t2 WHERE b=x AND b<>1 ORDER BY y DESC;
214 SELECT DISTINCT y FROM t1, t2 WHERE b=x AND b<>1 ORDER BY y DESC;
218 do_execsql_test 1020 {
219 CREATE TABLE t1(a, b);
220 CREATE INDEX t1a ON t1(a, b);
221 -- Lots of rows of (1, 'no'), followed by a single (1, 'yes').
222 WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<100)
223 INSERT INTO t1(a, b) SELECT 1, 'no' FROM c;
224 INSERT INTO t1(a, b) VALUES(1, 'yes');
225 CREATE TABLE t2(x PRIMARY KEY);
226 INSERT INTO t2 VALUES('yes');
227 SELECT DISTINCT a FROM t1, t2 WHERE x=b;
229 SELECT DISTINCT a FROM t1, t2 WHERE x=b;
232 #-------------------------------------------------------------------------
235 do_execsql_test 2000 {
236 CREATE TABLE t0 (c0, c1, c2, PRIMARY KEY (c0, c1));
237 CREATE TABLE t1 (c2);
238 INSERT INTO t0(c2) VALUES (0),(1),(3),(4),(5),(6),(7),(8),(9),(10),(11);
239 INSERT INTO t0(c1) VALUES ('a');
240 INSERT INTO t1(c2) VALUES (0);
242 do_execsql_test 2010 {
243 SELECT DISTINCT t0.c0, t1._rowid_, t0.c1 FROM t1 CROSS JOIN t0 ORDER BY t0.c0;
245 do_execsql_test 1.2 {
248 do_execsql_test 2020 {
249 SELECT DISTINCT t0.c0, t1._rowid_, t0.c1 FROM t1 CROSS JOIN t0 ORDER BY t0.c0;
253 do_execsql_test 2030 {
254 CREATE TABLE t2(a, b, c);
255 CREATE INDEX t2ab ON t2(a, b);
257 WITH c(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM c WHERE i<64)
258 INSERT INTO t2 SELECT 'one', i%2, 'one' FROM c;
260 WITH c(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM c WHERE i<64)
261 INSERT INTO t2 SELECT 'two', i%2, 'two' FROM c;
263 CREATE TABLE t3(x INTEGER PRIMARY KEY);
264 INSERT INTO t3 VALUES(1);
268 do_execsql_test 2040 {
269 SELECT DISTINCT a, b, x FROM t3 CROSS JOIN t2 ORDER BY a, +b;
277 #-------------------------------------------------------------------------
280 do_execsql_test 3000 {
281 CREATE TABLE t0 (c0, c1 NOT NULL DEFAULT 1, c2, PRIMARY KEY (c0, c1));
282 INSERT INTO t0(c2) VALUES (NULL), (NULL), (NULL), (NULL), (NULL), (NULL), (NULL), (NULL), (NULL), (NULL), (NULL);
283 INSERT INTO t0(c2) VALUES('a');
286 do_execsql_test 3010 {
287 SELECT DISTINCT * FROM t0 WHERE NULL IS t0.c0;
293 do_execsql_test 3020 {
297 do_execsql_test 3030 {
298 SELECT DISTINCT * FROM t0 WHERE NULL IS c0;
304 #-------------------------------------------------------------------------
308 do_execsql_test 4010 {
309 CREATE TABLE t1(a, b COLLATE RTRIM);
310 INSERT INTO t1 VALUES(1, ''), (2, ' '), (3, ' ');
312 do_execsql_test 4020 {
313 SELECT b FROM t1 UNION SELECT 1;