subquery.test 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. # 2005 January 19
  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 script is testing correlated subqueries
  13. #
  14. # $Id: subquery.test,v 1.17 2009/01/09 01:12:28 drh Exp $
  15. #
  16. set testdir [file dirname $argv0]
  17. source $testdir/tester.tcl
  18. ifcapable !subquery {
  19. finish_test
  20. return
  21. }
  22. do_test subquery-1.1 {
  23. execsql {
  24. BEGIN;
  25. CREATE TABLE t1(a,b);
  26. INSERT INTO t1 VALUES(1,2);
  27. INSERT INTO t1 VALUES(3,4);
  28. INSERT INTO t1 VALUES(5,6);
  29. INSERT INTO t1 VALUES(7,8);
  30. CREATE TABLE t2(x,y);
  31. INSERT INTO t2 VALUES(1,1);
  32. INSERT INTO t2 VALUES(3,9);
  33. INSERT INTO t2 VALUES(5,25);
  34. INSERT INTO t2 VALUES(7,49);
  35. COMMIT;
  36. }
  37. execsql {
  38. SELECT a, (SELECT y FROM t2 WHERE x=a) FROM t1 WHERE b<8
  39. }
  40. } {1 1 3 9 5 25}
  41. do_test subquery-1.2 {
  42. execsql {
  43. UPDATE t1 SET b=b+(SELECT y FROM t2 WHERE x=a);
  44. SELECT * FROM t1;
  45. }
  46. } {1 3 3 13 5 31 7 57}
  47. do_test subquery-1.3 {
  48. execsql {
  49. SELECT b FROM t1 WHERE EXISTS(SELECT * FROM t2 WHERE y=a)
  50. }
  51. } {3}
  52. do_test subquery-1.4 {
  53. execsql {
  54. SELECT b FROM t1 WHERE NOT EXISTS(SELECT * FROM t2 WHERE y=a)
  55. }
  56. } {13 31 57}
  57. # Simple tests to make sure correlated subqueries in WHERE clauses
  58. # are used by the query optimizer correctly.
  59. do_test subquery-1.5 {
  60. execsql {
  61. SELECT a, x FROM t1, t2 WHERE t1.a = (SELECT x);
  62. }
  63. } {1 1 3 3 5 5 7 7}
  64. do_test subquery-1.6 {
  65. execsql {
  66. CREATE INDEX i1 ON t1(a);
  67. SELECT a, x FROM t1, t2 WHERE t1.a = (SELECT x);
  68. }
  69. } {1 1 3 3 5 5 7 7}
  70. do_test subquery-1.7 {
  71. execsql {
  72. SELECT a, x FROM t2, t1 WHERE t1.a = (SELECT x);
  73. }
  74. } {1 1 3 3 5 5 7 7}
  75. # Try an aggregate in both the subquery and the parent query.
  76. do_test subquery-1.8 {
  77. execsql {
  78. SELECT count(*) FROM t1 WHERE a > (SELECT count(*) FROM t2);
  79. }
  80. } {2}
  81. # Test a correlated subquery disables the "only open the index" optimization.
  82. do_test subquery-1.9.1 {
  83. execsql {
  84. SELECT (y*2)>b FROM t1, t2 WHERE a=x;
  85. }
  86. } {0 1 1 1}
  87. do_test subquery-1.9.2 {
  88. execsql {
  89. SELECT a FROM t1 WHERE (SELECT (y*2)>b FROM t2 WHERE a=x);
  90. }
  91. } {3 5 7}
  92. # Test that the flattening optimization works with subquery expressions.
  93. do_test subquery-1.10.1 {
  94. execsql {
  95. SELECT (SELECT a), b FROM t1;
  96. }
  97. } {1 3 3 13 5 31 7 57}
  98. do_test subquery-1.10.2 {
  99. execsql {
  100. SELECT * FROM (SELECT (SELECT a), b FROM t1);
  101. }
  102. } {1 3 3 13 5 31 7 57}
  103. do_test subquery-1.10.3 {
  104. execsql {
  105. SELECT * FROM (SELECT (SELECT sum(a) FROM t1));
  106. }
  107. } {16}
  108. do_test subquery-1.10.4 {
  109. execsql {
  110. CREATE TABLE t5 (val int, period text PRIMARY KEY);
  111. INSERT INTO t5 VALUES(5, '2001-3');
  112. INSERT INTO t5 VALUES(10, '2001-4');
  113. INSERT INTO t5 VALUES(15, '2002-1');
  114. INSERT INTO t5 VALUES(5, '2002-2');
  115. INSERT INTO t5 VALUES(10, '2002-3');
  116. INSERT INTO t5 VALUES(15, '2002-4');
  117. INSERT INTO t5 VALUES(10, '2003-1');
  118. INSERT INTO t5 VALUES(5, '2003-2');
  119. INSERT INTO t5 VALUES(25, '2003-3');
  120. INSERT INTO t5 VALUES(5, '2003-4');
  121. SELECT period, vsum
  122. FROM (SELECT
  123. a.period,
  124. (select sum(val) from t5 where period between a.period and '2002-4') vsum
  125. FROM t5 a where a.period between '2002-1' and '2002-4')
  126. WHERE vsum < 45 ;
  127. }
  128. } {2002-2 30 2002-3 25 2002-4 15}
  129. do_test subquery-1.10.5 {
  130. execsql {
  131. SELECT period, vsum from
  132. (select a.period,
  133. (select sum(val) from t5 where period between a.period and '2002-4') vsum
  134. FROM t5 a where a.period between '2002-1' and '2002-4')
  135. WHERE vsum < 45 ;
  136. }
  137. } {2002-2 30 2002-3 25 2002-4 15}
  138. do_test subquery-1.10.6 {
  139. execsql {
  140. DROP TABLE t5;
  141. }
  142. } {}
  143. #------------------------------------------------------------------
  144. # The following test cases - subquery-2.* - are not logically
  145. # organized. They're here largely because they were failing during
  146. # one stage of development of sub-queries.
  147. #
  148. do_test subquery-2.1 {
  149. execsql {
  150. SELECT (SELECT 10);
  151. }
  152. } {10}
  153. do_test subquery-2.2.1 {
  154. execsql {
  155. CREATE TABLE t3(a PRIMARY KEY, b);
  156. INSERT INTO t3 VALUES(1, 2);
  157. INSERT INTO t3 VALUES(3, 1);
  158. }
  159. } {}
  160. do_test subquery-2.2.2 {
  161. execsql {
  162. SELECT * FROM t3 WHERE a IN (SELECT b FROM t3);
  163. }
  164. } {1 2}
  165. do_test subquery-2.2.3 {
  166. execsql {
  167. DROP TABLE t3;
  168. }
  169. } {}
  170. do_test subquery-2.3.1 {
  171. execsql {
  172. CREATE TABLE t3(a TEXT);
  173. INSERT INTO t3 VALUES('10');
  174. }
  175. } {}
  176. do_test subquery-2.3.2 {
  177. execsql {
  178. SELECT a IN (10.0, 20) FROM t3;
  179. }
  180. } {0}
  181. do_test subquery-2.3.3 {
  182. execsql {
  183. DROP TABLE t3;
  184. }
  185. } {}
  186. do_test subquery-2.4.1 {
  187. execsql {
  188. CREATE TABLE t3(a TEXT);
  189. INSERT INTO t3 VALUES('XX');
  190. }
  191. } {}
  192. do_test subquery-2.4.2 {
  193. execsql {
  194. SELECT count(*) FROM t3 WHERE a IN (SELECT 'XX')
  195. }
  196. } {1}
  197. do_test subquery-2.4.3 {
  198. execsql {
  199. DROP TABLE t3;
  200. }
  201. } {}
  202. do_test subquery-2.5.1 {
  203. execsql {
  204. CREATE TABLE t3(a INTEGER);
  205. INSERT INTO t3 VALUES(10);
  206. CREATE TABLE t4(x TEXT);
  207. INSERT INTO t4 VALUES('10.0');
  208. }
  209. } {}
  210. do_test subquery-2.5.2 {
  211. # In the expr "x IN (SELECT a FROM t3)" the RHS of the IN operator
  212. # has text affinity and the LHS has integer affinity. The rule is
  213. # that we try to convert both sides to an integer before doing the
  214. # comparision. Hence, the integer value 10 in t3 will compare equal
  215. # to the string value '10.0' in t4 because the t4 value will be
  216. # converted into an integer.
  217. execsql {
  218. SELECT * FROM t4 WHERE x IN (SELECT a FROM t3);
  219. }
  220. } {10.0}
  221. do_test subquery-2.5.3.1 {
  222. # The t4i index cannot be used to resolve the "x IN (...)" constraint
  223. # because the constraint has integer affinity but t4i has text affinity.
  224. execsql {
  225. CREATE INDEX t4i ON t4(x);
  226. SELECT * FROM t4 WHERE x IN (SELECT a FROM t3);
  227. }
  228. } {10.0}
  229. do_test subquery-2.5.3.2 {
  230. # Verify that the t4i index was not used in the previous query
  231. execsql {
  232. EXPLAIN QUERY PLAN
  233. SELECT * FROM t4 WHERE x IN (SELECT a FROM t3);
  234. }
  235. } {~/t4i/}
  236. do_test subquery-2.5.4 {
  237. execsql {
  238. DROP TABLE t3;
  239. DROP TABLE t4;
  240. }
  241. } {}
  242. #------------------------------------------------------------------
  243. # The following test cases - subquery-3.* - test tickets that
  244. # were raised during development of correlated subqueries.
  245. #
  246. # Ticket 1083
  247. ifcapable view {
  248. do_test subquery-3.1 {
  249. catchsql { DROP TABLE t1; }
  250. catchsql { DROP TABLE t2; }
  251. execsql {
  252. CREATE TABLE t1(a,b);
  253. INSERT INTO t1 VALUES(1,2);
  254. CREATE VIEW v1 AS SELECT b FROM t1 WHERE a>0;
  255. CREATE TABLE t2(p,q);
  256. INSERT INTO t2 VALUES(2,9);
  257. SELECT * FROM v1 WHERE EXISTS(SELECT * FROM t2 WHERE p=v1.b);
  258. }
  259. } {2}
  260. do_test subquery-3.1.1 {
  261. execsql {
  262. SELECT * FROM v1 WHERE EXISTS(SELECT 1);
  263. }
  264. } {2}
  265. } else {
  266. catchsql { DROP TABLE t1; }
  267. catchsql { DROP TABLE t2; }
  268. execsql {
  269. CREATE TABLE t1(a,b);
  270. INSERT INTO t1 VALUES(1,2);
  271. CREATE TABLE t2(p,q);
  272. INSERT INTO t2 VALUES(2,9);
  273. }
  274. }
  275. # Ticket 1084
  276. do_test subquery-3.2 {
  277. catchsql {
  278. CREATE TABLE t1(a,b);
  279. INSERT INTO t1 VALUES(1,2);
  280. }
  281. execsql {
  282. SELECT (SELECT t1.a) FROM t1;
  283. }
  284. } {1}
  285. # Test Cases subquery-3.3.* test correlated subqueries where the
  286. # parent query is an aggregate query. Ticket #1105 is an example
  287. # of such a query.
  288. #
  289. do_test subquery-3.3.1 {
  290. execsql {
  291. SELECT a, (SELECT b) FROM t1 GROUP BY a;
  292. }
  293. } {1 2}
  294. do_test subquery-3.3.2 {
  295. catchsql {DROP TABLE t2}
  296. execsql {
  297. CREATE TABLE t2(c, d);
  298. INSERT INTO t2 VALUES(1, 'one');
  299. INSERT INTO t2 VALUES(2, 'two');
  300. SELECT a, (SELECT d FROM t2 WHERE a=c) FROM t1 GROUP BY a;
  301. }
  302. } {1 one}
  303. do_test subquery-3.3.3 {
  304. execsql {
  305. INSERT INTO t1 VALUES(2, 4);
  306. SELECT max(a), (SELECT d FROM t2 WHERE a=c) FROM t1;
  307. }
  308. } {2 two}
  309. do_test subquery-3.3.4 {
  310. execsql {
  311. SELECT a, (SELECT (SELECT d FROM t2 WHERE a=c)) FROM t1 GROUP BY a;
  312. }
  313. } {1 one 2 two}
  314. do_test subquery-3.3.5 {
  315. execsql {
  316. SELECT a, (SELECT count(*) FROM t2 WHERE a=c) FROM t1;
  317. }
  318. } {1 1 2 1}
  319. # The following tests check for aggregate subqueries in an aggregate
  320. # query.
  321. #
  322. do_test subquery-3.4.1 {
  323. execsql {
  324. CREATE TABLE t34(x,y);
  325. INSERT INTO t34 VALUES(106,4), (107,3), (106,5), (107,5);
  326. SELECT a.x, avg(a.y)
  327. FROM t34 AS a
  328. GROUP BY a.x
  329. HAVING NOT EXISTS( SELECT b.x, avg(b.y)
  330. FROM t34 AS b
  331. GROUP BY b.x
  332. HAVING avg(a.y) > avg(b.y));
  333. }
  334. } {107 4.0}
  335. do_test subquery-3.4.2 {
  336. execsql {
  337. SELECT a.x, avg(a.y) AS avg1
  338. FROM t34 AS a
  339. GROUP BY a.x
  340. HAVING NOT EXISTS( SELECT b.x, avg(b.y) AS avg2
  341. FROM t34 AS b
  342. GROUP BY b.x
  343. HAVING avg1 > avg2);
  344. }
  345. } {107 4.0}
  346. do_test subquery-3.4.3 {
  347. execsql {
  348. SELECT
  349. a.x,
  350. avg(a.y),
  351. NOT EXISTS ( SELECT b.x, avg(b.y)
  352. FROM t34 AS b
  353. GROUP BY b.x
  354. HAVING avg(a.y) > avg(b.y)),
  355. EXISTS ( SELECT c.x, avg(c.y)
  356. FROM t34 AS c
  357. GROUP BY c.x
  358. HAVING avg(a.y) > avg(c.y))
  359. FROM t34 AS a
  360. GROUP BY a.x
  361. ORDER BY a.x;
  362. }
  363. } {106 4.5 0 1 107 4.0 1 0}
  364. do_test subquery-3.5.1 {
  365. execsql {
  366. CREATE TABLE t35a(x); INSERT INTO t35a VALUES(1),(2),(3);
  367. CREATE TABLE t35b(y); INSERT INTO t35b VALUES(98), (99);
  368. SELECT max((SELECT avg(y) FROM t35b)) FROM t35a;
  369. }
  370. } {98.5}
  371. do_test subquery-3.5.2 {
  372. execsql {
  373. SELECT max((SELECT count(y) FROM t35b)) FROM t35a;
  374. }
  375. } {2}
  376. do_test subquery-3.5.3 {
  377. execsql {
  378. SELECT max((SELECT count() FROM t35b)) FROM t35a;
  379. }
  380. } {2}
  381. do_test subquery-3.5.4 {
  382. catchsql {
  383. SELECT max((SELECT count(x) FROM t35b)) FROM t35a;
  384. }
  385. } {1 {misuse of aggregate: count()}}
  386. do_test subquery-3.5.5 {
  387. catchsql {
  388. SELECT max((SELECT count(x) FROM t35b)) FROM t35a;
  389. }
  390. } {1 {misuse of aggregate: count()}}
  391. do_test subquery-3.5.6 {
  392. catchsql {
  393. SELECT max((SELECT a FROM (SELECT count(x) AS a FROM t35b))) FROM t35a;
  394. }
  395. } {1 {misuse of aggregate: count()}}
  396. do_test subquery-3.5.7 {
  397. execsql {
  398. SELECT max((SELECT a FROM (SELECT count(y) AS a FROM t35b))) FROM t35a;
  399. }
  400. } {2}
  401. #------------------------------------------------------------------
  402. # These tests - subquery-4.* - use the TCL statement cache to try
  403. # and expose bugs to do with re-using statements that have been
  404. # passed to sqlite3_reset().
  405. #
  406. # One problem was that VDBE memory cells were not being initialized
  407. # to NULL on the second and subsequent executions.
  408. #
  409. do_test subquery-4.1.1 {
  410. execsql {
  411. SELECT (SELECT a FROM t1);
  412. }
  413. } {1}
  414. do_test subquery-4.2 {
  415. execsql {
  416. DELETE FROM t1;
  417. SELECT (SELECT a FROM t1);
  418. }
  419. } {{}}
  420. do_test subquery-4.2.1 {
  421. execsql {
  422. CREATE TABLE t3(a PRIMARY KEY);
  423. INSERT INTO t3 VALUES(10);
  424. }
  425. execsql {INSERT INTO t3 VALUES((SELECT max(a) FROM t3)+1)}
  426. } {}
  427. do_test subquery-4.2.2 {
  428. execsql {INSERT INTO t3 VALUES((SELECT max(a) FROM t3)+1)}
  429. } {}
  430. #------------------------------------------------------------------
  431. # The subquery-5.* tests make sure string literals in double-quotes
  432. # are handled efficiently. Double-quote literals are first checked
  433. # to see if they match any column names. If there is not column name
  434. # match then those literals are used a string constants. When a
  435. # double-quoted string appears, we want to make sure that the search
  436. # for a matching column name did not cause an otherwise static subquery
  437. # to become a dynamic (correlated) subquery.
  438. #
  439. do_test subquery-5.1 {
  440. proc callcntproc {n} {
  441. incr ::callcnt
  442. return $n
  443. }
  444. set callcnt 0
  445. db function callcnt callcntproc
  446. execsql {
  447. CREATE TABLE t4(x,y);
  448. INSERT INTO t4 VALUES('one',1);
  449. INSERT INTO t4 VALUES('two',2);
  450. INSERT INTO t4 VALUES('three',3);
  451. INSERT INTO t4 VALUES('four',4);
  452. CREATE TABLE t5(a,b);
  453. INSERT INTO t5 VALUES(1,11);
  454. INSERT INTO t5 VALUES(2,22);
  455. INSERT INTO t5 VALUES(3,33);
  456. INSERT INTO t5 VALUES(4,44);
  457. SELECT b FROM t5 WHERE a IN
  458. (SELECT callcnt(y)+0 FROM t4 WHERE x="two")
  459. }
  460. } {22}
  461. do_test subquery-5.2 {
  462. # This is the key test. The subquery should have only run once. If
  463. # The double-quoted identifier "two" were causing the subquery to be
  464. # processed as a correlated subquery, then it would have run 4 times.
  465. set callcnt
  466. } {1}
  467. # Ticket #1380. Make sure correlated subqueries on an IN clause work
  468. # correctly when the left-hand side of the IN operator is constant.
  469. #
  470. do_test subquery-6.1 {
  471. set callcnt 0
  472. execsql {
  473. SELECT x FROM t4 WHERE 1 IN (SELECT callcnt(count(*)) FROM t5 WHERE a=y)
  474. }
  475. } {one two three four}
  476. do_test subquery-6.2 {
  477. set callcnt
  478. } {4}
  479. do_test subquery-6.3 {
  480. set callcnt 0
  481. execsql {
  482. SELECT x FROM t4 WHERE 1 IN (SELECT callcnt(count(*)) FROM t5 WHERE a=1)
  483. }
  484. } {one two three four}
  485. do_test subquery-6.4 {
  486. set callcnt
  487. } {1}
  488. if 0 { ############# disable until we get #2652 fixed
  489. # Ticket #2652. Allow aggregate functions of outer queries inside
  490. # a non-aggregate subquery.
  491. #
  492. do_test subquery-7.1 {
  493. execsql {
  494. CREATE TABLE t7(c7);
  495. INSERT INTO t7 VALUES(1);
  496. INSERT INTO t7 VALUES(2);
  497. INSERT INTO t7 VALUES(3);
  498. CREATE TABLE t8(c8);
  499. INSERT INTO t8 VALUES(100);
  500. INSERT INTO t8 VALUES(200);
  501. INSERT INTO t8 VALUES(300);
  502. CREATE TABLE t9(c9);
  503. INSERT INTO t9 VALUES(10000);
  504. INSERT INTO t9 VALUES(20000);
  505. INSERT INTO t9 VALUES(30000);
  506. SELECT (SELECT c7+c8 FROM t7) FROM t8;
  507. }
  508. } {101 201 301}
  509. do_test subquery-7.2 {
  510. execsql {
  511. SELECT (SELECT max(c7)+c8 FROM t7) FROM t8;
  512. }
  513. } {103 203 303}
  514. do_test subquery-7.3 {
  515. execsql {
  516. SELECT (SELECT c7+max(c8) FROM t8) FROM t7
  517. }
  518. } {301}
  519. do_test subquery-7.4 {
  520. execsql {
  521. SELECT (SELECT max(c7)+max(c8) FROM t8) FROM t7
  522. }
  523. } {303}
  524. do_test subquery-7.5 {
  525. execsql {
  526. SELECT (SELECT c8 FROM t8 WHERE rowid=max(c7)) FROM t7
  527. }
  528. } {300}
  529. do_test subquery-7.6 {
  530. execsql {
  531. SELECT (SELECT (SELECT max(c7+c8+c9) FROM t9) FROM t8) FROM t7
  532. }
  533. } {30101 30102 30103}
  534. do_test subquery-7.7 {
  535. execsql {
  536. SELECT (SELECT (SELECT c7+max(c8+c9) FROM t9) FROM t8) FROM t7
  537. }
  538. } {30101 30102 30103}
  539. do_test subquery-7.8 {
  540. execsql {
  541. SELECT (SELECT (SELECT max(c7)+c8+c9 FROM t9) FROM t8) FROM t7
  542. }
  543. } {10103}
  544. do_test subquery-7.9 {
  545. execsql {
  546. SELECT (SELECT (SELECT c7+max(c8)+c9 FROM t9) FROM t8) FROM t7
  547. }
  548. } {10301 10302 10303}
  549. do_test subquery-7.10 {
  550. execsql {
  551. SELECT (SELECT (SELECT c7+c8+max(c9) FROM t9) FROM t8) FROM t7
  552. }
  553. } {30101 30102 30103}
  554. do_test subquery-7.11 {
  555. execsql {
  556. SELECT (SELECT (SELECT max(c7)+max(c8)+max(c9) FROM t9) FROM t8) FROM t7
  557. }
  558. } {30303}
  559. } ;############# Disabled
  560. finish_test