From 3c0483df88a950c3109c574a4a50a615ab122a23 Mon Sep 17 00:00:00 2001
From: "Andrei V. Lepikhov" <lepihov@gmail.com>
Date: Tue, 22 Oct 2024 16:54:49 +0400
Subject: [PATCH v2 3/4] Consider ndistinct on the first column in cost_sort().

---
 src/backend/optimizer/path/costsize.c        | 46 ++++++++++++++++++--
 src/test/regress/expected/aggregates.out     | 22 +++++-----
 src/test/regress/expected/partition_join.out | 18 ++++----
 src/test/regress/sql/aggregates.sql          |  5 ++-
 4 files changed, 66 insertions(+), 25 deletions(-)

diff --git a/src/backend/optimizer/path/costsize.c b/src/backend/optimizer/path/costsize.c
index ca1fd48af8..20e09a391a 100644
--- a/src/backend/optimizer/path/costsize.c
+++ b/src/backend/optimizer/path/costsize.c
@@ -205,6 +205,8 @@ static double page_size(double tuples, int width);
 static double get_parallel_divisor(Path *path);
 static EquivalenceMember *identify_sort_ecmember(PlannerInfo *root,
 												 EquivalenceClass *ec);
+static double sort_comparisons_factor(PlannerInfo *root, List *pathkeys,
+									  double ntuples);
 
 
 /*
@@ -2155,8 +2157,7 @@ cost_sort(Path *path, PlannerInfo *root,
 {
 	Cost		startup_cost;
 	Cost		run_cost;
-	double		cmpfrac =
-						(pathkeys == NIL) ? 2.0 : list_length(pathkeys) + 1.0;
+	double		cmpfrac = sort_comparisons_factor(root, pathkeys, tuples);
 
 	cost_tuplesort(&startup_cost, &run_cost,
 				   tuples, width, cmpfrac,
@@ -6673,4 +6674,43 @@ identify_sort_ecmember(PlannerInfo *root, EquivalenceClass *ec)
 
 	Assert(candidate != NULL);
 	return candidate;
-}
\ No newline at end of file
+}
+
+/*
+ * Calculate multiplier reflecting the number of comparisons which executor
+ * have to perform during the sort with this specific order of columns.
+ *
+ * The comparison factor f = 1.+F(pathkeys). There 1. incapsulates the
+ * second-order of significance phusics which cost function doesn't consider.
+ * F(pathkeys) is the estimated fraction of comparisons in the range [1..N].
+ * F = 1 corresponds the 'all-unique' first column case. In that case the sort
+ * will call comparison function only once for each couple of tuples.
+ * F = N represents the case, when values in all columns are constant.
+ */
+static double
+sort_comparisons_factor(PlannerInfo *root, List *pathkeys, double ntuples)
+{
+	int		n = list_length(pathkeys);
+	double	cmpfrac = (n == 0) ? 2.0 : n + 1;
+
+	if (root != NULL && ntuples > 1 && n > 1)
+	{
+		PathKey			   *key = linitial_node(PathKey, pathkeys);
+		EquivalenceMember  *em =  identify_sort_ecmember(root, key->pk_eclass);
+
+		Assert(em->em_ndistinct >= 0);
+
+		if (em->em_ndistinct == 0.)
+			/*
+			 * Optimiser doesn't have an info on ndistinct value, return
+			 * extreme case
+			 */
+			return cmpfrac;
+
+		if (ntuples >= em->em_ndistinct)
+			cmpfrac =
+				2.0 + ((ntuples - em->em_ndistinct) / (ntuples - 1)) * (n - 1);
+	}
+
+	return cmpfrac;
+}
diff --git a/src/test/regress/expected/aggregates.out b/src/test/regress/expected/aggregates.out
index 7546c795d0..b974d365be 100644
--- a/src/test/regress/expected/aggregates.out
+++ b/src/test/regress/expected/aggregates.out
@@ -2814,6 +2814,7 @@ ROLLBACK;
 CREATE TABLE btg AS SELECT
   i % 10 AS x,
   i % 10 AS y,
+  i % 10 AS n,
   'abc' || i % 10 AS z,
   i AS w
 FROM generate_series(1, 100) AS i;
@@ -2863,20 +2864,20 @@ SET enable_hashjoin = off;
 SET enable_nestloop = off;
 EXPLAIN (COSTS OFF)
 SELECT count(*)
-  FROM btg t1 JOIN btg t2 ON t1.w = t2.w AND t1.x = t2.x AND t1.z = t2.z
-  GROUP BY t1.w, t1.z, t1.x;
+  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                                
 -------------------------------------------------------------------------
  GroupAggregate
-   Group Key: t1.x, t1.w, t1.z
+   Group Key: t1.x, t1.n, t1.z
    ->  Merge Join
-         Merge Cond: ((t1.x = t2.x) AND (t1.w = t2.w) AND (t1.z = t2.z))
+         Merge Cond: ((t1.x = t2.x) AND (t1.n = t2.n) AND (t1.z = t2.z))
          ->  Incremental Sort
-               Sort Key: t1.x, t1.w, t1.z
+               Sort Key: t1.x, t1.n, t1.z
                Presorted Key: t1.x
                ->  Index Scan using btg_x_y_idx on btg t1
          ->  Sort
-               Sort Key: t2.x, t2.w, t2.z
+               Sort Key: t2.x, t2.n, t2.z
                ->  Index Scan using btg_x_y_idx on btg t2
 (11 rows)
 
@@ -2902,12 +2903,11 @@ SELECT count(*) FROM btg GROUP BY w, x, y, z ORDER BY x*x, z;
  Sort
    Sort Key: ((x * x)), z
    ->  GroupAggregate
-         Group Key: x, y, w, z
-         ->  Incremental Sort
-               Sort Key: x, y, w, z
-               Presorted Key: x, y
+         Group Key: w, x, y, z
+         ->  Sort
+               Sort Key: w, x, y, z
                ->  Index Scan using btg_x_y_idx on btg
-(8 rows)
+(7 rows)
 
 -- Test the case where the number of incoming subtree path keys is more than
 -- the number of grouping keys.
diff --git a/src/test/regress/expected/partition_join.out b/src/test/regress/expected/partition_join.out
index 886dc559a5..7a30b8bbe9 100644
--- a/src/test/regress/expected/partition_join.out
+++ b/src/test/regress/expected/partition_join.out
@@ -654,39 +654,39 @@ SELECT a, b FROM prt1 FULL JOIN prt2 p2(b,a,c) USING(a,b)
                ->  Sort
                      Sort Key: (COALESCE(prt1.a, p2.a)), (COALESCE(prt1.b, p2.b))
                      ->  Merge Full Join
-                           Merge Cond: ((prt1.a = p2.a) AND (prt1.b = p2.b))
+                           Merge Cond: ((prt1.b = p2.b) AND (prt1.a = p2.a))
                            Filter: ((COALESCE(prt1.a, p2.a) >= 490) AND (COALESCE(prt1.a, p2.a) <= 510))
                            ->  Sort
-                                 Sort Key: prt1.a, prt1.b
+                                 Sort Key: prt1.b, prt1.a
                                  ->  Seq Scan on prt1_p1 prt1
                            ->  Sort
-                                 Sort Key: p2.a, p2.b
+                                 Sort Key: p2.b, p2.a
                                  ->  Seq Scan on prt2_p1 p2
          ->  Group
                Group Key: (COALESCE(prt1_1.a, p2_1.a)), (COALESCE(prt1_1.b, p2_1.b))
                ->  Sort
                      Sort Key: (COALESCE(prt1_1.a, p2_1.a)), (COALESCE(prt1_1.b, p2_1.b))
                      ->  Merge Full Join
-                           Merge Cond: ((prt1_1.a = p2_1.a) AND (prt1_1.b = p2_1.b))
+                           Merge Cond: ((prt1_1.b = p2_1.b) AND (prt1_1.a = p2_1.a))
                            Filter: ((COALESCE(prt1_1.a, p2_1.a) >= 490) AND (COALESCE(prt1_1.a, p2_1.a) <= 510))
                            ->  Sort
-                                 Sort Key: prt1_1.a, prt1_1.b
+                                 Sort Key: prt1_1.b, prt1_1.a
                                  ->  Seq Scan on prt1_p2 prt1_1
                            ->  Sort
-                                 Sort Key: p2_1.a, p2_1.b
+                                 Sort Key: p2_1.b, p2_1.a
                                  ->  Seq Scan on prt2_p2 p2_1
          ->  Group
                Group Key: (COALESCE(prt1_2.a, p2_2.a)), (COALESCE(prt1_2.b, p2_2.b))
                ->  Sort
                      Sort Key: (COALESCE(prt1_2.a, p2_2.a)), (COALESCE(prt1_2.b, p2_2.b))
                      ->  Merge Full Join
-                           Merge Cond: ((prt1_2.a = p2_2.a) AND (prt1_2.b = p2_2.b))
+                           Merge Cond: ((prt1_2.b = p2_2.b) AND (prt1_2.a = p2_2.a))
                            Filter: ((COALESCE(prt1_2.a, p2_2.a) >= 490) AND (COALESCE(prt1_2.a, p2_2.a) <= 510))
                            ->  Sort
-                                 Sort Key: prt1_2.a, prt1_2.b
+                                 Sort Key: prt1_2.b, prt1_2.a
                                  ->  Seq Scan on prt1_p3 prt1_2
                            ->  Sort
-                                 Sort Key: p2_2.a, p2_2.b
+                                 Sort Key: p2_2.b, p2_2.a
                                  ->  Seq Scan on prt2_p3 p2_2
 (43 rows)
 
diff --git a/src/test/regress/sql/aggregates.sql b/src/test/regress/sql/aggregates.sql
index 4885daffe6..b474ed0cbf 100644
--- a/src/test/regress/sql/aggregates.sql
+++ b/src/test/regress/sql/aggregates.sql
@@ -1210,6 +1210,7 @@ ROLLBACK;
 CREATE TABLE btg AS SELECT
   i % 10 AS x,
   i % 10 AS y,
+  i % 10 AS n,
   'abc' || i % 10 AS z,
   i AS w
 FROM generate_series(1, 100) AS i;
@@ -1237,8 +1238,8 @@ SET enable_hashjoin = off;
 SET enable_nestloop = off;
 EXPLAIN (COSTS OFF)
 SELECT count(*)
-  FROM btg t1 JOIN btg t2 ON t1.w = t2.w AND t1.x = t2.x AND t1.z = t2.z
-  GROUP BY t1.w, t1.z, t1.x;
+  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;
 RESET enable_nestloop;
 RESET enable_hashjoin;
 
-- 
2.39.5

