]> granicus.if.org Git - postgresql/blob - src/test/regress/expected/aggregates.out
Tweak placement of explicit ANALYZE commands in the regression tests.
[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    InitPlan 1 (returns $0)
663      ->  Limit
664            ->  Index Only Scan Backward using tenk1_unique2 on tenk1
665                  Index Cond: (unique2 IS NOT NULL)
666    ->  Result
667 (6 rows)
668
669 select distinct max(unique2) from tenk1;
670  max  
671 ------
672  9999
673 (1 row)
674
675 explain (costs off)
676   select max(unique2) from tenk1 order by 1;
677                              QUERY PLAN                              
678 ---------------------------------------------------------------------
679  Sort
680    Sort Key: ($0)
681    InitPlan 1 (returns $0)
682      ->  Limit
683            ->  Index Only Scan Backward using tenk1_unique2 on tenk1
684                  Index Cond: (unique2 IS NOT NULL)
685    ->  Result
686 (7 rows)
687
688 select max(unique2) from tenk1 order by 1;
689  max  
690 ------
691  9999
692 (1 row)
693
694 explain (costs off)
695   select max(unique2) from tenk1 order by max(unique2);
696                              QUERY PLAN                              
697 ---------------------------------------------------------------------
698  Sort
699    Sort Key: ($0)
700    InitPlan 1 (returns $0)
701      ->  Limit
702            ->  Index Only Scan Backward using tenk1_unique2 on tenk1
703                  Index Cond: (unique2 IS NOT NULL)
704    ->  Result
705 (7 rows)
706
707 select max(unique2) from tenk1 order by max(unique2);
708  max  
709 ------
710  9999
711 (1 row)
712
713 explain (costs off)
714   select max(unique2) from tenk1 order by max(unique2)+1;
715                              QUERY PLAN                              
716 ---------------------------------------------------------------------
717  Sort
718    Sort Key: (($0 + 1))
719    InitPlan 1 (returns $0)
720      ->  Limit
721            ->  Index Only Scan Backward using tenk1_unique2 on tenk1
722                  Index Cond: (unique2 IS NOT NULL)
723    ->  Result
724 (7 rows)
725
726 select max(unique2) from tenk1 order by max(unique2)+1;
727  max  
728 ------
729  9999
730 (1 row)
731
732 explain (costs off)
733   select max(unique2), generate_series(1,3) as g from tenk1 order by g desc;
734                              QUERY PLAN                              
735 ---------------------------------------------------------------------
736  Sort
737    Sort Key: (generate_series(1, 3))
738    InitPlan 1 (returns $0)
739      ->  Limit
740            ->  Index Only Scan Backward using tenk1_unique2 on tenk1
741                  Index Cond: (unique2 IS NOT NULL)
742    ->  Result
743 (7 rows)
744
745 select max(unique2), generate_series(1,3) as g from tenk1 order by g desc;
746  max  | g 
747 ------+---
748  9999 | 3
749  9999 | 2
750  9999 | 1
751 (3 rows)
752
753 -- try it on an inheritance tree
754 create table minmaxtest(f1 int);
755 create table minmaxtest1() inherits (minmaxtest);
756 create table minmaxtest2() inherits (minmaxtest);
757 create table minmaxtest3() inherits (minmaxtest);
758 create index minmaxtesti on minmaxtest(f1);
759 create index minmaxtest1i on minmaxtest1(f1);
760 create index minmaxtest2i on minmaxtest2(f1 desc);
761 create index minmaxtest3i on minmaxtest3(f1) where f1 is not null;
762 insert into minmaxtest values(11), (12);
763 insert into minmaxtest1 values(13), (14);
764 insert into minmaxtest2 values(15), (16);
765 insert into minmaxtest3 values(17), (18);
766 explain (costs off)
767   select min(f1), max(f1) from minmaxtest;
768                                           QUERY PLAN                                          
769 ----------------------------------------------------------------------------------------------
770  Result
771    InitPlan 1 (returns $0)
772      ->  Limit
773            ->  Merge Append
774                  Sort Key: minmaxtest.f1
775                  ->  Index Only Scan using minmaxtesti on minmaxtest
776                        Index Cond: (f1 IS NOT NULL)
777                  ->  Index Only Scan using minmaxtest1i on minmaxtest1
778                        Index Cond: (f1 IS NOT NULL)
779                  ->  Index Only Scan Backward using minmaxtest2i on minmaxtest2
780                        Index Cond: (f1 IS NOT NULL)
781                  ->  Index Only Scan using minmaxtest3i on minmaxtest3
782                        Index Cond: (f1 IS NOT NULL)
783    InitPlan 2 (returns $1)
784      ->  Limit
785            ->  Merge Append
786                  Sort Key: minmaxtest_1.f1
787                  ->  Index Only Scan Backward using minmaxtesti on minmaxtest minmaxtest_1
788                        Index Cond: (f1 IS NOT NULL)
789                  ->  Index Only Scan Backward using minmaxtest1i on minmaxtest1 minmaxtest1_1
790                        Index Cond: (f1 IS NOT NULL)
791                  ->  Index Only Scan using minmaxtest2i on minmaxtest2 minmaxtest2_1
792                        Index Cond: (f1 IS NOT NULL)
793                  ->  Index Only Scan Backward using minmaxtest3i on minmaxtest3 minmaxtest3_1
794                        Index Cond: (f1 IS NOT NULL)
795 (25 rows)
796
797 select min(f1), max(f1) from minmaxtest;
798  min | max 
799 -----+-----
800   11 |  18
801 (1 row)
802
803 -- DISTINCT doesn't do anything useful here, but it shouldn't fail
804 explain (costs off)
805   select distinct min(f1), max(f1) from minmaxtest;
806                                           QUERY PLAN                                          
807 ----------------------------------------------------------------------------------------------
808  HashAggregate
809    InitPlan 1 (returns $0)
810      ->  Limit
811            ->  Merge Append
812                  Sort Key: minmaxtest.f1
813                  ->  Index Only Scan using minmaxtesti on minmaxtest
814                        Index Cond: (f1 IS NOT NULL)
815                  ->  Index Only Scan using minmaxtest1i on minmaxtest1
816                        Index Cond: (f1 IS NOT NULL)
817                  ->  Index Only Scan Backward using minmaxtest2i on minmaxtest2
818                        Index Cond: (f1 IS NOT NULL)
819                  ->  Index Only Scan using minmaxtest3i on minmaxtest3
820                        Index Cond: (f1 IS NOT NULL)
821    InitPlan 2 (returns $1)
822      ->  Limit
823            ->  Merge Append
824                  Sort Key: minmaxtest_1.f1
825                  ->  Index Only Scan Backward using minmaxtesti on minmaxtest minmaxtest_1
826                        Index Cond: (f1 IS NOT NULL)
827                  ->  Index Only Scan Backward using minmaxtest1i on minmaxtest1 minmaxtest1_1
828                        Index Cond: (f1 IS NOT NULL)
829                  ->  Index Only Scan using minmaxtest2i on minmaxtest2 minmaxtest2_1
830                        Index Cond: (f1 IS NOT NULL)
831                  ->  Index Only Scan Backward using minmaxtest3i on minmaxtest3 minmaxtest3_1
832                        Index Cond: (f1 IS NOT NULL)
833    ->  Result
834 (26 rows)
835
836 select distinct min(f1), max(f1) from minmaxtest;
837  min | max 
838 -----+-----
839   11 |  18
840 (1 row)
841
842 drop table minmaxtest cascade;
843 NOTICE:  drop cascades to 3 other objects
844 DETAIL:  drop cascades to table minmaxtest1
845 drop cascades to table minmaxtest2
846 drop cascades to table minmaxtest3
847 -- check for correct detection of nested-aggregate errors
848 select max(min(unique1)) from tenk1;
849 ERROR:  aggregate function calls cannot be nested
850 LINE 1: select max(min(unique1)) from tenk1;
851                    ^
852 select (select max(min(unique1)) from int8_tbl) from tenk1;
853 ERROR:  aggregate function calls cannot be nested
854 LINE 1: select (select max(min(unique1)) from int8_tbl) from tenk1;
855                            ^
856 --
857 -- Test combinations of DISTINCT and/or ORDER BY
858 --
859 select array_agg(a order by b)
860   from (values (1,4),(2,3),(3,1),(4,2)) v(a,b);
861  array_agg 
862 -----------
863  {3,4,2,1}
864 (1 row)
865
866 select array_agg(a order by a)
867   from (values (1,4),(2,3),(3,1),(4,2)) v(a,b);
868  array_agg 
869 -----------
870  {1,2,3,4}
871 (1 row)
872
873 select array_agg(a order by a desc)
874   from (values (1,4),(2,3),(3,1),(4,2)) v(a,b);
875  array_agg 
876 -----------
877  {4,3,2,1}
878 (1 row)
879
880 select array_agg(b order by a desc)
881   from (values (1,4),(2,3),(3,1),(4,2)) v(a,b);
882  array_agg 
883 -----------
884  {2,1,3,4}
885 (1 row)
886
887 select array_agg(distinct a)
888   from (values (1),(2),(1),(3),(null),(2)) v(a);
889   array_agg   
890 --------------
891  {1,2,3,NULL}
892 (1 row)
893
894 select array_agg(distinct a order by a)
895   from (values (1),(2),(1),(3),(null),(2)) v(a);
896   array_agg   
897 --------------
898  {1,2,3,NULL}
899 (1 row)
900
901 select array_agg(distinct a order by a desc)
902   from (values (1),(2),(1),(3),(null),(2)) v(a);
903   array_agg   
904 --------------
905  {NULL,3,2,1}
906 (1 row)
907
908 select array_agg(distinct a order by a desc nulls last)
909   from (values (1),(2),(1),(3),(null),(2)) v(a);
910   array_agg   
911 --------------
912  {3,2,1,NULL}
913 (1 row)
914
915 -- multi-arg aggs, strict/nonstrict, distinct/order by
916 select aggfstr(a,b,c)
917   from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c);
918                 aggfstr                
919 ---------------------------------------
920  {"(1,3,foo)","(2,2,bar)","(3,1,baz)"}
921 (1 row)
922
923 select aggfns(a,b,c)
924   from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c);
925                     aggfns                     
926 -----------------------------------------------
927  {"(1,3,foo)","(0,,)","(2,2,bar)","(3,1,baz)"}
928 (1 row)
929
930 select aggfstr(distinct a,b,c)
931   from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
932        generate_series(1,3) i;
933                 aggfstr                
934 ---------------------------------------
935  {"(1,3,foo)","(2,2,bar)","(3,1,baz)"}
936 (1 row)
937
938 select aggfns(distinct a,b,c)
939   from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
940        generate_series(1,3) i;
941                     aggfns                     
942 -----------------------------------------------
943  {"(0,,)","(1,3,foo)","(2,2,bar)","(3,1,baz)"}
944 (1 row)
945
946 select aggfstr(distinct a,b,c order by b)
947   from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
948        generate_series(1,3) i;
949                 aggfstr                
950 ---------------------------------------
951  {"(3,1,baz)","(2,2,bar)","(1,3,foo)"}
952 (1 row)
953
954 select aggfns(distinct a,b,c order by b)
955   from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
956        generate_series(1,3) i;
957                     aggfns                     
958 -----------------------------------------------
959  {"(3,1,baz)","(2,2,bar)","(1,3,foo)","(0,,)"}
960 (1 row)
961
962 -- test specific code paths
963 select aggfns(distinct a,a,c order by c using ~<~,a)
964   from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
965        generate_series(1,2) i;
966                      aggfns                     
967 ------------------------------------------------
968  {"(2,2,bar)","(3,3,baz)","(1,1,foo)","(0,0,)"}
969 (1 row)
970
971 select aggfns(distinct a,a,c order by c using ~<~)
972   from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
973        generate_series(1,2) i;
974                      aggfns                     
975 ------------------------------------------------
976  {"(2,2,bar)","(3,3,baz)","(1,1,foo)","(0,0,)"}
977 (1 row)
978
979 select aggfns(distinct a,a,c order by a)
980   from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
981        generate_series(1,2) i;
982                      aggfns                     
983 ------------------------------------------------
984  {"(0,0,)","(1,1,foo)","(2,2,bar)","(3,3,baz)"}
985 (1 row)
986
987 select aggfns(distinct a,b,c order by a,c using ~<~,b)
988   from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
989        generate_series(1,2) i;
990                     aggfns                     
991 -----------------------------------------------
992  {"(0,,)","(1,3,foo)","(2,2,bar)","(3,1,baz)"}
993 (1 row)
994
995 -- check node I/O via view creation and usage, also deparsing logic
996 create view agg_view1 as
997   select aggfns(a,b,c)
998     from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c);
999 select * from agg_view1;
1000                     aggfns                     
1001 -----------------------------------------------
1002  {"(1,3,foo)","(0,,)","(2,2,bar)","(3,1,baz)"}
1003 (1 row)
1004
1005 select pg_get_viewdef('agg_view1'::regclass);
1006                                                    pg_get_viewdef                                                    
1007 ---------------------------------------------------------------------------------------------------------------------
1008   SELECT aggfns(v.a, v.b, v.c) AS aggfns                                                                            +
1009     FROM ( VALUES (1,3,'foo'::text), (0,NULL::integer,NULL::text), (2,2,'bar'::text), (3,1,'baz'::text)) v(a, b, c);
1010 (1 row)
1011
1012 create or replace view agg_view1 as
1013   select aggfns(distinct a,b,c)
1014     from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
1015          generate_series(1,3) i;
1016 select * from agg_view1;
1017                     aggfns                     
1018 -----------------------------------------------
1019  {"(0,,)","(1,3,foo)","(2,2,bar)","(3,1,baz)"}
1020 (1 row)
1021
1022 select pg_get_viewdef('agg_view1'::regclass);
1023                                                    pg_get_viewdef                                                    
1024 ---------------------------------------------------------------------------------------------------------------------
1025   SELECT aggfns(DISTINCT v.a, v.b, v.c) AS aggfns                                                                   +
1026     FROM ( VALUES (1,3,'foo'::text), (0,NULL::integer,NULL::text), (2,2,'bar'::text), (3,1,'baz'::text)) v(a, b, c),+
1027      generate_series(1, 3) i(i);
1028 (1 row)
1029
1030 create or replace view agg_view1 as
1031   select aggfns(distinct a,b,c order by b)
1032     from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
1033          generate_series(1,3) i;
1034 select * from agg_view1;
1035                     aggfns                     
1036 -----------------------------------------------
1037  {"(3,1,baz)","(2,2,bar)","(1,3,foo)","(0,,)"}
1038 (1 row)
1039
1040 select pg_get_viewdef('agg_view1'::regclass);
1041                                                    pg_get_viewdef                                                    
1042 ---------------------------------------------------------------------------------------------------------------------
1043   SELECT aggfns(DISTINCT v.a, v.b, v.c ORDER BY v.b) AS aggfns                                                      +
1044     FROM ( VALUES (1,3,'foo'::text), (0,NULL::integer,NULL::text), (2,2,'bar'::text), (3,1,'baz'::text)) v(a, b, c),+
1045      generate_series(1, 3) i(i);
1046 (1 row)
1047
1048 create or replace view agg_view1 as
1049   select aggfns(a,b,c order by b+1)
1050     from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c);
1051 select * from agg_view1;
1052                     aggfns                     
1053 -----------------------------------------------
1054  {"(3,1,baz)","(2,2,bar)","(1,3,foo)","(0,,)"}
1055 (1 row)
1056
1057 select pg_get_viewdef('agg_view1'::regclass);
1058                                                    pg_get_viewdef                                                    
1059 ---------------------------------------------------------------------------------------------------------------------
1060   SELECT aggfns(v.a, v.b, v.c ORDER BY (v.b + 1)) AS aggfns                                                         +
1061     FROM ( VALUES (1,3,'foo'::text), (0,NULL::integer,NULL::text), (2,2,'bar'::text), (3,1,'baz'::text)) v(a, b, c);
1062 (1 row)
1063
1064 create or replace view agg_view1 as
1065   select aggfns(a,a,c order by b)
1066     from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c);
1067 select * from agg_view1;
1068                      aggfns                     
1069 ------------------------------------------------
1070  {"(3,3,baz)","(2,2,bar)","(1,1,foo)","(0,0,)"}
1071 (1 row)
1072
1073 select pg_get_viewdef('agg_view1'::regclass);
1074                                                    pg_get_viewdef                                                    
1075 ---------------------------------------------------------------------------------------------------------------------
1076   SELECT aggfns(v.a, v.a, v.c ORDER BY v.b) AS aggfns                                                               +
1077     FROM ( VALUES (1,3,'foo'::text), (0,NULL::integer,NULL::text), (2,2,'bar'::text), (3,1,'baz'::text)) v(a, b, c);
1078 (1 row)
1079
1080 create or replace view agg_view1 as
1081   select aggfns(a,b,c order by c using ~<~)
1082     from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c);
1083 select * from agg_view1;
1084                     aggfns                     
1085 -----------------------------------------------
1086  {"(2,2,bar)","(3,1,baz)","(1,3,foo)","(0,,)"}
1087 (1 row)
1088
1089 select pg_get_viewdef('agg_view1'::regclass);
1090                                                    pg_get_viewdef                                                    
1091 ---------------------------------------------------------------------------------------------------------------------
1092   SELECT aggfns(v.a, v.b, v.c ORDER BY v.c USING ~<~ NULLS LAST) AS aggfns                                          +
1093     FROM ( VALUES (1,3,'foo'::text), (0,NULL::integer,NULL::text), (2,2,'bar'::text), (3,1,'baz'::text)) v(a, b, c);
1094 (1 row)
1095
1096 create or replace view agg_view1 as
1097   select aggfns(distinct a,b,c order by a,c using ~<~,b)
1098     from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
1099          generate_series(1,2) i;
1100 select * from agg_view1;
1101                     aggfns                     
1102 -----------------------------------------------
1103  {"(0,,)","(1,3,foo)","(2,2,bar)","(3,1,baz)"}
1104 (1 row)
1105
1106 select pg_get_viewdef('agg_view1'::regclass);
1107                                                    pg_get_viewdef                                                    
1108 ---------------------------------------------------------------------------------------------------------------------
1109   SELECT aggfns(DISTINCT v.a, v.b, v.c ORDER BY v.a, v.c USING ~<~ NULLS LAST, v.b) AS aggfns                       +
1110     FROM ( VALUES (1,3,'foo'::text), (0,NULL::integer,NULL::text), (2,2,'bar'::text), (3,1,'baz'::text)) v(a, b, c),+
1111      generate_series(1, 2) i(i);
1112 (1 row)
1113
1114 drop view agg_view1;
1115 -- incorrect DISTINCT usage errors
1116 select aggfns(distinct a,b,c order by i)
1117   from (values (1,1,'foo')) v(a,b,c), generate_series(1,2) i;
1118 ERROR:  in an aggregate with DISTINCT, ORDER BY expressions must appear in argument list
1119 LINE 1: select aggfns(distinct a,b,c order by i)
1120                                               ^
1121 select aggfns(distinct a,b,c order by a,b+1)
1122   from (values (1,1,'foo')) v(a,b,c), generate_series(1,2) i;
1123 ERROR:  in an aggregate with DISTINCT, ORDER BY expressions must appear in argument list
1124 LINE 1: select aggfns(distinct a,b,c order by a,b+1)
1125                                                 ^
1126 select aggfns(distinct a,b,c order by a,b,i,c)
1127   from (values (1,1,'foo')) v(a,b,c), generate_series(1,2) i;
1128 ERROR:  in an aggregate with DISTINCT, ORDER BY expressions must appear in argument list
1129 LINE 1: select aggfns(distinct a,b,c order by a,b,i,c)
1130                                                   ^
1131 select aggfns(distinct a,a,c order by a,b)
1132   from (values (1,1,'foo')) v(a,b,c), generate_series(1,2) i;
1133 ERROR:  in an aggregate with DISTINCT, ORDER BY expressions must appear in argument list
1134 LINE 1: select aggfns(distinct a,a,c order by a,b)
1135                                                 ^
1136 -- string_agg tests
1137 select string_agg(a,',') from (values('aaaa'),('bbbb'),('cccc')) g(a);
1138    string_agg   
1139 ----------------
1140  aaaa,bbbb,cccc
1141 (1 row)
1142
1143 select string_agg(a,',') from (values('aaaa'),(null),('bbbb'),('cccc')) g(a);
1144    string_agg   
1145 ----------------
1146  aaaa,bbbb,cccc
1147 (1 row)
1148
1149 select string_agg(a,'AB') from (values(null),(null),('bbbb'),('cccc')) g(a);
1150  string_agg 
1151 ------------
1152  bbbbABcccc
1153 (1 row)
1154
1155 select string_agg(a,',') from (values(null),(null)) g(a);
1156  string_agg 
1157 ------------
1158  
1159 (1 row)
1160
1161 -- check some implicit casting cases, as per bug #5564
1162 select string_agg(distinct f1, ',' order by f1) from varchar_tbl;  -- ok
1163  string_agg 
1164 ------------
1165  a,ab,abcd
1166 (1 row)
1167
1168 select string_agg(distinct f1::text, ',' order by f1) from varchar_tbl;  -- not ok
1169 ERROR:  in an aggregate with DISTINCT, ORDER BY expressions must appear in argument list
1170 LINE 1: select string_agg(distinct f1::text, ',' order by f1) from v...
1171                                                           ^
1172 select string_agg(distinct f1, ',' order by f1::text) from varchar_tbl;  -- not ok
1173 ERROR:  in an aggregate with DISTINCT, ORDER BY expressions must appear in argument list
1174 LINE 1: select string_agg(distinct f1, ',' order by f1::text) from v...
1175                                                     ^
1176 select string_agg(distinct f1::text, ',' order by f1::text) from varchar_tbl;  -- ok
1177  string_agg 
1178 ------------
1179  a,ab,abcd
1180 (1 row)
1181
1182 -- string_agg bytea tests
1183 create table bytea_test_table(v bytea);
1184 select string_agg(v, '') from bytea_test_table;
1185  string_agg 
1186 ------------
1187  
1188 (1 row)
1189
1190 insert into bytea_test_table values(decode('ff','hex'));
1191 select string_agg(v, '') from bytea_test_table;
1192  string_agg 
1193 ------------
1194  \xff
1195 (1 row)
1196
1197 insert into bytea_test_table values(decode('aa','hex'));
1198 select string_agg(v, '') from bytea_test_table;
1199  string_agg 
1200 ------------
1201  \xffaa
1202 (1 row)
1203
1204 select string_agg(v, NULL) from bytea_test_table;
1205  string_agg 
1206 ------------
1207  \xffaa
1208 (1 row)
1209
1210 select string_agg(v, decode('ee', 'hex')) from bytea_test_table;
1211  string_agg 
1212 ------------
1213  \xffeeaa
1214 (1 row)
1215
1216 drop table bytea_test_table;
1217 -- FILTER tests
1218 select min(unique1) filter (where unique1 > 100) from tenk1;
1219  min 
1220 -----
1221  101
1222 (1 row)
1223
1224 select ten, sum(distinct four) filter (where four::text ~ '123') from onek a
1225 group by ten;
1226  ten | sum 
1227 -----+-----
1228    0 |    
1229    1 |    
1230    2 |    
1231    3 |    
1232    4 |    
1233    5 |    
1234    6 |    
1235    7 |    
1236    8 |    
1237    9 |    
1238 (10 rows)
1239
1240 select ten, sum(distinct four) filter (where four > 10) from onek a
1241 group by ten
1242 having exists (select 1 from onek b where sum(distinct a.four) = b.four);
1243  ten | sum 
1244 -----+-----
1245    0 |    
1246    2 |    
1247    4 |    
1248    6 |    
1249    8 |    
1250 (5 rows)
1251
1252 select max(foo COLLATE "C") filter (where (bar collate "POSIX") > '0')
1253 from (values ('a', 'b')) AS v(foo,bar);
1254  max 
1255 -----
1256  a
1257 (1 row)
1258
1259 -- outer reference in FILTER (PostgreSQL extension)
1260 select (select count(*)
1261         from (values (1)) t0(inner_c))
1262 from (values (2),(3)) t1(outer_c); -- inner query is aggregation query
1263  count 
1264 -------
1265      1
1266      1
1267 (2 rows)
1268
1269 select (select count(*) filter (where outer_c <> 0)
1270         from (values (1)) t0(inner_c))
1271 from (values (2),(3)) t1(outer_c); -- outer query is aggregation query
1272  count 
1273 -------
1274      2
1275 (1 row)
1276
1277 select (select count(inner_c) filter (where outer_c <> 0)
1278         from (values (1)) t0(inner_c))
1279 from (values (2),(3)) t1(outer_c); -- inner query is aggregation query
1280  count 
1281 -------
1282      1
1283      1
1284 (2 rows)
1285
1286 select
1287   (select max((select i.unique2 from tenk1 i where i.unique1 = o.unique1))
1288      filter (where o.unique1 < 10))
1289 from tenk1 o;                                   -- outer query is aggregation query
1290  max  
1291 ------
1292  9998
1293 (1 row)
1294
1295 -- subquery in FILTER clause (PostgreSQL extension)
1296 select sum(unique1) FILTER (WHERE
1297   unique1 IN (SELECT unique1 FROM onek where unique1 < 100)) FROM tenk1;
1298  sum  
1299 ------
1300  4950
1301 (1 row)
1302
1303 -- exercise lots of aggregate parts with FILTER
1304 select aggfns(distinct a,b,c order by a,c using ~<~,b) filter (where a > 1)
1305     from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
1306     generate_series(1,2) i;
1307           aggfns           
1308 ---------------------------
1309  {"(2,2,bar)","(3,1,baz)"}
1310 (1 row)
1311
1312 -- variadic aggregates
1313 select least_agg(q1,q2) from int8_tbl;
1314      least_agg     
1315 -------------------
1316  -4567890123456789
1317 (1 row)
1318
1319 select least_agg(variadic array[q1,q2]) from int8_tbl;
1320      least_agg     
1321 -------------------
1322  -4567890123456789
1323 (1 row)
1324