1# 2001 September 15 2# 3# The author disclaims copyright to this source code. In place of 4# a legal notice, here is a blessing: 5# 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. 9# 10#*********************************************************************** 11# This file implements regression tests for SQLite library. The 12# focus of this file is testing the SELECT statement. 13# 14# $Id: select2.test,v 1.26 2008/04/24 12:38:29 danielk1977 Exp $ 15 16set testdir [file dirname $argv0] 17source $testdir/tester.tcl 18 19# Create a table with some data 20# 21execsql {CREATE TABLE tbl1(f1 int, f2 int)} 22execsql {BEGIN} 23for {set i 0} {$i<=30} {incr i} { 24 execsql "INSERT INTO tbl1 VALUES([expr {$i%9}],[expr {$i%10}])" 25} 26execsql {COMMIT} 27 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 {} 33 catch {unset data} 34 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 45do_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} 58unset data 59 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 62# cache turned on. 63# 64ifcapable tclvar { 65 do_test select2-2.0.1 { 66 set t1 [time { 67 execsql {CREATE TABLE tbl2(f1 int, f2 int, f3 int); BEGIN;} 68 for {set i 1} {$i<=30000} {incr i} { 69 set i2 [expr {$i*2}] 70 set i3 [expr {$i*3}] 71 db eval {INSERT INTO tbl2 VALUES($i,$i2,$i3)} 72 } 73 execsql {COMMIT} 74 }] 75 list 76 } {} 77 puts "time with cache: $::t1" 78} 79catch {execsql {DROP TABLE tbl2}} 80do_test select2-2.0.2 { 81 set t2 [time { 82 execsql {CREATE TABLE tbl2(f1 int, f2 int, f3 int); BEGIN;} 83 for {set i 1} {$i<=30000} {incr i} { 84 set i2 [expr {$i*2}] 85 set i3 [expr {$i*3}] 86 execsql "INSERT INTO tbl2 VALUES($i,$i2,$i3)" 87 } 88 execsql {COMMIT} 89 }] 90 list 91} {} 92puts "time without cache: $t2" 93ifcapable tclvar { 94 do_test select2-2.0.3 { 95 expr {[lindex $t1 0]<[lindex $t2 0]} 96 } 1 97} 98 99do_test select2-2.1 { 100 execsql {SELECT count(*) FROM tbl2} 101} {30000} 102do_test select2-2.2 { 103 execsql {SELECT count(*) FROM tbl2 WHERE f2>1000} 104} {29500} 105 106do_test select2-3.1 { 107 execsql {SELECT f1 FROM tbl2 WHERE 1000=f2} 108} {500} 109 110do_test select2-3.2a { 111 execsql {CREATE INDEX idx1 ON tbl2(f2)} 112} {} 113do_test select2-3.2b { 114 execsql {SELECT f1 FROM tbl2 WHERE 1000=f2} 115} {500} 116do_test select2-3.2c { 117 execsql {SELECT f1 FROM tbl2 WHERE f2=1000} 118} {500} 119do_test select2-3.2d { 120 set sqlite_search_count 0 121btree_breakpoint 122 execsql {SELECT * FROM tbl2 WHERE 1000=f2} 123 set sqlite_search_count 124} {3} 125do_test select2-3.2e { 126 set sqlite_search_count 0 127 execsql {SELECT * FROM tbl2 WHERE f2=1000} 128 set sqlite_search_count 129} {3} 130 131# Make sure queries run faster with an index than without 132# 133do_test select2-3.3 { 134 execsql {DROP INDEX idx1} 135 set sqlite_search_count 0 136 execsql {SELECT f1 FROM tbl2 WHERE f2==2000} 137 set sqlite_search_count 138} {29999} 139 140# Make sure we can optimize functions in the WHERE clause that 141# use fields from two or more different table. (Bug #6) 142# 143do_test select2-4.1 { 144 execsql { 145 CREATE TABLE aa(a); 146 CREATE TABLE bb(b); 147 INSERT INTO aa VALUES(1); 148 INSERT INTO aa VALUES(3); 149 INSERT INTO bb VALUES(2); 150 INSERT INTO bb VALUES(4); 151 SELECT * FROM aa, bb WHERE max(a,b)>2; 152 } 153} {1 4 3 2 3 4} 154do_test select2-4.2 { 155 execsql { 156 INSERT INTO bb VALUES(0); 157 SELECT * FROM aa, bb WHERE b; 158 } 159} {1 2 1 4 3 2 3 4} 160do_test select2-4.3 { 161 execsql { 162 SELECT * FROM aa, bb WHERE NOT b; 163 } 164} {1 0 3 0} 165do_test select2-4.4 { 166 execsql { 167 SELECT * FROM aa, bb WHERE min(a,b); 168 } 169} {1 2 1 4 3 2 3 4} 170do_test select2-4.5 { 171 execsql { 172 SELECT * FROM aa, bb WHERE NOT min(a,b); 173 } 174} {1 0 3 0} 175do_test select2-4.6 { 176 execsql { 177 SELECT * FROM aa, bb WHERE CASE WHEN a=b-1 THEN 1 END; 178 } 179} {1 2 3 4} 180do_test select2-4.7 { 181 execsql { 182 SELECT * FROM aa, bb WHERE CASE WHEN a=b-1 THEN 0 ELSE 1 END; 183 } 184} {1 4 1 0 3 2 3 0} 185 186finish_test 187