drh | 8cdbf83 | 2004-08-29 16:25:03 +0000 | [diff] [blame] | 1 | # The author disclaims copyright to this source code. In place of |
| 2 | # a legal notice, here is a blessing: |
| 3 | # |
| 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. |
| 7 | # |
| 8 | #*********************************************************************** |
| 9 | # This file implements regression tests for SQLite library. The |
| 10 | # focus of this file is testing compute SELECT statements and nested |
| 11 | # views. |
| 12 | # |
danielk1977 | 4152e67 | 2007-09-12 17:01:45 +0000 | [diff] [blame] | 13 | # $Id: select7.test,v 1.11 2007/09/12 17:01:45 danielk1977 Exp $ |
drh | 8cdbf83 | 2004-08-29 16:25:03 +0000 | [diff] [blame] | 14 | |
| 15 | |
| 16 | set testdir [file dirname $argv0] |
| 17 | source $testdir/tester.tcl |
dan | 923cadb | 2015-06-23 12:19:55 +0000 | [diff] [blame] | 18 | set testprefix select7 |
drh | 8cdbf83 | 2004-08-29 16:25:03 +0000 | [diff] [blame] | 19 | |
danielk1977 | 27c7743 | 2004-11-22 13:35:41 +0000 | [diff] [blame] | 20 | ifcapable compound { |
| 21 | |
drh | 8cdbf83 | 2004-08-29 16:25:03 +0000 | [diff] [blame] | 22 | # A 3-way INTERSECT. Ticket #875 |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 23 | ifcapable tempdb { |
| 24 | do_test select7-1.1 { |
| 25 | execsql { |
| 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'; |
| 34 | } |
| 35 | } {amx} |
| 36 | } |
drh | 8cdbf83 | 2004-08-29 16:25:03 +0000 | [diff] [blame] | 37 | |
| 38 | |
| 39 | # Nested views do not handle * properly. Ticket #826. |
| 40 | # |
danielk1977 | 0fa8ddb | 2004-11-22 08:43:32 +0000 | [diff] [blame] | 41 | ifcapable view { |
drh | 8cdbf83 | 2004-08-29 16:25:03 +0000 | [diff] [blame] | 42 | do_test select7-2.1 { |
| 43 | execsql { |
| 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; |
| 51 | SELECT * FROM tv2; |
| 52 | } |
| 53 | } {1 1} |
danielk1977 | 0fa8ddb | 2004-11-22 08:43:32 +0000 | [diff] [blame] | 54 | } ;# ifcapable view |
drh | 8cdbf83 | 2004-08-29 16:25:03 +0000 | [diff] [blame] | 55 | |
danielk1977 | 27c7743 | 2004-11-22 13:35:41 +0000 | [diff] [blame] | 56 | } ;# ifcapable compound |
| 57 | |
drh | 49d642d | 2005-01-03 02:26:54 +0000 | [diff] [blame] | 58 | # Do not allow GROUP BY without an aggregate. Ticket #1039. |
| 59 | # |
danielk1977 | e257300 | 2005-01-26 03:58:35 +0000 | [diff] [blame] | 60 | # Change: force any query with a GROUP BY clause to be processed as |
| 61 | # an aggregate query, whether it contains aggregates or not. |
| 62 | # |
danielk1977 | 3e8c37e | 2005-01-21 03:12:14 +0000 | [diff] [blame] | 63 | ifcapable subquery { |
danielk1977 | e257300 | 2005-01-26 03:58:35 +0000 | [diff] [blame] | 64 | # do_test select7-3.1 { |
| 65 | # catchsql { |
| 66 | # SELECT * FROM (SELECT * FROM sqlite_master) GROUP BY name |
| 67 | # } |
| 68 | # } {1 {GROUP BY may only be used on aggregate queries}} |
danielk1977 | 3e8c37e | 2005-01-21 03:12:14 +0000 | [diff] [blame] | 69 | do_test select7-3.1 { |
| 70 | catchsql { |
| 71 | SELECT * FROM (SELECT * FROM sqlite_master) GROUP BY name |
| 72 | } |
danielk1977 | e257300 | 2005-01-26 03:58:35 +0000 | [diff] [blame] | 73 | } [list 0 [execsql {SELECT * FROM sqlite_master ORDER BY name}]] |
danielk1977 | 3e8c37e | 2005-01-21 03:12:14 +0000 | [diff] [blame] | 74 | } |
danielk1977 | e257300 | 2005-01-26 03:58:35 +0000 | [diff] [blame] | 75 | |
drh | f6bbe02 | 2006-10-13 15:34:16 +0000 | [diff] [blame] | 76 | # Ticket #2018 - Make sure names are resolved correctly on all |
| 77 | # SELECT statements of a compound subquery. |
| 78 | # |
| 79 | ifcapable {subquery && compound} { |
| 80 | do_test select7-4.1 { |
| 81 | execsql { |
| 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); |
| 84 | |
| 85 | SELECT P.pk from PHOTO P WHERE NOT EXISTS ( |
| 86 | SELECT T2.pk from TAG T2 WHERE T2.fk = P.pk |
| 87 | EXCEPT |
| 88 | SELECT T3.pk from TAG T3 WHERE T3.fk = P.pk AND T3.name LIKE '%foo%' |
| 89 | ); |
| 90 | } |
| 91 | } {} |
| 92 | do_test select7-4.2 { |
| 93 | execsql { |
| 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 |
| 102 | EXCEPT |
| 103 | SELECT T3.pk from TAG T3 WHERE T3.fk = P.pk AND T3.name LIKE '%foo%' |
| 104 | ); |
| 105 | } |
| 106 | } {2 3} |
drh | e305f43 | 2007-05-09 22:56:39 +0000 | [diff] [blame] | 107 | } |
drh | f6bbe02 | 2006-10-13 15:34:16 +0000 | [diff] [blame] | 108 | |
drh | e305f43 | 2007-05-09 22:56:39 +0000 | [diff] [blame] | 109 | # ticket #2347 |
| 110 | # |
| 111 | ifcapable {subquery && compound} { |
| 112 | do_test select7-5.1 { |
| 113 | catchsql { |
| 114 | CREATE TABLE t2(a,b); |
| 115 | SELECT 5 IN (SELECT a,b FROM t2); |
| 116 | } |
| 117 | } [list 1 \ |
| 118 | {only a single result allowed for a SELECT that is part of an expression}] |
| 119 | do_test select7-5.2 { |
| 120 | catchsql { |
| 121 | SELECT 5 IN (SELECT * FROM t2); |
| 122 | } |
| 123 | } [list 1 \ |
| 124 | {only a single result allowed for a SELECT that is part of an expression}] |
| 125 | do_test select7-5.3 { |
| 126 | catchsql { |
| 127 | SELECT 5 IN (SELECT a,b FROM t2 UNION SELECT b,a FROM t2); |
| 128 | } |
| 129 | } [list 1 \ |
| 130 | {only a single result allowed for a SELECT that is part of an expression}] |
| 131 | do_test select7-5.4 { |
| 132 | catchsql { |
| 133 | SELECT 5 IN (SELECT * FROM t2 UNION SELECT * FROM t2); |
| 134 | } |
| 135 | } [list 1 \ |
| 136 | {only a single result allowed for a SELECT that is part of an expression}] |
drh | f6bbe02 | 2006-10-13 15:34:16 +0000 | [diff] [blame] | 137 | } |
| 138 | |
drh | 0325d87 | 2007-06-07 10:55:35 +0000 | [diff] [blame] | 139 | # Verify that an error occurs if you have too many terms on a |
| 140 | # compound select statement. |
| 141 | # |
dan | afcf9bd | 2014-01-23 14:44:08 +0000 | [diff] [blame] | 142 | if {[clang_sanitize_address]==0} { |
| 143 | ifcapable compound { |
| 144 | if {$SQLITE_MAX_COMPOUND_SELECT>0} { |
| 145 | set sql {SELECT 0} |
| 146 | set result 0 |
| 147 | for {set i 1} {$i<$SQLITE_MAX_COMPOUND_SELECT} {incr i} { |
| 148 | append sql " UNION ALL SELECT $i" |
| 149 | lappend result $i |
| 150 | } |
| 151 | do_test select7-6.1 { |
| 152 | catchsql $sql |
| 153 | } [list 0 $result] |
| 154 | append sql { UNION ALL SELECT 99999999} |
| 155 | do_test select7-6.2 { |
| 156 | catchsql $sql |
| 157 | } {1 {too many terms in compound SELECT}} |
danielk1977 | 4152e67 | 2007-09-12 17:01:45 +0000 | [diff] [blame] | 158 | } |
drh | 0325d87 | 2007-06-07 10:55:35 +0000 | [diff] [blame] | 159 | } |
drh | 0325d87 | 2007-06-07 10:55:35 +0000 | [diff] [blame] | 160 | } |
| 161 | |
dan | b7dca7d | 2010-03-05 16:32:12 +0000 | [diff] [blame] | 162 | # This block of tests verifies that bug aa92c76cd4 is fixed. |
| 163 | # |
| 164 | do_test select7-7.1 { |
| 165 | execsql { |
| 166 | CREATE TABLE t3(a REAL); |
| 167 | INSERT INTO t3 VALUES(44.0); |
| 168 | INSERT INTO t3 VALUES(56.0); |
| 169 | } |
| 170 | } {} |
| 171 | do_test select7-7.2 { |
| 172 | execsql { |
| 173 | pragma vdbe_trace = 0; |
| 174 | SELECT (CASE WHEN a=0 THEN 0 ELSE (a + 25) / 50 END) AS categ, count(*) |
| 175 | FROM t3 GROUP BY categ |
| 176 | } |
| 177 | } {1.38 1 1.62 1} |
| 178 | do_test select7-7.3 { |
| 179 | execsql { |
| 180 | CREATE TABLE t4(a REAL); |
| 181 | INSERT INTO t4 VALUES( 2.0 ); |
| 182 | INSERT INTO t4 VALUES( 3.0 ); |
| 183 | } |
| 184 | } {} |
| 185 | do_test select7-7.4 { |
| 186 | execsql { |
| 187 | SELECT (CASE WHEN a=0 THEN 'zero' ELSE a/2 END) AS t FROM t4 GROUP BY t; |
| 188 | } |
| 189 | } {1.0 1.5} |
| 190 | do_test select7-7.5 { |
| 191 | execsql { SELECT a=0, typeof(a) FROM t4 } |
| 192 | } {0 real 0 real} |
| 193 | do_test select7-7.6 { |
| 194 | execsql { SELECT a=0, typeof(a) FROM t4 GROUP BY a } |
| 195 | } {0 real 0 real} |
| 196 | |
| 197 | do_test select7-7.7 { |
| 198 | execsql { |
| 199 | CREATE TABLE t5(a TEXT, b INT); |
| 200 | INSERT INTO t5 VALUES(123, 456); |
| 201 | SELECT typeof(a), a FROM t5 GROUP BY a HAVING a<b; |
| 202 | } |
| 203 | } {text 123} |
| 204 | |
dan | 923cadb | 2015-06-23 12:19:55 +0000 | [diff] [blame] | 205 | do_execsql_test 8.0 { |
| 206 | CREATE TABLE t01(x, y); |
| 207 | CREATE TABLE t02(x, y); |
| 208 | } |
| 209 | |
| 210 | do_catchsql_test 8.1 { |
| 211 | SELECT * FROM ( |
| 212 | SELECT * FROM t01 UNION SELECT x FROM t02 |
| 213 | ) WHERE y=1 |
| 214 | } {1 {SELECTs to the left and right of UNION do not have the same number of result columns}} |
| 215 | |
| 216 | do_catchsql_test 8.2 { |
| 217 | CREATE VIEW v0 as SELECT x, y FROM t01 UNION SELECT x FROM t02; |
| 218 | EXPLAIN QUERY PLAN SELECT * FROM v0 WHERE x='0' OR y; |
| 219 | } {1 {SELECTs to the left and right of UNION do not have the same number of result columns}} |
| 220 | |
| 221 | |
drh | f6bbe02 | 2006-10-13 15:34:16 +0000 | [diff] [blame] | 222 | finish_test |
dan | 923cadb | 2015-06-23 12:19:55 +0000 | [diff] [blame] | 223 | |
| 224 | |