xref: /titanic_50/usr/src/lib/libsqlite/test/misc1.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.
15*c5c4113dSnw141292#
16*c5c4113dSnw141292# This file implements tests for miscellanous features that were
17*c5c4113dSnw141292# left out of other test files.
18*c5c4113dSnw141292#
19*c5c4113dSnw141292# $Id: misc1.test,v 1.23 2003/08/05 13:13:39 drh Exp $
20*c5c4113dSnw141292
21*c5c4113dSnw141292set testdir [file dirname $argv0]
22*c5c4113dSnw141292source $testdir/tester.tcl
23*c5c4113dSnw141292
24*c5c4113dSnw141292# Test the creation and use of tables that have a large number
25*c5c4113dSnw141292# of columns.
26*c5c4113dSnw141292#
27*c5c4113dSnw141292do_test misc1-1.1 {
28*c5c4113dSnw141292  set cmd "CREATE TABLE manycol(x0 text"
29*c5c4113dSnw141292  for {set i 1} {$i<=99} {incr i} {
30*c5c4113dSnw141292    append cmd ",x$i text"
31*c5c4113dSnw141292  }
32*c5c4113dSnw141292  append cmd ")";
33*c5c4113dSnw141292  execsql $cmd
34*c5c4113dSnw141292  set cmd "INSERT INTO manycol VALUES(0"
35*c5c4113dSnw141292  for {set i 1} {$i<=99} {incr i} {
36*c5c4113dSnw141292    append cmd ",$i"
37*c5c4113dSnw141292  }
38*c5c4113dSnw141292  append cmd ")";
39*c5c4113dSnw141292  execsql $cmd
40*c5c4113dSnw141292  execsql "SELECT x99 FROM manycol"
41*c5c4113dSnw141292} 99
42*c5c4113dSnw141292do_test misc1-1.2 {
43*c5c4113dSnw141292  execsql {SELECT x0, x10, x25, x50, x75 FROM manycol}
44*c5c4113dSnw141292} {0 10 25 50 75}
45*c5c4113dSnw141292do_test misc1-1.3.1 {
46*c5c4113dSnw141292  for {set j 100} {$j<=1000} {incr j 100} {
47*c5c4113dSnw141292    set cmd "INSERT INTO manycol VALUES($j"
48*c5c4113dSnw141292    for {set i 1} {$i<=99} {incr i} {
49*c5c4113dSnw141292      append cmd ",[expr {$i+$j}]"
50*c5c4113dSnw141292    }
51*c5c4113dSnw141292    append cmd ")"
52*c5c4113dSnw141292    execsql $cmd
53*c5c4113dSnw141292  }
54*c5c4113dSnw141292  execsql {SELECT x50 FROM manycol ORDER BY x80+0}
55*c5c4113dSnw141292} {50 150 250 350 450 550 650 750 850 950 1050}
56*c5c4113dSnw141292do_test misc1-1.3.2 {
57*c5c4113dSnw141292  execsql {SELECT x50 FROM manycol ORDER BY x80}
58*c5c4113dSnw141292} {1050 150 250 350 450 550 650 750 50 850 950}
59*c5c4113dSnw141292do_test misc1-1.4 {
60*c5c4113dSnw141292  execsql {SELECT x75 FROM manycol WHERE x50=350}
61*c5c4113dSnw141292} 375
62*c5c4113dSnw141292do_test misc1-1.5 {
63*c5c4113dSnw141292  execsql {SELECT x50 FROM manycol WHERE x99=599}
64*c5c4113dSnw141292} 550
65*c5c4113dSnw141292do_test misc1-1.6 {
66*c5c4113dSnw141292  execsql {CREATE INDEX manycol_idx1 ON manycol(x99)}
67*c5c4113dSnw141292  execsql {SELECT x50 FROM manycol WHERE x99=899}
68*c5c4113dSnw141292} 850
69*c5c4113dSnw141292do_test misc1-1.7 {
70*c5c4113dSnw141292  execsql {SELECT count(*) FROM manycol}
71*c5c4113dSnw141292} 11
72*c5c4113dSnw141292do_test misc1-1.8 {
73*c5c4113dSnw141292  execsql {DELETE FROM manycol WHERE x98=1234}
74*c5c4113dSnw141292  execsql {SELECT count(*) FROM manycol}
75*c5c4113dSnw141292} 11
76*c5c4113dSnw141292do_test misc1-1.9 {
77*c5c4113dSnw141292  execsql {DELETE FROM manycol WHERE x98=998}
78*c5c4113dSnw141292  execsql {SELECT count(*) FROM manycol}
79*c5c4113dSnw141292} 10
80*c5c4113dSnw141292do_test misc1-1.10 {
81*c5c4113dSnw141292  execsql {DELETE FROM manycol WHERE x99=500}
82*c5c4113dSnw141292  execsql {SELECT count(*) FROM manycol}
83*c5c4113dSnw141292} 10
84*c5c4113dSnw141292do_test misc1-1.11 {
85*c5c4113dSnw141292  execsql {DELETE FROM manycol WHERE x99=599}
86*c5c4113dSnw141292  execsql {SELECT count(*) FROM manycol}
87*c5c4113dSnw141292} 9
88*c5c4113dSnw141292
89*c5c4113dSnw141292# Check GROUP BY expressions that name two or more columns.
90*c5c4113dSnw141292#
91*c5c4113dSnw141292do_test misc1-2.1 {
92*c5c4113dSnw141292  execsql {
93*c5c4113dSnw141292    BEGIN TRANSACTION;
94*c5c4113dSnw141292    CREATE TABLE agger(one text, two text, three text, four text);
95*c5c4113dSnw141292    INSERT INTO agger VALUES(1, 'one', 'hello', 'yes');
96*c5c4113dSnw141292    INSERT INTO agger VALUES(2, 'two', 'howdy', 'no');
97*c5c4113dSnw141292    INSERT INTO agger VALUES(3, 'thr', 'howareya', 'yes');
98*c5c4113dSnw141292    INSERT INTO agger VALUES(4, 'two', 'lothere', 'yes');
99*c5c4113dSnw141292    INSERT INTO agger VALUES(5, 'one', 'atcha', 'yes');
100*c5c4113dSnw141292    INSERT INTO agger VALUES(6, 'two', 'hello', 'no');
101*c5c4113dSnw141292    COMMIT
102*c5c4113dSnw141292  }
103*c5c4113dSnw141292  execsql {SELECT count(*) FROM agger}
104*c5c4113dSnw141292} 6
105*c5c4113dSnw141292do_test misc1-2.2 {
106*c5c4113dSnw141292  execsql {SELECT sum(one), two, four FROM agger
107*c5c4113dSnw141292           GROUP BY two, four ORDER BY sum(one) desc}
108*c5c4113dSnw141292} {8 two no 6 one yes 4 two yes 3 thr yes}
109*c5c4113dSnw141292do_test misc1-2.3 {
110*c5c4113dSnw141292  execsql {SELECT sum((one)), (two), (four) FROM agger
111*c5c4113dSnw141292           GROUP BY (two), (four) ORDER BY sum(one) desc}
112*c5c4113dSnw141292} {8 two no 6 one yes 4 two yes 3 thr yes}
113*c5c4113dSnw141292
114*c5c4113dSnw141292# Here's a test for a bug found by Joel Lucsy.  The code below
115*c5c4113dSnw141292# was causing an assertion failure.
116*c5c4113dSnw141292#
117*c5c4113dSnw141292do_test misc1-3.1 {
118*c5c4113dSnw141292  set r [execsql {
119*c5c4113dSnw141292    CREATE TABLE t1(a);
120*c5c4113dSnw141292    INSERT INTO t1 VALUES('hi');
121*c5c4113dSnw141292    PRAGMA full_column_names=on;
122*c5c4113dSnw141292    SELECT rowid, * FROM t1;
123*c5c4113dSnw141292  }]
124*c5c4113dSnw141292  lindex $r 1
125*c5c4113dSnw141292} {hi}
126*c5c4113dSnw141292
127*c5c4113dSnw141292# Here's a test for yet another bug found by Joel Lucsy.  The code
128*c5c4113dSnw141292# below was causing an assertion failure.
129*c5c4113dSnw141292#
130*c5c4113dSnw141292do_test misc1-4.1 {
131*c5c4113dSnw141292  execsql {
132*c5c4113dSnw141292    BEGIN;
133*c5c4113dSnw141292    CREATE TABLE t2(a);
134*c5c4113dSnw141292    INSERT INTO t2 VALUES('This is a long string to use up a lot of disk -');
135*c5c4113dSnw141292    UPDATE t2 SET a=a||a||a||a;
136*c5c4113dSnw141292    INSERT INTO t2 SELECT '1 - ' || a FROM t2;
137*c5c4113dSnw141292    INSERT INTO t2 SELECT '2 - ' || a FROM t2;
138*c5c4113dSnw141292    INSERT INTO t2 SELECT '3 - ' || a FROM t2;
139*c5c4113dSnw141292    INSERT INTO t2 SELECT '4 - ' || a FROM t2;
140*c5c4113dSnw141292    INSERT INTO t2 SELECT '5 - ' || a FROM t2;
141*c5c4113dSnw141292    INSERT INTO t2 SELECT '6 - ' || a FROM t2;
142*c5c4113dSnw141292    COMMIT;
143*c5c4113dSnw141292    SELECT count(*) FROM t2;
144*c5c4113dSnw141292  }
145*c5c4113dSnw141292} {64}
146*c5c4113dSnw141292
147*c5c4113dSnw141292# Make sure we actually see a semicolon or end-of-file in the SQL input
148*c5c4113dSnw141292# before executing a command.  Thus if "WHERE" is misspelled on an UPDATE,
149*c5c4113dSnw141292# the user won't accidently update every record.
150*c5c4113dSnw141292#
151*c5c4113dSnw141292do_test misc1-5.1 {
152*c5c4113dSnw141292  catchsql {
153*c5c4113dSnw141292    CREATE TABLE t3(a,b);
154*c5c4113dSnw141292    INSERT INTO t3 VALUES(1,2);
155*c5c4113dSnw141292    INSERT INTO t3 VALUES(3,4);
156*c5c4113dSnw141292    UPDATE t3 SET a=0 WHEREwww b=2;
157*c5c4113dSnw141292  }
158*c5c4113dSnw141292} {1 {near "WHEREwww": syntax error}}
159*c5c4113dSnw141292do_test misc1-5.2 {
160*c5c4113dSnw141292  execsql {
161*c5c4113dSnw141292    SELECT * FROM t3 ORDER BY a;
162*c5c4113dSnw141292  }
163*c5c4113dSnw141292} {1 2 3 4}
164*c5c4113dSnw141292
165*c5c4113dSnw141292# Certain keywords (especially non-standard keywords like "REPLACE") can
166*c5c4113dSnw141292# also be used as identifiers.  The way this works in the parser is that
167*c5c4113dSnw141292# the parser first detects a syntax error, the error handling routine
168*c5c4113dSnw141292# sees that the special keyword caused the error, then replaces the keyword
169*c5c4113dSnw141292# with "ID" and tries again.
170*c5c4113dSnw141292#
171*c5c4113dSnw141292# Check the operation of this logic.
172*c5c4113dSnw141292#
173*c5c4113dSnw141292do_test misc1-6.1 {
174*c5c4113dSnw141292  catchsql {
175*c5c4113dSnw141292    CREATE TABLE t4(
176*c5c4113dSnw141292      abort, asc, begin, cluster, conflict, copy, delimiters, desc, end,
177*c5c4113dSnw141292      explain, fail, ignore, key, offset, pragma, replace, temp,
178*c5c4113dSnw141292      vacuum, view
179*c5c4113dSnw141292    );
180*c5c4113dSnw141292  }
181*c5c4113dSnw141292} {0 {}}
182*c5c4113dSnw141292do_test misc1-6.2 {
183*c5c4113dSnw141292  catchsql {
184*c5c4113dSnw141292    INSERT INTO t4
185*c5c4113dSnw141292       VALUES(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19);
186*c5c4113dSnw141292  }
187*c5c4113dSnw141292} {0 {}}
188*c5c4113dSnw141292do_test misc1-6.3 {
189*c5c4113dSnw141292  execsql {
190*c5c4113dSnw141292    SELECT * FROM t4
191*c5c4113dSnw141292  }
192*c5c4113dSnw141292} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19}
193*c5c4113dSnw141292do_test misc1-6.4 {
194*c5c4113dSnw141292  execsql {
195*c5c4113dSnw141292    SELECT abort+asc,max(key,pragma,temp) FROM t4
196*c5c4113dSnw141292  }
197*c5c4113dSnw141292} {3 17}
198*c5c4113dSnw141292
199*c5c4113dSnw141292# Test for multi-column primary keys, and for multiple primary keys.
200*c5c4113dSnw141292#
201*c5c4113dSnw141292do_test misc1-7.1 {
202*c5c4113dSnw141292  catchsql {
203*c5c4113dSnw141292    CREATE TABLE error1(
204*c5c4113dSnw141292      a TYPE PRIMARY KEY,
205*c5c4113dSnw141292      b TYPE PRIMARY KEY
206*c5c4113dSnw141292    );
207*c5c4113dSnw141292  }
208*c5c4113dSnw141292} {1 {table "error1" has more than one primary key}}
209*c5c4113dSnw141292do_test misc1-7.2 {
210*c5c4113dSnw141292  catchsql {
211*c5c4113dSnw141292    CREATE TABLE error1(
212*c5c4113dSnw141292      a INTEGER PRIMARY KEY,
213*c5c4113dSnw141292      b TYPE PRIMARY KEY
214*c5c4113dSnw141292    );
215*c5c4113dSnw141292  }
216*c5c4113dSnw141292} {1 {table "error1" has more than one primary key}}
217*c5c4113dSnw141292do_test misc1-7.3 {
218*c5c4113dSnw141292  execsql {
219*c5c4113dSnw141292    CREATE TABLE t5(a,b,c,PRIMARY KEY(a,b));
220*c5c4113dSnw141292    INSERT INTO t5 VALUES(1,2,3);
221*c5c4113dSnw141292    SELECT * FROM t5 ORDER BY a;
222*c5c4113dSnw141292  }
223*c5c4113dSnw141292} {1 2 3}
224*c5c4113dSnw141292do_test misc1-7.4 {
225*c5c4113dSnw141292  catchsql {
226*c5c4113dSnw141292    INSERT INTO t5 VALUES(1,2,4);
227*c5c4113dSnw141292  }
228*c5c4113dSnw141292} {1 {columns a, b are not unique}}
229*c5c4113dSnw141292do_test misc1-7.5 {
230*c5c4113dSnw141292  catchsql {
231*c5c4113dSnw141292    INSERT INTO t5 VALUES(0,2,4);
232*c5c4113dSnw141292  }
233*c5c4113dSnw141292} {0 {}}
234*c5c4113dSnw141292do_test misc1-7.6 {
235*c5c4113dSnw141292  execsql {
236*c5c4113dSnw141292    SELECT * FROM t5 ORDER BY a;
237*c5c4113dSnw141292  }
238*c5c4113dSnw141292} {0 2 4 1 2 3}
239*c5c4113dSnw141292
240*c5c4113dSnw141292do_test misc1-8.1 {
241*c5c4113dSnw141292  catchsql {
242*c5c4113dSnw141292    SELECT *;
243*c5c4113dSnw141292  }
244*c5c4113dSnw141292} {1 {no tables specified}}
245*c5c4113dSnw141292do_test misc1-8.2 {
246*c5c4113dSnw141292  catchsql {
247*c5c4113dSnw141292    SELECT t1.*;
248*c5c4113dSnw141292  }
249*c5c4113dSnw141292} {1 {no such table: t1}}
250*c5c4113dSnw141292
251*c5c4113dSnw141292execsql {
252*c5c4113dSnw141292  DROP TABLE t1;
253*c5c4113dSnw141292  DROP TABLE t2;
254*c5c4113dSnw141292  DROP TABLE t3;
255*c5c4113dSnw141292  DROP TABLE t4;
256*c5c4113dSnw141292}
257*c5c4113dSnw141292
258*c5c4113dSnw141292# If an integer is too big to be represented as a 32-bit machine integer,
259*c5c4113dSnw141292# then treat it as a string.
260*c5c4113dSnw141292#
261*c5c4113dSnw141292do_test misc1-9.1 {
262*c5c4113dSnw141292  catchsql {
263*c5c4113dSnw141292    CREATE TABLE t1(a unique not null, b unique not null);
264*c5c4113dSnw141292    INSERT INTO t1 VALUES('a',12345678901234567890);
265*c5c4113dSnw141292    INSERT INTO t1 VALUES('b',12345678911234567890);
266*c5c4113dSnw141292    INSERT INTO t1 VALUES('c',12345678921234567890);
267*c5c4113dSnw141292    SELECT * FROM t1;
268*c5c4113dSnw141292  }
269*c5c4113dSnw141292} {0 {a 12345678901234567890 b 12345678911234567890 c 12345678921234567890}}
270*c5c4113dSnw141292
271*c5c4113dSnw141292# A WHERE clause is not allowed to contain more than 99 terms.  Check to
272*c5c4113dSnw141292# make sure this limit is enforced.
273*c5c4113dSnw141292#
274*c5c4113dSnw141292do_test misc1-10.0 {
275*c5c4113dSnw141292  execsql {SELECT count(*) FROM manycol}
276*c5c4113dSnw141292} {9}
277*c5c4113dSnw141292do_test misc1-10.1 {
278*c5c4113dSnw141292  set ::where {WHERE x0>=0}
279*c5c4113dSnw141292  for {set i 1} {$i<=99} {incr i} {
280*c5c4113dSnw141292    append ::where " AND x$i<>0"
281*c5c4113dSnw141292  }
282*c5c4113dSnw141292  catchsql "SELECT count(*) FROM manycol $::where"
283*c5c4113dSnw141292} {0 9}
284*c5c4113dSnw141292do_test misc1-10.2 {
285*c5c4113dSnw141292  catchsql "SELECT count(*) FROM manycol $::where AND rowid>0"
286*c5c4113dSnw141292} {1 {WHERE clause too complex - no more than 100 terms allowed}}
287*c5c4113dSnw141292do_test misc1-10.3 {
288*c5c4113dSnw141292  regsub "x0>=0" $::where "x0=0" ::where
289*c5c4113dSnw141292  catchsql "DELETE FROM manycol $::where"
290*c5c4113dSnw141292} {0 {}}
291*c5c4113dSnw141292do_test misc1-10.4 {
292*c5c4113dSnw141292  execsql {SELECT count(*) FROM manycol}
293*c5c4113dSnw141292} {8}
294*c5c4113dSnw141292do_test misc1-10.5 {
295*c5c4113dSnw141292  catchsql "DELETE FROM manycol $::where AND rowid>0"
296*c5c4113dSnw141292} {1 {WHERE clause too complex - no more than 100 terms allowed}}
297*c5c4113dSnw141292do_test misc1-10.6 {
298*c5c4113dSnw141292  execsql {SELECT x1 FROM manycol WHERE x0=100}
299*c5c4113dSnw141292} {101}
300*c5c4113dSnw141292do_test misc1-10.7 {
301*c5c4113dSnw141292  regsub "x0=0" $::where "x0=100" ::where
302*c5c4113dSnw141292  catchsql "UPDATE manycol SET x1=x1+1 $::where"
303*c5c4113dSnw141292} {0 {}}
304*c5c4113dSnw141292do_test misc1-10.8 {
305*c5c4113dSnw141292  execsql {SELECT x1 FROM manycol WHERE x0=100}
306*c5c4113dSnw141292} {102}
307*c5c4113dSnw141292do_test misc1-10.9 {
308*c5c4113dSnw141292  catchsql "UPDATE manycol SET x1=x1+1 $::where AND rowid>0"
309*c5c4113dSnw141292} {1 {WHERE clause too complex - no more than 100 terms allowed}}
310*c5c4113dSnw141292do_test misc1-10.10 {
311*c5c4113dSnw141292  execsql {SELECT x1 FROM manycol WHERE x0=100}
312*c5c4113dSnw141292} {102}
313*c5c4113dSnw141292
314*c5c4113dSnw141292# Make sure the initialization works even if a database is opened while
315*c5c4113dSnw141292# another process has the database locked.
316*c5c4113dSnw141292#
317*c5c4113dSnw141292do_test misc1-11.1 {
318*c5c4113dSnw141292  execsql {BEGIN}
319*c5c4113dSnw141292  sqlite db2 test.db
320*c5c4113dSnw141292  set rc [catch {db2 eval {SELECT count(*) FROM t1}} msg]
321*c5c4113dSnw141292  lappend rc $msg
322*c5c4113dSnw141292} {1 {database is locked}}
323*c5c4113dSnw141292do_test misc1-11.2 {
324*c5c4113dSnw141292  execsql {COMMIT}
325*c5c4113dSnw141292  set rc [catch {db2 eval {SELECT count(*) FROM t1}} msg]
326*c5c4113dSnw141292  db2 close
327*c5c4113dSnw141292  lappend rc $msg
328*c5c4113dSnw141292} {0 3}
329*c5c4113dSnw141292
330*c5c4113dSnw141292# Make sure string comparisons really do compare strings in format4+.
331*c5c4113dSnw141292# Similar tests in the format3.test file show that for format3 and earlier
332*c5c4113dSnw141292# all comparisions where numeric if either operand looked like a number.
333*c5c4113dSnw141292#
334*c5c4113dSnw141292do_test misc1-12.1 {
335*c5c4113dSnw141292  execsql {SELECT '0'=='0.0'}
336*c5c4113dSnw141292} {0}
337*c5c4113dSnw141292do_test misc1-12.2 {
338*c5c4113dSnw141292  execsql {SELECT '0'==0.0}
339*c5c4113dSnw141292} {1}
340*c5c4113dSnw141292do_test misc1-12.3 {
341*c5c4113dSnw141292  execsql {SELECT '12345678901234567890'=='12345678901234567891'}
342*c5c4113dSnw141292} {0}
343*c5c4113dSnw141292do_test misc1-12.4 {
344*c5c4113dSnw141292  execsql {
345*c5c4113dSnw141292    CREATE TABLE t6(a INT UNIQUE, b TEXT UNIQUE);
346*c5c4113dSnw141292    INSERT INTO t6 VALUES('0','0.0');
347*c5c4113dSnw141292    SELECT * FROM t6;
348*c5c4113dSnw141292  }
349*c5c4113dSnw141292} {0 0.0}
350*c5c4113dSnw141292do_test misc1-12.5 {
351*c5c4113dSnw141292  execsql {
352*c5c4113dSnw141292    INSERT OR IGNORE INTO t6 VALUES(0.0,'x');
353*c5c4113dSnw141292    SELECT * FROM t6;
354*c5c4113dSnw141292  }
355*c5c4113dSnw141292} {0 0.0}
356*c5c4113dSnw141292do_test misc1-12.6 {
357*c5c4113dSnw141292  execsql {
358*c5c4113dSnw141292    INSERT OR IGNORE INTO t6 VALUES('y',0);
359*c5c4113dSnw141292    SELECT * FROM t6;
360*c5c4113dSnw141292  }
361*c5c4113dSnw141292} {0 0.0 y 0}
362*c5c4113dSnw141292do_test misc1-12.7 {
363*c5c4113dSnw141292  execsql {
364*c5c4113dSnw141292    CREATE TABLE t7(x INTEGER, y TEXT, z);
365*c5c4113dSnw141292    INSERT INTO t7 VALUES(0,0,1);
366*c5c4113dSnw141292    INSERT INTO t7 VALUES(0.0,0,2);
367*c5c4113dSnw141292    INSERT INTO t7 VALUES(0,0.0,3);
368*c5c4113dSnw141292    INSERT INTO t7 VALUES(0.0,0.0,4);
369*c5c4113dSnw141292    SELECT DISTINCT x, y FROM t7 ORDER BY z;
370*c5c4113dSnw141292  }
371*c5c4113dSnw141292} {0 0 0 0.0}
372*c5c4113dSnw141292do_test misc1-12.8 {
373*c5c4113dSnw141292  execsql {
374*c5c4113dSnw141292    SELECT min(z), max(z), count(z) FROM t7 GROUP BY x ORDER BY 1;
375*c5c4113dSnw141292  }
376*c5c4113dSnw141292} {1 4 4}
377*c5c4113dSnw141292do_test misc1-12.9 {
378*c5c4113dSnw141292  execsql {
379*c5c4113dSnw141292    SELECT min(z), max(z), count(z) FROM t7 GROUP BY y ORDER BY 1;
380*c5c4113dSnw141292  }
381*c5c4113dSnw141292} {1 2 2 3 4 2}
382*c5c4113dSnw141292
383*c5c4113dSnw141292# This used to be an error.  But we changed the code so that arbitrary
384*c5c4113dSnw141292# identifiers can be used as a collating sequence.  Collation is by text
385*c5c4113dSnw141292# if the identifier contains "text", "blob", or "clob" and is numeric
386*c5c4113dSnw141292# otherwise.
387*c5c4113dSnw141292do_test misc1-12.10 {
388*c5c4113dSnw141292  catchsql {
389*c5c4113dSnw141292    SELECT * FROM t6 ORDER BY a COLLATE unknown;
390*c5c4113dSnw141292  }
391*c5c4113dSnw141292} {0 {0 0.0 y 0}}
392*c5c4113dSnw141292do_test misc1-12.11 {
393*c5c4113dSnw141292  execsql {
394*c5c4113dSnw141292    CREATE TABLE t8(x TEXT COLLATE numeric, y INTEGER COLLATE text, z);
395*c5c4113dSnw141292    INSERT INTO t8 VALUES(0,0,1);
396*c5c4113dSnw141292    INSERT INTO t8 VALUES(0.0,0,2);
397*c5c4113dSnw141292    INSERT INTO t8 VALUES(0,0.0,3);
398*c5c4113dSnw141292    INSERT INTO t8 VALUES(0.0,0.0,4);
399*c5c4113dSnw141292    SELECT DISTINCT x, y FROM t8 ORDER BY z;
400*c5c4113dSnw141292  }
401*c5c4113dSnw141292} {0 0 0 0.0}
402*c5c4113dSnw141292do_test misc1-12.12 {
403*c5c4113dSnw141292  execsql {
404*c5c4113dSnw141292    SELECT min(z), max(z), count(z) FROM t8 GROUP BY x ORDER BY 1;
405*c5c4113dSnw141292  }
406*c5c4113dSnw141292} {1 4 4}
407*c5c4113dSnw141292do_test misc1-12.13 {
408*c5c4113dSnw141292  execsql {
409*c5c4113dSnw141292    SELECT min(z), max(z), count(z) FROM t8 GROUP BY y ORDER BY 1;
410*c5c4113dSnw141292  }
411*c5c4113dSnw141292} {1 2 2 3 4 2}
412*c5c4113dSnw141292
413*c5c4113dSnw141292# There was a problem with realloc() in the OP_MemStore operation of
414*c5c4113dSnw141292# the VDBE.  A buffer was being reallocated but some pointers into
415*c5c4113dSnw141292# the old copy of the buffer were not being moved over to the new copy.
416*c5c4113dSnw141292# The following code tests for the problem.
417*c5c4113dSnw141292#
418*c5c4113dSnw141292do_test misc1-13.1 {
419*c5c4113dSnw141292   execsql {
420*c5c4113dSnw141292     CREATE TABLE t9(x,y);
421*c5c4113dSnw141292     INSERT INTO t9 VALUES('one',1);
422*c5c4113dSnw141292     INSERT INTO t9 VALUES('two',2);
423*c5c4113dSnw141292     INSERT INTO t9 VALUES('three',3);
424*c5c4113dSnw141292     INSERT INTO t9 VALUES('four',4);
425*c5c4113dSnw141292     INSERT INTO t9 VALUES('five',5);
426*c5c4113dSnw141292     INSERT INTO t9 VALUES('six',6);
427*c5c4113dSnw141292     INSERT INTO t9 VALUES('seven',7);
428*c5c4113dSnw141292     INSERT INTO t9 VALUES('eight',8);
429*c5c4113dSnw141292     INSERT INTO t9 VALUES('nine',9);
430*c5c4113dSnw141292     INSERT INTO t9 VALUES('ten',10);
431*c5c4113dSnw141292     INSERT INTO t9 VALUES('eleven',11);
432*c5c4113dSnw141292     SELECT y FROM t9
433*c5c4113dSnw141292     WHERE x=(SELECT x FROM t9 WHERE y=1)
434*c5c4113dSnw141292        OR x=(SELECT x FROM t9 WHERE y=2)
435*c5c4113dSnw141292        OR x=(SELECT x FROM t9 WHERE y=3)
436*c5c4113dSnw141292        OR x=(SELECT x FROM t9 WHERE y=4)
437*c5c4113dSnw141292        OR x=(SELECT x FROM t9 WHERE y=5)
438*c5c4113dSnw141292        OR x=(SELECT x FROM t9 WHERE y=6)
439*c5c4113dSnw141292        OR x=(SELECT x FROM t9 WHERE y=7)
440*c5c4113dSnw141292        OR x=(SELECT x FROM t9 WHERE y=8)
441*c5c4113dSnw141292        OR x=(SELECT x FROM t9 WHERE y=9)
442*c5c4113dSnw141292        OR x=(SELECT x FROM t9 WHERE y=10)
443*c5c4113dSnw141292        OR x=(SELECT x FROM t9 WHERE y=11)
444*c5c4113dSnw141292        OR x=(SELECT x FROM t9 WHERE y=12)
445*c5c4113dSnw141292        OR x=(SELECT x FROM t9 WHERE y=13)
446*c5c4113dSnw141292        OR x=(SELECT x FROM t9 WHERE y=14)
447*c5c4113dSnw141292     ;
448*c5c4113dSnw141292   }
449*c5c4113dSnw141292} {1 2 3 4 5 6 7 8 9 10 11}
450*c5c4113dSnw141292
451*c5c4113dSnw141292# Make sure a database connection still works after changing the
452*c5c4113dSnw141292# working directory.
453*c5c4113dSnw141292#
454*c5c4113dSnw141292do_test misc1-14.1 {
455*c5c4113dSnw141292  file mkdir tempdir
456*c5c4113dSnw141292  cd tempdir
457*c5c4113dSnw141292  execsql {BEGIN}
458*c5c4113dSnw141292  file exists ./test.db-journal
459*c5c4113dSnw141292} {0}
460*c5c4113dSnw141292do_test misc1-14.2 {
461*c5c4113dSnw141292  file exists ../test.db-journal
462*c5c4113dSnw141292} {1}
463*c5c4113dSnw141292do_test misc1-14.3 {
464*c5c4113dSnw141292  cd ..
465*c5c4113dSnw141292  file delete tempdir
466*c5c4113dSnw141292  execsql {COMMIT}
467*c5c4113dSnw141292  file exists ./test.db-journal
468*c5c4113dSnw141292} {0}
469*c5c4113dSnw141292
470*c5c4113dSnw141292# A failed create table should not leave the table in the internal
471*c5c4113dSnw141292# data structures.  Ticket #238.
472*c5c4113dSnw141292#
473*c5c4113dSnw141292do_test misc1-15.1 {
474*c5c4113dSnw141292  catchsql {
475*c5c4113dSnw141292    CREATE TABLE t10 AS SELECT c1;
476*c5c4113dSnw141292  }
477*c5c4113dSnw141292} {1 {no such column: c1}}
478*c5c4113dSnw141292do_test misc1-15.2 {
479*c5c4113dSnw141292  catchsql {
480*c5c4113dSnw141292    CREATE TABLE t10 AS SELECT 1;
481*c5c4113dSnw141292  }
482*c5c4113dSnw141292  # The bug in ticket #238 causes the statement above to fail with
483*c5c4113dSnw141292  # the error "table t10 alread exists"
484*c5c4113dSnw141292} {0 {}}
485*c5c4113dSnw141292
486*c5c4113dSnw141292# Test for memory leaks when a CREATE TABLE containing a primary key
487*c5c4113dSnw141292# fails.  Ticket #249.
488*c5c4113dSnw141292#
489*c5c4113dSnw141292do_test misc1-16.1 {
490*c5c4113dSnw141292  catchsql {SELECT name FROM sqlite_master LIMIT 1}
491*c5c4113dSnw141292  catchsql {
492*c5c4113dSnw141292    CREATE TABLE test(a integer, primary key(a));
493*c5c4113dSnw141292  }
494*c5c4113dSnw141292} {0 {}}
495*c5c4113dSnw141292do_test misc1-16.2 {
496*c5c4113dSnw141292  catchsql {
497*c5c4113dSnw141292    CREATE TABLE test(a integer, primary key(a));
498*c5c4113dSnw141292  }
499*c5c4113dSnw141292} {1 {table test already exists}}
500*c5c4113dSnw141292do_test misc1-16.3 {
501*c5c4113dSnw141292  catchsql {
502*c5c4113dSnw141292    CREATE TABLE test2(a text primary key, b text, primary key(a,b));
503*c5c4113dSnw141292  }
504*c5c4113dSnw141292} {1 {table "test2" has more than one primary key}}
505*c5c4113dSnw141292do_test misc1-16.4 {
506*c5c4113dSnw141292  execsql {
507*c5c4113dSnw141292    INSERT INTO test VALUES(1);
508*c5c4113dSnw141292    SELECT rowid, a FROM test;
509*c5c4113dSnw141292  }
510*c5c4113dSnw141292} {1 1}
511*c5c4113dSnw141292do_test misc1-16.5 {
512*c5c4113dSnw141292  execsql {
513*c5c4113dSnw141292    INSERT INTO test VALUES(5);
514*c5c4113dSnw141292    SELECT rowid, a FROM test;
515*c5c4113dSnw141292  }
516*c5c4113dSnw141292} {1 1 5 5}
517*c5c4113dSnw141292do_test misc1-16.6 {
518*c5c4113dSnw141292  execsql {
519*c5c4113dSnw141292    INSERT INTO test VALUES(NULL);
520*c5c4113dSnw141292    SELECT rowid, a FROM test;
521*c5c4113dSnw141292  }
522*c5c4113dSnw141292} {1 1 5 5 6 6}
523*c5c4113dSnw141292
524*c5c4113dSnw141292# Ticket #333: Temp triggers that modify persistent tables.
525*c5c4113dSnw141292#
526*c5c4113dSnw141292do_test misc1-17.1 {
527*c5c4113dSnw141292  execsql {
528*c5c4113dSnw141292    BEGIN;
529*c5c4113dSnw141292    CREATE TABLE RealTable(TestID INTEGER PRIMARY KEY, TestString TEXT);
530*c5c4113dSnw141292    CREATE TEMP TABLE TempTable(TestID INTEGER PRIMARY KEY, TestString TEXT);
531*c5c4113dSnw141292    CREATE TEMP TRIGGER trigTest_1 AFTER UPDATE ON TempTable BEGIN
532*c5c4113dSnw141292      INSERT INTO RealTable(TestString)
533*c5c4113dSnw141292         SELECT new.TestString FROM TempTable LIMIT 1;
534*c5c4113dSnw141292    END;
535*c5c4113dSnw141292    INSERT INTO TempTable(TestString) VALUES ('1');
536*c5c4113dSnw141292    INSERT INTO TempTable(TestString) VALUES ('2');
537*c5c4113dSnw141292    UPDATE TempTable SET TestString = TestString + 1 WHERE TestID IN (1, 2);
538*c5c4113dSnw141292    COMMIT;
539*c5c4113dSnw141292    SELECT TestString FROM RealTable ORDER BY 1;
540*c5c4113dSnw141292  }
541*c5c4113dSnw141292} {2 3}
542*c5c4113dSnw141292
543*c5c4113dSnw141292finish_test
544