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),
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);
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)
28 return query select v, i from generate_series(1,3) i;
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
43 2 | 3 | 0 | 15 | 1 | 15
45 3 | 3 | 0 | 16 | 1 | 16
46 3 | 4 | 0 | 17 | 1 | 17
48 4 | 1 | 0 | 37 | 2 | 19
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
61 2 | 3 | 0 | 15 | 1 | 15
63 3 | 3 | 0 | 16 | 1 | 16
64 3 | 4 | 0 | 17 | 1 | 17
66 4 | 1 | 0 | 37 | 2 | 19
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 ---+---+----------+-----+-------+-----
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
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 ---+---+----------+-----+-------+-----
95 1 | 1 | 0 | 21 | 2 | 11
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
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
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
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;
156 -- nesting with grouping sets
157 select sum(c) from gstest2
158 group by grouping sets((), grouping sets((), grouping sets(())))
167 select sum(c) from gstest2
168 group by grouping sets((), grouping sets((), grouping sets(((a, b)))))
179 select sum(c) from gstest2
180 group by grouping sets(grouping sets(rollup(c), grouping sets(cube(c))))
192 select sum(c) from gstest2
193 group by grouping sets(a, grouping sets(a, cube(b)))
206 select sum(c) from gstest2
207 group by grouping sets(grouping sets((a, (b))))
216 select sum(c) from gstest2
217 group by grouping sets(grouping sets((a, b)))
226 select sum(c) from gstest2
227 group by grouping sets(grouping sets(a, grouping sets(a), a))
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))
262 select sum(c) from gstest2
263 group by grouping sets((a,(a,b)), grouping sets((a,(a,b)),a))
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);
280 ---+---+-----+-------
283 select a, b, sum(v), count(*) from gstest_empty group by grouping sets ((a,b),());
285 ---+---+-----+-------
289 select a, b, sum(v), count(*) from gstest_empty group by grouping sets ((a,b),(),(),());
291 ---+---+-----+-------
297 select sum(v), count(*) from gstest_empty group by grouping sets ((),(),());
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),());
309 ---+---+-----+-------
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 ---+---+----------+------+-----
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 ---+---+----------+-----+-----
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 ---+---+----------+-----+-----
351 -- check that functionally dependent cols are not nulled
352 select a, d, grouping(a,b,c)
354 group by grouping sets ((a,b), (a,c));
363 -- check that distinct grouping columns are kept separate
364 -- even if they are equal()
366 select g as alias1, g as alias2
367 from generate_series(1,3) g
368 group by alias1, rollup(alias2);
370 ------------------------------------------------
376 -> Function Scan on generate_series g
379 select g as alias1, g as alias2
380 from generate_series(1,3) g
381 group by alias1, rollup(alias2);
392 -- check that pulled-up subquery outputs still go to null when appropriate
394 from (select four, ten, 'foo'::text as x from tenk1) as t
395 group by grouping sets (four, x)
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)
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);
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)
429 ---+-------+-------------------
431 | | -4567890123456789
437 -- check qual push-down rules for a subquery with grouping sets
438 explain (verbose, costs off)
440 select 1 as x, q1, sum(q2)
442 group by grouping sets(1, 2)
444 where x = 1 and q1 = 123;
446 --------------------------------------------------
448 Output: ss.x, ss.q1, ss.sum
449 Filter: ((ss.x = 1) AND (ss.q1 = 123))
451 Output: (1), i1.q1, sum(i1.q2)
456 Output: (1), i1.q1, i1.q2
458 -> Seq Scan on public.int8_tbl i1
459 Output: 1, i1.q1, i1.q2
463 select 1 as x, q1, sum(q2)
465 group by grouping sets(1, 2)
467 where x = 1 and q1 = 123;
472 -- check handling of pulled-up SubPlan in GROUPING() argument (bug #17479)
473 explain (verbose, costs off)
474 select grouping(ss.x)
476 cross join lateral (select (select i1.q1) as x) ss
479 ------------------------------------------------
481 Output: GROUPING((SubPlan 1)), ((SubPlan 2))
482 Group Key: ((SubPlan 2))
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
493 select grouping(ss.x)
495 cross join lateral (select (select i1.q1) as x) ss
503 explain (verbose, costs off)
504 select (select grouping(ss.x))
506 cross join lateral (select (select i1.q1) as x) ss
509 --------------------------------------------
511 Output: (SubPlan 2), ((SubPlan 3))
512 Group Key: ((SubPlan 3))
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
523 Output: GROUPING((SubPlan 1))
526 select (select grouping(ss.x))
528 cross join lateral (select (select i1.q1) as x) ss
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);
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) ...
559 -- min max optimization should still work with GROUP BY ()
561 select min(unique1) from tenk1 GROUP BY ();
563 ------------------------------------------------------------
567 -> Index Only Scan using tenk1_unique1 on tenk1
568 Index Cond: (unique1 IS NOT NULL)
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);
577 ---------------------------------------
580 GROUPING(a, b) AS "grouping", +
585 GROUP BY ROLLUP((a, b, c), (c, d));
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);
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);
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);
613 -- Combinations of operations
614 select a, b, c, d from gstest2 group by rollup(a,b),grouping sets(c,d);
637 select a, b from (values (1,2),(2,3)) v(a,b) group by a,b, grouping sets(a);
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
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));
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);
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;
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);
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;
739 -- Test reordering of grouping sets
741 select * from gstest1 group by grouping sets((a,b,v),(v)) order by v,b,a;
743 ------------------------------------------------------------------------------------
745 Sort Key: "*VALUES*".column3, "*VALUES*".column2, "*VALUES*".column1
746 Presorted Key: "*VALUES*".column3
748 Group Key: "*VALUES*".column3, "*VALUES*".column2, "*VALUES*".column1
749 Group Key: "*VALUES*".column3
751 Sort Key: "*VALUES*".column3, "*VALUES*".column2, "*VALUES*".column1
752 -> Values Scan on "*VALUES*"
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...
761 select a, b, sum(c), count(*) from gstest2 group by grouping sets (rollup(a,b),a);
763 ---+---+-----+-------
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);
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;
816 select a,count(*) from gstest2 group by rollup(a) having a is distinct from 1 order by a;
824 select a,count(*) from gstest2 group by rollup(a) having a is distinct from 1 order by a;
826 ----------------------------------------
832 Filter: (a IS DISTINCT FROM 1)
835 -> Seq Scan on gstest2
838 select v.c, (select count(*) from gstest2 group by () having v.c)
839 from (values (false),(true)) v(c) order by v.c;
847 select v.c, (select count(*) from gstest2 group by () having v.c)
848 from (values (false),(true)) v(c) order by v.c;
850 -----------------------------------------------------------
852 Sort Key: "*VALUES*".column1
853 -> Values Scan on "*VALUES*"
857 Filter: "*VALUES*".column1
859 One-Time Filter: "*VALUES*".column1
860 -> Seq Scan on gstest2
863 -- HAVING with GROUPING queries
864 select ten, grouping(ten) from onek
865 group by grouping sets(ten) having grouping(ten) >= 0
881 select ten, grouping(ten) from onek
882 group by grouping sets(ten, four) having grouping(ten) > 0
892 select ten, grouping(ten) from onek
893 group by rollup(ten) having grouping(ten) > 0
900 select ten, grouping(ten) from onek
901 group by cube(ten) having grouping(ten) > 0
908 select ten, grouping(ten) from onek
909 group by (ten) having grouping(ten) >= 0
926 select ten, sum(distinct four) filter (where four::text ~ '123') from onek a
927 group by rollup(ten);
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 ---+---+------+-----+-------
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);
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)"}
1026 -- Grouping on text columns
1027 select sum(ten) from onek group by two, rollup(four::text) order by 1;
1038 select sum(ten) from onek group by rollup(four::text), two order by 1;
1050 set enable_hashagg = true;
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.
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
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;
1076 --------------------------------------------------------------------------------------------------------
1078 Sort Key: (GROUPING("*VALUES*".column1, "*VALUES*".column2)), "*VALUES*".column1, "*VALUES*".column2
1080 Hash Key: "*VALUES*".column1
1081 Hash Key: "*VALUES*".column2
1082 -> Values Scan on "*VALUES*"
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
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;
1110 --------------------------------------------------------------------------------------------------------
1112 Sort Key: (GROUPING("*VALUES*".column1, "*VALUES*".column2)), "*VALUES*".column1, "*VALUES*".column2
1114 Hash Key: "*VALUES*".column1, "*VALUES*".column2
1115 Hash Key: "*VALUES*".column1
1116 Hash Key: "*VALUES*".column2
1118 -> Values Scan on "*VALUES*"
1121 -- shouldn't try and hash
1123 select a, b, grouping(a,b), array_agg(v order by v)
1124 from gstest1 group by cube(a,b);
1126 ----------------------------------------------------------
1128 Group Key: "*VALUES*".column1, "*VALUES*".column2
1129 Group Key: "*VALUES*".column1
1131 Sort Key: "*VALUES*".column2
1132 Group Key: "*VALUES*".column2
1134 Sort Key: "*VALUES*".column1, "*VALUES*".column2
1135 -> Values Scan on "*VALUES*"
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 ----------------+-------
1150 -- mixed hashable/sortable cases
1151 select unhashable_col, unsortable_col,
1152 grouping(unhashable_col, unsortable_col),
1154 from gstest4 group by grouping sets ((unhashable_col),(unsortable_col))
1156 unhashable_col | unsortable_col | grouping | count | sum
1157 ----------------+----------------+----------+-------+-----
1161 0011 | | 1 | 2 | 136
1167 select unhashable_col, unsortable_col,
1168 grouping(unhashable_col, unsortable_col),
1170 from gstest4 group by grouping sets ((unhashable_col),(unsortable_col))
1173 ------------------------------------------------------------------
1175 Sort Key: (GROUPING(unhashable_col, unsortable_col)), (sum(v))
1177 Hash Key: unsortable_col
1178 Group Key: unhashable_col
1180 Sort Key: unhashable_col
1181 -> Seq Scan on gstest4
1184 select unhashable_col, unsortable_col,
1185 grouping(unhashable_col, unsortable_col),
1187 from gstest4 group by grouping sets ((v,unhashable_col),(v,unsortable_col))
1189 unhashable_col | unsortable_col | grouping | count | sum
1190 ----------------+----------------+----------+-------+-----
1198 0011 | | 1 | 1 | 128
1210 select unhashable_col, unsortable_col,
1211 grouping(unhashable_col, unsortable_col),
1213 from gstest4 group by grouping sets ((v,unhashable_col),(v,unsortable_col))
1216 ------------------------------------------------------------------
1218 Sort Key: (GROUPING(unhashable_col, unsortable_col)), (sum(v))
1220 Hash Key: v, unsortable_col
1221 Group Key: v, unhashable_col
1223 Sort Key: v, unhashable_col
1224 -> Seq Scan on gstest4
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);
1230 ---+---+-----+-------
1234 select a, b, sum(v), count(*) from gstest_empty group by grouping sets ((a,b),a);
1236 --------------------------------
1240 -> Seq Scan on gstest_empty
1243 select a, b, sum(v), count(*) from gstest_empty group by grouping sets ((a,b),());
1245 ---+---+-----+-------
1249 select a, b, sum(v), count(*) from gstest_empty group by grouping sets ((a,b),(),(),());
1251 ---+---+-----+-------
1258 select a, b, sum(v), count(*) from gstest_empty group by grouping sets ((a,b),(),(),());
1260 --------------------------------
1266 -> Seq Scan on gstest_empty
1269 select sum(v), count(*) from gstest_empty group by grouping sets ((),(),());
1278 select sum(v), count(*) from gstest_empty group by grouping sets ((),(),());
1280 --------------------------------
1285 -> Seq Scan on gstest_empty
1288 -- check that functionally dependent cols are not nulled
1289 select a, d, grouping(a,b,c)
1291 group by grouping sets ((a,b), (a,c));
1301 select a, d, grouping(a,b,c)
1303 group by grouping sets ((a,b), (a,c));
1305 ---------------------------
1309 -> Seq Scan on gstest3
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)
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)
1332 ---------------------------------------------------------------------
1334 Sort Key: (sum("*VALUES*".column1)), gstest_data.a, gstest_data.b
1336 Hash Key: gstest_data.a
1337 Hash Key: gstest_data.b
1339 -> Values Scan on "*VALUES*"
1340 -> Function Scan on gstest_data
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) ...
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...
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
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;
1388 -------------------------------------------------------------------------------------------
1390 Sort Key: (GROUPING("*VALUES*".column1, "*VALUES*".column2)), (max("*VALUES*".column3))
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*"
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;
1401 ---+---+-----+------
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;
1416 ---------------------------------------------
1418 Sort Key: (sum((sum(c))) OVER (?)), a, b
1427 -> Seq Scan on gstest2
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;
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;
1454 ------------------------------------------------
1456 Sort Key: gstest_data.a, gstest_data.b
1458 Hash Key: gstest_data.a, gstest_data.b
1459 Hash Key: gstest_data.a
1460 Hash Key: gstest_data.b
1463 -> Values Scan on "*VALUES*"
1464 -> Function Scan on gstest_data
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.
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;
1474 ---------------------------------------
1484 -> Seq Scan on gstest3
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 ---+---+-------+-----+-----
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;
1500 ------------------------------------------------------
1508 -> Index Scan using gstest3_pkey on gstest3
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 ---+---+-------+-----+-----
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 ---+---+------+-----+-------
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);
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)"}
1605 -- Rescan logic changes when there are no empty grouping sets, so test
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 ---+---+------+-----+-------
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);
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)"}
1647 -- test the knapsack
1648 set enable_indexscan = false;
1649 set hash_mem_multiplier = 1.0;
1650 set work_mem = '64kB';
1653 count(two), count(four), count(ten),
1654 count(hundred), count(thousand), count(twothousand),
1656 from tenk1 group by grouping sets (unique1,twothousand,thousand,hundred,ten,four,two);
1658 -------------------------------
1665 Sort Key: twothousand
1666 Group Key: twothousand
1671 -> Seq Scan on tenk1
1676 count(two), count(four), count(ten),
1677 count(hundred), count(thousand), count(twothousand),
1679 from tenk1 group by grouping sets (unique1,hundred,ten,four,two);
1681 -------------------------------
1690 -> Seq Scan on tenk1
1693 set work_mem = '384kB';
1696 count(two), count(four), count(ten),
1697 count(hundred), count(thousand), count(twothousand),
1699 from tenk1 group by grouping sets (unique1,twothousand,thousand,hundred,ten,four,two);
1701 -------------------------------
1709 Sort Key: twothousand
1710 Group Key: twothousand
1713 -> Seq Scan on tenk1
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 ----------+------+-------
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 ----------+------+-------
1742 create table bug_16784(i int, j int);
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;
1750 (values (1),(2)) v(a),
1751 lateral (select a, i, j, count(*) from
1752 bug_16784 group by cube(i,j)) s
1754 a | a | i | j | count
1755 ---+---+---+----+-------
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;
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;
1947 select g100, g10, sum(g::numeric), count(*), max(g::text)
1948 from gs_data_1 group by cube (g1000, g100,g10);
1950 ------------------------------------
1952 Group Key: g1000, g100, g10
1953 Group Key: g1000, g100
1957 Group Key: g100, g10
1959 Sort Key: g10, g1000
1960 Group Key: g10, g1000
1963 Sort Key: g1000, g100, g10
1964 -> Seq Scan on gs_data_1
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;
1974 select g100, g10, sum(g::numeric), count(*), max(g::text)
1975 from gs_data_1 group by cube (g1000, g100,g10);
1977 ------------------------------
1979 Hash Key: g1000, g100, g10
1980 Hash Key: g1000, g100
1984 Hash Key: g10, g1000
1987 -> Seq Scan on gs_data_1
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;
1997 (select * from gs_hash_1 except select * from gs_group_1)
1999 (select * from gs_group_1 except select * from gs_hash_1);
2000 g100 | g10 | sum | count | max
2001 ------+-----+-----+-------+-----
2004 drop table gs_group_1;
2005 drop table gs_hash_1;
2006 -- GROUP BY DISTINCT
2007 -- "normal" behavior...
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)
2041 -- ...which is also the default
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)
2075 -- "group by distinct" behavior...
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)
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)
2117 -- test handling of outer GroupingFunc within subqueries
2119 select (select grouping(v1)) from (values ((select 1))) v(v1) group by cube(v1);
2121 -------------------------------
2123 Hash Key: (InitPlan 3).col1
2134 select (select grouping(v1)) from (values ((select 1))) v(v1) group by cube(v1);
2142 select (select grouping(v1)) from (values ((select 1))) v(v1) group by v1;
2155 select (select grouping(v1)) from (values ((select 1))) v(v1) group by v1;
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
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)
2174 ---------------------------------------------------------------------------------------------------------------------------------------------
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
2179 Output: GROUPING((SubPlan 1)), ((SubPlan 3)), CASE WHEN (GROUPING((SubPlan 2)) = 0) THEN ((SubPlan 3)) ELSE NULL::integer END, t1.v
2181 Hash Key: (SubPlan 3)
2182 -> Seq Scan on pg_temp.gstest5 t1
2183 Output: (SubPlan 3), t1.v, t1.id
2185 -> Bitmap Heap Scan on pg_temp.gstest5 t2
2187 Recheck Cond: (t2.id = t1.id)
2188 -> Bitmap Index Scan on gstest5_pkey
2189 Index Cond: (t2.id = t1.id)
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
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)
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)
2221 group by grouping sets(v, s)
2222 order by o nulls first;
2224 ---------------------------------------------------------------------------------------------------------------------------------------------
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
2229 Output: GROUPING((SubPlan 1)), ((SubPlan 3)), CASE WHEN (GROUPING((SubPlan 2)) = 0) THEN ((SubPlan 3)) ELSE NULL::integer END, t1.v
2231 Hash Key: (SubPlan 3)
2232 -> Seq Scan on pg_temp.gstest5 t1
2233 Output: (SubPlan 3), t1.v, t1.id
2235 -> Bitmap Heap Scan on pg_temp.gstest5 t2
2237 Recheck Cond: (t2.id = t1.id)
2238 -> Bitmap Index Scan on gstest5_pkey
2239 Index Cond: (t2.id = t1.id)
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)
2248 group by grouping sets(v, s)
2249 order by o nulls first;
2264 -- test handling of expressions that should match lower target items
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;
2268 -----------------------------------
2270 Hash Key: ((1 < 2) AND (2 < 3))
2272 Filter: (((1 < 2) AND (2 < 3)))
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;
2283 select not a from (values(true)) t(a) group by rollup(not a) having not not a;
2285 ------------------------------
2287 Hash Key: (NOT true)
2289 Filter: (NOT ((NOT true)))
2293 select not a from (values(true)) t(a) group by rollup(not a) having not not a;
2299 -- test handling of expressions nullable by grouping sets
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))
2306 ----------------------------------------------------------------
2309 Sort Key: "*VALUES*".column1, "*VALUES*".column2
2311 Hash Key: "*VALUES*".column1, "*VALUES*".column2
2312 Hash Key: "*VALUES*".column1
2313 -> Values Scan on "*VALUES*"
2314 Filter: (column1 = column2)
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))
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))
2335 ----------------------------------------------------------------------
2338 Sort Key: "*VALUES*".column1, (("*VALUES*".column2 + 1))
2340 Hash Key: "*VALUES*".column1, ("*VALUES*".column2 + 1)
2341 Hash Key: "*VALUES*".column1
2342 -> Values Scan on "*VALUES*"
2343 Filter: (column1 = (column2 + 1))
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))
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;
2364 ----------------------------------------------------------------
2366 Sort Key: "*VALUES*".column1, "*VALUES*".column2 NULLS FIRST
2368 Hash Key: "*VALUES*".column1, "*VALUES*".column2
2369 Hash Key: "*VALUES*".column1
2370 -> Values Scan on "*VALUES*"
2371 Filter: (column1 = column2)
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;
2387 select 1 as one group by rollup(one) order by one nulls first;
2389 -----------------------------
2391 Sort Key: (1) NULLS FIRST
2398 select 1 as one group by rollup(one) order by one nulls first;
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));
2410 ----------------------------------------------------------------------
2413 Sort Key: "*VALUES*".column1, "*VALUES*".column2 NULLS FIRST
2415 Hash Key: "*VALUES*".column1, "*VALUES*".column2
2416 Hash Key: "*VALUES*".column1
2417 -> Values Scan on "*VALUES*"
2418 Filter: (column1 = column2)
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));
2425 ---+---+------------