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 if 0 { # With the index-on-expressions enhancement, creating
148 # an index on ROWID has become possible.
149 # We cannot index by ROWID
152 set v [catch {execsql {CREATE INDEX idxt1 ON t1(rowid)}} msg]
154 } {1 {table t1 has no column named rowid}}
156 set v [catch {execsql {CREATE INDEX idxt1 ON t1(_rowid_)}} msg]
158 } {1 {table t1 has no column named _rowid_}}
160 set v [catch {execsql {CREATE INDEX idxt1 ON t1(oid)}} msg]
162 } {1 {table t1 has no column named oid}}
164 set v [catch {execsql {CREATE INDEX idxt1 ON t1(x, rowid)}} msg]
166 } {1 {table t1 has no column named rowid}}
169 # Columns defined in the CREATE statement override the buildin ROWID
174 CREATE TABLE t2(rowid int, x int, y int);
175 INSERT INTO t2 VALUES(0,2,3);
176 INSERT INTO t2 VALUES(4,5,6);
177 INSERT INTO t2 VALUES(7,8,9);
178 SELECT * FROM t2 ORDER BY x;
180 } {0 2 3 4 5 6 7 8 9}
182 execsql {SELECT * FROM t2 ORDER BY rowid}
183 } {0 2 3 4 5 6 7 8 9}
185 execsql {SELECT rowid, x, y FROM t2 ORDER BY rowid}
186 } {0 2 3 4 5 6 7 8 9}
188 set r1 [execsql {SELECT _rowid_, rowid FROM t2 ORDER BY rowid}]
189 foreach {a b c d e f} $r1 {}
190 set r2 [execsql {SELECT _rowid_, rowid FROM t2 ORDER BY x DESC}]
191 foreach {u v w x y z} $r2 {}
192 expr {$u==$e && $w==$c && $y==$a}
194 # sqlite3 v3 - do_probtest doesn't exist anymore?
196 do_probtest rowid-3.5 {
197 set r1 [execsql {SELECT _rowid_, rowid FROM t2 ORDER BY rowid}]
198 foreach {a b c d e f} $r1 {}
199 expr {$a!=$b && $c!=$d && $e!=$f}
203 # Let's try some more complex examples, including some joins.
210 for {set i 1} {$i<=50} {incr i} {
211 execsql "INSERT INTO t1(x,y) VALUES($i,[expr {$i*$i}])"
213 execsql {INSERT INTO t2 SELECT _rowid_, x*y, y*y FROM t1}
214 execsql {SELECT t2.y FROM t1, t2 WHERE t1.x==4 AND t1.rowid==t2.rowid}
217 execsql {SELECT t2.y FROM t2, t1 WHERE t1.x==4 AND t1.rowid==t2.rowid}
219 do_test rowid-4.2.1 {
220 execsql {SELECT t2.y FROM t2, t1 WHERE t1.x==4 AND t1.oid==t2.rowid}
222 do_test rowid-4.2.2 {
223 execsql {SELECT t2.y FROM t2, t1 WHERE t1.x==4 AND t1._rowid_==t2.rowid}
225 do_test rowid-4.2.3 {
226 execsql {SELECT t2.y FROM t2, t1 WHERE t1.x==4 AND t2.rowid==t1.rowid}
228 do_test rowid-4.2.4 {
229 execsql {SELECT t2.y FROM t2, t1 WHERE t2.rowid==t1.oid AND t1.x==4}
231 do_test rowid-4.2.5 {
232 execsql {SELECT t2.y FROM t1, t2 WHERE t1.x==4 AND t1._rowid_==t2.rowid}
234 do_test rowid-4.2.6 {
235 execsql {SELECT t2.y FROM t1, t2 WHERE t1.x==4 AND t2.rowid==t1.rowid}
237 do_test rowid-4.2.7 {
238 execsql {SELECT t2.y FROM t1, t2 WHERE t2.rowid==t1.oid AND t1.x==4}
241 execsql {CREATE INDEX idxt1 ON t1(x)}
242 execsql {SELECT t2.y FROM t1, t2 WHERE t1.x==4 AND t1.rowid==t2.rowid}
244 do_test rowid-4.3.1 {
245 execsql {SELECT t2.y FROM t1, t2 WHERE t1.x==4 AND t1._rowid_==t2.rowid}
247 do_test rowid-4.3.2 {
248 execsql {SELECT t2.y FROM t1, t2 WHERE t2.rowid==t1.oid AND 4==t1.x}
251 execsql {SELECT t2.y FROM t2, t1 WHERE t1.x==4 AND t1.rowid==t2.rowid}
253 do_test rowid-4.4.1 {
254 execsql {SELECT t2.y FROM t2, t1 WHERE t1.x==4 AND t1._rowid_==t2.rowid}
256 do_test rowid-4.4.2 {
257 execsql {SELECT t2.y FROM t2, t1 WHERE t2.rowid==t1.oid AND 4==t1.x}
260 execsql {CREATE INDEX idxt2 ON t2(y)}
261 set sqlite_search_count 0
263 SELECT t1.x FROM t2, t1
264 WHERE t2.y==256 AND t1.rowid==t2.rowid
265 }] $sqlite_search_count
267 do_test rowid-4.5.1 {
268 set sqlite_search_count 0
270 SELECT t1.x FROM t2, t1
271 WHERE t1.OID==t2.rowid AND t2.y==81
272 }] $sqlite_search_count
276 SELECT t1.x FROM t1, t2
277 WHERE t2.y==256 AND t1.rowid==t2.rowid
281 do_test rowid-5.1.1 {
283 execsql {DELETE FROM t1 WHERE _rowid_ IN (SELECT oid FROM t1 WHERE x>8)}
285 set oids [execsql {SELECT oid FROM t1 WHERE x>8}]
286 set where "_rowid_ = [join $oids { OR _rowid_ = }]"
287 execsql "DELETE FROM t1 WHERE $where"
290 do_test rowid-5.1.2 {
291 execsql {SELECT max(x) FROM t1}
294 # Make sure a "WHERE rowid=X" clause works when there is no ROWID of X.
302 for {set ::norow 1} {1} {incr ::norow} {
303 if {[execsql "SELECT x FROM t1 WHERE rowid=$::norow"]==""} break
306 DELETE FROM t1 WHERE rowid=$::norow
315 # Beginning with version 2.3.4, SQLite computes rowids of new rows by
316 # finding the maximum current rowid and adding one. It falls back to
317 # the old random algorithm if the maximum rowid is the largest integer.
318 # The following tests are for this new behavior.
325 INSERT INTO t1 VALUES(1,2);
326 SELECT rowid, * FROM t1;
331 INSERT INTO t1 VALUES(99,100);
332 SELECT rowid,* FROM t1
337 CREATE TABLE t2(a INTEGER PRIMARY KEY, b);
338 INSERT INTO t2(b) VALUES(55);
344 INSERT INTO t2(b) VALUES(66);
350 INSERT INTO t2(a,b) VALUES(1000000,77);
351 INSERT INTO t2(b) VALUES(88);
354 } {1 55 2 66 1000000 77 1000001 88}
357 INSERT INTO t2(a,b) VALUES(2147483647,99);
358 INSERT INTO t2(b) VALUES(11);
359 SELECT b FROM t2 ORDER BY b;
361 } {11 55 66 77 88 99}
365 SELECT b FROM t2 WHERE a NOT IN(1,2,1000000,1000001,2147483647);
370 INSERT INTO t2(b) VALUES(22);
371 INSERT INTO t2(b) VALUES(33);
372 INSERT INTO t2(b) VALUES(44);
373 INSERT INTO t2(b) VALUES(55);
374 SELECT b FROM t2 WHERE a NOT IN(1,2,1000000,1000001,2147483647)
381 DELETE FROM t2 WHERE a!=2;
382 INSERT INTO t2(b) VALUES(111);
387 ifcapable {trigger} {
388 # Make sure AFTER triggers that do INSERTs do not change the last_insert_rowid.
393 CREATE TABLE t3(a integer primary key);
395 INSERT INTO t4 VALUES(1);
396 CREATE TRIGGER r3 AFTER INSERT on t3 FOR EACH ROW BEGIN
397 INSERT INTO t4 VALUES(NEW.a+10);
404 SELECT rowid, * FROM t4;
409 INSERT INTO t3 VALUES(123);
410 SELECT last_insert_rowid();
420 SELECT rowid, * FROM t4;
425 INSERT INTO t3 VALUES(NULL);
426 SELECT last_insert_rowid();
436 SELECT rowid, * FROM t4;
441 # If triggers are not enable, simulate their effect for the tests that
443 ifcapable {!trigger} {
445 CREATE TABLE t3(a integer primary key);
446 INSERT INTO t3 VALUES(123);
447 INSERT INTO t3 VALUES(124);
451 # ticket #377: Comparison between integer primiary key and floating point
456 SELECT * FROM t3 WHERE a<123.5
461 SELECT * FROM t3 WHERE a<124.5
466 SELECT * FROM t3 WHERE a>123.5
471 SELECT * FROM t3 WHERE a>122.5
476 SELECT * FROM t3 WHERE a==123.5
481 SELECT * FROM t3 WHERE a==123.000
486 SELECT * FROM t3 WHERE a>100.5 AND a<200.5
491 SELECT * FROM t3 WHERE a>'xyz';
496 SELECT * FROM t3 WHERE a<'xyz';
501 SELECT * FROM t3 WHERE a>=122.9 AND a<=123.1
505 # Ticket #567. Comparisons of ROWID or integery primary key against
506 # floating point numbers still do not always work.
511 INSERT INTO t5 VALUES(1);
512 INSERT INTO t5 VALUES(2);
513 INSERT INTO t5 SELECT a+2 FROM t5;
514 INSERT INTO t5 SELECT a+4 FROM t5;
515 SELECT rowid, * FROM t5;
517 } {1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8}
519 execsql {SELECT rowid, a FROM t5 WHERE rowid>=5.5}
522 execsql {SELECT rowid, a FROM t5 WHERE rowid>=5.0}
525 execsql {SELECT rowid, a FROM t5 WHERE rowid>5.5}
527 do_test rowid-10.3.2 {
528 execsql {SELECT rowid, a FROM t5 WHERE rowid>5.0}
531 execsql {SELECT rowid, a FROM t5 WHERE 5.5<=rowid}
534 execsql {SELECT rowid, a FROM t5 WHERE 5.5<rowid}
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 rowid<5.5}
541 } {1 1 2 2 3 3 4 4 5 5}
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.10 {
546 execsql {SELECT rowid, a FROM t5 WHERE 5.5>rowid}
547 } {1 1 2 2 3 3 4 4 5 5}
548 do_test rowid-10.11 {
549 execsql {SELECT rowid, a FROM t5 WHERE rowid>=5.5 ORDER BY rowid DESC}
551 do_test rowid-10.11.2 {
552 execsql {SELECT rowid, a FROM t5 WHERE rowid>=5.0 ORDER BY rowid DESC}
554 do_test rowid-10.12 {
555 execsql {SELECT rowid, a FROM t5 WHERE rowid>5.5 ORDER BY rowid DESC}
557 do_test rowid-10.12.2 {
558 execsql {SELECT rowid, a FROM t5 WHERE rowid>5.0 ORDER BY rowid DESC}
560 do_test rowid-10.13 {
561 execsql {SELECT rowid, a FROM t5 WHERE 5.5<=rowid ORDER BY rowid DESC}
563 do_test rowid-10.14 {
564 execsql {SELECT rowid, a FROM t5 WHERE 5.5<rowid ORDER BY rowid DESC}
566 do_test rowid-10.15 {
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.16 {
570 execsql {SELECT rowid, a FROM t5 WHERE rowid<5.5 ORDER BY rowid DESC}
571 } {5 5 4 4 3 3 2 2 1 1}
572 do_test rowid-10.17 {
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}
575 do_test rowid-10.18 {
576 execsql {SELECT rowid, a FROM t5 WHERE 5.5>rowid ORDER BY rowid DESC}
577 } {5 5 4 4 3 3 2 2 1 1}
579 do_test rowid-10.30 {
582 INSERT INTO t6(rowid,a) SELECT -a,a FROM t5;
583 SELECT rowid, * FROM t6;
585 } {-8 8 -7 7 -6 6 -5 5 -4 4 -3 3 -2 2 -1 1}
586 do_test rowid-10.31.1 {
587 execsql {SELECT rowid, a FROM t6 WHERE rowid>=-5.5}
588 } {-5 5 -4 4 -3 3 -2 2 -1 1}
589 do_test rowid-10.31.2 {
590 execsql {SELECT rowid, a FROM t6 WHERE rowid>=-5.0}
591 } {-5 5 -4 4 -3 3 -2 2 -1 1}
592 do_test rowid-10.32.1 {
593 execsql {SELECT rowid, a FROM t6 WHERE rowid>=-5.5 ORDER BY rowid DESC}
594 } {-1 1 -2 2 -3 3 -4 4 -5 5}
595 do_test rowid-10.32.1 {
596 execsql {SELECT rowid, a FROM t6 WHERE rowid>=-5.0 ORDER BY rowid DESC}
597 } {-1 1 -2 2 -3 3 -4 4 -5 5}
598 do_test rowid-10.33 {
599 execsql {SELECT rowid, a FROM t6 WHERE -5.5<=rowid}
600 } {-5 5 -4 4 -3 3 -2 2 -1 1}
601 do_test rowid-10.34 {
602 execsql {SELECT rowid, a FROM t6 WHERE -5.5<=rowid ORDER BY rowid DESC}
603 } {-1 1 -2 2 -3 3 -4 4 -5 5}
604 do_test rowid-10.35.1 {
605 execsql {SELECT rowid, a FROM t6 WHERE rowid>-5.5}
606 } {-5 5 -4 4 -3 3 -2 2 -1 1}
607 do_test rowid-10.35.2 {
608 execsql {SELECT rowid, a FROM t6 WHERE rowid>-5.0}
609 } {-4 4 -3 3 -2 2 -1 1}
610 do_test rowid-10.36.1 {
611 execsql {SELECT rowid, a FROM t6 WHERE rowid>-5.5 ORDER BY rowid DESC}
612 } {-1 1 -2 2 -3 3 -4 4 -5 5}
613 do_test rowid-10.36.2 {
614 execsql {SELECT rowid, a FROM t6 WHERE rowid>-5.0 ORDER BY rowid DESC}
615 } {-1 1 -2 2 -3 3 -4 4}
616 do_test rowid-10.37 {
617 execsql {SELECT rowid, a FROM t6 WHERE -5.5<rowid}
618 } {-5 5 -4 4 -3 3 -2 2 -1 1}
619 do_test rowid-10.38 {
620 execsql {SELECT rowid, a FROM t6 WHERE -5.5<rowid ORDER BY rowid DESC}
621 } {-1 1 -2 2 -3 3 -4 4 -5 5}
622 do_test rowid-10.39 {
623 execsql {SELECT rowid, a FROM t6 WHERE rowid<=-5.5}
625 do_test rowid-10.40 {
626 execsql {SELECT rowid, a FROM t6 WHERE rowid<=-5.5 ORDER BY rowid DESC}
628 do_test rowid-10.41 {
629 execsql {SELECT rowid, a FROM t6 WHERE -5.5>=rowid}
631 do_test rowid-10.42 {
632 execsql {SELECT rowid, a FROM t6 WHERE -5.5>=rowid ORDER BY rowid DESC}
634 do_test rowid-10.43 {
635 execsql {SELECT rowid, a FROM t6 WHERE rowid<-5.5}
637 do_test rowid-10.44 {
638 execsql {SELECT rowid, a FROM t6 WHERE rowid<-5.5 ORDER BY rowid DESC}
640 do_test rowid-10.44 {
641 execsql {SELECT rowid, a FROM t6 WHERE -5.5>rowid}
643 do_test rowid-10.46 {
644 execsql {SELECT rowid, a FROM t6 WHERE -5.5>rowid ORDER BY rowid DESC}
647 # Comparison of rowid against string values.
650 execsql {SELECT rowid, a FROM t5 WHERE rowid>'abc'}
653 execsql {SELECT rowid, a FROM t5 WHERE rowid>='abc'}
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 execsql {SELECT rowid, a FROM t5 WHERE rowid<='abc'}
660 } {1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8}
662 # Test the automatic generation of rowids when the table already contains
663 # a rowid with the maximum value.
665 # Once the maximum rowid is taken, rowids are normally chosen at
666 # random. By by reseting the random number generator, we can cause
667 # the rowid guessing loop to collide with prior rowids, and test the
668 # loop out to its limit of 100 iterations. After 100 collisions, the
669 # rowid guesser gives up and reports SQLITE_FULL.
673 CREATE TABLE t7(x INTEGER PRIMARY KEY, y);
674 CREATE TABLE t7temp(a INTEGER PRIMARY KEY);
675 INSERT INTO t7 VALUES(9223372036854775807,'a');
684 INSERT INTO t7 VALUES(NULL,'b');
685 SELECT x, y FROM t7 ORDER BY x;
687 } {/\d+ b 9223372036854775807 a/}
688 execsql {INSERT INTO t7 VALUES(2,'y');}
689 for {set i 1} {$i<100} {incr i} {
690 do_test rowid-12.3.$i {
691 db eval {DELETE FROM t7temp; INSERT INTO t7temp VALUES(1);}
694 INSERT INTO t7 VALUES(NULL,'x');
695 SELECT count(*) FROM t7 WHERE y=='x';
700 db eval {DELETE FROM t7temp; INSERT INTO t7temp VALUES(1);}
703 INSERT INTO t7 VALUES(NULL,'x');
705 } {1 {database or disk is full}}
707 # INSERTs that happen inside of nested function calls are recorded
708 # by last_insert_rowid.
710 proc rowid_addrow_func {n} {
711 db eval {INSERT INTO t13(rowid,x) VALUES($n,$n*$n)}
712 return [db last_insert_rowid]
714 db function addrow rowid_addrow_func
715 do_execsql_test rowid-13.1 {
717 INSERT INTO t13(rowid,x) VALUES(1234,5);
718 SELECT rowid, x, addrow(rowid+1000), '|' FROM t13 LIMIT 3;
719 SELECT last_insert_rowid();
720 } {1234 5 2234 | 2234 4990756 3234 | 3234 10458756 4234 | 4234}