insert2.test 6.8 KB

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