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