]> granicus.if.org Git - postgresql/blob - src/test/regress/expected/aggregates.out
Perform line wrapping and indenting by default in ruleutils.c.
[postgresql] / src / test / regress / expected / aggregates.out
1 --
2 -- AGGREGATES
3 --
4 SELECT avg(four) AS avg_1 FROM onek;
5        avg_1        
6 --------------------
7  1.5000000000000000
8 (1 row)
9
10 SELECT avg(a) AS avg_32 FROM aggtest WHERE a < 100;
11        avg_32        
12 ---------------------
13  32.6666666666666667
14 (1 row)
15
16 -- In 7.1, avg(float4) is computed using float8 arithmetic.
17 -- Round the result to 3 digits to avoid platform-specific results.
18 SELECT avg(b)::numeric(10,3) AS avg_107_943 FROM aggtest;
19  avg_107_943 
20 -------------
21      107.943
22 (1 row)
23
24 SELECT avg(gpa) AS avg_3_4 FROM ONLY student;
25  avg_3_4 
26 ---------
27      3.4
28 (1 row)
29
30 SELECT sum(four) AS sum_1500 FROM onek;
31  sum_1500 
32 ----------
33      1500
34 (1 row)
35
36 SELECT sum(a) AS sum_198 FROM aggtest;
37  sum_198 
38 ---------
39      198
40 (1 row)
41
42 SELECT sum(b) AS avg_431_773 FROM aggtest;
43  avg_431_773 
44 -------------
45      431.773
46 (1 row)
47
48 SELECT sum(gpa) AS avg_6_8 FROM ONLY student;
49  avg_6_8 
50 ---------
51      6.8
52 (1 row)
53
54 SELECT max(four) AS max_3 FROM onek;
55  max_3 
56 -------
57      3
58 (1 row)
59
60 SELECT max(a) AS max_100 FROM aggtest;
61  max_100 
62 ---------
63      100
64 (1 row)
65
66 SELECT max(aggtest.b) AS max_324_78 FROM aggtest;
67  max_324_78 
68 ------------
69      324.78
70 (1 row)
71
72 SELECT max(student.gpa) AS max_3_7 FROM student;
73  max_3_7 
74 ---------
75      3.7
76 (1 row)
77
78 SELECT stddev_pop(b) FROM aggtest;
79    stddev_pop    
80 -----------------
81  131.10703231895
82 (1 row)
83
84 SELECT stddev_samp(b) FROM aggtest;
85    stddev_samp    
86 ------------------
87  151.389360803998
88 (1 row)
89
90 SELECT var_pop(b) FROM aggtest;
91      var_pop      
92 ------------------
93  17189.0539234823
94 (1 row)
95
96 SELECT var_samp(b) FROM aggtest;
97      var_samp     
98 ------------------
99  22918.7385646431
100 (1 row)
101
102 SELECT stddev_pop(b::numeric) FROM aggtest;
103     stddev_pop    
104 ------------------
105  131.107032862199
106 (1 row)
107
108 SELECT stddev_samp(b::numeric) FROM aggtest;
109    stddev_samp    
110 ------------------
111  151.389361431288
112 (1 row)
113
114 SELECT var_pop(b::numeric) FROM aggtest;
115       var_pop       
116 --------------------
117  17189.054065929769
118 (1 row)
119
120 SELECT var_samp(b::numeric) FROM aggtest;
121       var_samp      
122 --------------------
123  22918.738754573025
124 (1 row)
125
126 -- population variance is defined for a single tuple, sample variance
127 -- is not
128 SELECT var_pop(1.0), var_samp(2.0);
129  var_pop | var_samp 
130 ---------+----------
131        0 |         
132 (1 row)
133
134 SELECT stddev_pop(3.0::numeric), stddev_samp(4.0::numeric);
135  stddev_pop | stddev_samp 
136 ------------+-------------
137           0 |            
138 (1 row)
139
140 -- SQL2003 binary aggregates
141 SELECT regr_count(b, a) FROM aggtest;
142  regr_count 
143 ------------
144           4
145 (1 row)
146
147 SELECT regr_sxx(b, a) FROM aggtest;
148  regr_sxx 
149 ----------
150      5099
151 (1 row)
152
153 SELECT regr_syy(b, a) FROM aggtest;
154      regr_syy     
155 ------------------
156  68756.2156939293
157 (1 row)
158
159 SELECT regr_sxy(b, a) FROM aggtest;
160      regr_sxy     
161 ------------------
162  2614.51582155004
163 (1 row)
164
165 SELECT regr_avgx(b, a), regr_avgy(b, a) FROM aggtest;
166  regr_avgx |    regr_avgy     
167 -----------+------------------
168       49.5 | 107.943152273074
169 (1 row)
170
171 SELECT regr_r2(b, a) FROM aggtest;
172       regr_r2       
173 --------------------
174  0.0194977982031803
175 (1 row)
176
177 SELECT regr_slope(b, a), regr_intercept(b, a) FROM aggtest;
178     regr_slope     |  regr_intercept  
179 -------------------+------------------
180  0.512750700441271 | 82.5619926012309
181 (1 row)
182
183 SELECT covar_pop(b, a), covar_samp(b, a) FROM aggtest;
184     covar_pop    |    covar_samp    
185 -----------------+------------------
186  653.62895538751 | 871.505273850014
187 (1 row)
188
189 SELECT corr(b, a) FROM aggtest;
190        corr        
191 -------------------
192  0.139634516517873
193 (1 row)
194
195 SELECT count(four) AS cnt_1000 FROM onek;
196  cnt_1000 
197 ----------
198      1000
199 (1 row)
200
201 SELECT count(DISTINCT four) AS cnt_4 FROM onek;
202  cnt_4 
203 -------
204      4
205 (1 row)
206
207 select ten, count(*), sum(four) from onek
208 group by ten order by ten;
209  ten | count | sum 
210 -----+-------+-----
211    0 |   100 | 100
212    1 |   100 | 200
213    2 |   100 | 100
214    3 |   100 | 200
215    4 |   100 | 100
216    5 |   100 | 200
217    6 |   100 | 100
218    7 |   100 | 200
219    8 |   100 | 100
220    9 |   100 | 200
221 (10 rows)
222
223 select ten, count(four), sum(DISTINCT four) from onek
224 group by ten order by ten;
225  ten | count | sum 
226 -----+-------+-----
227    0 |   100 |   2
228    1 |   100 |   4
229    2 |   100 |   2
230    3 |   100 |   4
231    4 |   100 |   2
232    5 |   100 |   4
233    6 |   100 |   2
234    7 |   100 |   4
235    8 |   100 |   2
236    9 |   100 |   4
237 (10 rows)
238
239 -- user-defined aggregates
240 SELECT newavg(four) AS avg_1 FROM onek;
241        avg_1        
242 --------------------
243  1.5000000000000000
244 (1 row)
245
246 SELECT newsum(four) AS sum_1500 FROM onek;
247  sum_1500 
248 ----------
249      1500
250 (1 row)
251
252 SELECT newcnt(four) AS cnt_1000 FROM onek;
253  cnt_1000 
254 ----------
255      1000
256 (1 row)
257
258 SELECT newcnt(*) AS cnt_1000 FROM onek;
259  cnt_1000 
260 ----------
261      1000
262 (1 row)
263
264 SELECT oldcnt(*) AS cnt_1000 FROM onek;
265  cnt_1000 
266 ----------
267      1000
268 (1 row)
269
270 SELECT sum2(q1,q2) FROM int8_tbl;
271        sum2        
272 -------------------
273  18271560493827981
274 (1 row)
275
276 -- test for outer-level aggregates
277 -- this should work
278 select ten, sum(distinct four) from onek a
279 group by ten
280 having exists (select 1 from onek b where sum(distinct a.four) = b.four);
281  ten | sum 
282 -----+-----
283    0 |   2
284    2 |   2
285    4 |   2
286    6 |   2
287    8 |   2
288 (5 rows)
289
290 -- this should fail because subquery has an agg of its own in WHERE
291 select ten, sum(distinct four) from onek a
292 group by ten
293 having exists (select 1 from onek b
294                where sum(distinct a.four + b.four) = b.four);
295 ERROR:  aggregate functions are not allowed in WHERE
296 LINE 4:                where sum(distinct a.four + b.four) = b.four)...
297                              ^
298 -- Test handling of sublinks within outer-level aggregates.
299 -- Per bug report from Daniel Grace.
300 select
301   (select max((select i.unique2 from tenk1 i where i.unique1 = o.unique1)))
302 from tenk1 o;
303  max  
304 ------
305  9999
306 (1 row)
307
308 --
309 -- test for bitwise integer aggregates
310 --
311 CREATE TEMPORARY TABLE bitwise_test(
312   i2 INT2,
313   i4 INT4,
314   i8 INT8,
315   i INTEGER,
316   x INT2,
317   y BIT(4)
318 );
319 -- empty case
320 SELECT
321   BIT_AND(i2) AS "?",
322   BIT_OR(i4)  AS "?"
323 FROM bitwise_test;
324  ? | ? 
325 ---+---
326    |  
327 (1 row)
328
329 COPY bitwise_test FROM STDIN NULL 'null';
330 SELECT
331   BIT_AND(i2) AS "1",
332   BIT_AND(i4) AS "1",
333   BIT_AND(i8) AS "1",
334   BIT_AND(i)  AS "?",
335   BIT_AND(x)  AS "0",
336   BIT_AND(y)  AS "0100",
337   BIT_OR(i2)  AS "7",
338   BIT_OR(i4)  AS "7",
339   BIT_OR(i8)  AS "7",
340   BIT_OR(i)   AS "?",
341   BIT_OR(x)   AS "7",
342   BIT_OR(y)   AS "1101"
343 FROM bitwise_test;
344  1 | 1 | 1 | ? | 0 | 0100 | 7 | 7 | 7 | ? | 7 | 1101 
345 ---+---+---+---+---+------+---+---+---+---+---+------
346  1 | 1 | 1 | 1 | 0 | 0100 | 7 | 7 | 7 | 3 | 7 | 1101
347 (1 row)
348
349 --
350 -- test boolean aggregates
351 --
352 -- first test all possible transition and final states
353 SELECT
354   -- boolean and transitions
355   -- null because strict
356   booland_statefunc(NULL, NULL)  IS NULL AS "t",
357   booland_statefunc(TRUE, NULL)  IS NULL AS "t",
358   booland_statefunc(FALSE, NULL) IS NULL AS "t",
359   booland_statefunc(NULL, TRUE)  IS NULL AS "t",
360   booland_statefunc(NULL, FALSE) IS NULL AS "t",
361   -- and actual computations
362   booland_statefunc(TRUE, TRUE) AS "t",
363   NOT booland_statefunc(TRUE, FALSE) AS "t",
364   NOT booland_statefunc(FALSE, TRUE) AS "t",
365   NOT booland_statefunc(FALSE, FALSE) AS "t";
366  t | t | t | t | t | t | t | t | t 
367 ---+---+---+---+---+---+---+---+---
368  t | t | t | t | t | t | t | t | t
369 (1 row)
370
371 SELECT
372   -- boolean or transitions
373   -- null because strict
374   boolor_statefunc(NULL, NULL)  IS NULL AS "t",
375   boolor_statefunc(TRUE, NULL)  IS NULL AS "t",
376   boolor_statefunc(FALSE, NULL) IS NULL AS "t",
377   boolor_statefunc(NULL, TRUE)  IS NULL AS "t",
378   boolor_statefunc(NULL, FALSE) IS NULL AS "t",
379   -- actual computations
380   boolor_statefunc(TRUE, TRUE) AS "t",
381   boolor_statefunc(TRUE, FALSE) AS "t",
382   boolor_statefunc(FALSE, TRUE) AS "t",
383   NOT boolor_statefunc(FALSE, FALSE) AS "t";
384  t | t | t | t | t | t | t | t | t 
385 ---+---+---+---+---+---+---+---+---
386  t | t | t | t | t | t | t | t | t
387 (1 row)
388
389 CREATE TEMPORARY TABLE bool_test(
390   b1 BOOL,
391   b2 BOOL,
392   b3 BOOL,
393   b4 BOOL);
394 -- empty case
395 SELECT
396   BOOL_AND(b1)   AS "n",
397   BOOL_OR(b3)    AS "n"
398 FROM bool_test;
399  n | n 
400 ---+---
401    | 
402 (1 row)
403
404 COPY bool_test FROM STDIN NULL 'null';
405 SELECT
406   BOOL_AND(b1)     AS "f",
407   BOOL_AND(b2)     AS "t",
408   BOOL_AND(b3)     AS "f",
409   BOOL_AND(b4)     AS "n",
410   BOOL_AND(NOT b2) AS "f",
411   BOOL_AND(NOT b3) AS "t"
412 FROM bool_test;
413  f | t | f | n | f | t 
414 ---+---+---+---+---+---
415  f | t | f |   | f | t
416 (1 row)
417
418 SELECT
419   EVERY(b1)     AS "f",
420   EVERY(b2)     AS "t",
421   EVERY(b3)     AS "f",
422   EVERY(b4)     AS "n",
423   EVERY(NOT b2) AS "f",
424   EVERY(NOT b3) AS "t"
425 FROM bool_test;
426  f | t | f | n | f | t 
427 ---+---+---+---+---+---
428  f | t | f |   | f | t
429 (1 row)
430
431 SELECT
432   BOOL_OR(b1)      AS "t",
433   BOOL_OR(b2)      AS "t",
434   BOOL_OR(b3)      AS "f",
435   BOOL_OR(b4)      AS "n",
436   BOOL_OR(NOT b2)  AS "f",
437   BOOL_OR(NOT b3)  AS "t"
438 FROM bool_test;
439  t | t | f | n | f | t 
440 ---+---+---+---+---+---
441  t | t | f |   | f | t
442 (1 row)
443
444 --
445 -- Test cases that should be optimized into indexscans instead of
446 -- the generic aggregate implementation.
447 --
448 analyze tenk1;          -- ensure we get consistent plans here
449 -- Basic cases
450 explain (costs off)
451   select min(unique1) from tenk1;
452                          QUERY PLAN                         
453 ------------------------------------------------------------
454  Result
455    InitPlan 1 (returns $0)
456      ->  Limit
457            ->  Index Only Scan using tenk1_unique1 on tenk1
458                  Index Cond: (unique1 IS NOT NULL)
459 (5 rows)
460
461 select min(unique1) from tenk1;
462  min 
463 -----
464    0
465 (1 row)
466
467 explain (costs off)
468   select max(unique1) from tenk1;
469                              QUERY PLAN                              
470 ---------------------------------------------------------------------
471  Result
472    InitPlan 1 (returns $0)
473      ->  Limit
474            ->  Index Only Scan Backward using tenk1_unique1 on tenk1
475                  Index Cond: (unique1 IS NOT NULL)
476 (5 rows)
477
478 select max(unique1) from tenk1;
479  max  
480 ------
481  9999
482 (1 row)
483
484 explain (costs off)
485   select max(unique1) from tenk1 where unique1 < 42;
486                                QUERY PLAN                               
487 ------------------------------------------------------------------------
488  Result
489    InitPlan 1 (returns $0)
490      ->  Limit
491            ->  Index Only Scan Backward using tenk1_unique1 on tenk1
492                  Index Cond: ((unique1 IS NOT NULL) AND (unique1 < 42))
493 (5 rows)
494
495 select max(unique1) from tenk1 where unique1 < 42;
496  max 
497 -----
498   41
499 (1 row)
500
501 explain (costs off)
502   select max(unique1) from tenk1 where unique1 > 42;
503                                QUERY PLAN                               
504 ------------------------------------------------------------------------
505  Result
506    InitPlan 1 (returns $0)
507      ->  Limit
508            ->  Index Only Scan Backward using tenk1_unique1 on tenk1
509                  Index Cond: ((unique1 IS NOT NULL) AND (unique1 > 42))
510 (5 rows)
511
512 select max(unique1) from tenk1 where unique1 > 42;
513  max  
514 ------
515  9999
516 (1 row)
517
518 explain (costs off)
519   select max(unique1) from tenk1 where unique1 > 42000;
520                                 QUERY PLAN                                 
521 ---------------------------------------------------------------------------
522  Result
523    InitPlan 1 (returns $0)
524      ->  Limit
525            ->  Index Only Scan Backward using tenk1_unique1 on tenk1
526                  Index Cond: ((unique1 IS NOT NULL) AND (unique1 > 42000))
527 (5 rows)
528
529 select max(unique1) from tenk1 where unique1 > 42000;
530  max 
531 -----
532     
533 (1 row)
534
535 -- multi-column index (uses tenk1_thous_tenthous)
536 explain (costs off)
537   select max(tenthous) from tenk1 where thousand = 33;
538                                  QUERY PLAN                                 
539 ----------------------------------------------------------------------------
540  Result
541    InitPlan 1 (returns $0)
542      ->  Limit
543            ->  Index Only Scan Backward using tenk1_thous_tenthous on tenk1
544                  Index Cond: ((thousand = 33) AND (tenthous IS NOT NULL))
545 (5 rows)
546
547 select max(tenthous) from tenk1 where thousand = 33;
548  max  
549 ------
550  9033
551 (1 row)
552
553 explain (costs off)
554   select min(tenthous) from tenk1 where thousand = 33;
555                                 QUERY PLAN                                
556 --------------------------------------------------------------------------
557  Result
558    InitPlan 1 (returns $0)
559      ->  Limit
560            ->  Index Only Scan using tenk1_thous_tenthous on tenk1
561                  Index Cond: ((thousand = 33) AND (tenthous IS NOT NULL))
562 (5 rows)
563
564 select min(tenthous) from tenk1 where thousand = 33;
565  min 
566 -----
567   33
568 (1 row)
569
570 -- check parameter propagation into an indexscan subquery
571 explain (costs off)
572   select f1, (select min(unique1) from tenk1 where unique1 > f1) AS gt
573     from int4_tbl;
574                                        QUERY PLAN                                        
575 -----------------------------------------------------------------------------------------
576  Seq Scan on int4_tbl
577    SubPlan 2
578      ->  Result
579            InitPlan 1 (returns $1)
580              ->  Limit
581                    ->  Index Only Scan using tenk1_unique1 on tenk1
582                          Index Cond: ((unique1 IS NOT NULL) AND (unique1 > int4_tbl.f1))
583 (7 rows)
584
585 select f1, (select min(unique1) from tenk1 where unique1 > f1) AS gt
586   from int4_tbl;
587      f1      | gt 
588 -------------+----
589            0 |  1
590       123456 |   
591      -123456 |  0
592   2147483647 |   
593  -2147483647 |  0
594 (5 rows)
595
596 -- check some cases that were handled incorrectly in 8.3.0
597 explain (costs off)
598   select distinct max(unique2) from tenk1;
599                              QUERY PLAN                              
600 ---------------------------------------------------------------------
601  HashAggregate
602    InitPlan 1 (returns $0)
603      ->  Limit
604            ->  Index Only Scan Backward using tenk1_unique2 on tenk1
605                  Index Cond: (unique2 IS NOT NULL)
606    ->  Result
607 (6 rows)
608
609 select distinct max(unique2) from tenk1;
610  max  
611 ------
612  9999
613 (1 row)
614
615 explain (costs off)
616   select max(unique2) from tenk1 order by 1;
617                              QUERY PLAN                              
618 ---------------------------------------------------------------------
619  Sort
620    Sort Key: ($0)
621    InitPlan 1 (returns $0)
622      ->  Limit
623            ->  Index Only Scan Backward using tenk1_unique2 on tenk1
624                  Index Cond: (unique2 IS NOT NULL)
625    ->  Result
626 (7 rows)
627
628 select max(unique2) from tenk1 order by 1;
629  max  
630 ------
631  9999
632 (1 row)
633
634 explain (costs off)
635   select max(unique2) from tenk1 order by max(unique2);
636                              QUERY PLAN                              
637 ---------------------------------------------------------------------
638  Sort
639    Sort Key: ($0)
640    InitPlan 1 (returns $0)
641      ->  Limit
642            ->  Index Only Scan Backward using tenk1_unique2 on tenk1
643                  Index Cond: (unique2 IS NOT NULL)
644    ->  Result
645 (7 rows)
646
647 select max(unique2) from tenk1 order by max(unique2);
648  max  
649 ------
650  9999
651 (1 row)
652
653 explain (costs off)
654   select max(unique2) from tenk1 order by max(unique2)+1;
655                              QUERY PLAN                              
656 ---------------------------------------------------------------------
657  Sort
658    Sort Key: (($0 + 1))
659    InitPlan 1 (returns $0)
660      ->  Limit
661            ->  Index Only Scan Backward using tenk1_unique2 on tenk1
662                  Index Cond: (unique2 IS NOT NULL)
663    ->  Result
664 (7 rows)
665
666 select max(unique2) from tenk1 order by max(unique2)+1;
667  max  
668 ------
669  9999
670 (1 row)
671
672 explain (costs off)
673   select max(unique2), generate_series(1,3) as g from tenk1 order by g desc;
674                              QUERY PLAN                              
675 ---------------------------------------------------------------------
676  Sort
677    Sort Key: (generate_series(1, 3))
678    InitPlan 1 (returns $0)
679      ->  Limit
680            ->  Index Only Scan Backward using tenk1_unique2 on tenk1
681                  Index Cond: (unique2 IS NOT NULL)
682    ->  Result
683 (7 rows)
684
685 select max(unique2), generate_series(1,3) as g from tenk1 order by g desc;
686  max  | g 
687 ------+---
688  9999 | 3
689  9999 | 2
690  9999 | 1
691 (3 rows)
692
693 -- try it on an inheritance tree
694 create table minmaxtest(f1 int);
695 create table minmaxtest1() inherits (minmaxtest);
696 create table minmaxtest2() inherits (minmaxtest);
697 create table minmaxtest3() inherits (minmaxtest);
698 create index minmaxtesti on minmaxtest(f1);
699 create index minmaxtest1i on minmaxtest1(f1);
700 create index minmaxtest2i on minmaxtest2(f1 desc);
701 create index minmaxtest3i on minmaxtest3(f1) where f1 is not null;
702 insert into minmaxtest values(11), (12);
703 insert into minmaxtest1 values(13), (14);
704 insert into minmaxtest2 values(15), (16);
705 insert into minmaxtest3 values(17), (18);
706 explain (costs off)
707   select min(f1), max(f1) from minmaxtest;
708                                           QUERY PLAN                                          
709 ----------------------------------------------------------------------------------------------
710  Result
711    InitPlan 1 (returns $0)
712      ->  Limit
713            ->  Merge Append
714                  Sort Key: minmaxtest.f1
715                  ->  Index Only Scan using minmaxtesti on minmaxtest
716                        Index Cond: (f1 IS NOT NULL)
717                  ->  Index Only Scan using minmaxtest1i on minmaxtest1
718                        Index Cond: (f1 IS NOT NULL)
719                  ->  Index Only Scan Backward using minmaxtest2i on minmaxtest2
720                        Index Cond: (f1 IS NOT NULL)
721                  ->  Index Only Scan using minmaxtest3i on minmaxtest3
722                        Index Cond: (f1 IS NOT NULL)
723    InitPlan 2 (returns $1)
724      ->  Limit
725            ->  Merge Append
726                  Sort Key: minmaxtest_1.f1
727                  ->  Index Only Scan Backward using minmaxtesti on minmaxtest minmaxtest_1
728                        Index Cond: (f1 IS NOT NULL)
729                  ->  Index Only Scan Backward using minmaxtest1i on minmaxtest1 minmaxtest1_1
730                        Index Cond: (f1 IS NOT NULL)
731                  ->  Index Only Scan using minmaxtest2i on minmaxtest2 minmaxtest2_1
732                        Index Cond: (f1 IS NOT NULL)
733                  ->  Index Only Scan Backward using minmaxtest3i on minmaxtest3 minmaxtest3_1
734                        Index Cond: (f1 IS NOT NULL)
735 (25 rows)
736
737 select min(f1), max(f1) from minmaxtest;
738  min | max 
739 -----+-----
740   11 |  18
741 (1 row)
742
743 -- DISTINCT doesn't do anything useful here, but it shouldn't fail
744 explain (costs off)
745   select distinct min(f1), max(f1) from minmaxtest;
746                                           QUERY PLAN                                          
747 ----------------------------------------------------------------------------------------------
748  HashAggregate
749    InitPlan 1 (returns $0)
750      ->  Limit
751            ->  Merge Append
752                  Sort Key: minmaxtest.f1
753                  ->  Index Only Scan using minmaxtesti on minmaxtest
754                        Index Cond: (f1 IS NOT NULL)
755                  ->  Index Only Scan using minmaxtest1i on minmaxtest1
756                        Index Cond: (f1 IS NOT NULL)
757                  ->  Index Only Scan Backward using minmaxtest2i on minmaxtest2
758                        Index Cond: (f1 IS NOT NULL)
759                  ->  Index Only Scan using minmaxtest3i on minmaxtest3
760                        Index Cond: (f1 IS NOT NULL)
761    InitPlan 2 (returns $1)
762      ->  Limit
763            ->  Merge Append
764                  Sort Key: minmaxtest_1.f1
765                  ->  Index Only Scan Backward using minmaxtesti on minmaxtest minmaxtest_1
766                        Index Cond: (f1 IS NOT NULL)
767                  ->  Index Only Scan Backward using minmaxtest1i on minmaxtest1 minmaxtest1_1
768                        Index Cond: (f1 IS NOT NULL)
769                  ->  Index Only Scan using minmaxtest2i on minmaxtest2 minmaxtest2_1
770                        Index Cond: (f1 IS NOT NULL)
771                  ->  Index Only Scan Backward using minmaxtest3i on minmaxtest3 minmaxtest3_1
772                        Index Cond: (f1 IS NOT NULL)
773    ->  Result
774 (26 rows)
775
776 select distinct min(f1), max(f1) from minmaxtest;
777  min | max 
778 -----+-----
779   11 |  18
780 (1 row)
781
782 drop table minmaxtest cascade;
783 NOTICE:  drop cascades to 3 other objects
784 DETAIL:  drop cascades to table minmaxtest1
785 drop cascades to table minmaxtest2
786 drop cascades to table minmaxtest3
787 -- check for correct detection of nested-aggregate errors
788 select max(min(unique1)) from tenk1;
789 ERROR:  aggregate function calls cannot be nested
790 LINE 1: select max(min(unique1)) from tenk1;
791                    ^
792 select (select max(min(unique1)) from int8_tbl) from tenk1;
793 ERROR:  aggregate function calls cannot be nested
794 LINE 1: select (select max(min(unique1)) from int8_tbl) from tenk1;
795                            ^
796 --
797 -- Test combinations of DISTINCT and/or ORDER BY
798 --
799 select array_agg(a order by b)
800   from (values (1,4),(2,3),(3,1),(4,2)) v(a,b);
801  array_agg 
802 -----------
803  {3,4,2,1}
804 (1 row)
805
806 select array_agg(a order by a)
807   from (values (1,4),(2,3),(3,1),(4,2)) v(a,b);
808  array_agg 
809 -----------
810  {1,2,3,4}
811 (1 row)
812
813 select array_agg(a order by a desc)
814   from (values (1,4),(2,3),(3,1),(4,2)) v(a,b);
815  array_agg 
816 -----------
817  {4,3,2,1}
818 (1 row)
819
820 select array_agg(b order by a desc)
821   from (values (1,4),(2,3),(3,1),(4,2)) v(a,b);
822  array_agg 
823 -----------
824  {2,1,3,4}
825 (1 row)
826
827 select array_agg(distinct a)
828   from (values (1),(2),(1),(3),(null),(2)) v(a);
829   array_agg   
830 --------------
831  {1,2,3,NULL}
832 (1 row)
833
834 select array_agg(distinct a order by a)
835   from (values (1),(2),(1),(3),(null),(2)) v(a);
836   array_agg   
837 --------------
838  {1,2,3,NULL}
839 (1 row)
840
841 select array_agg(distinct a order by a desc)
842   from (values (1),(2),(1),(3),(null),(2)) v(a);
843   array_agg   
844 --------------
845  {NULL,3,2,1}
846 (1 row)
847
848 select array_agg(distinct a order by a desc nulls last)
849   from (values (1),(2),(1),(3),(null),(2)) v(a);
850   array_agg   
851 --------------
852  {3,2,1,NULL}
853 (1 row)
854
855 -- multi-arg aggs, strict/nonstrict, distinct/order by
856 select aggfstr(a,b,c)
857   from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c);
858                 aggfstr                
859 ---------------------------------------
860  {"(1,3,foo)","(2,2,bar)","(3,1,baz)"}
861 (1 row)
862
863 select aggfns(a,b,c)
864   from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c);
865                     aggfns                     
866 -----------------------------------------------
867  {"(1,3,foo)","(0,,)","(2,2,bar)","(3,1,baz)"}
868 (1 row)
869
870 select aggfstr(distinct a,b,c)
871   from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
872        generate_series(1,3) i;
873                 aggfstr                
874 ---------------------------------------
875  {"(1,3,foo)","(2,2,bar)","(3,1,baz)"}
876 (1 row)
877
878 select aggfns(distinct a,b,c)
879   from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
880        generate_series(1,3) i;
881                     aggfns                     
882 -----------------------------------------------
883  {"(0,,)","(1,3,foo)","(2,2,bar)","(3,1,baz)"}
884 (1 row)
885
886 select aggfstr(distinct a,b,c order by b)
887   from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
888        generate_series(1,3) i;
889                 aggfstr                
890 ---------------------------------------
891  {"(3,1,baz)","(2,2,bar)","(1,3,foo)"}
892 (1 row)
893
894 select aggfns(distinct a,b,c order by b)
895   from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
896        generate_series(1,3) i;
897                     aggfns                     
898 -----------------------------------------------
899  {"(3,1,baz)","(2,2,bar)","(1,3,foo)","(0,,)"}
900 (1 row)
901
902 -- test specific code paths
903 select aggfns(distinct a,a,c order by c using ~<~,a)
904   from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
905        generate_series(1,2) i;
906                      aggfns                     
907 ------------------------------------------------
908  {"(2,2,bar)","(3,3,baz)","(1,1,foo)","(0,0,)"}
909 (1 row)
910
911 select aggfns(distinct a,a,c order by c using ~<~)
912   from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
913        generate_series(1,2) i;
914                      aggfns                     
915 ------------------------------------------------
916  {"(2,2,bar)","(3,3,baz)","(1,1,foo)","(0,0,)"}
917 (1 row)
918
919 select aggfns(distinct a,a,c order by a)
920   from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
921        generate_series(1,2) i;
922                      aggfns                     
923 ------------------------------------------------
924  {"(0,0,)","(1,1,foo)","(2,2,bar)","(3,3,baz)"}
925 (1 row)
926
927 select aggfns(distinct a,b,c order by a,c using ~<~,b)
928   from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
929        generate_series(1,2) i;
930                     aggfns                     
931 -----------------------------------------------
932  {"(0,,)","(1,3,foo)","(2,2,bar)","(3,1,baz)"}
933 (1 row)
934
935 -- check node I/O via view creation and usage, also deparsing logic
936 create view agg_view1 as
937   select aggfns(a,b,c)
938     from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c);
939 select * from agg_view1;
940                     aggfns                     
941 -----------------------------------------------
942  {"(1,3,foo)","(0,,)","(2,2,bar)","(3,1,baz)"}
943 (1 row)
944
945 select pg_get_viewdef('agg_view1'::regclass);
946                                                    pg_get_viewdef                                                    
947 ---------------------------------------------------------------------------------------------------------------------
948   SELECT aggfns(v.a, v.b, v.c) AS aggfns                                                                            +
949     FROM ( VALUES (1,3,'foo'::text), (0,NULL::integer,NULL::text), (2,2,'bar'::text), (3,1,'baz'::text)) v(a, b, c);
950 (1 row)
951
952 create or replace view agg_view1 as
953   select aggfns(distinct a,b,c)
954     from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
955          generate_series(1,3) i;
956 select * from agg_view1;
957                     aggfns                     
958 -----------------------------------------------
959  {"(0,,)","(1,3,foo)","(2,2,bar)","(3,1,baz)"}
960 (1 row)
961
962 select pg_get_viewdef('agg_view1'::regclass);
963                                                     pg_get_viewdef                                                    
964 ----------------------------------------------------------------------------------------------------------------------
965   SELECT aggfns(DISTINCT v.a, v.b, v.c) AS aggfns                                                                    +
966     FROM ( VALUES (1,3,'foo'::text), (0,NULL::integer,NULL::text), (2,2,'bar'::text), (3,1,'baz'::text)) v(a, b, c), +
967      generate_series(1, 3) i(i);
968 (1 row)
969
970 create or replace view agg_view1 as
971   select aggfns(distinct a,b,c order by b)
972     from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
973          generate_series(1,3) i;
974 select * from agg_view1;
975                     aggfns                     
976 -----------------------------------------------
977  {"(3,1,baz)","(2,2,bar)","(1,3,foo)","(0,,)"}
978 (1 row)
979
980 select pg_get_viewdef('agg_view1'::regclass);
981                                                     pg_get_viewdef                                                    
982 ----------------------------------------------------------------------------------------------------------------------
983   SELECT aggfns(DISTINCT v.a, v.b, v.c ORDER BY v.b) AS aggfns                                                       +
984     FROM ( VALUES (1,3,'foo'::text), (0,NULL::integer,NULL::text), (2,2,'bar'::text), (3,1,'baz'::text)) v(a, b, c), +
985      generate_series(1, 3) i(i);
986 (1 row)
987
988 create or replace view agg_view1 as
989   select aggfns(a,b,c order by b+1)
990     from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c);
991 select * from agg_view1;
992                     aggfns                     
993 -----------------------------------------------
994  {"(3,1,baz)","(2,2,bar)","(1,3,foo)","(0,,)"}
995 (1 row)
996
997 select pg_get_viewdef('agg_view1'::regclass);
998                                                    pg_get_viewdef                                                    
999 ---------------------------------------------------------------------------------------------------------------------
1000   SELECT aggfns(v.a, v.b, v.c ORDER BY (v.b + 1)) AS aggfns                                                         +
1001     FROM ( VALUES (1,3,'foo'::text), (0,NULL::integer,NULL::text), (2,2,'bar'::text), (3,1,'baz'::text)) v(a, b, c);
1002 (1 row)
1003
1004 create or replace view agg_view1 as
1005   select aggfns(a,a,c order by b)
1006     from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c);
1007 select * from agg_view1;
1008                      aggfns                     
1009 ------------------------------------------------
1010  {"(3,3,baz)","(2,2,bar)","(1,1,foo)","(0,0,)"}
1011 (1 row)
1012
1013 select pg_get_viewdef('agg_view1'::regclass);
1014                                                    pg_get_viewdef                                                    
1015 ---------------------------------------------------------------------------------------------------------------------
1016   SELECT aggfns(v.a, v.a, v.c ORDER BY v.b) AS aggfns                                                               +
1017     FROM ( VALUES (1,3,'foo'::text), (0,NULL::integer,NULL::text), (2,2,'bar'::text), (3,1,'baz'::text)) v(a, b, c);
1018 (1 row)
1019
1020 create or replace view agg_view1 as
1021   select aggfns(a,b,c order by c using ~<~)
1022     from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c);
1023 select * from agg_view1;
1024                     aggfns                     
1025 -----------------------------------------------
1026  {"(2,2,bar)","(3,1,baz)","(1,3,foo)","(0,,)"}
1027 (1 row)
1028
1029 select pg_get_viewdef('agg_view1'::regclass);
1030                                                    pg_get_viewdef                                                    
1031 ---------------------------------------------------------------------------------------------------------------------
1032   SELECT aggfns(v.a, v.b, v.c ORDER BY v.c USING ~<~ NULLS LAST) AS aggfns                                          +
1033     FROM ( VALUES (1,3,'foo'::text), (0,NULL::integer,NULL::text), (2,2,'bar'::text), (3,1,'baz'::text)) v(a, b, c);
1034 (1 row)
1035
1036 create or replace view agg_view1 as
1037   select aggfns(distinct a,b,c order by a,c using ~<~,b)
1038     from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
1039          generate_series(1,2) i;
1040 select * from agg_view1;
1041                     aggfns                     
1042 -----------------------------------------------
1043  {"(0,,)","(1,3,foo)","(2,2,bar)","(3,1,baz)"}
1044 (1 row)
1045
1046 select pg_get_viewdef('agg_view1'::regclass);
1047                                                     pg_get_viewdef                                                    
1048 ----------------------------------------------------------------------------------------------------------------------
1049   SELECT aggfns(DISTINCT v.a, v.b, v.c ORDER BY v.a, v.c USING ~<~ NULLS LAST, v.b) AS aggfns                        +
1050     FROM ( VALUES (1,3,'foo'::text), (0,NULL::integer,NULL::text), (2,2,'bar'::text), (3,1,'baz'::text)) v(a, b, c), +
1051      generate_series(1, 2) i(i);
1052 (1 row)
1053
1054 drop view agg_view1;
1055 -- incorrect DISTINCT usage errors
1056 select aggfns(distinct a,b,c order by i)
1057   from (values (1,1,'foo')) v(a,b,c), generate_series(1,2) i;
1058 ERROR:  in an aggregate with DISTINCT, ORDER BY expressions must appear in argument list
1059 LINE 1: select aggfns(distinct a,b,c order by i)
1060                                               ^
1061 select aggfns(distinct a,b,c order by a,b+1)
1062   from (values (1,1,'foo')) v(a,b,c), generate_series(1,2) i;
1063 ERROR:  in an aggregate with DISTINCT, ORDER BY expressions must appear in argument list
1064 LINE 1: select aggfns(distinct a,b,c order by a,b+1)
1065                                                 ^
1066 select aggfns(distinct a,b,c order by a,b,i,c)
1067   from (values (1,1,'foo')) v(a,b,c), generate_series(1,2) i;
1068 ERROR:  in an aggregate with DISTINCT, ORDER BY expressions must appear in argument list
1069 LINE 1: select aggfns(distinct a,b,c order by a,b,i,c)
1070                                                   ^
1071 select aggfns(distinct a,a,c order by a,b)
1072   from (values (1,1,'foo')) v(a,b,c), generate_series(1,2) i;
1073 ERROR:  in an aggregate with DISTINCT, ORDER BY expressions must appear in argument list
1074 LINE 1: select aggfns(distinct a,a,c order by a,b)
1075                                                 ^
1076 -- string_agg tests
1077 select string_agg(a,',') from (values('aaaa'),('bbbb'),('cccc')) g(a);
1078    string_agg   
1079 ----------------
1080  aaaa,bbbb,cccc
1081 (1 row)
1082
1083 select string_agg(a,',') from (values('aaaa'),(null),('bbbb'),('cccc')) g(a);
1084    string_agg   
1085 ----------------
1086  aaaa,bbbb,cccc
1087 (1 row)
1088
1089 select string_agg(a,'AB') from (values(null),(null),('bbbb'),('cccc')) g(a);
1090  string_agg 
1091 ------------
1092  bbbbABcccc
1093 (1 row)
1094
1095 select string_agg(a,',') from (values(null),(null)) g(a);
1096  string_agg 
1097 ------------
1098  
1099 (1 row)
1100
1101 -- check some implicit casting cases, as per bug #5564
1102 select string_agg(distinct f1, ',' order by f1) from varchar_tbl;  -- ok
1103  string_agg 
1104 ------------
1105  a,ab,abcd
1106 (1 row)
1107
1108 select string_agg(distinct f1::text, ',' order by f1) from varchar_tbl;  -- not ok
1109 ERROR:  in an aggregate with DISTINCT, ORDER BY expressions must appear in argument list
1110 LINE 1: select string_agg(distinct f1::text, ',' order by f1) from v...
1111                                                           ^
1112 select string_agg(distinct f1, ',' order by f1::text) from varchar_tbl;  -- not ok
1113 ERROR:  in an aggregate with DISTINCT, ORDER BY expressions must appear in argument list
1114 LINE 1: select string_agg(distinct f1, ',' order by f1::text) from v...
1115                                                     ^
1116 select string_agg(distinct f1::text, ',' order by f1::text) from varchar_tbl;  -- ok
1117  string_agg 
1118 ------------
1119  a,ab,abcd
1120 (1 row)
1121
1122 -- string_agg bytea tests
1123 create table bytea_test_table(v bytea);
1124 select string_agg(v, '') from bytea_test_table;
1125  string_agg 
1126 ------------
1127  
1128 (1 row)
1129
1130 insert into bytea_test_table values(decode('ff','hex'));
1131 select string_agg(v, '') from bytea_test_table;
1132  string_agg 
1133 ------------
1134  \xff
1135 (1 row)
1136
1137 insert into bytea_test_table values(decode('aa','hex'));
1138 select string_agg(v, '') from bytea_test_table;
1139  string_agg 
1140 ------------
1141  \xffaa
1142 (1 row)
1143
1144 select string_agg(v, NULL) from bytea_test_table;
1145  string_agg 
1146 ------------
1147  \xffaa
1148 (1 row)
1149
1150 select string_agg(v, decode('ee', 'hex')) from bytea_test_table;
1151  string_agg 
1152 ------------
1153  \xffeeaa
1154 (1 row)
1155
1156 drop table bytea_test_table;