subselect.sql 8.2 KB

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