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.19 2004/05/13 05:16:17 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)} 22set f [open ./testdata1.txt w] 23for {set i 0} {$i<=30} {incr i} { 24 puts $f "[expr {$i%9}]\t[expr {$i%10}]" 25} 26close $f 27execsql {COPY tbl1 FROM './testdata1.txt'} 28file delete -force ./testdata1.txt 29catch {unset data} 30 31# Do a second query inside a first. 32# 33do_test select2-1.1 { 34 set sql {SELECT DISTINCT f1 FROM tbl1 ORDER BY f1} 35 set r {} 36 db eval $sql data { 37 set f1 $data(f1) 38 lappend r $f1: 39 set sql2 "SELECT f2 FROM tbl1 WHERE f1=$f1 ORDER BY f2" 40 db eval $sql2 d2 { 41 lappend r $d2(f2) 42 } 43 } 44 set r 45} {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 47do_test select2-1.2 { 48 set sql {SELECT DISTINCT f1 FROM tbl1 WHERE f1>3 AND f1<5} 49 set r {} 50 db eval $sql data { 51 set f1 $data(f1) 52 lappend r $f1: 53 set sql2 "SELECT f2 FROM tbl1 WHERE f1=$f1 ORDER BY f2" 54 db eval $sql2 d2 { 55 lappend r $d2(f2) 56 } 57 } 58 set r 59} {4: 2 3 4} 60 61# Create a largish table 62# 63do_test select2-2.0 { 64 execsql {CREATE TABLE tbl2(f1 int, f2 int, f3 int)} 65 set f [open ./testdata1.txt w] 66 for {set i 1} {$i<=30000} {incr i} { 67 puts $f "$i\t[expr {$i*2}]\t[expr {$i*3}]" 68 } 69 close $f 70 # execsql {--vdbe-trace-on--} 71 execsql {COPY tbl2 FROM './testdata1.txt'} 72 file delete -force ./testdata1.txt 73} {} 74 75do_test select2-2.1 { 76 execsql {SELECT count(*) FROM tbl2} 77} {30000} 78do_test select2-2.2 { 79 execsql {SELECT count(*) FROM tbl2 WHERE f2>1000} 80} {29500} 81 82do_test select2-3.1 { 83 execsql {SELECT f1 FROM tbl2 WHERE 1000=f2} 84} {500} 85 86# SQLite v3: Change the expressions in the following four test cases 87# from 1000=f2 to '1000'=f2. This is because fields read in using 88# the COPY command have manifest type TEXT. 89do_test select2-3.2a { 90 execsql {CREATE INDEX idx1 ON tbl2(f2)} 91} {} 92do_test select2-3.2b { 93 execsql {SELECT f1 FROM tbl2 WHERE '1000'=f2} 94} {500} 95do_test select2-3.2c { 96 execsql {SELECT f1 FROM tbl2 WHERE f2='1000'} 97} {500} 98do_test select2-3.2d { 99 set sqlite_search_count 0 100 execsql {SELECT * FROM tbl2 WHERE '1000'=f2} 101 set sqlite_search_count 102} {3} 103do_test select2-3.2e { 104 set sqlite_search_count 0 105 execsql {SELECT * FROM tbl2 WHERE f2='1000'} 106 set sqlite_search_count 107} {3} 108 109# Make sure queries run faster with an index than without 110# 111do_test select2-3.3 { 112 execsql {DROP INDEX idx1} 113 set sqlite_search_count 0 114 execsql {SELECT f1 FROM tbl2 WHERE f2==2000} 115 set sqlite_search_count 116} {29999} 117 118# Make sure we can optimize functions in the WHERE clause that 119# use fields from two or more different table. (Bug #6) 120# 121do_test select2-4.1 { 122 execsql { 123 CREATE TABLE aa(a); 124 CREATE TABLE bb(b); 125 INSERT INTO aa VALUES(1); 126 INSERT INTO aa VALUES(3); 127 INSERT INTO bb VALUES(2); 128 INSERT INTO bb VALUES(4); 129 SELECT * FROM aa, bb WHERE max(a,b)>2; 130 } 131} {1 4 3 2 3 4} 132do_test select2-4.2 { 133 execsql { 134 INSERT INTO bb VALUES(0); 135 SELECT * FROM aa, bb WHERE b; 136 } 137} {1 2 1 4 3 2 3 4} 138do_test select2-4.3 { 139 execsql { 140 SELECT * FROM aa, bb WHERE NOT b; 141 } 142} {1 0 3 0} 143do_test select2-4.4 { 144 execsql { 145 SELECT * FROM aa, bb WHERE min(a,b); 146 } 147} {1 2 1 4 3 2 3 4} 148do_test select2-4.5 { 149 execsql { 150 SELECT * FROM aa, bb WHERE NOT min(a,b); 151 } 152} {1 0 3 0} 153do_test select2-4.6 { 154 execsql { 155 SELECT * FROM aa, bb WHERE CASE WHEN a=b-1 THEN 1 END; 156 } 157} {1 2 3 4} 158do_test select2-4.7 { 159 execsql { 160 SELECT * FROM aa, bb WHERE CASE WHEN a=b-1 THEN 0 ELSE 1 END; 161 } 162} {1 4 1 0 3 2 3 0} 163 164finish_test 165