2 #pragma ident "%Z%%M% %I% %E% SMI"
6 # The author disclaims copyright to this source code. In place of
7 # a legal notice, here is a blessing:
9 # May you do good and not evil.
10 # May you find forgiveness for yourself and forgive others.
11 # May you share freely, never taking more than you give.
13 #***********************************************************************
14 # This file implements regression tests for SQLite library. The
15 # focus of this file is testing the magic ROWID column that is
16 # found on all tables.
18 # $Id: rowid.test,v 1.13 2004/01/14 21:59:24 drh Exp $
20 set testdir [file dirname $argv0]
21 source $testdir/tester.tcl
23 # Basic ROWID functionality tests.
27 CREATE TABLE t1(x int, y int);
28 INSERT INTO t1 VALUES(1,2);
29 INSERT INTO t1 VALUES(3,4);
30 SELECT x FROM t1 ORDER BY y;
34 set r [execsql {SELECT rowid FROM t1 ORDER BY x}]
35 global x2rowid rowid2x
36 set x2rowid(1) [lindex $r 0]
37 set x2rowid(3) [lindex $r 1]
38 set rowid2x($x2rowid(1)) 1
39 set rowid2x($x2rowid(3)) 3
44 set sql "SELECT x FROM t1 WHERE rowid==$x2rowid(1)"
49 set sql "SELECT x FROM t1 WHERE rowid==$x2rowid(3)"
54 set sql "SELECT x FROM t1 WHERE oid==$x2rowid(1)"
59 set sql "SELECT x FROM t1 WHERE OID==$x2rowid(3)"
64 set sql "SELECT x FROM t1 WHERE _rowid_==$x2rowid(1)"
69 set norow [expr {int(rand()*1000000)}]
70 if {$norow!=$x2rowid(1) && $norow!=$x2rowid(3)} break
72 execsql "SELECT x FROM t1 WHERE rowid=$norow"
76 set v [execsql {SELECT x, oid FROM t1 order by x}]
77 set v2 [list 1 $x2rowid(1) 3 $x2rowid(3)]
82 set v [execsql {SELECT x, RowID FROM t1 order by x}]
83 set v2 [list 1 $x2rowid(1) 3 $x2rowid(3)]
88 set v [execsql {SELECT x, _rowid_ FROM t1 order by x}]
89 set v2 [list 1 $x2rowid(1) 3 $x2rowid(3)]
93 # We can insert or update the ROWID column.
97 INSERT INTO t1(rowid,x,y) VALUES(1234,5,6);
98 SELECT rowid, * FROM t1;
100 } {0 {1 1 2 2 3 4 1234 5 6}}
103 UPDATE t1 SET rowid=12345 WHERE x==1;
104 SELECT rowid, * FROM t1
106 } {0 {2 3 4 1234 5 6 12345 1 2}}
109 INSERT INTO t1(y,x,oid) VALUES(8,7,1235);
110 SELECT rowid, * FROM t1 WHERE rowid>1000;
112 } {0 {1234 5 6 1235 7 8 12345 1 2}}
115 UPDATE t1 SET oid=12346 WHERE x==1;
116 SELECT rowid, * FROM t1;
118 } {0 {2 3 4 1234 5 6 1235 7 8 12346 1 2}}
121 INSERT INTO t1(x,_rowid_,y) VALUES(9,1236,10);
122 SELECT rowid, * FROM t1 WHERE rowid>1000;
124 } {0 {1234 5 6 1235 7 8 1236 9 10 12346 1 2}}
127 UPDATE t1 SET _rowid_=12347 WHERE x==1;
128 SELECT rowid, * FROM t1 WHERE rowid>1000;
130 } {0 {1234 5 6 1235 7 8 1236 9 10 12347 1 2}}
132 # But we can use ROWID in the WHERE clause of an UPDATE that does not
137 set sql "UPDATE t1 SET x=2 WHERE OID==$x2rowid(3)"
139 execsql {SELECT x FROM t1 ORDER BY x}
143 set sql "UPDATE t1 SET x=3 WHERE _rowid_==$x2rowid(3)"
145 execsql {SELECT x FROM t1 ORDER BY x}
148 # We cannot index by ROWID
151 set v [catch {execsql {CREATE INDEX idxt1 ON t1(rowid)}} msg]
153 } {1 {table t1 has no column named rowid}}
155 set v [catch {execsql {CREATE INDEX idxt1 ON t1(_rowid_)}} msg]
157 } {1 {table t1 has no column named _rowid_}}
159 set v [catch {execsql {CREATE INDEX idxt1 ON t1(oid)}} msg]
161 } {1 {table t1 has no column named oid}}
163 set v [catch {execsql {CREATE INDEX idxt1 ON t1(x, rowid)}} msg]
165 } {1 {table t1 has no column named rowid}}
167 # Columns defined in the CREATE statement override the buildin ROWID
172 CREATE TABLE t2(rowid int, x int, y int);
173 INSERT INTO t2 VALUES(0,2,3);
174 INSERT INTO t2 VALUES(4,5,6);
175 INSERT INTO t2 VALUES(7,8,9);
176 SELECT * FROM t2 ORDER BY x;
178 } {0 2 3 4 5 6 7 8 9}
180 execsql {SELECT * FROM t2 ORDER BY rowid}
181 } {0 2 3 4 5 6 7 8 9}
183 execsql {SELECT rowid, x, y FROM t2 ORDER BY rowid}
184 } {0 2 3 4 5 6 7 8 9}
186 set r1 [execsql {SELECT _rowid_, rowid FROM t2 ORDER BY rowid}]
187 foreach {a b c d e f} $r1 {}
188 set r2 [execsql {SELECT _rowid_, rowid FROM t2 ORDER BY x DESC}]
189 foreach {u v w x y z} $r2 {}
190 expr {$u==$e && $w==$c && $y==$a}
192 do_probtest rowid-3.5 {
193 set r1 [execsql {SELECT _rowid_, rowid FROM t2 ORDER BY rowid}]
194 foreach {a b c d e f} $r1 {}
195 expr {$a!=$b && $c!=$d && $e!=$f}
198 # Let's try some more complex examples, including some joins.
205 for {set i 1} {$i<=50} {incr i} {
206 execsql "INSERT INTO t1(x,y) VALUES($i,[expr {$i*$i}])"
208 execsql {INSERT INTO t2 SELECT _rowid_, x*y, y*y FROM t1}
209 execsql {SELECT t2.y FROM t1, t2 WHERE t1.x==4 AND t1.rowid==t2.rowid}
212 execsql {SELECT t2.y FROM t2, t1 WHERE t1.x==4 AND t1.rowid==t2.rowid}
214 do_test rowid-4.2.1 {
215 execsql {SELECT t2.y FROM t2, t1 WHERE t1.x==4 AND t1.oid==t2.rowid}
217 do_test rowid-4.2.2 {
218 execsql {SELECT t2.y FROM t2, t1 WHERE t1.x==4 AND t1._rowid_==t2.rowid}
220 do_test rowid-4.2.3 {
221 execsql {SELECT t2.y FROM t2, t1 WHERE t1.x==4 AND t2.rowid==t1.rowid}
223 do_test rowid-4.2.4 {
224 execsql {SELECT t2.y FROM t2, t1 WHERE t2.rowid==t1.oid AND t1.x==4}
226 do_test rowid-4.2.5 {
227 execsql {SELECT t2.y FROM t1, t2 WHERE t1.x==4 AND t1._rowid_==t2.rowid}
229 do_test rowid-4.2.6 {
230 execsql {SELECT t2.y FROM t1, t2 WHERE t1.x==4 AND t2.rowid==t1.rowid}
232 do_test rowid-4.2.7 {
233 execsql {SELECT t2.y FROM t1, t2 WHERE t2.rowid==t1.oid AND t1.x==4}
236 execsql {CREATE INDEX idxt1 ON t1(x)}
237 execsql {SELECT t2.y FROM t1, t2 WHERE t1.x==4 AND t1.rowid==t2.rowid}
239 do_test rowid-4.3.1 {
240 execsql {SELECT t2.y FROM t1, t2 WHERE t1.x==4 AND t1._rowid_==t2.rowid}
242 do_test rowid-4.3.2 {
243 execsql {SELECT t2.y FROM t1, t2 WHERE t2.rowid==t1.oid AND 4==t1.x}
246 execsql {SELECT t2.y FROM t2, t1 WHERE t1.x==4 AND t1.rowid==t2.rowid}
248 do_test rowid-4.4.1 {
249 execsql {SELECT t2.y FROM t2, t1 WHERE t1.x==4 AND t1._rowid_==t2.rowid}
251 do_test rowid-4.4.2 {
252 execsql {SELECT t2.y FROM t2, t1 WHERE t2.rowid==t1.oid AND 4==t1.x}
255 execsql {CREATE INDEX idxt2 ON t2(y)}
256 set sqlite_search_count 0
258 SELECT t1.x FROM t2, t1
259 WHERE t2.y==256 AND t1.rowid==t2.rowid
260 }] $sqlite_search_count
262 do_test rowid-4.5.1 {
263 set sqlite_search_count 0
265 SELECT t1.x FROM t2, t1
266 WHERE t1.OID==t2.rowid AND t2.y==81
267 }] $sqlite_search_count
271 SELECT t1.x FROM t1, t2
272 WHERE t2.y==256 AND t1.rowid==t2.rowid
277 execsql {DELETE FROM t1 WHERE _rowid_ IN (SELECT oid FROM t1 WHERE x>8)}
278 execsql {SELECT max(x) FROM t1}
281 # Make sure a "WHERE rowid=X" clause works when there is no ROWID of X.
289 for {set ::norow 1} {1} {incr ::norow} {
290 if {[execsql "SELECT x FROM t1 WHERE rowid=$::norow"]==""} break
293 DELETE FROM t1 WHERE rowid=$::norow
302 # Beginning with version 2.3.4, SQLite computes rowids of new rows by
303 # finding the maximum current rowid and adding one. It falls back to
304 # the old random algorithm if the maximum rowid is the largest integer.
305 # The following tests are for this new behavior.
312 INSERT INTO t1 VALUES(1,2);
313 SELECT rowid, * FROM t1;
318 INSERT INTO t1 VALUES(99,100);
319 SELECT rowid,* FROM t1
324 CREATE TABLE t2(a INTEGER PRIMARY KEY, b);
325 INSERT INTO t2(b) VALUES(55);
331 INSERT INTO t2(b) VALUES(66);
337 INSERT INTO t2(a,b) VALUES(1000000,77);
338 INSERT INTO t2(b) VALUES(88);
341 } {1 55 2 66 1000000 77 1000001 88}
344 INSERT INTO t2(a,b) VALUES(2147483647,99);
345 INSERT INTO t2(b) VALUES(11);
346 SELECT b FROM t2 ORDER BY b;
348 } {11 55 66 77 88 99}
351 SELECT b FROM t2 WHERE a NOT IN(1,2,1000000,1000001,2147483647);
356 INSERT INTO t2(b) VALUES(22);
357 INSERT INTO t2(b) VALUES(33);
358 INSERT INTO t2(b) VALUES(44);
359 INSERT INTO t2(b) VALUES(55);
360 SELECT b FROM t2 WHERE a NOT IN(1,2,1000000,1000001,2147483647) ORDER BY b;
365 DELETE FROM t2 WHERE a!=2;
366 INSERT INTO t2(b) VALUES(111);
371 # Make sure AFTER triggers that do INSERTs do not change the last_insert_rowid.
376 CREATE TABLE t3(a integer primary key);
378 INSERT INTO t4 VALUES(1);
379 CREATE TRIGGER r3 AFTER INSERT on t3 FOR EACH ROW BEGIN
380 INSERT INTO t4 VALUES(NEW.a+10);
387 SELECT rowid, * FROM t4;
392 INSERT INTO t3 VALUES(123);
393 SELECT last_insert_rowid();
403 SELECT rowid, * FROM t4;
408 INSERT INTO t3 VALUES(NULL);
409 SELECT last_insert_rowid();
419 SELECT rowid, * FROM t4;
423 # ticket #377: Comparison between integer primiary key and floating point
428 SELECT * FROM t3 WHERE a<123.5
433 SELECT * FROM t3 WHERE a<124.5
438 SELECT * FROM t3 WHERE a>123.5
443 SELECT * FROM t3 WHERE a>122.5
448 SELECT * FROM t3 WHERE a==123.5
453 SELECT * FROM t3 WHERE a==123.000
458 SELECT * FROM t3 WHERE a>100.5 AND a<200.5
463 SELECT * FROM t3 WHERE a>'xyz';
468 SELECT * FROM t3 WHERE a<'xyz';
473 SELECT * FROM t3 WHERE a>=122.9 AND a<=123.1
477 # Ticket #567. Comparisons of ROWID or integery primary key against
478 # floating point numbers still do not always work.
483 INSERT INTO t5 VALUES(1);
484 INSERT INTO t5 VALUES(2);
485 INSERT INTO t5 SELECT a+2 FROM t5;
486 INSERT INTO t5 SELECT a+4 FROM t5;
487 SELECT rowid, * FROM t5;
489 } {1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8}
491 execsql {SELECT rowid, a FROM t5 WHERE rowid>=5.5}
494 execsql {SELECT rowid, a FROM t5 WHERE rowid>=5.0}
497 execsql {SELECT rowid, a FROM t5 WHERE rowid>5.5}
499 do_test rowid-10.3.2 {
500 execsql {SELECT rowid, a FROM t5 WHERE rowid>5.0}
503 execsql {SELECT rowid, a FROM t5 WHERE 5.5<=rowid}
506 execsql {SELECT rowid, a FROM t5 WHERE 5.5<rowid}
509 execsql {SELECT rowid, a FROM t5 WHERE rowid<=5.5}
510 } {1 1 2 2 3 3 4 4 5 5}
512 execsql {SELECT rowid, a FROM t5 WHERE rowid<5.5}
513 } {1 1 2 2 3 3 4 4 5 5}
515 execsql {SELECT rowid, a FROM t5 WHERE 5.5>=rowid}
516 } {1 1 2 2 3 3 4 4 5 5}
517 do_test rowid-10.10 {
518 execsql {SELECT rowid, a FROM t5 WHERE 5.5>rowid}
519 } {1 1 2 2 3 3 4 4 5 5}
520 do_test rowid-10.11 {
521 execsql {SELECT rowid, a FROM t5 WHERE rowid>=5.5 ORDER BY rowid DESC}
523 do_test rowid-10.11.2 {
524 execsql {SELECT rowid, a FROM t5 WHERE rowid>=5.0 ORDER BY rowid DESC}
526 do_test rowid-10.12 {
527 execsql {SELECT rowid, a FROM t5 WHERE rowid>5.5 ORDER BY rowid DESC}
529 do_test rowid-10.12.2 {
530 execsql {SELECT rowid, a FROM t5 WHERE rowid>5.0 ORDER BY rowid DESC}
532 do_test rowid-10.13 {
533 execsql {SELECT rowid, a FROM t5 WHERE 5.5<=rowid ORDER BY rowid DESC}
535 do_test rowid-10.14 {
536 execsql {SELECT rowid, a FROM t5 WHERE 5.5<rowid ORDER BY rowid DESC}
538 do_test rowid-10.15 {
539 execsql {SELECT rowid, a FROM t5 WHERE rowid<=5.5 ORDER BY rowid DESC}
540 } {5 5 4 4 3 3 2 2 1 1}
541 do_test rowid-10.16 {
542 execsql {SELECT rowid, a FROM t5 WHERE rowid<5.5 ORDER BY rowid DESC}
543 } {5 5 4 4 3 3 2 2 1 1}
544 do_test rowid-10.17 {
545 execsql {SELECT rowid, a FROM t5 WHERE 5.5>=rowid ORDER BY rowid DESC}
546 } {5 5 4 4 3 3 2 2 1 1}
547 do_test rowid-10.18 {
548 execsql {SELECT rowid, a FROM t5 WHERE 5.5>rowid ORDER BY rowid DESC}
549 } {5 5 4 4 3 3 2 2 1 1}
551 do_test rowid-10.30 {
554 INSERT INTO t6(rowid,a) SELECT -a,a FROM t5;
555 SELECT rowid, * FROM t6;
557 } {-8 8 -7 7 -6 6 -5 5 -4 4 -3 3 -2 2 -1 1}
558 do_test rowid-10.31.1 {
559 execsql {SELECT rowid, a FROM t6 WHERE rowid>=-5.5}
560 } {-5 5 -4 4 -3 3 -2 2 -1 1}
561 do_test rowid-10.31.2 {
562 execsql {SELECT rowid, a FROM t6 WHERE rowid>=-5.0}
563 } {-5 5 -4 4 -3 3 -2 2 -1 1}
564 do_test rowid-10.32.1 {
565 execsql {SELECT rowid, a FROM t6 WHERE rowid>=-5.5 ORDER BY rowid DESC}
566 } {-1 1 -2 2 -3 3 -4 4 -5 5}
567 do_test rowid-10.32.1 {
568 execsql {SELECT rowid, a FROM t6 WHERE rowid>=-5.0 ORDER BY rowid DESC}
569 } {-1 1 -2 2 -3 3 -4 4 -5 5}
570 do_test rowid-10.33 {
571 execsql {SELECT rowid, a FROM t6 WHERE -5.5<=rowid}
572 } {-5 5 -4 4 -3 3 -2 2 -1 1}
573 do_test rowid-10.34 {
574 execsql {SELECT rowid, a FROM t6 WHERE -5.5<=rowid ORDER BY rowid DESC}
575 } {-1 1 -2 2 -3 3 -4 4 -5 5}
576 do_test rowid-10.35.1 {
577 execsql {SELECT rowid, a FROM t6 WHERE rowid>-5.5}
578 } {-5 5 -4 4 -3 3 -2 2 -1 1}
579 do_test rowid-10.35.2 {
580 execsql {SELECT rowid, a FROM t6 WHERE rowid>-5.0}
581 } {-4 4 -3 3 -2 2 -1 1}
582 do_test rowid-10.36.1 {
583 execsql {SELECT rowid, a FROM t6 WHERE rowid>-5.5 ORDER BY rowid DESC}
584 } {-1 1 -2 2 -3 3 -4 4 -5 5}
585 do_test rowid-10.36.2 {
586 execsql {SELECT rowid, a FROM t6 WHERE rowid>-5.0 ORDER BY rowid DESC}
587 } {-1 1 -2 2 -3 3 -4 4}
588 do_test rowid-10.37 {
589 execsql {SELECT rowid, a FROM t6 WHERE -5.5<rowid}
590 } {-5 5 -4 4 -3 3 -2 2 -1 1}
591 do_test rowid-10.38 {
592 execsql {SELECT rowid, a FROM t6 WHERE -5.5<rowid ORDER BY rowid DESC}
593 } {-1 1 -2 2 -3 3 -4 4 -5 5}
594 do_test rowid-10.39 {
595 execsql {SELECT rowid, a FROM t6 WHERE rowid<=-5.5}
597 do_test rowid-10.40 {
598 execsql {SELECT rowid, a FROM t6 WHERE rowid<=-5.5 ORDER BY rowid DESC}
600 do_test rowid-10.41 {
601 execsql {SELECT rowid, a FROM t6 WHERE -5.5>=rowid}
603 do_test rowid-10.42 {
604 execsql {SELECT rowid, a FROM t6 WHERE -5.5>=rowid ORDER BY rowid DESC}
606 do_test rowid-10.43 {
607 execsql {SELECT rowid, a FROM t6 WHERE rowid<-5.5}
609 do_test rowid-10.44 {
610 execsql {SELECT rowid, a FROM t6 WHERE rowid<-5.5 ORDER BY rowid DESC}
612 do_test rowid-10.44 {
613 execsql {SELECT rowid, a FROM t6 WHERE -5.5>rowid}
615 do_test rowid-10.46 {
616 execsql {SELECT rowid, a FROM t6 WHERE -5.5>rowid ORDER BY rowid DESC}
619 # Comparison of rowid against string values.
622 execsql {SELECT rowid, a FROM t5 WHERE rowid>'abc'}
625 execsql {SELECT rowid, a FROM t5 WHERE rowid>='abc'}
628 execsql {SELECT rowid, a FROM t5 WHERE rowid<'abc'}
629 } {1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8}
631 execsql {SELECT rowid, a FROM t5 WHERE rowid<='abc'}
632 } {1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8}