Repair logic for reordering grouping sets optimization.
authorAndrew Gierth <[email protected]>
Sun, 30 Jun 2019 22:49:29 +0000 (23:49 +0100)
committerAndrew Gierth <[email protected]>
Sun, 30 Jun 2019 22:49:29 +0000 (23:49 +0100)
The logic in reorder_grouping_sets to order grouping set elements to
match a pre-specified sort ordering was defective, resulting in
unnecessary sort nodes (though the query output would still be
correct). Repair, simplifying the code a little, and add a test.

Per report from Richard Guo, though I didn't use their patch. Original
bug seems to have been my fault.

Backpatch back to 9.5 where grouping sets were introduced.

Discussion: https://postgr.es/m/CAN_9JTzyjGcUjiBHxLsgqfk7PkdLGXiM=pwM+=ph2LsWw0WO1A@mail.gmail.com

src/backend/optimizer/plan/planner.c
src/test/regress/expected/groupingsets.out
src/test/regress/sql/groupingsets.sql

index 1bfcd5915e97709edeb8632e1152418a5efb7e2d..078df86b5eae839f447c6f9495cc2be829f452c6 100644 (file)
@@ -3049,7 +3049,6 @@ static List *
 reorder_grouping_sets(List *groupingsets, List *sortclause)
 {
    ListCell   *lc;
-   ListCell   *lc2;
    List       *previous = NIL;
    List       *result = NIL;
 
@@ -3058,34 +3057,32 @@ reorder_grouping_sets(List *groupingsets, List *sortclause)
        List       *candidate = lfirst(lc);
        List       *new_elems = list_difference_int(candidate, previous);
 
-       if (list_length(new_elems) > 0)
+       while (list_length(sortclause) > list_length(previous) &&
+              list_length(new_elems) > 0)
        {
-           while (list_length(sortclause) > list_length(previous))
-           {
-               SortGroupClause *sc = list_nth(sortclause, list_length(previous));
-               int         ref = sc->tleSortGroupRef;
+           SortGroupClause *sc = list_nth(sortclause, list_length(previous));
+           int         ref = sc->tleSortGroupRef;
 
-               if (list_member_int(new_elems, ref))
-               {
-                   previous = lappend_int(previous, ref);
-                   new_elems = list_delete_int(new_elems, ref);
-               }
-               else
-               {
-                   /* diverged from the sortclause; give up on it */
-                   sortclause = NIL;
-                   break;
-               }
+           if (list_member_int(new_elems, ref))
+           {
+               previous = lappend_int(previous, ref);
+               new_elems = list_delete_int(new_elems, ref);
            }
-
-           foreach(lc2, new_elems)
+           else
            {
-               previous = lappend_int(previous, lfirst_int(lc2));
+               /* diverged from the sortclause; give up on it */
+               sortclause = NIL;
+               break;
            }
        }
 
+       /*
+        * Safe to use list_concat (which shares cells of the second arg)
+        * because we know that new_elems does not share cells with anything.
+        */
+       previous = list_concat(previous, new_elems);
+
        result = lcons(list_copy(previous), result);
-       list_free(new_elems);
    }
 
    list_free(previous);
index 44fe5afcdcdc5e28ccf712f15b300f847b62697f..a12b048e343b7d832a2ac06857cd8e578d0f0b2c 100644 (file)
@@ -629,6 +629,19 @@ select a, b, sum(v.x)
    |   |   9
 (12 rows)
 
+-- Test reordering of grouping sets
+explain (costs off)
+select * from gstest1 group by grouping sets((a,b,v),(v)) order by v,b,a;
+                                  QUERY PLAN                                  
+------------------------------------------------------------------------------
+ GroupAggregate
+   Group Key: "*VALUES*".column3, "*VALUES*".column2, "*VALUES*".column1
+   Group Key: "*VALUES*".column3
+   ->  Sort
+         Sort Key: "*VALUES*".column3, "*VALUES*".column2, "*VALUES*".column1
+         ->  Values Scan on "*VALUES*"
+(6 rows)
+
 -- Agg level check. This query should error out.
 select (select grouping(a,b) from gstest2) from gstest2 group by a,b;
 ERROR:  arguments to GROUPING must be grouping expressions of the associated query level
index 85830e92d26dbef5efd90f93b32fbb2532f392b0..69815b643c330d8c036dbbece45a8cac22ec3c52 100644 (file)
@@ -202,6 +202,9 @@ select a, b, sum(v.x)
   from (values (1),(2)) v(x), gstest_data(v.x)
  group by cube (a,b) order by a,b;
 
+-- Test reordering of grouping sets
+explain (costs off)
+select * from gstest1 group by grouping sets((a,b,v),(v)) order by v,b,a;
 
 -- Agg level check. This query should error out.
 select (select grouping(a,b) from gstest2) from gstest2 group by a,b;