Remove old RULE privilege completely.
[pgsql.git] / src / test / regress / expected / groupingsets.out
blob717383c4f3ad5d8c5648e82021a6af62d33a5a59
1 --
2 -- grouping sets
3 --
4 -- test data sources
5 create temp view gstest1(a,b,v)
6   as values (1,1,10),(1,1,11),(1,2,12),(1,2,13),(1,3,14),
7             (2,3,15),
8             (3,3,16),(3,4,17),
9             (4,1,18),(4,1,19);
10 create temp table gstest2 (a integer, b integer, c integer, d integer,
11                            e integer, f integer, g integer, h integer);
12 copy gstest2 from stdin;
13 create temp table gstest3 (a integer, b integer, c integer, d integer);
14 copy gstest3 from stdin;
15 alter table gstest3 add primary key (a);
16 create temp table gstest4(id integer, v integer,
17                           unhashable_col bit(4), unsortable_col xid);
18 insert into gstest4
19 values (1,1,b'0000','1'), (2,2,b'0001','1'),
20        (3,4,b'0010','2'), (4,8,b'0011','2'),
21        (5,16,b'0000','2'), (6,32,b'0001','2'),
22        (7,64,b'0010','1'), (8,128,b'0011','1');
23 create temp table gstest_empty (a integer, b integer, v integer);
24 create function gstest_data(v integer, out a integer, out b integer)
25   returns setof record
26   as $f$
27     begin
28       return query select v, i from generate_series(1,3) i;
29     end;
30   $f$ language plpgsql;
31 -- basic functionality
32 set enable_hashagg = false;  -- test hashing explicitly later
33 -- simple rollup with multiple plain aggregates, with and without ordering
34 -- (and with ordering differing from grouping)
35 select a, b, grouping(a,b), sum(v), count(*), max(v)
36   from gstest1 group by rollup (a,b);
37  a | b | grouping | sum | count | max 
38 ---+---+----------+-----+-------+-----
39  1 | 1 |        0 |  21 |     2 |  11
40  1 | 2 |        0 |  25 |     2 |  13
41  1 | 3 |        0 |  14 |     1 |  14
42  1 |   |        1 |  60 |     5 |  14
43  2 | 3 |        0 |  15 |     1 |  15
44  2 |   |        1 |  15 |     1 |  15
45  3 | 3 |        0 |  16 |     1 |  16
46  3 | 4 |        0 |  17 |     1 |  17
47  3 |   |        1 |  33 |     2 |  17
48  4 | 1 |        0 |  37 |     2 |  19
49  4 |   |        1 |  37 |     2 |  19
50    |   |        3 | 145 |    10 |  19
51 (12 rows)
53 select a, b, grouping(a,b), sum(v), count(*), max(v)
54   from gstest1 group by rollup (a,b) order by a,b;
55  a | b | grouping | sum | count | max 
56 ---+---+----------+-----+-------+-----
57  1 | 1 |        0 |  21 |     2 |  11
58  1 | 2 |        0 |  25 |     2 |  13
59  1 | 3 |        0 |  14 |     1 |  14
60  1 |   |        1 |  60 |     5 |  14
61  2 | 3 |        0 |  15 |     1 |  15
62  2 |   |        1 |  15 |     1 |  15
63  3 | 3 |        0 |  16 |     1 |  16
64  3 | 4 |        0 |  17 |     1 |  17
65  3 |   |        1 |  33 |     2 |  17
66  4 | 1 |        0 |  37 |     2 |  19
67  4 |   |        1 |  37 |     2 |  19
68    |   |        3 | 145 |    10 |  19
69 (12 rows)
71 select a, b, grouping(a,b), sum(v), count(*), max(v)
72   from gstest1 group by rollup (a,b) order by b desc, a;
73  a | b | grouping | sum | count | max 
74 ---+---+----------+-----+-------+-----
75  1 |   |        1 |  60 |     5 |  14
76  2 |   |        1 |  15 |     1 |  15
77  3 |   |        1 |  33 |     2 |  17
78  4 |   |        1 |  37 |     2 |  19
79    |   |        3 | 145 |    10 |  19
80  3 | 4 |        0 |  17 |     1 |  17
81  1 | 3 |        0 |  14 |     1 |  14
82  2 | 3 |        0 |  15 |     1 |  15
83  3 | 3 |        0 |  16 |     1 |  16
84  1 | 2 |        0 |  25 |     2 |  13
85  1 | 1 |        0 |  21 |     2 |  11
86  4 | 1 |        0 |  37 |     2 |  19
87 (12 rows)
89 select a, b, grouping(a,b), sum(v), count(*), max(v)
90   from gstest1 group by rollup (a,b) order by coalesce(a,0)+coalesce(b,0);
91  a | b | grouping | sum | count | max 
92 ---+---+----------+-----+-------+-----
93    |   |        3 | 145 |    10 |  19
94  1 |   |        1 |  60 |     5 |  14
95  1 | 1 |        0 |  21 |     2 |  11
96  2 |   |        1 |  15 |     1 |  15
97  3 |   |        1 |  33 |     2 |  17
98  1 | 2 |        0 |  25 |     2 |  13
99  1 | 3 |        0 |  14 |     1 |  14
100  4 |   |        1 |  37 |     2 |  19
101  4 | 1 |        0 |  37 |     2 |  19
102  2 | 3 |        0 |  15 |     1 |  15
103  3 | 3 |        0 |  16 |     1 |  16
104  3 | 4 |        0 |  17 |     1 |  17
105 (12 rows)
107 -- various types of ordered aggs
108 select a, b, grouping(a,b),
109        array_agg(v order by v),
110        string_agg(v::text, ':' order by v desc),
111        percentile_disc(0.5) within group (order by v),
112        rank(1,2,12) within group (order by a,b,v)
113   from gstest1 group by rollup (a,b) order by a,b;
114  a | b | grouping |            array_agg            |          string_agg           | percentile_disc | rank 
115 ---+---+----------+---------------------------------+-------------------------------+-----------------+------
116  1 | 1 |        0 | {10,11}                         | 11:10                         |              10 |    3
117  1 | 2 |        0 | {12,13}                         | 13:12                         |              12 |    1
118  1 | 3 |        0 | {14}                            | 14                            |              14 |    1
119  1 |   |        1 | {10,11,12,13,14}                | 14:13:12:11:10                |              12 |    3
120  2 | 3 |        0 | {15}                            | 15                            |              15 |    1
121  2 |   |        1 | {15}                            | 15                            |              15 |    1
122  3 | 3 |        0 | {16}                            | 16                            |              16 |    1
123  3 | 4 |        0 | {17}                            | 17                            |              17 |    1
124  3 |   |        1 | {16,17}                         | 17:16                         |              16 |    1
125  4 | 1 |        0 | {18,19}                         | 19:18                         |              18 |    1
126  4 |   |        1 | {18,19}                         | 19:18                         |              18 |    1
127    |   |        3 | {10,11,12,13,14,15,16,17,18,19} | 19:18:17:16:15:14:13:12:11:10 |              14 |    3
128 (12 rows)
130 -- test usage of grouped columns in direct args of aggs
131 select grouping(a), a, array_agg(b),
132        rank(a) within group (order by b nulls first),
133        rank(a) within group (order by b nulls last)
134   from (values (1,1),(1,4),(1,5),(3,1),(3,2)) v(a,b)
135  group by rollup (a) order by a;
136  grouping | a |  array_agg  | rank | rank 
137 ----------+---+-------------+------+------
138         0 | 1 | {1,4,5}     |    1 |    1
139         0 | 3 | {1,2}       |    3 |    3
140         1 |   | {1,4,5,1,2} |    1 |    6
141 (3 rows)
143 -- nesting with window functions
144 select a, b, sum(c), sum(sum(c)) over (order by a,b) as rsum
145   from gstest2 group by rollup (a,b) order by rsum, a, b;
146  a | b | sum | rsum 
147 ---+---+-----+------
148  1 | 1 |   8 |    8
149  1 | 2 |   2 |   10
150  1 |   |  10 |   20
151  2 | 2 |   2 |   22
152  2 |   |   2 |   24
153    |   |  12 |   36
154 (6 rows)
156 -- nesting with grouping sets
157 select sum(c) from gstest2
158   group by grouping sets((), grouping sets((), grouping sets(())))
159   order by 1 desc;
160  sum 
161 -----
162   12
163   12
164   12
165 (3 rows)
167 select sum(c) from gstest2
168   group by grouping sets((), grouping sets((), grouping sets(((a, b)))))
169   order by 1 desc;
170  sum 
171 -----
172   12
173   12
174    8
175    2
176    2
177 (5 rows)
179 select sum(c) from gstest2
180   group by grouping sets(grouping sets(rollup(c), grouping sets(cube(c))))
181   order by 1 desc;
182  sum 
183 -----
184   12
185   12
186    6
187    6
188    6
189    6
190 (6 rows)
192 select sum(c) from gstest2
193   group by grouping sets(a, grouping sets(a, cube(b)))
194   order by 1 desc;
195  sum 
196 -----
197   12
198   10
199   10
200    8
201    4
202    2
203    2
204 (7 rows)
206 select sum(c) from gstest2
207   group by grouping sets(grouping sets((a, (b))))
208   order by 1 desc;
209  sum 
210 -----
211    8
212    2
213    2
214 (3 rows)
216 select sum(c) from gstest2
217   group by grouping sets(grouping sets((a, b)))
218   order by 1 desc;
219  sum 
220 -----
221    8
222    2
223    2
224 (3 rows)
226 select sum(c) from gstest2
227   group by grouping sets(grouping sets(a, grouping sets(a), a))
228   order by 1 desc;
229  sum 
230 -----
231   10
232   10
233   10
234    2
235    2
236    2
237 (6 rows)
239 select sum(c) from gstest2
240   group by grouping sets(grouping sets(a, grouping sets(a, grouping sets(a), ((a)), a, grouping sets(a), (a)), a))
241   order by 1 desc;
242  sum 
243 -----
244   10
245   10
246   10
247   10
248   10
249   10
250   10
251   10
252    2
253    2
254    2
255    2
256    2
257    2
258    2
259    2
260 (16 rows)
262 select sum(c) from gstest2
263   group by grouping sets((a,(a,b)), grouping sets((a,(a,b)),a))
264   order by 1 desc;
265  sum 
266 -----
267   10
268    8
269    8
270    2
271    2
272    2
273    2
274    2
275 (8 rows)
277 -- empty input: first is 0 rows, second 1, third 3 etc.
278 select a, b, sum(v), count(*) from gstest_empty group by grouping sets ((a,b),a);
279  a | b | sum | count 
280 ---+---+-----+-------
281 (0 rows)
283 select a, b, sum(v), count(*) from gstest_empty group by grouping sets ((a,b),());
284  a | b | sum | count 
285 ---+---+-----+-------
286    |   |     |     0
287 (1 row)
289 select a, b, sum(v), count(*) from gstest_empty group by grouping sets ((a,b),(),(),());
290  a | b | sum | count 
291 ---+---+-----+-------
292    |   |     |     0
293    |   |     |     0
294    |   |     |     0
295 (3 rows)
297 select sum(v), count(*) from gstest_empty group by grouping sets ((),(),());
298  sum | count 
299 -----+-------
300      |     0
301      |     0
302      |     0
303 (3 rows)
305 -- empty input with joins tests some important code paths
306 select t1.a, t2.b, sum(t1.v), count(*) from gstest_empty t1, gstest_empty t2
307  group by grouping sets ((t1.a,t2.b),());
308  a | b | sum | count 
309 ---+---+-----+-------
310    |   |     |     0
311 (1 row)
313 -- simple joins, var resolution, GROUPING on join vars
314 select t1.a, t2.b, grouping(t1.a, t2.b), sum(t1.v), max(t2.a)
315   from gstest1 t1, gstest2 t2
316  group by grouping sets ((t1.a, t2.b), ());
317  a | b | grouping | sum  | max 
318 ---+---+----------+------+-----
319  1 | 1 |        0 |  420 |   1
320  1 | 2 |        0 |  120 |   2
321  2 | 1 |        0 |  105 |   1
322  2 | 2 |        0 |   30 |   2
323  3 | 1 |        0 |  231 |   1
324  3 | 2 |        0 |   66 |   2
325  4 | 1 |        0 |  259 |   1
326  4 | 2 |        0 |   74 |   2
327    |   |        3 | 1305 |   2
328 (9 rows)
330 select t1.a, t2.b, grouping(t1.a, t2.b), sum(t1.v), max(t2.a)
331   from gstest1 t1 join gstest2 t2 on (t1.a=t2.a)
332  group by grouping sets ((t1.a, t2.b), ());
333  a | b | grouping | sum | max 
334 ---+---+----------+-----+-----
335  1 | 1 |        0 | 420 |   1
336  1 | 2 |        0 |  60 |   1
337  2 | 2 |        0 |  15 |   2
338    |   |        3 | 495 |   2
339 (4 rows)
341 select a, b, grouping(a, b), sum(t1.v), max(t2.c)
342   from gstest1 t1 join gstest2 t2 using (a,b)
343  group by grouping sets ((a, b), ());
344  a | b | grouping | sum | max 
345 ---+---+----------+-----+-----
346  1 | 1 |        0 | 147 |   2
347  1 | 2 |        0 |  25 |   2
348    |   |        3 | 172 |   2
349 (3 rows)
351 -- check that functionally dependent cols are not nulled
352 select a, d, grouping(a,b,c)
353   from gstest3
354  group by grouping sets ((a,b), (a,c));
355  a | d | grouping 
356 ---+---+----------
357  1 | 1 |        1
358  2 | 2 |        1
359  1 | 1 |        2
360  2 | 2 |        2
361 (4 rows)
363 -- check that distinct grouping columns are kept separate
364 -- even if they are equal()
365 explain (costs off)
366 select g as alias1, g as alias2
367   from generate_series(1,3) g
368  group by alias1, rollup(alias2);
369                    QUERY PLAN                   
370 ------------------------------------------------
371  GroupAggregate
372    Group Key: g, g
373    Group Key: g
374    ->  Sort
375          Sort Key: g
376          ->  Function Scan on generate_series g
377 (6 rows)
379 select g as alias1, g as alias2
380   from generate_series(1,3) g
381  group by alias1, rollup(alias2);
382  alias1 | alias2 
383 --------+--------
384       1 |      1
385       1 |       
386       2 |      2
387       2 |       
388       3 |      3
389       3 |       
390 (6 rows)
392 -- check that pulled-up subquery outputs still go to null when appropriate
393 select four, x
394   from (select four, ten, 'foo'::text as x from tenk1) as t
395   group by grouping sets (four, x)
396   having x = 'foo';
397  four |  x  
398 ------+-----
399       | foo
400 (1 row)
402 select four, x || 'x'
403   from (select four, ten, 'foo'::text as x from tenk1) as t
404   group by grouping sets (four, x)
405   order by four;
406  four | ?column? 
407 ------+----------
408     0 | 
409     1 | 
410     2 | 
411     3 | 
412       | foox
413 (5 rows)
415 select (x+y)*1, sum(z)
416  from (select 1 as x, 2 as y, 3 as z) s
417  group by grouping sets (x+y, x);
418  ?column? | sum 
419 ----------+-----
420         3 |   3
421           |   3
422 (2 rows)
424 select x, not x as not_x, q2 from
425   (select *, q1 = 1 as x from int8_tbl i1) as t
426   group by grouping sets(x, q2)
427   order by x, q2;
428  x | not_x |        q2         
429 ---+-------+-------------------
430  f | t     |                  
431    |       | -4567890123456789
432    |       |               123
433    |       |               456
434    |       |  4567890123456789
435 (5 rows)
437 -- check qual push-down rules for a subquery with grouping sets
438 explain (verbose, costs off)
439 select * from (
440   select 1 as x, q1, sum(q2)
441   from int8_tbl i1
442   group by grouping sets(1, 2)
443 ) ss
444 where x = 1 and q1 = 123;
445                     QUERY PLAN                    
446 --------------------------------------------------
447  Subquery Scan on ss
448    Output: ss.x, ss.q1, ss.sum
449    Filter: ((ss.x = 1) AND (ss.q1 = 123))
450    ->  GroupAggregate
451          Output: (1), i1.q1, sum(i1.q2)
452          Group Key: (1)
453          Sort Key: i1.q1
454            Group Key: i1.q1
455          ->  Sort
456                Output: (1), i1.q1, i1.q2
457                Sort Key: (1)
458                ->  Seq Scan on public.int8_tbl i1
459                      Output: 1, i1.q1, i1.q2
460 (13 rows)
462 select * from (
463   select 1 as x, q1, sum(q2)
464   from int8_tbl i1
465   group by grouping sets(1, 2)
466 ) ss
467 where x = 1 and q1 = 123;
468  x | q1 | sum 
469 ---+----+-----
470 (0 rows)
472 -- check handling of pulled-up SubPlan in GROUPING() argument (bug #17479)
473 explain (verbose, costs off)
474 select grouping(ss.x)
475 from int8_tbl i1
476 cross join lateral (select (select i1.q1) as x) ss
477 group by ss.x;
478                    QUERY PLAN                   
479 ------------------------------------------------
480  GroupAggregate
481    Output: GROUPING((SubPlan 1)), ((SubPlan 2))
482    Group Key: ((SubPlan 2))
483    ->  Sort
484          Output: ((SubPlan 2)), i1.q1
485          Sort Key: ((SubPlan 2))
486          ->  Seq Scan on public.int8_tbl i1
487                Output: (SubPlan 2), i1.q1
488                SubPlan 2
489                  ->  Result
490                        Output: i1.q1
491 (11 rows)
493 select grouping(ss.x)
494 from int8_tbl i1
495 cross join lateral (select (select i1.q1) as x) ss
496 group by ss.x;
497  grouping 
498 ----------
499         0
500         0
501 (2 rows)
503 explain (verbose, costs off)
504 select (select grouping(ss.x))
505 from int8_tbl i1
506 cross join lateral (select (select i1.q1) as x) ss
507 group by ss.x;
508                  QUERY PLAN                 
509 --------------------------------------------
510  GroupAggregate
511    Output: (SubPlan 2), ((SubPlan 3))
512    Group Key: ((SubPlan 3))
513    ->  Sort
514          Output: ((SubPlan 3)), i1.q1
515          Sort Key: ((SubPlan 3))
516          ->  Seq Scan on public.int8_tbl i1
517                Output: (SubPlan 3), i1.q1
518                SubPlan 3
519                  ->  Result
520                        Output: i1.q1
521    SubPlan 2
522      ->  Result
523            Output: GROUPING((SubPlan 1))
524 (14 rows)
526 select (select grouping(ss.x))
527 from int8_tbl i1
528 cross join lateral (select (select i1.q1) as x) ss
529 group by ss.x;
530  grouping 
531 ----------
532         0
533         0
534 (2 rows)
536 -- simple rescan tests
537 select a, b, sum(v.x)
538   from (values (1),(2)) v(x), gstest_data(v.x)
539  group by rollup (a,b);
540  a | b | sum 
541 ---+---+-----
542  1 | 1 |   1
543  1 | 2 |   1
544  1 | 3 |   1
545  1 |   |   3
546  2 | 1 |   2
547  2 | 2 |   2
548  2 | 3 |   2
549  2 |   |   6
550    |   |   9
551 (9 rows)
553 select *
554   from (values (1),(2)) v(x),
555        lateral (select a, b, sum(v.x) from gstest_data(v.x) group by rollup (a,b)) s;
556 ERROR:  aggregate functions are not allowed in FROM clause of their own query level
557 LINE 3:        lateral (select a, b, sum(v.x) from gstest_data(v.x) ...
558                                      ^
559 -- min max optimization should still work with GROUP BY ()
560 explain (costs off)
561   select min(unique1) from tenk1 GROUP BY ();
562                          QUERY PLAN                         
563 ------------------------------------------------------------
564  Result
565    InitPlan 1
566      ->  Limit
567            ->  Index Only Scan using tenk1_unique1 on tenk1
568                  Index Cond: (unique1 IS NOT NULL)
569 (5 rows)
571 -- Views with GROUPING SET queries
572 CREATE VIEW gstest_view AS select a, b, grouping(a,b), sum(c), count(*), max(c)
573   from gstest2 group by rollup ((a,b,c),(c,d));
574 NOTICE:  view "gstest_view" will be a temporary view
575 select pg_get_viewdef('gstest_view'::regclass, true);
576             pg_get_viewdef             
577 ---------------------------------------
578   SELECT a,                           +
579      b,                               +
580      GROUPING(a, b) AS "grouping",    +
581      sum(c) AS sum,                   +
582      count(*) AS count,               +
583      max(c) AS max                    +
584     FROM gstest2                      +
585    GROUP BY ROLLUP((a, b, c), (c, d));
586 (1 row)
588 -- Nested queries with 3 or more levels of nesting
589 select(select (select grouping(a,b) from (values (1)) v2(c)) from (values (1,2)) v1(a,b) group by (a,b)) from (values(6,7)) v3(e,f) GROUP BY ROLLUP(e,f);
590  grouping 
591 ----------
592         0
593         0
594         0
595 (3 rows)
597 select(select (select grouping(e,f) from (values (1)) v2(c)) from (values (1,2)) v1(a,b) group by (a,b)) from (values(6,7)) v3(e,f) GROUP BY ROLLUP(e,f);
598  grouping 
599 ----------
600         0
601         1
602         3
603 (3 rows)
605 select(select (select grouping(c) from (values (1)) v2(c) GROUP BY c) from (values (1,2)) v1(a,b) group by (a,b)) from (values(6,7)) v3(e,f) GROUP BY ROLLUP(e,f);
606  grouping 
607 ----------
608         0
609         0
610         0
611 (3 rows)
613 -- Combinations of operations
614 select a, b, c, d from gstest2 group by rollup(a,b),grouping sets(c,d);
615  a | b | c | d 
616 ---+---+---+---
617  1 | 1 | 1 |  
618  1 |   | 1 |  
619    |   | 1 |  
620  1 | 1 | 2 |  
621  1 | 2 | 2 |  
622  1 |   | 2 |  
623  2 | 2 | 2 |  
624  2 |   | 2 |  
625    |   | 2 |  
626  1 | 1 |   | 1
627  1 |   |   | 1
628    |   |   | 1
629  1 | 1 |   | 2
630  1 | 2 |   | 2
631  1 |   |   | 2
632  2 | 2 |   | 2
633  2 |   |   | 2
634    |   |   | 2
635 (18 rows)
637 select a, b from (values (1,2),(2,3)) v(a,b) group by a,b, grouping sets(a);
638  a | b 
639 ---+---
640  1 | 2
641  2 | 3
642 (2 rows)
644 -- Tests for chained aggregates
645 select a, b, grouping(a,b), sum(v), count(*), max(v)
646   from gstest1 group by grouping sets ((a,b),(a+1,b+1),(a+2,b+2)) order by 3,6;
647  a | b | grouping | sum | count | max 
648 ---+---+----------+-----+-------+-----
649  1 | 1 |        0 |  21 |     2 |  11
650  1 | 2 |        0 |  25 |     2 |  13
651  1 | 3 |        0 |  14 |     1 |  14
652  2 | 3 |        0 |  15 |     1 |  15
653  3 | 3 |        0 |  16 |     1 |  16
654  3 | 4 |        0 |  17 |     1 |  17
655  4 | 1 |        0 |  37 |     2 |  19
656    |   |        3 |  21 |     2 |  11
657    |   |        3 |  21 |     2 |  11
658    |   |        3 |  25 |     2 |  13
659    |   |        3 |  25 |     2 |  13
660    |   |        3 |  14 |     1 |  14
661    |   |        3 |  14 |     1 |  14
662    |   |        3 |  15 |     1 |  15
663    |   |        3 |  15 |     1 |  15
664    |   |        3 |  16 |     1 |  16
665    |   |        3 |  16 |     1 |  16
666    |   |        3 |  17 |     1 |  17
667    |   |        3 |  17 |     1 |  17
668    |   |        3 |  37 |     2 |  19
669    |   |        3 |  37 |     2 |  19
670 (21 rows)
672 select(select (select grouping(a,b) from (values (1)) v2(c)) from (values (1,2)) v1(a,b) group by (a,b)) from (values(6,7)) v3(e,f) GROUP BY ROLLUP((e+1),(f+1));
673  grouping 
674 ----------
675         0
676         0
677         0
678 (3 rows)
680 select(select (select grouping(a,b) from (values (1)) v2(c)) from (values (1,2)) v1(a,b) group by (a,b)) from (values(6,7)) v3(e,f) GROUP BY CUBE((e+1),(f+1)) ORDER BY (e+1),(f+1);
681  grouping 
682 ----------
683         0
684         0
685         0
686         0
687 (4 rows)
689 select a, b, sum(c), sum(sum(c)) over (order by a,b) as rsum
690   from gstest2 group by cube (a,b) order by rsum, a, b;
691  a | b | sum | rsum 
692 ---+---+-----+------
693  1 | 1 |   8 |    8
694  1 | 2 |   2 |   10
695  1 |   |  10 |   20
696  2 | 2 |   2 |   22
697  2 |   |   2 |   24
698    | 1 |   8 |   32
699    | 2 |   4 |   36
700    |   |  12 |   48
701 (8 rows)
703 select a, b, sum(c) from (values (1,1,10),(1,1,11),(1,2,12),(1,2,13),(1,3,14),(2,3,15),(3,3,16),(3,4,17),(4,1,18),(4,1,19)) v(a,b,c) group by rollup (a,b);
704  a | b | sum 
705 ---+---+-----
706  1 | 1 |  21
707  1 | 2 |  25
708  1 | 3 |  14
709  1 |   |  60
710  2 | 3 |  15
711  2 |   |  15
712  3 | 3 |  16
713  3 | 4 |  17
714  3 |   |  33
715  4 | 1 |  37
716  4 |   |  37
717    |   | 145
718 (12 rows)
720 select a, b, sum(v.x)
721   from (values (1),(2)) v(x), gstest_data(v.x)
722  group by cube (a,b) order by a,b;
723  a | b | sum 
724 ---+---+-----
725  1 | 1 |   1
726  1 | 2 |   1
727  1 | 3 |   1
728  1 |   |   3
729  2 | 1 |   2
730  2 | 2 |   2
731  2 | 3 |   2
732  2 |   |   6
733    | 1 |   3
734    | 2 |   3
735    | 3 |   3
736    |   |   9
737 (12 rows)
739 -- Test reordering of grouping sets
740 explain (costs off)
741 select * from gstest1 group by grouping sets((a,b,v),(v)) order by v,b,a;
742                                      QUERY PLAN                                     
743 ------------------------------------------------------------------------------------
744  Incremental Sort
745    Sort Key: "*VALUES*".column3, "*VALUES*".column2, "*VALUES*".column1
746    Presorted Key: "*VALUES*".column3
747    ->  GroupAggregate
748          Group Key: "*VALUES*".column3, "*VALUES*".column2, "*VALUES*".column1
749          Group Key: "*VALUES*".column3
750          ->  Sort
751                Sort Key: "*VALUES*".column3, "*VALUES*".column2, "*VALUES*".column1
752                ->  Values Scan on "*VALUES*"
753 (9 rows)
755 -- Agg level check. This query should error out.
756 select (select grouping(a,b) from gstest2) from gstest2 group by a,b;
757 ERROR:  arguments to GROUPING must be grouping expressions of the associated query level
758 LINE 1: select (select grouping(a,b) from gstest2) from gstest2 grou...
759                                 ^
760 --Nested queries
761 select a, b, sum(c), count(*) from gstest2 group by grouping sets (rollup(a,b),a);
762  a | b | sum | count 
763 ---+---+-----+-------
764  1 | 1 |   8 |     7
765  1 | 2 |   2 |     1
766  1 |   |  10 |     8
767  1 |   |  10 |     8
768  2 | 2 |   2 |     1
769  2 |   |   2 |     1
770  2 |   |   2 |     1
771    |   |  12 |     9
772 (8 rows)
774 -- HAVING queries
775 select ten, sum(distinct four) from onek a
776 group by grouping sets((ten,four),(ten))
777 having exists (select 1 from onek b where sum(distinct a.four) = b.four);
778  ten | sum 
779 -----+-----
780    0 |   0
781    0 |   2
782    0 |   2
783    1 |   1
784    1 |   3
785    2 |   0
786    2 |   2
787    2 |   2
788    3 |   1
789    3 |   3
790    4 |   0
791    4 |   2
792    4 |   2
793    5 |   1
794    5 |   3
795    6 |   0
796    6 |   2
797    6 |   2
798    7 |   1
799    7 |   3
800    8 |   0
801    8 |   2
802    8 |   2
803    9 |   1
804    9 |   3
805 (25 rows)
807 -- Tests around pushdown of HAVING clauses, partially testing against previous bugs
808 select a,count(*) from gstest2 group by rollup(a) order by a;
809  a | count 
810 ---+-------
811  1 |     8
812  2 |     1
813    |     9
814 (3 rows)
816 select a,count(*) from gstest2 group by rollup(a) having a is distinct from 1 order by a;
817  a | count 
818 ---+-------
819  2 |     1
820    |     9
821 (2 rows)
823 explain (costs off)
824   select a,count(*) from gstest2 group by rollup(a) having a is distinct from 1 order by a;
825                QUERY PLAN               
826 ----------------------------------------
827  Sort
828    Sort Key: a
829    ->  GroupAggregate
830          Group Key: a
831          Group Key: ()
832          Filter: (a IS DISTINCT FROM 1)
833          ->  Sort
834                Sort Key: a
835                ->  Seq Scan on gstest2
836 (9 rows)
838 select v.c, (select count(*) from gstest2 group by () having v.c)
839   from (values (false),(true)) v(c) order by v.c;
840  c | count 
841 ---+-------
842  f |      
843  t |     9
844 (2 rows)
846 explain (costs off)
847   select v.c, (select count(*) from gstest2 group by () having v.c)
848     from (values (false),(true)) v(c) order by v.c;
849                         QUERY PLAN                         
850 -----------------------------------------------------------
851  Sort
852    Sort Key: "*VALUES*".column1
853    ->  Values Scan on "*VALUES*"
854          SubPlan 1
855            ->  Aggregate
856                  Group Key: ()
857                  Filter: "*VALUES*".column1
858                  ->  Result
859                        One-Time Filter: "*VALUES*".column1
860                        ->  Seq Scan on gstest2
861 (10 rows)
863 -- HAVING with GROUPING queries
864 select ten, grouping(ten) from onek
865 group by grouping sets(ten) having grouping(ten) >= 0
866 order by 2,1;
867  ten | grouping 
868 -----+----------
869    0 |        0
870    1 |        0
871    2 |        0
872    3 |        0
873    4 |        0
874    5 |        0
875    6 |        0
876    7 |        0
877    8 |        0
878    9 |        0
879 (10 rows)
881 select ten, grouping(ten) from onek
882 group by grouping sets(ten, four) having grouping(ten) > 0
883 order by 2,1;
884  ten | grouping 
885 -----+----------
886      |        1
887      |        1
888      |        1
889      |        1
890 (4 rows)
892 select ten, grouping(ten) from onek
893 group by rollup(ten) having grouping(ten) > 0
894 order by 2,1;
895  ten | grouping 
896 -----+----------
897      |        1
898 (1 row)
900 select ten, grouping(ten) from onek
901 group by cube(ten) having grouping(ten) > 0
902 order by 2,1;
903  ten | grouping 
904 -----+----------
905      |        1
906 (1 row)
908 select ten, grouping(ten) from onek
909 group by (ten) having grouping(ten) >= 0
910 order by 2,1;
911  ten | grouping 
912 -----+----------
913    0 |        0
914    1 |        0
915    2 |        0
916    3 |        0
917    4 |        0
918    5 |        0
919    6 |        0
920    7 |        0
921    8 |        0
922    9 |        0
923 (10 rows)
925 -- FILTER queries
926 select ten, sum(distinct four) filter (where four::text ~ '123') from onek a
927 group by rollup(ten);
928  ten | sum 
929 -----+-----
930    0 |    
931    1 |    
932    2 |    
933    3 |    
934    4 |    
935    5 |    
936    6 |    
937    7 |    
938    8 |    
939    9 |    
940      |    
941 (11 rows)
943 -- More rescan tests
944 select * from (values (1),(2)) v(a) left join lateral (select v.a, four, ten, count(*) from onek group by cube(four,ten)) s on true order by v.a,four,ten;
945  a | a | four | ten | count 
946 ---+---+------+-----+-------
947  1 | 1 |    0 |   0 |    50
948  1 | 1 |    0 |   2 |    50
949  1 | 1 |    0 |   4 |    50
950  1 | 1 |    0 |   6 |    50
951  1 | 1 |    0 |   8 |    50
952  1 | 1 |    0 |     |   250
953  1 | 1 |    1 |   1 |    50
954  1 | 1 |    1 |   3 |    50
955  1 | 1 |    1 |   5 |    50
956  1 | 1 |    1 |   7 |    50
957  1 | 1 |    1 |   9 |    50
958  1 | 1 |    1 |     |   250
959  1 | 1 |    2 |   0 |    50
960  1 | 1 |    2 |   2 |    50
961  1 | 1 |    2 |   4 |    50
962  1 | 1 |    2 |   6 |    50
963  1 | 1 |    2 |   8 |    50
964  1 | 1 |    2 |     |   250
965  1 | 1 |    3 |   1 |    50
966  1 | 1 |    3 |   3 |    50
967  1 | 1 |    3 |   5 |    50
968  1 | 1 |    3 |   7 |    50
969  1 | 1 |    3 |   9 |    50
970  1 | 1 |    3 |     |   250
971  1 | 1 |      |   0 |   100
972  1 | 1 |      |   1 |   100
973  1 | 1 |      |   2 |   100
974  1 | 1 |      |   3 |   100
975  1 | 1 |      |   4 |   100
976  1 | 1 |      |   5 |   100
977  1 | 1 |      |   6 |   100
978  1 | 1 |      |   7 |   100
979  1 | 1 |      |   8 |   100
980  1 | 1 |      |   9 |   100
981  1 | 1 |      |     |  1000
982  2 | 2 |    0 |   0 |    50
983  2 | 2 |    0 |   2 |    50
984  2 | 2 |    0 |   4 |    50
985  2 | 2 |    0 |   6 |    50
986  2 | 2 |    0 |   8 |    50
987  2 | 2 |    0 |     |   250
988  2 | 2 |    1 |   1 |    50
989  2 | 2 |    1 |   3 |    50
990  2 | 2 |    1 |   5 |    50
991  2 | 2 |    1 |   7 |    50
992  2 | 2 |    1 |   9 |    50
993  2 | 2 |    1 |     |   250
994  2 | 2 |    2 |   0 |    50
995  2 | 2 |    2 |   2 |    50
996  2 | 2 |    2 |   4 |    50
997  2 | 2 |    2 |   6 |    50
998  2 | 2 |    2 |   8 |    50
999  2 | 2 |    2 |     |   250
1000  2 | 2 |    3 |   1 |    50
1001  2 | 2 |    3 |   3 |    50
1002  2 | 2 |    3 |   5 |    50
1003  2 | 2 |    3 |   7 |    50
1004  2 | 2 |    3 |   9 |    50
1005  2 | 2 |    3 |     |   250
1006  2 | 2 |      |   0 |   100
1007  2 | 2 |      |   1 |   100
1008  2 | 2 |      |   2 |   100
1009  2 | 2 |      |   3 |   100
1010  2 | 2 |      |   4 |   100
1011  2 | 2 |      |   5 |   100
1012  2 | 2 |      |   6 |   100
1013  2 | 2 |      |   7 |   100
1014  2 | 2 |      |   8 |   100
1015  2 | 2 |      |   9 |   100
1016  2 | 2 |      |     |  1000
1017 (70 rows)
1019 select array(select row(v.a,s1.*) from (select two,four, count(*) from onek group by cube(two,four) order by two,four) s1) from (values (1),(2)) v(a);
1020                                                                         array                                                                         
1021 ------------------------------------------------------------------------------------------------------------------------------------------------------
1022  {"(1,0,0,250)","(1,0,2,250)","(1,0,,500)","(1,1,1,250)","(1,1,3,250)","(1,1,,500)","(1,,0,250)","(1,,1,250)","(1,,2,250)","(1,,3,250)","(1,,,1000)"}
1023  {"(2,0,0,250)","(2,0,2,250)","(2,0,,500)","(2,1,1,250)","(2,1,3,250)","(2,1,,500)","(2,,0,250)","(2,,1,250)","(2,,2,250)","(2,,3,250)","(2,,,1000)"}
1024 (2 rows)
1026 -- Grouping on text columns
1027 select sum(ten) from onek group by two, rollup(four::text) order by 1;
1028  sum  
1029 ------
1030  1000
1031  1000
1032  1250
1033  1250
1034  2000
1035  2500
1036 (6 rows)
1038 select sum(ten) from onek group by rollup(four::text), two order by 1;
1039  sum  
1040 ------
1041  1000
1042  1000
1043  1250
1044  1250
1045  2000
1046  2500
1047 (6 rows)
1049 -- hashing support
1050 set enable_hashagg = true;
1051 -- failure cases
1052 select count(*) from gstest4 group by rollup(unhashable_col,unsortable_col);
1053 ERROR:  could not implement GROUP BY
1054 DETAIL:  Some of the datatypes only support hashing, while others only support sorting.
1055 select array_agg(v order by v) from gstest4 group by grouping sets ((id,unsortable_col),(id));
1056 ERROR:  could not implement GROUP BY
1057 DETAIL:  Some of the datatypes only support hashing, while others only support sorting.
1058 -- simple cases
1059 select a, b, grouping(a,b), sum(v), count(*), max(v)
1060   from gstest1 group by grouping sets ((a),(b)) order by 3,1,2;
1061  a | b | grouping | sum | count | max 
1062 ---+---+----------+-----+-------+-----
1063  1 |   |        1 |  60 |     5 |  14
1064  2 |   |        1 |  15 |     1 |  15
1065  3 |   |        1 |  33 |     2 |  17
1066  4 |   |        1 |  37 |     2 |  19
1067    | 1 |        2 |  58 |     4 |  19
1068    | 2 |        2 |  25 |     2 |  13
1069    | 3 |        2 |  45 |     3 |  16
1070    | 4 |        2 |  17 |     1 |  17
1071 (8 rows)
1073 explain (costs off) select a, b, grouping(a,b), sum(v), count(*), max(v)
1074   from gstest1 group by grouping sets ((a),(b)) order by 3,1,2;
1075                                                QUERY PLAN                                               
1076 --------------------------------------------------------------------------------------------------------
1077  Sort
1078    Sort Key: (GROUPING("*VALUES*".column1, "*VALUES*".column2)), "*VALUES*".column1, "*VALUES*".column2
1079    ->  HashAggregate
1080          Hash Key: "*VALUES*".column1
1081          Hash Key: "*VALUES*".column2
1082          ->  Values Scan on "*VALUES*"
1083 (6 rows)
1085 select a, b, grouping(a,b), sum(v), count(*), max(v)
1086   from gstest1 group by cube(a,b) order by 3,1,2;
1087  a | b | grouping | sum | count | max 
1088 ---+---+----------+-----+-------+-----
1089  1 | 1 |        0 |  21 |     2 |  11
1090  1 | 2 |        0 |  25 |     2 |  13
1091  1 | 3 |        0 |  14 |     1 |  14
1092  2 | 3 |        0 |  15 |     1 |  15
1093  3 | 3 |        0 |  16 |     1 |  16
1094  3 | 4 |        0 |  17 |     1 |  17
1095  4 | 1 |        0 |  37 |     2 |  19
1096  1 |   |        1 |  60 |     5 |  14
1097  2 |   |        1 |  15 |     1 |  15
1098  3 |   |        1 |  33 |     2 |  17
1099  4 |   |        1 |  37 |     2 |  19
1100    | 1 |        2 |  58 |     4 |  19
1101    | 2 |        2 |  25 |     2 |  13
1102    | 3 |        2 |  45 |     3 |  16
1103    | 4 |        2 |  17 |     1 |  17
1104    |   |        3 | 145 |    10 |  19
1105 (16 rows)
1107 explain (costs off) select a, b, grouping(a,b), sum(v), count(*), max(v)
1108   from gstest1 group by cube(a,b) order by 3,1,2;
1109                                                QUERY PLAN                                               
1110 --------------------------------------------------------------------------------------------------------
1111  Sort
1112    Sort Key: (GROUPING("*VALUES*".column1, "*VALUES*".column2)), "*VALUES*".column1, "*VALUES*".column2
1113    ->  MixedAggregate
1114          Hash Key: "*VALUES*".column1, "*VALUES*".column2
1115          Hash Key: "*VALUES*".column1
1116          Hash Key: "*VALUES*".column2
1117          Group Key: ()
1118          ->  Values Scan on "*VALUES*"
1119 (8 rows)
1121 -- shouldn't try and hash
1122 explain (costs off)
1123   select a, b, grouping(a,b), array_agg(v order by v)
1124     from gstest1 group by cube(a,b);
1125                         QUERY PLAN                        
1126 ----------------------------------------------------------
1127  GroupAggregate
1128    Group Key: "*VALUES*".column1, "*VALUES*".column2
1129    Group Key: "*VALUES*".column1
1130    Group Key: ()
1131    Sort Key: "*VALUES*".column2
1132      Group Key: "*VALUES*".column2
1133    ->  Sort
1134          Sort Key: "*VALUES*".column1, "*VALUES*".column2
1135          ->  Values Scan on "*VALUES*"
1136 (9 rows)
1138 -- unsortable cases
1139 select unsortable_col, count(*)
1140   from gstest4 group by grouping sets ((unsortable_col),(unsortable_col))
1141   order by unsortable_col::text;
1142  unsortable_col | count 
1143 ----------------+-------
1144               1 |     4
1145               1 |     4
1146               2 |     4
1147               2 |     4
1148 (4 rows)
1150 -- mixed hashable/sortable cases
1151 select unhashable_col, unsortable_col,
1152        grouping(unhashable_col, unsortable_col),
1153        count(*), sum(v)
1154   from gstest4 group by grouping sets ((unhashable_col),(unsortable_col))
1155  order by 3, 5;
1156  unhashable_col | unsortable_col | grouping | count | sum 
1157 ----------------+----------------+----------+-------+-----
1158  0000           |                |        1 |     2 |  17
1159  0001           |                |        1 |     2 |  34
1160  0010           |                |        1 |     2 |  68
1161  0011           |                |        1 |     2 | 136
1162                 |              2 |        2 |     4 |  60
1163                 |              1 |        2 |     4 | 195
1164 (6 rows)
1166 explain (costs off)
1167   select unhashable_col, unsortable_col,
1168          grouping(unhashable_col, unsortable_col),
1169          count(*), sum(v)
1170     from gstest4 group by grouping sets ((unhashable_col),(unsortable_col))
1171    order by 3,5;
1172                             QUERY PLAN                            
1173 ------------------------------------------------------------------
1174  Sort
1175    Sort Key: (GROUPING(unhashable_col, unsortable_col)), (sum(v))
1176    ->  MixedAggregate
1177          Hash Key: unsortable_col
1178          Group Key: unhashable_col
1179          ->  Sort
1180                Sort Key: unhashable_col
1181                ->  Seq Scan on gstest4
1182 (8 rows)
1184 select unhashable_col, unsortable_col,
1185        grouping(unhashable_col, unsortable_col),
1186        count(*), sum(v)
1187   from gstest4 group by grouping sets ((v,unhashable_col),(v,unsortable_col))
1188  order by 3,5;
1189  unhashable_col | unsortable_col | grouping | count | sum 
1190 ----------------+----------------+----------+-------+-----
1191  0000           |                |        1 |     1 |   1
1192  0001           |                |        1 |     1 |   2
1193  0010           |                |        1 |     1 |   4
1194  0011           |                |        1 |     1 |   8
1195  0000           |                |        1 |     1 |  16
1196  0001           |                |        1 |     1 |  32
1197  0010           |                |        1 |     1 |  64
1198  0011           |                |        1 |     1 | 128
1199                 |              1 |        2 |     1 |   1
1200                 |              1 |        2 |     1 |   2
1201                 |              2 |        2 |     1 |   4
1202                 |              2 |        2 |     1 |   8
1203                 |              2 |        2 |     1 |  16
1204                 |              2 |        2 |     1 |  32
1205                 |              1 |        2 |     1 |  64
1206                 |              1 |        2 |     1 | 128
1207 (16 rows)
1209 explain (costs off)
1210   select unhashable_col, unsortable_col,
1211          grouping(unhashable_col, unsortable_col),
1212          count(*), sum(v)
1213     from gstest4 group by grouping sets ((v,unhashable_col),(v,unsortable_col))
1214    order by 3,5;
1215                             QUERY PLAN                            
1216 ------------------------------------------------------------------
1217  Sort
1218    Sort Key: (GROUPING(unhashable_col, unsortable_col)), (sum(v))
1219    ->  MixedAggregate
1220          Hash Key: v, unsortable_col
1221          Group Key: v, unhashable_col
1222          ->  Sort
1223                Sort Key: v, unhashable_col
1224                ->  Seq Scan on gstest4
1225 (8 rows)
1227 -- empty input: first is 0 rows, second 1, third 3 etc.
1228 select a, b, sum(v), count(*) from gstest_empty group by grouping sets ((a,b),a);
1229  a | b | sum | count 
1230 ---+---+-----+-------
1231 (0 rows)
1233 explain (costs off)
1234   select a, b, sum(v), count(*) from gstest_empty group by grouping sets ((a,b),a);
1235            QUERY PLAN           
1236 --------------------------------
1237  HashAggregate
1238    Hash Key: a, b
1239    Hash Key: a
1240    ->  Seq Scan on gstest_empty
1241 (4 rows)
1243 select a, b, sum(v), count(*) from gstest_empty group by grouping sets ((a,b),());
1244  a | b | sum | count 
1245 ---+---+-----+-------
1246    |   |     |     0
1247 (1 row)
1249 select a, b, sum(v), count(*) from gstest_empty group by grouping sets ((a,b),(),(),());
1250  a | b | sum | count 
1251 ---+---+-----+-------
1252    |   |     |     0
1253    |   |     |     0
1254    |   |     |     0
1255 (3 rows)
1257 explain (costs off)
1258   select a, b, sum(v), count(*) from gstest_empty group by grouping sets ((a,b),(),(),());
1259            QUERY PLAN           
1260 --------------------------------
1261  MixedAggregate
1262    Hash Key: a, b
1263    Group Key: ()
1264    Group Key: ()
1265    Group Key: ()
1266    ->  Seq Scan on gstest_empty
1267 (6 rows)
1269 select sum(v), count(*) from gstest_empty group by grouping sets ((),(),());
1270  sum | count 
1271 -----+-------
1272      |     0
1273      |     0
1274      |     0
1275 (3 rows)
1277 explain (costs off)
1278   select sum(v), count(*) from gstest_empty group by grouping sets ((),(),());
1279            QUERY PLAN           
1280 --------------------------------
1281  Aggregate
1282    Group Key: ()
1283    Group Key: ()
1284    Group Key: ()
1285    ->  Seq Scan on gstest_empty
1286 (5 rows)
1288 -- check that functionally dependent cols are not nulled
1289 select a, d, grouping(a,b,c)
1290   from gstest3
1291  group by grouping sets ((a,b), (a,c));
1292  a | d | grouping 
1293 ---+---+----------
1294  1 | 1 |        1
1295  2 | 2 |        1
1296  1 | 1 |        2
1297  2 | 2 |        2
1298 (4 rows)
1300 explain (costs off)
1301   select a, d, grouping(a,b,c)
1302     from gstest3
1303    group by grouping sets ((a,b), (a,c));
1304         QUERY PLAN         
1305 ---------------------------
1306  HashAggregate
1307    Hash Key: a, b
1308    Hash Key: a, c
1309    ->  Seq Scan on gstest3
1310 (4 rows)
1312 -- simple rescan tests
1313 select a, b, sum(v.x)
1314   from (values (1),(2)) v(x), gstest_data(v.x)
1315  group by grouping sets (a,b)
1316  order by 1, 2, 3;
1317  a | b | sum 
1318 ---+---+-----
1319  1 |   |   3
1320  2 |   |   6
1321    | 1 |   3
1322    | 2 |   3
1323    | 3 |   3
1324 (5 rows)
1326 explain (costs off)
1327   select a, b, sum(v.x)
1328     from (values (1),(2)) v(x), gstest_data(v.x)
1329    group by grouping sets (a,b)
1330    order by 3, 1, 2;
1331                              QUERY PLAN                              
1332 ---------------------------------------------------------------------
1333  Sort
1334    Sort Key: (sum("*VALUES*".column1)), gstest_data.a, gstest_data.b
1335    ->  HashAggregate
1336          Hash Key: gstest_data.a
1337          Hash Key: gstest_data.b
1338          ->  Nested Loop
1339                ->  Values Scan on "*VALUES*"
1340                ->  Function Scan on gstest_data
1341 (8 rows)
1343 select *
1344   from (values (1),(2)) v(x),
1345        lateral (select a, b, sum(v.x) from gstest_data(v.x) group by grouping sets (a,b)) s;
1346 ERROR:  aggregate functions are not allowed in FROM clause of their own query level
1347 LINE 3:        lateral (select a, b, sum(v.x) from gstest_data(v.x) ...
1348                                      ^
1349 explain (costs off)
1350   select *
1351     from (values (1),(2)) v(x),
1352          lateral (select a, b, sum(v.x) from gstest_data(v.x) group by grouping sets (a,b)) s;
1353 ERROR:  aggregate functions are not allowed in FROM clause of their own query level
1354 LINE 4:          lateral (select a, b, sum(v.x) from gstest_data(v.x...
1355                                        ^
1356 -- Tests for chained aggregates
1357 select a, b, grouping(a,b), sum(v), count(*), max(v)
1358   from gstest1 group by grouping sets ((a,b),(a+1,b+1),(a+2,b+2)) order by 3,6;
1359  a | b | grouping | sum | count | max 
1360 ---+---+----------+-----+-------+-----
1361  1 | 1 |        0 |  21 |     2 |  11
1362  1 | 2 |        0 |  25 |     2 |  13
1363  1 | 3 |        0 |  14 |     1 |  14
1364  2 | 3 |        0 |  15 |     1 |  15
1365  3 | 3 |        0 |  16 |     1 |  16
1366  3 | 4 |        0 |  17 |     1 |  17
1367  4 | 1 |        0 |  37 |     2 |  19
1368    |   |        3 |  21 |     2 |  11
1369    |   |        3 |  21 |     2 |  11
1370    |   |        3 |  25 |     2 |  13
1371    |   |        3 |  25 |     2 |  13
1372    |   |        3 |  14 |     1 |  14
1373    |   |        3 |  14 |     1 |  14
1374    |   |        3 |  15 |     1 |  15
1375    |   |        3 |  15 |     1 |  15
1376    |   |        3 |  16 |     1 |  16
1377    |   |        3 |  16 |     1 |  16
1378    |   |        3 |  17 |     1 |  17
1379    |   |        3 |  17 |     1 |  17
1380    |   |        3 |  37 |     2 |  19
1381    |   |        3 |  37 |     2 |  19
1382 (21 rows)
1384 explain (costs off)
1385   select a, b, grouping(a,b), sum(v), count(*), max(v)
1386     from gstest1 group by grouping sets ((a,b),(a+1,b+1),(a+2,b+2)) order by 3,6;
1387                                         QUERY PLAN                                         
1388 -------------------------------------------------------------------------------------------
1389  Sort
1390    Sort Key: (GROUPING("*VALUES*".column1, "*VALUES*".column2)), (max("*VALUES*".column3))
1391    ->  HashAggregate
1392          Hash Key: "*VALUES*".column1, "*VALUES*".column2
1393          Hash Key: ("*VALUES*".column1 + 1), ("*VALUES*".column2 + 1)
1394          Hash Key: ("*VALUES*".column1 + 2), ("*VALUES*".column2 + 2)
1395          ->  Values Scan on "*VALUES*"
1396 (7 rows)
1398 select a, b, sum(c), sum(sum(c)) over (order by a,b) as rsum
1399   from gstest2 group by cube (a,b) order by rsum, a, b;
1400  a | b | sum | rsum 
1401 ---+---+-----+------
1402  1 | 1 |   8 |    8
1403  1 | 2 |   2 |   10
1404  1 |   |  10 |   20
1405  2 | 2 |   2 |   22
1406  2 |   |   2 |   24
1407    | 1 |   8 |   32
1408    | 2 |   4 |   36
1409    |   |  12 |   48
1410 (8 rows)
1412 explain (costs off)
1413   select a, b, sum(c), sum(sum(c)) over (order by a,b) as rsum
1414     from gstest2 group by cube (a,b) order by rsum, a, b;
1415                  QUERY PLAN                  
1416 ---------------------------------------------
1417  Sort
1418    Sort Key: (sum((sum(c))) OVER (?)), a, b
1419    ->  WindowAgg
1420          ->  Sort
1421                Sort Key: a, b
1422                ->  MixedAggregate
1423                      Hash Key: a, b
1424                      Hash Key: a
1425                      Hash Key: b
1426                      Group Key: ()
1427                      ->  Seq Scan on gstest2
1428 (11 rows)
1430 select a, b, sum(v.x)
1431   from (values (1),(2)) v(x), gstest_data(v.x)
1432  group by cube (a,b) order by a,b;
1433  a | b | sum 
1434 ---+---+-----
1435  1 | 1 |   1
1436  1 | 2 |   1
1437  1 | 3 |   1
1438  1 |   |   3
1439  2 | 1 |   2
1440  2 | 2 |   2
1441  2 | 3 |   2
1442  2 |   |   6
1443    | 1 |   3
1444    | 2 |   3
1445    | 3 |   3
1446    |   |   9
1447 (12 rows)
1449 explain (costs off)
1450   select a, b, sum(v.x)
1451     from (values (1),(2)) v(x), gstest_data(v.x)
1452    group by cube (a,b) order by a,b;
1453                    QUERY PLAN                   
1454 ------------------------------------------------
1455  Sort
1456    Sort Key: gstest_data.a, gstest_data.b
1457    ->  MixedAggregate
1458          Hash Key: gstest_data.a, gstest_data.b
1459          Hash Key: gstest_data.a
1460          Hash Key: gstest_data.b
1461          Group Key: ()
1462          ->  Nested Loop
1463                ->  Values Scan on "*VALUES*"
1464                ->  Function Scan on gstest_data
1465 (10 rows)
1467 -- Verify that we correctly handle the child node returning a
1468 -- non-minimal slot, which happens if the input is pre-sorted,
1469 -- e.g. due to an index scan.
1470 BEGIN;
1471 SET LOCAL enable_hashagg = false;
1472 EXPLAIN (COSTS OFF) SELECT a, b, count(*), max(a), max(b) FROM gstest3 GROUP BY GROUPING SETS(a, b,()) ORDER BY a, b;
1473               QUERY PLAN               
1474 ---------------------------------------
1475  Sort
1476    Sort Key: a, b
1477    ->  GroupAggregate
1478          Group Key: a
1479          Group Key: ()
1480          Sort Key: b
1481            Group Key: b
1482          ->  Sort
1483                Sort Key: a
1484                ->  Seq Scan on gstest3
1485 (10 rows)
1487 SELECT a, b, count(*), max(a), max(b) FROM gstest3 GROUP BY GROUPING SETS(a, b,()) ORDER BY a, b;
1488  a | b | count | max | max 
1489 ---+---+-------+-----+-----
1490  1 |   |     1 |   1 |   1
1491  2 |   |     1 |   2 |   2
1492    | 1 |     1 |   1 |   1
1493    | 2 |     1 |   2 |   2
1494    |   |     2 |   2 |   2
1495 (5 rows)
1497 SET LOCAL enable_seqscan = false;
1498 EXPLAIN (COSTS OFF) SELECT a, b, count(*), max(a), max(b) FROM gstest3 GROUP BY GROUPING SETS(a, b,()) ORDER BY a, b;
1499                       QUERY PLAN                      
1500 ------------------------------------------------------
1501  Sort
1502    Sort Key: a, b
1503    ->  GroupAggregate
1504          Group Key: a
1505          Group Key: ()
1506          Sort Key: b
1507            Group Key: b
1508          ->  Index Scan using gstest3_pkey on gstest3
1509 (8 rows)
1511 SELECT a, b, count(*), max(a), max(b) FROM gstest3 GROUP BY GROUPING SETS(a, b,()) ORDER BY a, b;
1512  a | b | count | max | max 
1513 ---+---+-------+-----+-----
1514  1 |   |     1 |   1 |   1
1515  2 |   |     1 |   2 |   2
1516    | 1 |     1 |   1 |   1
1517    | 2 |     1 |   2 |   2
1518    |   |     2 |   2 |   2
1519 (5 rows)
1521 COMMIT;
1522 -- More rescan tests
1523 select * from (values (1),(2)) v(a) left join lateral (select v.a, four, ten, count(*) from onek group by cube(four,ten)) s on true order by v.a,four,ten;
1524  a | a | four | ten | count 
1525 ---+---+------+-----+-------
1526  1 | 1 |    0 |   0 |    50
1527  1 | 1 |    0 |   2 |    50
1528  1 | 1 |    0 |   4 |    50
1529  1 | 1 |    0 |   6 |    50
1530  1 | 1 |    0 |   8 |    50
1531  1 | 1 |    0 |     |   250
1532  1 | 1 |    1 |   1 |    50
1533  1 | 1 |    1 |   3 |    50
1534  1 | 1 |    1 |   5 |    50
1535  1 | 1 |    1 |   7 |    50
1536  1 | 1 |    1 |   9 |    50
1537  1 | 1 |    1 |     |   250
1538  1 | 1 |    2 |   0 |    50
1539  1 | 1 |    2 |   2 |    50
1540  1 | 1 |    2 |   4 |    50
1541  1 | 1 |    2 |   6 |    50
1542  1 | 1 |    2 |   8 |    50
1543  1 | 1 |    2 |     |   250
1544  1 | 1 |    3 |   1 |    50
1545  1 | 1 |    3 |   3 |    50
1546  1 | 1 |    3 |   5 |    50
1547  1 | 1 |    3 |   7 |    50
1548  1 | 1 |    3 |   9 |    50
1549  1 | 1 |    3 |     |   250
1550  1 | 1 |      |   0 |   100
1551  1 | 1 |      |   1 |   100
1552  1 | 1 |      |   2 |   100
1553  1 | 1 |      |   3 |   100
1554  1 | 1 |      |   4 |   100
1555  1 | 1 |      |   5 |   100
1556  1 | 1 |      |   6 |   100
1557  1 | 1 |      |   7 |   100
1558  1 | 1 |      |   8 |   100
1559  1 | 1 |      |   9 |   100
1560  1 | 1 |      |     |  1000
1561  2 | 2 |    0 |   0 |    50
1562  2 | 2 |    0 |   2 |    50
1563  2 | 2 |    0 |   4 |    50
1564  2 | 2 |    0 |   6 |    50
1565  2 | 2 |    0 |   8 |    50
1566  2 | 2 |    0 |     |   250
1567  2 | 2 |    1 |   1 |    50
1568  2 | 2 |    1 |   3 |    50
1569  2 | 2 |    1 |   5 |    50
1570  2 | 2 |    1 |   7 |    50
1571  2 | 2 |    1 |   9 |    50
1572  2 | 2 |    1 |     |   250
1573  2 | 2 |    2 |   0 |    50
1574  2 | 2 |    2 |   2 |    50
1575  2 | 2 |    2 |   4 |    50
1576  2 | 2 |    2 |   6 |    50
1577  2 | 2 |    2 |   8 |    50
1578  2 | 2 |    2 |     |   250
1579  2 | 2 |    3 |   1 |    50
1580  2 | 2 |    3 |   3 |    50
1581  2 | 2 |    3 |   5 |    50
1582  2 | 2 |    3 |   7 |    50
1583  2 | 2 |    3 |   9 |    50
1584  2 | 2 |    3 |     |   250
1585  2 | 2 |      |   0 |   100
1586  2 | 2 |      |   1 |   100
1587  2 | 2 |      |   2 |   100
1588  2 | 2 |      |   3 |   100
1589  2 | 2 |      |   4 |   100
1590  2 | 2 |      |   5 |   100
1591  2 | 2 |      |   6 |   100
1592  2 | 2 |      |   7 |   100
1593  2 | 2 |      |   8 |   100
1594  2 | 2 |      |   9 |   100
1595  2 | 2 |      |     |  1000
1596 (70 rows)
1598 select array(select row(v.a,s1.*) from (select two,four, count(*) from onek group by cube(two,four) order by two,four) s1) from (values (1),(2)) v(a);
1599                                                                         array                                                                         
1600 ------------------------------------------------------------------------------------------------------------------------------------------------------
1601  {"(1,0,0,250)","(1,0,2,250)","(1,0,,500)","(1,1,1,250)","(1,1,3,250)","(1,1,,500)","(1,,0,250)","(1,,1,250)","(1,,2,250)","(1,,3,250)","(1,,,1000)"}
1602  {"(2,0,0,250)","(2,0,2,250)","(2,0,,500)","(2,1,1,250)","(2,1,3,250)","(2,1,,500)","(2,,0,250)","(2,,1,250)","(2,,2,250)","(2,,3,250)","(2,,,1000)"}
1603 (2 rows)
1605 -- Rescan logic changes when there are no empty grouping sets, so test
1606 -- that too:
1607 select * from (values (1),(2)) v(a) left join lateral (select v.a, four, ten, count(*) from onek group by grouping sets(four,ten)) s on true order by v.a,four,ten;
1608  a | a | four | ten | count 
1609 ---+---+------+-----+-------
1610  1 | 1 |    0 |     |   250
1611  1 | 1 |    1 |     |   250
1612  1 | 1 |    2 |     |   250
1613  1 | 1 |    3 |     |   250
1614  1 | 1 |      |   0 |   100
1615  1 | 1 |      |   1 |   100
1616  1 | 1 |      |   2 |   100
1617  1 | 1 |      |   3 |   100
1618  1 | 1 |      |   4 |   100
1619  1 | 1 |      |   5 |   100
1620  1 | 1 |      |   6 |   100
1621  1 | 1 |      |   7 |   100
1622  1 | 1 |      |   8 |   100
1623  1 | 1 |      |   9 |   100
1624  2 | 2 |    0 |     |   250
1625  2 | 2 |    1 |     |   250
1626  2 | 2 |    2 |     |   250
1627  2 | 2 |    3 |     |   250
1628  2 | 2 |      |   0 |   100
1629  2 | 2 |      |   1 |   100
1630  2 | 2 |      |   2 |   100
1631  2 | 2 |      |   3 |   100
1632  2 | 2 |      |   4 |   100
1633  2 | 2 |      |   5 |   100
1634  2 | 2 |      |   6 |   100
1635  2 | 2 |      |   7 |   100
1636  2 | 2 |      |   8 |   100
1637  2 | 2 |      |   9 |   100
1638 (28 rows)
1640 select array(select row(v.a,s1.*) from (select two,four, count(*) from onek group by grouping sets(two,four) order by two,four) s1) from (values (1),(2)) v(a);
1641                                       array                                      
1642 ---------------------------------------------------------------------------------
1643  {"(1,0,,500)","(1,1,,500)","(1,,0,250)","(1,,1,250)","(1,,2,250)","(1,,3,250)"}
1644  {"(2,0,,500)","(2,1,,500)","(2,,0,250)","(2,,1,250)","(2,,2,250)","(2,,3,250)"}
1645 (2 rows)
1647 -- test the knapsack
1648 set enable_indexscan = false;
1649 set hash_mem_multiplier = 1.0;
1650 set work_mem = '64kB';
1651 explain (costs off)
1652   select unique1,
1653          count(two), count(four), count(ten),
1654          count(hundred), count(thousand), count(twothousand),
1655          count(*)
1656     from tenk1 group by grouping sets (unique1,twothousand,thousand,hundred,ten,four,two);
1657           QUERY PLAN           
1658 -------------------------------
1659  MixedAggregate
1660    Hash Key: two
1661    Hash Key: four
1662    Hash Key: ten
1663    Hash Key: hundred
1664    Group Key: unique1
1665    Sort Key: twothousand
1666      Group Key: twothousand
1667    Sort Key: thousand
1668      Group Key: thousand
1669    ->  Sort
1670          Sort Key: unique1
1671          ->  Seq Scan on tenk1
1672 (13 rows)
1674 explain (costs off)
1675   select unique1,
1676          count(two), count(four), count(ten),
1677          count(hundred), count(thousand), count(twothousand),
1678          count(*)
1679     from tenk1 group by grouping sets (unique1,hundred,ten,four,two);
1680           QUERY PLAN           
1681 -------------------------------
1682  MixedAggregate
1683    Hash Key: two
1684    Hash Key: four
1685    Hash Key: ten
1686    Hash Key: hundred
1687    Group Key: unique1
1688    ->  Sort
1689          Sort Key: unique1
1690          ->  Seq Scan on tenk1
1691 (9 rows)
1693 set work_mem = '384kB';
1694 explain (costs off)
1695   select unique1,
1696          count(two), count(four), count(ten),
1697          count(hundred), count(thousand), count(twothousand),
1698          count(*)
1699     from tenk1 group by grouping sets (unique1,twothousand,thousand,hundred,ten,four,two);
1700           QUERY PLAN           
1701 -------------------------------
1702  MixedAggregate
1703    Hash Key: two
1704    Hash Key: four
1705    Hash Key: ten
1706    Hash Key: hundred
1707    Hash Key: thousand
1708    Group Key: unique1
1709    Sort Key: twothousand
1710      Group Key: twothousand
1711    ->  Sort
1712          Sort Key: unique1
1713          ->  Seq Scan on tenk1
1714 (12 rows)
1716 -- check collation-sensitive matching between grouping expressions
1717 -- (similar to a check for aggregates, but there are additional code
1718 -- paths for GROUPING, so check again here)
1719 select v||'a', case grouping(v||'a') when 1 then 1 else 0 end, count(*)
1720   from unnest(array[1,1], array['a','b']) u(i,v)
1721  group by rollup(i, v||'a') order by 1,3;
1722  ?column? | case | count 
1723 ----------+------+-------
1724  aa       |    0 |     1
1725  ba       |    0 |     1
1726           |    1 |     2
1727           |    1 |     2
1728 (4 rows)
1730 select v||'a', case when grouping(v||'a') = 1 then 1 else 0 end, count(*)
1731   from unnest(array[1,1], array['a','b']) u(i,v)
1732  group by rollup(i, v||'a') order by 1,3;
1733  ?column? | case | count 
1734 ----------+------+-------
1735  aa       |    0 |     1
1736  ba       |    0 |     1
1737           |    1 |     2
1738           |    1 |     2
1739 (4 rows)
1741 -- Bug #16784
1742 create table bug_16784(i int, j int);
1743 analyze bug_16784;
1744 alter table bug_16784 set (autovacuum_enabled = 'false');
1745 update pg_class set reltuples = 10 where relname='bug_16784';
1746 insert into bug_16784 select g/10, g from generate_series(1,40) g;
1747 set work_mem='64kB';
1748 set enable_sort = false;
1749 select * from
1750   (values (1),(2)) v(a),
1751   lateral (select a, i, j, count(*) from
1752              bug_16784 group by cube(i,j)) s
1753   order by v.a, i, j;
1754  a | a | i | j  | count 
1755 ---+---+---+----+-------
1756  1 | 1 | 0 |  1 |     1
1757  1 | 1 | 0 |  2 |     1
1758  1 | 1 | 0 |  3 |     1
1759  1 | 1 | 0 |  4 |     1
1760  1 | 1 | 0 |  5 |     1
1761  1 | 1 | 0 |  6 |     1
1762  1 | 1 | 0 |  7 |     1
1763  1 | 1 | 0 |  8 |     1
1764  1 | 1 | 0 |  9 |     1
1765  1 | 1 | 0 |    |     9
1766  1 | 1 | 1 | 10 |     1
1767  1 | 1 | 1 | 11 |     1
1768  1 | 1 | 1 | 12 |     1
1769  1 | 1 | 1 | 13 |     1
1770  1 | 1 | 1 | 14 |     1
1771  1 | 1 | 1 | 15 |     1
1772  1 | 1 | 1 | 16 |     1
1773  1 | 1 | 1 | 17 |     1
1774  1 | 1 | 1 | 18 |     1
1775  1 | 1 | 1 | 19 |     1
1776  1 | 1 | 1 |    |    10
1777  1 | 1 | 2 | 20 |     1
1778  1 | 1 | 2 | 21 |     1
1779  1 | 1 | 2 | 22 |     1
1780  1 | 1 | 2 | 23 |     1
1781  1 | 1 | 2 | 24 |     1
1782  1 | 1 | 2 | 25 |     1
1783  1 | 1 | 2 | 26 |     1
1784  1 | 1 | 2 | 27 |     1
1785  1 | 1 | 2 | 28 |     1
1786  1 | 1 | 2 | 29 |     1
1787  1 | 1 | 2 |    |    10
1788  1 | 1 | 3 | 30 |     1
1789  1 | 1 | 3 | 31 |     1
1790  1 | 1 | 3 | 32 |     1
1791  1 | 1 | 3 | 33 |     1
1792  1 | 1 | 3 | 34 |     1
1793  1 | 1 | 3 | 35 |     1
1794  1 | 1 | 3 | 36 |     1
1795  1 | 1 | 3 | 37 |     1
1796  1 | 1 | 3 | 38 |     1
1797  1 | 1 | 3 | 39 |     1
1798  1 | 1 | 3 |    |    10
1799  1 | 1 | 4 | 40 |     1
1800  1 | 1 | 4 |    |     1
1801  1 | 1 |   |  1 |     1
1802  1 | 1 |   |  2 |     1
1803  1 | 1 |   |  3 |     1
1804  1 | 1 |   |  4 |     1
1805  1 | 1 |   |  5 |     1
1806  1 | 1 |   |  6 |     1
1807  1 | 1 |   |  7 |     1
1808  1 | 1 |   |  8 |     1
1809  1 | 1 |   |  9 |     1
1810  1 | 1 |   | 10 |     1
1811  1 | 1 |   | 11 |     1
1812  1 | 1 |   | 12 |     1
1813  1 | 1 |   | 13 |     1
1814  1 | 1 |   | 14 |     1
1815  1 | 1 |   | 15 |     1
1816  1 | 1 |   | 16 |     1
1817  1 | 1 |   | 17 |     1
1818  1 | 1 |   | 18 |     1
1819  1 | 1 |   | 19 |     1
1820  1 | 1 |   | 20 |     1
1821  1 | 1 |   | 21 |     1
1822  1 | 1 |   | 22 |     1
1823  1 | 1 |   | 23 |     1
1824  1 | 1 |   | 24 |     1
1825  1 | 1 |   | 25 |     1
1826  1 | 1 |   | 26 |     1
1827  1 | 1 |   | 27 |     1
1828  1 | 1 |   | 28 |     1
1829  1 | 1 |   | 29 |     1
1830  1 | 1 |   | 30 |     1
1831  1 | 1 |   | 31 |     1
1832  1 | 1 |   | 32 |     1
1833  1 | 1 |   | 33 |     1
1834  1 | 1 |   | 34 |     1
1835  1 | 1 |   | 35 |     1
1836  1 | 1 |   | 36 |     1
1837  1 | 1 |   | 37 |     1
1838  1 | 1 |   | 38 |     1
1839  1 | 1 |   | 39 |     1
1840  1 | 1 |   | 40 |     1
1841  1 | 1 |   |    |    40
1842  2 | 2 | 0 |  1 |     1
1843  2 | 2 | 0 |  2 |     1
1844  2 | 2 | 0 |  3 |     1
1845  2 | 2 | 0 |  4 |     1
1846  2 | 2 | 0 |  5 |     1
1847  2 | 2 | 0 |  6 |     1
1848  2 | 2 | 0 |  7 |     1
1849  2 | 2 | 0 |  8 |     1
1850  2 | 2 | 0 |  9 |     1
1851  2 | 2 | 0 |    |     9
1852  2 | 2 | 1 | 10 |     1
1853  2 | 2 | 1 | 11 |     1
1854  2 | 2 | 1 | 12 |     1
1855  2 | 2 | 1 | 13 |     1
1856  2 | 2 | 1 | 14 |     1
1857  2 | 2 | 1 | 15 |     1
1858  2 | 2 | 1 | 16 |     1
1859  2 | 2 | 1 | 17 |     1
1860  2 | 2 | 1 | 18 |     1
1861  2 | 2 | 1 | 19 |     1
1862  2 | 2 | 1 |    |    10
1863  2 | 2 | 2 | 20 |     1
1864  2 | 2 | 2 | 21 |     1
1865  2 | 2 | 2 | 22 |     1
1866  2 | 2 | 2 | 23 |     1
1867  2 | 2 | 2 | 24 |     1
1868  2 | 2 | 2 | 25 |     1
1869  2 | 2 | 2 | 26 |     1
1870  2 | 2 | 2 | 27 |     1
1871  2 | 2 | 2 | 28 |     1
1872  2 | 2 | 2 | 29 |     1
1873  2 | 2 | 2 |    |    10
1874  2 | 2 | 3 | 30 |     1
1875  2 | 2 | 3 | 31 |     1
1876  2 | 2 | 3 | 32 |     1
1877  2 | 2 | 3 | 33 |     1
1878  2 | 2 | 3 | 34 |     1
1879  2 | 2 | 3 | 35 |     1
1880  2 | 2 | 3 | 36 |     1
1881  2 | 2 | 3 | 37 |     1
1882  2 | 2 | 3 | 38 |     1
1883  2 | 2 | 3 | 39 |     1
1884  2 | 2 | 3 |    |    10
1885  2 | 2 | 4 | 40 |     1
1886  2 | 2 | 4 |    |     1
1887  2 | 2 |   |  1 |     1
1888  2 | 2 |   |  2 |     1
1889  2 | 2 |   |  3 |     1
1890  2 | 2 |   |  4 |     1
1891  2 | 2 |   |  5 |     1
1892  2 | 2 |   |  6 |     1
1893  2 | 2 |   |  7 |     1
1894  2 | 2 |   |  8 |     1
1895  2 | 2 |   |  9 |     1
1896  2 | 2 |   | 10 |     1
1897  2 | 2 |   | 11 |     1
1898  2 | 2 |   | 12 |     1
1899  2 | 2 |   | 13 |     1
1900  2 | 2 |   | 14 |     1
1901  2 | 2 |   | 15 |     1
1902  2 | 2 |   | 16 |     1
1903  2 | 2 |   | 17 |     1
1904  2 | 2 |   | 18 |     1
1905  2 | 2 |   | 19 |     1
1906  2 | 2 |   | 20 |     1
1907  2 | 2 |   | 21 |     1
1908  2 | 2 |   | 22 |     1
1909  2 | 2 |   | 23 |     1
1910  2 | 2 |   | 24 |     1
1911  2 | 2 |   | 25 |     1
1912  2 | 2 |   | 26 |     1
1913  2 | 2 |   | 27 |     1
1914  2 | 2 |   | 28 |     1
1915  2 | 2 |   | 29 |     1
1916  2 | 2 |   | 30 |     1
1917  2 | 2 |   | 31 |     1
1918  2 | 2 |   | 32 |     1
1919  2 | 2 |   | 33 |     1
1920  2 | 2 |   | 34 |     1
1921  2 | 2 |   | 35 |     1
1922  2 | 2 |   | 36 |     1
1923  2 | 2 |   | 37 |     1
1924  2 | 2 |   | 38 |     1
1925  2 | 2 |   | 39 |     1
1926  2 | 2 |   | 40 |     1
1927  2 | 2 |   |    |    40
1928 (172 rows)
1931 -- Compare results between plans using sorting and plans using hash
1932 -- aggregation. Force spilling in both cases by setting work_mem low
1933 -- and altering the statistics.
1935 create table gs_data_1 as
1936 select g%1000 as g1000, g%100 as g100, g%10 as g10, g
1937    from generate_series(0,1999) g;
1938 analyze gs_data_1;
1939 alter table gs_data_1 set (autovacuum_enabled = 'false');
1940 update pg_class set reltuples = 10 where relname='gs_data_1';
1941 set work_mem='64kB';
1942 -- Produce results with sorting.
1943 set enable_sort = true;
1944 set enable_hashagg = false;
1945 set jit_above_cost = 0;
1946 explain (costs off)
1947 select g100, g10, sum(g::numeric), count(*), max(g::text)
1948 from gs_data_1 group by cube (g1000, g100,g10);
1949              QUERY PLAN             
1950 ------------------------------------
1951  GroupAggregate
1952    Group Key: g1000, g100, g10
1953    Group Key: g1000, g100
1954    Group Key: g1000
1955    Group Key: ()
1956    Sort Key: g100, g10
1957      Group Key: g100, g10
1958      Group Key: g100
1959    Sort Key: g10, g1000
1960      Group Key: g10, g1000
1961      Group Key: g10
1962    ->  Sort
1963          Sort Key: g1000, g100, g10
1964          ->  Seq Scan on gs_data_1
1965 (14 rows)
1967 create table gs_group_1 as
1968 select g100, g10, sum(g::numeric), count(*), max(g::text)
1969 from gs_data_1 group by cube (g1000, g100,g10);
1970 -- Produce results with hash aggregation.
1971 set enable_hashagg = true;
1972 set enable_sort = false;
1973 explain (costs off)
1974 select g100, g10, sum(g::numeric), count(*), max(g::text)
1975 from gs_data_1 group by cube (g1000, g100,g10);
1976           QUERY PLAN          
1977 ------------------------------
1978  MixedAggregate
1979    Hash Key: g1000, g100, g10
1980    Hash Key: g1000, g100
1981    Hash Key: g1000
1982    Hash Key: g100, g10
1983    Hash Key: g100
1984    Hash Key: g10, g1000
1985    Hash Key: g10
1986    Group Key: ()
1987    ->  Seq Scan on gs_data_1
1988 (10 rows)
1990 create table gs_hash_1 as
1991 select g100, g10, sum(g::numeric), count(*), max(g::text)
1992 from gs_data_1 group by cube (g1000, g100,g10);
1993 set enable_sort = true;
1994 set work_mem to default;
1995 set hash_mem_multiplier to default;
1996 -- Compare results
1997 (select * from gs_hash_1 except select * from gs_group_1)
1998   union all
1999 (select * from gs_group_1 except select * from gs_hash_1);
2000  g100 | g10 | sum | count | max 
2001 ------+-----+-----+-------+-----
2002 (0 rows)
2004 drop table gs_group_1;
2005 drop table gs_hash_1;
2006 -- GROUP BY DISTINCT
2007 -- "normal" behavior...
2008 select a, b, c
2009 from (values (1, 2, 3), (4, null, 6), (7, 8, 9)) as t (a, b, c)
2010 group by all rollup(a, b), rollup(a, c)
2011 order by a, b, c;
2012  a | b | c 
2013 ---+---+---
2014  1 | 2 | 3
2015  1 | 2 |  
2016  1 | 2 |  
2017  1 |   | 3
2018  1 |   | 3
2019  1 |   |  
2020  1 |   |  
2021  1 |   |  
2022  4 |   | 6
2023  4 |   | 6
2024  4 |   | 6
2025  4 |   |  
2026  4 |   |  
2027  4 |   |  
2028  4 |   |  
2029  4 |   |  
2030  7 | 8 | 9
2031  7 | 8 |  
2032  7 | 8 |  
2033  7 |   | 9
2034  7 |   | 9
2035  7 |   |  
2036  7 |   |  
2037  7 |   |  
2038    |   |  
2039 (25 rows)
2041 -- ...which is also the default
2042 select a, b, c
2043 from (values (1, 2, 3), (4, null, 6), (7, 8, 9)) as t (a, b, c)
2044 group by rollup(a, b), rollup(a, c)
2045 order by a, b, c;
2046  a | b | c 
2047 ---+---+---
2048  1 | 2 | 3
2049  1 | 2 |  
2050  1 | 2 |  
2051  1 |   | 3
2052  1 |   | 3
2053  1 |   |  
2054  1 |   |  
2055  1 |   |  
2056  4 |   | 6
2057  4 |   | 6
2058  4 |   | 6
2059  4 |   |  
2060  4 |   |  
2061  4 |   |  
2062  4 |   |  
2063  4 |   |  
2064  7 | 8 | 9
2065  7 | 8 |  
2066  7 | 8 |  
2067  7 |   | 9
2068  7 |   | 9
2069  7 |   |  
2070  7 |   |  
2071  7 |   |  
2072    |   |  
2073 (25 rows)
2075 -- "group by distinct" behavior...
2076 select a, b, c
2077 from (values (1, 2, 3), (4, null, 6), (7, 8, 9)) as t (a, b, c)
2078 group by distinct rollup(a, b), rollup(a, c)
2079 order by a, b, c;
2080  a | b | c 
2081 ---+---+---
2082  1 | 2 | 3
2083  1 | 2 |  
2084  1 |   | 3
2085  1 |   |  
2086  4 |   | 6
2087  4 |   | 6
2088  4 |   |  
2089  4 |   |  
2090  7 | 8 | 9
2091  7 | 8 |  
2092  7 |   | 9
2093  7 |   |  
2094    |   |  
2095 (13 rows)
2097 -- ...which is not the same as "select distinct"
2098 select distinct a, b, c
2099 from (values (1, 2, 3), (4, null, 6), (7, 8, 9)) as t (a, b, c)
2100 group by rollup(a, b), rollup(a, c)
2101 order by a, b, c;
2102  a | b | c 
2103 ---+---+---
2104  1 | 2 | 3
2105  1 | 2 |  
2106  1 |   | 3
2107  1 |   |  
2108  4 |   | 6
2109  4 |   |  
2110  7 | 8 | 9
2111  7 | 8 |  
2112  7 |   | 9
2113  7 |   |  
2114    |   |  
2115 (11 rows)
2117 -- test handling of outer GroupingFunc within subqueries
2118 explain (costs off)
2119 select (select grouping(v1)) from (values ((select 1))) v(v1) group by cube(v1);
2120           QUERY PLAN           
2121 -------------------------------
2122  MixedAggregate
2123    Hash Key: (InitPlan 3).col1
2124    Group Key: ()
2125    InitPlan 1
2126      ->  Result
2127    InitPlan 3
2128      ->  Result
2129    ->  Result
2130    SubPlan 2
2131      ->  Result
2132 (10 rows)
2134 select (select grouping(v1)) from (values ((select 1))) v(v1) group by cube(v1);
2135  grouping 
2136 ----------
2137         1
2138         0
2139 (2 rows)
2141 explain (costs off)
2142 select (select grouping(v1)) from (values ((select 1))) v(v1) group by v1;
2143    QUERY PLAN   
2144 ----------------
2145  GroupAggregate
2146    InitPlan 1
2147      ->  Result
2148    InitPlan 3
2149      ->  Result
2150    ->  Result
2151    SubPlan 2
2152      ->  Result
2153 (8 rows)
2155 select (select grouping(v1)) from (values ((select 1))) v(v1) group by v1;
2156  grouping 
2157 ----------
2158         0
2159 (1 row)
2161 -- test handling of subqueries in grouping sets
2162 create temp table gstest5(id integer primary key, v integer);
2163 insert into gstest5 select i, i from generate_series(1,5)i;
2164 explain (verbose, costs off)
2165 select grouping((select t1.v from gstest5 t2 where id = t1.id)),
2166        (select t1.v from gstest5 t2 where id = t1.id) as s
2167 from gstest5 t1
2168 group by grouping sets(v, s)
2169 order by case when grouping((select t1.v from gstest5 t2 where id = t1.id)) = 0
2170               then (select t1.v from gstest5 t2 where id = t1.id)
2171               else null end
2172          nulls first;
2173                                                                  QUERY PLAN                                                                  
2174 ---------------------------------------------------------------------------------------------------------------------------------------------
2175  Sort
2176    Output: (GROUPING((SubPlan 1))), ((SubPlan 3)), (CASE WHEN (GROUPING((SubPlan 2)) = 0) THEN ((SubPlan 3)) ELSE NULL::integer END), t1.v
2177    Sort Key: (CASE WHEN (GROUPING((SubPlan 2)) = 0) THEN ((SubPlan 3)) ELSE NULL::integer END) NULLS FIRST
2178    ->  HashAggregate
2179          Output: GROUPING((SubPlan 1)), ((SubPlan 3)), CASE WHEN (GROUPING((SubPlan 2)) = 0) THEN ((SubPlan 3)) ELSE NULL::integer END, t1.v
2180          Hash Key: t1.v
2181          Hash Key: (SubPlan 3)
2182          ->  Seq Scan on pg_temp.gstest5 t1
2183                Output: (SubPlan 3), t1.v, t1.id
2184                SubPlan 3
2185                  ->  Bitmap Heap Scan on pg_temp.gstest5 t2
2186                        Output: t1.v
2187                        Recheck Cond: (t2.id = t1.id)
2188                        ->  Bitmap Index Scan on gstest5_pkey
2189                              Index Cond: (t2.id = t1.id)
2190 (15 rows)
2192 select grouping((select t1.v from gstest5 t2 where id = t1.id)),
2193        (select t1.v from gstest5 t2 where id = t1.id) as s
2194 from gstest5 t1
2195 group by grouping sets(v, s)
2196 order by case when grouping((select t1.v from gstest5 t2 where id = t1.id)) = 0
2197               then (select t1.v from gstest5 t2 where id = t1.id)
2198               else null end
2199          nulls first;
2200  grouping | s 
2201 ----------+---
2202         1 |  
2203         1 |  
2204         1 |  
2205         1 |  
2206         1 |  
2207         0 | 1
2208         0 | 2
2209         0 | 3
2210         0 | 4
2211         0 | 5
2212 (10 rows)
2214 explain (verbose, costs off)
2215 select grouping((select t1.v from gstest5 t2 where id = t1.id)),
2216        (select t1.v from gstest5 t2 where id = t1.id) as s,
2217        case when grouping((select t1.v from gstest5 t2 where id = t1.id)) = 0
2218             then (select t1.v from gstest5 t2 where id = t1.id)
2219             else null end as o
2220 from gstest5 t1
2221 group by grouping sets(v, s)
2222 order by o nulls first;
2223                                                                  QUERY PLAN                                                                  
2224 ---------------------------------------------------------------------------------------------------------------------------------------------
2225  Sort
2226    Output: (GROUPING((SubPlan 1))), ((SubPlan 3)), (CASE WHEN (GROUPING((SubPlan 2)) = 0) THEN ((SubPlan 3)) ELSE NULL::integer END), t1.v
2227    Sort Key: (CASE WHEN (GROUPING((SubPlan 2)) = 0) THEN ((SubPlan 3)) ELSE NULL::integer END) NULLS FIRST
2228    ->  HashAggregate
2229          Output: GROUPING((SubPlan 1)), ((SubPlan 3)), CASE WHEN (GROUPING((SubPlan 2)) = 0) THEN ((SubPlan 3)) ELSE NULL::integer END, t1.v
2230          Hash Key: t1.v
2231          Hash Key: (SubPlan 3)
2232          ->  Seq Scan on pg_temp.gstest5 t1
2233                Output: (SubPlan 3), t1.v, t1.id
2234                SubPlan 3
2235                  ->  Bitmap Heap Scan on pg_temp.gstest5 t2
2236                        Output: t1.v
2237                        Recheck Cond: (t2.id = t1.id)
2238                        ->  Bitmap Index Scan on gstest5_pkey
2239                              Index Cond: (t2.id = t1.id)
2240 (15 rows)
2242 select grouping((select t1.v from gstest5 t2 where id = t1.id)),
2243        (select t1.v from gstest5 t2 where id = t1.id) as s,
2244        case when grouping((select t1.v from gstest5 t2 where id = t1.id)) = 0
2245             then (select t1.v from gstest5 t2 where id = t1.id)
2246             else null end as o
2247 from gstest5 t1
2248 group by grouping sets(v, s)
2249 order by o nulls first;
2250  grouping | s | o 
2251 ----------+---+---
2252         1 |   |  
2253         1 |   |  
2254         1 |   |  
2255         1 |   |  
2256         1 |   |  
2257         0 | 1 | 1
2258         0 | 2 | 2
2259         0 | 3 | 3
2260         0 | 4 | 4
2261         0 | 5 | 5
2262 (10 rows)
2264 -- test handling of expressions that should match lower target items
2265 explain (costs off)
2266 select a < b and b < 3 from (values (1, 2)) t(a, b) group by rollup(a < b and b < 3) having a < b and b < 3;
2267             QUERY PLAN             
2268 -----------------------------------
2269  MixedAggregate
2270    Hash Key: ((1 < 2) AND (2 < 3))
2271    Group Key: ()
2272    Filter: (((1 < 2) AND (2 < 3)))
2273    ->  Result
2274 (5 rows)
2276 select a < b and b < 3 from (values (1, 2)) t(a, b) group by rollup(a < b and b < 3) having a < b and b < 3;
2277  ?column? 
2278 ----------
2280 (1 row)
2282 explain (costs off)
2283 select not a from (values(true)) t(a) group by rollup(not a) having not not a;
2284           QUERY PLAN          
2285 ------------------------------
2286  MixedAggregate
2287    Hash Key: (NOT true)
2288    Group Key: ()
2289    Filter: (NOT ((NOT true)))
2290    ->  Result
2291 (5 rows)
2293 select not a from (values(true)) t(a) group by rollup(not a) having not not a;
2294  ?column? 
2295 ----------
2297 (1 row)
2299 -- test handling of expressions nullable by grouping sets
2300 explain (costs off)
2301 select distinct on (a, b) a, b
2302 from (values (1, 1), (2, 2)) as t (a, b) where a = b
2303 group by grouping sets((a, b), (a))
2304 order by a, b;
2305                            QUERY PLAN                           
2306 ----------------------------------------------------------------
2307  Unique
2308    ->  Sort
2309          Sort Key: "*VALUES*".column1, "*VALUES*".column2
2310          ->  HashAggregate
2311                Hash Key: "*VALUES*".column1, "*VALUES*".column2
2312                Hash Key: "*VALUES*".column1
2313                ->  Values Scan on "*VALUES*"
2314                      Filter: (column1 = column2)
2315 (8 rows)
2317 select distinct on (a, b) a, b
2318 from (values (1, 1), (2, 2)) as t (a, b) where a = b
2319 group by grouping sets((a, b), (a))
2320 order by a, b;
2321  a | b 
2322 ---+---
2323  1 | 1
2324  1 |  
2325  2 | 2
2326  2 |  
2327 (4 rows)
2329 explain (costs off)
2330 select distinct on (a, b+1) a, b+1
2331 from (values (1, 0), (2, 1)) as t (a, b) where a = b+1
2332 group by grouping sets((a, b+1), (a))
2333 order by a, b+1;
2334                               QUERY PLAN                              
2335 ----------------------------------------------------------------------
2336  Unique
2337    ->  Sort
2338          Sort Key: "*VALUES*".column1, (("*VALUES*".column2 + 1))
2339          ->  HashAggregate
2340                Hash Key: "*VALUES*".column1, ("*VALUES*".column2 + 1)
2341                Hash Key: "*VALUES*".column1
2342                ->  Values Scan on "*VALUES*"
2343                      Filter: (column1 = (column2 + 1))
2344 (8 rows)
2346 select distinct on (a, b+1) a, b+1
2347 from (values (1, 0), (2, 1)) as t (a, b) where a = b+1
2348 group by grouping sets((a, b+1), (a))
2349 order by a, b+1;
2350  a | ?column? 
2351 ---+----------
2352  1 |        1
2353  1 |         
2354  2 |        2
2355  2 |         
2356 (4 rows)
2358 explain (costs off)
2359 select a, b
2360 from (values (1, 1), (2, 2)) as t (a, b) where a = b
2361 group by grouping sets((a, b), (a))
2362 order by a, b nulls first;
2363                            QUERY PLAN                           
2364 ----------------------------------------------------------------
2365  Sort
2366    Sort Key: "*VALUES*".column1, "*VALUES*".column2 NULLS FIRST
2367    ->  HashAggregate
2368          Hash Key: "*VALUES*".column1, "*VALUES*".column2
2369          Hash Key: "*VALUES*".column1
2370          ->  Values Scan on "*VALUES*"
2371                Filter: (column1 = column2)
2372 (7 rows)
2374 select a, b
2375 from (values (1, 1), (2, 2)) as t (a, b) where a = b
2376 group by grouping sets((a, b), (a))
2377 order by a, b nulls first;
2378  a | b 
2379 ---+---
2380  1 |  
2381  1 | 1
2382  2 |  
2383  2 | 2
2384 (4 rows)
2386 explain (costs off)
2387 select 1 as one group by rollup(one) order by one nulls first;
2388          QUERY PLAN          
2389 -----------------------------
2390  Sort
2391    Sort Key: (1) NULLS FIRST
2392    ->  MixedAggregate
2393          Hash Key: 1
2394          Group Key: ()
2395          ->  Result
2396 (6 rows)
2398 select 1 as one group by rollup(one) order by one nulls first;
2399  one 
2400 -----
2401     
2402    1
2403 (2 rows)
2405 explain (costs off)
2406 select a, b, row_number() over (order by a, b nulls first)
2407 from (values (1, 1), (2, 2)) as t (a, b) where a = b
2408 group by grouping sets((a, b), (a));
2409                               QUERY PLAN                              
2410 ----------------------------------------------------------------------
2411  WindowAgg
2412    ->  Sort
2413          Sort Key: "*VALUES*".column1, "*VALUES*".column2 NULLS FIRST
2414          ->  HashAggregate
2415                Hash Key: "*VALUES*".column1, "*VALUES*".column2
2416                Hash Key: "*VALUES*".column1
2417                ->  Values Scan on "*VALUES*"
2418                      Filter: (column1 = column2)
2419 (8 rows)
2421 select a, b, row_number() over (order by a, b nulls first)
2422 from (values (1, 1), (2, 2)) as t (a, b) where a = b
2423 group by grouping sets((a, b), (a));
2424  a | b | row_number 
2425 ---+---+------------
2426  1 |   |          1
2427  1 | 1 |          2
2428  2 |   |          3
2429  2 | 2 |          4
2430 (4 rows)
2432 -- end