drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 1 | # 2001 September 15 |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 2 | # |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3 | # The author disclaims copyright to this source code. In place of |
| 4 | # a legal notice, here is a blessing: |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 5 | # |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 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. |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 9 | # |
| 10 | #*********************************************************************** |
| 11 | # This file implements regression tests for SQLite library. The |
| 12 | # focus of this file is testing the INSERT statement that takes is |
| 13 | # result from a SELECT. |
| 14 | # |
danielk1977 | 24acd8f | 2008-01-16 18:20:41 +0000 | [diff] [blame] | 15 | # $Id: insert2.test,v 1.19 2008/01/16 18:20:42 danielk1977 Exp $ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 16 | |
| 17 | set testdir [file dirname $argv0] |
| 18 | source $testdir/tester.tcl |
dan | 4b79bde | 2015-04-21 15:49:04 +0000 | [diff] [blame] | 19 | set testprefix insert2 |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 20 | |
| 21 | # Create some tables with data that we can select against |
| 22 | # |
| 23 | do_test insert2-1.0 { |
| 24 | execsql {CREATE TABLE d1(n int, log int);} |
| 25 | for {set i 1} {$i<=20} {incr i} { |
danielk1977 | 24acd8f | 2008-01-16 18:20:41 +0000 | [diff] [blame] | 26 | for {set j 0} {(1<<$j)<$i} {incr j} {} |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 27 | execsql "INSERT INTO d1 VALUES($i,$j)" |
| 28 | } |
| 29 | execsql {SELECT * FROM d1 ORDER BY n} |
| 30 | } {1 0 2 1 3 2 4 2 5 3 6 3 7 3 8 3 9 4 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 5 18 5 19 5 20 5} |
| 31 | |
| 32 | # Insert into a new table from the old one. |
| 33 | # |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 34 | do_test insert2-1.1.1 { |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 35 | execsql { |
| 36 | CREATE TABLE t1(log int, cnt int); |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 37 | PRAGMA count_changes=on; |
drh | cc43cab | 2005-10-05 11:35:09 +0000 | [diff] [blame] | 38 | } |
| 39 | ifcapable explain { |
| 40 | execsql { |
| 41 | EXPLAIN INSERT INTO t1 SELECT log, count(*) FROM d1 GROUP BY log; |
| 42 | } |
| 43 | } |
| 44 | execsql { |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 45 | INSERT INTO t1 SELECT log, count(*) FROM d1 GROUP BY log; |
| 46 | } |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 47 | } {6} |
| 48 | do_test insert2-1.1.2 { |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 49 | db changes |
| 50 | } {6} |
| 51 | do_test insert2-1.1.3 { |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 52 | execsql {SELECT * FROM t1 ORDER BY log} |
| 53 | } {0 1 1 1 2 2 3 4 4 8 5 4} |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 54 | |
danielk1977 | 27c7743 | 2004-11-22 13:35:41 +0000 | [diff] [blame] | 55 | ifcapable compound { |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 56 | do_test insert2-1.2.1 { |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 57 | catch {execsql {DROP TABLE t1}} |
| 58 | execsql { |
| 59 | CREATE TABLE t1(log int, cnt int); |
| 60 | INSERT INTO t1 |
| 61 | SELECT log, count(*) FROM d1 GROUP BY log |
| 62 | EXCEPT SELECT n-1,log FROM d1; |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 63 | } |
| 64 | } {4} |
| 65 | do_test insert2-1.2.2 { |
| 66 | execsql { |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 67 | SELECT * FROM t1 ORDER BY log; |
| 68 | } |
| 69 | } {0 1 3 4 4 8 5 4} |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 70 | do_test insert2-1.3.1 { |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 71 | catch {execsql {DROP TABLE t1}} |
| 72 | execsql { |
| 73 | CREATE TABLE t1(log int, cnt int); |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 74 | PRAGMA count_changes=off; |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 75 | INSERT INTO t1 |
| 76 | SELECT log, count(*) FROM d1 GROUP BY log |
| 77 | INTERSECT SELECT n-1,log FROM d1; |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 78 | } |
| 79 | } {} |
| 80 | do_test insert2-1.3.2 { |
| 81 | execsql { |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 82 | SELECT * FROM t1 ORDER BY log; |
| 83 | } |
| 84 | } {1 1 2 2} |
danielk1977 | 27c7743 | 2004-11-22 13:35:41 +0000 | [diff] [blame] | 85 | } ;# ifcapable compound |
| 86 | execsql {PRAGMA count_changes=off;} |
| 87 | |
drh | cc85b41 | 2000-06-07 15:11:27 +0000 | [diff] [blame] | 88 | do_test insert2-1.4 { |
| 89 | catch {execsql {DROP TABLE t1}} |
| 90 | set r [execsql { |
| 91 | CREATE TABLE t1(log int, cnt int); |
| 92 | CREATE INDEX i1 ON t1(log); |
| 93 | CREATE INDEX i2 ON t1(cnt); |
| 94 | INSERT INTO t1 SELECT log, count() FROM d1 GROUP BY log; |
| 95 | SELECT * FROM t1 ORDER BY log; |
| 96 | }] |
| 97 | lappend r [execsql {SELECT cnt FROM t1 WHERE log=3}] |
| 98 | lappend r [execsql {SELECT log FROM t1 WHERE cnt=4 ORDER BY log}] |
| 99 | } {0 1 1 1 2 2 3 4 4 8 5 4 4 {3 5}} |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 100 | |
drh | 24e97df | 2002-02-03 19:06:02 +0000 | [diff] [blame] | 101 | do_test insert2-2.0 { |
| 102 | execsql { |
| 103 | CREATE TABLE t3(a,b,c); |
| 104 | CREATE TABLE t4(x,y); |
| 105 | INSERT INTO t4 VALUES(1,2); |
| 106 | SELECT * FROM t4; |
| 107 | } |
| 108 | } {1 2} |
| 109 | do_test insert2-2.1 { |
| 110 | execsql { |
| 111 | INSERT INTO t3(a,c) SELECT * FROM t4; |
| 112 | SELECT * FROM t3; |
| 113 | } |
| 114 | } {1 {} 2} |
| 115 | do_test insert2-2.2 { |
| 116 | execsql { |
| 117 | DELETE FROM t3; |
| 118 | INSERT INTO t3(c,b) SELECT * FROM t4; |
| 119 | SELECT * FROM t3; |
| 120 | } |
| 121 | } {{} 2 1} |
| 122 | do_test insert2-2.3 { |
| 123 | execsql { |
| 124 | DELETE FROM t3; |
| 125 | INSERT INTO t3(c,a,b) SELECT x, 'hi', y FROM t4; |
| 126 | SELECT * FROM t3; |
| 127 | } |
| 128 | } {hi 2 1} |
| 129 | |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 130 | integrity_check insert2-3.0 |
drh | 24e97df | 2002-02-03 19:06:02 +0000 | [diff] [blame] | 131 | |
drh | 1b2e032 | 2002-03-03 02:49:51 +0000 | [diff] [blame] | 132 | # File table t4 with lots of data |
| 133 | # |
| 134 | do_test insert2-3.1 { |
| 135 | execsql { |
| 136 | SELECT * from t4; |
| 137 | } |
| 138 | } {1 2} |
| 139 | do_test insert2-3.2 { |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 140 | set x [db total_changes] |
drh | 1b2e032 | 2002-03-03 02:49:51 +0000 | [diff] [blame] | 141 | execsql { |
| 142 | BEGIN; |
| 143 | INSERT INTO t4 VALUES(2,4); |
| 144 | INSERT INTO t4 VALUES(3,6); |
| 145 | INSERT INTO t4 VALUES(4,8); |
| 146 | INSERT INTO t4 VALUES(5,10); |
| 147 | INSERT INTO t4 VALUES(6,12); |
| 148 | INSERT INTO t4 VALUES(7,14); |
| 149 | INSERT INTO t4 VALUES(8,16); |
| 150 | INSERT INTO t4 VALUES(9,18); |
| 151 | INSERT INTO t4 VALUES(10,20); |
| 152 | COMMIT; |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 153 | } |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 154 | expr [db total_changes] - $x |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 155 | } {9} |
| 156 | do_test insert2-3.2.1 { |
| 157 | execsql { |
drh | 1b2e032 | 2002-03-03 02:49:51 +0000 | [diff] [blame] | 158 | SELECT count(*) FROM t4; |
| 159 | } |
| 160 | } {10} |
| 161 | do_test insert2-3.3 { |
danielk1977 | 3e8c37e | 2005-01-21 03:12:14 +0000 | [diff] [blame] | 162 | ifcapable subquery { |
| 163 | execsql { |
| 164 | BEGIN; |
| 165 | INSERT INTO t4 SELECT x+(SELECT max(x) FROM t4),y FROM t4; |
| 166 | INSERT INTO t4 SELECT x+(SELECT max(x) FROM t4),y FROM t4; |
| 167 | INSERT INTO t4 SELECT x+(SELECT max(x) FROM t4),y FROM t4; |
| 168 | INSERT INTO t4 SELECT x+(SELECT max(x) FROM t4),y FROM t4; |
| 169 | COMMIT; |
| 170 | SELECT count(*) FROM t4; |
| 171 | } |
| 172 | } else { |
| 173 | db function max_x_t4 {execsql {SELECT max(x) FROM t4}} |
| 174 | execsql { |
| 175 | BEGIN; |
| 176 | INSERT INTO t4 SELECT x+max_x_t4() ,y FROM t4; |
| 177 | INSERT INTO t4 SELECT x+max_x_t4() ,y FROM t4; |
| 178 | INSERT INTO t4 SELECT x+max_x_t4() ,y FROM t4; |
| 179 | INSERT INTO t4 SELECT x+max_x_t4() ,y FROM t4; |
| 180 | COMMIT; |
| 181 | SELECT count(*) FROM t4; |
| 182 | } |
drh | 1b2e032 | 2002-03-03 02:49:51 +0000 | [diff] [blame] | 183 | } |
| 184 | } {160} |
| 185 | do_test insert2-3.4 { |
| 186 | execsql { |
| 187 | BEGIN; |
| 188 | UPDATE t4 SET y='lots of data for the row where x=' || x |
| 189 | || ' and y=' || y || ' - even more data to fill space'; |
| 190 | COMMIT; |
| 191 | SELECT count(*) FROM t4; |
| 192 | } |
| 193 | } {160} |
| 194 | do_test insert2-3.5 { |
danielk1977 | 3e8c37e | 2005-01-21 03:12:14 +0000 | [diff] [blame] | 195 | ifcapable subquery { |
| 196 | execsql { |
| 197 | BEGIN; |
| 198 | INSERT INTO t4 SELECT x+(SELECT max(x)+1 FROM t4),y FROM t4; |
| 199 | SELECT count(*) from t4; |
| 200 | ROLLBACK; |
| 201 | } |
| 202 | } else { |
| 203 | execsql { |
| 204 | BEGIN; |
| 205 | INSERT INTO t4 SELECT x+max_x_t4()+1,y FROM t4; |
| 206 | SELECT count(*) from t4; |
| 207 | ROLLBACK; |
| 208 | } |
drh | 1b2e032 | 2002-03-03 02:49:51 +0000 | [diff] [blame] | 209 | } |
| 210 | } {320} |
| 211 | do_test insert2-3.6 { |
| 212 | execsql { |
| 213 | SELECT count(*) FROM t4; |
| 214 | } |
| 215 | } {160} |
| 216 | do_test insert2-3.7 { |
| 217 | execsql { |
| 218 | BEGIN; |
| 219 | DELETE FROM t4 WHERE x!=123; |
| 220 | SELECT count(*) FROM t4; |
| 221 | ROLLBACK; |
| 222 | } |
| 223 | } {1} |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 224 | do_test insert2-3.8 { |
| 225 | db changes |
| 226 | } {159} |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 227 | integrity_check insert2-3.9 |
drh | 1b2e032 | 2002-03-03 02:49:51 +0000 | [diff] [blame] | 228 | |
drh | a42707b | 2004-09-17 17:23:15 +0000 | [diff] [blame] | 229 | # Ticket #901 |
| 230 | # |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 231 | ifcapable tempdb { |
| 232 | do_test insert2-4.1 { |
| 233 | execsql { |
| 234 | CREATE TABLE Dependencies(depId integer primary key, |
| 235 | class integer, name str, flag str); |
| 236 | CREATE TEMPORARY TABLE DepCheck(troveId INT, depNum INT, |
| 237 | flagCount INT, isProvides BOOL, class INTEGER, name STRING, |
| 238 | flag STRING); |
| 239 | INSERT INTO DepCheck |
| 240 | VALUES(-1, 0, 1, 0, 2, 'libc.so.6', 'GLIBC_2.0'); |
| 241 | INSERT INTO Dependencies |
| 242 | SELECT DISTINCT |
| 243 | NULL, |
| 244 | DepCheck.class, |
| 245 | DepCheck.name, |
| 246 | DepCheck.flag |
| 247 | FROM DepCheck LEFT OUTER JOIN Dependencies ON |
| 248 | DepCheck.class == Dependencies.class AND |
| 249 | DepCheck.name == Dependencies.name AND |
| 250 | DepCheck.flag == Dependencies.flag |
| 251 | WHERE |
| 252 | Dependencies.depId is NULL; |
| 253 | }; |
| 254 | } {} |
| 255 | } |
drh | a42707b | 2004-09-17 17:23:15 +0000 | [diff] [blame] | 256 | |
danielk1977 | 4d88778 | 2005-02-08 08:42:27 +0000 | [diff] [blame] | 257 | #-------------------------------------------------------------------- |
| 258 | # Test that the INSERT works when the SELECT statement (a) references |
| 259 | # the table being inserted into and (b) is optimized to use an index |
| 260 | # only. |
| 261 | do_test insert2-5.1 { |
| 262 | execsql { |
| 263 | CREATE TABLE t2(a, b); |
| 264 | INSERT INTO t2 VALUES(1, 2); |
| 265 | CREATE INDEX t2i1 ON t2(a); |
| 266 | INSERT INTO t2 SELECT a, 3 FROM t2 WHERE a = 1; |
| 267 | SELECT * FROM t2; |
| 268 | } |
| 269 | } {1 2 1 3} |
danielk1977 | a38b413 | 2005-05-29 14:23:12 +0000 | [diff] [blame] | 270 | ifcapable subquery { |
| 271 | do_test insert2-5.2 { |
| 272 | execsql { |
| 273 | INSERT INTO t2 SELECT (SELECT a FROM t2), 4; |
| 274 | SELECT * FROM t2; |
| 275 | } |
| 276 | } {1 2 1 3 1 4} |
| 277 | } |
danielk1977 | 4d88778 | 2005-02-08 08:42:27 +0000 | [diff] [blame] | 278 | |
dan | 4b79bde | 2015-04-21 15:49:04 +0000 | [diff] [blame] | 279 | do_execsql_test 6.0 { |
| 280 | CREATE TABLE t5(a, b, c DEFAULT 'c', d); |
| 281 | } |
| 282 | do_execsql_test 6.1 { |
| 283 | INSERT INTO t5(a) SELECT 456 UNION ALL SELECT 123 ORDER BY 1; |
| 284 | SELECT * FROM t5 ORDER BY rowid; |
| 285 | } {123 {} c {} 456 {} c {}} |
| 286 | |
| 287 | ifcapable fts3 { |
| 288 | do_execsql_test 6.2 { |
| 289 | CREATE VIRTUAL TABLE t0 USING fts4(a); |
| 290 | } |
| 291 | do_execsql_test 6.3 { |
| 292 | INSERT INTO t0 SELECT 0 UNION SELECT 0 AS 'x' ORDER BY x; |
| 293 | SELECT * FROM t0; |
| 294 | } {0} |
| 295 | } |
| 296 | |
| 297 | |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 298 | finish_test |