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 gstest_empty (a integer, b integer, v integer);
17 create function gstest_data(v integer, out a integer, out b integer)
21 return query select v, i from generate_series(1,3) i;
24 -- basic functionality
25 -- simple rollup with multiple plain aggregates, with and without ordering
26 -- (and with ordering differing from grouping)
27 select a, b, grouping(a,b), sum(v), count(*), max(v)
28 from gstest1 group by rollup (a,b);
29 a | b | grouping | sum | count | max
30 ---+---+----------+-----+-------+-----
31 1 | 1 | 0 | 21 | 2 | 11
32 1 | 2 | 0 | 25 | 2 | 13
33 1 | 3 | 0 | 14 | 1 | 14
35 2 | 3 | 0 | 15 | 1 | 15
37 3 | 3 | 0 | 16 | 1 | 16
38 3 | 4 | 0 | 17 | 1 | 17
40 4 | 1 | 0 | 37 | 2 | 19
45 select a, b, grouping(a,b), sum(v), count(*), max(v)
46 from gstest1 group by rollup (a,b) order by a,b;
47 a | b | grouping | sum | count | max
48 ---+---+----------+-----+-------+-----
49 1 | 1 | 0 | 21 | 2 | 11
50 1 | 2 | 0 | 25 | 2 | 13
51 1 | 3 | 0 | 14 | 1 | 14
53 2 | 3 | 0 | 15 | 1 | 15
55 3 | 3 | 0 | 16 | 1 | 16
56 3 | 4 | 0 | 17 | 1 | 17
58 4 | 1 | 0 | 37 | 2 | 19
63 select a, b, grouping(a,b), sum(v), count(*), max(v)
64 from gstest1 group by rollup (a,b) order by b desc, a;
65 a | b | grouping | sum | count | max
66 ---+---+----------+-----+-------+-----
72 3 | 4 | 0 | 17 | 1 | 17
73 1 | 3 | 0 | 14 | 1 | 14
74 2 | 3 | 0 | 15 | 1 | 15
75 3 | 3 | 0 | 16 | 1 | 16
76 1 | 2 | 0 | 25 | 2 | 13
77 1 | 1 | 0 | 21 | 2 | 11
78 4 | 1 | 0 | 37 | 2 | 19
81 select a, b, grouping(a,b), sum(v), count(*), max(v)
82 from gstest1 group by rollup (a,b) order by coalesce(a,0)+coalesce(b,0);
83 a | b | grouping | sum | count | max
84 ---+---+----------+-----+-------+-----
87 1 | 1 | 0 | 21 | 2 | 11
90 1 | 2 | 0 | 25 | 2 | 13
91 1 | 3 | 0 | 14 | 1 | 14
93 4 | 1 | 0 | 37 | 2 | 19
94 2 | 3 | 0 | 15 | 1 | 15
95 3 | 3 | 0 | 16 | 1 | 16
96 3 | 4 | 0 | 17 | 1 | 17
99 -- various types of ordered aggs
100 select a, b, grouping(a,b),
101 array_agg(v order by v),
102 string_agg(v::text, ':' order by v desc),
103 percentile_disc(0.5) within group (order by v),
104 rank(1,2,12) within group (order by a,b,v)
105 from gstest1 group by rollup (a,b) order by a,b;
106 a | b | grouping | array_agg | string_agg | percentile_disc | rank
107 ---+---+----------+---------------------------------+-------------------------------+-----------------+------
108 1 | 1 | 0 | {10,11} | 11:10 | 10 | 3
109 1 | 2 | 0 | {12,13} | 13:12 | 12 | 1
110 1 | 3 | 0 | {14} | 14 | 14 | 1
111 1 | | 1 | {10,11,12,13,14} | 14:13:12:11:10 | 12 | 3
112 2 | 3 | 0 | {15} | 15 | 15 | 1
113 2 | | 1 | {15} | 15 | 15 | 1
114 3 | 3 | 0 | {16} | 16 | 16 | 1
115 3 | 4 | 0 | {17} | 17 | 17 | 1
116 3 | | 1 | {16,17} | 17:16 | 16 | 1
117 4 | 1 | 0 | {18,19} | 19:18 | 18 | 1
118 4 | | 1 | {18,19} | 19:18 | 18 | 1
119 | | 3 | {10,11,12,13,14,15,16,17,18,19} | 19:18:17:16:15:14:13:12:11:10 | 14 | 3
122 -- test usage of grouped columns in direct args of aggs
123 select grouping(a), a, array_agg(b),
124 rank(a) within group (order by b nulls first),
125 rank(a) within group (order by b nulls last)
126 from (values (1,1),(1,4),(1,5),(3,1),(3,2)) v(a,b)
127 group by rollup (a) order by a;
128 grouping | a | array_agg | rank | rank
129 ----------+---+-------------+------+------
130 0 | 1 | {1,4,5} | 1 | 1
131 0 | 3 | {1,2} | 3 | 3
132 1 | | {1,4,5,1,2} | 1 | 6
135 -- nesting with window functions
136 select a, b, sum(c), sum(sum(c)) over (order by a,b) as rsum
137 from gstest2 group by rollup (a,b) order by rsum, a, b;
148 -- empty input: first is 0 rows, second 1, third 3 etc.
149 select a, b, sum(v), count(*) from gstest_empty group by grouping sets ((a,b),a);
151 ---+---+-----+-------
154 select a, b, sum(v), count(*) from gstest_empty group by grouping sets ((a,b),());
156 ---+---+-----+-------
160 select a, b, sum(v), count(*) from gstest_empty group by grouping sets ((a,b),(),(),());
162 ---+---+-----+-------
168 select sum(v), count(*) from gstest_empty group by grouping sets ((),(),());
176 -- empty input with joins tests some important code paths
177 select t1.a, t2.b, sum(t1.v), count(*) from gstest_empty t1, gstest_empty t2
178 group by grouping sets ((t1.a,t2.b),());
180 ---+---+-----+-------
184 -- simple joins, var resolution, GROUPING on join vars
185 select t1.a, t2.b, grouping(t1.a, t2.b), sum(t1.v), max(t2.a)
186 from gstest1 t1, gstest2 t2
187 group by grouping sets ((t1.a, t2.b), ());
188 a | b | grouping | sum | max
189 ---+---+----------+------+-----
201 select t1.a, t2.b, grouping(t1.a, t2.b), sum(t1.v), max(t2.a)
202 from gstest1 t1 join gstest2 t2 on (t1.a=t2.a)
203 group by grouping sets ((t1.a, t2.b), ());
204 a | b | grouping | sum | max
205 ---+---+----------+-----+-----
212 select a, b, grouping(a, b), sum(t1.v), max(t2.c)
213 from gstest1 t1 join gstest2 t2 using (a,b)
214 group by grouping sets ((a, b), ());
215 a | b | grouping | sum | max
216 ---+---+----------+-----+-----
222 -- check that functionally dependent cols are not nulled
223 select a, d, grouping(a,b,c)
225 group by grouping sets ((a,b), (a,c));
234 -- simple rescan tests
235 select a, b, sum(v.x)
236 from (values (1),(2)) v(x), gstest_data(v.x)
237 group by rollup (a,b);
252 from (values (1),(2)) v(x),
253 lateral (select a, b, sum(v.x) from gstest_data(v.x) group by rollup (a,b)) s;
254 ERROR: aggregate functions are not allowed in FROM clause of their own query level
255 LINE 3: lateral (select a, b, sum(v.x) from gstest_data(v.x) ...
257 -- min max optimisation should still work with GROUP BY ()
259 select min(unique1) from tenk1 GROUP BY ();
261 ------------------------------------------------------------
263 InitPlan 1 (returns $0)
265 -> Index Only Scan using tenk1_unique1 on tenk1
266 Index Cond: (unique1 IS NOT NULL)
269 -- Views with GROUPING SET queries
270 CREATE VIEW gstest_view AS select a, b, grouping(a,b), sum(c), count(*), max(c)
271 from gstest2 group by rollup ((a,b,c),(c,d));
272 NOTICE: view "gstest_view" will be a temporary view
273 select pg_get_viewdef('gstest_view'::regclass, true);
275 -------------------------------------------------------------------------------
278 GROUPING(gstest2.a, gstest2.b) AS "grouping", +
279 sum(gstest2.c) AS sum, +
281 max(gstest2.c) AS max +
283 GROUP BY ROLLUP((gstest2.a, gstest2.b, gstest2.c), (gstest2.c, gstest2.d));
286 -- Nested queries with 3 or more levels of nesting
287 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);
295 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);
303 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);
311 -- Combinations of operations
312 select a, b, c, d from gstest2 group by rollup(a,b),grouping sets(c,d);
335 select a, b from (values (1,2),(2,3)) v(a,b) group by a,b, grouping sets(a);
342 -- Tests for chained aggregates
343 select a, b, grouping(a,b), sum(v), count(*), max(v)
344 from gstest1 group by grouping sets ((a,b),(a+1,b+1),(a+2,b+2));
345 a | b | grouping | sum | count | max
346 ---+---+----------+-----+-------+-----
347 1 | 1 | 0 | 21 | 2 | 11
348 1 | 2 | 0 | 25 | 2 | 13
349 1 | 3 | 0 | 14 | 1 | 14
350 2 | 3 | 0 | 15 | 1 | 15
351 3 | 3 | 0 | 16 | 1 | 16
352 3 | 4 | 0 | 17 | 1 | 17
353 4 | 1 | 0 | 37 | 2 | 19
370 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));
378 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);
387 select a, b, sum(c), sum(sum(c)) over (order by a,b) as rsum
388 from gstest2 group by cube (a,b) order by rsum, a, b;
401 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);
418 select a, b, sum(v.x)
419 from (values (1),(2)) v(x), gstest_data(v.x)
420 group by cube (a,b) order by a,b;
437 -- Agg level check. This query should error out.
438 select (select grouping(a,b) from gstest2) from gstest2 group by a,b;
439 ERROR: arguments to GROUPING must be grouping expressions of the associated query level
440 LINE 1: select (select grouping(a,b) from gstest2) from gstest2 grou...
443 select a, b, sum(c), count(*) from gstest2 group by grouping sets (rollup(a,b),a);
445 ---+---+-----+-------
457 select ten, sum(distinct four) from onek a
458 group by grouping sets((ten,four),(ten))
459 having exists (select 1 from onek b where sum(distinct a.four) = b.four);
490 select ten, sum(distinct four) filter (where four::text ~ '123') from onek a
491 group by rollup(ten);
508 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;
509 a | a | four | ten | count
510 ---+---+------+-----+-------
583 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);
585 ------------------------------------------------------------------------------------------------------------------------------------------------------
586 {"(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)"}
587 {"(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)"}