select6.test 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 SELECT statements that contain
  13. # subqueries in their FROM clause.
  14. #
  15. # $Id: select6.test,v 1.9 2002/04/30 19:20:29 drh Exp $
  16. set testdir [file dirname $argv0]
  17. source $testdir/tester.tcl
  18. do_test select6-1.0 {
  19. execsql {
  20. BEGIN;
  21. CREATE TABLE t1(x, y);
  22. INSERT INTO t1 VALUES(1,1);
  23. INSERT INTO t1 VALUES(2,2);
  24. INSERT INTO t1 VALUES(3,2);
  25. INSERT INTO t1 VALUES(4,3);
  26. INSERT INTO t1 VALUES(5,3);
  27. INSERT INTO t1 VALUES(6,3);
  28. INSERT INTO t1 VALUES(7,3);
  29. INSERT INTO t1 VALUES(8,4);
  30. INSERT INTO t1 VALUES(9,4);
  31. INSERT INTO t1 VALUES(10,4);
  32. INSERT INTO t1 VALUES(11,4);
  33. INSERT INTO t1 VALUES(12,4);
  34. INSERT INTO t1 VALUES(13,4);
  35. INSERT INTO t1 VALUES(14,4);
  36. INSERT INTO t1 VALUES(15,4);
  37. INSERT INTO t1 VALUES(16,5);
  38. INSERT INTO t1 VALUES(17,5);
  39. INSERT INTO t1 VALUES(18,5);
  40. INSERT INTO t1 VALUES(19,5);
  41. INSERT INTO t1 VALUES(20,5);
  42. COMMIT;
  43. SELECT DISTINCT y FROM t1 ORDER BY y;
  44. }
  45. } {1 2 3 4 5}
  46. do_test select6-1.1 {
  47. execsql2 {SELECT * FROM (SELECT x, y FROM t1 WHERE x<2)}
  48. } {x 1 y 1}
  49. do_test select6-1.2 {
  50. execsql {SELECT count(*) FROM (SELECT y FROM t1)}
  51. } {20}
  52. do_test select6-1.3 {
  53. execsql {SELECT count(*) FROM (SELECT DISTINCT y FROM t1)}
  54. } {5}
  55. do_test select6-1.4 {
  56. execsql {SELECT count(*) FROM (SELECT DISTINCT * FROM (SELECT y FROM t1))}
  57. } {5}
  58. do_test select6-1.5 {
  59. execsql {SELECT count(*) FROM (SELECT * FROM (SELECT DISTINCT y FROM t1))}
  60. } {5}
  61. do_test select6-1.6 {
  62. execsql {
  63. SELECT *
  64. FROM (SELECT count(*),y FROM t1 GROUP BY y) AS a,
  65. (SELECT max(x),y FROM t1 GROUP BY y) as b
  66. WHERE a.y=b.y ORDER BY a.y
  67. }
  68. } {1 1 1 1 2 2 3 2 4 3 7 3 8 4 15 4 5 5 20 5}
  69. do_test select6-1.7 {
  70. execsql {
  71. SELECT a.y, a.[count(*)], [max(x)], [count(*)]
  72. FROM (SELECT count(*),y FROM t1 GROUP BY y) AS a,
  73. (SELECT max(x),y FROM t1 GROUP BY y) as b
  74. WHERE a.y=b.y ORDER BY a.y
  75. }
  76. } {1 1 1 1 2 2 3 2 3 4 7 4 4 8 15 8 5 5 20 5}
  77. do_test select6-1.8 {
  78. execsql {
  79. SELECT q, p, r
  80. FROM (SELECT count(*) as p , y as q FROM t1 GROUP BY y) AS a,
  81. (SELECT max(x) as r, y as s FROM t1 GROUP BY y) as b
  82. WHERE q=s ORDER BY s
  83. }
  84. } {1 1 1 2 2 3 3 4 7 4 8 15 5 5 20}
  85. do_test select6-1.9 {
  86. execsql {
  87. SELECT q, p, r, b.[min(x)+y]
  88. FROM (SELECT count(*) as p , y as q FROM t1 GROUP BY y) AS a,
  89. (SELECT max(x) as r, y as s, min(x)+y FROM t1 GROUP BY y) as b
  90. WHERE q=s ORDER BY s
  91. }
  92. } {1 1 1 2 2 2 3 4 3 4 7 7 4 8 15 12 5 5 20 21}
  93. do_test select6-2.0 {
  94. execsql {
  95. CREATE TABLE t2(a INTEGER PRIMARY KEY, b);
  96. INSERT INTO t2 SELECT * FROM t1;
  97. SELECT DISTINCT b FROM t2 ORDER BY b;
  98. }
  99. } {1 2 3 4 5}
  100. do_test select6-2.1 {
  101. execsql2 {SELECT * FROM (SELECT a, b FROM t2 WHERE a<2)}
  102. } {a 1 b 1}
  103. do_test select6-2.2 {
  104. execsql {SELECT count(*) FROM (SELECT b FROM t2)}
  105. } {20}
  106. do_test select6-2.3 {
  107. execsql {SELECT count(*) FROM (SELECT DISTINCT b FROM t2)}
  108. } {5}
  109. do_test select6-2.4 {
  110. execsql {SELECT count(*) FROM (SELECT DISTINCT * FROM (SELECT b FROM t2))}
  111. } {5}
  112. do_test select6-2.5 {
  113. execsql {SELECT count(*) FROM (SELECT * FROM (SELECT DISTINCT b FROM t2))}
  114. } {5}
  115. do_test select6-2.6 {
  116. execsql {
  117. SELECT *
  118. FROM (SELECT count(*),b FROM t2 GROUP BY b) AS a,
  119. (SELECT max(a),b FROM t2 GROUP BY b) as b
  120. WHERE a.b=b.b ORDER BY a.b
  121. }
  122. } {1 1 1 1 2 2 3 2 4 3 7 3 8 4 15 4 5 5 20 5}
  123. do_test select6-2.7 {
  124. execsql {
  125. SELECT a.b, a.[count(*)], [max(a)], [count(*)]
  126. FROM (SELECT count(*),b FROM t2 GROUP BY b) AS a,
  127. (SELECT max(a),b FROM t2 GROUP BY b) as b
  128. WHERE a.b=b.b ORDER BY a.b
  129. }
  130. } {1 1 1 1 2 2 3 2 3 4 7 4 4 8 15 8 5 5 20 5}
  131. do_test select6-2.8 {
  132. execsql {
  133. SELECT q, p, r
  134. FROM (SELECT count(*) as p , b as q FROM t2 GROUP BY b) AS a,
  135. (SELECT max(a) as r, b as s FROM t2 GROUP BY b) as b
  136. WHERE q=s ORDER BY s
  137. }
  138. } {1 1 1 2 2 3 3 4 7 4 8 15 5 5 20}
  139. do_test select6-2.9 {
  140. execsql {
  141. SELECT a.q, a.p, b.r
  142. FROM (SELECT count(*) as p , b as q FROM t2 GROUP BY q) AS a,
  143. (SELECT max(a) as r, b as s FROM t2 GROUP BY s) as b
  144. WHERE a.q=b.s ORDER BY a.q
  145. }
  146. } {1 1 1 2 2 3 3 4 7 4 8 15 5 5 20}
  147. do_test sqlite6-3.1 {
  148. execsql2 {
  149. SELECT * FROM (SELECT * FROM (SELECT * FROM t1 WHERE x=3));
  150. }
  151. } {x 3 y 2}
  152. do_test sqlite6-3.2 {
  153. execsql {
  154. SELECT * FROM
  155. (SELECT a.q, a.p, b.r
  156. FROM (SELECT count(*) as p , b as q FROM t2 GROUP BY q) AS a,
  157. (SELECT max(a) as r, b as s FROM t2 GROUP BY s) as b
  158. WHERE a.q=b.s ORDER BY a.q)
  159. ORDER BY [a.q]
  160. }
  161. } {1 1 1 2 2 3 3 4 7 4 8 15 5 5 20}
  162. do_test select6-3.3 {
  163. execsql {
  164. SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1)
  165. }
  166. } {10.5 3.7 14.2}
  167. do_test select6-3.4 {
  168. execsql {
  169. SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1 WHERE y=4)
  170. }
  171. } {11.5 4 15.5}
  172. do_test select6-3.5 {
  173. execsql {
  174. SELECT x,y,x+y FROM (SELECT avg(a) as 'x', avg(b) as 'y' FROM t2 WHERE a=4)
  175. }
  176. } {4 3 7}
  177. do_test select6-3.6 {
  178. execsql {
  179. SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1)
  180. WHERE a>10
  181. }
  182. } {10.5 3.7 14.2}
  183. do_test select6-3.7 {
  184. execsql {
  185. SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1)
  186. WHERE a<10
  187. }
  188. } {}
  189. do_test select6-3.8 {
  190. execsql {
  191. SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1 WHERE y=4)
  192. WHERE a>10
  193. }
  194. } {11.5 4 15.5}
  195. do_test select6-3.9 {
  196. execsql {
  197. SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1 WHERE y=4)
  198. WHERE a<10
  199. }
  200. } {}
  201. do_test select6-3.10 {
  202. execsql {
  203. SELECT a,b,a+b FROM (SELECT avg(x) as 'a', y as 'b' FROM t1 GROUP BY b)
  204. ORDER BY a
  205. }
  206. } {1 1 2 2.5 2 4.5 5.5 3 8.5 11.5 4 15.5 18 5 23}
  207. do_test select6-3.11 {
  208. execsql {
  209. SELECT a,b,a+b FROM
  210. (SELECT avg(x) as 'a', y as 'b' FROM t1 GROUP BY b)
  211. WHERE b<4 ORDER BY a
  212. }
  213. } {1 1 2 2.5 2 4.5 5.5 3 8.5}
  214. do_test select6-3.12 {
  215. execsql {
  216. SELECT a,b,a+b FROM
  217. (SELECT avg(x) as 'a', y as 'b' FROM t1 GROUP BY b HAVING a>1)
  218. WHERE b<4 ORDER BY a
  219. }
  220. } {2.5 2 4.5 5.5 3 8.5}
  221. do_test select6-3.13 {
  222. execsql {
  223. SELECT a,b,a+b FROM
  224. (SELECT avg(x) as 'a', y as 'b' FROM t1 GROUP BY b HAVING a>1)
  225. ORDER BY a
  226. }
  227. } {2.5 2 4.5 5.5 3 8.5 11.5 4 15.5 18 5 23}
  228. do_test select6-3.14 {
  229. execsql {
  230. SELECT [count(*)],y FROM (SELECT count(*), y FROM t1 GROUP BY y)
  231. ORDER BY [count(*)]
  232. }
  233. } {1 1 2 2 4 3 5 5 8 4}
  234. do_test select6-3.15 {
  235. execsql {
  236. SELECT [count(*)],y FROM (SELECT count(*), y FROM t1 GROUP BY y)
  237. ORDER BY y
  238. }
  239. } {1 1 2 2 4 3 8 4 5 5}
  240. do_test select6-4.1 {
  241. execsql {
  242. SELECT a,b,c FROM
  243. (SELECT x AS 'a', y AS 'b', x+y AS 'c' FROM t1 WHERE y=4)
  244. WHERE a<10 ORDER BY a;
  245. }
  246. } {8 4 12 9 4 13}
  247. do_test select6-4.2 {
  248. execsql {
  249. SELECT y FROM (SELECT DISTINCT y FROM t1) WHERE y<5 ORDER BY y
  250. }
  251. } {1 2 3 4}
  252. do_test select6-4.3 {
  253. execsql {
  254. SELECT DISTINCT y FROM (SELECT y FROM t1) WHERE y<5 ORDER BY y
  255. }
  256. } {1 2 3 4}
  257. do_test select6-4.4 {
  258. execsql {
  259. SELECT avg(y) FROM (SELECT DISTINCT y FROM t1) WHERE y<5 ORDER BY y
  260. }
  261. } {2.5}
  262. do_test select6-4.5 {
  263. execsql {
  264. SELECT avg(y) FROM (SELECT DISTINCT y FROM t1 WHERE y<5) ORDER BY y
  265. }
  266. } {2.5}
  267. do_test select6-5.1 {
  268. execsql {
  269. SELECT a,x,b FROM
  270. (SELECT x+3 AS 'a', x FROM t1 WHERE y=3) AS 'p',
  271. (SELECT x AS 'b' FROM t1 WHERE y=4) AS 'q'
  272. WHERE a=b
  273. ORDER BY a
  274. }
  275. } {8 5 8 9 6 9 10 7 10}
  276. do_test select6-5.2 {
  277. execsql {
  278. SELECT a,x,b FROM
  279. (SELECT x+3 AS 'a', x FROM t1 WHERE y=3),
  280. (SELECT x AS 'b' FROM t1 WHERE y=4)
  281. WHERE a=b
  282. ORDER BY a
  283. }
  284. } {8 5 8 9 6 9 10 7 10}
  285. # Tests of compound sub-selects
  286. #
  287. do_test select5-6.1 {
  288. execsql {
  289. DELETE FROM t1 WHERE x>4;
  290. SELECT * FROM t1
  291. }
  292. } {1 1 2 2 3 2 4 3}
  293. do_test select6-6.2 {
  294. execsql {
  295. SELECT * FROM (
  296. SELECT x AS 'a' FROM t1 UNION ALL SELECT x+10 AS 'a' FROM t1
  297. ) ORDER BY a;
  298. }
  299. } {1 2 3 4 11 12 13 14}
  300. do_test select6-6.3 {
  301. execsql {
  302. SELECT * FROM (
  303. SELECT x AS 'a' FROM t1 UNION ALL SELECT x+1 AS 'a' FROM t1
  304. ) ORDER BY a;
  305. }
  306. } {1 2 2 3 3 4 4 5}
  307. do_test select6-6.4 {
  308. execsql {
  309. SELECT * FROM (
  310. SELECT x AS 'a' FROM t1 UNION SELECT x+1 AS 'a' FROM t1
  311. ) ORDER BY a;
  312. }
  313. } {1 2 3 4 5}
  314. do_test select6-6.5 {
  315. execsql {
  316. SELECT * FROM (
  317. SELECT x AS 'a' FROM t1 INTERSECT SELECT x+1 AS 'a' FROM t1
  318. ) ORDER BY a;
  319. }
  320. } {2 3 4}
  321. do_test select6-6.6 {
  322. execsql {
  323. SELECT * FROM (
  324. SELECT x AS 'a' FROM t1 EXCEPT SELECT x*2 AS 'a' FROM t1
  325. ) ORDER BY a;
  326. }
  327. } {1 3}
  328. # Subselects with no FROM clause
  329. #
  330. do_test select6-7.1 {
  331. execsql {
  332. SELECT * FROM (SELECT 1)
  333. }
  334. } {1}
  335. do_test select6-7.2 {
  336. execsql {
  337. SELECT c,b,a,* FROM (SELECT 1 AS 'a', 2 AS 'b', 'abc' AS 'c')
  338. }
  339. } {abc 2 1 1 2 abc}
  340. do_test select6-7.3 {
  341. execsql {
  342. SELECT c,b,a,* FROM (SELECT 1 AS 'a', 2 AS 'b', 'abc' AS 'c' WHERE 0)
  343. }
  344. } {}
  345. do_test select6-7.4 {
  346. execsql2 {
  347. SELECT c,b,a,* FROM (SELECT 1 AS 'a', 2 AS 'b', 'abc' AS 'c' WHERE 1)
  348. }
  349. } {c abc b 2 a 1 a 1 b 2 c abc}
  350. finish_test