subselect.out 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. --
  2. -- SUBSELECT
  3. --
  4. SELECT 1 AS one WHERE 1 IN (SELECT 1);
  5. one
  6. -----
  7. 1
  8. (1 row)
  9. SELECT 1 AS zero WHERE 1 NOT IN (SELECT 1);
  10. zero
  11. ------
  12. (0 rows)
  13. SELECT 1 AS zero WHERE 1 IN (SELECT 2);
  14. zero
  15. ------
  16. (0 rows)
  17. -- Check grammar's handling of extra parens in assorted contexts
  18. SELECT * FROM (SELECT 1 AS x) ss;
  19. x
  20. ---
  21. 1
  22. (1 row)
  23. SELECT * FROM ((SELECT 1 AS x)) ss;
  24. x
  25. ---
  26. 1
  27. (1 row)
  28. (SELECT 2) UNION SELECT 2;
  29. ?column?
  30. ----------
  31. 2
  32. (1 row)
  33. ((SELECT 2)) UNION SELECT 2;
  34. ?column?
  35. ----------
  36. 2
  37. (1 row)
  38. SELECT ((SELECT 2) UNION SELECT 2);
  39. ?column?
  40. ----------
  41. 2
  42. (1 row)
  43. SELECT (((SELECT 2)) UNION SELECT 2);
  44. ?column?
  45. ----------
  46. 2
  47. (1 row)
  48. -- Set up some simple test tables
  49. CREATE TABLE SUBSELECT_TBL (
  50. f1 integer,
  51. f2 integer,
  52. f3 float
  53. );
  54. INSERT INTO SUBSELECT_TBL VALUES (1, 2, 3);
  55. INSERT INTO SUBSELECT_TBL VALUES (2, 3, 4);
  56. INSERT INTO SUBSELECT_TBL VALUES (3, 4, 5);
  57. INSERT INTO SUBSELECT_TBL VALUES (1, 1, 1);
  58. INSERT INTO SUBSELECT_TBL VALUES (2, 2, 2);
  59. INSERT INTO SUBSELECT_TBL VALUES (3, 3, 3);
  60. INSERT INTO SUBSELECT_TBL VALUES (6, 7, 8);
  61. INSERT INTO SUBSELECT_TBL VALUES (8, 9, NULL);
  62. SELECT * FROM SUBSELECT_TBL;
  63. f1 | f2 | f3
  64. ----+----+----
  65. 1 | 2 | 3
  66. 2 | 3 | 4
  67. 3 | 4 | 5
  68. 1 | 1 | 1
  69. 2 | 2 | 2
  70. 3 | 3 | 3
  71. 6 | 7 | 8
  72. 8 | 9 |
  73. (8 rows)
  74. -- Uncorrelated subselects
  75. SELECT f1 AS "Constant Select" FROM SUBSELECT_TBL
  76. WHERE f1 IN (SELECT 1);
  77. Constant Select
  78. -----------------
  79. 1
  80. 1
  81. (2 rows)
  82. select 1 = all (select (select 1));
  83. ?column?
  84. ----------
  85. t
  86. (1 row)
  87. --
  88. -- Test cases to catch unpleasant interactions between IN-join processing
  89. -- and subquery pullup.
  90. --
  91. select count(*) from
  92. (select 1 from tenk1 a
  93. where unique1 IN (select hundred from tenk1 b)) ss;
  94. count
  95. -------
  96. 100
  97. (1 row)
  98. select count(distinct ss.ten) from
  99. (select ten from tenk1 a
  100. where unique1 IN (select hundred from tenk1 b)) ss;
  101. count
  102. -------
  103. 10
  104. (1 row)
  105. select count(*) from
  106. (select 1 from tenk1 a
  107. where unique1 IN (select distinct hundred from tenk1 b)) ss;
  108. count
  109. -------
  110. 100
  111. (1 row)
  112. select count(distinct ss.ten) from
  113. (select ten from tenk1 a
  114. where unique1 IN (select distinct hundred from tenk1 b)) ss;
  115. count
  116. -------
  117. 10
  118. (1 row)
  119. --
  120. -- Test cases to check for overenthusiastic optimization of
  121. -- "IN (SELECT DISTINCT ...)" and related cases. Per example from
  122. -- Luca Pireddu and Michael Fuhr.
  123. --
  124. CREATE TEMP TABLE foo (id integer);
  125. CREATE TEMP TABLE bar (id1 integer, id2 integer);
  126. INSERT INTO foo VALUES (1);
  127. INSERT INTO bar VALUES (1, 1);
  128. INSERT INTO bar VALUES (2, 2);
  129. INSERT INTO bar VALUES (3, 1);
  130. -- These cases require an extra level of distinct-ing above subquery s
  131. SELECT * FROM foo WHERE id IN
  132. (SELECT id2 FROM (SELECT DISTINCT id1, id2 FROM bar) AS s);
  133. id
  134. ----
  135. 1
  136. (1 row)
  137. SELECT * FROM foo WHERE id IN
  138. (SELECT id2 FROM (SELECT id1,id2 FROM bar GROUP BY id1,id2) AS s);
  139. id
  140. ----
  141. 1
  142. (1 row)
  143. SELECT * FROM foo WHERE id IN
  144. (SELECT id2 FROM (SELECT id1, id2 FROM bar UNION
  145. SELECT id1, id2 FROM bar) AS s);
  146. id
  147. ----
  148. 1
  149. (1 row)
  150. -- These cases do not
  151. SELECT * FROM foo WHERE id IN
  152. (SELECT id2 FROM (SELECT DISTINCT ON (id2) id1, id2 FROM bar) AS s);
  153. id
  154. ----
  155. 1
  156. (1 row)
  157. SELECT * FROM foo WHERE id IN
  158. (SELECT id2 FROM (SELECT id2 FROM bar GROUP BY id2) AS s);
  159. id
  160. ----
  161. 1
  162. (1 row)
  163. SELECT * FROM foo WHERE id IN
  164. (SELECT id2 FROM (SELECT id2 FROM bar UNION
  165. SELECT id2 FROM bar) AS s);
  166. id
  167. ----
  168. 1
  169. (1 row)
  170. --
  171. -- Test case to catch problems with multiply nested sub-SELECTs not getting
  172. -- recalculated properly. Per bug report from Didier Moens.
  173. --
  174. CREATE TABLE orderstest (
  175. approver_ref integer,
  176. po_ref integer,
  177. ordercanceled boolean
  178. );
  179. INSERT INTO orderstest VALUES (1, 1, false);
  180. INSERT INTO orderstest VALUES (66, 5, false);
  181. INSERT INTO orderstest VALUES (66, 6, false);
  182. INSERT INTO orderstest VALUES (66, 7, false);
  183. INSERT INTO orderstest VALUES (66, 1, true);
  184. INSERT INTO orderstest VALUES (66, 8, false);
  185. INSERT INTO orderstest VALUES (66, 1, false);
  186. INSERT INTO orderstest VALUES (77, 1, false);
  187. INSERT INTO orderstest VALUES (1, 1, false);
  188. INSERT INTO orderstest VALUES (66, 1, false);
  189. INSERT INTO orderstest VALUES (1, 1, false);
  190. --
  191. -- Test cases to catch situations where rule rewriter fails to propagate
  192. -- hasSubLinks flag correctly. Per example from Kyle Bateman.
  193. --
  194. create temp table parts (
  195. partnum text,
  196. cost float8
  197. );
  198. create temp table shipped (
  199. ttype char(2),
  200. ordnum int4,
  201. partnum text,
  202. value float8
  203. );
  204. insert into parts (partnum, cost) values (1, 1234.56);
  205. --
  206. -- Test cases involving PARAM_EXEC parameters and min/max index optimizations.
  207. -- Per bug report from David Sanchez i Gregori.
  208. --
  209. select * from (
  210. select max(unique1) from tenk1 as a
  211. where exists (select 1 from tenk1 as b where b.thousand = a.unique2)
  212. ) ss;
  213. max
  214. ------
  215. 9997
  216. (1 row)
  217. select * from (
  218. select min(unique1) from tenk1 as a
  219. where not exists (select 1 from tenk1 as b where b.unique2 = 10000)
  220. ) ss;
  221. min
  222. -----
  223. 0
  224. (1 row)
  225. --
  226. -- Test that an IN implemented using a UniquePath does unique-ification
  227. -- with the right semantics, as per bug #4113. (Unfortunately we have
  228. -- no simple way to ensure that this test case actually chooses that type
  229. -- of plan, but it does in releases 7.4-8.3. Note that an ordering difference
  230. -- here might mean that some other plan type is being used, rendering the test
  231. -- pointless.)
  232. --
  233. create temp table numeric_table (num_col numeric);
  234. insert into numeric_table values (1), (1.000000000000000000001), (2), (3);
  235. create temp table float_table (float_col float8);
  236. insert into float_table values (1), (2), (3);
  237. select * from float_table
  238. where float_col in (select num_col from numeric_table);
  239. float_col
  240. -----------
  241. 1
  242. 2
  243. 3
  244. (3 rows)
  245. --
  246. -- Test case for bug #4290: bogus calculation of subplan param sets
  247. --
  248. create temp table ta (id int primary key, val int);
  249. insert into ta values(1,1);
  250. insert into ta values(2,2);
  251. create temp table tb (id int primary key, aval int);
  252. insert into tb values(1,1);
  253. insert into tb values(2,1);
  254. insert into tb values(3,2);
  255. insert into tb values(4,2);
  256. create temp table tc (id int primary key, aid int);
  257. insert into tc values(1,1);
  258. insert into tc values(2,2);
  259. --
  260. -- Test case for 8.3 "failed to locate grouping columns" bug
  261. --
  262. create temp table t1 (f1 numeric(14,0), f2 varchar(30));
  263. select * from
  264. (select distinct f1, f2, (select f2 from t1 x where x.f1 = up.f1) as fs
  265. from t1 up) ss
  266. group by f1,f2,fs;
  267. f1 | f2 | fs
  268. ----+----+----
  269. (0 rows)
  270. --
  271. -- Test case for bug #5514 (mishandling of whole-row Vars in subselects)
  272. --
  273. create temp table table_a(id integer);
  274. insert into table_a values (42);
  275. --
  276. -- Test case for sublinks pulled up into joinaliasvars lists in an
  277. -- inherited update/delete query
  278. --
  279. begin; -- this shouldn't delete anything, but be safe
  280. rollback;
  281. --
  282. -- Test case for subselect within UPDATE of INSERT...ON CONFLICT DO UPDATE
  283. --
  284. create temp table upsert(key int4 primary key, val text);
  285. --
  286. -- Test case for cross-type partial matching in hashed subplan (bug #7597)
  287. --
  288. create temp table outer_7597 (f1 int4, f2 int4);
  289. insert into outer_7597 values (0, 0);
  290. insert into outer_7597 values (1, 0);
  291. insert into outer_7597 values (0, null);
  292. insert into outer_7597 values (1, null);
  293. create temp table inner_7597(c1 int8, c2 int8);
  294. insert into inner_7597 values(0, null);
  295. --
  296. -- Similar test case using text that verifies that collation
  297. -- information is passed through by execTuplesEqual() in nodeSubplan.c
  298. -- (otherwise it would error in texteq())
  299. --
  300. create temp table outer_text (f1 text, f2 text);
  301. insert into outer_text values ('a', 'a');
  302. insert into outer_text values ('b', 'a');
  303. insert into outer_text values ('a', null);
  304. insert into outer_text values ('b', null);
  305. create temp table inner_text (c1 text, c2 text);
  306. insert into inner_text values ('a', null);
  307. insert into inner_text values ('123', '456');
  308. begin;
  309. rollback; -- to get rid of the bogus operator
  310. select count(*) from tenk1 t
  311. where (exists(select 1 from tenk1 k where k.unique1 = t.unique2) or ten < 0);
  312. count
  313. -------
  314. 10000
  315. (1 row)
  316. select count(*) from tenk1 t
  317. where (exists(select 1 from tenk1 k where k.unique1 = t.unique2) or ten < 0)
  318. and thousand = 1;
  319. count
  320. -------
  321. 10
  322. (1 row)
  323. --
  324. -- Check we don't misoptimize a NOT IN where the subquery returns no rows.
  325. --
  326. create temp table notinouter (a int);
  327. create temp table notininner (b int not null);
  328. insert into notinouter values (null), (1);
  329. --
  330. -- Check we behave sanely in corner case of empty SELECT list (bug #8648)
  331. --
  332. create temp table nocolumns();
  333. select exists(select * from nocolumns);
  334. exists
  335. --------
  336. f
  337. (1 row)
  338. --
  339. -- Test that LIMIT can be pushed to SORT through a subquery that just projects
  340. -- columns. We check for that having happened by looking to see if EXPLAIN
  341. -- ANALYZE shows that a top-N sort was used. We must suppress or filter away
  342. -- all the non-invariant parts of the EXPLAIN ANALYZE output.
  343. --
  344. create table sq_limit (pk int primary key, c1 int, c2 int);
  345. insert into sq_limit values
  346. (1, 1, 1),
  347. (2, 2, 2),
  348. (3, 3, 3),
  349. (4, 4, 4),
  350. (5, 1, 1),
  351. (6, 2, 2),
  352. (7, 3, 3),
  353. (8, 4, 4);
  354. select * from (select pk,c2 from sq_limit order by c1,pk) as x limit 3;
  355. pk | c2
  356. ----+----
  357. 1 | 1
  358. 5 | 1
  359. 2 | 2
  360. (3 rows)
  361. drop table sq_limit;
  362. --
  363. -- Ensure that backward scan direction isn't propagated into
  364. -- expression subqueries (bug #15336)
  365. --
  366. begin;
  367. commit;