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
21 # A 3-way INTERSECT. Ticket #875
25 create temp table t1(x);
26 insert into t1 values('amx');
27 insert into t1 values('anx');
28 insert into t1 values('amy');
29 insert into t1 values('bmy');
30 select * from t1 where x like 'a__'
31 intersect select * from t1 where x like '_m_'
32 intersect select * from t1 where x like '__x';
38 # Nested views do not handle * properly. Ticket #826.
43 CREATE TABLE x(id integer primary key, a TEXT NULL);
44 INSERT INTO x (a) VALUES ('first');
45 CREATE TABLE tempx(id integer primary key, a TEXT NULL);
46 INSERT INTO tempx (a) VALUES ('t-first');
47 CREATE VIEW tv1 AS SELECT x.id, tx.id FROM x JOIN tempx tx ON tx.id=x.id;
48 CREATE VIEW tv1b AS SELECT x.id, tx.id FROM x JOIN tempx tx on tx.id=x.id;
49 CREATE VIEW tv2 AS SELECT * FROM tv1 UNION SELECT * FROM tv1b;
55 } ;# ifcapable compound
57 # Do not allow GROUP BY without an aggregate. Ticket #1039.
59 # Change: force any query with a GROUP BY clause to be processed as
60 # an aggregate query, whether it contains aggregates or not.
63 # do_test select7-3.1 {
65 # SELECT * FROM (SELECT * FROM sqlite_master) GROUP BY name
67 # } {1 {GROUP BY may only be used on aggregate queries}}
70 SELECT * FROM (SELECT * FROM sqlite_master) GROUP BY name
72 } [list 0 [execsql {SELECT * FROM sqlite_master ORDER BY name}]]
75 # Ticket #2018 - Make sure names are resolved correctly on all
76 # SELECT statements of a compound subquery.
78 ifcapable {subquery && compound} {
81 CREATE TABLE IF NOT EXISTS photo(pk integer primary key, x);
82 CREATE TABLE IF NOT EXISTS tag(pk integer primary key, fk int, name);
84 SELECT P.pk from PHOTO P WHERE NOT EXISTS (
85 SELECT T2.pk from TAG T2 WHERE T2.fk = P.pk
87 SELECT T3.pk from TAG T3 WHERE T3.fk = P.pk AND T3.name LIKE '%foo%'
93 INSERT INTO photo VALUES(1,1);
94 INSERT INTO photo VALUES(2,2);
95 INSERT INTO photo VALUES(3,3);
96 INSERT INTO tag VALUES(11,1,'one');
97 INSERT INTO tag VALUES(12,1,'two');
98 INSERT INTO tag VALUES(21,1,'one-b');
99 SELECT P.pk from PHOTO P WHERE NOT EXISTS (
100 SELECT T2.pk from TAG T2 WHERE T2.fk = P.pk
102 SELECT T3.pk from TAG T3 WHERE T3.fk = P.pk AND T3.name LIKE '%foo%'
110 ifcapable {subquery && compound} {
111 do_test select7-5.1 {
113 CREATE TABLE t2(a,b);
114 SELECT 5 IN (SELECT a,b FROM t2);
117 {only a single result allowed for a SELECT that is part of an expression}]
118 do_test select7-5.2 {
120 SELECT 5 IN (SELECT * FROM t2);
123 {only a single result allowed for a SELECT that is part of an expression}]
124 do_test select7-5.3 {
126 SELECT 5 IN (SELECT a,b FROM t2 UNION SELECT b,a FROM t2);
129 {only a single result allowed for a SELECT that is part of an expression}]
130 do_test select7-5.4 {
132 SELECT 5 IN (SELECT * FROM t2 UNION SELECT * FROM t2);
135 {only a single result allowed for a SELECT that is part of an expression}]
138 # Verify that an error occurs if you have too many terms on a
139 # compound select statement.
141 if {[clang_sanitize_address]==0} {
143 if {$SQLITE_MAX_COMPOUND_SELECT>0} {
146 for {set i 1} {$i<$SQLITE_MAX_COMPOUND_SELECT} {incr i} {
147 append sql " UNION ALL SELECT $i"
150 do_test select7-6.1 {
153 append sql { UNION ALL SELECT 99999999}
154 do_test select7-6.2 {
156 } {1 {too many terms in compound SELECT}}
161 # This block of tests verifies that bug aa92c76cd4 is fixed.
163 do_test select7-7.1 {
165 CREATE TABLE t3(a REAL);
166 INSERT INTO t3 VALUES(44.0);
167 INSERT INTO t3 VALUES(56.0);
170 do_test select7-7.2 {
172 pragma vdbe_trace = 0;
173 SELECT (CASE WHEN a=0 THEN 0 ELSE (a + 25) / 50 END) AS categ, count(*)
174 FROM t3 GROUP BY categ
177 do_test select7-7.3 {
179 CREATE TABLE t4(a REAL);
180 INSERT INTO t4 VALUES( 2.0 );
181 INSERT INTO t4 VALUES( 3.0 );
184 do_test select7-7.4 {
186 SELECT (CASE WHEN a=0 THEN 'zero' ELSE a/2 END) AS t FROM t4 GROUP BY t;
189 do_test select7-7.5 {
190 execsql { SELECT a=0, typeof(a) FROM t4 }
192 do_test select7-7.6 {
193 execsql { SELECT a=0, typeof(a) FROM t4 GROUP BY a }
196 do_test select7-7.7 {
198 CREATE TABLE t5(a TEXT, b INT);
199 INSERT INTO t5 VALUES(123, 456);
200 SELECT typeof(a), a FROM t5 GROUP BY a HAVING a<b;