]> granicus.if.org Git - postgresql/blob - src/test/regress/expected/union.out
Don't allow logging in with empty password.
[postgresql] / src / test / regress / expected / union.out
1 --
2 -- UNION (also INTERSECT, EXCEPT)
3 --
4 -- Simple UNION constructs
5 SELECT 1 AS two UNION SELECT 2 ORDER BY 1;
6  two 
7 -----
8    1
9    2
10 (2 rows)
11
12 SELECT 1 AS one UNION SELECT 1 ORDER BY 1;
13  one 
14 -----
15    1
16 (1 row)
17
18 SELECT 1 AS two UNION ALL SELECT 2;
19  two 
20 -----
21    1
22    2
23 (2 rows)
24
25 SELECT 1 AS two UNION ALL SELECT 1;
26  two 
27 -----
28    1
29    1
30 (2 rows)
31
32 SELECT 1 AS three UNION SELECT 2 UNION SELECT 3 ORDER BY 1;
33  three 
34 -------
35      1
36      2
37      3
38 (3 rows)
39
40 SELECT 1 AS two UNION SELECT 2 UNION SELECT 2 ORDER BY 1;
41  two 
42 -----
43    1
44    2
45 (2 rows)
46
47 SELECT 1 AS three UNION SELECT 2 UNION ALL SELECT 2 ORDER BY 1;
48  three 
49 -------
50      1
51      2
52      2
53 (3 rows)
54
55 SELECT 1.1 AS two UNION SELECT 2.2 ORDER BY 1;
56  two 
57 -----
58  1.1
59  2.2
60 (2 rows)
61
62 -- Mixed types
63 SELECT 1.1 AS two UNION SELECT 2 ORDER BY 1;
64  two 
65 -----
66  1.1
67    2
68 (2 rows)
69
70 SELECT 1 AS two UNION SELECT 2.2 ORDER BY 1;
71  two 
72 -----
73    1
74  2.2
75 (2 rows)
76
77 SELECT 1 AS one UNION SELECT 1.0::float8 ORDER BY 1;
78  one 
79 -----
80    1
81 (1 row)
82
83 SELECT 1.1 AS two UNION ALL SELECT 2 ORDER BY 1;
84  two 
85 -----
86  1.1
87    2
88 (2 rows)
89
90 SELECT 1.0::float8 AS two UNION ALL SELECT 1 ORDER BY 1;
91  two 
92 -----
93    1
94    1
95 (2 rows)
96
97 SELECT 1.1 AS three UNION SELECT 2 UNION SELECT 3 ORDER BY 1;
98  three 
99 -------
100    1.1
101      2
102      3
103 (3 rows)
104
105 SELECT 1.1::float8 AS two UNION SELECT 2 UNION SELECT 2.0::float8 ORDER BY 1;
106  two 
107 -----
108  1.1
109    2
110 (2 rows)
111
112 SELECT 1.1 AS three UNION SELECT 2 UNION ALL SELECT 2 ORDER BY 1;
113  three 
114 -------
115    1.1
116      2
117      2
118 (3 rows)
119
120 SELECT 1.1 AS two UNION (SELECT 2 UNION ALL SELECT 2) ORDER BY 1;
121  two 
122 -----
123  1.1
124    2
125 (2 rows)
126
127 --
128 -- Try testing from tables...
129 --
130 SELECT f1 AS five FROM FLOAT8_TBL
131 UNION
132 SELECT f1 FROM FLOAT8_TBL
133 ORDER BY 1;
134          five          
135 -----------------------
136  -1.2345678901234e+200
137                -1004.3
138                 -34.84
139  -1.2345678901234e-200
140                      0
141 (5 rows)
142
143 SELECT f1 AS ten FROM FLOAT8_TBL
144 UNION ALL
145 SELECT f1 FROM FLOAT8_TBL;
146           ten          
147 -----------------------
148                      0
149                 -34.84
150                -1004.3
151  -1.2345678901234e+200
152  -1.2345678901234e-200
153                      0
154                 -34.84
155                -1004.3
156  -1.2345678901234e+200
157  -1.2345678901234e-200
158 (10 rows)
159
160 SELECT f1 AS nine FROM FLOAT8_TBL
161 UNION
162 SELECT f1 FROM INT4_TBL
163 ORDER BY 1;
164          nine          
165 -----------------------
166  -1.2345678901234e+200
167            -2147483647
168                -123456
169                -1004.3
170                 -34.84
171  -1.2345678901234e-200
172                      0
173                 123456
174             2147483647
175 (9 rows)
176
177 SELECT f1 AS ten FROM FLOAT8_TBL
178 UNION ALL
179 SELECT f1 FROM INT4_TBL;
180           ten          
181 -----------------------
182                      0
183                 -34.84
184                -1004.3
185  -1.2345678901234e+200
186  -1.2345678901234e-200
187                      0
188                 123456
189                -123456
190             2147483647
191            -2147483647
192 (10 rows)
193
194 SELECT f1 AS five FROM FLOAT8_TBL
195   WHERE f1 BETWEEN -1e6 AND 1e6
196 UNION
197 SELECT f1 FROM INT4_TBL
198   WHERE f1 BETWEEN 0 AND 1000000
199 ORDER BY 1;
200          five          
201 -----------------------
202                -1004.3
203                 -34.84
204  -1.2345678901234e-200
205                      0
206                 123456
207 (5 rows)
208
209 SELECT CAST(f1 AS char(4)) AS three FROM VARCHAR_TBL
210 UNION
211 SELECT f1 FROM CHAR_TBL
212 ORDER BY 1;
213  three 
214 -------
215  a   
216  ab  
217  abcd
218 (3 rows)
219
220 SELECT f1 AS three FROM VARCHAR_TBL
221 UNION
222 SELECT CAST(f1 AS varchar) FROM CHAR_TBL
223 ORDER BY 1;
224  three 
225 -------
226  a
227  ab
228  abcd
229 (3 rows)
230
231 SELECT f1 AS eight FROM VARCHAR_TBL
232 UNION ALL
233 SELECT f1 FROM CHAR_TBL;
234  eight 
235 -------
236  a
237  ab
238  abcd
239  abcd
240  a
241  ab
242  abcd
243  abcd
244 (8 rows)
245
246 SELECT f1 AS five FROM TEXT_TBL
247 UNION
248 SELECT f1 FROM VARCHAR_TBL
249 UNION
250 SELECT TRIM(TRAILING FROM f1) FROM CHAR_TBL
251 ORDER BY 1;
252        five        
253 -------------------
254  a
255  ab
256  abcd
257  doh!
258  hi de ho neighbor
259 (5 rows)
260
261 --
262 -- INTERSECT and EXCEPT
263 --
264 SELECT q2 FROM int8_tbl INTERSECT SELECT q1 FROM int8_tbl ORDER BY 1;
265         q2        
266 ------------------
267               123
268  4567890123456789
269 (2 rows)
270
271 SELECT q2 FROM int8_tbl INTERSECT ALL SELECT q1 FROM int8_tbl ORDER BY 1;
272         q2        
273 ------------------
274               123
275  4567890123456789
276  4567890123456789
277 (3 rows)
278
279 SELECT q2 FROM int8_tbl EXCEPT SELECT q1 FROM int8_tbl ORDER BY 1;
280         q2         
281 -------------------
282  -4567890123456789
283                456
284 (2 rows)
285
286 SELECT q2 FROM int8_tbl EXCEPT ALL SELECT q1 FROM int8_tbl ORDER BY 1;
287         q2         
288 -------------------
289  -4567890123456789
290                456
291 (2 rows)
292
293 SELECT q2 FROM int8_tbl EXCEPT ALL SELECT DISTINCT q1 FROM int8_tbl ORDER BY 1;
294         q2         
295 -------------------
296  -4567890123456789
297                456
298   4567890123456789
299 (3 rows)
300
301 SELECT q1 FROM int8_tbl EXCEPT SELECT q2 FROM int8_tbl ORDER BY 1;
302  q1 
303 ----
304 (0 rows)
305
306 SELECT q1 FROM int8_tbl EXCEPT ALL SELECT q2 FROM int8_tbl ORDER BY 1;
307         q1        
308 ------------------
309               123
310  4567890123456789
311 (2 rows)
312
313 SELECT q1 FROM int8_tbl EXCEPT ALL SELECT DISTINCT q2 FROM int8_tbl ORDER BY 1;
314         q1        
315 ------------------
316               123
317  4567890123456789
318  4567890123456789
319 (3 rows)
320
321 SELECT q1 FROM int8_tbl EXCEPT ALL SELECT q1 FROM int8_tbl FOR NO KEY UPDATE;
322 ERROR:  FOR NO KEY UPDATE is not allowed with UNION/INTERSECT/EXCEPT
323 -- nested cases
324 (SELECT 1,2,3 UNION SELECT 4,5,6) INTERSECT SELECT 4,5,6;
325  ?column? | ?column? | ?column? 
326 ----------+----------+----------
327         4 |        5 |        6
328 (1 row)
329
330 (SELECT 1,2,3 UNION SELECT 4,5,6 ORDER BY 1,2) INTERSECT SELECT 4,5,6;
331  ?column? | ?column? | ?column? 
332 ----------+----------+----------
333         4 |        5 |        6
334 (1 row)
335
336 (SELECT 1,2,3 UNION SELECT 4,5,6) EXCEPT SELECT 4,5,6;
337  ?column? | ?column? | ?column? 
338 ----------+----------+----------
339         1 |        2 |        3
340 (1 row)
341
342 (SELECT 1,2,3 UNION SELECT 4,5,6 ORDER BY 1,2) EXCEPT SELECT 4,5,6;
343  ?column? | ?column? | ?column? 
344 ----------+----------+----------
345         1 |        2 |        3
346 (1 row)
347
348 --
349 -- Mixed types
350 --
351 SELECT f1 FROM float8_tbl INTERSECT SELECT f1 FROM int4_tbl ORDER BY 1;
352  f1 
353 ----
354   0
355 (1 row)
356
357 SELECT f1 FROM float8_tbl EXCEPT SELECT f1 FROM int4_tbl ORDER BY 1;
358           f1           
359 -----------------------
360  -1.2345678901234e+200
361                -1004.3
362                 -34.84
363  -1.2345678901234e-200
364 (4 rows)
365
366 --
367 -- Operator precedence and (((((extra))))) parentheses
368 --
369 SELECT q1 FROM int8_tbl INTERSECT SELECT q2 FROM int8_tbl UNION ALL SELECT q2 FROM int8_tbl  ORDER BY 1;
370         q1         
371 -------------------
372  -4567890123456789
373                123
374                123
375                456
376   4567890123456789
377   4567890123456789
378   4567890123456789
379 (7 rows)
380
381 SELECT q1 FROM int8_tbl INTERSECT (((SELECT q2 FROM int8_tbl UNION ALL SELECT q2 FROM int8_tbl))) ORDER BY 1;
382         q1        
383 ------------------
384               123
385  4567890123456789
386 (2 rows)
387
388 (((SELECT q1 FROM int8_tbl INTERSECT SELECT q2 FROM int8_tbl ORDER BY 1))) UNION ALL SELECT q2 FROM int8_tbl;
389         q1         
390 -------------------
391                123
392   4567890123456789
393                456
394   4567890123456789
395                123
396   4567890123456789
397  -4567890123456789
398 (7 rows)
399
400 SELECT q1 FROM int8_tbl UNION ALL SELECT q2 FROM int8_tbl EXCEPT SELECT q1 FROM int8_tbl ORDER BY 1;
401         q1         
402 -------------------
403  -4567890123456789
404                456
405 (2 rows)
406
407 SELECT q1 FROM int8_tbl UNION ALL (((SELECT q2 FROM int8_tbl EXCEPT SELECT q1 FROM int8_tbl ORDER BY 1)));
408         q1         
409 -------------------
410                123
411                123
412   4567890123456789
413   4567890123456789
414   4567890123456789
415  -4567890123456789
416                456
417 (7 rows)
418
419 (((SELECT q1 FROM int8_tbl UNION ALL SELECT q2 FROM int8_tbl))) EXCEPT SELECT q1 FROM int8_tbl ORDER BY 1;
420         q1         
421 -------------------
422  -4567890123456789
423                456
424 (2 rows)
425
426 --
427 -- Subqueries with ORDER BY & LIMIT clauses
428 --
429 -- In this syntax, ORDER BY/LIMIT apply to the result of the EXCEPT
430 SELECT q1,q2 FROM int8_tbl EXCEPT SELECT q2,q1 FROM int8_tbl
431 ORDER BY q2,q1;
432         q1        |        q2         
433 ------------------+-------------------
434  4567890123456789 | -4567890123456789
435               123 |               456
436 (2 rows)
437
438 -- This should fail, because q2 isn't a name of an EXCEPT output column
439 SELECT q1 FROM int8_tbl EXCEPT SELECT q2 FROM int8_tbl ORDER BY q2 LIMIT 1;
440 ERROR:  column "q2" does not exist
441 LINE 1: ... int8_tbl EXCEPT SELECT q2 FROM int8_tbl ORDER BY q2 LIMIT 1...
442                                                              ^
443 HINT:  There is a column named "q2" in table "*SELECT* 2", but it cannot be referenced from this part of the query.
444 -- But this should work:
445 SELECT q1 FROM int8_tbl EXCEPT (((SELECT q2 FROM int8_tbl ORDER BY q2 LIMIT 1))) ORDER BY 1;
446         q1        
447 ------------------
448               123
449  4567890123456789
450 (2 rows)
451
452 --
453 -- New syntaxes (7.1) permit new tests
454 --
455 (((((select * from int8_tbl)))));
456         q1        |        q2         
457 ------------------+-------------------
458               123 |               456
459               123 |  4567890123456789
460  4567890123456789 |               123
461  4567890123456789 |  4567890123456789
462  4567890123456789 | -4567890123456789
463 (5 rows)
464
465 --
466 -- Check handling of a case with unknown constants.  We don't guarantee
467 -- an undecorated constant will work in all cases, but historically this
468 -- usage has worked, so test we don't break it.
469 --
470 SELECT a.f1 FROM (SELECT 'test' AS f1 FROM varchar_tbl) a
471 UNION
472 SELECT b.f1 FROM (SELECT f1 FROM varchar_tbl) b
473 ORDER BY 1;
474   f1  
475 ------
476  a
477  ab
478  abcd
479  test
480 (4 rows)
481
482 -- This should fail, but it should produce an error cursor
483 SELECT '3.4'::numeric UNION SELECT 'foo';
484 ERROR:  invalid input syntax for type numeric: "foo"
485 LINE 1: SELECT '3.4'::numeric UNION SELECT 'foo';
486                                            ^
487 --
488 -- Test that expression-index constraints can be pushed down through
489 -- UNION or UNION ALL
490 --
491 CREATE TEMP TABLE t1 (a text, b text);
492 CREATE INDEX t1_ab_idx on t1 ((a || b));
493 CREATE TEMP TABLE t2 (ab text primary key);
494 INSERT INTO t1 VALUES ('a', 'b'), ('x', 'y');
495 INSERT INTO t2 VALUES ('ab'), ('xy');
496 set enable_seqscan = off;
497 set enable_indexscan = on;
498 set enable_bitmapscan = off;
499 explain (costs off)
500  SELECT * FROM
501  (SELECT a || b AS ab FROM t1
502   UNION ALL
503   SELECT * FROM t2) t
504  WHERE ab = 'ab';
505                  QUERY PLAN                  
506 ---------------------------------------------
507  Append
508    ->  Index Scan using t1_ab_idx on t1
509          Index Cond: ((a || b) = 'ab'::text)
510    ->  Index Only Scan using t2_pkey on t2
511          Index Cond: (ab = 'ab'::text)
512 (5 rows)
513
514 explain (costs off)
515  SELECT * FROM
516  (SELECT a || b AS ab FROM t1
517   UNION
518   SELECT * FROM t2) t
519  WHERE ab = 'ab';
520                     QUERY PLAN                     
521 ---------------------------------------------------
522  HashAggregate
523    Group Key: ((t1.a || t1.b))
524    ->  Append
525          ->  Index Scan using t1_ab_idx on t1
526                Index Cond: ((a || b) = 'ab'::text)
527          ->  Index Only Scan using t2_pkey on t2
528                Index Cond: (ab = 'ab'::text)
529 (7 rows)
530
531 --
532 -- Test that ORDER BY for UNION ALL can be pushed down to inheritance
533 -- children.
534 --
535 CREATE TEMP TABLE t1c (b text, a text);
536 ALTER TABLE t1c INHERIT t1;
537 CREATE TEMP TABLE t2c (primary key (ab)) INHERITS (t2);
538 INSERT INTO t1c VALUES ('v', 'w'), ('c', 'd'), ('m', 'n'), ('e', 'f');
539 INSERT INTO t2c VALUES ('vw'), ('cd'), ('mn'), ('ef');
540 CREATE INDEX t1c_ab_idx on t1c ((a || b));
541 set enable_seqscan = on;
542 set enable_indexonlyscan = off;
543 explain (costs off)
544   SELECT * FROM
545   (SELECT a || b AS ab FROM t1
546    UNION ALL
547    SELECT ab FROM t2) t
548   ORDER BY 1 LIMIT 8;
549                    QUERY PLAN                   
550 ------------------------------------------------
551  Limit
552    ->  Merge Append
553          Sort Key: ((t1.a || t1.b))
554          ->  Index Scan using t1_ab_idx on t1
555          ->  Index Scan using t1c_ab_idx on t1c
556          ->  Index Scan using t2_pkey on t2
557          ->  Index Scan using t2c_pkey on t2c
558 (7 rows)
559
560   SELECT * FROM
561   (SELECT a || b AS ab FROM t1
562    UNION ALL
563    SELECT ab FROM t2) t
564   ORDER BY 1 LIMIT 8;
565  ab 
566 ----
567  ab
568  ab
569  cd
570  dc
571  ef
572  fe
573  mn
574  nm
575 (8 rows)
576
577 reset enable_seqscan;
578 reset enable_indexscan;
579 reset enable_bitmapscan;
580 -- This simpler variant of the above test has been observed to fail differently
581 create table events (event_id int primary key);
582 create table other_events (event_id int primary key);
583 create table events_child () inherits (events);
584 explain (costs off)
585 select event_id
586  from (select event_id from events
587        union all
588        select event_id from other_events) ss
589  order by event_id;
590                         QUERY PLAN                        
591 ----------------------------------------------------------
592  Merge Append
593    Sort Key: events.event_id
594    ->  Index Scan using events_pkey on events
595    ->  Sort
596          Sort Key: events_child.event_id
597          ->  Seq Scan on events_child
598    ->  Index Scan using other_events_pkey on other_events
599 (7 rows)
600
601 drop table events_child, events, other_events;
602 reset enable_indexonlyscan;
603 -- Test constraint exclusion of UNION ALL subqueries
604 explain (costs off)
605  SELECT * FROM
606   (SELECT 1 AS t, * FROM tenk1 a
607    UNION ALL
608    SELECT 2 AS t, * FROM tenk1 b) c
609  WHERE t = 2;
610         QUERY PLAN         
611 ---------------------------
612  Append
613    ->  Seq Scan on tenk1 b
614 (2 rows)
615
616 -- Test that we push quals into UNION sub-selects only when it's safe
617 explain (costs off)
618 SELECT * FROM
619   (SELECT 1 AS t, 2 AS x
620    UNION
621    SELECT 2 AS t, 4 AS x) ss
622 WHERE x < 4
623 ORDER BY x;
624                     QUERY PLAN                    
625 --------------------------------------------------
626  Sort
627    Sort Key: (2)
628    ->  Unique
629          ->  Sort
630                Sort Key: (1), (2)
631                ->  Append
632                      ->  Result
633                      ->  Result
634                            One-Time Filter: false
635 (9 rows)
636
637 SELECT * FROM
638   (SELECT 1 AS t, 2 AS x
639    UNION
640    SELECT 2 AS t, 4 AS x) ss
641 WHERE x < 4
642 ORDER BY x;
643  t | x 
644 ---+---
645  1 | 2
646 (1 row)
647
648 explain (costs off)
649 SELECT * FROM
650   (SELECT 1 AS t, generate_series(1,10) AS x
651    UNION
652    SELECT 2 AS t, 4 AS x) ss
653 WHERE x < 4
654 ORDER BY x;
655                        QUERY PLAN                       
656 --------------------------------------------------------
657  Sort
658    Sort Key: ss.x
659    ->  Subquery Scan on ss
660          Filter: (ss.x < 4)
661          ->  HashAggregate
662                Group Key: (1), (generate_series(1, 10))
663                ->  Append
664                      ->  ProjectSet
665                            ->  Result
666                      ->  Result
667 (10 rows)
668
669 SELECT * FROM
670   (SELECT 1 AS t, generate_series(1,10) AS x
671    UNION
672    SELECT 2 AS t, 4 AS x) ss
673 WHERE x < 4
674 ORDER BY x;
675  t | x 
676 ---+---
677  1 | 1
678  1 | 2
679  1 | 3
680 (3 rows)
681
682 explain (costs off)
683 SELECT * FROM
684   (SELECT 1 AS t, (random()*3)::int AS x
685    UNION
686    SELECT 2 AS t, 4 AS x) ss
687 WHERE x > 3
688 ORDER BY x;
689                                      QUERY PLAN                                     
690 ------------------------------------------------------------------------------------
691  Sort
692    Sort Key: ss.x
693    ->  Subquery Scan on ss
694          Filter: (ss.x > 3)
695          ->  Unique
696                ->  Sort
697                      Sort Key: (1), (((random() * '3'::double precision))::integer)
698                      ->  Append
699                            ->  Result
700                            ->  Result
701 (10 rows)
702
703 SELECT * FROM
704   (SELECT 1 AS t, (random()*3)::int AS x
705    UNION
706    SELECT 2 AS t, 4 AS x) ss
707 WHERE x > 3
708 ORDER BY x;
709  t | x 
710 ---+---
711  2 | 4
712 (1 row)
713
714 -- Test proper handling of parameterized appendrel paths when the
715 -- potential join qual is expensive
716 create function expensivefunc(int) returns int
717 language plpgsql immutable strict cost 10000
718 as $$begin return $1; end$$;
719 create temp table t3 as select generate_series(-1000,1000) as x;
720 create index t3i on t3 (expensivefunc(x));
721 analyze t3;
722 explain (costs off)
723 select * from
724   (select * from t3 a union all select * from t3 b) ss
725   join int4_tbl on f1 = expensivefunc(x);
726                          QUERY PLAN                         
727 ------------------------------------------------------------
728  Nested Loop
729    ->  Seq Scan on int4_tbl
730    ->  Append
731          ->  Index Scan using t3i on t3 a
732                Index Cond: (expensivefunc(x) = int4_tbl.f1)
733          ->  Index Scan using t3i on t3 b
734                Index Cond: (expensivefunc(x) = int4_tbl.f1)
735 (7 rows)
736
737 select * from
738   (select * from t3 a union all select * from t3 b) ss
739   join int4_tbl on f1 = expensivefunc(x);
740  x | f1 
741 ---+----
742  0 |  0
743  0 |  0
744 (2 rows)
745
746 drop table t3;
747 drop function expensivefunc(int);
748 -- Test handling of appendrel quals that const-simplify into an AND
749 explain (costs off)
750 select * from
751   (select *, 0 as x from int8_tbl a
752    union all
753    select *, 1 as x from int8_tbl b) ss
754 where (x = 0) or (q1 >= q2 and q1 <= q2);
755                  QUERY PLAN                  
756 ---------------------------------------------
757  Append
758    ->  Seq Scan on int8_tbl a
759    ->  Seq Scan on int8_tbl b
760          Filter: ((q1 >= q2) AND (q1 <= q2))
761 (4 rows)
762
763 select * from
764   (select *, 0 as x from int8_tbl a
765    union all
766    select *, 1 as x from int8_tbl b) ss
767 where (x = 0) or (q1 >= q2 and q1 <= q2);
768         q1        |        q2         | x 
769 ------------------+-------------------+---
770               123 |               456 | 0
771               123 |  4567890123456789 | 0
772  4567890123456789 |               123 | 0
773  4567890123456789 |  4567890123456789 | 0
774  4567890123456789 | -4567890123456789 | 0
775  4567890123456789 |  4567890123456789 | 1
776 (6 rows)
777