From: Andres Freund Date: Sun, 26 Jul 2015 13:17:44 +0000 (+0200) Subject: Build column mapping for grouping sets in all required cases. X-Git-Tag: REL9_5_ALPHA2~68 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=65b86c1767a7dac0cc79dcfba7ba4cbd326dc03f;p=postgresql Build column mapping for grouping sets in all required cases. The previous coding frequently failed to fail because for one it's unusual to have rollup clauses with one column, and for another sometimes the wrong mapping didn't cause obvious problems. Author: Jeevan Chalke Reviewed-By: Andrew Gierth Discussion: CAM2+6=W=9=hQOipH0HAPbkun3Z3TFWij_EiHue0_6UX=oR=1kw@mail.gmail.com Backpatch: 9.5, where grouping sets were introduced --- diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c index b95cc95e5d..11678388fa 100644 --- a/src/backend/optimizer/plan/planner.c +++ b/src/backend/optimizer/plan/planner.c @@ -2401,13 +2401,8 @@ build_grouping_chain(PlannerInfo *root, * Prepare the grpColIdx for the real Agg node first, because we may need * it for sorting */ - if (list_length(rollup_groupclauses) > 1) - { - Assert(rollup_lists && llast(rollup_lists)); - - top_grpColIdx = - remap_groupColIdx(root, llast(rollup_groupclauses)); - } + if (parse->groupingSets) + top_grpColIdx = remap_groupColIdx(root, llast(rollup_groupclauses)); /* * If we need a Sort operation on the input, generate that. diff --git a/src/test/regress/expected/groupingsets.out b/src/test/regress/expected/groupingsets.out index 842c2aec7e..2e12a53d69 100644 --- a/src/test/regress/expected/groupingsets.out +++ b/src/test/regress/expected/groupingsets.out @@ -587,4 +587,27 @@ select array(select row(v.a,s1.*) from (select two,four, count(*) from onek grou {"(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)"} (2 rows) +-- Grouping on text columns +select sum(ten) from onek group by two, rollup(four::text) order by 1; + sum +------ + 1000 + 1000 + 1250 + 1250 + 2000 + 2500 +(6 rows) + +select sum(ten) from onek group by rollup(four::text), two order by 1; + sum +------ + 1000 + 1000 + 1250 + 1250 + 2000 + 2500 +(6 rows) + -- end diff --git a/src/test/regress/sql/groupingsets.sql b/src/test/regress/sql/groupingsets.sql index 0bffb8531c..eeea995f33 100644 --- a/src/test/regress/sql/groupingsets.sql +++ b/src/test/regress/sql/groupingsets.sql @@ -162,4 +162,8 @@ group by rollup(ten); 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; 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); +-- Grouping on text columns +select sum(ten) from onek group by two, rollup(four::text) order by 1; +select sum(ten) from onek group by rollup(four::text), two order by 1; + -- end