aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/sqlite/unix/sqlite-3.5.1/test/misc5.test
blob: 86963b2bf5093ce20667d38f5276f10b56aca683 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
# 2005 Mar 16
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library.
#
# This file implements tests for miscellanous features that were
# left out of other test files.
#
# $Id: misc5.test,v 1.17 2007/09/12 17:01:45 danielk1977 Exp $

set testdir [file dirname $argv0]
source $testdir/tester.tcl

# Build records using the MakeRecord opcode such that the size of the 
# header is at the transition point in the size of a varint.
#
# This test causes an assertion failure or a buffer overrun in version
# 3.1.5 and earlier.
#
for {set i 120} {$i<140} {incr i} {
  do_test misc5-1.$i {
    catchsql {DROP TABLE t1}
    set sql1 {CREATE TABLE t1}
    set sql2 {INSERT INTO t1 VALUES}
    set sep (
    for {set j 0} {$j<$i} {incr j} {
      append sql1 ${sep}a$j
      append sql2 ${sep}$j
      set sep ,
    }
    append sql1 {);}
    append sql2 {);}
    execsql $sql1$sql2
  } {}
}

# Make sure large integers are stored correctly.
#
ifcapable conflict {
  do_test misc5-2.1 {
    execsql {
      create table t2(x unique);
      insert into t2 values(1);
      insert or ignore into t2 select x*2 from t2;
      insert or ignore into t2 select x*4 from t2;
      insert or ignore into t2 select x*16 from t2;
      insert or ignore into t2 select x*256 from t2;
      insert or ignore into t2 select x*65536 from t2;
      insert or ignore into t2 select x*2147483648 from t2;
      insert or ignore into t2 select x-1 from t2;
      insert or ignore into t2 select x+1 from t2;
      insert or ignore into t2 select -x from t2;
      select count(*) from t2;
    }
  } 371
} else {
  do_test misc5-2.1 {
    execsql {
      BEGIN;
      create table t2(x unique);
      create table t2_temp(x);
      insert into t2_temp values(1);
      insert into t2_temp select x*2 from t2_temp;
      insert into t2_temp select x*4 from t2_temp;
      insert into t2_temp select x*16 from t2_temp;
      insert into t2_temp select x*256 from t2_temp;
      insert into t2_temp select x*65536 from t2_temp;
      insert into t2_temp select x*2147483648 from t2_temp;
      insert into t2_temp select x-1 from t2_temp;
      insert into t2_temp select x+1 from t2_temp;
      insert into t2_temp select -x from t2_temp;
      INSERT INTO t2 SELECT DISTINCT(x) FROM t2_temp;
      DROP TABLE t2_temp;
      COMMIT;
      select count(*) from t2;
    }
  } 371
}
do_test misc5-2.2 {
  execsql {
    select x from t2 order by x;
  }
} \
"-4611686018427387905\
-4611686018427387904\
-4611686018427387903\
-2305843009213693953\
-2305843009213693952\
-2305843009213693951\
-1152921504606846977\
-1152921504606846976\
-1152921504606846975\
-576460752303423489\
-576460752303423488\
-576460752303423487\
-288230376151711745\
-288230376151711744\
-288230376151711743\
-144115188075855873\
-144115188075855872\
-144115188075855871\
-72057594037927937\
-72057594037927936\
-72057594037927935\
-36028797018963969\
-36028797018963968\
-36028797018963967\
-18014398509481985\
-18014398509481984\
-18014398509481983\
-9007199254740993\
-9007199254740992\
-9007199254740991\
-4503599627370497\
-4503599627370496\
-4503599627370495\
-2251799813685249\
-2251799813685248\
-2251799813685247\
-1125899906842625\
-1125899906842624\
-1125899906842623\
-562949953421313\
-562949953421312\
-562949953421311\
-281474976710657\
-281474976710656\
-281474976710655\
-140737488355329\
-140737488355328\
-140737488355327\
-70368744177665\
-70368744177664\
-70368744177663\
-35184372088833\
-35184372088832\
-35184372088831\
-17592186044417\
-17592186044416\
-17592186044415\
-8796093022209\
-8796093022208\
-8796093022207\
-4398046511105\
-4398046511104\
-4398046511103\
-2199023255553\
-2199023255552\
-2199023255551\
-1099511627777\
-1099511627776\
-1099511627775\
-549755813889\
-549755813888\
-549755813887\
-274877906945\
-274877906944\
-274877906943\
-137438953473\
-137438953472\
-137438953471\
-68719476737\
-68719476736\
-68719476735\
-34359738369\
-34359738368\
-34359738367\
-17179869185\
-17179869184\
-17179869183\
-8589934593\
-8589934592\
-8589934591\
-4294967297\
-4294967296\
-4294967295\
-2147483649\
-2147483648\
-2147483647\
-1073741825\
-1073741824\
-1073741823\
-536870913\
-536870912\
-536870911\
-268435457\
-268435456\
-268435455\
-134217729\
-134217728\
-134217727\
-67108865\
-67108864\
-67108863\
-33554433\
-33554432\
-33554431\
-16777217\
-16777216\
-16777215\
-8388609\
-8388608\
-8388607\
-4194305\
-4194304\
-4194303\
-2097153\
-2097152\
-2097151\
-1048577\
-1048576\
-1048575\
-524289\
-524288\
-524287\
-262145\
-262144\
-262143\
-131073\
-131072\
-131071\
-65537\
-65536\
-65535\
-32769\
-32768\
-32767\
-16385\
-16384\
-16383\
-8193\
-8192\
-8191\
-4097\
-4096\
-4095\
-2049\
-2048\
-2047\
-1025\
-1024\
-1023\
-513\
-512\
-511\
-257\
-256\
-255\
-129\
-128\
-127\
-65\
-64\
-63\
-33\
-32\
-31\
-17\
-16\
-15\
-9\
-8\
-7\
-5\
-4\
-3\
-2\
-1\
0\
1\
2\
3\
4\
5\
7\
8\
9\
15\
16\
17\
31\
32\
33\
63\
64\
65\
127\
128\
129\
255\
256\
257\
511\
512\
513\
1023\
1024\
1025\
2047\
2048\
2049\
4095\
4096\
4097\
8191\
8192\
8193\
16383\
16384\
16385\
32767\
32768\
32769\
65535\
65536\
65537\
131071\
131072\
131073\
262143\
262144\
262145\
524287\
524288\
524289\
1048575\
1048576\
1048577\
2097151\
2097152\
2097153\
4194303\
4194304\
4194305\
8388607\
8388608\
8388609\
16777215\
16777216\
16777217\
33554431\
33554432\
33554433\
67108863\
67108864\
67108865\
134217727\
134217728\
134217729\
268435455\
268435456\
268435457\
536870911\
536870912\
536870913\
1073741823\
1073741824\
1073741825\
2147483647\
2147483648\
2147483649\
4294967295\
4294967296\
4294967297\
8589934591\
8589934592\
8589934593\
17179869183\
17179869184\
17179869185\
34359738367\
34359738368\
34359738369\
68719476735\
68719476736\
68719476737\
137438953471\
137438953472\
137438953473\
274877906943\
274877906944\
274877906945\
549755813887\
549755813888\
549755813889\
1099511627775\
1099511627776\
1099511627777\
2199023255551\
2199023255552\
2199023255553\
4398046511103\
4398046511104\
4398046511105\
8796093022207\
8796093022208\
8796093022209\
17592186044415\
17592186044416\
17592186044417\
35184372088831\
35184372088832\
35184372088833\
70368744177663\
70368744177664\
70368744177665\
140737488355327\
140737488355328\
140737488355329\
281474976710655\
281474976710656\
281474976710657\
562949953421311\
562949953421312\
562949953421313\
1125899906842623\
1125899906842624\
1125899906842625\
2251799813685247\
2251799813685248\
2251799813685249\
4503599627370495\
4503599627370496\
4503599627370497\
9007199254740991\
9007199254740992\
9007199254740993\
18014398509481983\
18014398509481984\
18014398509481985\
36028797018963967\
36028797018963968\
36028797018963969\
72057594037927935\
72057594037927936\
72057594037927937\
144115188075855871\
144115188075855872\
144115188075855873\
288230376151711743\
288230376151711744\
288230376151711745\
576460752303423487\
576460752303423488\
576460752303423489\
1152921504606846975\
1152921504606846976\
1152921504606846977\
2305843009213693951\
2305843009213693952\
2305843009213693953\
4611686018427387903\
4611686018427387904\
4611686018427387905"

# Ticket #1210.  Do proper reference counting of Table structures
# so that deeply nested SELECT statements can be flattened correctly.
#
ifcapable subquery {
  do_test misc5-3.1 {
    execsql {
      CREATE TABLE songs(songid, artist, timesplayed);
      INSERT INTO songs VALUES(1,'one',1);
      INSERT INTO songs VALUES(2,'one',2);
      INSERT INTO songs VALUES(3,'two',3);
      INSERT INTO songs VALUES(4,'three',5);
      INSERT INTO songs VALUES(5,'one',7);
      INSERT INTO songs VALUES(6,'two',11);
      SELECT DISTINCT artist 
      FROM (    
       SELECT DISTINCT artist    
       FROM songs      
       WHERE songid IN (    
        SELECT songid    
        FROM songs    
        WHERE LOWER(artist) = (    
          SELECT DISTINCT LOWER(artist)    
          FROM (      
            SELECT DISTINCT artist,sum(timesplayed) AS total      
            FROM songs      
            GROUP BY LOWER(artist)      
            ORDER BY total DESC      
            LIMIT 10    
          )    
          WHERE artist <> '' 
        )  
       )       
      )  
      ORDER BY LOWER(artist) ASC;
    }
  } {two}
}

# Ticket #1370.  Do not overwrite small files (less than 1024 bytes)
# when trying to open them as a database.
#
do_test misc5-4.1 {
  db close
  file delete -force test.db
  set fd [open test.db w]
  puts $fd "This is not really a database"
  close $fd
  sqlite3 db test.db
  catchsql {
    CREATE TABLE t1(a,b,c);
  }
} {1 {file is encrypted or is not a database}}

# Ticket #1371.  Allow floating point numbers of the form .N  or N.
#
do_test misc5-5.1 {
  execsql {SELECT .1 }
} 0.1
do_test misc5-5.2 {
  execsql {SELECT 2. }
} 2.0
do_test misc5-5.3 {
  execsql {SELECT 3.e0 }
} 3.0
do_test misc5-5.4 {
  execsql {SELECT .4e+1}
} 4.0

# Ticket #1582.  Ensure that an unknown table in a LIMIT clause applied to
# a UNION ALL query causes an error, not a crash.
#
db close
file delete -force test.db
sqlite3 db test.db
ifcapable subquery&&compound {
  do_test misc5-6.1 {
    catchsql {
      SELECT * FROM sqlite_master 
      UNION ALL 
      SELECT * FROM sqlite_master
      LIMIT (SELECT count(*) FROM blah);
    }
  } {1 {no such table: blah}}
  do_test misc5-6.2 {
    execsql {
      CREATE TABLE logs(msg TEXT, timestamp INTEGER, dbtime TEXT);
    }
    catchsql {
      SELECT * FROM logs WHERE logs.id >= (SELECT head FROM logs_base) 
      UNION ALL 
      SELECT * FROM logs 
      LIMIT (SELECT lmt FROM logs_base) ;
    }
  } {1 {no such column: logs.id}}
}

# Overflow the lemon parser stack by providing an overly complex
# expression.  Make sure that the overflow is detected and reported.
#
do_test misc5-7.1 {
  execsql {CREATE TABLE t1(x)}
  set sql "INSERT INTO t1 VALUES("
  set tail ""
  for {set i 0} {$i<200} {incr i} {
    append sql "(1+"
    append tail ")"
  }
  append sql 2$tail
  catchsql $sql
} {1 {parser stack overflow}}

# Check the MISUSE return from sqlitee3_busy_timeout
#
do_test misc5-8.1-misuse {
  set DB [sqlite3_connection_pointer db]
  db close
  sqlite3_busy_timeout $DB 1000
} SQLITE_MISUSE
sqlite3 db test.db

# Ticket #1911
#
ifcapable compound {
  do_test misc5-9.1 {
    execsql {
      SELECT name, type FROM sqlite_master WHERE name IS NULL
      UNION
      SELECT type, name FROM sqlite_master WHERE type IS NULL
      ORDER BY 1, 2, 1, 2, 1, 2
    }
  } {}
  do_test misc5-9.2 {
    execsql {
      SELECT name, type FROM sqlite_master WHERE name IS NULL
      UNION
      SELECT type, name FROM sqlite_master WHERE type IS NULL
      ORDER BY 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2
    }
  } {}
}

# Ticket #1912.  Make the tokenizer require a space after a numeric
# literal.
#
do_test misc5-10.1 {
  catchsql {
    SELECT 123abc
  }
} {1 {unrecognized token: "123abc"}}
do_test misc5-10.2 {
  catchsql {
    SELECT 1*123.4e5ghi;
  }
} {1 {unrecognized token: "123.4e5ghi"}}



finish_test