1# 2001 September 15 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 clases. 13# 14# $Id: where.test,v 1.32 2005/07/28 16:51:51 drh Exp $ 15 16set testdir [file dirname $argv0] 17source $testdir/tester.tcl 18 19# Build some test data 20# 21do_test where-1.0 { 22 execsql { 23 CREATE TABLE t1(w int, x int, y int); 24 CREATE TABLE t2(p int, q int, r int, s int); 25 } 26 for {set i 1} {$i<=100} {incr i} { 27 set w $i 28 set x [expr {int(log($i)/log(2))}] 29 set y [expr {$i*$i + 2*$i + 1}] 30 execsql "INSERT INTO t1 VALUES($w,$x,$y)" 31 } 32 33 ifcapable subquery { 34 execsql { 35 INSERT INTO t2 SELECT 101-w, x, (SELECT max(y) FROM t1)+1-y, y FROM t1; 36 } 37 } else { 38 set maxy [execsql {select max(y) from t1}] 39 execsql " 40 INSERT INTO t2 SELECT 101-w, x, $maxy+1-y, y FROM t1; 41 " 42 } 43 44 execsql { 45 CREATE INDEX i1w ON t1(w); 46 CREATE INDEX i1xy ON t1(x,y); 47 CREATE INDEX i2p ON t2(p); 48 CREATE INDEX i2r ON t2(r); 49 CREATE INDEX i2qs ON t2(q, s); 50 } 51} {} 52 53# Do an SQL statement. Append the search count to the end of the result. 54# 55proc count sql { 56 set ::sqlite_search_count 0 57 return [concat [execsql $sql] $::sqlite_search_count] 58} 59 60# Verify that queries use an index. We are using the special variable 61# "sqlite_search_count" which tallys the number of executions of MoveTo 62# and Next operators in the VDBE. By verifing that the search count is 63# small we can be assured that indices are being used properly. 64# 65do_test where-1.1 { 66 count {SELECT x, y FROM t1 WHERE w=10} 67} {3 121 3} 68do_test where-1.1.2 { 69 set sqlite_query_plan 70} {t1 i1w} 71do_test where-1.2 { 72 count {SELECT x, y FROM t1 WHERE w=11} 73} {3 144 3} 74do_test where-1.3 { 75 count {SELECT x, y FROM t1 WHERE 11=w} 76} {3 144 3} 77do_test where-1.4 { 78 count {SELECT x, y FROM t1 WHERE 11=w AND x>2} 79} {3 144 3} 80do_test where-1.4.2 { 81 set sqlite_query_plan 82} {t1 i1w} 83do_test where-1.5 { 84 count {SELECT x, y FROM t1 WHERE y<200 AND w=11 AND x>2} 85} {3 144 3} 86do_test where-1.5.2 { 87 set sqlite_query_plan 88} {t1 i1w} 89do_test where-1.6 { 90 count {SELECT x, y FROM t1 WHERE y<200 AND x>2 AND w=11} 91} {3 144 3} 92do_test where-1.7 { 93 count {SELECT x, y FROM t1 WHERE w=11 AND y<200 AND x>2} 94} {3 144 3} 95do_test where-1.8 { 96 count {SELECT x, y FROM t1 WHERE w>10 AND y=144 AND x=3} 97} {3 144 3} 98do_test where-1.8.2 { 99 set sqlite_query_plan 100} {t1 i1xy} 101do_test where-1.8.3 { 102 count {SELECT x, y FROM t1 WHERE y=144 AND x=3} 103 set sqlite_query_plan 104} {{} i1xy} 105do_test where-1.9 { 106 count {SELECT x, y FROM t1 WHERE y=144 AND w>10 AND x=3} 107} {3 144 3} 108do_test where-1.10 { 109 count {SELECT x, y FROM t1 WHERE x=3 AND w>=10 AND y=121} 110} {3 121 3} 111do_test where-1.11 { 112 count {SELECT x, y FROM t1 WHERE x=3 AND y=100 AND w<10} 113} {3 100 3} 114 115# New for SQLite version 2.1: Verify that that inequality constraints 116# are used correctly. 117# 118do_test where-1.12 { 119 count {SELECT w FROM t1 WHERE x=3 AND y<100} 120} {8 3} 121do_test where-1.13 { 122 count {SELECT w FROM t1 WHERE x=3 AND 100>y} 123} {8 3} 124do_test where-1.14 { 125 count {SELECT w FROM t1 WHERE 3=x AND y<100} 126} {8 3} 127do_test where-1.15 { 128 count {SELECT w FROM t1 WHERE 3=x AND 100>y} 129} {8 3} 130do_test where-1.16 { 131 count {SELECT w FROM t1 WHERE x=3 AND y<=100} 132} {8 9 5} 133do_test where-1.17 { 134 count {SELECT w FROM t1 WHERE x=3 AND 100>=y} 135} {8 9 5} 136do_test where-1.18 { 137 count {SELECT w FROM t1 WHERE x=3 AND y>225} 138} {15 3} 139do_test where-1.19 { 140 count {SELECT w FROM t1 WHERE x=3 AND 225<y} 141} {15 3} 142do_test where-1.20 { 143 count {SELECT w FROM t1 WHERE x=3 AND y>=225} 144} {14 15 5} 145do_test where-1.21 { 146 count {SELECT w FROM t1 WHERE x=3 AND 225<=y} 147} {14 15 5} 148do_test where-1.22 { 149 count {SELECT w FROM t1 WHERE x=3 AND y>121 AND y<196} 150} {11 12 5} 151do_test where-1.23 { 152 count {SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<=196} 153} {10 11 12 13 9} 154do_test where-1.24 { 155 count {SELECT w FROM t1 WHERE x=3 AND 121<y AND 196>y} 156} {11 12 5} 157do_test where-1.25 { 158 count {SELECT w FROM t1 WHERE x=3 AND 121<=y AND 196>=y} 159} {10 11 12 13 9} 160 161# Need to work on optimizing the BETWEEN operator. 162# 163# do_test where-1.26 { 164# count {SELECT w FROM t1 WHERE x=3 AND y BETWEEN 121 AND 196} 165# } {10 11 12 13 9} 166 167do_test where-1.27 { 168 count {SELECT w FROM t1 WHERE x=3 AND y+1==122} 169} {10 17} 170 171do_test where-1.28 { 172 count {SELECT w FROM t1 WHERE x+1=4 AND y+1==122} 173} {10 99} 174do_test where-1.29 { 175 count {SELECT w FROM t1 WHERE y==121} 176} {10 99} 177 178 179do_test where-1.30 { 180 count {SELECT w FROM t1 WHERE w>97} 181} {98 99 100 3} 182do_test where-1.31 { 183 count {SELECT w FROM t1 WHERE w>=97} 184} {97 98 99 100 4} 185do_test where-1.33 { 186 count {SELECT w FROM t1 WHERE w==97} 187} {97 2} 188do_test where-1.33.1 { 189 count {SELECT w FROM t1 WHERE w<=97 AND w==97} 190} {97 2} 191do_test where-1.33.2 { 192 count {SELECT w FROM t1 WHERE w<98 AND w==97} 193} {97 2} 194do_test where-1.33.3 { 195 count {SELECT w FROM t1 WHERE w>=97 AND w==97} 196} {97 2} 197do_test where-1.33.4 { 198 count {SELECT w FROM t1 WHERE w>96 AND w==97} 199} {97 2} 200do_test where-1.33.5 { 201 count {SELECT w FROM t1 WHERE w==97 AND w==97} 202} {97 2} 203do_test where-1.34 { 204 count {SELECT w FROM t1 WHERE w+1==98} 205} {97 99} 206do_test where-1.35 { 207 count {SELECT w FROM t1 WHERE w<3} 208} {1 2 2} 209do_test where-1.36 { 210 count {SELECT w FROM t1 WHERE w<=3} 211} {1 2 3 3} 212do_test where-1.37 { 213 count {SELECT w FROM t1 WHERE w+1<=4 ORDER BY w} 214} {1 2 3 99} 215 216do_test where-1.38 { 217 count {SELECT (w) FROM t1 WHERE (w)>(97)} 218} {98 99 100 3} 219do_test where-1.39 { 220 count {SELECT (w) FROM t1 WHERE (w)>=(97)} 221} {97 98 99 100 4} 222do_test where-1.40 { 223 count {SELECT (w) FROM t1 WHERE (w)==(97)} 224} {97 2} 225do_test where-1.41 { 226 count {SELECT (w) FROM t1 WHERE ((w)+(1))==(98)} 227} {97 99} 228 229 230# Do the same kind of thing except use a join as the data source. 231# 232do_test where-2.1 { 233 count { 234 SELECT w, p FROM t2, t1 235 WHERE x=q AND y=s AND r=8977 236 } 237} {34 67 6} 238do_test where-2.2 { 239 count { 240 SELECT w, p FROM t2, t1 241 WHERE x=q AND s=y AND r=8977 242 } 243} {34 67 6} 244do_test where-2.3 { 245 count { 246 SELECT w, p FROM t2, t1 247 WHERE x=q AND s=y AND r=8977 AND w>10 248 } 249} {34 67 6} 250do_test where-2.4 { 251 count { 252 SELECT w, p FROM t2, t1 253 WHERE p<80 AND x=q AND s=y AND r=8977 AND w>10 254 } 255} {34 67 6} 256do_test where-2.5 { 257 count { 258 SELECT w, p FROM t2, t1 259 WHERE p<80 AND x=q AND 8977=r AND s=y AND w>10 260 } 261} {34 67 6} 262do_test where-2.6 { 263 count { 264 SELECT w, p FROM t2, t1 265 WHERE x=q AND p=77 AND s=y AND w>5 266 } 267} {24 77 6} 268do_test where-2.7 { 269 count { 270 SELECT w, p FROM t1, t2 271 WHERE x=q AND p>77 AND s=y AND w=5 272 } 273} {5 96 6} 274 275# Lets do a 3-way join. 276# 277do_test where-3.1 { 278 count { 279 SELECT A.w, B.p, C.w FROM t1 as A, t2 as B, t1 as C 280 WHERE C.w=101-B.p AND B.r=10202-A.y AND A.w=11 281 } 282} {11 90 11 8} 283do_test where-3.2 { 284 count { 285 SELECT A.w, B.p, C.w FROM t1 as A, t2 as B, t1 as C 286 WHERE C.w=101-B.p AND B.r=10202-A.y AND A.w=12 287 } 288} {12 89 12 8} 289do_test where-3.3 { 290 count { 291 SELECT A.w, B.p, C.w FROM t1 as A, t2 as B, t1 as C 292 WHERE A.w=15 AND B.p=C.w AND B.r=10202-A.y 293 } 294} {15 86 86 8} 295 296# Test to see that the special case of a constant WHERE clause is 297# handled. 298# 299do_test where-4.1 { 300 count { 301 SELECT * FROM t1 WHERE 0 302 } 303} {0} 304do_test where-4.2 { 305 count { 306 SELECT * FROM t1 WHERE 1 LIMIT 1 307 } 308} {1 0 4 1} 309do_test where-4.3 { 310 execsql { 311 SELECT 99 WHERE 0 312 } 313} {} 314do_test where-4.4 { 315 execsql { 316 SELECT 99 WHERE 1 317 } 318} {99} 319do_test where-4.5 { 320 execsql { 321 SELECT 99 WHERE 0.1 322 } 323} {99} 324do_test where-4.6 { 325 execsql { 326 SELECT 99 WHERE 0.0 327 } 328} {} 329 330# Verify that IN operators in a WHERE clause are handled correctly. 331# Omit these tests if the build is not capable of sub-queries. 332# 333ifcapable subquery { 334 do_test where-5.1 { 335 count { 336 SELECT * FROM t1 WHERE rowid IN (1,2,3,1234) order by 1; 337 } 338 } {1 0 4 2 1 9 3 1 16 3} 339 do_test where-5.2 { 340 count { 341 SELECT * FROM t1 WHERE rowid+0 IN (1,2,3,1234) order by 1; 342 } 343 } {1 0 4 2 1 9 3 1 16 199} 344 do_test where-5.3 { 345 count { 346 SELECT * FROM t1 WHERE w IN (-1,1,2,3) order by 1; 347 } 348 } {1 0 4 2 1 9 3 1 16 13} 349 do_test where-5.4 { 350 count { 351 SELECT * FROM t1 WHERE w+0 IN (-1,1,2,3) order by 1; 352 } 353 } {1 0 4 2 1 9 3 1 16 199} 354 do_test where-5.5 { 355 count { 356 SELECT * FROM t1 WHERE rowid IN 357 (select rowid from t1 where rowid IN (-1,2,4)) 358 ORDER BY 1; 359 } 360 } {2 1 9 4 2 25 3} 361 do_test where-5.6 { 362 count { 363 SELECT * FROM t1 WHERE rowid+0 IN 364 (select rowid from t1 where rowid IN (-1,2,4)) 365 ORDER BY 1; 366 } 367 } {2 1 9 4 2 25 201} 368 do_test where-5.7 { 369 count { 370 SELECT * FROM t1 WHERE w IN 371 (select rowid from t1 where rowid IN (-1,2,4)) 372 ORDER BY 1; 373 } 374 } {2 1 9 4 2 25 9} 375 do_test where-5.8 { 376 count { 377 SELECT * FROM t1 WHERE w+0 IN 378 (select rowid from t1 where rowid IN (-1,2,4)) 379 ORDER BY 1; 380 } 381 } {2 1 9 4 2 25 201} 382 do_test where-5.9 { 383 count { 384 SELECT * FROM t1 WHERE x IN (1,7) ORDER BY 1; 385 } 386 } {2 1 9 3 1 16 7} 387 do_test where-5.10 { 388 count { 389 SELECT * FROM t1 WHERE x+0 IN (1,7) ORDER BY 1; 390 } 391 } {2 1 9 3 1 16 199} 392 do_test where-5.11 { 393 count { 394 SELECT * FROM t1 WHERE y IN (6400,8100) ORDER BY 1; 395 } 396 } {79 6 6400 89 6 8100 199} 397 do_test where-5.12 { 398 count { 399 SELECT * FROM t1 WHERE x=6 AND y IN (6400,8100) ORDER BY 1; 400 } 401 } {79 6 6400 89 6 8100 7} 402 do_test where-5.13 { 403 count { 404 SELECT * FROM t1 WHERE x IN (1,7) AND y NOT IN (6400,8100) ORDER BY 1; 405 } 406 } {2 1 9 3 1 16 7} 407 do_test where-5.14 { 408 count { 409 SELECT * FROM t1 WHERE x IN (1,7) AND y IN (9,10) ORDER BY 1; 410 } 411 } {2 1 9 9} 412 do_test where-5.15 { 413 count { 414 SELECT * FROM t1 WHERE x IN (1,7) AND y IN (9,16) ORDER BY 1; 415 } 416 } {2 1 9 3 1 16 11} 417} 418 419# This procedure executes the SQL. Then it checks to see if the OP_Sort 420# opcode was executed. If an OP_Sort did occur, then "sort" is appended 421# to the result. If no OP_Sort happened, then "nosort" is appended. 422# 423# This procedure is used to check to make sure sorting is or is not 424# occurring as expected. 425# 426proc cksort {sql} { 427 set ::sqlite_sort_count 0 428 set data [execsql $sql] 429 if {$::sqlite_sort_count} {set x sort} {set x nosort} 430 lappend data $x 431 return $data 432} 433# Check out the logic that attempts to implement the ORDER BY clause 434# using an index rather than by sorting. 435# 436do_test where-6.1 { 437 execsql { 438 CREATE TABLE t3(a,b,c); 439 CREATE INDEX t3a ON t3(a); 440 CREATE INDEX t3bc ON t3(b,c); 441 CREATE INDEX t3acb ON t3(a,c,b); 442 INSERT INTO t3 SELECT w, 101-w, y FROM t1; 443 SELECT count(*), sum(a), sum(b), sum(c) FROM t3; 444 } 445} {100 5050.0 5050.0 348550.0} 446do_test where-6.2 { 447 cksort { 448 SELECT * FROM t3 ORDER BY a LIMIT 3 449 } 450} {1 100 4 2 99 9 3 98 16 nosort} 451do_test where-6.3 { 452 cksort { 453 SELECT * FROM t3 ORDER BY a+1 LIMIT 3 454 } 455} {1 100 4 2 99 9 3 98 16 sort} 456do_test where-6.4 { 457 cksort { 458 SELECT * FROM t3 WHERE a<10 ORDER BY a LIMIT 3 459 } 460} {1 100 4 2 99 9 3 98 16 nosort} 461do_test where-6.5 { 462 cksort { 463 SELECT * FROM t3 WHERE a>0 AND a<10 ORDER BY a LIMIT 3 464 } 465} {1 100 4 2 99 9 3 98 16 nosort} 466do_test where-6.6 { 467 cksort { 468 SELECT * FROM t3 WHERE a>0 ORDER BY a LIMIT 3 469 } 470} {1 100 4 2 99 9 3 98 16 nosort} 471do_test where-6.7 { 472 cksort { 473 SELECT * FROM t3 WHERE b>0 ORDER BY a LIMIT 3 474 } 475} {1 100 4 2 99 9 3 98 16 nosort} 476ifcapable subquery { 477 do_test where-6.8 { 478 cksort { 479 SELECT * FROM t3 WHERE a IN (3,5,7,1,9,4,2) ORDER BY a LIMIT 3 480 } 481 } {1 100 4 2 99 9 3 98 16 sort} 482} 483do_test where-6.9.1 { 484 cksort { 485 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a LIMIT 3 486 } 487} {1 100 4 nosort} 488do_test where-6.9.1.1 { 489 cksort { 490 SELECT * FROM t3 WHERE a>=1 AND a=1 AND c>0 ORDER BY a LIMIT 3 491 } 492} {1 100 4 nosort} 493do_test where-6.9.1.2 { 494 cksort { 495 SELECT * FROM t3 WHERE a<2 AND a=1 AND c>0 ORDER BY a LIMIT 3 496 } 497} {1 100 4 nosort} 498do_test where-6.9.2 { 499 cksort { 500 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a,c LIMIT 3 501 } 502} {1 100 4 nosort} 503do_test where-6.9.3 { 504 cksort { 505 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY c LIMIT 3 506 } 507} {1 100 4 nosort} 508do_test where-6.9.4 { 509 cksort { 510 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a DESC LIMIT 3 511 } 512} {1 100 4 nosort} 513do_test where-6.9.5 { 514 cksort { 515 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a DESC, c DESC LIMIT 3 516 } 517} {1 100 4 nosort} 518do_test where-6.9.6 { 519 cksort { 520 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY c DESC LIMIT 3 521 } 522} {1 100 4 nosort} 523do_test where-6.9.7 { 524 cksort { 525 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY c,a LIMIT 3 526 } 527} {1 100 4 sort} 528do_test where-6.9.8 { 529 cksort { 530 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a DESC, c ASC LIMIT 3 531 } 532} {1 100 4 nosort} 533do_test where-6.9.9 { 534 cksort { 535 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a ASC, c DESC LIMIT 3 536 } 537} {1 100 4 nosort} 538do_test where-6.10 { 539 cksort { 540 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a LIMIT 3 541 } 542} {1 100 4 nosort} 543do_test where-6.11 { 544 cksort { 545 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a,c LIMIT 3 546 } 547} {1 100 4 nosort} 548do_test where-6.12 { 549 cksort { 550 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a,c,b LIMIT 3 551 } 552} {1 100 4 nosort} 553do_test where-6.13 { 554 cksort { 555 SELECT * FROM t3 WHERE a>0 ORDER BY a DESC LIMIT 3 556 } 557} {100 1 10201 99 2 10000 98 3 9801 nosort} 558do_test where-6.13.1 { 559 cksort { 560 SELECT * FROM t3 WHERE a>0 ORDER BY -a LIMIT 3 561 } 562} {100 1 10201 99 2 10000 98 3 9801 sort} 563do_test where-6.14 { 564 cksort { 565 SELECT * FROM t3 ORDER BY b LIMIT 3 566 } 567} {100 1 10201 99 2 10000 98 3 9801 nosort} 568do_test where-6.15 { 569 cksort { 570 SELECT t3.a, t1.x FROM t3, t1 WHERE t3.a=t1.w ORDER BY t3.a LIMIT 3 571 } 572} {1 0 2 1 3 1 nosort} 573do_test where-6.16 { 574 cksort { 575 SELECT t3.a, t1.x FROM t3, t1 WHERE t3.a=t1.w ORDER BY t1.x, t3.a LIMIT 3 576 } 577} {1 0 2 1 3 1 sort} 578do_test where-6.19 { 579 cksort { 580 SELECT y FROM t1 ORDER BY w LIMIT 3; 581 } 582} {4 9 16 nosort} 583do_test where-6.20 { 584 cksort { 585 SELECT y FROM t1 ORDER BY rowid LIMIT 3; 586 } 587} {4 9 16 nosort} 588do_test where-6.21 { 589 cksort { 590 SELECT y FROM t1 ORDER BY rowid, y LIMIT 3; 591 } 592} {4 9 16 nosort} 593do_test where-6.22 { 594 cksort { 595 SELECT y FROM t1 ORDER BY rowid, y DESC LIMIT 3; 596 } 597} {4 9 16 nosort} 598do_test where-6.23 { 599 cksort { 600 SELECT y FROM t1 WHERE y>4 ORDER BY rowid, w, x LIMIT 3; 601 } 602} {9 16 25 nosort} 603do_test where-6.24 { 604 cksort { 605 SELECT y FROM t1 WHERE y>=9 ORDER BY rowid, x DESC, w LIMIT 3; 606 } 607} {9 16 25 nosort} 608do_test where-6.25 { 609 cksort { 610 SELECT y FROM t1 WHERE y>4 AND y<25 ORDER BY rowid; 611 } 612} {9 16 nosort} 613do_test where-6.26 { 614 cksort { 615 SELECT y FROM t1 WHERE y>=4 AND y<=25 ORDER BY oid; 616 } 617} {4 9 16 25 nosort} 618do_test where-6.27 { 619 cksort { 620 SELECT y FROM t1 WHERE y<=25 ORDER BY _rowid_, w+y; 621 } 622} {4 9 16 25 nosort} 623 624 625# Tests for reverse-order sorting. 626# 627do_test where-7.1 { 628 cksort { 629 SELECT w FROM t1 WHERE x=3 ORDER BY y; 630 } 631} {8 9 10 11 12 13 14 15 nosort} 632do_test where-7.2 { 633 cksort { 634 SELECT w FROM t1 WHERE x=3 ORDER BY y DESC; 635 } 636} {15 14 13 12 11 10 9 8 nosort} 637do_test where-7.3 { 638 cksort { 639 SELECT w FROM t1 WHERE x=3 AND y>100 ORDER BY y LIMIT 3; 640 } 641} {10 11 12 nosort} 642do_test where-7.4 { 643 cksort { 644 SELECT w FROM t1 WHERE x=3 AND y>100 ORDER BY y DESC LIMIT 3; 645 } 646} {15 14 13 nosort} 647do_test where-7.5 { 648 cksort { 649 SELECT w FROM t1 WHERE x=3 AND y>121 ORDER BY y DESC; 650 } 651} {15 14 13 12 11 nosort} 652do_test where-7.6 { 653 cksort { 654 SELECT w FROM t1 WHERE x=3 AND y>=121 ORDER BY y DESC; 655 } 656} {15 14 13 12 11 10 nosort} 657do_test where-7.7 { 658 cksort { 659 SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<196 ORDER BY y DESC; 660 } 661} {12 11 10 nosort} 662do_test where-7.8 { 663 cksort { 664 SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<=196 ORDER BY y DESC; 665 } 666} {13 12 11 10 nosort} 667do_test where-7.9 { 668 cksort { 669 SELECT w FROM t1 WHERE x=3 AND y>121 AND y<=196 ORDER BY y DESC; 670 } 671} {13 12 11 nosort} 672do_test where-7.10 { 673 cksort { 674 SELECT w FROM t1 WHERE x=3 AND y>100 AND y<196 ORDER BY y DESC; 675 } 676} {12 11 10 nosort} 677do_test where-7.11 { 678 cksort { 679 SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<196 ORDER BY y; 680 } 681} {10 11 12 nosort} 682do_test where-7.12 { 683 cksort { 684 SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<=196 ORDER BY y; 685 } 686} {10 11 12 13 nosort} 687do_test where-7.13 { 688 cksort { 689 SELECT w FROM t1 WHERE x=3 AND y>121 AND y<=196 ORDER BY y; 690 } 691} {11 12 13 nosort} 692do_test where-7.14 { 693 cksort { 694 SELECT w FROM t1 WHERE x=3 AND y>100 AND y<196 ORDER BY y; 695 } 696} {10 11 12 nosort} 697do_test where-7.15 { 698 cksort { 699 SELECT w FROM t1 WHERE x=3 AND y<81 ORDER BY y; 700 } 701} {nosort} 702do_test where-7.16 { 703 cksort { 704 SELECT w FROM t1 WHERE x=3 AND y<=81 ORDER BY y; 705 } 706} {8 nosort} 707do_test where-7.17 { 708 cksort { 709 SELECT w FROM t1 WHERE x=3 AND y>256 ORDER BY y; 710 } 711} {nosort} 712do_test where-7.18 { 713 cksort { 714 SELECT w FROM t1 WHERE x=3 AND y>=256 ORDER BY y; 715 } 716} {15 nosort} 717do_test where-7.19 { 718 cksort { 719 SELECT w FROM t1 WHERE x=3 AND y<81 ORDER BY y DESC; 720 } 721} {nosort} 722do_test where-7.20 { 723 cksort { 724 SELECT w FROM t1 WHERE x=3 AND y<=81 ORDER BY y DESC; 725 } 726} {8 nosort} 727do_test where-7.21 { 728 cksort { 729 SELECT w FROM t1 WHERE x=3 AND y>256 ORDER BY y DESC; 730 } 731} {nosort} 732do_test where-7.22 { 733 cksort { 734 SELECT w FROM t1 WHERE x=3 AND y>=256 ORDER BY y DESC; 735 } 736} {15 nosort} 737do_test where-7.23 { 738 cksort { 739 SELECT w FROM t1 WHERE x=0 AND y<4 ORDER BY y; 740 } 741} {nosort} 742do_test where-7.24 { 743 cksort { 744 SELECT w FROM t1 WHERE x=0 AND y<=4 ORDER BY y; 745 } 746} {1 nosort} 747do_test where-7.25 { 748 cksort { 749 SELECT w FROM t1 WHERE x=6 AND y>10201 ORDER BY y; 750 } 751} {nosort} 752do_test where-7.26 { 753 cksort { 754 SELECT w FROM t1 WHERE x=6 AND y>=10201 ORDER BY y; 755 } 756} {100 nosort} 757do_test where-7.27 { 758 cksort { 759 SELECT w FROM t1 WHERE x=0 AND y<4 ORDER BY y DESC; 760 } 761} {nosort} 762do_test where-7.28 { 763 cksort { 764 SELECT w FROM t1 WHERE x=0 AND y<=4 ORDER BY y DESC; 765 } 766} {1 nosort} 767do_test where-7.29 { 768 cksort { 769 SELECT w FROM t1 WHERE x=6 AND y>10201 ORDER BY y DESC; 770 } 771} {nosort} 772do_test where-7.30 { 773 cksort { 774 SELECT w FROM t1 WHERE x=6 AND y>=10201 ORDER BY y DESC; 775 } 776} {100 nosort} 777do_test where-7.31 { 778 cksort { 779 SELECT y FROM t1 ORDER BY rowid DESC LIMIT 3 780 } 781} {10201 10000 9801 nosort} 782do_test where-7.32 { 783 cksort { 784 SELECT y FROM t1 WHERE y<25 ORDER BY rowid DESC, x 785 } 786} {16 9 4 nosort} 787do_test where-7.33 { 788 cksort { 789 SELECT y FROM t1 WHERE y<=25 ORDER BY rowid DESC, x 790 } 791} {25 16 9 4 nosort} 792do_test where-7.34 { 793 cksort { 794 SELECT y FROM t1 WHERE y<25 AND y>4 ORDER BY rowid DESC, y DESC 795 } 796} {16 9 nosort} 797do_test where-7.35 { 798 cksort { 799 SELECT y FROM t1 WHERE y<25 AND y>=4 ORDER BY rowid DESC 800 } 801} {16 9 4 nosort} 802 803do_test where-8.1 { 804 execsql { 805 CREATE TABLE t4 AS SELECT * FROM t1; 806 CREATE INDEX i4xy ON t4(x,y); 807 } 808 cksort { 809 SELECT w FROM t4 WHERE x=4 and y<1000 ORDER BY y DESC limit 3; 810 } 811} {30 29 28 nosort} 812do_test where-8.2 { 813 execsql { 814 DELETE FROM t4; 815 } 816 cksort { 817 SELECT w FROM t4 WHERE x=4 and y<1000 ORDER BY y DESC limit 3; 818 } 819} {nosort} 820 821# Make sure searches with an index work with an empty table. 822# 823do_test where-9.1 { 824 execsql { 825 CREATE TABLE t5(x PRIMARY KEY); 826 SELECT * FROM t5 WHERE x<10; 827 } 828} {} 829do_test where-9.2 { 830 execsql { 831 SELECT * FROM t5 WHERE x<10 ORDER BY x DESC; 832 } 833} {} 834do_test where-9.3 { 835 execsql { 836 SELECT * FROM t5 WHERE x=10; 837 } 838} {} 839 840do_test where-10.1 { 841 execsql { 842 SELECT 1 WHERE abs(random())<0 843 } 844} {} 845do_test where-10.2 { 846 proc tclvar_func {vname} {return [set ::$vname]} 847 db function tclvar tclvar_func 848 set ::v1 0 849 execsql { 850 SELECT count(*) FROM t1 WHERE tclvar('v1'); 851 } 852} {0} 853do_test where-10.3 { 854 set ::v1 1 855 execsql { 856 SELECT count(*) FROM t1 WHERE tclvar('v1'); 857 } 858} {100} 859do_test where-10.4 { 860 set ::v1 1 861 proc tclvar_func {vname} { 862 upvar #0 $vname v 863 set v [expr {!$v}] 864 return $v 865 } 866 execsql { 867 SELECT count(*) FROM t1 WHERE tclvar('v1'); 868 } 869} {50} 870 871integrity_check {where-99.0} 872 873finish_test 874