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.14 2001/09/16 00:13:28 drh 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 29 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 46do_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 60# Create a largish table 61# 62do_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 69 # execsql {--vdbe-trace-on--} 70 execsql {COPY tbl2 FROM './testdata1.txt'} 71 file delete -force ./testdata1.txt 72} {} 73 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 { 82 execsql {SELECT f1 FROM tbl2 WHERE 1000=f2} 83} {500} 84 85do_test select2-3.2a { 86 execsql {CREATE INDEX idx1 ON tbl2(f2)} 87} {} 88 89do_test select2-3.2b { 90 execsql {SELECT f1 FROM tbl2 WHERE 1000=f2} 91} {500} 92do_test select2-3.2c { 93 execsql {SELECT f1 FROM tbl2 WHERE f2=1000} 94} {500} 95do_test select2-3.2d { 96 execsql {SELECT fcnt() FROM tbl2 WHERE 1000=f2} 97} {1} 98do_test select2-3.2e { 99 execsql {SELECT fcnt() FROM tbl2 WHERE f2=1000} 100} {1} 101 102# omit the time-dependent tests 103# 104do_probtest select2-3.2f { 105 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] 107 expr {$t1*0.7<$t2 && $t2*0.7<$t1} 108} {1} 109 110# Make sure queries run faster with an index than without 111# 112do_probtest select2-3.3 { 113 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] 116 expr {$t1*10 < $t2} 117} {1} 118do_probtest select2-3.4 { 119 expr {[execsql {SELECT fcnt() FROM tbl2 WHERE f2==2000}]>10} 120} {1} 121 122finish_test 123