xref: /titanic_50/usr/src/lib/libsqlite/test/rowid.test (revision c5c4113dfcabb1eed3d4bdf7609de5170027a794)
1*c5c4113dSnw141292
2*c5c4113dSnw141292#pragma ident	"%Z%%M%	%I%	%E% SMI"
3*c5c4113dSnw141292
4*c5c4113dSnw141292# 2001 September 15
5*c5c4113dSnw141292#
6*c5c4113dSnw141292# The author disclaims copyright to this source code.  In place of
7*c5c4113dSnw141292# a legal notice, here is a blessing:
8*c5c4113dSnw141292#
9*c5c4113dSnw141292#    May you do good and not evil.
10*c5c4113dSnw141292#    May you find forgiveness for yourself and forgive others.
11*c5c4113dSnw141292#    May you share freely, never taking more than you give.
12*c5c4113dSnw141292#
13*c5c4113dSnw141292#***********************************************************************
14*c5c4113dSnw141292# This file implements regression tests for SQLite library.  The
15*c5c4113dSnw141292# focus of this file is testing the magic ROWID column that is
16*c5c4113dSnw141292# found on all tables.
17*c5c4113dSnw141292#
18*c5c4113dSnw141292# $Id: rowid.test,v 1.13 2004/01/14 21:59:24 drh Exp $
19*c5c4113dSnw141292
20*c5c4113dSnw141292set testdir [file dirname $argv0]
21*c5c4113dSnw141292source $testdir/tester.tcl
22*c5c4113dSnw141292
23*c5c4113dSnw141292# Basic ROWID functionality tests.
24*c5c4113dSnw141292#
25*c5c4113dSnw141292do_test rowid-1.1 {
26*c5c4113dSnw141292  execsql {
27*c5c4113dSnw141292    CREATE TABLE t1(x int, y int);
28*c5c4113dSnw141292    INSERT INTO t1 VALUES(1,2);
29*c5c4113dSnw141292    INSERT INTO t1 VALUES(3,4);
30*c5c4113dSnw141292    SELECT x FROM t1 ORDER BY y;
31*c5c4113dSnw141292  }
32*c5c4113dSnw141292} {1 3}
33*c5c4113dSnw141292do_test rowid-1.2 {
34*c5c4113dSnw141292  set r [execsql {SELECT rowid FROM t1 ORDER BY x}]
35*c5c4113dSnw141292  global x2rowid rowid2x
36*c5c4113dSnw141292  set x2rowid(1) [lindex $r 0]
37*c5c4113dSnw141292  set x2rowid(3) [lindex $r 1]
38*c5c4113dSnw141292  set rowid2x($x2rowid(1)) 1
39*c5c4113dSnw141292  set rowid2x($x2rowid(3)) 3
40*c5c4113dSnw141292  llength $r
41*c5c4113dSnw141292} {2}
42*c5c4113dSnw141292do_test rowid-1.3 {
43*c5c4113dSnw141292  global x2rowid
44*c5c4113dSnw141292  set sql "SELECT x FROM t1 WHERE rowid==$x2rowid(1)"
45*c5c4113dSnw141292  execsql $sql
46*c5c4113dSnw141292} {1}
47*c5c4113dSnw141292do_test rowid-1.4 {
48*c5c4113dSnw141292  global x2rowid
49*c5c4113dSnw141292  set sql "SELECT x FROM t1 WHERE rowid==$x2rowid(3)"
50*c5c4113dSnw141292  execsql $sql
51*c5c4113dSnw141292} {3}
52*c5c4113dSnw141292do_test rowid-1.5 {
53*c5c4113dSnw141292  global x2rowid
54*c5c4113dSnw141292  set sql "SELECT x FROM t1 WHERE oid==$x2rowid(1)"
55*c5c4113dSnw141292  execsql $sql
56*c5c4113dSnw141292} {1}
57*c5c4113dSnw141292do_test rowid-1.6 {
58*c5c4113dSnw141292  global x2rowid
59*c5c4113dSnw141292  set sql "SELECT x FROM t1 WHERE OID==$x2rowid(3)"
60*c5c4113dSnw141292  execsql $sql
61*c5c4113dSnw141292} {3}
62*c5c4113dSnw141292do_test rowid-1.7 {
63*c5c4113dSnw141292  global x2rowid
64*c5c4113dSnw141292  set sql "SELECT x FROM t1 WHERE _rowid_==$x2rowid(1)"
65*c5c4113dSnw141292  execsql $sql
66*c5c4113dSnw141292} {1}
67*c5c4113dSnw141292do_test rowid-1.7.1 {
68*c5c4113dSnw141292  while 1 {
69*c5c4113dSnw141292    set norow [expr {int(rand()*1000000)}]
70*c5c4113dSnw141292    if {$norow!=$x2rowid(1) && $norow!=$x2rowid(3)} break
71*c5c4113dSnw141292  }
72*c5c4113dSnw141292  execsql "SELECT x FROM t1 WHERE rowid=$norow"
73*c5c4113dSnw141292} {}
74*c5c4113dSnw141292do_test rowid-1.8 {
75*c5c4113dSnw141292  global x2rowid
76*c5c4113dSnw141292  set v [execsql {SELECT x, oid FROM t1 order by x}]
77*c5c4113dSnw141292  set v2 [list 1 $x2rowid(1) 3 $x2rowid(3)]
78*c5c4113dSnw141292  expr {$v==$v2}
79*c5c4113dSnw141292} {1}
80*c5c4113dSnw141292do_test rowid-1.9 {
81*c5c4113dSnw141292  global x2rowid
82*c5c4113dSnw141292  set v [execsql {SELECT x, RowID FROM t1 order by x}]
83*c5c4113dSnw141292  set v2 [list 1 $x2rowid(1) 3 $x2rowid(3)]
84*c5c4113dSnw141292  expr {$v==$v2}
85*c5c4113dSnw141292} {1}
86*c5c4113dSnw141292do_test rowid-1.9 {
87*c5c4113dSnw141292  global x2rowid
88*c5c4113dSnw141292  set v [execsql {SELECT x, _rowid_ FROM t1 order by x}]
89*c5c4113dSnw141292  set v2 [list 1 $x2rowid(1) 3 $x2rowid(3)]
90*c5c4113dSnw141292  expr {$v==$v2}
91*c5c4113dSnw141292} {1}
92*c5c4113dSnw141292
93*c5c4113dSnw141292# We can insert or update the ROWID column.
94*c5c4113dSnw141292#
95*c5c4113dSnw141292do_test rowid-2.1 {
96*c5c4113dSnw141292  catchsql {
97*c5c4113dSnw141292    INSERT INTO t1(rowid,x,y) VALUES(1234,5,6);
98*c5c4113dSnw141292    SELECT rowid, * FROM t1;
99*c5c4113dSnw141292  }
100*c5c4113dSnw141292} {0 {1 1 2 2 3 4 1234 5 6}}
101*c5c4113dSnw141292do_test rowid-2.2 {
102*c5c4113dSnw141292  catchsql {
103*c5c4113dSnw141292    UPDATE t1 SET rowid=12345 WHERE x==1;
104*c5c4113dSnw141292    SELECT rowid, * FROM t1
105*c5c4113dSnw141292  }
106*c5c4113dSnw141292} {0 {2 3 4 1234 5 6 12345 1 2}}
107*c5c4113dSnw141292do_test rowid-2.3 {
108*c5c4113dSnw141292  catchsql {
109*c5c4113dSnw141292    INSERT INTO t1(y,x,oid) VALUES(8,7,1235);
110*c5c4113dSnw141292    SELECT rowid, * FROM t1 WHERE rowid>1000;
111*c5c4113dSnw141292  }
112*c5c4113dSnw141292} {0 {1234 5 6 1235 7 8 12345 1 2}}
113*c5c4113dSnw141292do_test rowid-2.4 {
114*c5c4113dSnw141292  catchsql {
115*c5c4113dSnw141292    UPDATE t1 SET oid=12346 WHERE x==1;
116*c5c4113dSnw141292    SELECT rowid, * FROM t1;
117*c5c4113dSnw141292  }
118*c5c4113dSnw141292} {0 {2 3 4 1234 5 6 1235 7 8 12346 1 2}}
119*c5c4113dSnw141292do_test rowid-2.5 {
120*c5c4113dSnw141292  catchsql {
121*c5c4113dSnw141292    INSERT INTO t1(x,_rowid_,y) VALUES(9,1236,10);
122*c5c4113dSnw141292    SELECT rowid, * FROM t1 WHERE rowid>1000;
123*c5c4113dSnw141292  }
124*c5c4113dSnw141292} {0 {1234 5 6 1235 7 8 1236 9 10 12346 1 2}}
125*c5c4113dSnw141292do_test rowid-2.6 {
126*c5c4113dSnw141292  catchsql {
127*c5c4113dSnw141292    UPDATE t1 SET _rowid_=12347 WHERE x==1;
128*c5c4113dSnw141292    SELECT rowid, * FROM t1 WHERE rowid>1000;
129*c5c4113dSnw141292  }
130*c5c4113dSnw141292} {0 {1234 5 6 1235 7 8 1236 9 10 12347 1 2}}
131*c5c4113dSnw141292
132*c5c4113dSnw141292# But we can use ROWID in the WHERE clause of an UPDATE that does not
133*c5c4113dSnw141292# change the ROWID.
134*c5c4113dSnw141292#
135*c5c4113dSnw141292do_test rowid-2.7 {
136*c5c4113dSnw141292  global x2rowid
137*c5c4113dSnw141292  set sql "UPDATE t1 SET x=2 WHERE OID==$x2rowid(3)"
138*c5c4113dSnw141292  execsql $sql
139*c5c4113dSnw141292  execsql {SELECT x FROM t1 ORDER BY x}
140*c5c4113dSnw141292} {1 2 5 7 9}
141*c5c4113dSnw141292do_test rowid-2.8 {
142*c5c4113dSnw141292  global x2rowid
143*c5c4113dSnw141292  set sql "UPDATE t1 SET x=3 WHERE _rowid_==$x2rowid(3)"
144*c5c4113dSnw141292  execsql $sql
145*c5c4113dSnw141292  execsql {SELECT x FROM t1 ORDER BY x}
146*c5c4113dSnw141292} {1 3 5 7 9}
147*c5c4113dSnw141292
148*c5c4113dSnw141292# We cannot index by ROWID
149*c5c4113dSnw141292#
150*c5c4113dSnw141292do_test rowid-2.9 {
151*c5c4113dSnw141292  set v [catch {execsql {CREATE INDEX idxt1 ON t1(rowid)}} msg]
152*c5c4113dSnw141292  lappend v $msg
153*c5c4113dSnw141292} {1 {table t1 has no column named rowid}}
154*c5c4113dSnw141292do_test rowid-2.10 {
155*c5c4113dSnw141292  set v [catch {execsql {CREATE INDEX idxt1 ON t1(_rowid_)}} msg]
156*c5c4113dSnw141292  lappend v $msg
157*c5c4113dSnw141292} {1 {table t1 has no column named _rowid_}}
158*c5c4113dSnw141292do_test rowid-2.11 {
159*c5c4113dSnw141292  set v [catch {execsql {CREATE INDEX idxt1 ON t1(oid)}} msg]
160*c5c4113dSnw141292  lappend v $msg
161*c5c4113dSnw141292} {1 {table t1 has no column named oid}}
162*c5c4113dSnw141292do_test rowid-2.12 {
163*c5c4113dSnw141292  set v [catch {execsql {CREATE INDEX idxt1 ON t1(x, rowid)}} msg]
164*c5c4113dSnw141292  lappend v $msg
165*c5c4113dSnw141292} {1 {table t1 has no column named rowid}}
166*c5c4113dSnw141292
167*c5c4113dSnw141292# Columns defined in the CREATE statement override the buildin ROWID
168*c5c4113dSnw141292# column names.
169*c5c4113dSnw141292#
170*c5c4113dSnw141292do_test rowid-3.1 {
171*c5c4113dSnw141292  execsql {
172*c5c4113dSnw141292    CREATE TABLE t2(rowid int, x int, y int);
173*c5c4113dSnw141292    INSERT INTO t2 VALUES(0,2,3);
174*c5c4113dSnw141292    INSERT INTO t2 VALUES(4,5,6);
175*c5c4113dSnw141292    INSERT INTO t2 VALUES(7,8,9);
176*c5c4113dSnw141292    SELECT * FROM t2 ORDER BY x;
177*c5c4113dSnw141292  }
178*c5c4113dSnw141292} {0 2 3 4 5 6 7 8 9}
179*c5c4113dSnw141292do_test rowid-3.2 {
180*c5c4113dSnw141292  execsql {SELECT * FROM t2 ORDER BY rowid}
181*c5c4113dSnw141292} {0 2 3 4 5 6 7 8 9}
182*c5c4113dSnw141292do_test rowid-3.3 {
183*c5c4113dSnw141292  execsql {SELECT rowid, x, y FROM t2 ORDER BY rowid}
184*c5c4113dSnw141292} {0 2 3 4 5 6 7 8 9}
185*c5c4113dSnw141292do_test rowid-3.4 {
186*c5c4113dSnw141292  set r1 [execsql {SELECT _rowid_, rowid FROM t2 ORDER BY rowid}]
187*c5c4113dSnw141292  foreach {a b c d e f} $r1 {}
188*c5c4113dSnw141292  set r2 [execsql {SELECT _rowid_, rowid FROM t2 ORDER BY x DESC}]
189*c5c4113dSnw141292  foreach {u v w x y z} $r2 {}
190*c5c4113dSnw141292  expr {$u==$e && $w==$c && $y==$a}
191*c5c4113dSnw141292} {1}
192*c5c4113dSnw141292do_probtest rowid-3.5 {
193*c5c4113dSnw141292  set r1 [execsql {SELECT _rowid_, rowid FROM t2 ORDER BY rowid}]
194*c5c4113dSnw141292  foreach {a b c d e f} $r1 {}
195*c5c4113dSnw141292  expr {$a!=$b && $c!=$d && $e!=$f}
196*c5c4113dSnw141292} {1}
197*c5c4113dSnw141292
198*c5c4113dSnw141292# Let's try some more complex examples, including some joins.
199*c5c4113dSnw141292#
200*c5c4113dSnw141292do_test rowid-4.1 {
201*c5c4113dSnw141292  execsql {
202*c5c4113dSnw141292    DELETE FROM t1;
203*c5c4113dSnw141292    DELETE FROM t2;
204*c5c4113dSnw141292  }
205*c5c4113dSnw141292  for {set i 1} {$i<=50} {incr i} {
206*c5c4113dSnw141292    execsql "INSERT INTO t1(x,y) VALUES($i,[expr {$i*$i}])"
207*c5c4113dSnw141292  }
208*c5c4113dSnw141292  execsql {INSERT INTO t2 SELECT _rowid_, x*y, y*y FROM t1}
209*c5c4113dSnw141292  execsql {SELECT t2.y FROM t1, t2 WHERE t1.x==4 AND t1.rowid==t2.rowid}
210*c5c4113dSnw141292} {256}
211*c5c4113dSnw141292do_test rowid-4.2 {
212*c5c4113dSnw141292  execsql {SELECT t2.y FROM t2, t1 WHERE t1.x==4 AND t1.rowid==t2.rowid}
213*c5c4113dSnw141292} {256}
214*c5c4113dSnw141292do_test rowid-4.2.1 {
215*c5c4113dSnw141292  execsql {SELECT t2.y FROM t2, t1 WHERE t1.x==4 AND t1.oid==t2.rowid}
216*c5c4113dSnw141292} {256}
217*c5c4113dSnw141292do_test rowid-4.2.2 {
218*c5c4113dSnw141292  execsql {SELECT t2.y FROM t2, t1 WHERE t1.x==4 AND t1._rowid_==t2.rowid}
219*c5c4113dSnw141292} {256}
220*c5c4113dSnw141292do_test rowid-4.2.3 {
221*c5c4113dSnw141292  execsql {SELECT t2.y FROM t2, t1 WHERE t1.x==4 AND t2.rowid==t1.rowid}
222*c5c4113dSnw141292} {256}
223*c5c4113dSnw141292do_test rowid-4.2.4 {
224*c5c4113dSnw141292  execsql {SELECT t2.y FROM t2, t1 WHERE t2.rowid==t1.oid AND t1.x==4}
225*c5c4113dSnw141292} {256}
226*c5c4113dSnw141292do_test rowid-4.2.5 {
227*c5c4113dSnw141292  execsql {SELECT t2.y FROM t1, t2 WHERE t1.x==4 AND t1._rowid_==t2.rowid}
228*c5c4113dSnw141292} {256}
229*c5c4113dSnw141292do_test rowid-4.2.6 {
230*c5c4113dSnw141292  execsql {SELECT t2.y FROM t1, t2 WHERE t1.x==4 AND t2.rowid==t1.rowid}
231*c5c4113dSnw141292} {256}
232*c5c4113dSnw141292do_test rowid-4.2.7 {
233*c5c4113dSnw141292  execsql {SELECT t2.y FROM t1, t2 WHERE t2.rowid==t1.oid AND t1.x==4}
234*c5c4113dSnw141292} {256}
235*c5c4113dSnw141292do_test rowid-4.3 {
236*c5c4113dSnw141292  execsql {CREATE INDEX idxt1 ON t1(x)}
237*c5c4113dSnw141292  execsql {SELECT t2.y FROM t1, t2 WHERE t1.x==4 AND t1.rowid==t2.rowid}
238*c5c4113dSnw141292} {256}
239*c5c4113dSnw141292do_test rowid-4.3.1 {
240*c5c4113dSnw141292  execsql {SELECT t2.y FROM t1, t2 WHERE t1.x==4 AND t1._rowid_==t2.rowid}
241*c5c4113dSnw141292} {256}
242*c5c4113dSnw141292do_test rowid-4.3.2 {
243*c5c4113dSnw141292  execsql {SELECT t2.y FROM t1, t2 WHERE t2.rowid==t1.oid AND 4==t1.x}
244*c5c4113dSnw141292} {256}
245*c5c4113dSnw141292do_test rowid-4.4 {
246*c5c4113dSnw141292  execsql {SELECT t2.y FROM t2, t1 WHERE t1.x==4 AND t1.rowid==t2.rowid}
247*c5c4113dSnw141292} {256}
248*c5c4113dSnw141292do_test rowid-4.4.1 {
249*c5c4113dSnw141292  execsql {SELECT t2.y FROM t2, t1 WHERE t1.x==4 AND t1._rowid_==t2.rowid}
250*c5c4113dSnw141292} {256}
251*c5c4113dSnw141292do_test rowid-4.4.2 {
252*c5c4113dSnw141292  execsql {SELECT t2.y FROM t2, t1 WHERE t2.rowid==t1.oid AND 4==t1.x}
253*c5c4113dSnw141292} {256}
254*c5c4113dSnw141292do_test rowid-4.5 {
255*c5c4113dSnw141292  execsql {CREATE INDEX idxt2 ON t2(y)}
256*c5c4113dSnw141292  set sqlite_search_count 0
257*c5c4113dSnw141292  concat [execsql {
258*c5c4113dSnw141292    SELECT t1.x FROM t2, t1
259*c5c4113dSnw141292    WHERE t2.y==256 AND t1.rowid==t2.rowid
260*c5c4113dSnw141292  }] $sqlite_search_count
261*c5c4113dSnw141292} {4 3}
262*c5c4113dSnw141292do_test rowid-4.5.1 {
263*c5c4113dSnw141292  set sqlite_search_count 0
264*c5c4113dSnw141292  concat [execsql {
265*c5c4113dSnw141292    SELECT t1.x FROM t2, t1
266*c5c4113dSnw141292    WHERE t1.OID==t2.rowid AND t2.y==81
267*c5c4113dSnw141292  }] $sqlite_search_count
268*c5c4113dSnw141292} {3 3}
269*c5c4113dSnw141292do_test rowid-4.6 {
270*c5c4113dSnw141292  execsql {
271*c5c4113dSnw141292    SELECT t1.x FROM t1, t2
272*c5c4113dSnw141292    WHERE t2.y==256 AND t1.rowid==t2.rowid
273*c5c4113dSnw141292  }
274*c5c4113dSnw141292} {4}
275*c5c4113dSnw141292
276*c5c4113dSnw141292do_test rowid-5.1 {
277*c5c4113dSnw141292  execsql {DELETE FROM t1 WHERE _rowid_ IN (SELECT oid FROM t1 WHERE x>8)}
278*c5c4113dSnw141292  execsql {SELECT max(x) FROM t1}
279*c5c4113dSnw141292} {8}
280*c5c4113dSnw141292
281*c5c4113dSnw141292# Make sure a "WHERE rowid=X" clause works when there is no ROWID of X.
282*c5c4113dSnw141292#
283*c5c4113dSnw141292do_test rowid-6.1 {
284*c5c4113dSnw141292  execsql {
285*c5c4113dSnw141292    SELECT x FROM t1
286*c5c4113dSnw141292  }
287*c5c4113dSnw141292} {1 2 3 4 5 6 7 8}
288*c5c4113dSnw141292do_test rowid-6.2 {
289*c5c4113dSnw141292  for {set ::norow 1} {1} {incr ::norow} {
290*c5c4113dSnw141292    if {[execsql "SELECT x FROM t1 WHERE rowid=$::norow"]==""}  break
291*c5c4113dSnw141292  }
292*c5c4113dSnw141292  execsql [subst {
293*c5c4113dSnw141292    DELETE FROM t1 WHERE rowid=$::norow
294*c5c4113dSnw141292  }]
295*c5c4113dSnw141292} {}
296*c5c4113dSnw141292do_test rowid-6.3 {
297*c5c4113dSnw141292  execsql {
298*c5c4113dSnw141292    SELECT x FROM t1
299*c5c4113dSnw141292  }
300*c5c4113dSnw141292} {1 2 3 4 5 6 7 8}
301*c5c4113dSnw141292
302*c5c4113dSnw141292# Beginning with version 2.3.4, SQLite computes rowids of new rows by
303*c5c4113dSnw141292# finding the maximum current rowid and adding one.  It falls back to
304*c5c4113dSnw141292# the old random algorithm if the maximum rowid is the largest integer.
305*c5c4113dSnw141292# The following tests are for this new behavior.
306*c5c4113dSnw141292#
307*c5c4113dSnw141292do_test rowid-7.0 {
308*c5c4113dSnw141292  execsql {
309*c5c4113dSnw141292    DELETE FROM t1;
310*c5c4113dSnw141292    DROP TABLE t2;
311*c5c4113dSnw141292    DROP INDEX idxt1;
312*c5c4113dSnw141292    INSERT INTO t1 VALUES(1,2);
313*c5c4113dSnw141292    SELECT rowid, * FROM t1;
314*c5c4113dSnw141292  }
315*c5c4113dSnw141292} {1 1 2}
316*c5c4113dSnw141292do_test rowid-7.1 {
317*c5c4113dSnw141292  execsql {
318*c5c4113dSnw141292    INSERT INTO t1 VALUES(99,100);
319*c5c4113dSnw141292    SELECT rowid,* FROM t1
320*c5c4113dSnw141292  }
321*c5c4113dSnw141292} {1 1 2 2 99 100}
322*c5c4113dSnw141292do_test rowid-7.2 {
323*c5c4113dSnw141292  execsql {
324*c5c4113dSnw141292    CREATE TABLE t2(a INTEGER PRIMARY KEY, b);
325*c5c4113dSnw141292    INSERT INTO t2(b) VALUES(55);
326*c5c4113dSnw141292    SELECT * FROM t2;
327*c5c4113dSnw141292  }
328*c5c4113dSnw141292} {1 55}
329*c5c4113dSnw141292do_test rowid-7.3 {
330*c5c4113dSnw141292  execsql {
331*c5c4113dSnw141292    INSERT INTO t2(b) VALUES(66);
332*c5c4113dSnw141292    SELECT * FROM t2;
333*c5c4113dSnw141292  }
334*c5c4113dSnw141292} {1 55 2 66}
335*c5c4113dSnw141292do_test rowid-7.4 {
336*c5c4113dSnw141292  execsql {
337*c5c4113dSnw141292    INSERT INTO t2(a,b) VALUES(1000000,77);
338*c5c4113dSnw141292    INSERT INTO t2(b) VALUES(88);
339*c5c4113dSnw141292    SELECT * FROM t2;
340*c5c4113dSnw141292  }
341*c5c4113dSnw141292} {1 55 2 66 1000000 77 1000001 88}
342*c5c4113dSnw141292do_test rowid-7.5 {
343*c5c4113dSnw141292  execsql {
344*c5c4113dSnw141292    INSERT INTO t2(a,b) VALUES(2147483647,99);
345*c5c4113dSnw141292    INSERT INTO t2(b) VALUES(11);
346*c5c4113dSnw141292    SELECT b FROM t2 ORDER BY b;
347*c5c4113dSnw141292  }
348*c5c4113dSnw141292} {11 55 66 77 88 99}
349*c5c4113dSnw141292do_test rowid-7.6 {
350*c5c4113dSnw141292  execsql {
351*c5c4113dSnw141292    SELECT b FROM t2 WHERE a NOT IN(1,2,1000000,1000001,2147483647);
352*c5c4113dSnw141292  }
353*c5c4113dSnw141292} {11}
354*c5c4113dSnw141292do_test rowid-7.7 {
355*c5c4113dSnw141292  execsql {
356*c5c4113dSnw141292    INSERT INTO t2(b) VALUES(22);
357*c5c4113dSnw141292    INSERT INTO t2(b) VALUES(33);
358*c5c4113dSnw141292    INSERT INTO t2(b) VALUES(44);
359*c5c4113dSnw141292    INSERT INTO t2(b) VALUES(55);
360*c5c4113dSnw141292    SELECT b FROM t2 WHERE a NOT IN(1,2,1000000,1000001,2147483647) ORDER BY b;
361*c5c4113dSnw141292  }
362*c5c4113dSnw141292} {11 22 33 44 55}
363*c5c4113dSnw141292do_test rowid-7.8 {
364*c5c4113dSnw141292  execsql {
365*c5c4113dSnw141292    DELETE FROM t2 WHERE a!=2;
366*c5c4113dSnw141292    INSERT INTO t2(b) VALUES(111);
367*c5c4113dSnw141292    SELECT * FROM t2;
368*c5c4113dSnw141292  }
369*c5c4113dSnw141292} {2 66 3 111}
370*c5c4113dSnw141292
371*c5c4113dSnw141292# Make sure AFTER triggers that do INSERTs do not change the last_insert_rowid.
372*c5c4113dSnw141292# Ticket #290
373*c5c4113dSnw141292#
374*c5c4113dSnw141292do_test rowid-8.1 {
375*c5c4113dSnw141292  execsql {
376*c5c4113dSnw141292    CREATE TABLE t3(a integer primary key);
377*c5c4113dSnw141292    CREATE TABLE t4(x);
378*c5c4113dSnw141292    INSERT INTO t4 VALUES(1);
379*c5c4113dSnw141292    CREATE TRIGGER r3 AFTER INSERT on t3 FOR EACH ROW BEGIN
380*c5c4113dSnw141292      INSERT INTO t4 VALUES(NEW.a+10);
381*c5c4113dSnw141292    END;
382*c5c4113dSnw141292    SELECT * FROM t3;
383*c5c4113dSnw141292  }
384*c5c4113dSnw141292} {}
385*c5c4113dSnw141292do_test rowid-8.2 {
386*c5c4113dSnw141292  execsql {
387*c5c4113dSnw141292    SELECT rowid, * FROM t4;
388*c5c4113dSnw141292  }
389*c5c4113dSnw141292} {1 1}
390*c5c4113dSnw141292do_test rowid-8.3 {
391*c5c4113dSnw141292  execsql {
392*c5c4113dSnw141292    INSERT INTO t3 VALUES(123);
393*c5c4113dSnw141292    SELECT last_insert_rowid();
394*c5c4113dSnw141292  }
395*c5c4113dSnw141292} {123}
396*c5c4113dSnw141292do_test rowid-8.4 {
397*c5c4113dSnw141292  execsql {
398*c5c4113dSnw141292    SELECT * FROM t3;
399*c5c4113dSnw141292  }
400*c5c4113dSnw141292} {123}
401*c5c4113dSnw141292do_test rowid-8.5 {
402*c5c4113dSnw141292  execsql {
403*c5c4113dSnw141292    SELECT rowid, * FROM t4;
404*c5c4113dSnw141292  }
405*c5c4113dSnw141292} {1 1 2 133}
406*c5c4113dSnw141292do_test rowid-8.6 {
407*c5c4113dSnw141292  execsql {
408*c5c4113dSnw141292    INSERT INTO t3 VALUES(NULL);
409*c5c4113dSnw141292    SELECT last_insert_rowid();
410*c5c4113dSnw141292  }
411*c5c4113dSnw141292} {124}
412*c5c4113dSnw141292do_test rowid-8.7 {
413*c5c4113dSnw141292  execsql {
414*c5c4113dSnw141292    SELECT * FROM t3;
415*c5c4113dSnw141292  }
416*c5c4113dSnw141292} {123 124}
417*c5c4113dSnw141292do_test rowid-8.8 {
418*c5c4113dSnw141292  execsql {
419*c5c4113dSnw141292    SELECT rowid, * FROM t4;
420*c5c4113dSnw141292  }
421*c5c4113dSnw141292} {1 1 2 133 3 134}
422*c5c4113dSnw141292
423*c5c4113dSnw141292# ticket #377: Comparison between integer primiary key and floating point
424*c5c4113dSnw141292# values.
425*c5c4113dSnw141292#
426*c5c4113dSnw141292do_test rowid-9.1 {
427*c5c4113dSnw141292  execsql {
428*c5c4113dSnw141292    SELECT * FROM t3 WHERE a<123.5
429*c5c4113dSnw141292  }
430*c5c4113dSnw141292} {123}
431*c5c4113dSnw141292do_test rowid-9.2 {
432*c5c4113dSnw141292  execsql {
433*c5c4113dSnw141292    SELECT * FROM t3 WHERE a<124.5
434*c5c4113dSnw141292  }
435*c5c4113dSnw141292} {123 124}
436*c5c4113dSnw141292do_test rowid-9.3 {
437*c5c4113dSnw141292  execsql {
438*c5c4113dSnw141292    SELECT * FROM t3 WHERE a>123.5
439*c5c4113dSnw141292  }
440*c5c4113dSnw141292} {124}
441*c5c4113dSnw141292do_test rowid-9.4 {
442*c5c4113dSnw141292  execsql {
443*c5c4113dSnw141292    SELECT * FROM t3 WHERE a>122.5
444*c5c4113dSnw141292  }
445*c5c4113dSnw141292} {123 124}
446*c5c4113dSnw141292do_test rowid-9.5 {
447*c5c4113dSnw141292  execsql {
448*c5c4113dSnw141292    SELECT * FROM t3 WHERE a==123.5
449*c5c4113dSnw141292  }
450*c5c4113dSnw141292} {}
451*c5c4113dSnw141292do_test rowid-9.6 {
452*c5c4113dSnw141292  execsql {
453*c5c4113dSnw141292    SELECT * FROM t3 WHERE a==123.000
454*c5c4113dSnw141292  }
455*c5c4113dSnw141292} {123}
456*c5c4113dSnw141292do_test rowid-9.7 {
457*c5c4113dSnw141292  execsql {
458*c5c4113dSnw141292    SELECT * FROM t3 WHERE a>100.5 AND a<200.5
459*c5c4113dSnw141292  }
460*c5c4113dSnw141292} {123 124}
461*c5c4113dSnw141292do_test rowid-9.8 {
462*c5c4113dSnw141292  execsql {
463*c5c4113dSnw141292    SELECT * FROM t3 WHERE a>'xyz';
464*c5c4113dSnw141292  }
465*c5c4113dSnw141292} {}
466*c5c4113dSnw141292do_test rowid-9.9 {
467*c5c4113dSnw141292  execsql {
468*c5c4113dSnw141292    SELECT * FROM t3 WHERE a<'xyz';
469*c5c4113dSnw141292  }
470*c5c4113dSnw141292} {123 124}
471*c5c4113dSnw141292do_test rowid-9.10 {
472*c5c4113dSnw141292  execsql {
473*c5c4113dSnw141292    SELECT * FROM t3 WHERE a>=122.9 AND a<=123.1
474*c5c4113dSnw141292  }
475*c5c4113dSnw141292} {123}
476*c5c4113dSnw141292
477*c5c4113dSnw141292# Ticket #567.  Comparisons of ROWID or integery primary key against
478*c5c4113dSnw141292# floating point numbers still do not always work.
479*c5c4113dSnw141292#
480*c5c4113dSnw141292do_test rowid-10.1 {
481*c5c4113dSnw141292  execsql {
482*c5c4113dSnw141292    CREATE TABLE t5(a);
483*c5c4113dSnw141292    INSERT INTO t5 VALUES(1);
484*c5c4113dSnw141292    INSERT INTO t5 VALUES(2);
485*c5c4113dSnw141292    INSERT INTO t5 SELECT a+2 FROM t5;
486*c5c4113dSnw141292    INSERT INTO t5 SELECT a+4 FROM t5;
487*c5c4113dSnw141292    SELECT rowid, * FROM t5;
488*c5c4113dSnw141292  }
489*c5c4113dSnw141292} {1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8}
490*c5c4113dSnw141292do_test rowid-10.2 {
491*c5c4113dSnw141292  execsql {SELECT rowid, a FROM t5 WHERE rowid>=5.5}
492*c5c4113dSnw141292} {6 6 7 7 8 8}
493*c5c4113dSnw141292do_test rowid-10.3 {
494*c5c4113dSnw141292  execsql {SELECT rowid, a FROM t5 WHERE rowid>=5.0}
495*c5c4113dSnw141292} {5 5 6 6 7 7 8 8}
496*c5c4113dSnw141292do_test rowid-10.4 {
497*c5c4113dSnw141292  execsql {SELECT rowid, a FROM t5 WHERE rowid>5.5}
498*c5c4113dSnw141292} {6 6 7 7 8 8}
499*c5c4113dSnw141292do_test rowid-10.3.2 {
500*c5c4113dSnw141292  execsql {SELECT rowid, a FROM t5 WHERE rowid>5.0}
501*c5c4113dSnw141292} {6 6 7 7 8 8}
502*c5c4113dSnw141292do_test rowid-10.5 {
503*c5c4113dSnw141292  execsql {SELECT rowid, a FROM t5 WHERE 5.5<=rowid}
504*c5c4113dSnw141292} {6 6 7 7 8 8}
505*c5c4113dSnw141292do_test rowid-10.6 {
506*c5c4113dSnw141292  execsql {SELECT rowid, a FROM t5 WHERE 5.5<rowid}
507*c5c4113dSnw141292} {6 6 7 7 8 8}
508*c5c4113dSnw141292do_test rowid-10.7 {
509*c5c4113dSnw141292  execsql {SELECT rowid, a FROM t5 WHERE rowid<=5.5}
510*c5c4113dSnw141292} {1 1 2 2 3 3 4 4 5 5}
511*c5c4113dSnw141292do_test rowid-10.8 {
512*c5c4113dSnw141292  execsql {SELECT rowid, a FROM t5 WHERE rowid<5.5}
513*c5c4113dSnw141292} {1 1 2 2 3 3 4 4 5 5}
514*c5c4113dSnw141292do_test rowid-10.9 {
515*c5c4113dSnw141292  execsql {SELECT rowid, a FROM t5 WHERE 5.5>=rowid}
516*c5c4113dSnw141292} {1 1 2 2 3 3 4 4 5 5}
517*c5c4113dSnw141292do_test rowid-10.10 {
518*c5c4113dSnw141292  execsql {SELECT rowid, a FROM t5 WHERE 5.5>rowid}
519*c5c4113dSnw141292} {1 1 2 2 3 3 4 4 5 5}
520*c5c4113dSnw141292do_test rowid-10.11 {
521*c5c4113dSnw141292  execsql {SELECT rowid, a FROM t5 WHERE rowid>=5.5 ORDER BY rowid DESC}
522*c5c4113dSnw141292} {8 8 7 7 6 6}
523*c5c4113dSnw141292do_test rowid-10.11.2 {
524*c5c4113dSnw141292  execsql {SELECT rowid, a FROM t5 WHERE rowid>=5.0 ORDER BY rowid DESC}
525*c5c4113dSnw141292} {8 8 7 7 6 6 5 5}
526*c5c4113dSnw141292do_test rowid-10.12 {
527*c5c4113dSnw141292  execsql {SELECT rowid, a FROM t5 WHERE rowid>5.5 ORDER BY rowid DESC}
528*c5c4113dSnw141292} {8 8 7 7 6 6}
529*c5c4113dSnw141292do_test rowid-10.12.2 {
530*c5c4113dSnw141292  execsql {SELECT rowid, a FROM t5 WHERE rowid>5.0 ORDER BY rowid DESC}
531*c5c4113dSnw141292} {8 8 7 7 6 6}
532*c5c4113dSnw141292do_test rowid-10.13 {
533*c5c4113dSnw141292  execsql {SELECT rowid, a FROM t5 WHERE 5.5<=rowid ORDER BY rowid DESC}
534*c5c4113dSnw141292} {8 8 7 7 6 6}
535*c5c4113dSnw141292do_test rowid-10.14 {
536*c5c4113dSnw141292  execsql {SELECT rowid, a FROM t5 WHERE 5.5<rowid ORDER BY rowid DESC}
537*c5c4113dSnw141292} {8 8 7 7 6 6}
538*c5c4113dSnw141292do_test rowid-10.15 {
539*c5c4113dSnw141292  execsql {SELECT rowid, a FROM t5 WHERE rowid<=5.5 ORDER BY rowid DESC}
540*c5c4113dSnw141292} {5 5 4 4 3 3 2 2 1 1}
541*c5c4113dSnw141292do_test rowid-10.16 {
542*c5c4113dSnw141292  execsql {SELECT rowid, a FROM t5 WHERE rowid<5.5 ORDER BY rowid DESC}
543*c5c4113dSnw141292} {5 5 4 4 3 3 2 2 1 1}
544*c5c4113dSnw141292do_test rowid-10.17 {
545*c5c4113dSnw141292  execsql {SELECT rowid, a FROM t5 WHERE 5.5>=rowid ORDER BY rowid DESC}
546*c5c4113dSnw141292} {5 5 4 4 3 3 2 2 1 1}
547*c5c4113dSnw141292do_test rowid-10.18 {
548*c5c4113dSnw141292  execsql {SELECT rowid, a FROM t5 WHERE 5.5>rowid ORDER BY rowid DESC}
549*c5c4113dSnw141292} {5 5 4 4 3 3 2 2 1 1}
550*c5c4113dSnw141292
551*c5c4113dSnw141292do_test rowid-10.30 {
552*c5c4113dSnw141292  execsql {
553*c5c4113dSnw141292    CREATE TABLE t6(a);
554*c5c4113dSnw141292    INSERT INTO t6(rowid,a) SELECT -a,a FROM t5;
555*c5c4113dSnw141292    SELECT rowid, * FROM t6;
556*c5c4113dSnw141292  }
557*c5c4113dSnw141292} {-8 8 -7 7 -6 6 -5 5 -4 4 -3 3 -2 2 -1 1}
558*c5c4113dSnw141292do_test rowid-10.31.1 {
559*c5c4113dSnw141292  execsql {SELECT rowid, a FROM t6 WHERE rowid>=-5.5}
560*c5c4113dSnw141292} {-5 5 -4 4 -3 3 -2 2 -1 1}
561*c5c4113dSnw141292do_test rowid-10.31.2 {
562*c5c4113dSnw141292  execsql {SELECT rowid, a FROM t6 WHERE rowid>=-5.0}
563*c5c4113dSnw141292} {-5 5 -4 4 -3 3 -2 2 -1 1}
564*c5c4113dSnw141292do_test rowid-10.32.1 {
565*c5c4113dSnw141292  execsql {SELECT rowid, a FROM t6 WHERE rowid>=-5.5 ORDER BY rowid DESC}
566*c5c4113dSnw141292} {-1 1 -2 2 -3 3 -4 4 -5 5}
567*c5c4113dSnw141292do_test rowid-10.32.1 {
568*c5c4113dSnw141292  execsql {SELECT rowid, a FROM t6 WHERE rowid>=-5.0 ORDER BY rowid DESC}
569*c5c4113dSnw141292} {-1 1 -2 2 -3 3 -4 4 -5 5}
570*c5c4113dSnw141292do_test rowid-10.33 {
571*c5c4113dSnw141292  execsql {SELECT rowid, a FROM t6 WHERE -5.5<=rowid}
572*c5c4113dSnw141292} {-5 5 -4 4 -3 3 -2 2 -1 1}
573*c5c4113dSnw141292do_test rowid-10.34 {
574*c5c4113dSnw141292  execsql {SELECT rowid, a FROM t6 WHERE -5.5<=rowid ORDER BY rowid DESC}
575*c5c4113dSnw141292} {-1 1 -2 2 -3 3 -4 4 -5 5}
576*c5c4113dSnw141292do_test rowid-10.35.1 {
577*c5c4113dSnw141292  execsql {SELECT rowid, a FROM t6 WHERE rowid>-5.5}
578*c5c4113dSnw141292} {-5 5 -4 4 -3 3 -2 2 -1 1}
579*c5c4113dSnw141292do_test rowid-10.35.2 {
580*c5c4113dSnw141292  execsql {SELECT rowid, a FROM t6 WHERE rowid>-5.0}
581*c5c4113dSnw141292} {-4 4 -3 3 -2 2 -1 1}
582*c5c4113dSnw141292do_test rowid-10.36.1 {
583*c5c4113dSnw141292  execsql {SELECT rowid, a FROM t6 WHERE rowid>-5.5 ORDER BY rowid DESC}
584*c5c4113dSnw141292} {-1 1 -2 2 -3 3 -4 4 -5 5}
585*c5c4113dSnw141292do_test rowid-10.36.2 {
586*c5c4113dSnw141292  execsql {SELECT rowid, a FROM t6 WHERE rowid>-5.0 ORDER BY rowid DESC}
587*c5c4113dSnw141292} {-1 1 -2 2 -3 3 -4 4}
588*c5c4113dSnw141292do_test rowid-10.37 {
589*c5c4113dSnw141292  execsql {SELECT rowid, a FROM t6 WHERE -5.5<rowid}
590*c5c4113dSnw141292} {-5 5 -4 4 -3 3 -2 2 -1 1}
591*c5c4113dSnw141292do_test rowid-10.38 {
592*c5c4113dSnw141292  execsql {SELECT rowid, a FROM t6 WHERE -5.5<rowid ORDER BY rowid DESC}
593*c5c4113dSnw141292} {-1 1 -2 2 -3 3 -4 4 -5 5}
594*c5c4113dSnw141292do_test rowid-10.39 {
595*c5c4113dSnw141292  execsql {SELECT rowid, a FROM t6 WHERE rowid<=-5.5}
596*c5c4113dSnw141292} {-8 8 -7 7 -6 6}
597*c5c4113dSnw141292do_test rowid-10.40 {
598*c5c4113dSnw141292  execsql {SELECT rowid, a FROM t6 WHERE rowid<=-5.5 ORDER BY rowid DESC}
599*c5c4113dSnw141292} {-6 6 -7 7 -8 8}
600*c5c4113dSnw141292do_test rowid-10.41 {
601*c5c4113dSnw141292  execsql {SELECT rowid, a FROM t6 WHERE -5.5>=rowid}
602*c5c4113dSnw141292} {-8 8 -7 7 -6 6}
603*c5c4113dSnw141292do_test rowid-10.42 {
604*c5c4113dSnw141292  execsql {SELECT rowid, a FROM t6 WHERE -5.5>=rowid ORDER BY rowid DESC}
605*c5c4113dSnw141292} {-6 6 -7 7 -8 8}
606*c5c4113dSnw141292do_test rowid-10.43 {
607*c5c4113dSnw141292  execsql {SELECT rowid, a FROM t6 WHERE rowid<-5.5}
608*c5c4113dSnw141292} {-8 8 -7 7 -6 6}
609*c5c4113dSnw141292do_test rowid-10.44 {
610*c5c4113dSnw141292  execsql {SELECT rowid, a FROM t6 WHERE rowid<-5.5 ORDER BY rowid DESC}
611*c5c4113dSnw141292} {-6 6 -7 7 -8 8}
612*c5c4113dSnw141292do_test rowid-10.44 {
613*c5c4113dSnw141292  execsql {SELECT rowid, a FROM t6 WHERE -5.5>rowid}
614*c5c4113dSnw141292} {-8 8 -7 7 -6 6}
615*c5c4113dSnw141292do_test rowid-10.46 {
616*c5c4113dSnw141292  execsql {SELECT rowid, a FROM t6 WHERE -5.5>rowid ORDER BY rowid DESC}
617*c5c4113dSnw141292} {-6 6 -7 7 -8 8}
618*c5c4113dSnw141292
619*c5c4113dSnw141292# Comparison of rowid against string values.
620*c5c4113dSnw141292#
621*c5c4113dSnw141292do_test rowid-11.1 {
622*c5c4113dSnw141292  execsql {SELECT rowid, a FROM t5 WHERE rowid>'abc'}
623*c5c4113dSnw141292} {}
624*c5c4113dSnw141292do_test rowid-11.2 {
625*c5c4113dSnw141292  execsql {SELECT rowid, a FROM t5 WHERE rowid>='abc'}
626*c5c4113dSnw141292} {}
627*c5c4113dSnw141292do_test rowid-11.3 {
628*c5c4113dSnw141292  execsql {SELECT rowid, a FROM t5 WHERE rowid<'abc'}
629*c5c4113dSnw141292} {1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8}
630*c5c4113dSnw141292do_test rowid-11.4 {
631*c5c4113dSnw141292  execsql {SELECT rowid, a FROM t5 WHERE rowid<='abc'}
632*c5c4113dSnw141292} {1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8}
633*c5c4113dSnw141292
634*c5c4113dSnw141292
635*c5c4113dSnw141292
636*c5c4113dSnw141292finish_test
637