3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
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.
15 # EVIDENCE-OF: R-36924-43758 By default, every row in SQLite has a
16 # special column, usually called the "rowid", that uniquely identifies
17 # that row within the table.
19 set testdir [file dirname $argv0]
20 source $testdir/tester.tcl
22 # Basic ROWID functionality tests.
26 CREATE TABLE t1(x int, y int);
27 INSERT INTO t1 VALUES(1,2);
28 INSERT INTO t1 VALUES(3,4);
29 SELECT x FROM t1 ORDER BY y;
33 set r [execsql {SELECT rowid FROM t1 ORDER BY x}]
34 global x2rowid rowid2x
35 set x2rowid(1) [lindex $r 0]
36 set x2rowid(3) [lindex $r 1]
37 set rowid2x($x2rowid(1)) 1
38 set rowid2x($x2rowid(3)) 3
43 set sql "SELECT x FROM t1 WHERE rowid==$x2rowid(1)"
48 set sql "SELECT x FROM t1 WHERE rowid==$x2rowid(3)"
53 set sql "SELECT x FROM t1 WHERE oid==$x2rowid(1)"
58 set sql "SELECT x FROM t1 WHERE OID==$x2rowid(3)"
63 set sql "SELECT x FROM t1 WHERE _rowid_==$x2rowid(1)"
68 set norow [expr {int(rand()*1000000)}]
69 if {$norow!=$x2rowid(1) && $norow!=$x2rowid(3)} break
71 execsql "SELECT x FROM t1 WHERE rowid=$norow"
75 set v [execsql {SELECT x, oid FROM t1 order by x}]
76 set v2 [list 1 $x2rowid(1) 3 $x2rowid(3)]
81 set v [execsql {SELECT x, RowID FROM t1 order by x}]
82 set v2 [list 1 $x2rowid(1) 3 $x2rowid(3)]
87 set v [execsql {SELECT x, _rowid_ FROM t1 order by x}]
88 set v2 [list 1 $x2rowid(1) 3 $x2rowid(3)]
92 # We can insert or update the ROWID column.
96 INSERT INTO t1(rowid,x,y) VALUES(1234,5,6);
97 SELECT rowid, * FROM t1;
99 } {0 {1 1 2 2 3 4 1234 5 6}}
102 UPDATE t1 SET rowid=12345 WHERE x==1;
103 SELECT rowid, * FROM t1
105 } {0 {2 3 4 1234 5 6 12345 1 2}}
108 INSERT INTO t1(y,x,oid) VALUES(8,7,1235);
109 SELECT rowid, * FROM t1 WHERE rowid>1000;
111 } {0 {1234 5 6 1235 7 8 12345 1 2}}
114 UPDATE t1 SET oid=12346 WHERE x==1;
115 SELECT rowid, * FROM t1;
117 } {0 {2 3 4 1234 5 6 1235 7 8 12346 1 2}}
120 INSERT INTO t1(x,_rowid_,y) VALUES(9,1236,10);
121 SELECT rowid, * FROM t1 WHERE rowid>1000;
123 } {0 {1234 5 6 1235 7 8 1236 9 10 12346 1 2}}
126 UPDATE t1 SET _rowid_=12347 WHERE x==1;
127 SELECT rowid, * FROM t1 WHERE rowid>1000;
129 } {0 {1234 5 6 1235 7 8 1236 9 10 12347 1 2}}
131 # But we can use ROWID in the WHERE clause of an UPDATE that does not
136 set sql "UPDATE t1 SET x=2 WHERE OID==$x2rowid(3)"
138 execsql {SELECT x FROM t1 ORDER BY x}
142 set sql "UPDATE t1 SET x=3 WHERE _rowid_==$x2rowid(3)"
144 execsql {SELECT x FROM t1 ORDER BY x}
147 # We cannot index by ROWID
150 set v [catch {execsql {CREATE INDEX idxt1 ON t1(rowid)}} msg]
152 } {1 {table t1 has no column named rowid}}
154 set v [catch {execsql {CREATE INDEX idxt1 ON t1(_rowid_)}} msg]
156 } {1 {table t1 has no column named _rowid_}}
158 set v [catch {execsql {CREATE INDEX idxt1 ON t1(oid)}} msg]
160 } {1 {table t1 has no column named oid}}
162 set v [catch {execsql {CREATE INDEX idxt1 ON t1(x, rowid)}} msg]
164 } {1 {table t1 has no column named rowid}}
166 # Columns defined in the CREATE statement override the buildin ROWID
171 CREATE TABLE t2(rowid int, x int, y int);
172 INSERT INTO t2 VALUES(0,2,3);
173 INSERT INTO t2 VALUES(4,5,6);
174 INSERT INTO t2 VALUES(7,8,9);
175 SELECT * FROM t2 ORDER BY x;
177 } {0 2 3 4 5 6 7 8 9}
179 execsql {SELECT * FROM t2 ORDER BY rowid}
180 } {0 2 3 4 5 6 7 8 9}
182 execsql {SELECT rowid, x, y FROM t2 ORDER BY rowid}
183 } {0 2 3 4 5 6 7 8 9}
185 set r1 [execsql {SELECT _rowid_, rowid FROM t2 ORDER BY rowid}]
186 foreach {a b c d e f} $r1 {}
187 set r2 [execsql {SELECT _rowid_, rowid FROM t2 ORDER BY x DESC}]
188 foreach {u v w x y z} $r2 {}
189 expr {$u==$e && $w==$c && $y==$a}
191 # sqlite3 v3 - do_probtest doesn't exist anymore?
193 do_probtest rowid-3.5 {
194 set r1 [execsql {SELECT _rowid_, rowid FROM t2 ORDER BY rowid}]
195 foreach {a b c d e f} $r1 {}
196 expr {$a!=$b && $c!=$d && $e!=$f}
200 # Let's try some more complex examples, including some joins.
207 for {set i 1} {$i<=50} {incr i} {
208 execsql "INSERT INTO t1(x,y) VALUES($i,[expr {$i*$i}])"
210 execsql {INSERT INTO t2 SELECT _rowid_, x*y, y*y FROM t1}
211 execsql {SELECT t2.y FROM t1, t2 WHERE t1.x==4 AND t1.rowid==t2.rowid}
214 execsql {SELECT t2.y FROM t2, t1 WHERE t1.x==4 AND t1.rowid==t2.rowid}
216 do_test rowid-4.2.1 {
217 execsql {SELECT t2.y FROM t2, t1 WHERE t1.x==4 AND t1.oid==t2.rowid}
219 do_test rowid-4.2.2 {
220 execsql {SELECT t2.y FROM t2, t1 WHERE t1.x==4 AND t1._rowid_==t2.rowid}
222 do_test rowid-4.2.3 {
223 execsql {SELECT t2.y FROM t2, t1 WHERE t1.x==4 AND t2.rowid==t1.rowid}
225 do_test rowid-4.2.4 {
226 execsql {SELECT t2.y FROM t2, t1 WHERE t2.rowid==t1.oid AND t1.x==4}
228 do_test rowid-4.2.5 {
229 execsql {SELECT t2.y FROM t1, t2 WHERE t1.x==4 AND t1._rowid_==t2.rowid}
231 do_test rowid-4.2.6 {
232 execsql {SELECT t2.y FROM t1, t2 WHERE t1.x==4 AND t2.rowid==t1.rowid}
234 do_test rowid-4.2.7 {
235 execsql {SELECT t2.y FROM t1, t2 WHERE t2.rowid==t1.oid AND t1.x==4}
238 execsql {CREATE INDEX idxt1 ON t1(x)}
239 execsql {SELECT t2.y FROM t1, t2 WHERE t1.x==4 AND t1.rowid==t2.rowid}
241 do_test rowid-4.3.1 {
242 execsql {SELECT t2.y FROM t1, t2 WHERE t1.x==4 AND t1._rowid_==t2.rowid}
244 do_test rowid-4.3.2 {
245 execsql {SELECT t2.y FROM t1, t2 WHERE t2.rowid==t1.oid AND 4==t1.x}
248 execsql {SELECT t2.y FROM t2, t1 WHERE t1.x==4 AND t1.rowid==t2.rowid}
250 do_test rowid-4.4.1 {
251 execsql {SELECT t2.y FROM t2, t1 WHERE t1.x==4 AND t1._rowid_==t2.rowid}
253 do_test rowid-4.4.2 {
254 execsql {SELECT t2.y FROM t2, t1 WHERE t2.rowid==t1.oid AND 4==t1.x}
257 execsql {CREATE INDEX idxt2 ON t2(y)}
258 set sqlite_search_count 0
260 SELECT t1.x FROM t2, t1
261 WHERE t2.y==256 AND t1.rowid==t2.rowid
262 }] $sqlite_search_count
264 do_test rowid-4.5.1 {
265 set sqlite_search_count 0
267 SELECT t1.x FROM t2, t1
268 WHERE t1.OID==t2.rowid AND t2.y==81
269 }] $sqlite_search_count
273 SELECT t1.x FROM t1, t2
274 WHERE t2.y==256 AND t1.rowid==t2.rowid
278 do_test rowid-5.1.1 {
280 execsql {DELETE FROM t1 WHERE _rowid_ IN (SELECT oid FROM t1 WHERE x>8)}
282 set oids [execsql {SELECT oid FROM t1 WHERE x>8}]
283 set where "_rowid_ = [join $oids { OR _rowid_ = }]"
284 execsql "DELETE FROM t1 WHERE $where"
287 do_test rowid-5.1.2 {
288 execsql {SELECT max(x) FROM t1}
291 # Make sure a "WHERE rowid=X" clause works when there is no ROWID of X.
299 for {set ::norow 1} {1} {incr ::norow} {
300 if {[execsql "SELECT x FROM t1 WHERE rowid=$::norow"]==""} break
303 DELETE FROM t1 WHERE rowid=$::norow
312 # Beginning with version 2.3.4, SQLite computes rowids of new rows by
313 # finding the maximum current rowid and adding one. It falls back to
314 # the old random algorithm if the maximum rowid is the largest integer.
315 # The following tests are for this new behavior.
322 INSERT INTO t1 VALUES(1,2);
323 SELECT rowid, * FROM t1;
328 INSERT INTO t1 VALUES(99,100);
329 SELECT rowid,* FROM t1
334 CREATE TABLE t2(a INTEGER PRIMARY KEY, b);
335 INSERT INTO t2(b) VALUES(55);
341 INSERT INTO t2(b) VALUES(66);
347 INSERT INTO t2(a,b) VALUES(1000000,77);
348 INSERT INTO t2(b) VALUES(88);
351 } {1 55 2 66 1000000 77 1000001 88}
354 INSERT INTO t2(a,b) VALUES(2147483647,99);
355 INSERT INTO t2(b) VALUES(11);
356 SELECT b FROM t2 ORDER BY b;
358 } {11 55 66 77 88 99}
362 SELECT b FROM t2 WHERE a NOT IN(1,2,1000000,1000001,2147483647);
367 INSERT INTO t2(b) VALUES(22);
368 INSERT INTO t2(b) VALUES(33);
369 INSERT INTO t2(b) VALUES(44);
370 INSERT INTO t2(b) VALUES(55);
371 SELECT b FROM t2 WHERE a NOT IN(1,2,1000000,1000001,2147483647)
378 DELETE FROM t2 WHERE a!=2;
379 INSERT INTO t2(b) VALUES(111);
384 ifcapable {trigger} {
385 # Make sure AFTER triggers that do INSERTs do not change the last_insert_rowid.
390 CREATE TABLE t3(a integer primary key);
392 INSERT INTO t4 VALUES(1);
393 CREATE TRIGGER r3 AFTER INSERT on t3 FOR EACH ROW BEGIN
394 INSERT INTO t4 VALUES(NEW.a+10);
401 SELECT rowid, * FROM t4;
406 INSERT INTO t3 VALUES(123);
407 SELECT last_insert_rowid();
417 SELECT rowid, * FROM t4;
422 INSERT INTO t3 VALUES(NULL);
423 SELECT last_insert_rowid();
433 SELECT rowid, * FROM t4;
438 # If triggers are not enable, simulate their effect for the tests that
440 ifcapable {!trigger} {
442 CREATE TABLE t3(a integer primary key);
443 INSERT INTO t3 VALUES(123);
444 INSERT INTO t3 VALUES(124);
448 # ticket #377: Comparison between integer primiary key and floating point
453 SELECT * FROM t3 WHERE a<123.5
458 SELECT * FROM t3 WHERE a<124.5
463 SELECT * FROM t3 WHERE a>123.5
468 SELECT * FROM t3 WHERE a>122.5
473 SELECT * FROM t3 WHERE a==123.5
478 SELECT * FROM t3 WHERE a==123.000
483 SELECT * FROM t3 WHERE a>100.5 AND a<200.5
488 SELECT * FROM t3 WHERE a>'xyz';
493 SELECT * FROM t3 WHERE a<'xyz';
498 SELECT * FROM t3 WHERE a>=122.9 AND a<=123.1
502 # Ticket #567. Comparisons of ROWID or integery primary key against
503 # floating point numbers still do not always work.
508 INSERT INTO t5 VALUES(1);
509 INSERT INTO t5 VALUES(2);
510 INSERT INTO t5 SELECT a+2 FROM t5;
511 INSERT INTO t5 SELECT a+4 FROM t5;
512 SELECT rowid, * FROM t5;
514 } {1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8}
516 execsql {SELECT rowid, a FROM t5 WHERE rowid>=5.5}
519 execsql {SELECT rowid, a FROM t5 WHERE rowid>=5.0}
522 execsql {SELECT rowid, a FROM t5 WHERE rowid>5.5}
524 do_test rowid-10.3.2 {
525 execsql {SELECT rowid, a FROM t5 WHERE rowid>5.0}
528 execsql {SELECT rowid, a FROM t5 WHERE 5.5<=rowid}
531 execsql {SELECT rowid, a FROM t5 WHERE 5.5<rowid}
534 execsql {SELECT rowid, a FROM t5 WHERE rowid<=5.5}
535 } {1 1 2 2 3 3 4 4 5 5}
537 execsql {SELECT rowid, a FROM t5 WHERE rowid<5.5}
538 } {1 1 2 2 3 3 4 4 5 5}
540 execsql {SELECT rowid, a FROM t5 WHERE 5.5>=rowid}
541 } {1 1 2 2 3 3 4 4 5 5}
542 do_test rowid-10.10 {
543 execsql {SELECT rowid, a FROM t5 WHERE 5.5>rowid}
544 } {1 1 2 2 3 3 4 4 5 5}
545 do_test rowid-10.11 {
546 execsql {SELECT rowid, a FROM t5 WHERE rowid>=5.5 ORDER BY rowid DESC}
548 do_test rowid-10.11.2 {
549 execsql {SELECT rowid, a FROM t5 WHERE rowid>=5.0 ORDER BY rowid DESC}
551 do_test rowid-10.12 {
552 execsql {SELECT rowid, a FROM t5 WHERE rowid>5.5 ORDER BY rowid DESC}
554 do_test rowid-10.12.2 {
555 execsql {SELECT rowid, a FROM t5 WHERE rowid>5.0 ORDER BY rowid DESC}
557 do_test rowid-10.13 {
558 execsql {SELECT rowid, a FROM t5 WHERE 5.5<=rowid ORDER BY rowid DESC}
560 do_test rowid-10.14 {
561 execsql {SELECT rowid, a FROM t5 WHERE 5.5<rowid ORDER BY rowid DESC}
563 do_test rowid-10.15 {
564 execsql {SELECT rowid, a FROM t5 WHERE rowid<=5.5 ORDER BY rowid DESC}
565 } {5 5 4 4 3 3 2 2 1 1}
566 do_test rowid-10.16 {
567 execsql {SELECT rowid, a FROM t5 WHERE rowid<5.5 ORDER BY rowid DESC}
568 } {5 5 4 4 3 3 2 2 1 1}
569 do_test rowid-10.17 {
570 execsql {SELECT rowid, a FROM t5 WHERE 5.5>=rowid ORDER BY rowid DESC}
571 } {5 5 4 4 3 3 2 2 1 1}
572 do_test rowid-10.18 {
573 execsql {SELECT rowid, a FROM t5 WHERE 5.5>rowid ORDER BY rowid DESC}
574 } {5 5 4 4 3 3 2 2 1 1}
576 do_test rowid-10.30 {
579 INSERT INTO t6(rowid,a) SELECT -a,a FROM t5;
580 SELECT rowid, * FROM t6;
582 } {-8 8 -7 7 -6 6 -5 5 -4 4 -3 3 -2 2 -1 1}
583 do_test rowid-10.31.1 {
584 execsql {SELECT rowid, a FROM t6 WHERE rowid>=-5.5}
585 } {-5 5 -4 4 -3 3 -2 2 -1 1}
586 do_test rowid-10.31.2 {
587 execsql {SELECT rowid, a FROM t6 WHERE rowid>=-5.0}
588 } {-5 5 -4 4 -3 3 -2 2 -1 1}
589 do_test rowid-10.32.1 {
590 execsql {SELECT rowid, a FROM t6 WHERE rowid>=-5.5 ORDER BY rowid DESC}
591 } {-1 1 -2 2 -3 3 -4 4 -5 5}
592 do_test rowid-10.32.1 {
593 execsql {SELECT rowid, a FROM t6 WHERE rowid>=-5.0 ORDER BY rowid DESC}
594 } {-1 1 -2 2 -3 3 -4 4 -5 5}
595 do_test rowid-10.33 {
596 execsql {SELECT rowid, a FROM t6 WHERE -5.5<=rowid}
597 } {-5 5 -4 4 -3 3 -2 2 -1 1}
598 do_test rowid-10.34 {
599 execsql {SELECT rowid, a FROM t6 WHERE -5.5<=rowid ORDER BY rowid DESC}
600 } {-1 1 -2 2 -3 3 -4 4 -5 5}
601 do_test rowid-10.35.1 {
602 execsql {SELECT rowid, a FROM t6 WHERE rowid>-5.5}
603 } {-5 5 -4 4 -3 3 -2 2 -1 1}
604 do_test rowid-10.35.2 {
605 execsql {SELECT rowid, a FROM t6 WHERE rowid>-5.0}
606 } {-4 4 -3 3 -2 2 -1 1}
607 do_test rowid-10.36.1 {
608 execsql {SELECT rowid, a FROM t6 WHERE rowid>-5.5 ORDER BY rowid DESC}
609 } {-1 1 -2 2 -3 3 -4 4 -5 5}
610 do_test rowid-10.36.2 {
611 execsql {SELECT rowid, a FROM t6 WHERE rowid>-5.0 ORDER BY rowid DESC}
612 } {-1 1 -2 2 -3 3 -4 4}
613 do_test rowid-10.37 {
614 execsql {SELECT rowid, a FROM t6 WHERE -5.5<rowid}
615 } {-5 5 -4 4 -3 3 -2 2 -1 1}
616 do_test rowid-10.38 {
617 execsql {SELECT rowid, a FROM t6 WHERE -5.5<rowid ORDER BY rowid DESC}
618 } {-1 1 -2 2 -3 3 -4 4 -5 5}
619 do_test rowid-10.39 {
620 execsql {SELECT rowid, a FROM t6 WHERE rowid<=-5.5}
622 do_test rowid-10.40 {
623 execsql {SELECT rowid, a FROM t6 WHERE rowid<=-5.5 ORDER BY rowid DESC}
625 do_test rowid-10.41 {
626 execsql {SELECT rowid, a FROM t6 WHERE -5.5>=rowid}
628 do_test rowid-10.42 {
629 execsql {SELECT rowid, a FROM t6 WHERE -5.5>=rowid ORDER BY rowid DESC}
631 do_test rowid-10.43 {
632 execsql {SELECT rowid, a FROM t6 WHERE rowid<-5.5}
634 do_test rowid-10.44 {
635 execsql {SELECT rowid, a FROM t6 WHERE rowid<-5.5 ORDER BY rowid DESC}
637 do_test rowid-10.44 {
638 execsql {SELECT rowid, a FROM t6 WHERE -5.5>rowid}
640 do_test rowid-10.46 {
641 execsql {SELECT rowid, a FROM t6 WHERE -5.5>rowid ORDER BY rowid DESC}
644 # Comparison of rowid against string values.
647 execsql {SELECT rowid, a FROM t5 WHERE rowid>'abc'}
650 execsql {SELECT rowid, a FROM t5 WHERE rowid>='abc'}
653 execsql {SELECT rowid, a FROM t5 WHERE rowid<'abc'}
654 } {1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8}
656 execsql {SELECT rowid, a FROM t5 WHERE rowid<='abc'}
657 } {1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8}
659 # Test the automatic generation of rowids when the table already contains
660 # a rowid with the maximum value.
662 # Once the maximum rowid is taken, rowids are normally chosen at
663 # random. By by reseting the random number generator, we can cause
664 # the rowid guessing loop to collide with prior rowids, and test the
665 # loop out to its limit of 100 iterations. After 100 collisions, the
666 # rowid guesser gives up and reports SQLITE_FULL.
670 CREATE TABLE t7(x INTEGER PRIMARY KEY, y);
671 CREATE TABLE t7temp(a INTEGER PRIMARY KEY);
672 INSERT INTO t7 VALUES(9223372036854775807,'a');
681 INSERT INTO t7 VALUES(NULL,'b');
682 SELECT x, y FROM t7 ORDER BY x;
684 } {/\d+ b 9223372036854775807 a/}
685 execsql {INSERT INTO t7 VALUES(2,'y');}
686 for {set i 1} {$i<100} {incr i} {
687 do_test rowid-12.3.$i {
688 db eval {DELETE FROM t7temp; INSERT INTO t7temp VALUES(1);}
691 INSERT INTO t7 VALUES(NULL,'x');
692 SELECT count(*) FROM t7 WHERE y=='x';
697 db eval {DELETE FROM t7temp; INSERT INTO t7temp VALUES(1);}
700 INSERT INTO t7 VALUES(NULL,'x');
702 } {1 {database or disk is full}}
704 # INSERTs that happen inside of nested function calls are recorded
705 # by last_insert_rowid.
707 proc rowid_addrow_func {n} {
708 db eval {INSERT INTO t13(rowid,x) VALUES($n,$n*$n)}
709 return [db last_insert_rowid]
711 db function addrow rowid_addrow_func
712 do_execsql_test rowid-13.1 {
714 INSERT INTO t13(rowid,x) VALUES(1234,5);
715 SELECT rowid, x, addrow(rowid+1000), '|' FROM t13 LIMIT 3;
716 SELECT last_insert_rowid();
717 } {1234 5 2234 | 2234 4990756 3234 | 3234 10458756 4234 | 4234}