PageRenderTime 67ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/test/subquery.test

https://bitbucket.org/aware/sqlite
Unknown | 584 lines | 557 code | 27 blank | 0 comment | 0 complexity | 04df00f5345cabd7c094f5d55ca2113e MD5 | raw file
  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:$
  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. set ::sqlite_query_plan
  232. } {t4 {}}
  233. do_test subquery-2.5.4 {
  234. execsql {
  235. DROP TABLE t3;
  236. DROP TABLE t4;
  237. }
  238. } {}
  239. #------------------------------------------------------------------
  240. # The following test cases - subquery-3.* - test tickets that
  241. # were raised during development of correlated subqueries.
  242. #
  243. # Ticket 1083
  244. ifcapable view {
  245. do_test subquery-3.1 {
  246. catchsql { DROP TABLE t1; }
  247. catchsql { DROP TABLE t2; }
  248. execsql {
  249. CREATE TABLE t1(a,b);
  250. INSERT INTO t1 VALUES(1,2);
  251. CREATE VIEW v1 AS SELECT b FROM t1 WHERE a>0;
  252. CREATE TABLE t2(p,q);
  253. INSERT INTO t2 VALUES(2,9);
  254. SELECT * FROM v1 WHERE EXISTS(SELECT * FROM t2 WHERE p=v1.b);
  255. }
  256. } {2}
  257. do_test subquery-3.1.1 {
  258. execsql {
  259. SELECT * FROM v1 WHERE EXISTS(SELECT 1);
  260. }
  261. } {2}
  262. } else {
  263. catchsql { DROP TABLE t1; }
  264. catchsql { DROP TABLE t2; }
  265. execsql {
  266. CREATE TABLE t1(a,b);
  267. INSERT INTO t1 VALUES(1,2);
  268. CREATE TABLE t2(p,q);
  269. INSERT INTO t2 VALUES(2,9);
  270. }
  271. }
  272. # Ticket 1084
  273. do_test subquery-3.2 {
  274. catchsql {
  275. CREATE TABLE t1(a,b);
  276. INSERT INTO t1 VALUES(1,2);
  277. }
  278. execsql {
  279. SELECT (SELECT t1.a) FROM t1;
  280. }
  281. } {1}
  282. # Test Cases subquery-3.3.* test correlated subqueries where the
  283. # parent query is an aggregate query. Ticket #1105 is an example
  284. # of such a query.
  285. #
  286. do_test subquery-3.3.1 {
  287. execsql {
  288. SELECT a, (SELECT b) FROM t1 GROUP BY a;
  289. }
  290. } {1 2}
  291. do_test subquery-3.3.2 {
  292. catchsql {DROP TABLE t2}
  293. execsql {
  294. CREATE TABLE t2(c, d);
  295. INSERT INTO t2 VALUES(1, 'one');
  296. INSERT INTO t2 VALUES(2, 'two');
  297. SELECT a, (SELECT d FROM t2 WHERE a=c) FROM t1 GROUP BY a;
  298. }
  299. } {1 one}
  300. do_test subquery-3.3.3 {
  301. execsql {
  302. INSERT INTO t1 VALUES(2, 4);
  303. SELECT max(a), (SELECT d FROM t2 WHERE a=c) FROM t1;
  304. }
  305. } {2 two}
  306. do_test subquery-3.3.4 {
  307. execsql {
  308. SELECT a, (SELECT (SELECT d FROM t2 WHERE a=c)) FROM t1 GROUP BY a;
  309. }
  310. } {1 one 2 two}
  311. do_test subquery-3.3.5 {
  312. execsql {
  313. SELECT a, (SELECT count(*) FROM t2 WHERE a=c) FROM t1;
  314. }
  315. } {1 1 2 1}
  316. # The following tests check for aggregate subqueries in an aggregate
  317. # query.
  318. #
  319. do_test subquery-3.4.1 {
  320. execsql {
  321. CREATE TABLE t34(x,y);
  322. INSERT INTO t34 VALUES(106,4), (107,3), (106,5), (107,5);
  323. SELECT a.x, avg(a.y)
  324. FROM t34 AS a
  325. GROUP BY a.x
  326. HAVING NOT EXISTS( SELECT b.x, avg(b.y)
  327. FROM t34 AS b
  328. GROUP BY b.x
  329. HAVING avg(a.y) > avg(b.y));
  330. }
  331. } {107 4.0}
  332. do_test subquery-3.4.2 {
  333. execsql {
  334. SELECT a.x, avg(a.y) AS avg1
  335. FROM t34 AS a
  336. GROUP BY a.x
  337. HAVING NOT EXISTS( SELECT b.x, avg(b.y) AS avg2
  338. FROM t34 AS b
  339. GROUP BY b.x
  340. HAVING avg1 > avg2);
  341. }
  342. } {107 4.0}
  343. do_test subquery-3.4.3 {
  344. execsql {
  345. SELECT
  346. a.x,
  347. avg(a.y),
  348. NOT EXISTS ( SELECT b.x, avg(b.y)
  349. FROM t34 AS b
  350. GROUP BY b.x
  351. HAVING avg(a.y) > avg(b.y)),
  352. EXISTS ( SELECT c.x, avg(c.y)
  353. FROM t34 AS c
  354. GROUP BY c.x
  355. HAVING avg(a.y) > avg(c.y))
  356. FROM t34 AS a
  357. GROUP BY a.x
  358. ORDER BY a.x;
  359. }
  360. } {106 4.5 0 1 107 4.0 1 0}
  361. do_test subquery-3.5.1 {
  362. execsql {
  363. CREATE TABLE t35a(x); INSERT INTO t35a VALUES(1),(2),(3);
  364. CREATE TABLE t35b(y); INSERT INTO t35b VALUES(98), (99);
  365. SELECT max((SELECT avg(y) FROM t35b)) FROM t35a;
  366. }
  367. } {98.5}
  368. do_test subquery-3.5.2 {
  369. execsql {
  370. SELECT max((SELECT count(y) FROM t35b)) FROM t35a;
  371. }
  372. } {2}
  373. do_test subquery-3.5.3 {
  374. execsql {
  375. SELECT max((SELECT count() FROM t35b)) FROM t35a;
  376. }
  377. } {2}
  378. do_test subquery-3.5.4 {
  379. catchsql {
  380. SELECT max((SELECT count(x) FROM t35b)) FROM t35a;
  381. }
  382. } {1 {misuse of aggregate: count()}}
  383. do_test subquery-3.5.5 {
  384. catchsql {
  385. SELECT max((SELECT count(x) FROM t35b)) FROM t35a;
  386. }
  387. } {1 {misuse of aggregate: count()}}
  388. do_test subquery-3.5.6 {
  389. catchsql {
  390. SELECT max((SELECT a FROM (SELECT count(x) AS a FROM t35b))) FROM t35a;
  391. }
  392. } {1 {misuse of aggregate: count()}}
  393. do_test subquery-3.5.7 {
  394. execsql {
  395. SELECT max((SELECT a FROM (SELECT count(y) AS a FROM t35b))) FROM t35a;
  396. }
  397. } {2}
  398. #------------------------------------------------------------------
  399. # These tests - subquery-4.* - use the TCL statement cache to try
  400. # and expose bugs to do with re-using statements that have been
  401. # passed to sqlite3_reset().
  402. #
  403. # One problem was that VDBE memory cells were not being initialised
  404. # to NULL on the second and subsequent executions.
  405. #
  406. do_test subquery-4.1.1 {
  407. execsql {
  408. SELECT (SELECT a FROM t1);
  409. }
  410. } {1}
  411. do_test subquery-4.2 {
  412. execsql {
  413. DELETE FROM t1;
  414. SELECT (SELECT a FROM t1);
  415. }
  416. } {{}}
  417. do_test subquery-4.2.1 {
  418. execsql {
  419. CREATE TABLE t3(a PRIMARY KEY);
  420. INSERT INTO t3 VALUES(10);
  421. }
  422. execsql {INSERT INTO t3 VALUES((SELECT max(a) FROM t3)+1)}
  423. } {}
  424. do_test subquery-4.2.2 {
  425. execsql {INSERT INTO t3 VALUES((SELECT max(a) FROM t3)+1)}
  426. } {}
  427. #------------------------------------------------------------------
  428. # The subquery-5.* tests make sure string literals in double-quotes
  429. # are handled efficiently. Double-quote literals are first checked
  430. # to see if they match any column names. If there is not column name
  431. # match then those literals are used a string constants. When a
  432. # double-quoted string appears, we want to make sure that the search
  433. # for a matching column name did not cause an otherwise static subquery
  434. # to become a dynamic (correlated) subquery.
  435. #
  436. do_test subquery-5.1 {
  437. proc callcntproc {n} {
  438. incr ::callcnt
  439. return $n
  440. }
  441. set callcnt 0
  442. db function callcnt callcntproc
  443. execsql {
  444. CREATE TABLE t4(x,y);
  445. INSERT INTO t4 VALUES('one',1);
  446. INSERT INTO t4 VALUES('two',2);
  447. INSERT INTO t4 VALUES('three',3);
  448. INSERT INTO t4 VALUES('four',4);
  449. CREATE TABLE t5(a,b);
  450. INSERT INTO t5 VALUES(1,11);
  451. INSERT INTO t5 VALUES(2,22);
  452. INSERT INTO t5 VALUES(3,33);
  453. INSERT INTO t5 VALUES(4,44);
  454. SELECT b FROM t5 WHERE a IN
  455. (SELECT callcnt(y)+0 FROM t4 WHERE x="two")
  456. }
  457. } {22}
  458. do_test subquery-5.2 {
  459. # This is the key test. The subquery should have only run once. If
  460. # The double-quoted identifier "two" were causing the subquery to be
  461. # processed as a correlated subquery, then it would have run 4 times.
  462. set callcnt
  463. } {1}
  464. # Ticket #1380. Make sure correlated subqueries on an IN clause work
  465. # correctly when the left-hand side of the IN operator is constant.
  466. #
  467. do_test subquery-6.1 {
  468. set callcnt 0
  469. execsql {
  470. SELECT x FROM t4 WHERE 1 IN (SELECT callcnt(count(*)) FROM t5 WHERE a=y)
  471. }
  472. } {one two three four}
  473. do_test subquery-6.2 {
  474. set callcnt
  475. } {4}
  476. do_test subquery-6.3 {
  477. set callcnt 0
  478. execsql {
  479. SELECT x FROM t4 WHERE 1 IN (SELECT callcnt(count(*)) FROM t5 WHERE a=1)
  480. }
  481. } {one two three four}
  482. do_test subquery-6.4 {
  483. set callcnt
  484. } {1}
  485. if 0 { ############# disable until we get #2652 fixed
  486. # Ticket #2652. Allow aggregate functions of outer queries inside
  487. # a non-aggregate subquery.
  488. #
  489. do_test subquery-7.1 {
  490. execsql {
  491. CREATE TABLE t7(c7);
  492. INSERT INTO t7 VALUES(1);
  493. INSERT INTO t7 VALUES(2);
  494. INSERT INTO t7 VALUES(3);
  495. CREATE TABLE t8(c8);
  496. INSERT INTO t8 VALUES(100);
  497. INSERT INTO t8 VALUES(200);
  498. INSERT INTO t8 VALUES(300);
  499. CREATE TABLE t9(c9);
  500. INSERT INTO t9 VALUES(10000);
  501. INSERT INTO t9 VALUES(20000);
  502. INSERT INTO t9 VALUES(30000);
  503. SELECT (SELECT c7+c8 FROM t7) FROM t8;
  504. }
  505. } {101 201 301}
  506. do_test subquery-7.2 {
  507. execsql {
  508. SELECT (SELECT max(c7)+c8 FROM t7) FROM t8;
  509. }
  510. } {103 203 303}
  511. do_test subquery-7.3 {
  512. execsql {
  513. SELECT (SELECT c7+max(c8) FROM t8) FROM t7
  514. }
  515. } {301}
  516. do_test subquery-7.4 {
  517. execsql {
  518. SELECT (SELECT max(c7)+max(c8) FROM t8) FROM t7
  519. }
  520. } {303}
  521. do_test subquery-7.5 {
  522. execsql {
  523. SELECT (SELECT c8 FROM t8 WHERE rowid=max(c7)) FROM t7
  524. }
  525. } {300}
  526. do_test subquery-7.6 {
  527. execsql {
  528. SELECT (SELECT (SELECT max(c7+c8+c9) FROM t9) FROM t8) FROM t7
  529. }
  530. } {30101 30102 30103}
  531. do_test subquery-7.7 {
  532. execsql {
  533. SELECT (SELECT (SELECT c7+max(c8+c9) FROM t9) FROM t8) FROM t7
  534. }
  535. } {30101 30102 30103}
  536. do_test subquery-7.8 {
  537. execsql {
  538. SELECT (SELECT (SELECT max(c7)+c8+c9 FROM t9) FROM t8) FROM t7
  539. }
  540. } {10103}
  541. do_test subquery-7.9 {
  542. execsql {
  543. SELECT (SELECT (SELECT c7+max(c8)+c9 FROM t9) FROM t8) FROM t7
  544. }
  545. } {10301 10302 10303}
  546. do_test subquery-7.10 {
  547. execsql {
  548. SELECT (SELECT (SELECT c7+c8+max(c9) FROM t9) FROM t8) FROM t7
  549. }
  550. } {30101 30102 30103}
  551. do_test subquery-7.11 {
  552. execsql {
  553. SELECT (SELECT (SELECT max(c7)+max(c8)+max(c9) FROM t9) FROM t8) FROM t7
  554. }
  555. } {30303}
  556. } ;############# Disabled
  557. finish_test