3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # 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.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library. The
12 # focus of this file is testing the SELECT statement.
14 # $Id: select2.test,v 1.28 2009/01/15 15:23:59 drh Exp $
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
19 # Create a table with some data
21 execsql {CREATE TABLE tbl1(f1 int, f2 int)}
23 for {set i 0} {$i<=30} {incr i} {
24 execsql "INSERT INTO tbl1 VALUES([expr {$i%9}],[expr {$i%10}])"
28 # Do a second query inside a first.
31 set sql {SELECT DISTINCT f1 FROM tbl1 ORDER BY f1}
37 set sql2 "SELECT f2 FROM tbl1 WHERE f1=$f1 ORDER BY f2"
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}
46 set sql {SELECT DISTINCT f1 FROM tbl1 WHERE f1>3 AND f1<5}
51 set sql2 "SELECT f2 FROM tbl1 WHERE f1=$f1 ORDER BY f2"
60 # Create a largish table. Do this twice, once using the TCL cache and once
61 # without. Compare the performance to make sure things go faster with the
65 do_test select2-2.0.1 {
67 execsql {CREATE TABLE tbl2(f1 int, f2 int, f3 int); BEGIN;}
68 for {set i 1} {$i<=30000} {incr i} {
71 db eval {INSERT INTO tbl2 VALUES($i,$i2,$i3)}
77 puts "time with cache: $::t1"
79 catch {execsql {DROP TABLE tbl2}}
80 do_test select2-2.0.2 {
82 execsql {CREATE TABLE tbl2(f1 int, f2 int, f3 int); BEGIN;}
83 for {set i 1} {$i<=30000} {incr i} {
86 execsql "INSERT INTO tbl2 VALUES($i,$i2,$i3)"
92 puts "time without cache: $t2"
94 # do_test select2-2.0.3 {
95 # expr {[lindex $t1 0]<[lindex $t2 0]}
100 execsql {SELECT count(*) FROM tbl2}
102 do_test select2-2.2 {
103 execsql {SELECT count(*) FROM tbl2 WHERE f2>1000}
106 do_test select2-3.1 {
107 execsql {SELECT f1 FROM tbl2 WHERE 1000=f2}
110 do_test select2-3.2a {
111 execsql {CREATE INDEX idx1 ON tbl2(f2)}
113 do_test select2-3.2b {
114 execsql {SELECT f1 FROM tbl2 WHERE 1000=f2}
116 do_test select2-3.2c {
117 execsql {SELECT f1 FROM tbl2 WHERE f2=1000}
119 do_test select2-3.2d {
120 set sqlite_search_count 0
121 execsql {SELECT * FROM tbl2 WHERE 1000=f2}
122 set sqlite_search_count
124 do_test select2-3.2e {
125 set sqlite_search_count 0
126 execsql {SELECT * FROM tbl2 WHERE f2=1000}
127 set sqlite_search_count
130 # Make sure queries run faster with an index than without
132 do_test select2-3.3 {
133 execsql {DROP INDEX idx1}
134 set sqlite_search_count 0
135 execsql {SELECT f1 FROM tbl2 WHERE f2==2000}
136 set sqlite_search_count
139 # Make sure we can optimize functions in the WHERE clause that
140 # use fields from two or more different table. (Bug #6)
142 do_test select2-4.1 {
146 INSERT INTO aa VALUES(1);
147 INSERT INTO aa VALUES(3);
148 INSERT INTO bb VALUES(2);
149 INSERT INTO bb VALUES(4);
150 SELECT * FROM aa, bb WHERE max(a,b)>2;
153 do_test select2-4.2 {
155 INSERT INTO bb VALUES(0);
156 SELECT * FROM aa CROSS JOIN bb WHERE b;
159 do_test select2-4.3 {
161 SELECT * FROM aa CROSS JOIN bb WHERE NOT b;
164 do_test select2-4.4 {
166 SELECT * FROM aa, bb WHERE min(a,b);
169 do_test select2-4.5 {
171 SELECT * FROM aa, bb WHERE NOT min(a,b);
174 do_test select2-4.6 {
176 SELECT * FROM aa, bb WHERE CASE WHEN a=b-1 THEN 1 END;
179 do_test select2-4.7 {
181 SELECT * FROM aa, bb WHERE CASE WHEN a=b-1 THEN 0 ELSE 1 END;