1#
2# 2001 September 15
3#
4# The author disclaims copyright to this source code.  In place of
5# a legal notice, here is a blessing:
6#
7#    May you do good and not evil.
8#    May you find forgiveness for yourself and forgive others.
9#    May you share freely, never taking more than you give.
10#
11#***********************************************************************
12# This file implements regression tests for SQLite library.  The
13# focus of this file is testing the the library is able to correctly
14# handle file-format 3 (version 2.6.x) databases.
15#
16# $Id: format3.test,v 1.4 2003/12/23 02:17:35 drh Exp $
17
18set testdir [file dirname $argv0]
19source $testdir/tester.tcl
20
21# Create a bunch of data to sort against
22#
23do_test format3-1.0 {
24  set fd [open data.txt w]
25  puts $fd "1\tone\t0\tI\t3.141592653"
26  puts $fd "2\ttwo\t1\tII\t2.15"
27  puts $fd "3\tthree\t1\tIII\t4221.0"
28  puts $fd "4\tfour\t2\tIV\t-0.0013442"
29  puts $fd "5\tfive\t2\tV\t-11"
30  puts $fd "6\tsix\t2\tVI\t0.123"
31  puts $fd "7\tseven\t2\tVII\t123.0"
32  puts $fd "8\teight\t3\tVIII\t-1.6"
33  close $fd
34  execsql {
35    CREATE TABLE t1(
36       n int,
37       v varchar(10),
38       log int,
39       roman varchar(10),
40       flt real
41    );
42    COPY t1 FROM 'data.txt'
43  }
44  file delete data.txt
45  db close
46  set ::bt [btree_open test.db]
47  btree_begin_transaction $::bt
48  set m [btree_get_meta $::bt]
49  set m [lreplace $m 2 2 3]
50  eval btree_update_meta $::bt $m
51  btree_commit $::bt
52  btree_close $::bt
53  sqlite db test.db
54  execsql {SELECT count(*) FROM t1}
55} {8}
56
57do_test format3-1.1 {
58  execsql {SELECT n FROM t1 ORDER BY n}
59} {1 2 3 4 5 6 7 8}
60do_test format3-1.1.1 {
61  execsql {SELECT n FROM t1 ORDER BY n ASC}
62} {1 2 3 4 5 6 7 8}
63do_test format3-1.1.1 {
64  execsql {SELECT ALL n FROM t1 ORDER BY n ASC}
65} {1 2 3 4 5 6 7 8}
66do_test format3-1.2 {
67  execsql {SELECT n FROM t1 ORDER BY n DESC}
68} {8 7 6 5 4 3 2 1}
69do_test format3-1.3a {
70  execsql {SELECT v FROM t1 ORDER BY v}
71} {eight five four one seven six three two}
72do_test format3-1.3b {
73  execsql {SELECT n FROM t1 ORDER BY v}
74} {8 5 4 1 7 6 3 2}
75do_test format3-1.4 {
76  execsql {SELECT n FROM t1 ORDER BY v DESC}
77} {2 3 6 7 1 4 5 8}
78do_test format3-1.5 {
79  execsql {SELECT flt FROM t1 ORDER BY flt}
80} {-11 -1.6 -0.0013442 0.123 2.15 3.141592653 123.0 4221.0}
81do_test format3-1.6 {
82  execsql {SELECT flt FROM t1 ORDER BY flt DESC}
83} {4221.0 123.0 3.141592653 2.15 0.123 -0.0013442 -1.6 -11}
84do_test format3-1.7 {
85  execsql {SELECT roman FROM t1 ORDER BY roman}
86} {I II III IV V VI VII VIII}
87do_test format3-1.8 {
88  execsql {SELECT n FROM t1 ORDER BY log, flt}
89} {1 2 3 5 4 6 7 8}
90do_test format3-1.8.1 {
91  execsql {SELECT n FROM t1 ORDER BY log asc, flt}
92} {1 2 3 5 4 6 7 8}
93do_test format3-1.8.2 {
94  execsql {SELECT n FROM t1 ORDER BY log, flt ASC}
95} {1 2 3 5 4 6 7 8}
96do_test format3-1.8.3 {
97  execsql {SELECT n FROM t1 ORDER BY log ASC, flt asc}
98} {1 2 3 5 4 6 7 8}
99do_test format3-1.9 {
100  execsql {SELECT n FROM t1 ORDER BY log, flt DESC}
101} {1 3 2 7 6 4 5 8}
102do_test format3-1.9.1 {
103  execsql {SELECT n FROM t1 ORDER BY log ASC, flt DESC}
104} {1 3 2 7 6 4 5 8}
105do_test format3-1.10 {
106  execsql {SELECT n FROM t1 ORDER BY log DESC, flt}
107} {8 5 4 6 7 2 3 1}
108do_test format3-1.11 {
109  execsql {SELECT n FROM t1 ORDER BY log DESC, flt DESC}
110} {8 7 6 4 5 3 2 1}
111
112# These tests are designed to reach some hard-to-reach places
113# inside the string comparison routines.
114#
115# (Later) The sorting behavior changed in 2.7.0.  But we will
116# keep these tests.  You can never have too many test cases!
117#
118do_test format3-2.1.1 {
119  execsql {
120    UPDATE t1 SET v='x' || -flt;
121    UPDATE t1 SET v='x-2b' where v=='x-0.123';
122    SELECT v FROM t1 ORDER BY v;
123  }
124} {x-123 x-2.15 x-2b x-3.141592653 x-4221 x0.0013442 x1.6 x11}
125do_test format3-2.1.2 {
126  execsql {
127    SELECT v FROM t1 ORDER BY substr(v,2,999);
128  }
129} {x-4221 x-123 x-3.141592653 x-2.15 x0.0013442 x1.6 x11 x-2b}
130do_test format3-2.1.3 {
131  execsql {
132    SELECT v FROM t1 ORDER BY substr(v,2,999)+0.0;
133  }
134} {x-4221 x-123 x-3.141592653 x-2.15 x-2b x0.0013442 x1.6 x11}
135do_test format3-2.1.4 {
136  execsql {
137    SELECT v FROM t1 ORDER BY substr(v,2,999) DESC;
138  }
139} {x-2b x11 x1.6 x0.0013442 x-2.15 x-3.141592653 x-123 x-4221}
140do_test format3-2.1.5 {
141  execsql {
142    SELECT v FROM t1 ORDER BY substr(v,2,999)+0.0 DESC;
143  }
144} {x11 x1.6 x0.0013442 x-2b x-2.15 x-3.141592653 x-123 x-4221}
145
146# This is a bug fix for 2.2.4.
147# Strings are normally mapped to upper-case for a caseless comparison.
148# But this can cause problems for characters in between 'Z' and 'a'.
149#
150do_test format3-3.1 {
151  execsql {
152    CREATE TABLE t2(a,b);
153    INSERT INTO t2 VALUES('AGLIENTU',1);
154    INSERT INTO t2 VALUES('AGLIE`',2);
155    INSERT INTO t2 VALUES('AGNA',3);
156    SELECT a, b FROM t2 ORDER BY a;
157  }
158} {AGLIENTU 1 AGLIE` 2 AGNA 3}
159do_test format3-3.2 {
160  execsql {
161    SELECT a, b FROM t2 ORDER BY a DESC;
162  }
163} {AGNA 3 AGLIE` 2 AGLIENTU 1}
164do_test format3-3.3 {
165  execsql {
166    DELETE FROM t2;
167    INSERT INTO t2 VALUES('aglientu',1);
168    INSERT INTO t2 VALUES('aglie`',2);
169    INSERT INTO t2 VALUES('agna',3);
170    SELECT a, b FROM t2 ORDER BY a;
171  }
172} {aglie` 2 aglientu 1 agna 3}
173do_test format3-3.4 {
174  execsql {
175    SELECT a, b FROM t2 ORDER BY a DESC;
176  }
177} {agna 3 aglientu 1 aglie` 2}
178
179# Version 2.7.0 testing.
180#
181do_test format3-4.1 {
182  execsql {
183    INSERT INTO t1 VALUES(9,'x2.7',3,'IX',4.0e5);
184    INSERT INTO t1 VALUES(10,'x5.0e10',3,'X',-4.0e5);
185    INSERT INTO t1 VALUES(11,'x-4.0e9',3,'XI',4.1e4);
186    INSERT INTO t1 VALUES(12,'x01234567890123456789',3,'XII',-4.2e3);
187    SELECT n FROM t1 ORDER BY n;
188  }
189} {1 2 3 4 5 6 7 8 9 10 11 12}
190do_test format3-4.2 {
191  execsql {
192    SELECT n||'' FROM t1 ORDER BY 1;
193  }
194} {1 2 3 4 5 6 7 8 9 10 11 12}
195do_test format3-4.3 {
196  execsql {
197    SELECT n+0 FROM t1 ORDER BY 1;
198  }
199} {1 2 3 4 5 6 7 8 9 10 11 12}
200do_test format3-4.4 {
201  execsql {
202    SELECT n||'' FROM t1 ORDER BY 1 DESC;
203  }
204} {12 11 10 9 8 7 6 5 4 3 2 1}
205do_test format3-4.5 {
206  execsql {
207    SELECT n+0 FROM t1 ORDER BY 1 DESC;
208  }
209} {12 11 10 9 8 7 6 5 4 3 2 1}
210do_test format3-4.6 {
211  execsql {
212    SELECT v FROM t1 ORDER BY 1;
213  }
214} {x-123 x-2.15 x-2b x-3.141592653 x-4.0e9 x-4221 x0.0013442 x01234567890123456789 x1.6 x11 x2.7 x5.0e10}
215do_test format3-4.7 {
216  execsql {
217    SELECT v FROM t1 ORDER BY 1 DESC;
218  }
219} {x5.0e10 x2.7 x11 x1.6 x01234567890123456789 x0.0013442 x-4221 x-4.0e9 x-3.141592653 x-2b x-2.15 x-123}
220do_test format3-4.8 {
221  execsql {
222    SELECT substr(v,2,99) FROM t1 ORDER BY 1;
223  }
224} {-4.0e9 -4221 -123 -3.141592653 -2.15 0.0013442 1.6 2.7 11 5.0e10 01234567890123456789 -2b}
225
226# Build some new test data, this time with indices.
227#
228do_test format3-5.0 {
229  execsql {
230    DROP TABLE t1;
231    CREATE TABLE t1(w int, x text, y blob);
232    DROP TABLE t2;
233    CREATE TABLE t2(p varchar(1), q clob, r real, s numeric(8));
234  }
235  for {set i 1} {$i<=100} {incr i} {
236    set w $i
237    set x [expr {int(log($i)/log(2))}]
238    set y [expr {$i*$i + 2*$i + 1}]
239    execsql "INSERT INTO t1 VALUES($w,$x,$y)"
240  }
241  execsql {
242    INSERT INTO t2 SELECT 101-w, x, (SELECT max(y) FROM t1)+1-y, y FROM t1;
243    CREATE INDEX i1w ON t1(w);
244    CREATE INDEX i1xy ON t1(x,y);
245    CREATE INDEX i2p ON t2(p);
246    CREATE INDEX i2r ON t2(r);
247    CREATE INDEX i2qs ON t2(q, s);
248  }
249} {}
250
251# Do an SQL statement.  Append the search count to the end of the result.
252#
253proc count sql {
254  set ::sqlite_search_count 0
255  return [concat [execsql $sql] $::sqlite_search_count]
256}
257
258# Verify that queries use an index.  We are using the special variable
259# "sqlite_search_count" which tallys the number of executions of MoveTo
260# and Next operators in the VDBE.  By verifing that the search count is
261# small we can be assured that indices are being used properly.
262#
263do_test format3-5.1 {
264  db close
265  sqlite db test.db
266  count {SELECT x, y FROM t1 WHERE w=10}
267} {3 121 3}
268do_test format3-5.2 {
269  count {SELECT x, y FROM t1 WHERE w=11}
270} {3 144 3}
271do_test format3-5.3 {
272  count {SELECT x, y FROM t1 WHERE 11=w}
273} {3 144 3}
274do_test format3-5.4 {
275  count {SELECT x, y FROM t1 WHERE 11=w AND x>2}
276} {3 144 3}
277do_test format3-5.5 {
278  count {SELECT x, y FROM t1 WHERE y<200 AND w=11 AND x>2}
279} {3 144 3}
280do_test format3-5.6 {
281  count {SELECT x, y FROM t1 WHERE y<200 AND x>2 AND w=11}
282} {3 144 3}
283do_test format3-5.7 {
284  count {SELECT x, y FROM t1 WHERE w=11 AND y<200 AND x>2}
285} {3 144 3}
286do_test format3-5.8 {
287  count {SELECT x, y FROM t1 WHERE w>10 AND y=144 AND x=3}
288} {3 144 3}
289do_test format3-5.9 {
290  count {SELECT x, y FROM t1 WHERE y=144 AND w>10 AND x=3}
291} {3 144 3}
292do_test format3-5.10 {
293  count {SELECT x, y FROM t1 WHERE x=3 AND w>=10 AND y=121}
294} {3 121 3}
295do_test format3-5.11 {
296  count {SELECT x, y FROM t1 WHERE x=3 AND y=100 AND w<10}
297} {3 100 3}
298
299# New for SQLite version 2.1: Verify that that inequality constraints
300# are used correctly.
301#
302do_test format3-5.12 {
303  count {SELECT w FROM t1 WHERE x=3 AND y<100}
304} {8 3}
305do_test format3-5.13 {
306  count {SELECT w FROM t1 WHERE x=3 AND 100>y}
307} {8 3}
308do_test format3-5.14 {
309  count {SELECT w FROM t1 WHERE 3=x AND y<100}
310} {8 3}
311do_test format3-5.15 {
312  count {SELECT w FROM t1 WHERE 3=x AND 100>y}
313} {8 3}
314do_test format3-5.16 {
315  count {SELECT w FROM t1 WHERE x=3 AND y<=100}
316} {8 9 5}
317do_test format3-5.17 {
318  count {SELECT w FROM t1 WHERE x=3 AND 100>=y}
319} {8 9 5}
320do_test format3-5.18 {
321  count {SELECT w FROM t1 WHERE x=3 AND y>225}
322} {15 3}
323do_test format3-5.19 {
324  count {SELECT w FROM t1 WHERE x=3 AND 225<y}
325} {15 3}
326do_test format3-5.20 {
327  count {SELECT w FROM t1 WHERE x=3 AND y>=225}
328} {14 15 5}
329do_test format3-5.21 {
330  count {SELECT w FROM t1 WHERE x=3 AND 225<=y}
331} {14 15 5}
332do_test format3-5.22 {
333  count {SELECT w FROM t1 WHERE x=3 AND y>121 AND y<196}
334} {11 12 5}
335do_test format3-5.23 {
336  count {SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<=196}
337} {10 11 12 13 9}
338do_test format3-5.24 {
339  count {SELECT w FROM t1 WHERE x=3 AND 121<y AND 196>y}
340} {11 12 5}
341do_test format3-5.25 {
342  count {SELECT w FROM t1 WHERE x=3 AND 121<=y AND 196>=y}
343} {10 11 12 13 9}
344
345# Need to work on optimizing the BETWEEN operator.
346#
347# do_test format3-5.26 {
348#   count {SELECT w FROM t1 WHERE x=3 AND y BETWEEN 121 AND 196}
349# } {10 11 12 13 9}
350
351do_test format3-5.27 {
352  count {SELECT w FROM t1 WHERE x=3 AND y+1==122}
353} {10 17}
354do_test format3-5.28 {
355  count {SELECT w FROM t1 WHERE x+1=4 AND y+1==122}
356} {10 99}
357do_test format3-5.29 {
358  count {SELECT w FROM t1 WHERE y==121}
359} {10 99}
360
361
362do_test format3-5.30 {
363  count {SELECT w FROM t1 WHERE w>97}
364} {98 99 100 6}
365do_test format3-5.31 {
366  count {SELECT w FROM t1 WHERE w>=97}
367} {97 98 99 100 8}
368do_test format3-5.33 {
369  count {SELECT w FROM t1 WHERE w==97}
370} {97 3}
371do_test format3-5.34 {
372  count {SELECT w FROM t1 WHERE w+1==98}
373} {97 99}
374do_test format3-5.35 {
375  count {SELECT w FROM t1 WHERE w<3}
376} {1 2 4}
377do_test format3-5.36 {
378  count {SELECT w FROM t1 WHERE w<=3}
379} {1 2 3 6}
380do_test format3-5.37 {
381  count {SELECT w FROM t1 WHERE w+1<=4 ORDER BY w}
382} {1 2 3 199}
383
384
385# Do the same kind of thing except use a join as the data source.
386#
387do_test format3-6.1 {
388  db close
389  sqlite db test.db
390  count {
391    SELECT w, p FROM t2, t1
392    WHERE x=q AND y=s AND r=8977
393  }
394} {34 67 6}
395do_test format3-6.2 {
396  count {
397    SELECT w, p FROM t2, t1
398    WHERE x=q AND s=y AND r=8977
399  }
400} {34 67 6}
401do_test format3-6.3 {
402  count {
403    SELECT w, p FROM t2, t1
404    WHERE x=q AND s=y AND r=8977 AND w>10
405  }
406} {34 67 6}
407do_test format3-6.4 {
408  count {
409    SELECT w, p FROM t2, t1
410    WHERE p<80 AND x=q AND s=y AND r=8977 AND w>10
411  }
412} {34 67 6}
413do_test format3-6.5 {
414  count {
415    SELECT w, p FROM t2, t1
416    WHERE p<80 AND x=q AND 8977=r AND s=y AND w>10
417  }
418} {34 67 6}
419do_test format3-6.6 {
420  count {
421    SELECT w, p FROM t2, t1
422    WHERE x=q AND p=77 AND s=y AND w>5
423  }
424} {24 77 6}
425do_test format3-6.7 {
426  count {
427    SELECT w, p FROM t1, t2
428    WHERE x=q AND p>77 AND s=y AND w=5
429  }
430} {5 96 6}
431
432# Lets do a 3-way join.
433#
434do_test format3-7.1 {
435  count {
436    SELECT A.w, B.p, C.w FROM t1 as A, t2 as B, t1 as C
437    WHERE C.w=101-B.p AND B.r=10202-A.y AND A.w=11
438  }
439} {11 90 11 9}
440do_test format3-7.2 {
441  count {
442    SELECT A.w, B.p, C.w FROM t1 as A, t2 as B, t1 as C
443    WHERE C.w=101-B.p AND B.r=10202-A.y AND A.w=12
444  }
445} {12 89 12 9}
446do_test format3-7.3 {
447  count {
448    SELECT A.w, B.p, C.w FROM t1 as A, t2 as B, t1 as C
449    WHERE A.w=15 AND B.p=C.w AND B.r=10202-A.y
450  }
451} {15 86 86 9}
452
453# Test to see that the special case of a constant WHERE clause is
454# handled.
455#
456do_test format3-8.1 {
457  count {
458    SELECT * FROM t1 WHERE 0
459  }
460} {0}
461do_test format3-8.2 {
462  count {
463    SELECT * FROM t1 WHERE 1 LIMIT 1
464  }
465} {1 0 4 1}
466do_test format3-8.3 {
467  execsql {
468    SELECT 99 WHERE 0
469  }
470} {}
471do_test format3-8.4 {
472  execsql {
473    SELECT 99 WHERE 1
474  }
475} {99}
476
477# Verify that IN operators in a WHERE clause are handled correctly.
478#
479do_test format3-9.1 {
480  count {
481    SELECT * FROM t1 WHERE rowid IN (1,2,3,1234) order by 1;
482  }
483} {1 0 4 2 1 9 3 1 16 0}
484do_test format3-9.2 {
485  count {
486    SELECT * FROM t1 WHERE rowid+0 IN (1,2,3,1234) order by 1;
487  }
488} {1 0 4 2 1 9 3 1 16 199}
489do_test format3-9.3 {
490  count {
491    SELECT * FROM t1 WHERE w IN (-1,1,2,3) order by 1;
492  }
493} {1 0 4 2 1 9 3 1 16 10}
494do_test format3-9.4 {
495  count {
496    SELECT * FROM t1 WHERE w+0 IN (-1,1,2,3) order by 1;
497  }
498} {1 0 4 2 1 9 3 1 16 199}
499do_test format3-9.5 {
500  count {
501    SELECT * FROM t1 WHERE rowid IN
502       (select rowid from t1 where rowid IN (-1,2,4))
503    ORDER BY 1;
504  }
505} {2 1 9 4 2 25 1}
506do_test format3-9.6 {
507  count {
508    SELECT * FROM t1 WHERE rowid+0 IN
509       (select rowid from t1 where rowid IN (-1,2,4))
510    ORDER BY 1;
511  }
512} {2 1 9 4 2 25 199}
513do_test format3-9.7 {
514  count {
515    SELECT * FROM t1 WHERE w IN
516       (select rowid from t1 where rowid IN (-1,2,4))
517    ORDER BY 1;
518  }
519} {2 1 9 4 2 25 7}
520do_test format3-9.8 {
521  count {
522    SELECT * FROM t1 WHERE w+0 IN
523       (select rowid from t1 where rowid IN (-1,2,4))
524    ORDER BY 1;
525  }
526} {2 1 9 4 2 25 199}
527do_test format3-9.9 {
528  count {
529    SELECT * FROM t1 WHERE x IN (1,7) ORDER BY 1;
530  }
531} {2 1 9 3 1 16 6}
532do_test format3-9.10 {
533  count {
534    SELECT * FROM t1 WHERE x+0 IN (1,7) ORDER BY 1;
535  }
536} {2 1 9 3 1 16 199}
537do_test format3-9.11 {
538  count {
539    SELECT * FROM t1 WHERE y IN (6400,8100) ORDER BY 1;
540  }
541} {79 6 6400 89 6 8100 199}
542do_test format3-9.12 {
543  count {
544    SELECT * FROM t1 WHERE x=6 AND y IN (6400,8100) ORDER BY 1;
545  }
546} {79 6 6400 89 6 8100 74}
547do_test format3-9.13 {
548  count {
549    SELECT * FROM t1 WHERE x IN (1,7) AND y NOT IN (6400,8100) ORDER BY 1;
550  }
551} {2 1 9 3 1 16 6}
552do_test format3-9.14 {
553  count {
554    SELECT * FROM t1 WHERE x IN (1,7) AND y IN (9,10) ORDER BY 1;
555  }
556} {2 1 9 6}
557
558# This procedure executes the SQL.  Then it checks the generated program
559# for the SQL and appends a "nosort" to the result if the program contains the
560# SortCallback opcode.  If the program does not contain the SortCallback
561# opcode it appends "sort"
562#
563proc cksort {sql} {
564  set data [execsql $sql]
565  set prog [execsql "EXPLAIN $sql"]
566  if {[regexp SortCallback $prog]} {set x sort} {set x nosort}
567  lappend data $x
568  return $data
569}
570# Check out the logic that attempts to implement the ORDER BY clause
571# using an index rather than by sorting.
572#
573do_test format3-10.1 {
574  execsql {
575    CREATE TABLE t3(a,b,c);
576    CREATE INDEX t3a ON t3(a);
577    CREATE INDEX t3bc ON t3(b,c);
578    CREATE INDEX t3acb ON t3(a,c,b);
579    INSERT INTO t3 SELECT w, 101-w, y FROM t1;
580    SELECT count(*), sum(a), sum(b), sum(c) FROM t3;
581  }
582} {100 5050 5050 348550}
583do_test format3-10.2 {
584  cksort {
585    SELECT * FROM t3 ORDER BY a LIMIT 3
586  }
587} {1 100 4 2 99 9 3 98 16 nosort}
588do_test format3-10.3 {
589  cksort {
590    SELECT * FROM t3 ORDER BY a+1 LIMIT 3
591  }
592} {1 100 4 2 99 9 3 98 16 sort}
593do_test format3-10.4 {
594  cksort {
595    SELECT * FROM t3 WHERE a<10 ORDER BY a LIMIT 3
596  }
597} {1 100 4 2 99 9 3 98 16 nosort}
598do_test format3-10.5 {
599  cksort {
600    SELECT * FROM t3 WHERE a>0 AND a<10 ORDER BY a LIMIT 3
601  }
602} {1 100 4 2 99 9 3 98 16 nosort}
603do_test format3-10.6 {
604  cksort {
605    SELECT * FROM t3 WHERE a>0 ORDER BY a LIMIT 3
606  }
607} {1 100 4 2 99 9 3 98 16 nosort}
608do_test format3-10.7 {
609  cksort {
610    SELECT * FROM t3 WHERE b>0 ORDER BY a LIMIT 3
611  }
612} {1 100 4 2 99 9 3 98 16 sort}
613do_test format3-10.8 {
614  cksort {
615    SELECT * FROM t3 WHERE a IN (3,5,7,1,9,4,2) ORDER BY a LIMIT 3
616  }
617} {1 100 4 2 99 9 3 98 16 sort}
618do_test format3-10.9 {
619  cksort {
620    SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a LIMIT 3
621  }
622} {1 100 4 nosort}
623do_test format3-10.10 {
624  cksort {
625    SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a LIMIT 3
626  }
627} {1 100 4 nosort}
628do_test format3-10.11 {
629  cksort {
630    SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a,c LIMIT 3
631  }
632} {1 100 4 nosort}
633do_test format3-10.12 {
634  cksort {
635    SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a,c,b LIMIT 3
636  }
637} {1 100 4 nosort}
638do_test format3-10.13 {
639  cksort {
640    SELECT * FROM t3 WHERE a>0 ORDER BY a DESC LIMIT 3
641  }
642} {100 1 10201 99 2 10000 98 3 9801 nosort}
643do_test format3-10.13.1 {
644  cksort {
645    SELECT * FROM t3 WHERE a>0 ORDER BY a+1 DESC LIMIT 3
646  }
647} {100 1 10201 99 2 10000 98 3 9801 sort}
648do_test format3-10.14 {
649  cksort {
650    SELECT * FROM t3 ORDER BY b LIMIT 3
651  }
652} {100 1 10201 99 2 10000 98 3 9801 nosort}
653do_test format3-10.15 {
654  cksort {
655    SELECT t3.a, t1.x FROM t3, t1 WHERE t3.a=t1.w ORDER BY t3.a LIMIT 3
656  }
657} {1 0 2 1 3 1 nosort}
658do_test format3-10.16 {
659  cksort {
660    SELECT t3.a, t1.x FROM t3, t1 WHERE t3.a=t1.w ORDER BY t1.x, t3.a LIMIT 3
661  }
662} {1 0 2 1 3 1 sort}
663do_test format3-10.17 {
664  cksort {
665    SELECT y FROM t1 ORDER BY w COLLATE text LIMIT 3;
666  }
667} {4 121 10201 sort}
668do_test format3-10.18 {
669  cksort {
670    SELECT y FROM t1 ORDER BY w COLLATE numeric LIMIT 3;
671  }
672} {4 9 16 sort}
673do_test format3-10.19 {
674  cksort {
675    SELECT y FROM t1 ORDER BY w LIMIT 3;
676  }
677} {4 9 16 nosort}
678
679# Check that all comparisons are numeric.  Similar tests in misc1.test
680# check the same comparisons on a format4+ database and find that some
681# are numeric and some are text.
682#
683do_test format3-11.1 {
684  execsql {SELECT '0'=='0.0'}
685} {1}
686do_test format3-11.2 {
687  execsql {SELECT '0'==0.0}
688} {1}
689do_test format3-11.3 {
690  execsql {SELECT '123456789012345678901'=='123456789012345678900'}
691} {1}
692do_test format3-11.4 {
693  execsql {
694    CREATE TABLE t6(a INT UNIQUE, b TEXT UNIQUE);
695    INSERT INTO t6 VALUES('0','0.0');
696    SELECT * FROM t6;
697  }
698} {0 0.0}
699do_test format3-11.5 {
700  execsql {
701    INSERT OR IGNORE INTO t6 VALUES(0.0,'x');
702    SELECT * FROM t6;
703  }
704} {0 0.0}
705do_test format3-11.6 {
706  execsql {
707    INSERT OR IGNORE INTO t6 VALUES('y',0);
708    SELECT * FROM t6;
709  }
710} {0 0.0}
711do_test format3-11.7 {
712  execsql {
713    CREATE TABLE t7(x INTEGER, y TEXT, z);
714    INSERT INTO t7 VALUES(0,0,1);
715    INSERT INTO t7 VALUES(0.0,0,2);
716    INSERT INTO t7 VALUES(0,0.0,3);
717    INSERT INTO t7 VALUES(0.0,0.0,4);
718    SELECT DISTINCT x, y FROM t7 ORDER BY z;
719  }
720} {0 0}
721
722# Make sure attempts to attach a format 3 database fail.
723#
724do_test format3-12.1 {
725  file delete -force test2.db
726  sqlite db2 test2.db
727  catchsql {
728    CREATE TABLE t8(x,y);
729    ATTACH DATABASE 'test.db' AS format3;
730  } db2;
731} {1 {incompatible file format in auxiliary database: format3}}
732do_test format3-12.2 {
733  catchsql {
734    ATTACH DATABASE 'test2.db' AS test2;
735  }
736} {1 {cannot attach auxiliary databases to an older format master database}}
737db2 close
738
739finish_test
740