From 889f9e78fee9956c3a9a296c8917cd3aecb6f5fb Mon Sep 17 00:00:00 2001
From: Richard Guo <guofenglinux@gmail.com>
Date: Mon, 25 Sep 2023 17:33:20 +0800
Subject: [PATCH v1] Fix a wrong comment in setrefs.c

---
 src/backend/optimizer/plan/setrefs.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/src/backend/optimizer/plan/setrefs.c b/src/backend/optimizer/plan/setrefs.c
index 5700bfb5cd..adb747aae0 100644
--- a/src/backend/optimizer/plan/setrefs.c
+++ b/src/backend/optimizer/plan/setrefs.c
@@ -2936,7 +2936,13 @@ search_indexed_tlist_for_sortgroupref(Expr *node,
 	{
 		TargetEntry *tle = (TargetEntry *) lfirst(lc);
 
-		/* The equal() check should be redundant, but let's be paranoid */
+		/*
+		 * The equal() check is necessary, because expressions with the same
+		 * sortgroupref might be different, e.g., the given sort/group
+		 * expression can be type of FuncExpr which converts integer to
+		 * numeric, and we need to modify its args not itself to reference the
+		 * matching subplan output expression in this case.
+		 */
 		if (tle->ressortgroupref == sortgroupref &&
 			equal(node, tle->expr))
 		{
-- 
2.31.0

