1 # The author disclaims copyright to this source code. In place of
2 # a legal notice, here is a blessing:
4 # May you do good and not evil.
5 # May you find forgiveness for yourself and forgive others.
6 # May you share freely, never taking more than you give.
8 #***********************************************************************
9 # This file implements regression tests for SQLite library. The
10 # focus of this file is testing compute SELECT statements and nested
13 # $Id: select7.test,v 1.11 2007/09/12 17:01:45 danielk1977 Exp $
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
18 set testprefix select7
22 # A 3-way INTERSECT. Ticket #875
26 create temp table t1(x);
27 insert into t1 values('amx');
28 insert into t1 values('anx');
29 insert into t1 values('amy');
30 insert into t1 values('bmy');
31 select * from t1 where x like 'a__'
32 intersect select * from t1 where x like '_m_'
33 intersect select * from t1 where x like '__x';
39 # Nested views do not handle * properly. Ticket #826.
44 CREATE TABLE x(id integer primary key, a TEXT NULL);
45 INSERT INTO x (a) VALUES ('first');
46 CREATE TABLE tempx(id integer primary key, a TEXT NULL);
47 INSERT INTO tempx (a) VALUES ('t-first');
48 CREATE VIEW tv1 AS SELECT x.id, tx.id FROM x JOIN tempx tx ON tx.id=x.id;
49 CREATE VIEW tv1b AS SELECT x.id, tx.id FROM x JOIN tempx tx on tx.id=x.id;
50 CREATE VIEW tv2 AS SELECT * FROM tv1 UNION SELECT * FROM tv1b;
56 } ;# ifcapable compound
58 # Do not allow GROUP BY without an aggregate. Ticket #1039.
60 # Change: force any query with a GROUP BY clause to be processed as
61 # an aggregate query, whether it contains aggregates or not.
64 # do_test select7-3.1 {
66 # SELECT * FROM (SELECT * FROM sqlite_master) GROUP BY name
68 # } {1 {GROUP BY may only be used on aggregate queries}}
71 SELECT * FROM (SELECT * FROM sqlite_master) GROUP BY name
73 } [list 0 [execsql {SELECT * FROM sqlite_master ORDER BY name}]]
76 # Ticket #2018 - Make sure names are resolved correctly on all
77 # SELECT statements of a compound subquery.
79 ifcapable {subquery && compound} {
82 CREATE TABLE IF NOT EXISTS photo(pk integer primary key, x);
83 CREATE TABLE IF NOT EXISTS tag(pk integer primary key, fk int, name);
85 SELECT P.pk from PHOTO P WHERE NOT EXISTS (
86 SELECT T2.pk from TAG T2 WHERE T2.fk = P.pk
88 SELECT T3.pk from TAG T3 WHERE T3.fk = P.pk AND T3.name LIKE '%foo%'
94 INSERT INTO photo VALUES(1,1);
95 INSERT INTO photo VALUES(2,2);
96 INSERT INTO photo VALUES(3,3);
97 INSERT INTO tag VALUES(11,1,'one');
98 INSERT INTO tag VALUES(12,1,'two');
99 INSERT INTO tag VALUES(21,1,'one-b');
100 SELECT P.pk from PHOTO P WHERE NOT EXISTS (
101 SELECT T2.pk from TAG T2 WHERE T2.fk = P.pk
103 SELECT T3.pk from TAG T3 WHERE T3.fk = P.pk AND T3.name LIKE '%foo%'
111 ifcapable {subquery && compound} {
112 do_test select7-5.1 {
114 CREATE TABLE t2(a,b);
115 SELECT 5 IN (SELECT a,b FROM t2);
117 } {1 {sub-select returns 2 columns - expected 1}}
118 do_test select7-5.2 {
120 SELECT 5 IN (SELECT * FROM t2);
122 } {1 {sub-select returns 2 columns - expected 1}}
123 do_test select7-5.3 {
125 SELECT 5 IN (SELECT a,b FROM t2 UNION SELECT b,a FROM t2);
127 } {1 {sub-select returns 2 columns - expected 1}}
128 do_test select7-5.4 {
130 SELECT 5 IN (SELECT * FROM t2 UNION SELECT * FROM t2);
132 } {1 {sub-select returns 2 columns - expected 1}}
135 # Verify that an error occurs if you have too many terms on a
136 # compound select statement.
138 if {[clang_sanitize_address]==0} {
140 if {$SQLITE_MAX_COMPOUND_SELECT>0} {
143 for {set i 1} {$i<$SQLITE_MAX_COMPOUND_SELECT} {incr i} {
144 append sql " UNION ALL SELECT $i"
147 do_test select7-6.1 {
150 append sql { UNION ALL SELECT 99999999}
151 do_test select7-6.2 {
153 } {1 {too many terms in compound SELECT}}
158 # https://issues.chromium.org/issues/358174302
159 # Need to support an unlimited number of terms in a VALUES clause, even
160 # if some of those terms contain double-quoted string literals.
162 do_execsql_test select7-6.5 {
163 DROP TABLE IF EXISTS t1;
164 CREATE TABLE t1(a,b,c);
166 sqlite3_limit db SQLITE_LIMIT_COMPOUND_SELECT 10
167 sqlite3_db_config db SQLITE_DBCONFIG_DQS_DML 0
168 do_catchsql_test select7-6.6 {
169 INSERT INTO t1 VALUES
170 (NULL,0,""), (X'',0.0,0.0), (X'',X'',""), (0.0,0.0,""), (NULL,NULL,0.0),
171 (0,"",0), (0.0,X'',0), ("",X'',0.0), (0.0,X'',NULL), (0,NULL,""),
172 (0,"",NULL), (0.0,NULL,X''), ("",X'',NULL), (NULL,0,""),
173 (0,NULL,0), (X'',X'',0.0);
174 } {1 {no such column: "" - should this be a string literal in single-quotes?}}
175 do_execsql_test select7-6.7 {
176 SELECT count(*) FROM t1;
178 sqlite3_db_config db SQLITE_DBCONFIG_DQS_DML 1
179 do_catchsql_test select7-6.8 {
180 INSERT INTO t1 VALUES
181 (NULL,0,""), (X'',0.0,0.0), (X'',X'',""), (0.0,0.0,""), (NULL,NULL,0.0),
182 (0,"",0), (0.0,X'',0), ("",X'',0.0), (0.0,X'',NULL), (0,NULL,""),
183 (0,"",NULL), (0.0,NULL,X''), ("",X'',NULL), (NULL,0,""),
184 (0,NULL,0), (X'',X'',0.0);
186 do_execsql_test select7-6.9 {
187 SELECT count(*) FROM t1;
190 # This block of tests verifies that bug aa92c76cd4 is fixed.
192 do_test select7-7.1 {
194 CREATE TABLE t3(a REAL);
195 INSERT INTO t3 VALUES(44.0);
196 INSERT INTO t3 VALUES(56.0);
199 do_test select7-7.2 {
201 pragma vdbe_trace = 0;
202 SELECT (CASE WHEN a=0 THEN 0 ELSE (a + 25) / 50 END) AS categ, count(*)
203 FROM t3 GROUP BY categ
206 do_test select7-7.3 {
208 CREATE TABLE t4(a REAL);
209 INSERT INTO t4 VALUES( 2.0 );
210 INSERT INTO t4 VALUES( 3.0 );
213 do_test select7-7.4 {
215 SELECT (CASE WHEN a=0 THEN 'zero' ELSE a/2 END) AS t FROM t4 GROUP BY t;
218 do_test select7-7.5 {
219 execsql { SELECT a=0, typeof(a) FROM t4 }
221 do_test select7-7.6 {
222 execsql { SELECT a=0, typeof(a) FROM t4 GROUP BY a }
225 do_test select7-7.7 {
227 CREATE TABLE t5(a TEXT, b INT);
228 INSERT INTO t5 VALUES(123, 456);
229 SELECT typeof(a), a FROM t5 GROUP BY a HAVING a<b;
233 do_execsql_test 8.0 {
234 CREATE TABLE t01(x, y);
235 CREATE TABLE t02(x, y);
238 do_catchsql_test 8.1 {
240 SELECT * FROM t01 UNION SELECT x FROM t02
242 } {1 {SELECTs to the left and right of UNION do not have the same number of result columns}}
244 do_catchsql_test 8.2 {
245 CREATE VIEW v0 as SELECT x, y FROM t01 UNION SELECT x FROM t02;
246 EXPLAIN QUERY PLAN SELECT * FROM v0 WHERE x='0' OR y;
247 } {1 {SELECTs to the left and right of UNION do not have the same number of result columns}}