blob: 1d9d87bd17ce1a5075e17e3b8b10b37c21fd417d [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#
drh190765c2005-01-24 12:46:14 +000014# $Id: select2.test,v 1.23 2005/01/24 12:46:14 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)}
drh5f3b4ab2004-05-27 17:22:54 +000022execsql {BEGIN}
drhbed86902000-06-02 13:27:59 +000023for {set i 0} {$i<=30} {incr i} {
drh5f3b4ab2004-05-27 17:22:54 +000024 execsql "INSERT INTO tbl1 VALUES([expr {$i%9}],[expr {$i%10}])"
drhbed86902000-06-02 13:27:59 +000025}
drh5f3b4ab2004-05-27 17:22:54 +000026execsql {COMMIT}
drhbed86902000-06-02 13:27:59 +000027
28# Do a second query inside a first.
29#
30do_test select2-1.1 {
31 set sql {SELECT DISTINCT f1 FROM tbl1 ORDER BY f1}
32 set r {}
danielk1977cbb18d22004-05-28 11:37:27 +000033 catch {unset data}
drhbed86902000-06-02 13:27:59 +000034 db eval $sql data {
35 set f1 $data(f1)
36 lappend r $f1:
37 set sql2 "SELECT f2 FROM tbl1 WHERE f1=$f1 ORDER BY f2"
38 db eval $sql2 d2 {
39 lappend r $d2(f2)
40 }
41 }
42 set r
43} {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}
44
drhbed86902000-06-02 13:27:59 +000045do_test select2-1.2 {
46 set sql {SELECT DISTINCT f1 FROM tbl1 WHERE f1>3 AND f1<5}
47 set r {}
48 db eval $sql data {
49 set f1 $data(f1)
50 lappend r $f1:
51 set sql2 "SELECT f2 FROM tbl1 WHERE f1=$f1 ORDER BY f2"
52 db eval $sql2 d2 {
53 lappend r $d2(f2)
54 }
55 }
56 set r
57} {4: 2 3 4}
58
drh190765c2005-01-24 12:46:14 +000059# Create a largish table. Do this twice, once using the TCL cache and once
60# without. Compare the performance to make sure things go faster with the
61# cache turned on.
drhd75f54e2000-06-02 15:05:33 +000062#
drh190765c2005-01-24 12:46:14 +000063ifcapable tclvar {
64 do_test select2-2.0.1 {
65 set t1 [time {
66 execsql {CREATE TABLE tbl2(f1 int, f2 int, f3 int); BEGIN;}
67 for {set i 1} {$i<=30000} {incr i} {
68 set i2 [expr {$i*2}]
69 set i3 [expr {$i*3}]
70 db eval {INSERT INTO tbl2 VALUES($i,$i2,$i3)}
71 }
72 execsql {COMMIT}
73 }]
74 set result {}
75 } {}
76 puts "time with cache: $::t1"
77}
78catch {execsql {DROP TABLE tbl2}}
79do_test select2-2.0.2 {
80 set t2 [time {
81 execsql {CREATE TABLE tbl2(f1 int, f2 int, f3 int); BEGIN;}
82 for {set i 1} {$i<=30000} {incr i} {
83 set i2 [expr {$i*2}]
84 set i3 [expr {$i*3}]
85 execsql "INSERT INTO tbl2 VALUES($i,$i2,$i3)"
86 }
87 execsql {COMMIT}
88 }]
89 set result {}
drh19a775c2000-06-05 18:54:46 +000090} {}
drh190765c2005-01-24 12:46:14 +000091puts "time without cache: $t2"
92ifcapable tclvar {
93 do_test select2-2.0.3 {
94 expr {[lindex $t1 0]<[lindex $t2 0]}
95 } 1
96}
drhd75f54e2000-06-02 15:05:33 +000097
98do_test select2-2.1 {
99 execsql {SELECT count(*) FROM tbl2}
100} {30000}
101do_test select2-2.2 {
102 execsql {SELECT count(*) FROM tbl2 WHERE f2>1000}
103} {29500}
104
105do_test select2-3.1 {
drhe8409722000-06-08 16:54:40 +0000106 execsql {SELECT f1 FROM tbl2 WHERE 1000=f2}
drhd75f54e2000-06-02 15:05:33 +0000107} {500}
108
drh19a775c2000-06-05 18:54:46 +0000109do_test select2-3.2a {
110 execsql {CREATE INDEX idx1 ON tbl2(f2)}
111} {}
drh19a775c2000-06-05 18:54:46 +0000112do_test select2-3.2b {
danielk19773d1bfea2004-05-14 11:00:53 +0000113 execsql {SELECT f1 FROM tbl2 WHERE 1000=f2}
drhd75f54e2000-06-02 15:05:33 +0000114} {500}
drhe8409722000-06-08 16:54:40 +0000115do_test select2-3.2c {
danielk19773d1bfea2004-05-14 11:00:53 +0000116 execsql {SELECT f1 FROM tbl2 WHERE f2=1000}
drhe8409722000-06-08 16:54:40 +0000117} {500}
118do_test select2-3.2d {
drh487ab3c2001-11-08 00:45:21 +0000119 set sqlite_search_count 0
danielk19773d1bfea2004-05-14 11:00:53 +0000120 execsql {SELECT * FROM tbl2 WHERE 1000=f2}
drh487ab3c2001-11-08 00:45:21 +0000121 set sqlite_search_count
122} {3}
drh767c2002000-10-19 14:10:08 +0000123do_test select2-3.2e {
drh487ab3c2001-11-08 00:45:21 +0000124 set sqlite_search_count 0
danielk19773d1bfea2004-05-14 11:00:53 +0000125 execsql {SELECT * FROM tbl2 WHERE f2=1000}
drh487ab3c2001-11-08 00:45:21 +0000126 set sqlite_search_count
127} {3}
drhd75f54e2000-06-02 15:05:33 +0000128
129# Make sure queries run faster with an index than without
130#
drh487ab3c2001-11-08 00:45:21 +0000131do_test select2-3.3 {
drhd75f54e2000-06-02 15:05:33 +0000132 execsql {DROP INDEX idx1}
drh487ab3c2001-11-08 00:45:21 +0000133 set sqlite_search_count 0
134 execsql {SELECT f1 FROM tbl2 WHERE f2==2000}
135 set sqlite_search_count
136} {29999}
drhd75f54e2000-06-02 15:05:33 +0000137
drhdd579122002-04-02 01:58:57 +0000138# Make sure we can optimize functions in the WHERE clause that
139# use fields from two or more different table. (Bug #6)
140#
141do_test select2-4.1 {
142 execsql {
143 CREATE TABLE aa(a);
144 CREATE TABLE bb(b);
145 INSERT INTO aa VALUES(1);
146 INSERT INTO aa VALUES(3);
147 INSERT INTO bb VALUES(2);
148 INSERT INTO bb VALUES(4);
149 SELECT * FROM aa, bb WHERE max(a,b)>2;
150 }
151} {1 4 3 2 3 4}
drh3f6b5482002-04-02 13:26:10 +0000152do_test select2-4.2 {
153 execsql {
154 INSERT INTO bb VALUES(0);
155 SELECT * FROM aa, bb WHERE b;
156 }
157} {1 2 1 4 3 2 3 4}
158do_test select2-4.3 {
159 execsql {
160 SELECT * FROM aa, bb WHERE NOT b;
161 }
162} {1 0 3 0}
163do_test select2-4.4 {
164 execsql {
165 SELECT * FROM aa, bb WHERE min(a,b);
166 }
167} {1 2 1 4 3 2 3 4}
168do_test select2-4.5 {
169 execsql {
170 SELECT * FROM aa, bb WHERE NOT min(a,b);
171 }
172} {1 0 3 0}
173do_test select2-4.6 {
174 execsql {
175 SELECT * FROM aa, bb WHERE CASE WHEN a=b-1 THEN 1 END;
176 }
177} {1 2 3 4}
178do_test select2-4.7 {
179 execsql {
180 SELECT * FROM aa, bb WHERE CASE WHEN a=b-1 THEN 0 ELSE 1 END;
181 }
182} {1 4 1 0 3 2 3 0}
drhdd579122002-04-02 01:58:57 +0000183
drhbed86902000-06-02 13:27:59 +0000184finish_test