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 | 5f3b4ab | 2004-05-27 17:22:54 +0000 | [diff] [blame^] | 14 | # $Id: select2.test,v 1.21 2004/05/27 17:22:56 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 | 5f3b4ab | 2004-05-27 17:22:54 +0000 | [diff] [blame^] | 22 | execsql {BEGIN} |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 23 | for {set i 0} {$i<=30} {incr i} { |
drh | 5f3b4ab | 2004-05-27 17:22:54 +0000 | [diff] [blame^] | 24 | execsql "INSERT INTO tbl1 VALUES([expr {$i%9}],[expr {$i%10}])" |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 25 | } |
drh | 5f3b4ab | 2004-05-27 17:22:54 +0000 | [diff] [blame^] | 26 | execsql {COMMIT} |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 27 | |
| 28 | # Do a second query inside a first. |
| 29 | # |
| 30 | do_test select2-1.1 { |
| 31 | set sql {SELECT DISTINCT f1 FROM tbl1 ORDER BY f1} |
| 32 | set r {} |
| 33 | db eval $sql data { |
| 34 | set f1 $data(f1) |
| 35 | lappend r $f1: |
| 36 | set sql2 "SELECT f2 FROM tbl1 WHERE f1=$f1 ORDER BY f2" |
| 37 | db eval $sql2 d2 { |
| 38 | lappend r $d2(f2) |
| 39 | } |
| 40 | } |
| 41 | set r |
| 42 | } {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} |
| 43 | |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 44 | do_test select2-1.2 { |
| 45 | set sql {SELECT DISTINCT f1 FROM tbl1 WHERE f1>3 AND f1<5} |
| 46 | set r {} |
| 47 | db eval $sql data { |
| 48 | set f1 $data(f1) |
| 49 | lappend r $f1: |
| 50 | set sql2 "SELECT f2 FROM tbl1 WHERE f1=$f1 ORDER BY f2" |
| 51 | db eval $sql2 d2 { |
| 52 | lappend r $d2(f2) |
| 53 | } |
| 54 | } |
| 55 | set r |
| 56 | } {4: 2 3 4} |
| 57 | |
drh | d75f54e | 2000-06-02 15:05:33 +0000 | [diff] [blame] | 58 | # Create a largish table |
| 59 | # |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 60 | do_test select2-2.0 { |
drh | 5f3b4ab | 2004-05-27 17:22:54 +0000 | [diff] [blame^] | 61 | execsql {CREATE TABLE tbl2(f1 int, f2 int, f3 int); BEGIN;} |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 62 | for {set i 1} {$i<=30000} {incr i} { |
drh | 5f3b4ab | 2004-05-27 17:22:54 +0000 | [diff] [blame^] | 63 | execsql "INSERT INTO tbl2 VALUES($i,[expr {$i*2}],[expr {$i*3}])" |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 64 | } |
drh | 5f3b4ab | 2004-05-27 17:22:54 +0000 | [diff] [blame^] | 65 | execsql {COMMIT} |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 66 | } {} |
drh | d75f54e | 2000-06-02 15:05:33 +0000 | [diff] [blame] | 67 | |
| 68 | do_test select2-2.1 { |
| 69 | execsql {SELECT count(*) FROM tbl2} |
| 70 | } {30000} |
| 71 | do_test select2-2.2 { |
| 72 | execsql {SELECT count(*) FROM tbl2 WHERE f2>1000} |
| 73 | } {29500} |
| 74 | |
| 75 | do_test select2-3.1 { |
drh | e840972 | 2000-06-08 16:54:40 +0000 | [diff] [blame] | 76 | execsql {SELECT f1 FROM tbl2 WHERE 1000=f2} |
drh | d75f54e | 2000-06-02 15:05:33 +0000 | [diff] [blame] | 77 | } {500} |
| 78 | |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 79 | do_test select2-3.2a { |
| 80 | execsql {CREATE INDEX idx1 ON tbl2(f2)} |
| 81 | } {} |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 82 | do_test select2-3.2b { |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +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} |
drh | e840972 | 2000-06-08 16:54:40 +0000 | [diff] [blame] | 85 | do_test select2-3.2c { |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 86 | execsql {SELECT f1 FROM tbl2 WHERE f2=1000} |
drh | e840972 | 2000-06-08 16:54:40 +0000 | [diff] [blame] | 87 | } {500} |
| 88 | do_test select2-3.2d { |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 89 | set sqlite_search_count 0 |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 90 | execsql {SELECT * FROM tbl2 WHERE 1000=f2} |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 91 | set sqlite_search_count |
| 92 | } {3} |
drh | 767c200 | 2000-10-19 14:10:08 +0000 | [diff] [blame] | 93 | do_test select2-3.2e { |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 94 | set sqlite_search_count 0 |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 95 | execsql {SELECT * FROM tbl2 WHERE f2=1000} |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 96 | set sqlite_search_count |
| 97 | } {3} |
drh | d75f54e | 2000-06-02 15:05:33 +0000 | [diff] [blame] | 98 | |
| 99 | # Make sure queries run faster with an index than without |
| 100 | # |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 101 | do_test select2-3.3 { |
drh | d75f54e | 2000-06-02 15:05:33 +0000 | [diff] [blame] | 102 | execsql {DROP INDEX idx1} |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 103 | set sqlite_search_count 0 |
| 104 | execsql {SELECT f1 FROM tbl2 WHERE f2==2000} |
| 105 | set sqlite_search_count |
| 106 | } {29999} |
drh | d75f54e | 2000-06-02 15:05:33 +0000 | [diff] [blame] | 107 | |
drh | dd57912 | 2002-04-02 01:58:57 +0000 | [diff] [blame] | 108 | # Make sure we can optimize functions in the WHERE clause that |
| 109 | # use fields from two or more different table. (Bug #6) |
| 110 | # |
| 111 | do_test select2-4.1 { |
| 112 | execsql { |
| 113 | CREATE TABLE aa(a); |
| 114 | CREATE TABLE bb(b); |
| 115 | INSERT INTO aa VALUES(1); |
| 116 | INSERT INTO aa VALUES(3); |
| 117 | INSERT INTO bb VALUES(2); |
| 118 | INSERT INTO bb VALUES(4); |
| 119 | SELECT * FROM aa, bb WHERE max(a,b)>2; |
| 120 | } |
| 121 | } {1 4 3 2 3 4} |
drh | 3f6b548 | 2002-04-02 13:26:10 +0000 | [diff] [blame] | 122 | do_test select2-4.2 { |
| 123 | execsql { |
| 124 | INSERT INTO bb VALUES(0); |
| 125 | SELECT * FROM aa, bb WHERE b; |
| 126 | } |
| 127 | } {1 2 1 4 3 2 3 4} |
| 128 | do_test select2-4.3 { |
| 129 | execsql { |
| 130 | SELECT * FROM aa, bb WHERE NOT b; |
| 131 | } |
| 132 | } {1 0 3 0} |
| 133 | do_test select2-4.4 { |
| 134 | execsql { |
| 135 | SELECT * FROM aa, bb WHERE min(a,b); |
| 136 | } |
| 137 | } {1 2 1 4 3 2 3 4} |
| 138 | do_test select2-4.5 { |
| 139 | execsql { |
| 140 | SELECT * FROM aa, bb WHERE NOT min(a,b); |
| 141 | } |
| 142 | } {1 0 3 0} |
| 143 | do_test select2-4.6 { |
| 144 | execsql { |
| 145 | SELECT * FROM aa, bb WHERE CASE WHEN a=b-1 THEN 1 END; |
| 146 | } |
| 147 | } {1 2 3 4} |
| 148 | do_test select2-4.7 { |
| 149 | execsql { |
| 150 | SELECT * FROM aa, bb WHERE CASE WHEN a=b-1 THEN 0 ELSE 1 END; |
| 151 | } |
| 152 | } {1 4 1 0 3 2 3 0} |
drh | dd57912 | 2002-04-02 01:58:57 +0000 | [diff] [blame] | 153 | |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 154 | finish_test |