]> granicus.if.org Git - postgresql/blob - src/test/regress/expected/aggregates.out
1a0ca5c5f3c109b92e5fb936efa42dafe982c81a
[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 -- verify correct results for null and NaN inputs
141 select sum(null::int4) from generate_series(1,3);
142  sum 
143 -----
144     
145 (1 row)
146
147 select sum(null::int8) from generate_series(1,3);
148  sum 
149 -----
150     
151 (1 row)
152
153 select sum(null::numeric) from generate_series(1,3);
154  sum 
155 -----
156     
157 (1 row)
158
159 select sum(null::float8) from generate_series(1,3);
160  sum 
161 -----
162     
163 (1 row)
164
165 select avg(null::int4) from generate_series(1,3);
166  avg 
167 -----
168     
169 (1 row)
170
171 select avg(null::int8) from generate_series(1,3);
172  avg 
173 -----
174     
175 (1 row)
176
177 select avg(null::numeric) from generate_series(1,3);
178  avg 
179 -----
180     
181 (1 row)
182
183 select avg(null::float8) from generate_series(1,3);
184  avg 
185 -----
186     
187 (1 row)
188
189 select sum('NaN'::numeric) from generate_series(1,3);
190  sum 
191 -----
192  NaN
193 (1 row)
194
195 select avg('NaN'::numeric) from generate_series(1,3);
196  avg 
197 -----
198  NaN
199 (1 row)
200
201 -- SQL2003 binary aggregates
202 SELECT regr_count(b, a) FROM aggtest;
203  regr_count 
204 ------------
205           4
206 (1 row)
207
208 SELECT regr_sxx(b, a) FROM aggtest;
209  regr_sxx 
210 ----------
211      5099
212 (1 row)
213
214 SELECT regr_syy(b, a) FROM aggtest;
215      regr_syy     
216 ------------------
217  68756.2156939293
218 (1 row)
219
220 SELECT regr_sxy(b, a) FROM aggtest;
221      regr_sxy     
222 ------------------
223  2614.51582155004
224 (1 row)
225
226 SELECT regr_avgx(b, a), regr_avgy(b, a) FROM aggtest;
227  regr_avgx |    regr_avgy     
228 -----------+------------------
229       49.5 | 107.943152273074
230 (1 row)
231
232 SELECT regr_r2(b, a) FROM aggtest;
233       regr_r2       
234 --------------------
235  0.0194977982031803
236 (1 row)
237
238 SELECT regr_slope(b, a), regr_intercept(b, a) FROM aggtest;
239     regr_slope     |  regr_intercept  
240 -------------------+------------------
241  0.512750700441271 | 82.5619926012309
242 (1 row)
243
244 SELECT covar_pop(b, a), covar_samp(b, a) FROM aggtest;
245     covar_pop    |    covar_samp    
246 -----------------+------------------
247  653.62895538751 | 871.505273850014
248 (1 row)
249
250 SELECT corr(b, a) FROM aggtest;
251        corr        
252 -------------------
253  0.139634516517873
254 (1 row)
255
256 SELECT count(four) AS cnt_1000 FROM onek;
257  cnt_1000 
258 ----------
259      1000
260 (1 row)
261
262 SELECT count(DISTINCT four) AS cnt_4 FROM onek;
263  cnt_4 
264 -------
265      4
266 (1 row)
267
268 select ten, count(*), sum(four) from onek
269 group by ten order by ten;
270  ten | count | sum 
271 -----+-------+-----
272    0 |   100 | 100
273    1 |   100 | 200
274    2 |   100 | 100
275    3 |   100 | 200
276    4 |   100 | 100
277    5 |   100 | 200
278    6 |   100 | 100
279    7 |   100 | 200
280    8 |   100 | 100
281    9 |   100 | 200
282 (10 rows)
283
284 select ten, count(four), sum(DISTINCT four) from onek
285 group by ten order by ten;
286  ten | count | sum 
287 -----+-------+-----
288    0 |   100 |   2
289    1 |   100 |   4
290    2 |   100 |   2
291    3 |   100 |   4
292    4 |   100 |   2
293    5 |   100 |   4
294    6 |   100 |   2
295    7 |   100 |   4
296    8 |   100 |   2
297    9 |   100 |   4
298 (10 rows)
299
300 -- user-defined aggregates
301 SELECT newavg(four) AS avg_1 FROM onek;
302        avg_1        
303 --------------------
304  1.5000000000000000
305 (1 row)
306
307 SELECT newsum(four) AS sum_1500 FROM onek;
308  sum_1500 
309 ----------
310      1500
311 (1 row)
312
313 SELECT newcnt(four) AS cnt_1000 FROM onek;
314  cnt_1000 
315 ----------
316      1000
317 (1 row)
318
319 SELECT newcnt(*) AS cnt_1000 FROM onek;
320  cnt_1000 
321 ----------
322      1000
323 (1 row)
324
325 SELECT oldcnt(*) AS cnt_1000 FROM onek;
326  cnt_1000 
327 ----------
328      1000
329 (1 row)
330
331 SELECT sum2(q1,q2) FROM int8_tbl;
332        sum2        
333 -------------------
334  18271560493827981
335 (1 row)
336
337 -- test for outer-level aggregates
338 -- this should work
339 select ten, sum(distinct four) from onek a
340 group by ten
341 having exists (select 1 from onek b where sum(distinct a.four) = b.four);
342  ten | sum 
343 -----+-----
344    0 |   2
345    2 |   2
346    4 |   2
347    6 |   2
348    8 |   2
349 (5 rows)
350
351 -- this should fail because subquery has an agg of its own in WHERE
352 select ten, sum(distinct four) from onek a
353 group by ten
354 having exists (select 1 from onek b
355                where sum(distinct a.four + b.four) = b.four);
356 ERROR:  aggregate functions are not allowed in WHERE
357 LINE 4:                where sum(distinct a.four + b.four) = b.four)...
358                              ^
359 -- Test handling of sublinks within outer-level aggregates.
360 -- Per bug report from Daniel Grace.
361 select
362   (select max((select i.unique2 from tenk1 i where i.unique1 = o.unique1)))
363 from tenk1 o;
364  max  
365 ------
366  9999
367 (1 row)
368
369 --
370 -- test for bitwise integer aggregates
371 --
372 CREATE TEMPORARY TABLE bitwise_test(
373   i2 INT2,
374   i4 INT4,
375   i8 INT8,
376   i INTEGER,
377   x INT2,
378   y BIT(4)
379 );
380 -- empty case
381 SELECT
382   BIT_AND(i2) AS "?",
383   BIT_OR(i4)  AS "?"
384 FROM bitwise_test;
385  ? | ? 
386 ---+---
387    |  
388 (1 row)
389
390 COPY bitwise_test FROM STDIN NULL 'null';
391 SELECT
392   BIT_AND(i2) AS "1",
393   BIT_AND(i4) AS "1",
394   BIT_AND(i8) AS "1",
395   BIT_AND(i)  AS "?",
396   BIT_AND(x)  AS "0",
397   BIT_AND(y)  AS "0100",
398   BIT_OR(i2)  AS "7",
399   BIT_OR(i4)  AS "7",
400   BIT_OR(i8)  AS "7",
401   BIT_OR(i)   AS "?",
402   BIT_OR(x)   AS "7",
403   BIT_OR(y)   AS "1101"
404 FROM bitwise_test;
405  1 | 1 | 1 | ? | 0 | 0100 | 7 | 7 | 7 | ? | 7 | 1101 
406 ---+---+---+---+---+------+---+---+---+---+---+------
407  1 | 1 | 1 | 1 | 0 | 0100 | 7 | 7 | 7 | 3 | 7 | 1101
408 (1 row)
409
410 --
411 -- test boolean aggregates
412 --
413 -- first test all possible transition and final states
414 SELECT
415   -- boolean and transitions
416   -- null because strict
417   booland_statefunc(NULL, NULL)  IS NULL AS "t",
418   booland_statefunc(TRUE, NULL)  IS NULL AS "t",
419   booland_statefunc(FALSE, NULL) IS NULL AS "t",
420   booland_statefunc(NULL, TRUE)  IS NULL AS "t",
421   booland_statefunc(NULL, FALSE) IS NULL AS "t",
422   -- and actual computations
423   booland_statefunc(TRUE, TRUE) AS "t",
424   NOT booland_statefunc(TRUE, FALSE) AS "t",
425   NOT booland_statefunc(FALSE, TRUE) AS "t",
426   NOT booland_statefunc(FALSE, FALSE) AS "t";
427  t | t | t | t | t | t | t | t | t 
428 ---+---+---+---+---+---+---+---+---
429  t | t | t | t | t | t | t | t | t
430 (1 row)
431
432 SELECT
433   -- boolean or transitions
434   -- null because strict
435   boolor_statefunc(NULL, NULL)  IS NULL AS "t",
436   boolor_statefunc(TRUE, NULL)  IS NULL AS "t",
437   boolor_statefunc(FALSE, NULL) IS NULL AS "t",
438   boolor_statefunc(NULL, TRUE)  IS NULL AS "t",
439   boolor_statefunc(NULL, FALSE) IS NULL AS "t",
440   -- actual computations
441   boolor_statefunc(TRUE, TRUE) AS "t",
442   boolor_statefunc(TRUE, FALSE) AS "t",
443   boolor_statefunc(FALSE, TRUE) AS "t",
444   NOT boolor_statefunc(FALSE, FALSE) AS "t";
445  t | t | t | t | t | t | t | t | t 
446 ---+---+---+---+---+---+---+---+---
447  t | t | t | t | t | t | t | t | t
448 (1 row)
449
450 CREATE TEMPORARY TABLE bool_test(
451   b1 BOOL,
452   b2 BOOL,
453   b3 BOOL,
454   b4 BOOL);
455 -- empty case
456 SELECT
457   BOOL_AND(b1)   AS "n",
458   BOOL_OR(b3)    AS "n"
459 FROM bool_test;
460  n | n 
461 ---+---
462    | 
463 (1 row)
464
465 COPY bool_test FROM STDIN NULL 'null';
466 SELECT
467   BOOL_AND(b1)     AS "f",
468   BOOL_AND(b2)     AS "t",
469   BOOL_AND(b3)     AS "f",
470   BOOL_AND(b4)     AS "n",
471   BOOL_AND(NOT b2) AS "f",
472   BOOL_AND(NOT b3) AS "t"
473 FROM bool_test;
474  f | t | f | n | f | t 
475 ---+---+---+---+---+---
476  f | t | f |   | f | t
477 (1 row)
478
479 SELECT
480   EVERY(b1)     AS "f",
481   EVERY(b2)     AS "t",
482   EVERY(b3)     AS "f",
483   EVERY(b4)     AS "n",
484   EVERY(NOT b2) AS "f",
485   EVERY(NOT b3) AS "t"
486 FROM bool_test;
487  f | t | f | n | f | t 
488 ---+---+---+---+---+---
489  f | t | f |   | f | t
490 (1 row)
491
492 SELECT
493   BOOL_OR(b1)      AS "t",
494   BOOL_OR(b2)      AS "t",
495   BOOL_OR(b3)      AS "f",
496   BOOL_OR(b4)      AS "n",
497   BOOL_OR(NOT b2)  AS "f",
498   BOOL_OR(NOT b3)  AS "t"
499 FROM bool_test;
500  t | t | f | n | f | t 
501 ---+---+---+---+---+---
502  t | t | f |   | f | t
503 (1 row)
504
505 --
506 -- Test cases that should be optimized into indexscans instead of
507 -- the generic aggregate implementation.
508 --
509 -- Basic cases
510 explain (costs off)
511   select min(unique1) from tenk1;
512                          QUERY PLAN                         
513 ------------------------------------------------------------
514  Result
515    InitPlan 1 (returns $0)
516      ->  Limit
517            ->  Index Only Scan using tenk1_unique1 on tenk1
518                  Index Cond: (unique1 IS NOT NULL)
519 (5 rows)
520
521 select min(unique1) from tenk1;
522  min 
523 -----
524    0
525 (1 row)
526
527 explain (costs off)
528   select max(unique1) from tenk1;
529                              QUERY PLAN                              
530 ---------------------------------------------------------------------
531  Result
532    InitPlan 1 (returns $0)
533      ->  Limit
534            ->  Index Only Scan Backward using tenk1_unique1 on tenk1
535                  Index Cond: (unique1 IS NOT NULL)
536 (5 rows)
537
538 select max(unique1) from tenk1;
539  max  
540 ------
541  9999
542 (1 row)
543
544 explain (costs off)
545   select max(unique1) from tenk1 where unique1 < 42;
546                                QUERY PLAN                               
547 ------------------------------------------------------------------------
548  Result
549    InitPlan 1 (returns $0)
550      ->  Limit
551            ->  Index Only Scan Backward using tenk1_unique1 on tenk1
552                  Index Cond: ((unique1 IS NOT NULL) AND (unique1 < 42))
553 (5 rows)
554
555 select max(unique1) from tenk1 where unique1 < 42;
556  max 
557 -----
558   41
559 (1 row)
560
561 explain (costs off)
562   select max(unique1) from tenk1 where unique1 > 42;
563                                QUERY PLAN                               
564 ------------------------------------------------------------------------
565  Result
566    InitPlan 1 (returns $0)
567      ->  Limit
568            ->  Index Only Scan Backward using tenk1_unique1 on tenk1
569                  Index Cond: ((unique1 IS NOT NULL) AND (unique1 > 42))
570 (5 rows)
571
572 select max(unique1) from tenk1 where unique1 > 42;
573  max  
574 ------
575  9999
576 (1 row)
577
578 explain (costs off)
579   select max(unique1) from tenk1 where unique1 > 42000;
580                                 QUERY PLAN                                 
581 ---------------------------------------------------------------------------
582  Result
583    InitPlan 1 (returns $0)
584      ->  Limit
585            ->  Index Only Scan Backward using tenk1_unique1 on tenk1
586                  Index Cond: ((unique1 IS NOT NULL) AND (unique1 > 42000))
587 (5 rows)
588
589 select max(unique1) from tenk1 where unique1 > 42000;
590  max 
591 -----
592     
593 (1 row)
594
595 -- multi-column index (uses tenk1_thous_tenthous)
596 explain (costs off)
597   select max(tenthous) from tenk1 where thousand = 33;
598                                  QUERY PLAN                                 
599 ----------------------------------------------------------------------------
600  Result
601    InitPlan 1 (returns $0)
602      ->  Limit
603            ->  Index Only Scan Backward using tenk1_thous_tenthous on tenk1
604                  Index Cond: ((thousand = 33) AND (tenthous IS NOT NULL))
605 (5 rows)
606
607 select max(tenthous) from tenk1 where thousand = 33;
608  max  
609 ------
610  9033
611 (1 row)
612
613 explain (costs off)
614   select min(tenthous) from tenk1 where thousand = 33;
615                                 QUERY PLAN                                
616 --------------------------------------------------------------------------
617  Result
618    InitPlan 1 (returns $0)
619      ->  Limit
620            ->  Index Only Scan using tenk1_thous_tenthous on tenk1
621                  Index Cond: ((thousand = 33) AND (tenthous IS NOT NULL))
622 (5 rows)
623
624 select min(tenthous) from tenk1 where thousand = 33;
625  min 
626 -----
627   33
628 (1 row)
629
630 -- check parameter propagation into an indexscan subquery
631 explain (costs off)
632   select f1, (select min(unique1) from tenk1 where unique1 > f1) AS gt
633     from int4_tbl;
634                                        QUERY PLAN                                        
635 -----------------------------------------------------------------------------------------
636  Seq Scan on int4_tbl
637    SubPlan 2
638      ->  Result
639            InitPlan 1 (returns $1)
640              ->  Limit
641                    ->  Index Only Scan using tenk1_unique1 on tenk1
642                          Index Cond: ((unique1 IS NOT NULL) AND (unique1 > int4_tbl.f1))
643 (7 rows)
644
645 select f1, (select min(unique1) from tenk1 where unique1 > f1) AS gt
646   from int4_tbl;
647      f1      | gt 
648 -------------+----
649            0 |  1
650       123456 |   
651      -123456 |  0
652   2147483647 |   
653  -2147483647 |  0
654 (5 rows)
655
656 -- check some cases that were handled incorrectly in 8.3.0
657 explain (costs off)
658   select distinct max(unique2) from tenk1;
659                              QUERY PLAN                              
660 ---------------------------------------------------------------------
661  HashAggregate
662    Group Key: $0
663    InitPlan 1 (returns $0)
664      ->  Limit
665            ->  Index Only Scan Backward using tenk1_unique2 on tenk1
666                  Index Cond: (unique2 IS NOT NULL)
667    ->  Result
668 (7 rows)
669
670 select distinct max(unique2) from tenk1;
671  max  
672 ------
673  9999
674 (1 row)
675
676 explain (costs off)
677   select max(unique2) from tenk1 order by 1;
678                              QUERY PLAN                              
679 ---------------------------------------------------------------------
680  Sort
681    Sort Key: ($0)
682    InitPlan 1 (returns $0)
683      ->  Limit
684            ->  Index Only Scan Backward using tenk1_unique2 on tenk1
685                  Index Cond: (unique2 IS NOT NULL)
686    ->  Result
687 (7 rows)
688
689 select max(unique2) from tenk1 order by 1;
690  max  
691 ------
692  9999
693 (1 row)
694
695 explain (costs off)
696   select max(unique2) from tenk1 order by max(unique2);
697                              QUERY PLAN                              
698 ---------------------------------------------------------------------
699  Sort
700    Sort Key: ($0)
701    InitPlan 1 (returns $0)
702      ->  Limit
703            ->  Index Only Scan Backward using tenk1_unique2 on tenk1
704                  Index Cond: (unique2 IS NOT NULL)
705    ->  Result
706 (7 rows)
707
708 select max(unique2) from tenk1 order by max(unique2);
709  max  
710 ------
711  9999
712 (1 row)
713
714 explain (costs off)
715   select max(unique2) from tenk1 order by max(unique2)+1;
716                              QUERY PLAN                              
717 ---------------------------------------------------------------------
718  Sort
719    Sort Key: (($0 + 1))
720    InitPlan 1 (returns $0)
721      ->  Limit
722            ->  Index Only Scan Backward using tenk1_unique2 on tenk1
723                  Index Cond: (unique2 IS NOT NULL)
724    ->  Result
725 (7 rows)
726
727 select max(unique2) from tenk1 order by max(unique2)+1;
728  max  
729 ------
730  9999
731 (1 row)
732
733 explain (costs off)
734   select max(unique2), generate_series(1,3) as g from tenk1 order by g desc;
735                              QUERY PLAN                              
736 ---------------------------------------------------------------------
737  Sort
738    Sort Key: (generate_series(1, 3))
739    InitPlan 1 (returns $0)
740      ->  Limit
741            ->  Index Only Scan Backward using tenk1_unique2 on tenk1
742                  Index Cond: (unique2 IS NOT NULL)
743    ->  Result
744 (7 rows)
745
746 select max(unique2), generate_series(1,3) as g from tenk1 order by g desc;
747  max  | g 
748 ------+---
749  9999 | 3
750  9999 | 2
751  9999 | 1
752 (3 rows)
753
754 -- try it on an inheritance tree
755 create table minmaxtest(f1 int);
756 create table minmaxtest1() inherits (minmaxtest);
757 create table minmaxtest2() inherits (minmaxtest);
758 create table minmaxtest3() inherits (minmaxtest);
759 create index minmaxtesti on minmaxtest(f1);
760 create index minmaxtest1i on minmaxtest1(f1);
761 create index minmaxtest2i on minmaxtest2(f1 desc);
762 create index minmaxtest3i on minmaxtest3(f1) where f1 is not null;
763 insert into minmaxtest values(11), (12);
764 insert into minmaxtest1 values(13), (14);
765 insert into minmaxtest2 values(15), (16);
766 insert into minmaxtest3 values(17), (18);
767 explain (costs off)
768   select min(f1), max(f1) from minmaxtest;
769                                           QUERY PLAN                                          
770 ----------------------------------------------------------------------------------------------
771  Result
772    InitPlan 1 (returns $0)
773      ->  Limit
774            ->  Merge Append
775                  Sort Key: minmaxtest.f1
776                  ->  Index Only Scan using minmaxtesti on minmaxtest
777                        Index Cond: (f1 IS NOT NULL)
778                  ->  Index Only Scan using minmaxtest1i on minmaxtest1
779                        Index Cond: (f1 IS NOT NULL)
780                  ->  Index Only Scan Backward using minmaxtest2i on minmaxtest2
781                        Index Cond: (f1 IS NOT NULL)
782                  ->  Index Only Scan using minmaxtest3i on minmaxtest3
783                        Index Cond: (f1 IS NOT NULL)
784    InitPlan 2 (returns $1)
785      ->  Limit
786            ->  Merge Append
787                  Sort Key: minmaxtest_1.f1
788                  ->  Index Only Scan Backward using minmaxtesti on minmaxtest minmaxtest_1
789                        Index Cond: (f1 IS NOT NULL)
790                  ->  Index Only Scan Backward using minmaxtest1i on minmaxtest1 minmaxtest1_1
791                        Index Cond: (f1 IS NOT NULL)
792                  ->  Index Only Scan using minmaxtest2i on minmaxtest2 minmaxtest2_1
793                        Index Cond: (f1 IS NOT NULL)
794                  ->  Index Only Scan Backward using minmaxtest3i on minmaxtest3 minmaxtest3_1
795                        Index Cond: (f1 IS NOT NULL)
796 (25 rows)
797
798 select min(f1), max(f1) from minmaxtest;
799  min | max 
800 -----+-----
801   11 |  18
802 (1 row)
803
804 -- DISTINCT doesn't do anything useful here, but it shouldn't fail
805 explain (costs off)
806   select distinct min(f1), max(f1) from minmaxtest;
807                                           QUERY PLAN                                          
808 ----------------------------------------------------------------------------------------------
809  HashAggregate
810    Group Key: $0, $1
811    InitPlan 1 (returns $0)
812      ->  Limit
813            ->  Merge Append
814                  Sort Key: minmaxtest.f1
815                  ->  Index Only Scan using minmaxtesti on minmaxtest
816                        Index Cond: (f1 IS NOT NULL)
817                  ->  Index Only Scan using minmaxtest1i on minmaxtest1
818                        Index Cond: (f1 IS NOT NULL)
819                  ->  Index Only Scan Backward using minmaxtest2i on minmaxtest2
820                        Index Cond: (f1 IS NOT NULL)
821                  ->  Index Only Scan using minmaxtest3i on minmaxtest3
822                        Index Cond: (f1 IS NOT NULL)
823    InitPlan 2 (returns $1)
824      ->  Limit
825            ->  Merge Append
826                  Sort Key: minmaxtest_1.f1
827                  ->  Index Only Scan Backward using minmaxtesti on minmaxtest minmaxtest_1
828                        Index Cond: (f1 IS NOT NULL)
829                  ->  Index Only Scan Backward using minmaxtest1i on minmaxtest1 minmaxtest1_1
830                        Index Cond: (f1 IS NOT NULL)
831                  ->  Index Only Scan using minmaxtest2i on minmaxtest2 minmaxtest2_1
832                        Index Cond: (f1 IS NOT NULL)
833                  ->  Index Only Scan Backward using minmaxtest3i on minmaxtest3 minmaxtest3_1
834                        Index Cond: (f1 IS NOT NULL)
835    ->  Result
836 (27 rows)
837
838 select distinct min(f1), max(f1) from minmaxtest;
839  min | max 
840 -----+-----
841   11 |  18
842 (1 row)
843
844 drop table minmaxtest cascade;
845 NOTICE:  drop cascades to 3 other objects
846 DETAIL:  drop cascades to table minmaxtest1
847 drop cascades to table minmaxtest2
848 drop cascades to table minmaxtest3
849 -- check for correct detection of nested-aggregate errors
850 select max(min(unique1)) from tenk1;
851 ERROR:  aggregate function calls cannot be nested
852 LINE 1: select max(min(unique1)) from tenk1;
853                    ^
854 select (select max(min(unique1)) from int8_tbl) from tenk1;
855 ERROR:  aggregate function calls cannot be nested
856 LINE 1: select (select max(min(unique1)) from int8_tbl) from tenk1;
857                            ^
858 --
859 -- Test combinations of DISTINCT and/or ORDER BY
860 --
861 select array_agg(a order by b)
862   from (values (1,4),(2,3),(3,1),(4,2)) v(a,b);
863  array_agg 
864 -----------
865  {3,4,2,1}
866 (1 row)
867
868 select array_agg(a order by a)
869   from (values (1,4),(2,3),(3,1),(4,2)) v(a,b);
870  array_agg 
871 -----------
872  {1,2,3,4}
873 (1 row)
874
875 select array_agg(a order by a desc)
876   from (values (1,4),(2,3),(3,1),(4,2)) v(a,b);
877  array_agg 
878 -----------
879  {4,3,2,1}
880 (1 row)
881
882 select array_agg(b order by a desc)
883   from (values (1,4),(2,3),(3,1),(4,2)) v(a,b);
884  array_agg 
885 -----------
886  {2,1,3,4}
887 (1 row)
888
889 select array_agg(distinct a)
890   from (values (1),(2),(1),(3),(null),(2)) v(a);
891   array_agg   
892 --------------
893  {1,2,3,NULL}
894 (1 row)
895
896 select array_agg(distinct a order by a)
897   from (values (1),(2),(1),(3),(null),(2)) v(a);
898   array_agg   
899 --------------
900  {1,2,3,NULL}
901 (1 row)
902
903 select array_agg(distinct a order by a desc)
904   from (values (1),(2),(1),(3),(null),(2)) v(a);
905   array_agg   
906 --------------
907  {NULL,3,2,1}
908 (1 row)
909
910 select array_agg(distinct a order by a desc nulls last)
911   from (values (1),(2),(1),(3),(null),(2)) v(a);
912   array_agg   
913 --------------
914  {3,2,1,NULL}
915 (1 row)
916
917 -- multi-arg aggs, strict/nonstrict, distinct/order by
918 select aggfstr(a,b,c)
919   from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c);
920                 aggfstr                
921 ---------------------------------------
922  {"(1,3,foo)","(2,2,bar)","(3,1,baz)"}
923 (1 row)
924
925 select aggfns(a,b,c)
926   from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c);
927                     aggfns                     
928 -----------------------------------------------
929  {"(1,3,foo)","(0,,)","(2,2,bar)","(3,1,baz)"}
930 (1 row)
931
932 select aggfstr(distinct a,b,c)
933   from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
934        generate_series(1,3) i;
935                 aggfstr                
936 ---------------------------------------
937  {"(1,3,foo)","(2,2,bar)","(3,1,baz)"}
938 (1 row)
939
940 select aggfns(distinct a,b,c)
941   from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
942        generate_series(1,3) i;
943                     aggfns                     
944 -----------------------------------------------
945  {"(0,,)","(1,3,foo)","(2,2,bar)","(3,1,baz)"}
946 (1 row)
947
948 select aggfstr(distinct a,b,c order by b)
949   from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
950        generate_series(1,3) i;
951                 aggfstr                
952 ---------------------------------------
953  {"(3,1,baz)","(2,2,bar)","(1,3,foo)"}
954 (1 row)
955
956 select aggfns(distinct a,b,c order by b)
957   from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
958        generate_series(1,3) i;
959                     aggfns                     
960 -----------------------------------------------
961  {"(3,1,baz)","(2,2,bar)","(1,3,foo)","(0,,)"}
962 (1 row)
963
964 -- test specific code paths
965 select aggfns(distinct a,a,c order by c using ~<~,a)
966   from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
967        generate_series(1,2) i;
968                      aggfns                     
969 ------------------------------------------------
970  {"(2,2,bar)","(3,3,baz)","(1,1,foo)","(0,0,)"}
971 (1 row)
972
973 select aggfns(distinct a,a,c order by c using ~<~)
974   from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
975        generate_series(1,2) i;
976                      aggfns                     
977 ------------------------------------------------
978  {"(2,2,bar)","(3,3,baz)","(1,1,foo)","(0,0,)"}
979 (1 row)
980
981 select aggfns(distinct a,a,c order by a)
982   from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
983        generate_series(1,2) i;
984                      aggfns                     
985 ------------------------------------------------
986  {"(0,0,)","(1,1,foo)","(2,2,bar)","(3,3,baz)"}
987 (1 row)
988
989 select aggfns(distinct a,b,c order by a,c using ~<~,b)
990   from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
991        generate_series(1,2) i;
992                     aggfns                     
993 -----------------------------------------------
994  {"(0,,)","(1,3,foo)","(2,2,bar)","(3,1,baz)"}
995 (1 row)
996
997 -- check node I/O via view creation and usage, also deparsing logic
998 create view agg_view1 as
999   select aggfns(a,b,c)
1000     from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c);
1001 select * from agg_view1;
1002                     aggfns                     
1003 -----------------------------------------------
1004  {"(1,3,foo)","(0,,)","(2,2,bar)","(3,1,baz)"}
1005 (1 row)
1006
1007 select pg_get_viewdef('agg_view1'::regclass);
1008                                                    pg_get_viewdef                                                    
1009 ---------------------------------------------------------------------------------------------------------------------
1010   SELECT aggfns(v.a, v.b, v.c) AS aggfns                                                                            +
1011     FROM ( VALUES (1,3,'foo'::text), (0,NULL::integer,NULL::text), (2,2,'bar'::text), (3,1,'baz'::text)) v(a, b, c);
1012 (1 row)
1013
1014 create or replace view agg_view1 as
1015   select aggfns(distinct a,b,c)
1016     from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
1017          generate_series(1,3) i;
1018 select * from agg_view1;
1019                     aggfns                     
1020 -----------------------------------------------
1021  {"(0,,)","(1,3,foo)","(2,2,bar)","(3,1,baz)"}
1022 (1 row)
1023
1024 select pg_get_viewdef('agg_view1'::regclass);
1025                                                    pg_get_viewdef                                                    
1026 ---------------------------------------------------------------------------------------------------------------------
1027   SELECT aggfns(DISTINCT v.a, v.b, v.c) AS aggfns                                                                   +
1028     FROM ( VALUES (1,3,'foo'::text), (0,NULL::integer,NULL::text), (2,2,'bar'::text), (3,1,'baz'::text)) v(a, b, c),+
1029      generate_series(1, 3) i(i);
1030 (1 row)
1031
1032 create or replace view agg_view1 as
1033   select aggfns(distinct a,b,c order by b)
1034     from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
1035          generate_series(1,3) i;
1036 select * from agg_view1;
1037                     aggfns                     
1038 -----------------------------------------------
1039  {"(3,1,baz)","(2,2,bar)","(1,3,foo)","(0,,)"}
1040 (1 row)
1041
1042 select pg_get_viewdef('agg_view1'::regclass);
1043                                                    pg_get_viewdef                                                    
1044 ---------------------------------------------------------------------------------------------------------------------
1045   SELECT aggfns(DISTINCT v.a, v.b, v.c ORDER BY v.b) AS aggfns                                                      +
1046     FROM ( VALUES (1,3,'foo'::text), (0,NULL::integer,NULL::text), (2,2,'bar'::text), (3,1,'baz'::text)) v(a, b, c),+
1047      generate_series(1, 3) i(i);
1048 (1 row)
1049
1050 create or replace view agg_view1 as
1051   select aggfns(a,b,c order by b+1)
1052     from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c);
1053 select * from agg_view1;
1054                     aggfns                     
1055 -----------------------------------------------
1056  {"(3,1,baz)","(2,2,bar)","(1,3,foo)","(0,,)"}
1057 (1 row)
1058
1059 select pg_get_viewdef('agg_view1'::regclass);
1060                                                    pg_get_viewdef                                                    
1061 ---------------------------------------------------------------------------------------------------------------------
1062   SELECT aggfns(v.a, v.b, v.c ORDER BY (v.b + 1)) AS aggfns                                                         +
1063     FROM ( VALUES (1,3,'foo'::text), (0,NULL::integer,NULL::text), (2,2,'bar'::text), (3,1,'baz'::text)) v(a, b, c);
1064 (1 row)
1065
1066 create or replace view agg_view1 as
1067   select aggfns(a,a,c order by b)
1068     from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c);
1069 select * from agg_view1;
1070                      aggfns                     
1071 ------------------------------------------------
1072  {"(3,3,baz)","(2,2,bar)","(1,1,foo)","(0,0,)"}
1073 (1 row)
1074
1075 select pg_get_viewdef('agg_view1'::regclass);
1076                                                    pg_get_viewdef                                                    
1077 ---------------------------------------------------------------------------------------------------------------------
1078   SELECT aggfns(v.a, v.a, v.c ORDER BY v.b) AS aggfns                                                               +
1079     FROM ( VALUES (1,3,'foo'::text), (0,NULL::integer,NULL::text), (2,2,'bar'::text), (3,1,'baz'::text)) v(a, b, c);
1080 (1 row)
1081
1082 create or replace view agg_view1 as
1083   select aggfns(a,b,c order by c using ~<~)
1084     from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c);
1085 select * from agg_view1;
1086                     aggfns                     
1087 -----------------------------------------------
1088  {"(2,2,bar)","(3,1,baz)","(1,3,foo)","(0,,)"}
1089 (1 row)
1090
1091 select pg_get_viewdef('agg_view1'::regclass);
1092                                                    pg_get_viewdef                                                    
1093 ---------------------------------------------------------------------------------------------------------------------
1094   SELECT aggfns(v.a, v.b, v.c ORDER BY v.c USING ~<~ NULLS LAST) AS aggfns                                          +
1095     FROM ( VALUES (1,3,'foo'::text), (0,NULL::integer,NULL::text), (2,2,'bar'::text), (3,1,'baz'::text)) v(a, b, c);
1096 (1 row)
1097
1098 create or replace view agg_view1 as
1099   select aggfns(distinct a,b,c order by a,c using ~<~,b)
1100     from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
1101          generate_series(1,2) i;
1102 select * from agg_view1;
1103                     aggfns                     
1104 -----------------------------------------------
1105  {"(0,,)","(1,3,foo)","(2,2,bar)","(3,1,baz)"}
1106 (1 row)
1107
1108 select pg_get_viewdef('agg_view1'::regclass);
1109                                                    pg_get_viewdef                                                    
1110 ---------------------------------------------------------------------------------------------------------------------
1111   SELECT aggfns(DISTINCT v.a, v.b, v.c ORDER BY v.a, v.c USING ~<~ NULLS LAST, v.b) AS aggfns                       +
1112     FROM ( VALUES (1,3,'foo'::text), (0,NULL::integer,NULL::text), (2,2,'bar'::text), (3,1,'baz'::text)) v(a, b, c),+
1113      generate_series(1, 2) i(i);
1114 (1 row)
1115
1116 drop view agg_view1;
1117 -- incorrect DISTINCT usage errors
1118 select aggfns(distinct a,b,c order by i)
1119   from (values (1,1,'foo')) v(a,b,c), generate_series(1,2) i;
1120 ERROR:  in an aggregate with DISTINCT, ORDER BY expressions must appear in argument list
1121 LINE 1: select aggfns(distinct a,b,c order by i)
1122                                               ^
1123 select aggfns(distinct a,b,c order by a,b+1)
1124   from (values (1,1,'foo')) v(a,b,c), generate_series(1,2) i;
1125 ERROR:  in an aggregate with DISTINCT, ORDER BY expressions must appear in argument list
1126 LINE 1: select aggfns(distinct a,b,c order by a,b+1)
1127                                                 ^
1128 select aggfns(distinct a,b,c order by a,b,i,c)
1129   from (values (1,1,'foo')) v(a,b,c), generate_series(1,2) i;
1130 ERROR:  in an aggregate with DISTINCT, ORDER BY expressions must appear in argument list
1131 LINE 1: select aggfns(distinct a,b,c order by a,b,i,c)
1132                                                   ^
1133 select aggfns(distinct a,a,c order by a,b)
1134   from (values (1,1,'foo')) v(a,b,c), generate_series(1,2) i;
1135 ERROR:  in an aggregate with DISTINCT, ORDER BY expressions must appear in argument list
1136 LINE 1: select aggfns(distinct a,a,c order by a,b)
1137                                                 ^
1138 -- string_agg tests
1139 select string_agg(a,',') from (values('aaaa'),('bbbb'),('cccc')) g(a);
1140    string_agg   
1141 ----------------
1142  aaaa,bbbb,cccc
1143 (1 row)
1144
1145 select string_agg(a,',') from (values('aaaa'),(null),('bbbb'),('cccc')) g(a);
1146    string_agg   
1147 ----------------
1148  aaaa,bbbb,cccc
1149 (1 row)
1150
1151 select string_agg(a,'AB') from (values(null),(null),('bbbb'),('cccc')) g(a);
1152  string_agg 
1153 ------------
1154  bbbbABcccc
1155 (1 row)
1156
1157 select string_agg(a,',') from (values(null),(null)) g(a);
1158  string_agg 
1159 ------------
1160  
1161 (1 row)
1162
1163 -- check some implicit casting cases, as per bug #5564
1164 select string_agg(distinct f1, ',' order by f1) from varchar_tbl;  -- ok
1165  string_agg 
1166 ------------
1167  a,ab,abcd
1168 (1 row)
1169
1170 select string_agg(distinct f1::text, ',' order by f1) from varchar_tbl;  -- not ok
1171 ERROR:  in an aggregate with DISTINCT, ORDER BY expressions must appear in argument list
1172 LINE 1: select string_agg(distinct f1::text, ',' order by f1) from v...
1173                                                           ^
1174 select string_agg(distinct f1, ',' order by f1::text) from varchar_tbl;  -- not ok
1175 ERROR:  in an aggregate with DISTINCT, ORDER BY expressions must appear in argument list
1176 LINE 1: select string_agg(distinct f1, ',' order by f1::text) from v...
1177                                                     ^
1178 select string_agg(distinct f1::text, ',' order by f1::text) from varchar_tbl;  -- ok
1179  string_agg 
1180 ------------
1181  a,ab,abcd
1182 (1 row)
1183
1184 -- string_agg bytea tests
1185 create table bytea_test_table(v bytea);
1186 select string_agg(v, '') from bytea_test_table;
1187  string_agg 
1188 ------------
1189  
1190 (1 row)
1191
1192 insert into bytea_test_table values(decode('ff','hex'));
1193 select string_agg(v, '') from bytea_test_table;
1194  string_agg 
1195 ------------
1196  \xff
1197 (1 row)
1198
1199 insert into bytea_test_table values(decode('aa','hex'));
1200 select string_agg(v, '') from bytea_test_table;
1201  string_agg 
1202 ------------
1203  \xffaa
1204 (1 row)
1205
1206 select string_agg(v, NULL) from bytea_test_table;
1207  string_agg 
1208 ------------
1209  \xffaa
1210 (1 row)
1211
1212 select string_agg(v, decode('ee', 'hex')) from bytea_test_table;
1213  string_agg 
1214 ------------
1215  \xffeeaa
1216 (1 row)
1217
1218 drop table bytea_test_table;
1219 -- FILTER tests
1220 select min(unique1) filter (where unique1 > 100) from tenk1;
1221  min 
1222 -----
1223  101
1224 (1 row)
1225
1226 select ten, sum(distinct four) filter (where four::text ~ '123') from onek a
1227 group by ten;
1228  ten | sum 
1229 -----+-----
1230    0 |    
1231    1 |    
1232    2 |    
1233    3 |    
1234    4 |    
1235    5 |    
1236    6 |    
1237    7 |    
1238    8 |    
1239    9 |    
1240 (10 rows)
1241
1242 select ten, sum(distinct four) filter (where four > 10) from onek a
1243 group by ten
1244 having exists (select 1 from onek b where sum(distinct a.four) = b.four);
1245  ten | sum 
1246 -----+-----
1247    0 |    
1248    2 |    
1249    4 |    
1250    6 |    
1251    8 |    
1252 (5 rows)
1253
1254 select max(foo COLLATE "C") filter (where (bar collate "POSIX") > '0')
1255 from (values ('a', 'b')) AS v(foo,bar);
1256  max 
1257 -----
1258  a
1259 (1 row)
1260
1261 -- outer reference in FILTER (PostgreSQL extension)
1262 select (select count(*)
1263         from (values (1)) t0(inner_c))
1264 from (values (2),(3)) t1(outer_c); -- inner query is aggregation query
1265  count 
1266 -------
1267      1
1268      1
1269 (2 rows)
1270
1271 select (select count(*) filter (where outer_c <> 0)
1272         from (values (1)) t0(inner_c))
1273 from (values (2),(3)) t1(outer_c); -- outer query is aggregation query
1274  count 
1275 -------
1276      2
1277 (1 row)
1278
1279 select (select count(inner_c) filter (where outer_c <> 0)
1280         from (values (1)) t0(inner_c))
1281 from (values (2),(3)) t1(outer_c); -- inner query is aggregation query
1282  count 
1283 -------
1284      1
1285      1
1286 (2 rows)
1287
1288 select
1289   (select max((select i.unique2 from tenk1 i where i.unique1 = o.unique1))
1290      filter (where o.unique1 < 10))
1291 from tenk1 o;                                   -- outer query is aggregation query
1292  max  
1293 ------
1294  9998
1295 (1 row)
1296
1297 -- subquery in FILTER clause (PostgreSQL extension)
1298 select sum(unique1) FILTER (WHERE
1299   unique1 IN (SELECT unique1 FROM onek where unique1 < 100)) FROM tenk1;
1300  sum  
1301 ------
1302  4950
1303 (1 row)
1304
1305 -- exercise lots of aggregate parts with FILTER
1306 select aggfns(distinct a,b,c order by a,c using ~<~,b) filter (where a > 1)
1307     from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
1308     generate_series(1,2) i;
1309           aggfns           
1310 ---------------------------
1311  {"(2,2,bar)","(3,1,baz)"}
1312 (1 row)
1313
1314 -- variadic aggregates
1315 select least_agg(q1,q2) from int8_tbl;
1316      least_agg     
1317 -------------------
1318  -4567890123456789
1319 (1 row)
1320
1321 select least_agg(variadic array[q1,q2]) from int8_tbl;
1322      least_agg     
1323 -------------------
1324  -4567890123456789
1325 (1 row)
1326