Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/test/seekscan1.test # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / test / seekscan1.test` 201 lines ยท 4731 bytes Raw Blame History 1 # 2022 October 7 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. 12 # 13 14 set testdir [file dirname $argv0] 15 source $testdir/tester.tcl 16 set testprefix seekscan1 17 18 do_execsql_test 1.0 { 19 CREATE TABLE t1(a TEXT, b INT, c INT NOT NULL, PRIMARY KEY(a,b,c)); 20 WITH RECURSIVE c(x) AS (VALUES(0) UNION ALL SELECT x+1 FROM c WHERE x<1997) 21 INSERT INTO t1(a,b,c) SELECT printf('xyz%d',x/10),x/6,x FROM c; 22 INSERT INTO t1 VALUES('abc',234,6); 23 INSERT INTO t1 VALUES('abc',345,7); 24 ANALYZE; 25 } 26 27 28 do_execsql_test 1.1 { 29 SELECT a,b,c FROM t1 30 WHERE b IN (234, 345) AND c BETWEEN 6 AND 6.5 AND a='abc' 31 ORDER BY a, b; 32 } { 33 abc 234 6 34 } 35 36 do_execsql_test 1.2 { 37 SELECT a,b,c FROM t1 38 WHERE b IN (234, 345) AND c BETWEEN 6 AND 7 AND a='abc' 39 ORDER BY a, b; 40 } { 41 abc 234 6 42 abc 345 7 43 } 44 45 do_execsql_test 1.3 { 46 SELECT a,b,c FROM t1 47 WHERE b IN (234, 345) AND c >=6 AND a='abc' 48 ORDER BY a, b; 49 } { 50 abc 234 6 51 abc 345 7 52 } 53 54 do_execsql_test 1.4 { 55 SELECT a,b,c FROM t1 56 WHERE b IN (234, 345) AND c<=7 AND a='abc' 57 ORDER BY a, b; 58 } { 59 abc 234 6 60 abc 345 7 61 } 62 63 do_execsql_test 1.5 { 64 SELECT a,b,c FROM t1 WHERE b IN (235, 345) AND c<=3 AND a='abc' ORDER BY a, b; 65 } 66 67 proc do_unordered_test {tn sql res} { 68 set body [subst -nocom {lsort [execsql {$sql}]}] 69 uplevel [list do_test $tn $body [lsort $res]] 70 } 71 72 #------------------------------------------------------------------------- 73 # Report 74 # 75 foreach {tn idx} { 76 1 { 77 CREATE UNIQUE INDEX i10 ON t1(c1, c0); 78 CREATE INDEX i1 ON t0(c0, c1); 79 } 80 81 2 { 82 CREATE UNIQUE INDEX i10 ON t1(c0, c1); 83 CREATE INDEX i1 ON t0(c1, c0); 84 } 85 86 3 { 87 CREATE UNIQUE INDEX i10 ON t1(c0, c1); 88 CREATE INDEX i1 ON t0(c0, c1); 89 } 90 91 } { 92 reset_db 93 do_execsql_test 2.$tn.0 { 94 CREATE TABLE t0(c0, c1); 95 CREATE TABLE t1(c0, c1); 96 } 97 do_execsql_test 2.$tn.1 $idx 98 do_execsql_test 2.$tn.2 { 99 100 CREATE INDEX i2 ON t0(c0); 101 CREATE INDEX i3 ON t0(c1); 102 103 INSERT INTO t0 VALUES (NULL,'c'), (0,'a'), (0.83,'d'), (9,'e'), (0.87,'b'); 104 INSERT INTO t1 VALUES (NULL,'c'), (0,'a'), (0.83,'d'), (9,'e'), (0.87,'b'); 105 106 ANALYZE; 107 DELETE FROM sqlite_stat1; 108 INSERT INTO sqlite_stat1 VALUES('t0', 'i1', '4 1 1'); 109 INSERT INTO sqlite_stat1 VALUES('t0', 'i0', '4 1 1'); 110 INSERT INTO sqlite_stat1 VALUES('t1', 'i11', '4 1 1'); 111 INSERT INTO sqlite_stat1 VALUES('t1', 'i10', '4 1 1'); 112 } 113 114 # Ensure new ANALYZE data is loaded. 115 db close 116 sqlite3 db test.db 117 118 if {$tn==3} { 119 set sqlite_where_trace [expr 0x40] 120 # explain_i { SELECT * FROM t0 WHERE (c0,c1) IN (SELECT c0,c1 FROM t1) } 121 set sqlite_where_trace 0 122 } 123 do_unordered_test 2.$tn.3 { 124 SELECT * FROM t0 WHERE (c0,c1) IN (SELECT c0,c1 FROM t1); 125 } { 126 0 a 127 0.83 d 128 0.87 b 129 9 e 130 } 131 132 do_unordered_test 2.$tn.4 { 133 SELECT * FROM t0 WHERE (c1,c0) IN (SELECT c1,c0 FROM t1); 134 } { 135 0 a 136 0.83 d 137 0.87 b 138 9 e 139 } 140 } 141 142 #------------------------------------------------------------------------- 143 reset_db 144 145 do_execsql_test 3.0 { 146 CREATE TABLE t1(a, b, c); 147 CREATE TABLE t2(c, d); 148 149 INSERT INTO t1 VALUES 150 (1, 'a', 4), 151 (1, 'b', 3), 152 (1, 'c', 2), 153 (1, 'd', 1), 154 (1, 'd', 2); 155 INSERT INTO t2 VALUES 156 ('a', 4), 157 ('c', 1), 158 ('d', 1), 159 ('d', 2); 160 161 CREATE INDEX t1abc ON t1(a, b, c); 162 ANALYZE; 163 164 UPDATE sqlite_stat1 SET stat = '2000 2 2 2' WHERE idx='t1abc'; 165 ANALYZE sqlite_master; 166 } 167 168 foreach {tn idx used} { 169 1 { CREATE UNIQUE INDEX t2idx ON t2(c, d); } 0 170 2 { CREATE UNIQUE INDEX t2idx ON t2(d, c); } 0 171 3 { CREATE UNIQUE INDEX t2idx ON t2(c, d DESC); } 0 172 } { 173 do_execsql_test 3.$tn.1 { DROP INDEX IF EXISTS t2idx; } 174 execsql $idx 175 set SQL { SELECT * FROM t1 WHERE a=1 AND (b, c) IN (SELECT c, d FROM t2); } 176 177 do_test 3.$tn.2 { 178 set res [lsearch [execsql "EXPLAIN $SQL"] SeekScan] 179 expr $res>=0 180 } {1} 181 182 do_test 3.$tn.3 { 183 set res [lsearch [execsql "EXPLAIN $SQL"] t2idx] 184 expr $res>=0 185 } $used 186 187 do_execsql_test 3.$tn.3 $SQL { 188 1 a 4 1 d 1 1 d 2 189 } 190 } 191 192 # Bug report 2026-10-23T03:09:01Z 193 do_execsql_test 4.1 { 194 DROP TABLE t1; 195 CREATE TABLE t1(a, b, PRIMARY KEY(a, b)) WITHOUT ROWID; 196 INSERT INTO t1 VALUES(1, 1), (2, 1), (3, 1), (4, 1); 197 ANALYZE; 198 SELECT * FROM t1 WHERE a = 2 OR (a = 1 AND b IN (1, 2)); 199 } {2 1 1 1} 200 201 finish_test