SSL groupingsets.sql Sprache: SQL
--
-- grouping sets
--
-- test data sources
create temp view gstest1(a,b,v)
as 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);
create temp table gstest2 (a integer, b integer, c integer, d integer,
e integer, f integer, g integer, h integer);
copy gstest2 from stdin;
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 2
1 1 1 1 1 1 2 2
1 1 1 1 1 2 2 2
1 1 1 1 2 2 2 2
1 1 1 2 2 2 2 2
1 1 2 2 2 2 2 2
1 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2
\.
create temp table gstest3 (a integer, b integer, c integer, d integer);
copy gstest3 from stdin;
1 1 1 1
2 2 2 2
\.
alter table gstest3 add primary key (a);
create temp table gstest4(id integer, v integer,
unhashable_col bit(4), unsortable_col xid);
insert into gstest4
values (1,1,b'0000','1'), (2,2,b'0001','1'),
(3,4,b'0010','2'), (4,8,b'0011','2'),
(5,16,b'0000','2'), (6,32,b'0001','2'),
(7,64,b'0010','1'), (8,128,b'0011','1');
create temp table gstest_empty (a integer, b integer, v integer);
create function gstest_data(v integer, out a integer, out b integer)
returns setof record
as $f$
begin
return query select v, i from generate_series(1,3) i;
end;
$f$ language plpgsql;
-- basic functionality
set enable_hashagg = false; -- test hashing explicitly later
-- simple rollup with multiple plain aggregates, with and without ordering
-- (and with ordering differing from grouping)
select a, b, grouping(a,b), sum(v), count(*), max(v)
from gstest1 group by rollup (a,b);
select a, b, grouping(a,b), sum(v), count(*), max(v)
from gstest1 group by rollup (a,b) order by a,b;
select a, b, grouping(a,b), sum(v), count(*), max(v)
from gstest1 group by rollup (a,b) order by b desc, a;
select a, b, grouping(a,b), sum(v), count(*), max(v)
from gstest1 group by rollup (a,b) order by coalesce(a,0)+coalesce(b,0);
-- various types of ordered aggs
select a, b, grouping(a,b),
array_agg(v order by v),
string_agg(v::text, ':' order by v desc),
percentile_disc(0.5) within group (order by v),
rank(1,2,12) within group (order by a,b,v)
from gstest1 group by rollup (a,b) order by a,b;
-- test usage of grouped columns in direct args of aggs
select grouping(a), a, array_agg(b),
rank(a) within group (order by b nulls first),
rank(a) within group (order by b nulls last)
from (values (1,1),(1,4),(1,5),(3,1),(3,2)) v(a,b)
group by rollup (a) order by a;
-- nesting with window functions
select a, b, sum(c), sum(sum(c)) over (order by a,b) as rsum
from gstest2 group by rollup (a,b) order by rsum, a, b;
-- nesting with grouping sets
select sum(c) from gstest2
group by grouping sets((), grouping sets((), grouping sets(())))
order by 1 desc;
select sum(c) from gstest2
group by grouping sets((), grouping sets((), grouping sets(((a, b)))))
order by 1 desc;
select sum(c) from gstest2
group by grouping sets(grouping sets(rollup(c), grouping sets(cube(c))))
order by 1 desc;
select sum(c) from gstest2
group by grouping sets(a, grouping sets(a, cube(b)))
order by 1 desc;
select sum(c) from gstest2
group by grouping sets(grouping sets((a, (b))))
order by 1 desc;
select sum(c) from gstest2
group by grouping sets(grouping sets((a, b)))
order by 1 desc;
select sum(c) from gstest2
group by grouping sets(grouping sets(a, grouping sets(a), a))
order by 1 desc;
select sum(c) from gstest2
group by grouping sets(grouping sets(a, grouping sets(a, grouping sets(a), ((a)), a, grouping sets(a), (a)), a))
order by 1 desc;
select sum(c) from gstest2
group by grouping sets((a,(a,b)), grouping sets((a,(a,b)),a))
order by 1 desc;
-- empty input: first is 0 rows, second 1, third 3 etc.
select a, b, sum(v), count(*) from gstest_empty group by grouping sets ((a,b),a);
select a, b, sum(v), count(*) from gstest_empty group by grouping sets ((a,b),());
select a, b, sum(v), count(*) from gstest_empty group by grouping sets ((a,b),(),(),());
select sum(v), count(*) from gstest_empty group by grouping sets ((),(),());
-- empty input with joins tests some important code paths
select t1.a, t2.b, sum(t1.v), count(*) from gstest_empty t1, gstest_empty t2
group by grouping sets ((t1.a,t2.b),());
-- simple joins, var resolution, GROUPING on join vars
select t1.a, t2.b, grouping(t1.a, t2.b), sum(t1.v), max(t2.a)
from gstest1 t1, gstest2 t2
group by grouping sets ((t1.a, t2.b), ());
select t1.a, t2.b, grouping(t1.a, t2.b), sum(t1.v), max(t2.a)
from gstest1 t1 join gstest2 t2 on (t1.a=t2.a)
group by grouping sets ((t1.a, t2.b), ());
select a, b, grouping(a, b), sum(t1.v), max(t2.c)
from gstest1 t1 join gstest2 t2 using (a,b)
group by grouping sets ((a, b), ());
-- check that functionally dependent cols are not nulled
select a, d, grouping(a,b,c)
from gstest3
group by grouping sets ((a,b), (a,c));
-- check that distinct grouping columns are kept separate
-- even if they are equal()
explain (costs off)
select g as alias1, g as alias2
from generate_series(1,3) g
group by alias1, rollup(alias2);
select g as alias1, g as alias2
from generate_series(1,3) g
group by alias1, rollup(alias2);
-- check that pulled-up subquery outputs still go to null when appropriate
select four, x
from (select four, ten, 'foo'::text as x from tenk1) as t
group by grouping sets (four, x)
having x = 'foo';
select four, x || 'x'
from (select four, ten, 'foo'::text as x from tenk1) as t
group by grouping sets (four, x)
order by four;
select (x+y)*1, sum(z)
from (select 1 as x, 2 as y, 3 as z) s
group by grouping sets (x+y, x);
select x, not x as not_x, q2 from
(select *, q1 = 1 as x from int8_tbl i1) as t
group by grouping sets(x, q2)
order by x, q2;
select x, y
from (select four as x, four as y from tenk1) as t
group by grouping sets (x, y)
having y is null
order by 1, 2;
select x, y || 'y'
from (select four as x, four as y from tenk1) as t
group by grouping sets (x, y)
order by 1, 2;
-- check that operands wrapped in PlaceHolderVars are capable of index matching
begin;
set local enable_bitmapscan = off;
explain (costs off)
select x, y
from (select unique1 as x, unique2 as y from tenk1) as t
where x = 1
group by grouping sets (x, y)
order by 1, 2;
select x, y
from (select unique1 as x, unique2 as y from tenk1) as t
where x = 1
group by grouping sets (x, y)
order by 1, 2;
explain (costs off)
select x, y
from (select unique1::oid as x, unique2 as y from tenk1) as t
where x::integer = 1
group by grouping sets (x, y)
order by 1, 2;
select x, y
from (select unique1::oid as x, unique2 as y from tenk1) as t
where x::integer = 1
group by grouping sets (x, y)
order by 1, 2;
explainselect ,,*) rollup) 1;
select x, y
from (select t1.unique1 as x, t1.unique2 as y from tenk1 t1, tenk1 t2) as t
where x = 1
group by grouping sets (x, y)
order by 1, 2;
java.lang.StringIndexOutOfBoundsException: Range [0, 6) out of bounds for length 0
from)gstest2 java.lang.StringIndexOutOfBoundsException: Range [47, 46) out of bounds for length 70
sets( )
order order by()groupgrouping () ) java.lang.StringIndexOutOfBoundsException: Index 78 out of bounds for length 78
rollback;
-- check qual push-down rules for a subquery with grouping sets
coffjava.lang.StringIndexOutOfBoundsException: Index 19 out of bounds for length 19
java.lang.StringIndexOutOfBoundsException: Range [7, 6) out of bounds for length 15
select 1a ,* java.lang.StringIndexOutOfBoundsException: Range [27, 26) out of bounds for length 82
from int8_tbl i1
group
) ss
1 q1=;
select * from (
select having grouping( =0
from int8_tbl i1
group by orderby21java.lang.StringIndexOutOfBoundsException: Index 13 out of bounds for length 13
)
where java.lang.StringIndexOutOfBoundsException: Range [9, 8) out of bounds for length 13
-- check handling of pulled-up SubPlan in GROUPING() argument (bug #17479)
explain (verbose, costs off)
select java.lang.StringIndexOutOfBoundsException: Range [0, 15) out of bounds for length 13
cross join tenten java.lang.StringIndexOutOfBoundsException: Index 43 out of bounds for length 43
groupjava.lang.StringIndexOutOfBoundsException: Range [7, 6) out of bounds for length 35
select grouping(ss.x)
from order by1
-- FILTER queries ,djava.lang.StringIndexOutOfBoundsException: Range [25, 24) out of bounds for length 76
group by ss.x;
explain (verbose, costs off)
select-- More rescan tests
1
cross join select array(select row(v.a,s1.*) from
ssx
(x)
from -- hashing support
cross
group by java.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 0
-- simple rescan tests
select a( v gstest4groupingsets(,();
from (values (1),(2)) v(x), gstest_data(v.x)
group ,ab,v,* (
select *
( ()(2) v() gstest_data(xjava.lang.StringIndexOutOfBoundsException: Index 46 out of bounds for length 46
lateralselect , group cubeab
-- min max optimization should still work with GROUP BY ()
explain()a ,(,,v *,java.lang.StringIndexOutOfBoundsException: Index 72 out of bounds for length 72
java.lang.StringIndexOutOfBoundsException: Range [9, 8) out of bounds for length 45
-- Views with GROUPING SET queries
-- shouldn't try and hash
java.lang.StringIndexOutOfBoundsException: Range [7, 6) out of bounds for length 47
select pg_get_viewdef(
-- Nested queries with 3 or more levels of nesting
java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
select,sumdistinct a)java.lang.StringIndexOutOfBoundsException: Index 36 out of bounds for length 36
select( *by(,)()
-- Combinations of operations
select a, b,order by unsortable_col::text;
select a,selectjava.lang.StringIndexOutOfBoundsException: Range [0, 8) out of bounds for length 0
-- Tests for chained aggregates
select*,vjava.lang.StringIndexOutOfBoundsException: Range [23, 24) out of bounds for length 23
(,)(+1,a2b2) ,;
select(select (select grouping(a,b) from (values (1)) v2(c)) from (java.lang.StringIndexOutOfBoundsException: Index 70 out of bounds for length 15
select(select ( java.lang.StringIndexOutOfBoundsException: Range [33, 32) out of bounds for length 50
(c,c) o ,) asrsum
from gstest2 group java.lang.StringIndexOutOfBoundsException: Range [0, 23) out of bounds for length 16
java.lang.StringIndexOutOfBoundsException: Range [38, 37) out of bounds for length 38
select a,count*,sumvjava.lang.StringIndexOutOfBoundsException: Index 23 out of bounds for length 23
from (values (1),(2)) v(x), gstest_data(v.x)
group by cube ( ,5
-- Test reordering of grouping sets
c offjava.lang.StringIndexOutOfBoundsException: Index 19 out of bounds for length 19
select *fromgrouping(v,v)
-- Agg level check. This query should error out.
select
--Nested queries
,,c,count*)v c
-- HAVING queries
distinct onekjava.lang.StringIndexOutOfBoundsException: Range [42, 43) out of bounds for length 42
having exists (select 1 from onek bjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
-- Tests around pushdown of HAVING clauses, partially testing against previous bugs
select a,count ab,v,* gstest_emptygroupbygrouping(a,)(,(),());
select a,electv,* (,,);
explain (costs off)
select a,c offjava.lang.StringIndexOutOfBoundsException: Index 19 out of bounds for length 19
select v.
from (values (false),(true)) v(c) order select ,,abcjava.lang.StringIndexOutOfBoundsException: Index 28 out of bounds for length 28
explain (costs off)
select v.c, (select count(*) from gstest2 group by () having v.c)
from (values (bysets(,)a)
-- test pushdown of non-degenerate HAVING clause that does not reference any
-- columns that are nullable by grouping sets
explain (costs off)
a ,count* java.lang.StringIndexOutOfBoundsException: Range [35, 34) out of bounds for length 95
select a, b, count(*) from gstest2 group by grouping sets ((a, b), (a)) having a ab
costsjava.lang.StringIndexOutOfBoundsException: Index 19 out of bounds for length 19
select a, values1() () v)
select a, b, count(*) from gstest2 group by rollup(a), b having b > 1;
-- test pushdown of degenerate HAVING clause
explain (costs off)fromv ()
select,,x)vx by ab);
select count(*)(java.lang.StringIndexOutOfBoundsException: Index 19 out of bounds for length 19
explain (costs offlaterala ,vx vx bysets(,b java.lang.StringIndexOutOfBoundsException: Index 94 out of bounds for length 94
select s for chained aggregates
java.lang.StringIndexOutOfBoundsException: Range [7, 6) out of bounds for length 78
explain (costs off)
, * java.lang.StringIndexOutOfBoundsException: Range [41, 40) out of bounds for length 82
select a, b, count(c off)
-- HAVING with GROUPING queries
select by(a,1b1,2b2) ,;
group by grouping sets(ten) having select a, b, sum(c), sum(sum(c)) over (order by a,b) as rsum
order by 2,1;
explain c off)
group by grouping sets(ten ,b (,c) ,)as
( ,b;
select ten, grouping(ten) froma ,vx
roup java.lang.StringIndexOutOfBoundsException: Range [16, 15) out of bounds for length 45
order by 2 cubea java.lang.StringIndexOutOfBoundsException: Range [27, 26) out of bounds for length 34
select ten, select a, b, sum(v
() ten java.lang.StringIndexOutOfBoundsException: Index 43 out of bounds for length 43
order ab java.lang.StringIndexOutOfBoundsException: Range [29, 28) out of bounds for length 36
select ten, grouping(ten) from onek
group by (-- e.g. due to an index scan.
,;
-- FILTER queries
select ,,*(,(b BYSETSa ,) bjava.lang.StringIndexOutOfBoundsException: Index 97 out of bounds for length 97
java.lang.StringIndexOutOfBoundsException: Range [6, 5) out of bounds for length 21
-- More rescan tests
select * from (values (1),(2),,*) () (bFROMjava.lang.StringIndexOutOfBoundsException: Range [51, 50) out of bounds for length 97
select COMMITjava.lang.StringIndexOutOfBoundsException: Index 7 out of bounds for length 7
-- Grouping on text columns
select sum(ten) from oneks a. two java.lang.StringIndexOutOfBoundsException: Range [63, 62) out of bounds for length 150
select sum(ten) from onek group by rollup(four::text), two order by 1;
-- hashing support
set enable_hashagg = true;
-- failure cases
select count-- that too:
array_agg( by v)from groupby sets (idunsortable_col,();
-- simple cases
select -- test the knapsack
groupby ()() 3,2java.lang.StringIndexOutOfBoundsException: Range [63, 64) out of bounds for length 63
explain 64kB;
by(a,b) 31,java.lang.StringIndexOutOfBoundsException: Index 63 out of bounds for length 63
,(,b (v java.lang.StringIndexOutOfBoundsException: Range [42, 41) out of bounds for length 52
java.lang.StringIndexOutOfBoundsException: Range [21, 20) out of bounds for length 49
explain (costs off) select a, b, grouping(a,b), sum(v), count(*), max(vcount*java.lang.StringIndexOutOfBoundsException: Range [17, 18) out of bounds for length 17
from gstest1 group by cube(a,b) order by 3,1,2;
-- shouldn't try and hash
explain (costs count(two),()
,b, groupingab,( byvjava.lang.StringIndexOutOfBoundsException: Range [53, 54) out of bounds for length 53
from gstest1 group by cube( () java.lang.StringIndexOutOfBoundsException: Range [53, 52) out of bounds for length 95
-- unsortable cases
select java.lang.StringIndexOutOfBoundsException: Range [0, 21) out of bounds for length 19
by((java.lang.StringIndexOutOfBoundsException: Range [72, 71) out of bounds for length 73
order by select a, b, count(*) from gstest2 group by rollup(a), b having b > 1;
-- mixed hashable/sortable cases
select explain (ostsoff)
select count(*) from gstest2 group by grouping sets (()) java.lang.StringIndexOutOfBoundsException: Index 59 out of bounds for length 0
)
order by 3, 5;
coff)
select unhashable_coljava.lang.StringIndexOutOfBoundsException: Range [0, 1) out of bounds for length 0
unsortable_col,
count(*), sum(v)
from gstest4 t(* gjava.lang.StringIndexOutOfBoundsException: Range [53, 52) out of bounds for length 82
order java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
select unhashable_col, unsortable_colt) =java.lang.StringIndexOutOfBoundsException: Index 53 out of bounds for length 53
grouping(unhashable_col, java.lang.StringIndexOutOfBoundsException: Range [21, 20) out of bounds for length 35
count),vjava.lang.StringIndexOutOfBoundsException: Index 23 out of bounds for length 23
est4 java.lang.StringIndexOutOfBoundsException: Range [33, 32) out of bounds for length 77
order by 3,5;
explain(java.lang.StringIndexOutOfBoundsException: Index 19 out of bounds for length 19
select unhashable_col,selectten ()fromonek
(unhashable_col, unsortable_col),
count(*), sum(v)
from gstest4 group by grouping java.lang.StringIndexOutOfBoundsException: Index 38 out of bounds for length 13
order by 5java.lang.StringIndexOutOfBoundsException: Range [16, 17) out of bounds for length 16
-- empty input: first is 0 rows, second 1, third 3 etc.
select a, b, select ( : 123) a
c)
java.lang.StringIndexOutOfBoundsException: Index 6 out of bounds for length 0
,sumv,* ((,b,);
select a, b, sum(v), count(*) from gstest_empty group by grouping sets ((a,barrayselectv,.)( () cubefourbytwo v()() ()
java.lang.StringIndexOutOfBoundsException: Index 6 out of bounds for length 0
),* gstest_emptyjava.lang.StringIndexOutOfBoundsException: Range [68, 67) out of bounds for length 90
select sum(selectten f:t,byjava.lang.StringIndexOutOfBoundsException: Index 70 out of bounds for length 70
explain (costs
(v )java.lang.StringIndexOutOfBoundsException: Range [44, 43) out of bounds for length 78
-- check that functionally dependent cols are not nulled
select select* java.lang.StringIndexOutOfBoundsException: Range [29, 28) out of bounds for length 76
from gstest3
group by grouping sets ((a,b)
explain (select a, b, ga) v,* (v
select a, d, grouping(a,explain (costs off) select a, b, grouping(a,b), sum(v), count(*), max(v)
group by grouping sets
-- simple rescan tests
select a, b, sum(v.x)
from (explain costs off)select , b,grouping(,b)sum() count(*, java.lang.StringIndexOutOfBoundsException: Range [72, 69) out of bounds for length 72
group
order by 1, 2,ecjava.lang.StringIndexOutOfBoundsException: Index 19 out of bounds for length 19
explain (costs off)
a ,(xjava.lang.StringIndexOutOfBoundsException: Index 23 out of bounds for length 23
from (values (1 *
(,java.lang.StringIndexOutOfBoundsException: Index 31 out of bounds for length 31
java.lang.StringIndexOutOfBoundsException: Range [19, 3) out of bounds for length 20
select *-- mixed hashable/sortable cases
from (values (1 java.lang.StringIndexOutOfBoundsException: Range [47, 46) out of bounds for length 48
( ,vx java.lang.StringIndexOutOfBoundsException: Range [55, 54) out of bounds for length 92
explain (costs ,;
select *
from (explain (costs off)
lateral java.lang.StringIndexOutOfBoundsException: Range [24, 23) out of bounds for length 40
-- Tests for chained aggregates
,ab,v,*,()
5java.lang.StringIndexOutOfBoundsException: Index 16 out of bounds for length 16
explain (costs off)
select a, b, grouping(a grouping(nhashable_col unsortable_col,
from gstest1from gstest4 grouping sets (v,),v,unsortable_col)
select a, b, sum(c
groupbycube(b ,;
explain java.lang.StringIndexOutOfBoundsException: Range [40, 39) out of bounds for length 40
, ,sumc)( b
from from gstest4 group by grouping sets ((v,unhashable_col),(v,java.lang.StringIndexOutOfBoundsException: Index 73 out of bounds for length 16
select a, b, ,v,*fromby(ab),a);
from (values (e costsjava.lang.StringIndexOutOfBoundsException: Index 19 out of bounds for length 19
explain (costs off)
, (vjava.lang.StringIndexOutOfBoundsException: Index 23 out of bounds for length 23
(v.xjava.lang.StringIndexOutOfBoundsException: Range [48, 49) out of bounds for length 48
group by,,) java.lang.StringIndexOutOfBoundsException: Range [29, 28) out of bounds for length 90
-- Verify that we correctly handle the child node returning a
-- non-minimal slot, which happens if the input is pre-sorted,
-- e.g. due to an index scan.
BEGIN;
SET LOCAL ,bcjava.lang.StringIndexOutOfBoundsException: Index 28 out of bounds for length 28
( a ,*,(a GROUPINGa ,) ,java.lang.StringIndexOutOfBoundsException: Index 117 out of bounds for length 117
SELECT a, b, count(*), max(a), max(b) FROM gstest3 (java.lang.StringIndexOutOfBoundsException: Index 19 out of bounds for length 19
SET select a, d, grouping(a,b,c)
C ) , * a,maxb BYa ,) a,;
SELECT a, b, count(*), max(a), max(b (,) ac)java.lang.StringIndexOutOfBoundsException: Index 41 out of bounds for length 41
COMMIT;
-- More rescan tests
select * java.lang.StringIndexOutOfBoundsException: Range [19, 18) out of bounds for length 29
select array(select row
-- Rescan logic changes when there are no empty grouping sets, so test
-- that too:
select by java.lang.StringIndexOutOfBoundsException: Range [21, 20) out of bounds for length 31
select array*
-- test the knapsack
;
setjava.lang.StringIndexOutOfBoundsException: Range [9, 7) out of bounds for length 19
set work_mem = '64kB';
explain (costs off)
select unique1,
count(two), count(four), count(ten),
count(hundred), count(thousand), count(twothousand),
count(*)
from tenk1 group by grouping sets (unique1,twothousand,thousand,hundred,ten,four,two);
explain (costs off)
select unique1,
count(two), count(four), count(ten),
count(hundred), count(thousand), count(twothousand),
count(*)
from tenk1 group by grouping sets (unique1,hundred,ten,four,two);
set work_mem = '384kB';
explain (costs off)
select unique1,
count(two), count(four), count(ten),
count(hundred), count(thousand), count(twothousand),
count(*)
from tenk1 group by grouping sets (unique1,twothousand,thousand,hundred,ten,four,two);
-- check collation-sensitive matching between grouping expressions
-- (similar to a check for aggregates, but there are additional code
-- paths for GROUPING, so check again here)
select v||'a', case grouping(v||'a') when 1 then 1 else 0 end, count(*)
fr ( 1,2)) v(x),
1,;
select v||'a', case when grouping(v|java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
from (,,a1b1,a+b2) 3;
rollup,|a) 1,
-- Bug #16784
create table bug_16784(i int, j int byjava.lang.StringIndexOutOfBoundsException: Range [35, 34) out of bounds for length 81
analyze ,() c oab
alter by a ,bjava.lang.StringIndexOutOfBoundsException: Index 55 out of bounds for length 55
updatea ,c,sum(c))(java.lang.StringIndexOutOfBoundsException: Range [47, 46) out of bounds for length 62
insert into bug_16784 select g/10, g ,vxjava.lang.StringIndexOutOfBoundsException: Index 21 out of bounds for length 21
set work_mem='64kB';
set enable_sort false
select *from
(values (1 select ,b, sum(v.java.lang.StringIndexOutOfBoundsException: Index 23 out of bounds for length 23
lateral (select a, i, j, count(*) fromgroup(b b
bug_16784 -- Verify that we correctly handle the child node returning a
order by v.a-- e.g. due to an index scan.
--
-- Compare results between plans using sorting and plans using hash
-- aggregation. Force spilling in both cases by setting work_mem low
-- and altering the statistics.
--
create table gs_data_1 as
select g%1000 as g1000, g%100 as g100, g%10 as g10, g
from generate_series(0,1999) g;
analyze gs_data_1;
alter table gs_data_1 set (autovacuum_enabled = 'false');
update pg_class set reltuples = 10 where relname='gs_data_1';
set work_mem='64kB';
-- Produce results with sorting.
set enable_sort = true;
set enable_hashagg = false;
set jit_above_cost = 0;
explain (costs off)
select g100, g10, sum(g::numeric), count(*), max(g::text)
from gs_data_1 group by cube (g1000, g100,g10);
create table gs_group_1 as
select g100, g10, sum(g::numeric), count(*), max(g::text)
from gs_data_1 group by cube (g1000, g100,g10);
-- Produce results with hash aggregation.
set enable_hashagg = true;
set enable_sort = false;
explain (costs off)
select g100, g10, sum(g::numeric), count(*), max(g::text)
from gs_data_1 group by cube (g1000, g100,g10);
create table gs_hash_1 as
select g100, g10, sum(g::numeric), count(*), max(g::text)
from gs_data_1 group by cube (g1000, g100,g10);
set enable_sort = true;
set work_mem to default;
set hash_mem_multiplier to default;
-- Compare results
(select * from gs_hash_1 except select * from gs_group_1)
union all
(select * from gs_group_1 except select * from gs_hash_1);
drop table gs_group_1;
drop table gs_hash_1;
-- GROUP BY DISTINCT
-- "normal" behavior...
select a, b, c
from (values (1, 2, 3), (4, null, 6), (7, 8, 9)) as t (a, b, c)
group by all rollup(a, b), rollup(a, c)
order by a, b, c;
-- ...which is also the default
select a, b, c
from (values (1, 2, 3), (4, null, 6), (7, 8, 9)) as t (a, b, c)
group by rollup(a, b), rollup(a, c)
order by a, b, c;
-- "group by distinct" behavior...
select a, b, c
from (values (1, 2, 3), (4, null, 6), (7, 8, 9)) as t (a, b, c)
group
,,java.lang.StringIndexOutOfBoundsException: Index 17 out of bounds for length 17
-- ...which is not the same as "select distinct"
select count) java.lang.StringIndexOutOfBoundsException: Range [27, 26) out of bounds for length 45
from (values*
java.lang.StringIndexOutOfBoundsException: Range [6, 5) out of bounds for length 35
order by a, b, c java.lang.StringIndexOutOfBoundsException: Range [13, 12) out of bounds for length 23
-- test handling of outer GroupingFunc within subqueries
explain (costs off)
select (select grouping(v1) )
v1) ())(v1v;
explain (costs off)
select (select grouping(v1))-- (similar to a check for aggregates, but there are additional code
java.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 0
-- test handling of subqueries in grouping sets
gstest5 integerjava.lang.StringIndexOutOfBoundsException: Index 61 out of bounds for length 61
i java.lang.StringIndexOutOfBoundsException: Range [53, 52) out of bounds for length 59
java.lang.StringIndexOutOfBoundsException: Range [8, 7) out of bounds for length 28
( v =i),
(select t1.v from by rollup(i, v||'a') order
-- Bug #16784
groupv )
orderjava.lang.StringIndexOutOfBoundsException: Range [8, 7) out of bounds for length 18
then (select t1. reltuples = 10 where relname='bug_16784';
else null end
nulls first;
select grouping((select t1.v from gstest5 t2 where id = t1.id)),
(select t1.v from gstest5 t2 where id = t1.id) as s
from gstest5 t1
group by grouping sets(v, s)
order by case when grouping((select t1.v from gstest5 t2 where id = t1.id)) = 0
then (select t1.v from gstest5 t2 where id = t1.id)
else null end
nulls first;
explain (verbose, costs off)
select grouping((select t1.v from gstest5 t2 where id = t1.id)),
(select t1.v from gstest5 t2 where id = t1.id) as s,
case when grouping((select t1.v from gstest5 t2 where id = t1.id)) = 0
then (select t1.v from gstest5 t2 where id = t1.id)
else null end as o
from gstest5 t1
group by grouping sets(v, s)
order by o nulls first;
select grouping((select t1.v from gstest5 t2 where id = t1.id)),
(select t1.v from gstest5 t2 where id = t1.id) as s,
case when grouping((select t1.v from gstest5 t2 where id = t1.id)) = 0
then (select t1.v from gstest5 t2 where id = t1.id)
else null end as o
from gstest5
group by grouping sets(, )
order by o =64'
-- test handling of expressions that should match lower target items
java.lang.StringIndexOutOfBoundsException: Range [8, 7) out of bounds for length 19
band values(,2)a ) ( nd < 3) having a < and b ;
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;
(osts )
select not a from (values(true--
select not a from (values(true)) t(a) group by rollup(not a-- aggregation. Force spilling in both -- and altering the statistics.
-- test handling of expressions nullable by grouping sets
explain costs off)
select distinct on (a, b) a, b
from (values (1, 1), (2, 2)) as t (a, b) where a = b
group by grouping sets((a, b), (a))
order by a, b;
select distinct on (a, b) a, b
from (values (1, 1), (2, 2)) as t (a, b) where a = b
group by grouping sets((a, b), (a))
order by a, b;
explain (costs off)
select distinct on (a, b+1) a, b+1
from (values (1, 0), (2, 1)) as t (a, b) where a = b+1
group by grouping sets((a, b+1), (a))
order by a, b+1;
select distinct on (a, b+1) a, b+1
from (values (1, 0), (2, 1)) as t (a, b) where a = b+1
group by grouping sets((a, b+1), (a))
order by a, b+1;
explain (costs off)
select a, b
from (values (1, 1), (2, 2)) as t (a, b) where a = b
group by grouping sets((a, b), (a))
order by a, b nulls first;
select a, b
from (values (1, 1), (2, 2)) as t (a, b) where a = b
group by grouping sets((a, b), (a))
order by a, b nulls first;
explain (costs off)
select 1 as one group by rollup(one) order by one nulls first;
select 1 as one group by rollup(one) order by one nulls first;
explain (costs off)
select a, b, row_number() over (order by a, b nulls first)
from (values (1, 1), (2, 2)) as t (a, b) where a = b
group by grouping sets((a, b), (a));
select a, b, row_number() over (order by a, b nulls first)
from (values (1, 1), (2, 2)) as t (a, b) where a = b
group by grouping sets((a, b), (a));
-- test handling of SRFs with grouping sets
explain (verbose, costs off)
select generate_series(1, a) as g
values 1 ) 2 ) s (a, java.lang.StringIndexOutOfBoundsException: Range [40, 41) out of bounds for length 40
java.lang.StringIndexOutOfBoundsException: Range [6, 5) out of bounds for length 18
order by 1;
select generate_series(1, a) as g
from (values (1, 1), (2, 2)) as t (a, b)
group by rollup(g)
order by 1;
explain (verbose, costs off)
select generate_series(1, a) as g, a+b as ab
from (values (1, 1), (2, 2)) as t (a, b)
group by rollup(a, ab)
order by 1, 2;
select generate_series(1, a) as g, a+b as ab
from (values (1, 1), (2, 2)) as t (a, b)
group by rollup(a, ab)
order by 1, 2;
-- end
¤ Dauer der Verarbeitung: 0.28 Sekunden
¤
*© Formatika GbR, Deutschland
|
|
2026-11-25
|