From a0d0572457815f5f1a039a15d8922cbce947ac10 Mon Sep 17 00:00:00 2001
From: "Andrei V. Lepikhov" <lepihov@gmail.com>
Date: Tue, 29 Oct 2024 09:29:30 +0700
Subject: [PATCH v2 4/4] Add GROUP-BY strategy: put the most distinct column at
 the head.

Let's allow GROUP-BY to utilize cost_sort feature which can differentiate
orders of pathkeys lists according to the ndistinct of the first column.
---
 src/backend/optimizer/path/pathkeys.c    | 73 +++++++++++++++++++++++-
 src/test/regress/expected/aggregates.out | 43 +++++++-------
 src/test/regress/sql/aggregates.sql      | 10 ++--
 3 files changed, 97 insertions(+), 29 deletions(-)

diff --git a/src/backend/optimizer/path/pathkeys.c b/src/backend/optimizer/path/pathkeys.c
index 7cf15e89b6..5bacf170e0 100644
--- a/src/backend/optimizer/path/pathkeys.c
+++ b/src/backend/optimizer/path/pathkeys.c
@@ -27,6 +27,7 @@
 #include "partitioning/partbounds.h"
 #include "rewrite/rewriteManip.h"
 #include "utils/lsyscache.h"
+#include "utils/selfuncs.h"
 
 /* Consider reordering of GROUP BY keys? */
 bool		enable_group_by_reordering = true;
@@ -473,6 +474,10 @@ get_useful_group_keys_orderings(PlannerInfo *root, Path *path)
 	List	   *pathkeys = root->group_pathkeys;
 	List	   *clauses = root->processed_groupClause;
 
+	double		nd_max = 0;
+	PathKey	   *pk_opt = NULL;
+	ListCell   *lc1, *lc2;
+
 	/* always return at least the original pathkeys/clauses */
 	info = makeNode(GroupByOrdering);
 	info->pathkeys = pathkeys;
@@ -526,9 +531,6 @@ get_useful_group_keys_orderings(PlannerInfo *root, Path *path)
 		/* Test consistency of info structures */
 		for_each_from(lc, infos, 1)
 		{
-			ListCell   *lc1,
-					   *lc2;
-
 			info = lfirst_node(GroupByOrdering, lc);
 
 			Assert(list_length(info->clauses) == list_length(pinfo->clauses));
@@ -546,6 +548,71 @@ get_useful_group_keys_orderings(PlannerInfo *root, Path *path)
 		}
 	}
 #endif
+
+	/*
+	 * Let's try the order with the column having max ndistinct value
+	 */
+
+	forboth(lc1, root->group_pathkeys, lc2, root->processed_groupClause)
+	{
+		PathKey			   *pkey = lfirst_node(PathKey, lc1);
+		SortGroupClause	   *gc = (SortGroupClause *) lfirst(lc2);
+		Node			   *node;
+		Bitmapset		   *relids;
+		VariableStatData	vardata;
+		double				nd = -1;
+		bool				isdefault;
+
+		if (foreach_current_index(lc1) >= root->num_groupby_pathkeys)
+			break;
+
+		node = get_sortgroupclause_expr(gc, root->parse->targetList);
+		relids = pull_varnos(root, node);
+
+		if (bms_num_members(relids) != 1 && bms_is_member(0, relids))
+			/*
+			 *Although functional index can estimate distincts here, the chance
+			 * is too low.
+			 */
+			continue;
+
+		examine_variable(root, node, 0, &vardata);
+		if (!HeapTupleIsValid(vardata.statsTuple))
+			continue;
+		nd = get_variable_numdistinct(&vardata, &isdefault);
+		ReleaseVariableStats(vardata);
+		if (isdefault)
+			continue;
+
+		Assert(nd >= 0);
+		if (nd_max == 0 || nd > nd_max)
+		{
+			nd_max = nd;
+			pk_opt = pkey;
+		}
+	}
+
+	if (pk_opt != NULL)
+	{
+		List   *new_pathkeys = list_make1(pk_opt);
+		int		n;
+
+		new_pathkeys = list_concat_unique_ptr(new_pathkeys, root->group_pathkeys);
+		n = group_keys_reorder_by_pathkeys(new_pathkeys, &pathkeys, &clauses,
+										   root->num_groupby_pathkeys);
+
+		if (n > 0 &&
+			(enable_incremental_sort || n == root->num_groupby_pathkeys) &&
+			compare_pathkeys(pathkeys, root->group_pathkeys) != PATHKEYS_EQUAL)
+		{
+			info = makeNode(GroupByOrdering);
+			info->pathkeys = pathkeys;
+			info->clauses = clauses;
+
+			infos = lappend(infos, info);
+		}
+	}
+
 	return infos;
 }
 
diff --git a/src/test/regress/expected/aggregates.out b/src/test/regress/expected/aggregates.out
index b974d365be..d57c4e553f 100644
--- a/src/test/regress/expected/aggregates.out
+++ b/src/test/regress/expected/aggregates.out
@@ -2812,14 +2812,14 @@ SELECT balk(hundred) FROM tenk1;
 ROLLBACK;
 -- GROUP BY optimization by reordering GROUP BY clauses
 CREATE TABLE btg AS SELECT
-  i % 10 AS x,
-  i % 10 AS y,
+  i % 231 AS x,
+  i % 49 AS y,
   i % 10 AS n,
-  'abc' || i % 10 AS z,
+  'abc' || i % 2 AS z,
   i AS w
-FROM generate_series(1, 100) AS i;
+FROM generate_series(1, 1000) AS i;
 CREATE INDEX btg_x_y_idx ON btg(x, y);
-ANALYZE btg;
+VACUUM ANALYZE btg;
 SET enable_hashagg = off;
 SET enable_seqscan = off;
 -- Utilize the ordering of index scan to avoid a Sort operation
@@ -2866,19 +2866,19 @@ EXPLAIN (COSTS OFF)
 SELECT count(*)
   FROM btg t1 JOIN btg t2 ON t1.n = t2.n AND t1.x = t2.x AND t1.z = t2.z
   GROUP BY t1.n, t1.z, t1.x;
-                               QUERY PLAN                                
--------------------------------------------------------------------------
+                           QUERY PLAN                           
+----------------------------------------------------------------
  GroupAggregate
    Group Key: t1.x, t1.n, t1.z
-   ->  Merge Join
-         Merge Cond: ((t1.x = t2.x) AND (t1.n = t2.n) AND (t1.z = t2.z))
-         ->  Incremental Sort
-               Sort Key: t1.x, t1.n, t1.z
-               Presorted Key: t1.x
+   ->  Incremental Sort
+         Sort Key: t1.x, t1.n, t1.z
+         Presorted Key: t1.x
+         ->  Merge Join
+               Merge Cond: (t1.x = t2.x)
+               Join Filter: ((t2.n = t1.n) AND (t2.z = t1.z))
                ->  Index Scan using btg_x_y_idx on btg t1
-         ->  Sort
-               Sort Key: t2.x, t2.n, t2.z
-               ->  Index Scan using btg_x_y_idx on btg t2
+               ->  Materialize
+                     ->  Index Scan using btg_x_y_idx on btg t2
 (11 rows)
 
 RESET enable_nestloop;
@@ -2903,11 +2903,12 @@ SELECT count(*) FROM btg GROUP BY w, x, y, z ORDER BY x*x, z;
  Sort
    Sort Key: ((x * x)), z
    ->  GroupAggregate
-         Group Key: w, x, y, z
-         ->  Sort
-               Sort Key: w, x, y, z
+         Group Key: x, y, w, z
+         ->  Incremental Sort
+               Sort Key: x, y, w, z
+               Presorted Key: x, y
                ->  Index Scan using btg_x_y_idx on btg
-(7 rows)
+(8 rows)
 
 -- Test the case where the number of incoming subtree path keys is more than
 -- the number of grouping keys.
@@ -2944,9 +2945,9 @@ GROUP BY c1.w, c1.z;
                      QUERY PLAN                      
 -----------------------------------------------------
  GroupAggregate
-   Group Key: c1.w, c1.z
+   Group Key: c1.z, c1.w
    ->  Sort
-         Sort Key: c1.w, c1.z, c1.x, c1.y
+         Sort Key: c1.z, c1.w, c1.x, c1.y
          ->  Merge Join
                Merge Cond: (c1.x = c2.x)
                ->  Sort
diff --git a/src/test/regress/sql/aggregates.sql b/src/test/regress/sql/aggregates.sql
index b474ed0cbf..557efc1923 100644
--- a/src/test/regress/sql/aggregates.sql
+++ b/src/test/regress/sql/aggregates.sql
@@ -1208,14 +1208,14 @@ ROLLBACK;
 
 -- GROUP BY optimization by reordering GROUP BY clauses
 CREATE TABLE btg AS SELECT
-  i % 10 AS x,
-  i % 10 AS y,
+  i % 231 AS x,
+  i % 49 AS y,
   i % 10 AS n,
-  'abc' || i % 10 AS z,
+  'abc' || i % 2 AS z,
   i AS w
-FROM generate_series(1, 100) AS i;
+FROM generate_series(1, 1000) AS i;
 CREATE INDEX btg_x_y_idx ON btg(x, y);
-ANALYZE btg;
+VACUUM ANALYZE btg;
 
 SET enable_hashagg = off;
 SET enable_seqscan = off;
-- 
2.39.5

