properly redistribute WindowAgg input, fix plan change in window.out
authorTomas Vondra <[email protected]>
Sun, 22 Jan 2017 20:11:39 +0000 (21:11 +0100)
committerTomas Vondra <[email protected]>
Sun, 22 Jan 2017 20:11:39 +0000 (21:11 +0100)
We can't push down the whole WindowAgg (at least now for now), so
we need to redistribute the input properly. Luckily thanks to the
parallel aggregates, we get plans like this:

                               QUERY PLAN
    -----------------------------------------------------------------
     WindowAgg
       ->  Finalize GroupAggregate
             Group Key: (tenk1.ten + tenk1.four)
             ->  Remote Subquery Scan on all (datanode_1,datanode_2)
                   ->  Sort
                         Sort Key: ((tenk1.ten + tenk1.four))
                         ->  Partial HashAggregate
                               Group Key: (tenk1.ten + tenk1.four)
                               ->  Seq Scan on tenk1
    (9 rows)

That however means mild breakage in window.out, due to the partial
aggregates etc.

src/backend/optimizer/plan/planner.c
src/backend/optimizer/util/pathnode.c
src/test/regress/expected/window.out

index 4ea83f0b0d3dec59efdee0de1313fe87f93072e8..fffcddd0ff0a1447fb3e7ce53bcfd8f55a354100 100644 (file)
@@ -167,7 +167,7 @@ static bool groupingsets_distribution_match(PlannerInfo *root, Query *parse,
 static Path *adjust_path_distribution(PlannerInfo *root, Query *parse,
                                          Path *path);
 static bool can_push_down_grouping(PlannerInfo *root, Query *parse, Path *path);
-
+static bool can_push_down_window(PlannerInfo *root, Path *path);
 
 /*****************************************************************************
  *
@@ -4820,6 +4820,10 @@ create_one_window_path(PlannerInfo *root,
                        window_target = output_target;
                }
 
+               /* We can't really push down window functions for now. */
+               if (!can_push_down_window(root, path))
+                       path = create_remotesubplan_path(root, path, NULL);
+
                path = (Path *)
                        create_windowagg_path(root, window_rel, path, window_target,
                                                                  wflists->windowFuncs[wc->winref],
@@ -6340,3 +6344,13 @@ can_push_down_grouping(PlannerInfo *root, Query *parse, Path *path)
 
        return grouping_distribution_match(root, parse, path, parse->groupClause);
 }
+
+static bool
+can_push_down_window(PlannerInfo *root, Path *path)
+{
+       /*  */
+       if (! path->distribution)
+               return true;
+
+       return false;
+}
index 7bd784078685b286c6f337e5c1d9addfc092ca7a..3c7553051db8271ec3c9e383593252d154cc9a15 100644 (file)
@@ -4189,6 +4189,8 @@ create_windowagg_path(PlannerInfo *root,
        pathnode->winclause = winclause;
        pathnode->winpathkeys = winpathkeys;
 
+       pathnode->path.distribution = copyObject(subpath->distribution);
+
        /*
         * For costing purposes, assume that there are no redundant partitioning
         * or ordering columns; it's not worth the trouble to deal with that
index a844fb357bd19b4ba5c8508da7d9415c4665e6c7..2d92a32e4fe541cd5147ecc0f5517066b505f958 100644 (file)
@@ -598,13 +598,15 @@ select first_value(max(x)) over (), y
                            QUERY PLAN                            
 -----------------------------------------------------------------
  WindowAgg
-   ->  HashAggregate
+   ->  Finalize GroupAggregate
          Group Key: (tenk1.ten + tenk1.four)
          ->  Remote Subquery Scan on all (datanode_1,datanode_2)
-               ->  HashAggregate
-                     Group Key: (tenk1.ten + tenk1.four)
-                     ->  Seq Scan on tenk1
-(7 rows)
+               ->  Sort
+                     Sort Key: ((tenk1.ten + tenk1.four))
+                     ->  Partial HashAggregate
+                           Group Key: (tenk1.ten + tenk1.four)
+                           ->  Seq Scan on tenk1
+(9 rows)
 
 -- test non-default frame specifications
 SELECT four, ten,