]> granicus.if.org Git - postgresql/blob - src/test/regress/expected/subselect.out
Fix whole-row Var evaluation to cope with resjunk columns (again).
[postgresql] / src / test / regress / expected / subselect.out
1 --
2 -- SUBSELECT
3 --
4 SELECT 1 AS one WHERE 1 IN (SELECT 1);
5  one 
6 -----
7    1
8 (1 row)
9
10 SELECT 1 AS zero WHERE 1 NOT IN (SELECT 1);
11  zero 
12 ------
13 (0 rows)
14
15 SELECT 1 AS zero WHERE 1 IN (SELECT 2);
16  zero 
17 ------
18 (0 rows)
19
20 -- Set up some simple test tables
21 CREATE TABLE SUBSELECT_TBL (
22   f1 integer,
23   f2 integer,
24   f3 float
25 );
26 INSERT INTO SUBSELECT_TBL VALUES (1, 2, 3);
27 INSERT INTO SUBSELECT_TBL VALUES (2, 3, 4);
28 INSERT INTO SUBSELECT_TBL VALUES (3, 4, 5);
29 INSERT INTO SUBSELECT_TBL VALUES (1, 1, 1);
30 INSERT INTO SUBSELECT_TBL VALUES (2, 2, 2);
31 INSERT INTO SUBSELECT_TBL VALUES (3, 3, 3);
32 INSERT INTO SUBSELECT_TBL VALUES (6, 7, 8);
33 INSERT INTO SUBSELECT_TBL VALUES (8, 9, NULL);
34 SELECT '' AS eight, * FROM SUBSELECT_TBL;
35  eight | f1 | f2 | f3 
36 -------+----+----+----
37        |  1 |  2 |  3
38        |  2 |  3 |  4
39        |  3 |  4 |  5
40        |  1 |  1 |  1
41        |  2 |  2 |  2
42        |  3 |  3 |  3
43        |  6 |  7 |  8
44        |  8 |  9 |   
45 (8 rows)
46
47 -- Uncorrelated subselects
48 SELECT '' AS two, f1 AS "Constant Select" FROM SUBSELECT_TBL
49   WHERE f1 IN (SELECT 1);
50  two | Constant Select 
51 -----+-----------------
52      |               1
53      |               1
54 (2 rows)
55
56 SELECT '' AS six, f1 AS "Uncorrelated Field" FROM SUBSELECT_TBL
57   WHERE f1 IN (SELECT f2 FROM SUBSELECT_TBL);
58  six | Uncorrelated Field 
59 -----+--------------------
60      |                  1
61      |                  2
62      |                  3
63      |                  1
64      |                  2
65      |                  3
66 (6 rows)
67
68 SELECT '' AS six, f1 AS "Uncorrelated Field" FROM SUBSELECT_TBL
69   WHERE f1 IN (SELECT f2 FROM SUBSELECT_TBL WHERE
70     f2 IN (SELECT f1 FROM SUBSELECT_TBL));
71  six | Uncorrelated Field 
72 -----+--------------------
73      |                  1
74      |                  2
75      |                  3
76      |                  1
77      |                  2
78      |                  3
79 (6 rows)
80
81 SELECT '' AS three, f1, f2
82   FROM SUBSELECT_TBL
83   WHERE (f1, f2) NOT IN (SELECT f2, CAST(f3 AS int4) FROM SUBSELECT_TBL
84                          WHERE f3 IS NOT NULL);
85  three | f1 | f2 
86 -------+----+----
87        |  1 |  2
88        |  6 |  7
89        |  8 |  9
90 (3 rows)
91
92 -- Correlated subselects
93 SELECT '' AS six, f1 AS "Correlated Field", f2 AS "Second Field"
94   FROM SUBSELECT_TBL upper
95   WHERE f1 IN (SELECT f2 FROM SUBSELECT_TBL WHERE f1 = upper.f1);
96  six | Correlated Field | Second Field 
97 -----+------------------+--------------
98      |                1 |            2
99      |                2 |            3
100      |                3 |            4
101      |                1 |            1
102      |                2 |            2
103      |                3 |            3
104 (6 rows)
105
106 SELECT '' AS six, f1 AS "Correlated Field", f3 AS "Second Field"
107   FROM SUBSELECT_TBL upper
108   WHERE f1 IN
109     (SELECT f2 FROM SUBSELECT_TBL WHERE CAST(upper.f2 AS float) = f3);
110  six | Correlated Field | Second Field 
111 -----+------------------+--------------
112      |                2 |            4
113      |                3 |            5
114      |                1 |            1
115      |                2 |            2
116      |                3 |            3
117 (5 rows)
118
119 SELECT '' AS six, f1 AS "Correlated Field", f3 AS "Second Field"
120   FROM SUBSELECT_TBL upper
121   WHERE f3 IN (SELECT upper.f1 + f2 FROM SUBSELECT_TBL
122                WHERE f2 = CAST(f3 AS integer));
123  six | Correlated Field | Second Field 
124 -----+------------------+--------------
125      |                1 |            3
126      |                2 |            4
127      |                3 |            5
128      |                6 |            8
129 (4 rows)
130
131 SELECT '' AS five, f1 AS "Correlated Field"
132   FROM SUBSELECT_TBL
133   WHERE (f1, f2) IN (SELECT f2, CAST(f3 AS int4) FROM SUBSELECT_TBL
134                      WHERE f3 IS NOT NULL);
135  five | Correlated Field 
136 ------+------------------
137       |                2
138       |                3
139       |                1
140       |                2
141       |                3
142 (5 rows)
143
144 --
145 -- Use some existing tables in the regression test
146 --
147 SELECT '' AS eight, ss.f1 AS "Correlated Field", ss.f3 AS "Second Field"
148   FROM SUBSELECT_TBL ss
149   WHERE f1 NOT IN (SELECT f1+1 FROM INT4_TBL
150                    WHERE f1 != ss.f1 AND f1 < 2147483647);
151  eight | Correlated Field | Second Field 
152 -------+------------------+--------------
153        |                2 |            4
154        |                3 |            5
155        |                2 |            2
156        |                3 |            3
157        |                6 |            8
158        |                8 |             
159 (6 rows)
160
161 select q1, float8(count(*)) / (select count(*) from int8_tbl)
162 from int8_tbl group by q1 order by q1;
163         q1        | ?column? 
164 ------------------+----------
165               123 |      0.4
166  4567890123456789 |      0.6
167 (2 rows)
168
169 --
170 -- Test cases to catch unpleasant interactions between IN-join processing
171 -- and subquery pullup.
172 --
173 select count(*) from
174   (select 1 from tenk1 a
175    where unique1 IN (select hundred from tenk1 b)) ss;
176  count 
177 -------
178    100
179 (1 row)
180
181 select count(distinct ss.ten) from
182   (select ten from tenk1 a
183    where unique1 IN (select hundred from tenk1 b)) ss;
184  count 
185 -------
186     10
187 (1 row)
188
189 select count(*) from
190   (select 1 from tenk1 a
191    where unique1 IN (select distinct hundred from tenk1 b)) ss;
192  count 
193 -------
194    100
195 (1 row)
196
197 select count(distinct ss.ten) from
198   (select ten from tenk1 a
199    where unique1 IN (select distinct hundred from tenk1 b)) ss;
200  count 
201 -------
202     10
203 (1 row)
204
205 --
206 -- Test cases to check for overenthusiastic optimization of
207 -- "IN (SELECT DISTINCT ...)" and related cases.  Per example from
208 -- Luca Pireddu and Michael Fuhr.
209 --
210 CREATE TEMP TABLE foo (id integer);
211 CREATE TEMP TABLE bar (id1 integer, id2 integer);
212 INSERT INTO foo VALUES (1);
213 INSERT INTO bar VALUES (1, 1);
214 INSERT INTO bar VALUES (2, 2);
215 INSERT INTO bar VALUES (3, 1);
216 -- These cases require an extra level of distinct-ing above subquery s
217 SELECT * FROM foo WHERE id IN
218     (SELECT id2 FROM (SELECT DISTINCT id1, id2 FROM bar) AS s);
219  id 
220 ----
221   1
222 (1 row)
223
224 SELECT * FROM foo WHERE id IN
225     (SELECT id2 FROM (SELECT id1,id2 FROM bar GROUP BY id1,id2) AS s);
226  id 
227 ----
228   1
229 (1 row)
230
231 SELECT * FROM foo WHERE id IN
232     (SELECT id2 FROM (SELECT id1, id2 FROM bar UNION
233                       SELECT id1, id2 FROM bar) AS s);
234  id 
235 ----
236   1
237 (1 row)
238
239 -- These cases do not
240 SELECT * FROM foo WHERE id IN
241     (SELECT id2 FROM (SELECT DISTINCT ON (id2) id1, id2 FROM bar) AS s);
242  id 
243 ----
244   1
245 (1 row)
246
247 SELECT * FROM foo WHERE id IN
248     (SELECT id2 FROM (SELECT id2 FROM bar GROUP BY id2) AS s);
249  id 
250 ----
251   1
252 (1 row)
253
254 SELECT * FROM foo WHERE id IN
255     (SELECT id2 FROM (SELECT id2 FROM bar UNION
256                       SELECT id2 FROM bar) AS s);
257  id 
258 ----
259   1
260 (1 row)
261
262 --
263 -- Test case to catch problems with multiply nested sub-SELECTs not getting
264 -- recalculated properly.  Per bug report from Didier Moens.
265 --
266 CREATE TABLE orderstest (
267     approver_ref integer,
268     po_ref integer,
269     ordercanceled boolean
270 );
271 INSERT INTO orderstest VALUES (1, 1, false);
272 INSERT INTO orderstest VALUES (66, 5, false);
273 INSERT INTO orderstest VALUES (66, 6, false);
274 INSERT INTO orderstest VALUES (66, 7, false);
275 INSERT INTO orderstest VALUES (66, 1, true);
276 INSERT INTO orderstest VALUES (66, 8, false);
277 INSERT INTO orderstest VALUES (66, 1, false);
278 INSERT INTO orderstest VALUES (77, 1, false);
279 INSERT INTO orderstest VALUES (1, 1, false);
280 INSERT INTO orderstest VALUES (66, 1, false);
281 INSERT INTO orderstest VALUES (1, 1, false);
282 CREATE VIEW orders_view AS
283 SELECT *,
284 (SELECT CASE
285    WHEN ord.approver_ref=1 THEN '---' ELSE 'Approved'
286  END) AS "Approved",
287 (SELECT CASE
288  WHEN ord.ordercanceled
289  THEN 'Canceled'
290  ELSE
291   (SELECT CASE
292                 WHEN ord.po_ref=1
293                 THEN
294                  (SELECT CASE
295                                 WHEN ord.approver_ref=1
296                                 THEN '---'
297                                 ELSE 'Approved'
298                         END)
299                 ELSE 'PO'
300         END)
301 END) AS "Status",
302 (CASE
303  WHEN ord.ordercanceled
304  THEN 'Canceled'
305  ELSE
306   (CASE
307                 WHEN ord.po_ref=1
308                 THEN
309                  (CASE
310                                 WHEN ord.approver_ref=1
311                                 THEN '---'
312                                 ELSE 'Approved'
313                         END)
314                 ELSE 'PO'
315         END)
316 END) AS "Status_OK"
317 FROM orderstest ord;
318 SELECT * FROM orders_view;
319  approver_ref | po_ref | ordercanceled | Approved |  Status  | Status_OK 
320 --------------+--------+---------------+----------+----------+-----------
321             1 |      1 | f             | ---      | ---      | ---
322            66 |      5 | f             | Approved | PO       | PO
323            66 |      6 | f             | Approved | PO       | PO
324            66 |      7 | f             | Approved | PO       | PO
325            66 |      1 | t             | Approved | Canceled | Canceled
326            66 |      8 | f             | Approved | PO       | PO
327            66 |      1 | f             | Approved | Approved | Approved
328            77 |      1 | f             | Approved | Approved | Approved
329             1 |      1 | f             | ---      | ---      | ---
330            66 |      1 | f             | Approved | Approved | Approved
331             1 |      1 | f             | ---      | ---      | ---
332 (11 rows)
333
334 DROP TABLE orderstest cascade;
335 NOTICE:  drop cascades to view orders_view
336 --
337 -- Test cases to catch situations where rule rewriter fails to propagate
338 -- hasSubLinks flag correctly.  Per example from Kyle Bateman.
339 --
340 create temp table parts (
341     partnum     text,
342     cost        float8
343 );
344 create temp table shipped (
345     ttype       char(2),
346     ordnum      int4,
347     partnum     text,
348     value       float8
349 );
350 create temp view shipped_view as
351     select * from shipped where ttype = 'wt';
352 create rule shipped_view_insert as on insert to shipped_view do instead
353     insert into shipped values('wt', new.ordnum, new.partnum, new.value);
354 insert into parts (partnum, cost) values (1, 1234.56);
355 insert into shipped_view (ordnum, partnum, value)
356     values (0, 1, (select cost from parts where partnum = '1'));
357 select * from shipped_view;
358  ttype | ordnum | partnum |  value  
359 -------+--------+---------+---------
360  wt    |      0 | 1       | 1234.56
361 (1 row)
362
363 create rule shipped_view_update as on update to shipped_view do instead
364     update shipped set partnum = new.partnum, value = new.value
365         where ttype = new.ttype and ordnum = new.ordnum;
366 update shipped_view set value = 11
367     from int4_tbl a join int4_tbl b
368       on (a.f1 = (select f1 from int4_tbl c where c.f1=b.f1))
369     where ordnum = a.f1;
370 select * from shipped_view;
371  ttype | ordnum | partnum | value 
372 -------+--------+---------+-------
373  wt    |      0 | 1       |    11
374 (1 row)
375
376 select f1, ss1 as relabel from
377     (select *, (select sum(f1) from int4_tbl b where f1 >= a.f1) as ss1
378      from int4_tbl a) ss;
379      f1      |  relabel   
380 -------------+------------
381            0 | 2147607103
382       123456 | 2147607103
383      -123456 | 2147483647
384   2147483647 | 2147483647
385  -2147483647 |          0
386 (5 rows)
387
388 --
389 -- Test cases involving PARAM_EXEC parameters and min/max index optimizations.
390 -- Per bug report from David Sanchez i Gregori.
391 --
392 select * from (
393   select max(unique1) from tenk1 as a
394   where exists (select 1 from tenk1 as b where b.thousand = a.unique2)
395 ) ss;
396  max  
397 ------
398  9997
399 (1 row)
400
401 select * from (
402   select min(unique1) from tenk1 as a
403   where not exists (select 1 from tenk1 as b where b.unique2 = 10000)
404 ) ss;
405  min 
406 -----
407    0
408 (1 row)
409
410 --
411 -- Test that an IN implemented using a UniquePath does unique-ification
412 -- with the right semantics, as per bug #4113.  (Unfortunately we have
413 -- no simple way to ensure that this test case actually chooses that type
414 -- of plan, but it does in releases 7.4-8.3.  Note that an ordering difference
415 -- here might mean that some other plan type is being used, rendering the test
416 -- pointless.)
417 --
418 create temp table numeric_table (num_col numeric);
419 insert into numeric_table values (1), (1.000000000000000000001), (2), (3);
420 create temp table float_table (float_col float8);
421 insert into float_table values (1), (2), (3);
422 select * from float_table
423   where float_col in (select num_col from numeric_table);
424  float_col 
425 -----------
426          1
427          2
428          3
429 (3 rows)
430
431 select * from numeric_table
432   where num_col in (select float_col from float_table);
433          num_col         
434 -------------------------
435                        1
436  1.000000000000000000001
437                        2
438                        3
439 (4 rows)
440
441 --
442 -- Test case for bug #4290: bogus calculation of subplan param sets
443 --
444 create temp table ta (id int primary key, val int);
445 insert into ta values(1,1);
446 insert into ta values(2,2);
447 create temp table tb (id int primary key, aval int);
448 insert into tb values(1,1);
449 insert into tb values(2,1);
450 insert into tb values(3,2);
451 insert into tb values(4,2);
452 create temp table tc (id int primary key, aid int);
453 insert into tc values(1,1);
454 insert into tc values(2,2);
455 select
456   ( select min(tb.id) from tb
457     where tb.aval = (select ta.val from ta where ta.id = tc.aid) ) as min_tb_id
458 from tc;
459  min_tb_id 
460 -----------
461          1
462          3
463 (2 rows)
464
465 --
466 -- Test case for 8.3 "failed to locate grouping columns" bug
467 --
468 create temp table t1 (f1 numeric(14,0), f2 varchar(30));
469 select * from
470   (select distinct f1, f2, (select f2 from t1 x where x.f1 = up.f1) as fs
471    from t1 up) ss
472 group by f1,f2,fs;
473  f1 | f2 | fs 
474 ----+----+----
475 (0 rows)
476
477 --
478 -- Test case for bug #5514 (mishandling of whole-row Vars in subselects)
479 --
480 create temp table table_a(id integer);
481 insert into table_a values (42);
482 create temp view view_a as select * from table_a;
483 select view_a from view_a;
484  view_a 
485 --------
486  (42)
487 (1 row)
488
489 select (select view_a) from view_a;
490  view_a 
491 --------
492  (42)
493 (1 row)
494
495 select (select (select view_a)) from view_a;
496  view_a 
497 --------
498  (42)
499 (1 row)
500
501 select (select (a.*)::text) from view_a a;
502   a   
503 ------
504  (42)
505 (1 row)
506
507 --
508 -- Check that whole-row Vars reading the result of a subselect don't include
509 -- any junk columns therein
510 --
511 select q from (select max(f1) from int4_tbl group by f1 order by f1) q;
512        q       
513 ---------------
514  (-2147483647)
515  (-123456)
516  (0)
517  (123456)
518  (2147483647)
519 (5 rows)
520
521 with q as (select max(f1) from int4_tbl group by f1 order by f1)
522   select q from q;
523        q       
524 ---------------
525  (-2147483647)
526  (-123456)
527  (0)
528  (123456)
529  (2147483647)
530 (5 rows)
531
532 --
533 -- Test case for sublinks pushed down into subselects via join alias expansion
534 --
535 select
536   (select sq1) as qq1
537 from
538   (select exists(select 1 from int4_tbl where f1 = q2) as sq1, 42 as dummy
539    from int8_tbl) sq0
540   join
541   int4_tbl i4 on dummy = i4.f1;
542  qq1 
543 -----
544 (0 rows)
545
546 --
547 -- Test case for premature memory release during hashing of subplan output
548 --
549 select '1'::text in (select '1'::name union all select '1'::name);
550  ?column? 
551 ----------
552  t
553 (1 row)
554
555 --
556 -- Test case for planner bug with nested EXISTS handling
557 --
558 select a.thousand from tenk1 a, tenk1 b
559 where a.thousand = b.thousand
560   and exists ( select 1 from tenk1 c where b.hundred = c.hundred
561                    and not exists ( select 1 from tenk1 d
562                                     where a.thousand = d.thousand ) );
563  thousand 
564 ----------
565 (0 rows)
566