From d0dfb0641ec100aaff2c3a7cf0cefa2e55be7beb Mon Sep 17 00:00:00 2001
From: "Andrei V. Lepikhov" <lepihov@gmail.com>
Date: Wed, 9 Oct 2024 15:42:54 +0700
Subject: [PATCH 2/3] Comments for 0002 patch

---
 src/backend/optimizer/path/indxpath.c | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)

diff --git a/src/backend/optimizer/path/indxpath.c b/src/backend/optimizer/path/indxpath.c
index 602141911d..a2ddfb2bb9 100644
--- a/src/backend/optimizer/path/indxpath.c
+++ b/src/backend/optimizer/path/indxpath.c
@@ -1174,9 +1174,10 @@ build_paths_for_OR(PlannerInfo *root, RelOptInfo *rel,
 }
 
 /*
- * Data structure representing information about OR-clause argument and its
- * matching index key.  Used for grouping of similar OR-clause arguments in
+ * Utility structure used to group similar OR-clause arguments in
  * group_similar_or_args().
+ * It represents information about OR-clause argument and its
+ * matching index key.
  */
 typedef struct
 {
@@ -1228,12 +1229,17 @@ or_arg_index_match_cmp(const void *a, const void *b)
 
 /*
  * group_similar_or_args
- *		Group similar OR-arguments into dedicated RestrictInfos.  Process
- *		arguments of 'rinfo' clause, returns the processed list of arguments.
+ *		Transform incoming OR-restrictinfo into a list of sub-restrictinfos,
+ *		each of them contain a subset of OR-clauses from the source rinfo
+ *		matching the same index column with the same operator and collation,
+ *		It may be employed later, during the match_clause_to_indexcol to
+ *		transform whole OR-sub-rinfo to a SAOP clause.
  *
  * Similar arguments clauses of form "indexkey op constant" having same
  * indexkey, operator, and collation.  Constant may comprise either Const
  * or Param.
+ *
+ * Returns the processed list of arguments.
  */
 static List *
 group_similar_or_args(PlannerInfo *root, RelOptInfo *rel, RestrictInfo *rinfo)
@@ -1252,8 +1258,9 @@ group_similar_or_args(PlannerInfo *root, RelOptInfo *rel, RestrictInfo *rinfo)
 	n = list_length(orargs);
 
 	/*
-	 * Allocate and fill OrArgIndexMatch struct for each clause in the
-	 * argument list.
+	 * To avoid N^2 behaviour, take utility pass along the list of OR-clause
+	 * arguments. For each argument fill the OrArgIndexMatch structure which
+	 * will be used to sort these arguments at the next step.
 	 */
 	i = -1;
 	matches = (OrArgIndexMatch *) palloc(sizeof(OrArgIndexMatch) * n);
@@ -1368,7 +1375,11 @@ group_similar_or_args(PlannerInfo *root, RelOptInfo *rel, RestrictInfo *rinfo)
 	/* Sort clauses to make similar clauses go together */
 	qsort(matches, n, sizeof(OrArgIndexMatch), or_arg_index_match_cmp);
 
-	/* Group similar clauses into */
+	/*
+	 * Group similar clauses into single sub-restrictinfo.
+	 * Side effect: resulting list of restrictions will be sorted by indexnum
+	 * and colnum. Can we employ it somehow later?
+	 */
 	group_start = 0;
 	for (i = 1; i <= n; i++)
 	{
-- 
2.39.5

