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.
13 # $Id: selectC.test,v 1.5 2009/05/17 15:26:21 drh Exp $
15 set testdir [file dirname $argv0]
16 source $testdir/tester.tcl
17 set testprefix selectC
22 CREATE TABLE t1(a, b, c);
23 INSERT INTO t1 VALUES(1,'aaa','bbb');
24 INSERT INTO t1 SELECT * FROM t1;
25 INSERT INTO t1 VALUES(2,'ccc','ddd');
27 SELECT DISTINCT a AS x, b||c AS y
29 WHERE y IN ('aaabbb','xxx');
34 SELECT DISTINCT a AS x, b||c AS y
36 WHERE b||c IN ('aaabbb','xxx');
41 SELECT DISTINCT a AS x, b||c AS y
48 SELECT DISTINCT a AS x, b||c AS y
55 SELECT DISTINCT a AS x, b||c AS y
62 SELECT DISTINCT a AS x, b||c AS y
69 SELECT DISTINCT a AS x, b||c AS y
76 SELECT a AS x, b||c AS y
84 SELECT a AS x, b||c AS y
90 do_test selectC-1.10 {
92 SELECT a AS x, b||c AS y
98 do_test selectC-1.11 {
100 SELECT a AS x, b||c AS y
106 proc longname_toupper x {return [string toupper $x]}
107 db function uppercaseconversionfunctionwithaverylongname longname_toupper
108 do_test selectC-1.12.1 {
110 SELECT DISTINCT upper(b) AS x
115 do_test selectC-1.12.2 {
117 SELECT DISTINCT uppercaseconversionfunctionwithaverylongname(b) AS x
122 do_test selectC-1.13.1 {
130 do_test selectC-1.13.2 {
132 SELECT uppercaseconversionfunctionwithaverylongname(b) AS x
138 do_test selectC-1.14.1 {
145 do_test selectC-1.14.2 {
147 SELECT uppercaseconversionfunctionwithaverylongname(b) AS x
153 # The following query used to leak memory. Verify that has been fixed.
155 ifcapable trigger&&compound {
156 do_test selectC-2.1 {
158 CREATE TABLE t21a(a,b);
159 INSERT INTO t21a VALUES(1,2);
160 CREATE TABLE t21b(n);
161 CREATE TRIGGER r21 AFTER INSERT ON t21b BEGIN
162 SELECT a FROM t21a WHERE a>new.x UNION ALL
163 SELECT b FROM t21a WHERE b>new.x ORDER BY 1 LIMIT 2;
165 INSERT INTO t21b VALUES(6);
167 } {1 {no such column: new.x}}
170 # Check that ticket [883034dcb5] is fixed.
172 do_test selectC-3.1 {
174 CREATE TABLE person (
175 org_id TEXT NOT NULL,
176 nickname TEXT NOT NULL,
178 CONSTRAINT person_pk PRIMARY KEY (org_id, nickname),
179 CONSTRAINT person_license_uk UNIQUE (license)
181 INSERT INTO person VALUES('meyers', 'jack', '2GAT123');
182 INSERT INTO person VALUES('meyers', 'hill', 'V345FMP');
183 INSERT INTO person VALUES('meyers', 'jim', '2GAT138');
184 INSERT INTO person VALUES('smith', 'maggy', '');
185 INSERT INTO person VALUES('smith', 'jose', 'JJZ109');
186 INSERT INTO person VALUES('smith', 'jack', 'THX138');
187 INSERT INTO person VALUES('lakeside', 'dave', '953OKG');
188 INSERT INTO person VALUES('lakeside', 'amy', NULL);
189 INSERT INTO person VALUES('lake-apts', 'tom', NULL);
190 INSERT INTO person VALUES('acorn', 'hideo', 'CQB421');
194 count((NOT (org_id IS NULL)) AND (NOT (nickname IS NULL)))
196 WHERE (CASE WHEN license != '' THEN 1 ELSE 0 END)
199 } {acorn 1 lakeside 1 meyers 3 smith 2}
200 do_test selectC-3.2 {
202 CREATE TABLE t2(a PRIMARY KEY, b);
203 INSERT INTO t2 VALUES('abc', 'xxx');
204 INSERT INTO t2 VALUES('def', 'yyy');
205 SELECT a, max(b || a) FROM t2 WHERE (b||b||b)!='value' GROUP BY a;
207 } {abc xxxabc def yyydef}
208 do_test selectC-3.3 {
210 SELECT b, max(a || b) FROM t2 WHERE (b||b||b)!='value' GROUP BY a;
212 } {xxx abcxxx yyy defyyy}
215 proc udf {} { incr ::udf }
219 do_execsql_test selectC-4.1 {
220 create table t_distinct_bug (a, b, c);
221 insert into t_distinct_bug values ('1', '1', 'a');
222 insert into t_distinct_bug values ('1', '2', 'b');
223 insert into t_distinct_bug values ('1', '3', 'c');
224 insert into t_distinct_bug values ('1', '1', 'd');
225 insert into t_distinct_bug values ('1', '2', 'e');
226 insert into t_distinct_bug values ('1', '3', 'f');
229 do_execsql_test selectC-4.2 {
230 select a from (select distinct a, b from t_distinct_bug)
233 do_execsql_test selectC-4.2b {
234 CREATE VIEW v42b AS SELECT DISTINCT a, b FROM t_distinct_bug;
238 do_execsql_test selectC-4.3 {
239 select a, udf() from (select distinct a, b from t_distinct_bug)
242 #-------------------------------------------------------------------------
243 # Test that the problem in ticket #190c2507 has been fixed.
245 do_execsql_test 5.0 {
249 CREATE VIEW vvv AS SELECT b FROM x2 ORDER BY 1;
251 INSERT INTO x1 VALUES('a'), ('b');
252 INSERT INTO x2 VALUES(22), (23), (25), (24), (21);
253 INSERT INTO x3 VALUES(302), (303), (301);
256 do_execsql_test 5.1 {
257 CREATE TABLE x4 AS SELECT b FROM vvv UNION ALL SELECT c from x3;
259 } {21 22 23 24 25 302 303 301}
261 do_execsql_test 5.2 {
264 a 21 a 22 a 23 a 24 a 25 a 302 a 303 a 301
265 b 21 b 22 b 23 b 24 b 25 b 302 b 303 b 301
268 do_execsql_test 5.3 {
269 SELECT * FROM x1, (SELECT b FROM vvv UNION ALL SELECT c from x3) ORDER BY 1,2;
271 a 21 a 22 a 23 a 24 a 25 a 301 a 302 a 303
272 b 21 b 22 b 23 b 24 b 25 b 301 b 302 b 303