drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 1 | # 2001 September 15 |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 2 | # |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3 | # The author disclaims copyright to this source code. In place of |
| 4 | # a legal notice, here is a blessing: |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 5 | # |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 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. |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 9 | # |
| 10 | #*********************************************************************** |
| 11 | # This file implements regression tests for SQLite library. The |
| 12 | # focus of this file is testing the SELECT statement. |
| 13 | # |
drh | f5bf0a7 | 2001-11-23 00:24:12 +0000 | [diff] [blame^] | 14 | # $Id: select2.test,v 1.16 2001/11/23 00:24:13 drh Exp $ |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 15 | |
| 16 | set testdir [file dirname $argv0] |
| 17 | source $testdir/tester.tcl |
| 18 | |
drh | d75f54e | 2000-06-02 15:05:33 +0000 | [diff] [blame] | 19 | # Create a table with some data |
| 20 | # |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 21 | execsql {CREATE TABLE tbl1(f1 int, f2 int)} |
drh | df16aed | 2000-06-02 14:27:22 +0000 | [diff] [blame] | 22 | set f [open ./testdata1.txt w] |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 23 | for {set i 0} {$i<=30} {incr i} { |
drh | 1b07be5 | 2000-06-02 14:38:46 +0000 | [diff] [blame] | 24 | puts $f "[expr {$i%9}]\t[expr {$i%10}]" |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 25 | } |
drh | df16aed | 2000-06-02 14:27:22 +0000 | [diff] [blame] | 26 | close $f |
| 27 | execsql {COPY tbl1 FROM './testdata1.txt'} |
drh | d75f54e | 2000-06-02 15:05:33 +0000 | [diff] [blame] | 28 | file delete -force ./testdata1.txt |
drh | f5bf0a7 | 2001-11-23 00:24:12 +0000 | [diff] [blame^] | 29 | catch {unset data} |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 30 | |
| 31 | # Do a second query inside a first. |
| 32 | # |
| 33 | do_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 | |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 47 | do_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 | |
drh | d75f54e | 2000-06-02 15:05:33 +0000 | [diff] [blame] | 61 | # Create a largish table |
| 62 | # |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 63 | do_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 |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 70 | # execsql {--vdbe-trace-on--} |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 71 | execsql {COPY tbl2 FROM './testdata1.txt'} |
| 72 | file delete -force ./testdata1.txt |
| 73 | } {} |
drh | d75f54e | 2000-06-02 15:05:33 +0000 | [diff] [blame] | 74 | |
| 75 | do_test select2-2.1 { |
| 76 | execsql {SELECT count(*) FROM tbl2} |
| 77 | } {30000} |
| 78 | do_test select2-2.2 { |
| 79 | execsql {SELECT count(*) FROM tbl2 WHERE f2>1000} |
| 80 | } {29500} |
| 81 | |
| 82 | do_test select2-3.1 { |
drh | e840972 | 2000-06-08 16:54:40 +0000 | [diff] [blame] | 83 | execsql {SELECT f1 FROM tbl2 WHERE 1000=f2} |
drh | d75f54e | 2000-06-02 15:05:33 +0000 | [diff] [blame] | 84 | } {500} |
| 85 | |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 86 | do_test select2-3.2a { |
| 87 | execsql {CREATE INDEX idx1 ON tbl2(f2)} |
| 88 | } {} |
drh | d75f54e | 2000-06-02 15:05:33 +0000 | [diff] [blame] | 89 | |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 90 | do_test select2-3.2b { |
drh | e840972 | 2000-06-08 16:54:40 +0000 | [diff] [blame] | 91 | execsql {SELECT f1 FROM tbl2 WHERE 1000=f2} |
drh | d75f54e | 2000-06-02 15:05:33 +0000 | [diff] [blame] | 92 | } {500} |
drh | e840972 | 2000-06-08 16:54:40 +0000 | [diff] [blame] | 93 | do_test select2-3.2c { |
| 94 | execsql {SELECT f1 FROM tbl2 WHERE f2=1000} |
| 95 | } {500} |
| 96 | do_test select2-3.2d { |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 97 | set sqlite_search_count 0 |
| 98 | execsql {SELECT * FROM tbl2 WHERE 1000=f2} |
| 99 | set sqlite_search_count |
| 100 | } {3} |
drh | 767c200 | 2000-10-19 14:10:08 +0000 | [diff] [blame] | 101 | do_test select2-3.2e { |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 102 | set sqlite_search_count 0 |
| 103 | execsql {SELECT * FROM tbl2 WHERE f2=1000} |
| 104 | set sqlite_search_count |
| 105 | } {3} |
drh | d75f54e | 2000-06-02 15:05:33 +0000 | [diff] [blame] | 106 | |
| 107 | # Make sure queries run faster with an index than without |
| 108 | # |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 109 | do_test select2-3.3 { |
drh | d75f54e | 2000-06-02 15:05:33 +0000 | [diff] [blame] | 110 | execsql {DROP INDEX idx1} |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 111 | set sqlite_search_count 0 |
| 112 | execsql {SELECT f1 FROM tbl2 WHERE f2==2000} |
| 113 | set sqlite_search_count |
| 114 | } {29999} |
drh | d75f54e | 2000-06-02 15:05:33 +0000 | [diff] [blame] | 115 | |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 116 | finish_test |