1# 2005 July 28 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 use of indices in WHERE clauses 13# based on recent changes to the optimizer. 14# 15# $Id: where2.test,v 1.15 2009/02/02 01:50:40 drh Exp $ 16 17set testdir [file dirname $argv0] 18source $testdir/tester.tcl 19 20# Build some test data 21# 22do_test where2-1.0 { 23 execsql { 24 BEGIN; 25 CREATE TABLE t1(w int, x int, y int, z int); 26 } 27 for {set i 1} {$i<=100} {incr i} { 28 set w $i 29 set x [expr {int(log($i)/log(2))}] 30 set y [expr {$i*$i + 2*$i + 1}] 31 set z [expr {$x+$y}] 32 ifcapable tclvar { 33 execsql {INSERT INTO t1 VALUES($::w,$::x,$::y,$::z)} 34 } else { 35 execsql {INSERT INTO t1 VALUES(:w,:x,:y,:z)} 36 } 37 } 38 execsql { 39 CREATE UNIQUE INDEX i1w ON t1(w); 40 CREATE INDEX i1xy ON t1(x,y); 41 CREATE INDEX i1zyx ON t1(z,y,x); 42 COMMIT; 43 } 44} {} 45 46# Do an SQL statement. Append the search count to the end of the result. 47# 48proc count sql { 49 set ::sqlite_search_count 0 50 return [concat [execsql $sql] $::sqlite_search_count] 51} 52 53# This procedure executes the SQL. Then it checks to see if the OP_Sort 54# opcode was executed. If an OP_Sort did occur, then "sort" is appended 55# to the result. If no OP_Sort happened, then "nosort" is appended. 56# 57# This procedure is used to check to make sure sorting is or is not 58# occurring as expected. 59# 60proc cksort {sql} { 61 set data [execsql $sql] 62 if {[db status sort]} {set x sort} {set x nosort} 63 lappend data $x 64 return $data 65} 66 67# This procedure executes the SQL. Then it appends to the result the 68# "sort" or "nosort" keyword (as in the cksort procedure above) then 69# it appends the name of the table and index used. 70# 71proc queryplan {sql} { 72 set ::sqlite_sort_count 0 73 set data [execsql $sql] 74 if {$::sqlite_sort_count} {set x sort} {set x nosort} 75 lappend data $x 76 set eqp [execsql "EXPLAIN QUERY PLAN $sql"] 77 # puts eqp=$eqp 78 foreach {a b c x} $eqp { 79 if {[regexp { TABLE (\w+ AS )?(\w+) USING.* INDEX (\w+)\y} \ 80 $x all as tab idx]} { 81 lappend data $tab $idx 82 } elseif {[regexp { TABLE (\w+ AS )?(\w+)\y} $x all as tab]} { 83 lappend data $tab * 84 } 85 } 86 return $data 87} 88 89 90# Prefer a UNIQUE index over another index. 91# 92do_test where2-1.1 { 93 queryplan { 94 SELECT * FROM t1 WHERE w=85 AND x=6 AND y=7396 95 } 96} {85 6 7396 7402 nosort t1 i1w} 97 98# Always prefer a rowid== constraint over any other index. 99# 100do_test where2-1.3 { 101 queryplan { 102 SELECT * FROM t1 WHERE w=85 AND x=6 AND y=7396 AND rowid=85 103 } 104} {85 6 7396 7402 nosort t1 *} 105 106# When constrained by a UNIQUE index, the ORDER BY clause is always ignored. 107# 108do_test where2-2.1 { 109 queryplan { 110 SELECT * FROM t1 WHERE w=85 ORDER BY random(); 111 } 112} {85 6 7396 7402 nosort t1 i1w} 113do_test where2-2.2 { 114 queryplan { 115 SELECT * FROM t1 WHERE x=6 AND y=7396 ORDER BY random(); 116 } 117} {85 6 7396 7402 sort t1 i1xy} 118do_test where2-2.3 { 119 queryplan { 120 SELECT * FROM t1 WHERE rowid=85 AND x=6 AND y=7396 ORDER BY random(); 121 } 122} {85 6 7396 7402 nosort t1 *} 123 124# Ticket [65bdeb9739605cc22966f49208452996ff29a640] 2014-02-26 125# Make sure "ORDER BY random" does not gets optimized out. 126# 127do_test where2-2.4 { 128 db eval { 129 CREATE TABLE x1(a INTEGER PRIMARY KEY, b DEFAULT 1); 130 WITH RECURSIVE 131 cnt(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM cnt WHERE x<50) 132 INSERT INTO x1 SELECT x, 1 FROM cnt; 133 CREATE TABLE x2(x INTEGER PRIMARY KEY); 134 INSERT INTO x2 VALUES(1); 135 } 136 set sql {SELECT * FROM x1, x2 WHERE x=1 ORDER BY random()} 137 set out1 [db eval $sql] 138 set out2 [db eval $sql] 139 set out3 [db eval $sql] 140 expr {$out1!=$out2 && $out2!=$out3} 141} {1} 142do_execsql_test where2-2.5 { 143 -- random() is not optimized out 144 EXPLAIN SELECT * FROM x1, x2 WHERE x=1 ORDER BY random(); 145} {/ random/} 146do_execsql_test where2-2.5b { 147 -- random() is not optimized out 148 EXPLAIN SELECT * FROM x1, x2 WHERE x=1 ORDER BY random(); 149} {/ SorterOpen /} 150do_execsql_test where2-2.6 { 151 -- other constant functions are optimized out 152 EXPLAIN SELECT * FROM x1, x2 WHERE x=1 ORDER BY abs(5); 153} {~/ abs/} 154do_execsql_test where2-2.6b { 155 -- other constant functions are optimized out 156 EXPLAIN SELECT * FROM x1, x2 WHERE x=1 ORDER BY abs(5); 157} {~/ SorterOpen /} 158 159 160 161# Efficient handling of forward and reverse table scans. 162# 163do_test where2-3.1 { 164 queryplan { 165 SELECT * FROM t1 ORDER BY rowid LIMIT 2 166 } 167} {1 0 4 4 2 1 9 10 nosort t1 *} 168do_test where2-3.2 { 169 queryplan { 170 SELECT * FROM t1 ORDER BY rowid DESC LIMIT 2 171 } 172} {100 6 10201 10207 99 6 10000 10006 nosort t1 *} 173 174# The IN operator can be used by indices at multiple layers 175# 176ifcapable subquery { 177 do_test where2-4.1 { 178 queryplan { 179 SELECT * FROM t1 WHERE z IN (10207,10006) AND y IN (10000,10201) 180 AND x>0 AND x<10 181 ORDER BY w 182 } 183 } {99 6 10000 10006 100 6 10201 10207 sort t1 i1zyx} 184 do_test where2-4.2 { 185 queryplan { 186 SELECT * FROM t1 WHERE z IN (10207,10006) AND y=10000 187 AND x>0 AND x<10 188 ORDER BY w 189 } 190 } {99 6 10000 10006 sort t1 i1zyx} 191 do_test where2-4.3 { 192 queryplan { 193 SELECT * FROM t1 WHERE z=10006 AND y IN (10000,10201) 194 AND x>0 AND x<10 195 ORDER BY w 196 } 197 } {99 6 10000 10006 sort t1 i1zyx} 198 ifcapable compound { 199 do_test where2-4.4 { 200 queryplan { 201 SELECT * FROM t1 WHERE z IN (SELECT 10207 UNION SELECT 10006) 202 AND y IN (10000,10201) 203 AND x>0 AND x<10 204 ORDER BY w 205 } 206 } {99 6 10000 10006 100 6 10201 10207 sort t1 i1zyx} 207 do_test where2-4.5 { 208 queryplan { 209 SELECT * FROM t1 WHERE z IN (SELECT 10207 UNION SELECT 10006) 210 AND y IN (SELECT 10000 UNION SELECT 10201) 211 AND x>0 AND x<10 212 ORDER BY w 213 } 214 } {99 6 10000 10006 100 6 10201 10207 sort t1 i1zyx} 215 } 216 do_test where2-4.6a { 217 queryplan { 218 SELECT * FROM t1 219 WHERE x IN (1,2,3,4,5,6,7,8) 220 AND y IN (10000,10001,10002,10003,10004,10005) 221 ORDER BY x 222 } 223 } {99 6 10000 10006 nosort t1 i1xy} 224 do_test where2-4.6b { 225 queryplan { 226 SELECT * FROM t1 227 WHERE x IN (1,2,3,4,5,6,7,8) 228 AND y IN (10000,10001,10002,10003,10004,10005) 229 ORDER BY x DESC 230 } 231 } {99 6 10000 10006 nosort t1 i1xy} 232 do_test where2-4.6c { 233 queryplan { 234 SELECT * FROM t1 235 WHERE x IN (1,2,3,4,5,6,7,8) 236 AND y IN (10000,10001,10002,10003,10004,10005) 237 ORDER BY x, y 238 } 239 } {99 6 10000 10006 nosort t1 i1xy} 240 do_test where2-4.6d { 241 queryplan { 242 SELECT * FROM t1 243 WHERE x IN (1,2,3,4,5,6,7,8) 244 AND y IN (10000,10001,10002,10003,10004,10005) 245 ORDER BY x, y DESC 246 } 247 } {99 6 10000 10006 sort t1 i1xy} 248 249 # Duplicate entires on the RHS of an IN operator do not cause duplicate 250 # output rows. 251 # 252 do_test where2-4.6x { 253 queryplan { 254 SELECT * FROM t1 WHERE z IN (10207,10006,10006,10207) 255 ORDER BY w 256 } 257 } {99 6 10000 10006 100 6 10201 10207 sort t1 i1zyx} 258 do_test where2-4.6y { 259 queryplan { 260 SELECT * FROM t1 WHERE z IN (10207,10006,10006,10207) 261 ORDER BY w DESC 262 } 263 } {100 6 10201 10207 99 6 10000 10006 sort t1 i1zyx} 264 ifcapable compound { 265 do_test where2-4.7 { 266 queryplan { 267 SELECT * FROM t1 WHERE z IN ( 268 SELECT 10207 UNION ALL SELECT 10006 269 UNION ALL SELECT 10006 UNION ALL SELECT 10207) 270 ORDER BY w 271 } 272 } {99 6 10000 10006 100 6 10201 10207 sort t1 i1zyx} 273 } 274 275} ;# ifcapable subquery 276 277# The use of an IN operator disables the index as a sorter. 278# 279do_test where2-5.1 { 280 queryplan { 281 SELECT * FROM t1 WHERE w=99 ORDER BY w 282 } 283} {99 6 10000 10006 nosort t1 i1w} 284 285ifcapable subquery { 286 do_test where2-5.2a { 287 queryplan { 288 SELECT * FROM t1 WHERE w IN (99) ORDER BY w 289 } 290 } {99 6 10000 10006 nosort t1 i1w} 291 do_test where2-5.2b { 292 queryplan { 293 SELECT * FROM t1 WHERE w IN (99) ORDER BY w DESC 294 } 295 } {99 6 10000 10006 nosort t1 i1w} 296} 297 298# Verify that OR clauses get translated into IN operators. 299# 300set ::idx {} 301ifcapable subquery {set ::idx i1w} 302do_test where2-6.1.1 { 303 queryplan { 304 SELECT * FROM t1 WHERE w=99 OR w=100 ORDER BY +w 305 } 306} [list 99 6 10000 10006 100 6 10201 10207 sort t1 $::idx] 307do_test where2-6.1.2 { 308 queryplan { 309 SELECT * FROM t1 WHERE 99=w OR 100=w ORDER BY +w 310 } 311} [list 99 6 10000 10006 100 6 10201 10207 sort t1 $::idx] 312do_test where2-6.2 { 313 queryplan { 314 SELECT * FROM t1 WHERE w=99 OR w=100 OR 6=w ORDER BY +w 315 } 316} [list 6 2 49 51 99 6 10000 10006 100 6 10201 10207 sort t1 $::idx] 317 318do_test where2-6.3 { 319 queryplan { 320 SELECT * FROM t1 WHERE w=99 OR w=100 OR 6=+w ORDER BY +w 321 } 322} {6 2 49 51 99 6 10000 10006 100 6 10201 10207 sort t1 *} 323do_test where2-6.4 { 324 queryplan { 325 SELECT * FROM t1 WHERE w=99 OR +w=100 OR 6=w ORDER BY +w 326 } 327} {6 2 49 51 99 6 10000 10006 100 6 10201 10207 sort t1 *} 328 329set ::idx {} 330ifcapable subquery {set ::idx i1zyx} 331do_test where2-6.5 { 332 queryplan { 333 SELECT b.* FROM t1 a, t1 b 334 WHERE a.w=1 AND (a.y=b.z OR b.z=10) 335 ORDER BY +b.w 336 } 337} [list 1 0 4 4 2 1 9 10 sort a i1w b $::idx] 338do_test where2-6.6 { 339 queryplan { 340 SELECT b.* FROM t1 a, t1 b 341 WHERE a.w=1 AND (b.z=10 OR a.y=b.z OR b.z=10) 342 ORDER BY +b.w 343 } 344} [list 1 0 4 4 2 1 9 10 sort a i1w b $::idx] 345 346if {[permutation] != "no_optimization"} { 347 348# Ticket #2249. Make sure the OR optimization is not attempted if 349# comparisons between columns of different affinities are needed. 350# 351do_test where2-6.7 { 352 execsql { 353 CREATE TABLE t2249a(a TEXT UNIQUE, x CHAR(100)); 354 CREATE TABLE t2249b(b INTEGER); 355 INSERT INTO t2249a(a) VALUES('0123'); 356 INSERT INTO t2249b VALUES(123); 357 } 358 queryplan { 359 -- Because a is type TEXT and b is type INTEGER, both a and b 360 -- will attempt to convert to NUMERIC before the comparison. 361 -- They will thus compare equal. 362 -- 363 SELECT b,a FROM t2249b CROSS JOIN t2249a WHERE a=b; 364 } 365} {123 0123 nosort t2249b * t2249a sqlite_autoindex_t2249a_1} 366do_test where2-6.9 { 367 queryplan { 368 -- The + operator removes affinity from the rhs. No conversions 369 -- occur and the comparison is false. The result is an empty set. 370 -- 371 SELECT b,a FROM t2249b CROSS JOIN t2249a WHERE a=+b; 372 } 373} {nosort t2249b * t2249a sqlite_autoindex_t2249a_1} 374do_test where2-6.9.2 { 375 # The same thing but with the expression flipped around. 376 queryplan { 377 SELECT b,a FROM t2249b CROSS JOIN t2249a WHERE +b=a 378 } 379} {nosort t2249b * t2249a sqlite_autoindex_t2249a_1} 380do_test where2-6.10 { 381 queryplan { 382 -- Use + on both sides of the comparison to disable indices 383 -- completely. Make sure we get the same result. 384 -- 385 SELECT b,a FROM t2249b CROSS JOIN t2249a WHERE +a=+b; 386 } 387} {nosort t2249b * t2249a sqlite_autoindex_t2249a_1} 388do_test where2-6.11 { 389 # This will not attempt the OR optimization because of the a=b 390 # comparison. 391 queryplan { 392 SELECT b,a FROM t2249b CROSS JOIN t2249a WHERE a=b OR a='hello'; 393 } 394} {123 0123 nosort t2249b * t2249a sqlite_autoindex_t2249a_1} 395do_test where2-6.11.2 { 396 # Permutations of the expression terms. 397 queryplan { 398 SELECT b,a FROM t2249b CROSS JOIN t2249a WHERE b=a OR a='hello'; 399 } 400} {123 0123 nosort t2249b * t2249a sqlite_autoindex_t2249a_1} 401do_test where2-6.11.3 { 402 # Permutations of the expression terms. 403 queryplan { 404 SELECT b,a FROM t2249b CROSS JOIN t2249a WHERE 'hello'=a OR b=a; 405 } 406} {123 0123 nosort t2249b * t2249a sqlite_autoindex_t2249a_1} 407do_test where2-6.11.4 { 408 # Permutations of the expression terms. 409 queryplan { 410 SELECT b,a FROM t2249b CROSS JOIN t2249a WHERE a='hello' OR b=a; 411 } 412} {123 0123 nosort t2249b * t2249a sqlite_autoindex_t2249a_1} 413ifcapable explain&&subquery { 414 # These tests are not run if subquery support is not included in the 415 # build. This is because these tests test the "a = 1 OR a = 2" to 416 # "a IN (1, 2)" optimisation transformation, which is not enabled if 417 # subqueries and the IN operator is not available. 418 # 419 do_test where2-6.12 { 420 # In this case, the +b disables the affinity conflict and allows 421 # the OR optimization to be used again. The result is now an empty 422 # set, the same as in where2-6.9. 423 queryplan { 424 SELECT b,a FROM t2249b CROSS JOIN t2249a WHERE a=+b OR a='hello'; 425 } 426 } {nosort t2249b * t2249a sqlite_autoindex_t2249a_1} 427 do_test where2-6.12.2 { 428 # In this case, the +b disables the affinity conflict and allows 429 # the OR optimization to be used again. The result is now an empty 430 # set, the same as in where2-6.9. 431 queryplan { 432 SELECT b,a FROM t2249b CROSS JOIN t2249a WHERE a='hello' OR +b=a; 433 } 434 } {nosort t2249b * t2249a sqlite_autoindex_t2249a_1} 435 do_test where2-6.12.3 { 436 # In this case, the +b disables the affinity conflict and allows 437 # the OR optimization to be used again. The result is now an empty 438 # set, the same as in where2-6.9. 439 queryplan { 440 SELECT b,a FROM t2249b CROSS JOIN t2249a WHERE +b=a OR a='hello'; 441 } 442 } {nosort t2249b * t2249a sqlite_autoindex_t2249a_1} 443 do_test where2-6.13 { 444 # The addition of +a on the second term disabled the OR optimization. 445 # But we should still get the same empty-set result as in where2-6.9. 446 queryplan { 447 SELECT b,a FROM t2249b CROSS JOIN t2249a WHERE a=+b OR +a='hello'; 448 } 449 } {nosort t2249b * t2249a sqlite_autoindex_t2249a_1} 450} 451 452# Variations on the order of terms in a WHERE clause in order 453# to make sure the OR optimizer can recognize them all. 454do_test where2-6.20 { 455 queryplan { 456 SELECT x.a, y.a FROM t2249a x CROSS JOIN t2249a y WHERE x.a=y.a 457 } 458} {0123 0123 nosort x sqlite_autoindex_t2249a_1 y sqlite_autoindex_t2249a_1} 459ifcapable explain&&subquery { 460 # These tests are not run if subquery support is not included in the 461 # build. This is because these tests test the "a = 1 OR a = 2" to 462 # "a IN (1, 2)" optimisation transformation, which is not enabled if 463 # subqueries and the IN operator is not available. 464 # 465 do_test where2-6.21 { 466 queryplan { 467 SELECT x.a,y.a FROM t2249a x CROSS JOIN t2249a y 468 WHERE x.a=y.a OR y.a='hello' 469 } 470 } {0123 0123 nosort x sqlite_autoindex_t2249a_1 y sqlite_autoindex_t2249a_1} 471 do_test where2-6.22 { 472 queryplan { 473 SELECT x.a,y.a FROM t2249a x CROSS JOIN t2249a y 474 WHERE y.a=x.a OR y.a='hello' 475 } 476 } {0123 0123 nosort x sqlite_autoindex_t2249a_1 y sqlite_autoindex_t2249a_1} 477 do_test where2-6.23 { 478 queryplan { 479 SELECT x.a,y.a FROM t2249a x CROSS JOIN t2249a y 480 WHERE y.a='hello' OR x.a=y.a 481 } 482 } {0123 0123 nosort x sqlite_autoindex_t2249a_1 y sqlite_autoindex_t2249a_1} 483} 484 485# Unique queries (queries that are guaranteed to return only a single 486# row of result) do not call the sorter. But all tables must give 487# a unique result. If any one table in the join does not give a unique 488# result then sorting is necessary. 489# 490do_test where2-7.1 { 491 cksort { 492 create table t8(a unique, b, c); 493 insert into t8 values(1,2,3); 494 insert into t8 values(2,3,4); 495 create table t9(x,y); 496 insert into t9 values(2,4); 497 insert into t9 values(2,3); 498 select y from t8, t9 where a=1 order by a, y; 499 } 500} {3 4 sort} 501do_test where2-7.2 { 502 cksort { 503 select * from t8 where a=1 order by b, c 504 } 505} {1 2 3 nosort} 506do_test where2-7.3 { 507 cksort { 508 select * from t8, t9 where a=1 and y=3 order by b, x 509 } 510} {1 2 3 2 3 sort} 511do_test where2-7.4 { 512 cksort { 513 create unique index i9y on t9(y); 514 select * from t8, t9 where a=1 and y=3 order by b, x 515 } 516} {1 2 3 2 3 nosort} 517 518} ;# if {[permutation] != "no_optimization"} 519 520# Ticket #1807. Using IN constrains on multiple columns of 521# a multi-column index. 522# 523ifcapable subquery { 524 do_test where2-8.1 { 525 execsql { 526 SELECT * FROM t1 WHERE x IN (20,21) AND y IN (1,2) 527 } 528 } {} 529 do_test where2-8.2 { 530 execsql { 531 SELECT * FROM t1 WHERE x IN (1,2) AND y IN (-5,-6) 532 } 533 } {} 534 execsql {CREATE TABLE tx AS SELECT * FROM t1} 535 do_test where2-8.3 { 536 execsql { 537 SELECT w FROM t1 538 WHERE x IN (SELECT x FROM tx WHERE rowid<0) 539 AND +y IN (SELECT y FROM tx WHERE rowid=1) 540 } 541 } {} 542 do_test where2-8.4 { 543 execsql { 544 SELECT w FROM t1 545 WHERE x IN (SELECT x FROM tx WHERE rowid=1) 546 AND y IN (SELECT y FROM tx WHERE rowid<0) 547 } 548 } {} 549 #set sqlite_where_trace 1 550 do_test where2-8.5 { 551 execsql { 552 CREATE INDEX tx_xyz ON tx(x, y, z, w); 553 SELECT w FROM tx 554 WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20) 555 AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20) 556 AND z IN (SELECT z FROM t1 WHERE w BETWEEN 12 AND 14) 557 } 558 } {12 13 14} 559 do_test where2-8.6 { 560 execsql { 561 SELECT w FROM tx 562 WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20) 563 AND y IN (SELECT y FROM t1 WHERE w BETWEEN 12 AND 14) 564 AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20) 565 } 566 } {12 13 14} 567 do_test where2-8.7 { 568 execsql { 569 SELECT w FROM tx 570 WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 12 AND 14) 571 AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20) 572 AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20) 573 } 574 } {10 11 12 13 14 15} 575 do_test where2-8.8 { 576 execsql { 577 SELECT w FROM tx 578 WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20) 579 AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20) 580 AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20) 581 } 582 } {10 11 12 13 14 15 16 17 18 19 20} 583 do_test where2-8.9 { 584 execsql { 585 SELECT w FROM tx 586 WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20) 587 AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20) 588 AND z IN (SELECT z FROM t1 WHERE w BETWEEN 2 AND 4) 589 } 590 } {} 591 do_test where2-8.10 { 592 execsql { 593 SELECT w FROM tx 594 WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20) 595 AND y IN (SELECT y FROM t1 WHERE w BETWEEN 2 AND 4) 596 AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20) 597 } 598 } {} 599 do_test where2-8.11 { 600 execsql { 601 SELECT w FROM tx 602 WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 2 AND 4) 603 AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20) 604 AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20) 605 } 606 } {} 607 do_test where2-8.12 { 608 execsql { 609 SELECT w FROM tx 610 WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20) 611 AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20) 612 AND z IN (SELECT z FROM t1 WHERE w BETWEEN -4 AND -2) 613 } 614 } {} 615 do_test where2-8.13 { 616 execsql { 617 SELECT w FROM tx 618 WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20) 619 AND y IN (SELECT y FROM t1 WHERE w BETWEEN -4 AND -2) 620 AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20) 621 } 622 } {} 623 do_test where2-8.14 { 624 execsql { 625 SELECT w FROM tx 626 WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN -4 AND -2) 627 AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20) 628 AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20) 629 } 630 } {} 631 do_test where2-8.15 { 632 execsql { 633 SELECT w FROM tx 634 WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20) 635 AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20) 636 AND z IN (SELECT z FROM t1 WHERE w BETWEEN 200 AND 300) 637 } 638 } {} 639 do_test where2-8.16 { 640 execsql { 641 SELECT w FROM tx 642 WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20) 643 AND y IN (SELECT y FROM t1 WHERE w BETWEEN 200 AND 300) 644 AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20) 645 } 646 } {} 647 do_test where2-8.17 { 648 execsql { 649 SELECT w FROM tx 650 WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 200 AND 300) 651 AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20) 652 AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20) 653 } 654 } {} 655 do_test where2-8.18 { 656 execsql { 657 SELECT w FROM tx 658 WHERE x IN (SELECT x FROM t1 WHERE +w BETWEEN 10 AND 20) 659 AND y IN (SELECT y FROM t1 WHERE +w BETWEEN 10 AND 20) 660 AND z IN (SELECT z FROM t1 WHERE +w BETWEEN 200 AND 300) 661 } 662 } {} 663 do_test where2-8.19 { 664 execsql { 665 SELECT w FROM tx 666 WHERE x IN (SELECT x FROM t1 WHERE +w BETWEEN 10 AND 20) 667 AND y IN (SELECT y FROM t1 WHERE +w BETWEEN 200 AND 300) 668 AND z IN (SELECT z FROM t1 WHERE +w BETWEEN 10 AND 20) 669 } 670 } {} 671 do_test where2-8.20 { 672 execsql { 673 SELECT w FROM tx 674 WHERE x IN (SELECT x FROM t1 WHERE +w BETWEEN 200 AND 300) 675 AND y IN (SELECT y FROM t1 WHERE +w BETWEEN 10 AND 20) 676 AND z IN (SELECT z FROM t1 WHERE +w BETWEEN 10 AND 20) 677 } 678 } {} 679} 680 681# Make sure WHERE clauses of the form A=1 AND (B=2 OR B=3) are optimized 682# when we have an index on A and B. 683# 684ifcapable or_opt&&tclvar { 685 do_test where2-9.1 { 686 execsql { 687 BEGIN; 688 CREATE TABLE t10(a,b,c); 689 INSERT INTO t10 VALUES(1,1,1); 690 INSERT INTO t10 VALUES(1,2,2); 691 INSERT INTO t10 VALUES(1,3,3); 692 } 693 for {set i 4} {$i<=1000} {incr i} { 694 execsql {INSERT INTO t10 VALUES(1,$i,$i)} 695 } 696 execsql { 697 CREATE INDEX i10 ON t10(a,b); 698 COMMIT; 699 SELECT count(*) FROM t10; 700 } 701 } 1000 702 ifcapable subquery { 703 do_test where2-9.2 { 704 count { 705 SELECT * FROM t10 WHERE a=1 AND (b=2 OR b=3) 706 } 707 } {1 2 2 1 3 3 7} 708 } 709} 710 711# Indices with redundant columns 712# 713do_test where2-11.1 { 714 execsql { 715 CREATE TABLE t11(a,b,c,d); 716 CREATE INDEX i11aba ON t11(a,b,a,c); -- column A occurs twice. 717 INSERT INTO t11 VALUES(1,2,3,4); 718 INSERT INTO t11 VALUES(5,6,7,8); 719 INSERT INTO t11 VALUES(1,2,9,10); 720 INSERT INTO t11 VALUES(5,11,12,13); 721 SELECT c FROM t11 WHERE a=1 AND b=2 ORDER BY c; 722 } 723} {3 9} 724do_test where2-11.2 { 725 execsql { 726 CREATE INDEX i11cccccccc ON t11(c,c,c,c,c,c,c,c); -- repeated column 727 SELECT d FROM t11 WHERE c=9; 728 } 729} {10} 730do_test where2-11.3 { 731 execsql { 732 SELECT d FROM t11 WHERE c IN (1,2,3,4,5); 733 } 734} {4} 735do_test where2-11.4 { 736 execsql { 737 SELECT d FROM t11 WHERE c=7 OR (a=1 AND b=2) ORDER BY d; 738 } 739} {4 8 10} 740 741# Verify that the OR clause is used in an outer loop even when 742# the OR clause scores slightly better on an inner loop. 743if {[permutation] != "no_optimization"} { 744do_execsql_test where2-12.1 { 745 CREATE TABLE t12(x INTEGER PRIMARY KEY, y INT, z CHAR(100)); 746 CREATE INDEX t12y ON t12(y); 747 EXPLAIN QUERY PLAN 748 SELECT a.x, b.x 749 FROM t12 AS a JOIN t12 AS b ON a.y=b.x 750 WHERE (b.x=$abc OR b.y=$abc); 751} {/.*SEARCH TABLE t12 AS b .*SEARCH TABLE t12 AS b .*/} 752} 753 754# Verify that all necessary OP_OpenRead opcodes occur in the OR optimization. 755# 756do_execsql_test where2-13.1 { 757 CREATE TABLE t13(a,b); 758 CREATE INDEX t13a ON t13(a); 759 INSERT INTO t13 VALUES(4,5); 760 SELECT * FROM t13 WHERE (1=2 AND a=3) OR a=4; 761} {4 5} 762 763finish_test 764