rowid.test 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 magic ROWID column that is
  13. # found on all tables.
  14. #
  15. # $Id: rowid.test,v 1.8 2002/02/19 22:42:06 drh Exp $
  16. set testdir [file dirname $argv0]
  17. source $testdir/tester.tcl
  18. # Basic ROWID functionality tests.
  19. #
  20. do_test rowid-1.1 {
  21. execsql {
  22. CREATE TABLE t1(x int, y int);
  23. INSERT INTO t1 VALUES(1,2);
  24. INSERT INTO t1 VALUES(3,4);
  25. SELECT x FROM t1 ORDER BY y;
  26. }
  27. } {1 3}
  28. do_test rowid-1.2 {
  29. set r [execsql {SELECT rowid FROM t1 ORDER BY x}]
  30. global x2rowid rowid2x
  31. set x2rowid(1) [lindex $r 0]
  32. set x2rowid(3) [lindex $r 1]
  33. set rowid2x($x2rowid(1)) 1
  34. set rowid2x($x2rowid(3)) 3
  35. llength $r
  36. } {2}
  37. do_test rowid-1.3 {
  38. global x2rowid
  39. set sql "SELECT x FROM t1 WHERE rowid==$x2rowid(1)"
  40. execsql $sql
  41. } {1}
  42. do_test rowid-1.4 {
  43. global x2rowid
  44. set sql "SELECT x FROM t1 WHERE rowid==$x2rowid(3)"
  45. execsql $sql
  46. } {3}
  47. do_test rowid-1.5 {
  48. global x2rowid
  49. set sql "SELECT x FROM t1 WHERE oid==$x2rowid(1)"
  50. execsql $sql
  51. } {1}
  52. do_test rowid-1.6 {
  53. global x2rowid
  54. set sql "SELECT x FROM t1 WHERE OID==$x2rowid(3)"
  55. execsql $sql
  56. } {3}
  57. do_test rowid-1.7 {
  58. global x2rowid
  59. set sql "SELECT x FROM t1 WHERE _rowid_==$x2rowid(1)"
  60. execsql $sql
  61. } {1}
  62. do_test rowid-1.7.1 {
  63. while 1 {
  64. set norow [expr {int(rand()*1000000)}]
  65. if {$norow!=$x2rowid(1) && $norow!=$x2rowid(3)} break
  66. }
  67. execsql "SELECT x FROM t1 WHERE rowid=$norow"
  68. } {}
  69. do_test rowid-1.8 {
  70. global x2rowid
  71. set v [execsql {SELECT x, oid FROM t1 order by x}]
  72. set v2 [list 1 $x2rowid(1) 3 $x2rowid(3)]
  73. expr {$v==$v2}
  74. } {1}
  75. do_test rowid-1.9 {
  76. global x2rowid
  77. set v [execsql {SELECT x, RowID FROM t1 order by x}]
  78. set v2 [list 1 $x2rowid(1) 3 $x2rowid(3)]
  79. expr {$v==$v2}
  80. } {1}
  81. do_test rowid-1.9 {
  82. global x2rowid
  83. set v [execsql {SELECT x, _rowid_ FROM t1 order by x}]
  84. set v2 [list 1 $x2rowid(1) 3 $x2rowid(3)]
  85. expr {$v==$v2}
  86. } {1}
  87. # We cannot update or insert the ROWID column
  88. #
  89. do_test rowid-2.1 {
  90. set v [catch {execsql {INSERT INTO t1(rowid,x,y) VALUES(1234,5,6)}} msg]
  91. lappend v $msg
  92. } {1 {table t1 has no column named rowid}}
  93. do_test rowid-2.2 {
  94. set v [catch {execsql {UPDATE t1 SET rowid=12345 WHERE x==1}}]
  95. lappend v $msg
  96. } {1 {table t1 has no column named rowid}}
  97. do_test rowid-2.3 {
  98. set v [catch {execsql {INSERT INTO t1(oid,x,y) VALUES(1234,5,6)}} msg]
  99. lappend v $msg
  100. } {1 {table t1 has no column named oid}}
  101. do_test rowid-2.4 {
  102. set v [catch {execsql {UPDATE t1 SET oid=12345 WHERE x==1}}]
  103. lappend v $msg
  104. } {1 {table t1 has no column named oid}}
  105. do_test rowid-2.5 {
  106. set v [catch {execsql {INSERT INTO t1(_rowid_,x,y) VALUES(1234,5,6)}} msg]
  107. lappend v $msg
  108. } {1 {table t1 has no column named _rowid_}}
  109. do_test rowid-2.6 {
  110. set v [catch {execsql {UPDATE t1 SET _rowid_=12345 WHERE x==1}}]
  111. lappend v $msg
  112. } {1 {table t1 has no column named _rowid_}}
  113. # But we can use ROWID in the WHERE clause of an UPDATE that does not
  114. # change the ROWID.
  115. #
  116. do_test rowid-2.7 {
  117. global x2rowid
  118. set sql "UPDATE t1 SET x=2 WHERE OID==$x2rowid(3)"
  119. execsql $sql
  120. execsql {SELECT x FROM t1 ORDER BY x}
  121. } {1 2}
  122. do_test rowid-2.8 {
  123. global x2rowid
  124. set sql "UPDATE t1 SET x=3 WHERE _rowid_==$x2rowid(3)"
  125. execsql $sql
  126. execsql {SELECT x FROM t1 ORDER BY x}
  127. } {1 3}
  128. # We cannot index by ROWID
  129. #
  130. do_test rowid-2.9 {
  131. set v [catch {execsql {CREATE INDEX idxt1 ON t1(rowid)}} msg]
  132. lappend v $msg
  133. } {1 {table t1 has no column named rowid}}
  134. do_test rowid-2.10 {
  135. set v [catch {execsql {CREATE INDEX idxt1 ON t1(_rowid_)}} msg]
  136. lappend v $msg
  137. } {1 {table t1 has no column named _rowid_}}
  138. do_test rowid-2.11 {
  139. set v [catch {execsql {CREATE INDEX idxt1 ON t1(oid)}} msg]
  140. lappend v $msg
  141. } {1 {table t1 has no column named oid}}
  142. do_test rowid-2.12 {
  143. set v [catch {execsql {CREATE INDEX idxt1 ON t1(x, rowid)}} msg]
  144. lappend v $msg
  145. } {1 {table t1 has no column named rowid}}
  146. # Columns defined in the CREATE statement override the buildin ROWID
  147. # column names.
  148. #
  149. do_test rowid-3.1 {
  150. execsql {
  151. CREATE TABLE t2(rowid int, x int, y int);
  152. INSERT INTO t2 VALUES(0,2,3);
  153. INSERT INTO t2 VALUES(4,5,6);
  154. INSERT INTO t2 VALUES(7,8,9);
  155. SELECT * FROM t2 ORDER BY x;
  156. }
  157. } {0 2 3 4 5 6 7 8 9}
  158. do_test rowid-3.2 {
  159. execsql {SELECT * FROM t2 ORDER BY rowid}
  160. } {0 2 3 4 5 6 7 8 9}
  161. do_test rowid-3.3 {
  162. execsql {SELECT rowid, x, y FROM t2 ORDER BY rowid}
  163. } {0 2 3 4 5 6 7 8 9}
  164. do_test rowid-3.4 {
  165. set r1 [execsql {SELECT _rowid_, rowid FROM t2 ORDER BY rowid}]
  166. foreach {a b c d e f} $r1 {}
  167. set r2 [execsql {SELECT _rowid_, rowid FROM t2 ORDER BY x DESC}]
  168. foreach {u v w x y z} $r2 {}
  169. expr {$u==$e && $w==$c && $y==$a}
  170. } {1}
  171. do_probtest rowid-3.5 {
  172. set r1 [execsql {SELECT _rowid_, rowid FROM t2 ORDER BY rowid}]
  173. foreach {a b c d e f} $r1 {}
  174. expr {$a!=$b && $c!=$d && $e!=$f}
  175. } {1}
  176. # Let's try some more complex examples, including some joins.
  177. #
  178. do_test rowid-4.1 {
  179. execsql {
  180. DELETE FROM t1;
  181. DELETE FROM t2;
  182. }
  183. for {set i 1} {$i<=50} {incr i} {
  184. execsql "INSERT INTO t1(x,y) VALUES($i,[expr {$i*$i}])"
  185. }
  186. execsql {INSERT INTO t2 SELECT _rowid_, x*y, y*y FROM t1}
  187. execsql {SELECT t2.y FROM t1, t2 WHERE t1.x==4 AND t1.rowid==t2.rowid}
  188. } {256}
  189. do_test rowid-4.2 {
  190. execsql {SELECT t2.y FROM t2, t1 WHERE t1.x==4 AND t1.rowid==t2.rowid}
  191. } {256}
  192. do_test rowid-4.2.1 {
  193. execsql {SELECT t2.y FROM t2, t1 WHERE t1.x==4 AND t1.oid==t2.rowid}
  194. } {256}
  195. do_test rowid-4.2.2 {
  196. execsql {SELECT t2.y FROM t2, t1 WHERE t1.x==4 AND t1._rowid_==t2.rowid}
  197. } {256}
  198. do_test rowid-4.2.3 {
  199. execsql {SELECT t2.y FROM t2, t1 WHERE t1.x==4 AND t2.rowid==t1.rowid}
  200. } {256}
  201. do_test rowid-4.2.4 {
  202. execsql {SELECT t2.y FROM t2, t1 WHERE t2.rowid==t1.oid AND t1.x==4}
  203. } {256}
  204. do_test rowid-4.2.5 {
  205. execsql {SELECT t2.y FROM t1, t2 WHERE t1.x==4 AND t1._rowid_==t2.rowid}
  206. } {256}
  207. do_test rowid-4.2.6 {
  208. execsql {SELECT t2.y FROM t1, t2 WHERE t1.x==4 AND t2.rowid==t1.rowid}
  209. } {256}
  210. do_test rowid-4.2.7 {
  211. execsql {SELECT t2.y FROM t1, t2 WHERE t2.rowid==t1.oid AND t1.x==4}
  212. } {256}
  213. do_test rowid-4.3 {
  214. execsql {CREATE INDEX idxt1 ON t1(x)}
  215. execsql {SELECT t2.y FROM t1, t2 WHERE t1.x==4 AND t1.rowid==t2.rowid}
  216. } {256}
  217. do_test rowid-4.3.1 {
  218. execsql {SELECT t2.y FROM t1, t2 WHERE t1.x==4 AND t1._rowid_==t2.rowid}
  219. } {256}
  220. do_test rowid-4.3.2 {
  221. execsql {SELECT t2.y FROM t1, t2 WHERE t2.rowid==t1.oid AND 4==t1.x}
  222. } {256}
  223. do_test rowid-4.4 {
  224. execsql {SELECT t2.y FROM t2, t1 WHERE t1.x==4 AND t1.rowid==t2.rowid}
  225. } {256}
  226. do_test rowid-4.4.1 {
  227. execsql {SELECT t2.y FROM t2, t1 WHERE t1.x==4 AND t1._rowid_==t2.rowid}
  228. } {256}
  229. do_test rowid-4.4.2 {
  230. execsql {SELECT t2.y FROM t2, t1 WHERE t2.rowid==t1.oid AND 4==t1.x}
  231. } {256}
  232. do_test rowid-4.5 {
  233. execsql {CREATE INDEX idxt2 ON t2(y)}
  234. set sqlite_search_count 0
  235. concat [execsql {
  236. SELECT t1.x FROM t2, t1
  237. WHERE t2.y==256 AND t1.rowid==t2.rowid
  238. }] $sqlite_search_count
  239. } {4 3}
  240. do_test rowid-4.5.1 {
  241. set sqlite_search_count 0
  242. concat [execsql {
  243. SELECT t1.x FROM t2, t1
  244. WHERE t1.OID==t2.rowid AND t2.y==81
  245. }] $sqlite_search_count
  246. } {3 3}
  247. do_test rowid-4.6 {
  248. execsql {
  249. SELECT t1.x FROM t1, t2
  250. WHERE t2.y==256 AND t1.rowid==t2.rowid
  251. }
  252. } {4}
  253. do_test rowid-5.1 {
  254. execsql {DELETE FROM t1 WHERE _rowid_ IN (SELECT oid FROM t1 WHERE x>8)}
  255. execsql {SELECT max(x) FROM t1}
  256. } {8}
  257. # Make sure a "WHERE rowid=X" clause works when there is no ROWID of X.
  258. #
  259. do_test rowid-6.1 {
  260. execsql {
  261. SELECT x FROM t1
  262. }
  263. } {1 2 3 4 5 6 7 8}
  264. do_test rowid-6.2 {
  265. for {set ::norow 1} {1} {incr ::norow} {
  266. if {[execsql "SELECT x FROM t1 WHERE rowid=$::norow"]==""} break
  267. }
  268. execsql [subst {
  269. DELETE FROM t1 WHERE rowid=$::norow
  270. }]
  271. } {}
  272. do_test rowid-6.3 {
  273. execsql {
  274. SELECT x FROM t1
  275. }
  276. } {1 2 3 4 5 6 7 8}
  277. # Beginning with version 2.3.4, SQLite computes rowids of new rows by
  278. # finding the maximum current rowid and adding one. It falls back to
  279. # the old random algorithm if the maximum rowid is the largest integer.
  280. # The following tests are for this new behavior.
  281. #
  282. do_test rowid-7.0 {
  283. execsql {
  284. DELETE FROM t1;
  285. DROP TABLE t2;
  286. DROP INDEX idxt1;
  287. INSERT INTO t1 VALUES(1,2);
  288. SELECT rowid, * FROM t1;
  289. }
  290. } {1 1 2}
  291. do_test rowid-7.1 {
  292. execsql {
  293. INSERT INTO t1 VALUES(99,100);
  294. SELECT rowid,* FROM t1
  295. }
  296. } {1 1 2 2 99 100}
  297. do_test rowid-7.2 {
  298. execsql {
  299. CREATE TABLE t2(a INTEGER PRIMARY KEY, b);
  300. INSERT INTO t2(b) VALUES(55);
  301. SELECT * FROM t2;
  302. }
  303. } {1 55}
  304. do_test rowid-7.3 {
  305. execsql {
  306. INSERT INTO t2(b) VALUES(66);
  307. SELECT * FROM t2;
  308. }
  309. } {1 55 2 66}
  310. do_test rowid-7.4 {
  311. execsql {
  312. INSERT INTO t2(a,b) VALUES(1000000,77);
  313. INSERT INTO t2(b) VALUES(88);
  314. SELECT * FROM t2;
  315. }
  316. } {1 55 2 66 1000000 77 1000001 88}
  317. do_test rowid-7.5 {
  318. execsql {
  319. INSERT INTO t2(a,b) VALUES(2147483647,99);
  320. INSERT INTO t2(b) VALUES(11);
  321. SELECT b FROM t2 ORDER BY b;
  322. }
  323. } {11 55 66 77 88 99}
  324. do_test rowid-7.6 {
  325. execsql {
  326. SELECT b FROM t2 WHERE a NOT IN(1,2,1000000,1000001,2147483647);
  327. }
  328. } {11}
  329. do_test rowid-7.7 {
  330. execsql {
  331. INSERT INTO t2(b) VALUES(22);
  332. INSERT INTO t2(b) VALUES(33);
  333. INSERT INTO t2(b) VALUES(44);
  334. INSERT INTO t2(b) VALUES(55);
  335. SELECT b FROM t2 WHERE a NOT IN(1,2,1000000,1000001,2147483647) ORDER BY b;
  336. }
  337. } {11 22 33 44 55}
  338. do_test rowid-7.8 {
  339. execsql {
  340. DELETE FROM t2 WHERE a!=2;
  341. INSERT INTO t2(b) VALUES(111);
  342. SELECT * FROM t2;
  343. }
  344. } {2 66 3 111}
  345. finish_test