blob: ccce1a4c216d1c367f40d2f42f1116f6e09f4b0d [file] [log] [blame]
drhb19a2bc2001-09-16 00:13:26 +00001# 2001 September 15
drhbed86902000-06-02 13:27:59 +00002#
drhb19a2bc2001-09-16 00:13:26 +00003# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
drhbed86902000-06-02 13:27:59 +00005#
drhb19a2bc2001-09-16 00:13:26 +00006# 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.
drhbed86902000-06-02 13:27:59 +00009#
10#***********************************************************************
11# This file implements regression tests for SQLite library. The
12# focus of this file is testing the SELECT statement.
13#
drhb19a2bc2001-09-16 00:13:26 +000014# $Id: select2.test,v 1.14 2001/09/16 00:13:28 drh Exp $
drhbed86902000-06-02 13:27:59 +000015
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18
drhd75f54e2000-06-02 15:05:33 +000019# Create a table with some data
20#
drhbed86902000-06-02 13:27:59 +000021execsql {CREATE TABLE tbl1(f1 int, f2 int)}
drhdf16aed2000-06-02 14:27:22 +000022set f [open ./testdata1.txt w]
drhbed86902000-06-02 13:27:59 +000023for {set i 0} {$i<=30} {incr i} {
drh1b07be52000-06-02 14:38:46 +000024 puts $f "[expr {$i%9}]\t[expr {$i%10}]"
drhbed86902000-06-02 13:27:59 +000025}
drhdf16aed2000-06-02 14:27:22 +000026close $f
27execsql {COPY tbl1 FROM './testdata1.txt'}
drhd75f54e2000-06-02 15:05:33 +000028file delete -force ./testdata1.txt
drhbed86902000-06-02 13:27:59 +000029
30# Do a second query inside a first.
31#
32do_test select2-1.1 {
33 set sql {SELECT DISTINCT f1 FROM tbl1 ORDER BY f1}
34 set r {}
35 db eval $sql data {
36 set f1 $data(f1)
37 lappend r $f1:
38 set sql2 "SELECT f2 FROM tbl1 WHERE f1=$f1 ORDER BY f2"
39 db eval $sql2 d2 {
40 lappend r $d2(f2)
41 }
42 }
43 set r
44} {0: 0 7 8 9 1: 0 1 8 9 2: 0 1 2 9 3: 0 1 2 3 4: 2 3 4 5: 3 4 5 6: 4 5 6 7: 5 6 7 8: 6 7 8}
45
drhbed86902000-06-02 13:27:59 +000046do_test select2-1.2 {
47 set sql {SELECT DISTINCT f1 FROM tbl1 WHERE f1>3 AND f1<5}
48 set r {}
49 db eval $sql data {
50 set f1 $data(f1)
51 lappend r $f1:
52 set sql2 "SELECT f2 FROM tbl1 WHERE f1=$f1 ORDER BY f2"
53 db eval $sql2 d2 {
54 lappend r $d2(f2)
55 }
56 }
57 set r
58} {4: 2 3 4}
59
drhd75f54e2000-06-02 15:05:33 +000060# Create a largish table
61#
drh19a775c2000-06-05 18:54:46 +000062do_test select2-2.0 {
63 execsql {CREATE TABLE tbl2(f1 int, f2 int, f3 int)}
64 set f [open ./testdata1.txt w]
65 for {set i 1} {$i<=30000} {incr i} {
66 puts $f "$i\t[expr {$i*2}]\t[expr {$i*3}]"
67 }
68 close $f
drh3fc190c2001-09-14 03:24:23 +000069 # execsql {--vdbe-trace-on--}
drh19a775c2000-06-05 18:54:46 +000070 execsql {COPY tbl2 FROM './testdata1.txt'}
71 file delete -force ./testdata1.txt
72} {}
drhd75f54e2000-06-02 15:05:33 +000073
74do_test select2-2.1 {
75 execsql {SELECT count(*) FROM tbl2}
76} {30000}
77do_test select2-2.2 {
78 execsql {SELECT count(*) FROM tbl2 WHERE f2>1000}
79} {29500}
80
81do_test select2-3.1 {
drhe8409722000-06-08 16:54:40 +000082 execsql {SELECT f1 FROM tbl2 WHERE 1000=f2}
drhd75f54e2000-06-02 15:05:33 +000083} {500}
84
drh19a775c2000-06-05 18:54:46 +000085do_test select2-3.2a {
86 execsql {CREATE INDEX idx1 ON tbl2(f2)}
87} {}
drhd75f54e2000-06-02 15:05:33 +000088
drh19a775c2000-06-05 18:54:46 +000089do_test select2-3.2b {
drhe8409722000-06-08 16:54:40 +000090 execsql {SELECT f1 FROM tbl2 WHERE 1000=f2}
drhd75f54e2000-06-02 15:05:33 +000091} {500}
drhe8409722000-06-08 16:54:40 +000092do_test select2-3.2c {
93 execsql {SELECT f1 FROM tbl2 WHERE f2=1000}
94} {500}
95do_test select2-3.2d {
drh767c2002000-10-19 14:10:08 +000096 execsql {SELECT fcnt() FROM tbl2 WHERE 1000=f2}
drhc87fa692001-08-19 18:19:46 +000097} {1}
drh767c2002000-10-19 14:10:08 +000098do_test select2-3.2e {
99 execsql {SELECT fcnt() FROM tbl2 WHERE f2=1000}
drhc87fa692001-08-19 18:19:46 +0000100} {1}
drhdb25e382001-03-15 18:21:22 +0000101
102# omit the time-dependent tests
103#
drhdb25e382001-03-15 18:21:22 +0000104do_probtest select2-3.2f {
drhe8409722000-06-08 16:54:40 +0000105 set t1 [lindex [time {execsql {SELECT f1 FROM tbl2 WHERE 1000=f2}} 1] 0]
106 set t2 [lindex [time {execsql {SELECT f1 FROM tbl2 WHERE f2=1000}} 1] 0]
drh767c2002000-10-19 14:10:08 +0000107 expr {$t1*0.7<$t2 && $t2*0.7<$t1}
drhe8409722000-06-08 16:54:40 +0000108} {1}
drhd75f54e2000-06-02 15:05:33 +0000109
110# Make sure queries run faster with an index than without
111#
drhdb25e382001-03-15 18:21:22 +0000112do_probtest select2-3.3 {
drhd75f54e2000-06-02 15:05:33 +0000113 set t1 [lindex [time {execsql {SELECT f1 from tbl2 WHERE f2==2000}} 1] 0]
114 execsql {DROP INDEX idx1}
115 set t2 [lindex [time {execsql {SELECT f1 FROM tbl2 WHERE f2==2000}} 1] 0]
drhe1b6a5b2000-07-29 13:06:59 +0000116 expr {$t1*10 < $t2}
drhd75f54e2000-06-02 15:05:33 +0000117} {1}
drhdb25e382001-03-15 18:21:22 +0000118do_probtest select2-3.4 {
drh767c2002000-10-19 14:10:08 +0000119 expr {[execsql {SELECT fcnt() FROM tbl2 WHERE f2==2000}]>10}
120} {1}
drhd75f54e2000-06-02 15:05:33 +0000121
drhbed86902000-06-02 13:27:59 +0000122finish_test