OSDN Git Service

Unify spelling of "canceled", "canceling", "cancellation"
[pg-rex/syncrep.git] / src / test / regress / sql / subselect.sql
1 --
2 -- SUBSELECT
3 --
4
5 SELECT 1 AS one WHERE 1 IN (SELECT 1);
6
7 SELECT 1 AS zero WHERE 1 NOT IN (SELECT 1);
8
9 SELECT 1 AS zero WHERE 1 IN (SELECT 2);
10
11 -- Set up some simple test tables
12
13 CREATE TABLE SUBSELECT_TBL (
14   f1 integer,
15   f2 integer,
16   f3 float
17 );
18
19 INSERT INTO SUBSELECT_TBL VALUES (1, 2, 3);
20 INSERT INTO SUBSELECT_TBL VALUES (2, 3, 4);
21 INSERT INTO SUBSELECT_TBL VALUES (3, 4, 5);
22 INSERT INTO SUBSELECT_TBL VALUES (1, 1, 1);
23 INSERT INTO SUBSELECT_TBL VALUES (2, 2, 2);
24 INSERT INTO SUBSELECT_TBL VALUES (3, 3, 3);
25 INSERT INTO SUBSELECT_TBL VALUES (6, 7, 8);
26 INSERT INTO SUBSELECT_TBL VALUES (8, 9, NULL);
27
28 SELECT '' AS eight, * FROM SUBSELECT_TBL;
29
30 -- Uncorrelated subselects
31
32 SELECT '' AS two, f1 AS "Constant Select" FROM SUBSELECT_TBL
33   WHERE f1 IN (SELECT 1);
34
35 SELECT '' AS six, f1 AS "Uncorrelated Field" FROM SUBSELECT_TBL
36   WHERE f1 IN (SELECT f2 FROM SUBSELECT_TBL);
37
38 SELECT '' AS six, f1 AS "Uncorrelated Field" FROM SUBSELECT_TBL
39   WHERE f1 IN (SELECT f2 FROM SUBSELECT_TBL WHERE
40     f2 IN (SELECT f1 FROM SUBSELECT_TBL));
41
42 SELECT '' AS three, f1, f2
43   FROM SUBSELECT_TBL
44   WHERE (f1, f2) NOT IN (SELECT f2, CAST(f3 AS int4) FROM SUBSELECT_TBL
45                          WHERE f3 IS NOT NULL);
46
47 -- Correlated subselects
48
49 SELECT '' AS six, f1 AS "Correlated Field", f2 AS "Second Field"
50   FROM SUBSELECT_TBL upper
51   WHERE f1 IN (SELECT f2 FROM SUBSELECT_TBL WHERE f1 = upper.f1);
52
53 SELECT '' AS six, f1 AS "Correlated Field", f3 AS "Second Field"
54   FROM SUBSELECT_TBL upper
55   WHERE f1 IN
56     (SELECT f2 FROM SUBSELECT_TBL WHERE CAST(upper.f2 AS float) = f3);
57
58 SELECT '' AS six, f1 AS "Correlated Field", f3 AS "Second Field"
59   FROM SUBSELECT_TBL upper
60   WHERE f3 IN (SELECT upper.f1 + f2 FROM SUBSELECT_TBL
61                WHERE f2 = CAST(f3 AS integer));
62
63 SELECT '' AS five, f1 AS "Correlated Field"
64   FROM SUBSELECT_TBL
65   WHERE (f1, f2) IN (SELECT f2, CAST(f3 AS int4) FROM SUBSELECT_TBL
66                      WHERE f3 IS NOT NULL);
67
68 --
69 -- Use some existing tables in the regression test
70 --
71
72 SELECT '' AS eight, ss.f1 AS "Correlated Field", ss.f3 AS "Second Field"
73   FROM SUBSELECT_TBL ss
74   WHERE f1 NOT IN (SELECT f1+1 FROM INT4_TBL
75                    WHERE f1 != ss.f1 AND f1 < 2147483647);
76
77 select q1, float8(count(*)) / (select count(*) from int8_tbl)
78 from int8_tbl group by q1 order by q1;
79
80 --
81 -- Test cases to catch unpleasant interactions between IN-join processing
82 -- and subquery pullup.
83 --
84
85 select count(*) from
86   (select 1 from tenk1 a
87    where unique1 IN (select hundred from tenk1 b)) ss;
88 select count(distinct ss.ten) from
89   (select ten from tenk1 a
90    where unique1 IN (select hundred from tenk1 b)) ss;
91 select count(*) from
92   (select 1 from tenk1 a
93    where unique1 IN (select distinct hundred from tenk1 b)) ss;
94 select count(distinct ss.ten) from
95   (select ten from tenk1 a
96    where unique1 IN (select distinct hundred from tenk1 b)) ss;
97
98 --
99 -- Test cases to check for overenthusiastic optimization of
100 -- "IN (SELECT DISTINCT ...)" and related cases.  Per example from
101 -- Luca Pireddu and Michael Fuhr.
102 --
103
104 CREATE TEMP TABLE foo (id integer);
105 CREATE TEMP TABLE bar (id1 integer, id2 integer);
106
107 INSERT INTO foo VALUES (1);
108
109 INSERT INTO bar VALUES (1, 1);
110 INSERT INTO bar VALUES (2, 2);
111 INSERT INTO bar VALUES (3, 1);
112
113 -- These cases require an extra level of distinct-ing above subquery s
114 SELECT * FROM foo WHERE id IN
115     (SELECT id2 FROM (SELECT DISTINCT id1, id2 FROM bar) AS s);
116 SELECT * FROM foo WHERE id IN
117     (SELECT id2 FROM (SELECT id1,id2 FROM bar GROUP BY id1,id2) AS s);
118 SELECT * FROM foo WHERE id IN
119     (SELECT id2 FROM (SELECT id1, id2 FROM bar UNION
120                       SELECT id1, id2 FROM bar) AS s);
121
122 -- These cases do not
123 SELECT * FROM foo WHERE id IN
124     (SELECT id2 FROM (SELECT DISTINCT ON (id2) id1, id2 FROM bar) AS s);
125 SELECT * FROM foo WHERE id IN
126     (SELECT id2 FROM (SELECT id2 FROM bar GROUP BY id2) AS s);
127 SELECT * FROM foo WHERE id IN
128     (SELECT id2 FROM (SELECT id2 FROM bar UNION
129                       SELECT id2 FROM bar) AS s);
130
131 --
132 -- Test case to catch problems with multiply nested sub-SELECTs not getting
133 -- recalculated properly.  Per bug report from Didier Moens.
134 --
135
136 CREATE TABLE orderstest (
137     approver_ref integer,
138     po_ref integer,
139     ordercanceled boolean
140 );
141
142 INSERT INTO orderstest VALUES (1, 1, false);
143 INSERT INTO orderstest VALUES (66, 5, false);
144 INSERT INTO orderstest VALUES (66, 6, false);
145 INSERT INTO orderstest VALUES (66, 7, false);
146 INSERT INTO orderstest VALUES (66, 1, true);
147 INSERT INTO orderstest VALUES (66, 8, false);
148 INSERT INTO orderstest VALUES (66, 1, false);
149 INSERT INTO orderstest VALUES (77, 1, false);
150 INSERT INTO orderstest VALUES (1, 1, false);
151 INSERT INTO orderstest VALUES (66, 1, false);
152 INSERT INTO orderstest VALUES (1, 1, false);
153
154 CREATE VIEW orders_view AS
155 SELECT *,
156 (SELECT CASE
157    WHEN ord.approver_ref=1 THEN '---' ELSE 'Approved'
158  END) AS "Approved",
159 (SELECT CASE
160  WHEN ord.ordercanceled
161  THEN 'Canceled'
162  ELSE
163   (SELECT CASE
164                 WHEN ord.po_ref=1
165                 THEN
166                  (SELECT CASE
167                                 WHEN ord.approver_ref=1
168                                 THEN '---'
169                                 ELSE 'Approved'
170                         END)
171                 ELSE 'PO'
172         END)
173 END) AS "Status",
174 (CASE
175  WHEN ord.ordercanceled
176  THEN 'Canceled'
177  ELSE
178   (CASE
179                 WHEN ord.po_ref=1
180                 THEN
181                  (CASE
182                                 WHEN ord.approver_ref=1
183                                 THEN '---'
184                                 ELSE 'Approved'
185                         END)
186                 ELSE 'PO'
187         END)
188 END) AS "Status_OK"
189 FROM orderstest ord;
190
191 SELECT * FROM orders_view;
192
193 DROP TABLE orderstest cascade;
194
195 --
196 -- Test cases to catch situations where rule rewriter fails to propagate
197 -- hasSubLinks flag correctly.  Per example from Kyle Bateman.
198 --
199
200 create temp table parts (
201     partnum     text,
202     cost        float8
203 );
204
205 create temp table shipped (
206     ttype       char(2),
207     ordnum      int4,
208     partnum     text,
209     value       float8
210 );
211
212 create temp view shipped_view as
213     select * from shipped where ttype = 'wt';
214
215 create rule shipped_view_insert as on insert to shipped_view do instead
216     insert into shipped values('wt', new.ordnum, new.partnum, new.value);
217
218 insert into parts (partnum, cost) values (1, 1234.56);
219
220 insert into shipped_view (ordnum, partnum, value)
221     values (0, 1, (select cost from parts where partnum = '1'));
222
223 select * from shipped_view;
224
225 create rule shipped_view_update as on update to shipped_view do instead
226     update shipped set partnum = new.partnum, value = new.value
227         where ttype = new.ttype and ordnum = new.ordnum;
228
229 update shipped_view set value = 11
230     from int4_tbl a join int4_tbl b
231       on (a.f1 = (select f1 from int4_tbl c where c.f1=b.f1))
232     where ordnum = a.f1;
233
234 select * from shipped_view;
235
236 select f1, ss1 as relabel from
237     (select *, (select sum(f1) from int4_tbl b where f1 >= a.f1) as ss1
238      from int4_tbl a) ss;
239
240 --
241 -- Test cases involving PARAM_EXEC parameters and min/max index optimizations.
242 -- Per bug report from David Sanchez i Gregori.
243 --
244
245 select * from (
246   select max(unique1) from tenk1 as a
247   where exists (select 1 from tenk1 as b where b.thousand = a.unique2)
248 ) ss;
249
250 select * from (
251   select min(unique1) from tenk1 as a
252   where not exists (select 1 from tenk1 as b where b.unique2 = 10000)
253 ) ss;
254
255 --
256 -- Test that an IN implemented using a UniquePath does unique-ification
257 -- with the right semantics, as per bug #4113.  (Unfortunately we have
258 -- no simple way to ensure that this test case actually chooses that type
259 -- of plan, but it does in releases 7.4-8.3.  Note that an ordering difference
260 -- here might mean that some other plan type is being used, rendering the test
261 -- pointless.)
262 --
263
264 create temp table numeric_table (num_col numeric);
265 insert into numeric_table values (1), (1.000000000000000000001), (2), (3);
266
267 create temp table float_table (float_col float8);
268 insert into float_table values (1), (2), (3);
269
270 select * from float_table
271   where float_col in (select num_col from numeric_table);
272
273 select * from numeric_table
274   where num_col in (select float_col from float_table);
275
276 --
277 -- Test case for bug #4290: bogus calculation of subplan param sets
278 --
279
280 create temp table ta (id int primary key, val int);
281
282 insert into ta values(1,1);
283 insert into ta values(2,2);
284
285 create temp table tb (id int primary key, aval int);
286
287 insert into tb values(1,1);
288 insert into tb values(2,1);
289 insert into tb values(3,2);
290 insert into tb values(4,2);
291
292 create temp table tc (id int primary key, aid int);
293
294 insert into tc values(1,1);
295 insert into tc values(2,2);
296
297 select
298   ( select min(tb.id) from tb
299     where tb.aval = (select ta.val from ta where ta.id = tc.aid) ) as min_tb_id
300 from tc;
301
302 --
303 -- Test case for 8.3 "failed to locate grouping columns" bug
304 --
305
306 create temp table t1 (f1 numeric(14,0), f2 varchar(30));
307
308 select * from
309   (select distinct f1, f2, (select f2 from t1 x where x.f1 = up.f1) as fs
310    from t1 up) ss
311 group by f1,f2,fs;
312
313 --
314 -- Test case for bug #5514 (mishandling of whole-row Vars in subselects)
315 --
316
317 create temp table table_a(id integer);
318 insert into table_a values (42);
319
320 create temp view view_a as select * from table_a;
321
322 select view_a from view_a;
323 select (select view_a) from view_a;
324 select (select (select view_a)) from view_a;
325 select (select (a.*)::text) from view_a a;
326
327 --
328 -- Test case for sublinks pushed down into subselects via join alias expansion
329 --
330
331 select
332   (select sq1) as qq1
333 from
334   (select exists(select 1 from int4_tbl where f1 = q2) as sq1, 42 as dummy
335    from int8_tbl) sq0
336   join
337   int4_tbl i4 on dummy = i4.f1;
338
339 --
340 -- Test case for premature memory release during hashing of subplan output
341 --
342
343 select '1'::text in (select '1'::name union all select '1'::name);
344
345 --
346 -- Test case for planner bug with nested EXISTS handling
347 --
348 select a.thousand from tenk1 a, tenk1 b
349 where a.thousand = b.thousand
350   and exists ( select 1 from tenk1 c where b.hundred = c.hundred
351                    and not exists ( select 1 from tenk1 d
352                                     where a.thousand = d.thousand ) );