PageRenderTime 35ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 1ms

/test/where.test

https://bitbucket.org/aware/sqlite
Unknown | 1262 lines | 1229 code | 33 blank | 0 comment | 0 complexity | 517257fe200969badc8a1a50d7468a4c MD5 | raw file
  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:$
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. # Build some test data
  18. #
  19. do_test where-1.0 {
  20. execsql {
  21. CREATE TABLE t1(w int, x int, y int);
  22. CREATE TABLE t2(p int, q int, r int, s int);
  23. }
  24. for {set i 1} {$i<=100} {incr i} {
  25. set w $i
  26. set x [expr {int(log($i)/log(2))}]
  27. set y [expr {$i*$i + 2*$i + 1}]
  28. execsql "INSERT INTO t1 VALUES($w,$x,$y)"
  29. }
  30. ifcapable subquery {
  31. execsql {
  32. INSERT INTO t2 SELECT 101-w, x, (SELECT max(y) FROM t1)+1-y, y FROM t1;
  33. }
  34. } else {
  35. set maxy [execsql {select max(y) from t1}]
  36. execsql "
  37. INSERT INTO t2 SELECT 101-w, x, $maxy+1-y, y FROM t1;
  38. "
  39. }
  40. execsql {
  41. CREATE INDEX i1w ON t1(w);
  42. CREATE INDEX i1xy ON t1(x,y);
  43. CREATE INDEX i2p ON t2(p);
  44. CREATE INDEX i2r ON t2(r);
  45. CREATE INDEX i2qs ON t2(q, s);
  46. }
  47. } {}
  48. # Do an SQL statement. Append the search count to the end of the result.
  49. #
  50. proc count sql {
  51. set ::sqlite_search_count 0
  52. return [concat [execsql $sql] $::sqlite_search_count]
  53. }
  54. # Verify that queries use an index. We are using the special variable
  55. # "sqlite_search_count" which tallys the number of executions of MoveTo
  56. # and Next operators in the VDBE. By verifing that the search count is
  57. # small we can be assured that indices are being used properly.
  58. #
  59. do_test where-1.1.1 {
  60. count {SELECT x, y, w FROM t1 WHERE w=10}
  61. } {3 121 10 3}
  62. do_test where-1.1.2 {
  63. set sqlite_query_plan
  64. } {t1 i1w}
  65. do_test where-1.1.3 {
  66. db status step
  67. } {0}
  68. do_test where-1.1.4 {
  69. db eval {SELECT x, y, w FROM t1 WHERE +w=10}
  70. } {3 121 10}
  71. do_test where-1.1.5 {
  72. db status step
  73. } {99}
  74. do_test where-1.1.6 {
  75. set sqlite_query_plan
  76. } {t1 {}}
  77. do_test where-1.1.7 {
  78. count {SELECT x, y, w AS abc FROM t1 WHERE abc=10}
  79. } {3 121 10 3}
  80. do_test where-1.1.8 {
  81. set sqlite_query_plan
  82. } {t1 i1w}
  83. do_test where-1.1.9 {
  84. db status step
  85. } {0}
  86. do_test where-1.2.1 {
  87. count {SELECT x, y, w FROM t1 WHERE w=11}
  88. } {3 144 11 3}
  89. do_test where-1.2.2 {
  90. count {SELECT x, y, w AS abc FROM t1 WHERE abc=11}
  91. } {3 144 11 3}
  92. do_test where-1.3.1 {
  93. count {SELECT x, y, w AS abc FROM t1 WHERE 11=w}
  94. } {3 144 11 3}
  95. do_test where-1.3.2 {
  96. count {SELECT x, y, w AS abc FROM t1 WHERE 11=abc}
  97. } {3 144 11 3}
  98. do_test where-1.4.1 {
  99. count {SELECT w, x, y FROM t1 WHERE 11=w AND x>2}
  100. } {11 3 144 3}
  101. do_test where-1.4.2 {
  102. set sqlite_query_plan
  103. } {t1 i1w}
  104. do_test where-1.4.3 {
  105. count {SELECT w AS a, x AS b, y FROM t1 WHERE 11=a AND b>2}
  106. } {11 3 144 3}
  107. do_test where-1.4.4 {
  108. set sqlite_query_plan
  109. } {t1 i1w}
  110. do_test where-1.5 {
  111. count {SELECT x, y FROM t1 WHERE y<200 AND w=11 AND x>2}
  112. } {3 144 3}
  113. do_test where-1.5.2 {
  114. set sqlite_query_plan
  115. } {t1 i1w}
  116. do_test where-1.6 {
  117. count {SELECT x, y FROM t1 WHERE y<200 AND x>2 AND w=11}
  118. } {3 144 3}
  119. do_test where-1.7 {
  120. count {SELECT x, y FROM t1 WHERE w=11 AND y<200 AND x>2}
  121. } {3 144 3}
  122. do_test where-1.8 {
  123. count {SELECT x, y FROM t1 WHERE w>10 AND y=144 AND x=3}
  124. } {3 144 3}
  125. do_test where-1.8.2 {
  126. set sqlite_query_plan
  127. } {t1 i1xy}
  128. do_test where-1.8.3 {
  129. count {SELECT x, y FROM t1 WHERE y=144 AND x=3}
  130. set sqlite_query_plan
  131. } {{} i1xy}
  132. do_test where-1.9 {
  133. count {SELECT x, y FROM t1 WHERE y=144 AND w>10 AND x=3}
  134. } {3 144 3}
  135. do_test where-1.10 {
  136. count {SELECT x, y FROM t1 WHERE x=3 AND w>=10 AND y=121}
  137. } {3 121 3}
  138. do_test where-1.11 {
  139. count {SELECT x, y FROM t1 WHERE x=3 AND y=100 AND w<10}
  140. } {3 100 3}
  141. # New for SQLite version 2.1: Verify that that inequality constraints
  142. # are used correctly.
  143. #
  144. do_test where-1.12 {
  145. count {SELECT w FROM t1 WHERE x=3 AND y<100}
  146. } {8 3}
  147. do_test where-1.13 {
  148. count {SELECT w FROM t1 WHERE x=3 AND 100>y}
  149. } {8 3}
  150. do_test where-1.14 {
  151. count {SELECT w FROM t1 WHERE 3=x AND y<100}
  152. } {8 3}
  153. do_test where-1.15 {
  154. count {SELECT w FROM t1 WHERE 3=x AND 100>y}
  155. } {8 3}
  156. do_test where-1.16 {
  157. count {SELECT w FROM t1 WHERE x=3 AND y<=100}
  158. } {8 9 5}
  159. do_test where-1.17 {
  160. count {SELECT w FROM t1 WHERE x=3 AND 100>=y}
  161. } {8 9 5}
  162. do_test where-1.18 {
  163. count {SELECT w FROM t1 WHERE x=3 AND y>225}
  164. } {15 3}
  165. do_test where-1.19 {
  166. count {SELECT w FROM t1 WHERE x=3 AND 225<y}
  167. } {15 3}
  168. do_test where-1.20 {
  169. count {SELECT w FROM t1 WHERE x=3 AND y>=225}
  170. } {14 15 5}
  171. do_test where-1.21 {
  172. count {SELECT w FROM t1 WHERE x=3 AND 225<=y}
  173. } {14 15 5}
  174. do_test where-1.22 {
  175. count {SELECT w FROM t1 WHERE x=3 AND y>121 AND y<196}
  176. } {11 12 5}
  177. do_test where-1.23 {
  178. count {SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<=196}
  179. } {10 11 12 13 9}
  180. do_test where-1.24 {
  181. count {SELECT w FROM t1 WHERE x=3 AND 121<y AND 196>y}
  182. } {11 12 5}
  183. do_test where-1.25 {
  184. count {SELECT w FROM t1 WHERE x=3 AND 121<=y AND 196>=y}
  185. } {10 11 12 13 9}
  186. # Need to work on optimizing the BETWEEN operator.
  187. #
  188. # do_test where-1.26 {
  189. # count {SELECT w FROM t1 WHERE x=3 AND y BETWEEN 121 AND 196}
  190. # } {10 11 12 13 9}
  191. do_test where-1.27 {
  192. count {SELECT w FROM t1 WHERE x=3 AND y+1==122}
  193. } {10 10}
  194. do_test where-1.28 {
  195. count {SELECT w FROM t1 WHERE x+1=4 AND y+1==122}
  196. } {10 99}
  197. do_test where-1.29 {
  198. count {SELECT w FROM t1 WHERE y==121}
  199. } {10 99}
  200. do_test where-1.30 {
  201. count {SELECT w FROM t1 WHERE w>97}
  202. } {98 99 100 3}
  203. do_test where-1.31 {
  204. count {SELECT w FROM t1 WHERE w>=97}
  205. } {97 98 99 100 4}
  206. do_test where-1.33 {
  207. count {SELECT w FROM t1 WHERE w==97}
  208. } {97 2}
  209. do_test where-1.33.1 {
  210. count {SELECT w FROM t1 WHERE w<=97 AND w==97}
  211. } {97 2}
  212. do_test where-1.33.2 {
  213. count {SELECT w FROM t1 WHERE w<98 AND w==97}
  214. } {97 2}
  215. do_test where-1.33.3 {
  216. count {SELECT w FROM t1 WHERE w>=97 AND w==97}
  217. } {97 2}
  218. do_test where-1.33.4 {
  219. count {SELECT w FROM t1 WHERE w>96 AND w==97}
  220. } {97 2}
  221. do_test where-1.33.5 {
  222. count {SELECT w FROM t1 WHERE w==97 AND w==97}
  223. } {97 2}
  224. do_test where-1.34 {
  225. count {SELECT w FROM t1 WHERE w+1==98}
  226. } {97 99}
  227. do_test where-1.35 {
  228. count {SELECT w FROM t1 WHERE w<3}
  229. } {1 2 2}
  230. do_test where-1.36 {
  231. count {SELECT w FROM t1 WHERE w<=3}
  232. } {1 2 3 3}
  233. do_test where-1.37 {
  234. count {SELECT w FROM t1 WHERE w+1<=4 ORDER BY w}
  235. } {1 2 3 99}
  236. do_test where-1.38 {
  237. count {SELECT (w) FROM t1 WHERE (w)>(97)}
  238. } {98 99 100 3}
  239. do_test where-1.39 {
  240. count {SELECT (w) FROM t1 WHERE (w)>=(97)}
  241. } {97 98 99 100 4}
  242. do_test where-1.40 {
  243. count {SELECT (w) FROM t1 WHERE (w)==(97)}
  244. } {97 2}
  245. do_test where-1.41 {
  246. count {SELECT (w) FROM t1 WHERE ((w)+(1))==(98)}
  247. } {97 99}
  248. # Do the same kind of thing except use a join as the data source.
  249. #
  250. do_test where-2.1 {
  251. count {
  252. SELECT w, p FROM t2, t1
  253. WHERE x=q AND y=s AND r=8977
  254. }
  255. } {34 67 6}
  256. do_test where-2.2 {
  257. count {
  258. SELECT w, p FROM t2, t1
  259. WHERE x=q AND s=y AND r=8977
  260. }
  261. } {34 67 6}
  262. do_test where-2.3 {
  263. count {
  264. SELECT w, p FROM t2, t1
  265. WHERE x=q AND s=y AND r=8977 AND w>10
  266. }
  267. } {34 67 6}
  268. do_test where-2.4 {
  269. count {
  270. SELECT w, p FROM t2, t1
  271. WHERE p<80 AND x=q AND s=y AND r=8977 AND w>10
  272. }
  273. } {34 67 6}
  274. do_test where-2.5 {
  275. count {
  276. SELECT w, p FROM t2, t1
  277. WHERE p<80 AND x=q AND 8977=r AND s=y AND w>10
  278. }
  279. } {34 67 6}
  280. do_test where-2.6 {
  281. count {
  282. SELECT w, p FROM t2, t1
  283. WHERE x=q AND p=77 AND s=y AND w>5
  284. }
  285. } {24 77 6}
  286. do_test where-2.7 {
  287. count {
  288. SELECT w, p FROM t1, t2
  289. WHERE x=q AND p>77 AND s=y AND w=5
  290. }
  291. } {5 96 6}
  292. # Lets do a 3-way join.
  293. #
  294. do_test where-3.1 {
  295. count {
  296. SELECT A.w, B.p, C.w FROM t1 as A, t2 as B, t1 as C
  297. WHERE C.w=101-B.p AND B.r=10202-A.y AND A.w=11
  298. }
  299. } {11 90 11 8}
  300. do_test where-3.2 {
  301. count {
  302. SELECT A.w, B.p, C.w FROM t1 as A, t2 as B, t1 as C
  303. WHERE C.w=101-B.p AND B.r=10202-A.y AND A.w=12
  304. }
  305. } {12 89 12 8}
  306. do_test where-3.3 {
  307. count {
  308. SELECT A.w, B.p, C.w FROM t1 as A, t2 as B, t1 as C
  309. WHERE A.w=15 AND B.p=C.w AND B.r=10202-A.y
  310. }
  311. } {15 86 86 8}
  312. # Test to see that the special case of a constant WHERE clause is
  313. # handled.
  314. #
  315. do_test where-4.1 {
  316. count {
  317. SELECT * FROM t1 WHERE 0
  318. }
  319. } {0}
  320. do_test where-4.2 {
  321. count {
  322. SELECT * FROM t1 WHERE 1 LIMIT 1
  323. }
  324. } {1 0 4 0}
  325. do_test where-4.3 {
  326. execsql {
  327. SELECT 99 WHERE 0
  328. }
  329. } {}
  330. do_test where-4.4 {
  331. execsql {
  332. SELECT 99 WHERE 1
  333. }
  334. } {99}
  335. do_test where-4.5 {
  336. execsql {
  337. SELECT 99 WHERE 0.1
  338. }
  339. } {99}
  340. do_test where-4.6 {
  341. execsql {
  342. SELECT 99 WHERE 0.0
  343. }
  344. } {}
  345. do_test where-4.7 {
  346. execsql {
  347. SELECT count(*) FROM t1 WHERE t1.w
  348. }
  349. } {100}
  350. # Verify that IN operators in a WHERE clause are handled correctly.
  351. # Omit these tests if the build is not capable of sub-queries.
  352. #
  353. ifcapable subquery {
  354. do_test where-5.1 {
  355. count {
  356. SELECT * FROM t1 WHERE rowid IN (1,2,3,1234) order by 1;
  357. }
  358. } {1 0 4 2 1 9 3 1 16 4}
  359. do_test where-5.2 {
  360. count {
  361. SELECT * FROM t1 WHERE rowid+0 IN (1,2,3,1234) order by 1;
  362. }
  363. } {1 0 4 2 1 9 3 1 16 102}
  364. do_test where-5.3 {
  365. count {
  366. SELECT * FROM t1 WHERE w IN (-1,1,2,3) order by 1;
  367. }
  368. } {1 0 4 2 1 9 3 1 16 14}
  369. do_test where-5.4 {
  370. count {
  371. SELECT * FROM t1 WHERE w+0 IN (-1,1,2,3) order by 1;
  372. }
  373. } {1 0 4 2 1 9 3 1 16 102}
  374. do_test where-5.5 {
  375. count {
  376. SELECT * FROM t1 WHERE rowid IN
  377. (select rowid from t1 where rowid IN (-1,2,4))
  378. ORDER BY 1;
  379. }
  380. } {2 1 9 4 2 25 3}
  381. do_test where-5.6 {
  382. count {
  383. SELECT * FROM t1 WHERE rowid+0 IN
  384. (select rowid from t1 where rowid IN (-1,2,4))
  385. ORDER BY 1;
  386. }
  387. } {2 1 9 4 2 25 103}
  388. do_test where-5.7 {
  389. count {
  390. SELECT * FROM t1 WHERE w IN
  391. (select rowid from t1 where rowid IN (-1,2,4))
  392. ORDER BY 1;
  393. }
  394. } {2 1 9 4 2 25 9}
  395. do_test where-5.8 {
  396. count {
  397. SELECT * FROM t1 WHERE w+0 IN
  398. (select rowid from t1 where rowid IN (-1,2,4))
  399. ORDER BY 1;
  400. }
  401. } {2 1 9 4 2 25 103}
  402. do_test where-5.9 {
  403. count {
  404. SELECT * FROM t1 WHERE x IN (1,7) ORDER BY 1;
  405. }
  406. } {2 1 9 3 1 16 7}
  407. do_test where-5.10 {
  408. count {
  409. SELECT * FROM t1 WHERE x+0 IN (1,7) ORDER BY 1;
  410. }
  411. } {2 1 9 3 1 16 199}
  412. do_test where-5.11 {
  413. count {
  414. SELECT * FROM t1 WHERE y IN (6400,8100) ORDER BY 1;
  415. }
  416. } {79 6 6400 89 6 8100 199}
  417. do_test where-5.12 {
  418. count {
  419. SELECT * FROM t1 WHERE x=6 AND y IN (6400,8100) ORDER BY 1;
  420. }
  421. } {79 6 6400 89 6 8100 7}
  422. do_test where-5.13 {
  423. count {
  424. SELECT * FROM t1 WHERE x IN (1,7) AND y NOT IN (6400,8100) ORDER BY 1;
  425. }
  426. } {2 1 9 3 1 16 7}
  427. do_test where-5.14 {
  428. count {
  429. SELECT * FROM t1 WHERE x IN (1,7) AND y IN (9,10) ORDER BY 1;
  430. }
  431. } {2 1 9 8}
  432. do_test where-5.15 {
  433. count {
  434. SELECT * FROM t1 WHERE x IN (1,7) AND y IN (9,16) ORDER BY 1;
  435. }
  436. } {2 1 9 3 1 16 11}
  437. }
  438. # This procedure executes the SQL. Then it checks to see if the OP_Sort
  439. # opcode was executed. If an OP_Sort did occur, then "sort" is appended
  440. # to the result. If no OP_Sort happened, then "nosort" is appended.
  441. #
  442. # This procedure is used to check to make sure sorting is or is not
  443. # occurring as expected.
  444. #
  445. proc cksort {sql} {
  446. set data [execsql $sql]
  447. if {[db status sort]} {set x sort} {set x nosort}
  448. lappend data $x
  449. return $data
  450. }
  451. # Check out the logic that attempts to implement the ORDER BY clause
  452. # using an index rather than by sorting.
  453. #
  454. do_test where-6.1 {
  455. execsql {
  456. CREATE TABLE t3(a,b,c);
  457. CREATE INDEX t3a ON t3(a);
  458. CREATE INDEX t3bc ON t3(b,c);
  459. CREATE INDEX t3acb ON t3(a,c,b);
  460. INSERT INTO t3 SELECT w, 101-w, y FROM t1;
  461. SELECT count(*), sum(a), sum(b), sum(c) FROM t3;
  462. }
  463. } {100 5050 5050 348550}
  464. do_test where-6.2 {
  465. cksort {
  466. SELECT * FROM t3 ORDER BY a LIMIT 3
  467. }
  468. } {1 100 4 2 99 9 3 98 16 nosort}
  469. do_test where-6.3 {
  470. cksort {
  471. SELECT * FROM t3 ORDER BY a+1 LIMIT 3
  472. }
  473. } {1 100 4 2 99 9 3 98 16 sort}
  474. do_test where-6.4 {
  475. cksort {
  476. SELECT * FROM t3 WHERE a<10 ORDER BY a LIMIT 3
  477. }
  478. } {1 100 4 2 99 9 3 98 16 nosort}
  479. do_test where-6.5 {
  480. cksort {
  481. SELECT * FROM t3 WHERE a>0 AND a<10 ORDER BY a LIMIT 3
  482. }
  483. } {1 100 4 2 99 9 3 98 16 nosort}
  484. do_test where-6.6 {
  485. cksort {
  486. SELECT * FROM t3 WHERE a>0 ORDER BY a LIMIT 3
  487. }
  488. } {1 100 4 2 99 9 3 98 16 nosort}
  489. do_test where-6.7 {
  490. cksort {
  491. SELECT * FROM t3 WHERE b>0 ORDER BY a LIMIT 3
  492. }
  493. } {1 100 4 2 99 9 3 98 16 nosort}
  494. ifcapable subquery {
  495. do_test where-6.8 {
  496. cksort {
  497. SELECT * FROM t3 WHERE a IN (3,5,7,1,9,4,2) ORDER BY a LIMIT 3
  498. }
  499. } {1 100 4 2 99 9 3 98 16 sort}
  500. }
  501. do_test where-6.9.1 {
  502. cksort {
  503. SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a LIMIT 3
  504. }
  505. } {1 100 4 nosort}
  506. do_test where-6.9.1.1 {
  507. cksort {
  508. SELECT * FROM t3 WHERE a>=1 AND a=1 AND c>0 ORDER BY a LIMIT 3
  509. }
  510. } {1 100 4 nosort}
  511. do_test where-6.9.1.2 {
  512. cksort {
  513. SELECT * FROM t3 WHERE a<2 AND a=1 AND c>0 ORDER BY a LIMIT 3
  514. }
  515. } {1 100 4 nosort}
  516. do_test where-6.9.2 {
  517. cksort {
  518. SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a,c LIMIT 3
  519. }
  520. } {1 100 4 nosort}
  521. do_test where-6.9.3 {
  522. cksort {
  523. SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY c LIMIT 3
  524. }
  525. } {1 100 4 nosort}
  526. do_test where-6.9.4 {
  527. cksort {
  528. SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a DESC LIMIT 3
  529. }
  530. } {1 100 4 nosort}
  531. do_test where-6.9.5 {
  532. cksort {
  533. SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a DESC, c DESC LIMIT 3
  534. }
  535. } {1 100 4 nosort}
  536. do_test where-6.9.6 {
  537. cksort {
  538. SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY c DESC LIMIT 3
  539. }
  540. } {1 100 4 nosort}
  541. do_test where-6.9.7 {
  542. cksort {
  543. SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY c,a LIMIT 3
  544. }
  545. } {1 100 4 sort}
  546. do_test where-6.9.8 {
  547. cksort {
  548. SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a DESC, c ASC LIMIT 3
  549. }
  550. } {1 100 4 nosort}
  551. do_test where-6.9.9 {
  552. cksort {
  553. SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a ASC, c DESC LIMIT 3
  554. }
  555. } {1 100 4 nosort}
  556. do_test where-6.10 {
  557. cksort {
  558. SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a LIMIT 3
  559. }
  560. } {1 100 4 nosort}
  561. do_test where-6.11 {
  562. cksort {
  563. SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a,c LIMIT 3
  564. }
  565. } {1 100 4 nosort}
  566. do_test where-6.12 {
  567. cksort {
  568. SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a,c,b LIMIT 3
  569. }
  570. } {1 100 4 nosort}
  571. do_test where-6.13 {
  572. cksort {
  573. SELECT * FROM t3 WHERE a>0 ORDER BY a DESC LIMIT 3
  574. }
  575. } {100 1 10201 99 2 10000 98 3 9801 nosort}
  576. do_test where-6.13.1 {
  577. cksort {
  578. SELECT * FROM t3 WHERE a>0 ORDER BY -a LIMIT 3
  579. }
  580. } {100 1 10201 99 2 10000 98 3 9801 sort}
  581. do_test where-6.14 {
  582. cksort {
  583. SELECT * FROM t3 ORDER BY b LIMIT 3
  584. }
  585. } {100 1 10201 99 2 10000 98 3 9801 nosort}
  586. do_test where-6.15 {
  587. cksort {
  588. SELECT t3.a, t1.x FROM t3, t1 WHERE t3.a=t1.w ORDER BY t3.a LIMIT 3
  589. }
  590. } {1 0 2 1 3 1 nosort}
  591. do_test where-6.16 {
  592. cksort {
  593. SELECT t3.a, t1.x FROM t3, t1 WHERE t3.a=t1.w ORDER BY t1.x, t3.a LIMIT 3
  594. }
  595. } {1 0 2 1 3 1 sort}
  596. do_test where-6.19 {
  597. cksort {
  598. SELECT y FROM t1 ORDER BY w LIMIT 3;
  599. }
  600. } {4 9 16 nosort}
  601. do_test where-6.20 {
  602. cksort {
  603. SELECT y FROM t1 ORDER BY rowid LIMIT 3;
  604. }
  605. } {4 9 16 nosort}
  606. do_test where-6.21 {
  607. cksort {
  608. SELECT y FROM t1 ORDER BY rowid, y LIMIT 3;
  609. }
  610. } {4 9 16 nosort}
  611. do_test where-6.22 {
  612. cksort {
  613. SELECT y FROM t1 ORDER BY rowid, y DESC LIMIT 3;
  614. }
  615. } {4 9 16 nosort}
  616. do_test where-6.23 {
  617. cksort {
  618. SELECT y FROM t1 WHERE y>4 ORDER BY rowid, w, x LIMIT 3;
  619. }
  620. } {9 16 25 nosort}
  621. do_test where-6.24 {
  622. cksort {
  623. SELECT y FROM t1 WHERE y>=9 ORDER BY rowid, x DESC, w LIMIT 3;
  624. }
  625. } {9 16 25 nosort}
  626. do_test where-6.25 {
  627. cksort {
  628. SELECT y FROM t1 WHERE y>4 AND y<25 ORDER BY rowid;
  629. }
  630. } {9 16 nosort}
  631. do_test where-6.26 {
  632. cksort {
  633. SELECT y FROM t1 WHERE y>=4 AND y<=25 ORDER BY oid;
  634. }
  635. } {4 9 16 25 nosort}
  636. do_test where-6.27 {
  637. cksort {
  638. SELECT y FROM t1 WHERE y<=25 ORDER BY _rowid_, w+y;
  639. }
  640. } {4 9 16 25 nosort}
  641. # Tests for reverse-order sorting.
  642. #
  643. do_test where-7.1 {
  644. cksort {
  645. SELECT w FROM t1 WHERE x=3 ORDER BY y;
  646. }
  647. } {8 9 10 11 12 13 14 15 nosort}
  648. do_test where-7.2 {
  649. cksort {
  650. SELECT w FROM t1 WHERE x=3 ORDER BY y DESC;
  651. }
  652. } {15 14 13 12 11 10 9 8 nosort}
  653. do_test where-7.3 {
  654. cksort {
  655. SELECT w FROM t1 WHERE x=3 AND y>100 ORDER BY y LIMIT 3;
  656. }
  657. } {10 11 12 nosort}
  658. do_test where-7.4 {
  659. cksort {
  660. SELECT w FROM t1 WHERE x=3 AND y>100 ORDER BY y DESC LIMIT 3;
  661. }
  662. } {15 14 13 nosort}
  663. do_test where-7.5 {
  664. cksort {
  665. SELECT w FROM t1 WHERE x=3 AND y>121 ORDER BY y DESC;
  666. }
  667. } {15 14 13 12 11 nosort}
  668. do_test where-7.6 {
  669. cksort {
  670. SELECT w FROM t1 WHERE x=3 AND y>=121 ORDER BY y DESC;
  671. }
  672. } {15 14 13 12 11 10 nosort}
  673. do_test where-7.7 {
  674. cksort {
  675. SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<196 ORDER BY y DESC;
  676. }
  677. } {12 11 10 nosort}
  678. do_test where-7.8 {
  679. cksort {
  680. SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<=196 ORDER BY y DESC;
  681. }
  682. } {13 12 11 10 nosort}
  683. do_test where-7.9 {
  684. cksort {
  685. SELECT w FROM t1 WHERE x=3 AND y>121 AND y<=196 ORDER BY y DESC;
  686. }
  687. } {13 12 11 nosort}
  688. do_test where-7.10 {
  689. cksort {
  690. SELECT w FROM t1 WHERE x=3 AND y>100 AND y<196 ORDER BY y DESC;
  691. }
  692. } {12 11 10 nosort}
  693. do_test where-7.11 {
  694. cksort {
  695. SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<196 ORDER BY y;
  696. }
  697. } {10 11 12 nosort}
  698. do_test where-7.12 {
  699. cksort {
  700. SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<=196 ORDER BY y;
  701. }
  702. } {10 11 12 13 nosort}
  703. do_test where-7.13 {
  704. cksort {
  705. SELECT w FROM t1 WHERE x=3 AND y>121 AND y<=196 ORDER BY y;
  706. }
  707. } {11 12 13 nosort}
  708. do_test where-7.14 {
  709. cksort {
  710. SELECT w FROM t1 WHERE x=3 AND y>100 AND y<196 ORDER BY y;
  711. }
  712. } {10 11 12 nosort}
  713. do_test where-7.15 {
  714. cksort {
  715. SELECT w FROM t1 WHERE x=3 AND y<81 ORDER BY y;
  716. }
  717. } {nosort}
  718. do_test where-7.16 {
  719. cksort {
  720. SELECT w FROM t1 WHERE x=3 AND y<=81 ORDER BY y;
  721. }
  722. } {8 nosort}
  723. do_test where-7.17 {
  724. cksort {
  725. SELECT w FROM t1 WHERE x=3 AND y>256 ORDER BY y;
  726. }
  727. } {nosort}
  728. do_test where-7.18 {
  729. cksort {
  730. SELECT w FROM t1 WHERE x=3 AND y>=256 ORDER BY y;
  731. }
  732. } {15 nosort}
  733. do_test where-7.19 {
  734. cksort {
  735. SELECT w FROM t1 WHERE x=3 AND y<81 ORDER BY y DESC;
  736. }
  737. } {nosort}
  738. do_test where-7.20 {
  739. cksort {
  740. SELECT w FROM t1 WHERE x=3 AND y<=81 ORDER BY y DESC;
  741. }
  742. } {8 nosort}
  743. do_test where-7.21 {
  744. cksort {
  745. SELECT w FROM t1 WHERE x=3 AND y>256 ORDER BY y DESC;
  746. }
  747. } {nosort}
  748. do_test where-7.22 {
  749. cksort {
  750. SELECT w FROM t1 WHERE x=3 AND y>=256 ORDER BY y DESC;
  751. }
  752. } {15 nosort}
  753. do_test where-7.23 {
  754. cksort {
  755. SELECT w FROM t1 WHERE x=0 AND y<4 ORDER BY y;
  756. }
  757. } {nosort}
  758. do_test where-7.24 {
  759. cksort {
  760. SELECT w FROM t1 WHERE x=0 AND y<=4 ORDER BY y;
  761. }
  762. } {1 nosort}
  763. do_test where-7.25 {
  764. cksort {
  765. SELECT w FROM t1 WHERE x=6 AND y>10201 ORDER BY y;
  766. }
  767. } {nosort}
  768. do_test where-7.26 {
  769. cksort {
  770. SELECT w FROM t1 WHERE x=6 AND y>=10201 ORDER BY y;
  771. }
  772. } {100 nosort}
  773. do_test where-7.27 {
  774. cksort {
  775. SELECT w FROM t1 WHERE x=0 AND y<4 ORDER BY y DESC;
  776. }
  777. } {nosort}
  778. do_test where-7.28 {
  779. cksort {
  780. SELECT w FROM t1 WHERE x=0 AND y<=4 ORDER BY y DESC;
  781. }
  782. } {1 nosort}
  783. do_test where-7.29 {
  784. cksort {
  785. SELECT w FROM t1 WHERE x=6 AND y>10201 ORDER BY y DESC;
  786. }
  787. } {nosort}
  788. do_test where-7.30 {
  789. cksort {
  790. SELECT w FROM t1 WHERE x=6 AND y>=10201 ORDER BY y DESC;
  791. }
  792. } {100 nosort}
  793. do_test where-7.31 {
  794. cksort {
  795. SELECT y FROM t1 ORDER BY rowid DESC LIMIT 3
  796. }
  797. } {10201 10000 9801 nosort}
  798. do_test where-7.32 {
  799. cksort {
  800. SELECT y FROM t1 WHERE y<25 ORDER BY rowid DESC
  801. }
  802. } {16 9 4 nosort}
  803. do_test where-7.33 {
  804. cksort {
  805. SELECT y FROM t1 WHERE y<=25 ORDER BY rowid DESC
  806. }
  807. } {25 16 9 4 nosort}
  808. do_test where-7.34 {
  809. cksort {
  810. SELECT y FROM t1 WHERE y<25 AND y>4 ORDER BY rowid DESC, y DESC
  811. }
  812. } {16 9 nosort}
  813. do_test where-7.35 {
  814. cksort {
  815. SELECT y FROM t1 WHERE y<25 AND y>=4 ORDER BY rowid DESC
  816. }
  817. } {16 9 4 nosort}
  818. do_test where-8.1 {
  819. execsql {
  820. CREATE TABLE t4 AS SELECT * FROM t1;
  821. CREATE INDEX i4xy ON t4(x,y);
  822. }
  823. cksort {
  824. SELECT w FROM t4 WHERE x=4 and y<1000 ORDER BY y DESC limit 3;
  825. }
  826. } {30 29 28 nosort}
  827. do_test where-8.2 {
  828. execsql {
  829. DELETE FROM t4;
  830. }
  831. cksort {
  832. SELECT w FROM t4 WHERE x=4 and y<1000 ORDER BY y DESC limit 3;
  833. }
  834. } {nosort}
  835. # Make sure searches with an index work with an empty table.
  836. #
  837. do_test where-9.1 {
  838. execsql {
  839. CREATE TABLE t5(x PRIMARY KEY);
  840. SELECT * FROM t5 WHERE x<10;
  841. }
  842. } {}
  843. do_test where-9.2 {
  844. execsql {
  845. SELECT * FROM t5 WHERE x<10 ORDER BY x DESC;
  846. }
  847. } {}
  848. do_test where-9.3 {
  849. execsql {
  850. SELECT * FROM t5 WHERE x=10;
  851. }
  852. } {}
  853. do_test where-10.1 {
  854. execsql {
  855. SELECT 1 WHERE abs(random())<0
  856. }
  857. } {}
  858. do_test where-10.2 {
  859. proc tclvar_func {vname} {return [set ::$vname]}
  860. db function tclvar tclvar_func
  861. set ::v1 0
  862. execsql {
  863. SELECT count(*) FROM t1 WHERE tclvar('v1');
  864. }
  865. } {0}
  866. do_test where-10.3 {
  867. set ::v1 1
  868. execsql {
  869. SELECT count(*) FROM t1 WHERE tclvar('v1');
  870. }
  871. } {100}
  872. do_test where-10.4 {
  873. set ::v1 1
  874. proc tclvar_func {vname} {
  875. upvar #0 $vname v
  876. set v [expr {!$v}]
  877. return $v
  878. }
  879. execsql {
  880. SELECT count(*) FROM t1 WHERE tclvar('v1');
  881. }
  882. } {50}
  883. # Ticket #1376. The query below was causing a segfault.
  884. # The problem was the age-old error of calling realloc() on an
  885. # array while there are still pointers to individual elements of
  886. # that array.
  887. #
  888. do_test where-11.1 {
  889. execsql {
  890. CREATE TABLE t99(Dte INT, X INT);
  891. DELETE FROM t99 WHERE (Dte = 2451337) OR (Dte = 2451339) OR
  892. (Dte BETWEEN 2451345 AND 2451347) OR (Dte = 2451351) OR
  893. (Dte BETWEEN 2451355 AND 2451356) OR (Dte = 2451358) OR
  894. (Dte = 2451362) OR (Dte = 2451365) OR (Dte = 2451367) OR
  895. (Dte BETWEEN 2451372 AND 2451376) OR (Dte BETWEEN 2451382 AND 2451384) OR
  896. (Dte = 2451387) OR (Dte BETWEEN 2451389 AND 2451391) OR
  897. (Dte BETWEEN 2451393 AND 2451395) OR (Dte = 2451400) OR
  898. (Dte = 2451402) OR (Dte = 2451404) OR (Dte BETWEEN 2451416 AND 2451418) OR
  899. (Dte = 2451422) OR (Dte = 2451426) OR (Dte BETWEEN 2451445 AND 2451446) OR
  900. (Dte = 2451456) OR (Dte = 2451458) OR (Dte BETWEEN 2451465 AND 2451467) OR
  901. (Dte BETWEEN 2451469 AND 2451471) OR (Dte = 2451474) OR
  902. (Dte BETWEEN 2451477 AND 2451501) OR (Dte BETWEEN 2451503 AND 2451509) OR
  903. (Dte BETWEEN 2451511 AND 2451514) OR (Dte BETWEEN 2451518 AND 2451521) OR
  904. (Dte BETWEEN 2451523 AND 2451531) OR (Dte BETWEEN 2451533 AND 2451537) OR
  905. (Dte BETWEEN 2451539 AND 2451544) OR (Dte BETWEEN 2451546 AND 2451551) OR
  906. (Dte BETWEEN 2451553 AND 2451555) OR (Dte = 2451557) OR
  907. (Dte BETWEEN 2451559 AND 2451561) OR (Dte = 2451563) OR
  908. (Dte BETWEEN 2451565 AND 2451566) OR (Dte BETWEEN 2451569 AND 2451571) OR
  909. (Dte = 2451573) OR (Dte = 2451575) OR (Dte = 2451577) OR (Dte = 2451581) OR
  910. (Dte BETWEEN 2451583 AND 2451586) OR (Dte BETWEEN 2451588 AND 2451592) OR
  911. (Dte BETWEEN 2451596 AND 2451598) OR (Dte = 2451600) OR
  912. (Dte BETWEEN 2451602 AND 2451603) OR (Dte = 2451606) OR (Dte = 2451611);
  913. }
  914. } {}
  915. # Ticket #2116: Make sure sorting by index works well with nn INTEGER PRIMARY
  916. # KEY.
  917. #
  918. do_test where-12.1 {
  919. execsql {
  920. CREATE TABLE t6(a INTEGER PRIMARY KEY, b TEXT);
  921. INSERT INTO t6 VALUES(1,'one');
  922. INSERT INTO t6 VALUES(4,'four');
  923. CREATE INDEX t6i1 ON t6(b);
  924. }
  925. cksort {
  926. SELECT * FROM t6 ORDER BY b;
  927. }
  928. } {4 four 1 one nosort}
  929. do_test where-12.2 {
  930. cksort {
  931. SELECT * FROM t6 ORDER BY b, a;
  932. }
  933. } {4 four 1 one nosort}
  934. do_test where-12.3 {
  935. cksort {
  936. SELECT * FROM t6 ORDER BY a;
  937. }
  938. } {1 one 4 four nosort}
  939. do_test where-12.4 {
  940. cksort {
  941. SELECT * FROM t6 ORDER BY a, b;
  942. }
  943. } {1 one 4 four nosort}
  944. do_test where-12.5 {
  945. cksort {
  946. SELECT * FROM t6 ORDER BY b DESC;
  947. }
  948. } {1 one 4 four nosort}
  949. do_test where-12.6 {
  950. cksort {
  951. SELECT * FROM t6 ORDER BY b DESC, a DESC;
  952. }
  953. } {1 one 4 four nosort}
  954. do_test where-12.7 {
  955. cksort {
  956. SELECT * FROM t6 ORDER BY b DESC, a ASC;
  957. }
  958. } {1 one 4 four sort}
  959. do_test where-12.8 {
  960. cksort {
  961. SELECT * FROM t6 ORDER BY b ASC, a DESC;
  962. }
  963. } {4 four 1 one sort}
  964. do_test where-12.9 {
  965. cksort {
  966. SELECT * FROM t6 ORDER BY a DESC;
  967. }
  968. } {4 four 1 one nosort}
  969. do_test where-12.10 {
  970. cksort {
  971. SELECT * FROM t6 ORDER BY a DESC, b DESC;
  972. }
  973. } {4 four 1 one nosort}
  974. do_test where-12.11 {
  975. cksort {
  976. SELECT * FROM t6 ORDER BY a DESC, b ASC;
  977. }
  978. } {4 four 1 one nosort}
  979. do_test where-12.12 {
  980. cksort {
  981. SELECT * FROM t6 ORDER BY a ASC, b DESC;
  982. }
  983. } {1 one 4 four nosort}
  984. do_test where-13.1 {
  985. execsql {
  986. CREATE TABLE t7(a INTEGER PRIMARY KEY, b TEXT);
  987. INSERT INTO t7 VALUES(1,'one');
  988. INSERT INTO t7 VALUES(4,'four');
  989. CREATE INDEX t7i1 ON t7(b);
  990. }
  991. cksort {
  992. SELECT * FROM t7 ORDER BY b;
  993. }
  994. } {4 four 1 one nosort}
  995. do_test where-13.2 {
  996. cksort {
  997. SELECT * FROM t7 ORDER BY b, a;
  998. }
  999. } {4 four 1 one nosort}
  1000. do_test where-13.3 {
  1001. cksort {
  1002. SELECT * FROM t7 ORDER BY a;
  1003. }
  1004. } {1 one 4 four nosort}
  1005. do_test where-13.4 {
  1006. cksort {
  1007. SELECT * FROM t7 ORDER BY a, b;
  1008. }
  1009. } {1 one 4 four nosort}
  1010. do_test where-13.5 {
  1011. cksort {
  1012. SELECT * FROM t7 ORDER BY b DESC;
  1013. }
  1014. } {1 one 4 four nosort}
  1015. do_test where-13.6 {
  1016. cksort {
  1017. SELECT * FROM t7 ORDER BY b DESC, a DESC;
  1018. }
  1019. } {1 one 4 four nosort}
  1020. do_test where-13.7 {
  1021. cksort {
  1022. SELECT * FROM t7 ORDER BY b DESC, a ASC;
  1023. }
  1024. } {1 one 4 four sort}
  1025. do_test where-13.8 {
  1026. cksort {
  1027. SELECT * FROM t7 ORDER BY b ASC, a DESC;
  1028. }
  1029. } {4 four 1 one sort}
  1030. do_test where-13.9 {
  1031. cksort {
  1032. SELECT * FROM t7 ORDER BY a DESC;
  1033. }
  1034. } {4 four 1 one nosort}
  1035. do_test where-13.10 {
  1036. cksort {
  1037. SELECT * FROM t7 ORDER BY a DESC, b DESC;
  1038. }
  1039. } {4 four 1 one nosort}
  1040. do_test where-13.11 {
  1041. cksort {
  1042. SELECT * FROM t7 ORDER BY a DESC, b ASC;
  1043. }
  1044. } {4 four 1 one nosort}
  1045. do_test where-13.12 {
  1046. cksort {
  1047. SELECT * FROM t7 ORDER BY a ASC, b DESC;
  1048. }
  1049. } {1 one 4 four nosort}
  1050. # Ticket #2211.
  1051. #
  1052. # When optimizing out ORDER BY clauses, make sure that trailing terms
  1053. # of the ORDER BY clause do not reference other tables in a join.
  1054. #
  1055. do_test where-14.1 {
  1056. execsql {
  1057. CREATE TABLE t8(a INTEGER PRIMARY KEY, b TEXT UNIQUE);
  1058. INSERT INTO t8 VALUES(1,'one');
  1059. INSERT INTO t8 VALUES(4,'four');
  1060. }
  1061. cksort {
  1062. SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.a, y.b
  1063. }
  1064. } {1/4 1/1 4/4 4/1 sort}
  1065. do_test where-14.2 {
  1066. cksort {
  1067. SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.a, y.b DESC
  1068. }
  1069. } {1/1 1/4 4/1 4/4 sort}
  1070. do_test where-14.3 {
  1071. cksort {
  1072. SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.a, x.b
  1073. }
  1074. } {1/1 1/4 4/1 4/4 nosort}
  1075. do_test where-14.4 {
  1076. cksort {
  1077. SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.a, x.b DESC
  1078. }
  1079. } {1/1 1/4 4/1 4/4 nosort}
  1080. do_test where-14.5 {
  1081. # This test case changed from "nosort" to "sort". See ticket 2a5629202f.
  1082. cksort {
  1083. SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, x.a||x.b
  1084. }
  1085. } {4/1 4/4 1/1 1/4 sort}
  1086. do_test where-14.6 {
  1087. # This test case changed from "nosort" to "sort". See ticket 2a5629202f.
  1088. cksort {
  1089. SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, x.a||x.b DESC
  1090. }
  1091. } {4/1 4/4 1/1 1/4 sort}
  1092. do_test where-14.7 {
  1093. cksort {
  1094. SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, y.a||y.b
  1095. }
  1096. } {4/1 4/4 1/1 1/4 sort}
  1097. do_test where-14.7.1 {
  1098. cksort {
  1099. SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, x.a, y.a||y.b
  1100. }
  1101. } {4/1 4/4 1/1 1/4 sort}
  1102. do_test where-14.7.2 {
  1103. cksort {
  1104. SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, x.a, x.a||x.b
  1105. }
  1106. } {4/1 4/4 1/1 1/4 nosort}
  1107. do_test where-14.8 {
  1108. cksort {
  1109. SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, y.a||y.b DESC
  1110. }
  1111. } {4/4 4/1 1/4 1/1 sort}
  1112. do_test where-14.9 {
  1113. cksort {
  1114. SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, x.a||y.b
  1115. }
  1116. } {4/4 4/1 1/4 1/1 sort}
  1117. do_test where-14.10 {
  1118. cksort {
  1119. SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, x.a||y.b DESC
  1120. }
  1121. } {4/1 4/4 1/1 1/4 sort}
  1122. do_test where-14.11 {
  1123. cksort {
  1124. SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, y.a||x.b
  1125. }
  1126. } {4/1 4/4 1/1 1/4 sort}
  1127. do_test where-14.12 {
  1128. cksort {
  1129. SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, y.a||x.b DESC
  1130. }
  1131. } {4/4 4/1 1/4 1/1 sort}
  1132. # Ticket #2445.
  1133. #
  1134. # There was a crash that could occur when a where clause contains an
  1135. # alias for an expression in the result set, and that expression retrieves
  1136. # a column of the second or subsequent table in a join.
  1137. #
  1138. do_test where-15.1 {
  1139. execsql {
  1140. CREATE TEMP TABLE t1 (a, b, c, d, e);
  1141. CREATE TEMP TABLE t2 (f);
  1142. SELECT t1.e AS alias FROM t2, t1 WHERE alias = 1 ;
  1143. }
  1144. } {}
  1145. # Ticket #3408.
  1146. #
  1147. # The branch of code in where.c that generated rowid lookups was
  1148. # incorrectly deallocating a constant register, meaning that if the
  1149. # vdbe code ran more than once, the second time around the constant
  1150. # value may have been clobbered by some other value.
  1151. #
  1152. do_test where-16.1 {
  1153. execsql {
  1154. CREATE TABLE a1(id INTEGER PRIMARY KEY, v);
  1155. CREATE TABLE a2(id INTEGER PRIMARY KEY, v);
  1156. INSERT INTO a1 VALUES(1, 'one');
  1157. INSERT INTO a1 VALUES(2, 'two');
  1158. INSERT INTO a2 VALUES(1, 'one');
  1159. INSERT INTO a2 VALUES(2, 'two');
  1160. }
  1161. } {}
  1162. do_test where-16.2 {
  1163. execsql {
  1164. SELECT * FROM a2 CROSS JOIN a1 WHERE a1.id=1 AND a1.v='one';
  1165. }
  1166. } {1 one 1 one 2 two 1 one}
  1167. # The actual problem reported in #3408.
  1168. do_test where-16.3 {
  1169. execsql {
  1170. CREATE TEMP TABLE foo(idx INTEGER);
  1171. INSERT INTO foo VALUES(1);
  1172. INSERT INTO foo VALUES(1);
  1173. INSERT INTO foo VALUES(1);
  1174. INSERT INTO foo VALUES(2);
  1175. INSERT INTO foo VALUES(2);
  1176. CREATE TEMP TABLE bar(stuff INTEGER);
  1177. INSERT INTO bar VALUES(100);
  1178. INSERT INTO bar VALUES(200);
  1179. INSERT INTO bar VALUES(300);
  1180. }
  1181. } {}
  1182. do_test where-16.4 {
  1183. execsql {
  1184. SELECT bar.RowID id FROM foo, bar WHERE foo.idx = bar.RowID AND id = 2;
  1185. }
  1186. } {2 2}
  1187. integrity_check {where-99.0}
  1188. #---------------------------------------------------------------------
  1189. # These tests test that a bug surrounding the use of ForceInt has been
  1190. # fixed in where.c.
  1191. #
  1192. do_test where-17.1 {
  1193. execsql {
  1194. CREATE TABLE tbooking (
  1195. id INTEGER PRIMARY KEY,
  1196. eventtype INTEGER NOT NULL
  1197. );
  1198. INSERT INTO tbooking VALUES(42, 3);
  1199. INSERT INTO tbooking VALUES(43, 4);
  1200. }
  1201. } {}
  1202. do_test where-17.2 {
  1203. execsql {
  1204. SELECT a.id
  1205. FROM tbooking AS a
  1206. WHERE a.eventtype=3;
  1207. }
  1208. } {42}
  1209. do_test where-17.3 {
  1210. execsql {
  1211. SELECT a.id, (SELECT b.id FROM tbooking AS b WHERE b.id>a.id)
  1212. FROM tbooking AS a
  1213. WHERE a.eventtype=3;
  1214. }
  1215. } {42 43}
  1216. do_test where-17.4 {
  1217. execsql {
  1218. SELECT a.id, (SELECT b.id FROM tbooking AS b WHERE b.id>a.id)
  1219. FROM (SELECT 1.5 AS id) AS a
  1220. }
  1221. } {1.5 42}
  1222. do_test where-17.5 {
  1223. execsql {
  1224. CREATE TABLE tother(a, b);
  1225. INSERT INTO tother VALUES(1, 3.7);
  1226. SELECT id, a FROM tbooking, tother WHERE id>a;
  1227. }
  1228. } {42 1 43 1}
  1229. finish_test