diff --git a/src/backend/distributed/deparser/ruleutils_14.c b/src/backend/distributed/deparser/ruleutils_14.c index 88948cff5..5410d10cb 100644 --- a/src/backend/distributed/deparser/ruleutils_14.c +++ b/src/backend/distributed/deparser/ruleutils_14.c @@ -2462,9 +2462,21 @@ get_basic_select_query(Query *query, deparse_context *context, { SortGroupClause *srt = (SortGroupClause *) lfirst(l); - appendStringInfoString(buf, sep); - get_rule_sortgroupclause(srt->tleSortGroupRef, query->targetList, - false, context); + /* Patch: Ensure the correct target list is used without alias swapping */ + TargetEntry *tle = get_sortgroupclause_tle(srt, query->targetList); + + /* Ensure the attribute names are preserved */ + if (tle && tle->resname != NULL) + { + appendStringInfoString(buf, sep); + appendStringInfoString(buf, quote_identifier(tle->resname)); + } + else + { + /* Fallback to the default behavior */ + get_rule_sortgroupclause(srt->tleSortGroupRef, query->targetList, false, context); + } + sep = ", "; } appendStringInfoChar(buf, ')'); diff --git a/src/backend/distributed/deparser/ruleutils_15.c b/src/backend/distributed/deparser/ruleutils_15.c index 018468d0b..0eb8f8a49 100644 --- a/src/backend/distributed/deparser/ruleutils_15.c +++ b/src/backend/distributed/deparser/ruleutils_15.c @@ -2551,9 +2551,21 @@ get_basic_select_query(Query *query, deparse_context *context, { SortGroupClause *srt = (SortGroupClause *) lfirst(l); - appendStringInfoString(buf, sep); - get_rule_sortgroupclause(srt->tleSortGroupRef, query->targetList, - false, context); + /* Patch: Ensure the correct target list is used without alias swapping */ + TargetEntry *tle = get_sortgroupclause_tle(srt, query->targetList); + + /* Ensure the attribute names are preserved */ + if (tle && tle->resname != NULL) + { + appendStringInfoString(buf, sep); + appendStringInfoString(buf, quote_identifier(tle->resname)); + } + else + { + /* Fallback to the default behavior */ + get_rule_sortgroupclause(srt->tleSortGroupRef, query->targetList, false, context); + } + sep = ", "; } appendStringInfoChar(buf, ')'); diff --git a/src/backend/distributed/deparser/ruleutils_16.c b/src/backend/distributed/deparser/ruleutils_16.c index 7f2a41d75..b31468bed 100644 --- a/src/backend/distributed/deparser/ruleutils_16.c +++ b/src/backend/distributed/deparser/ruleutils_16.c @@ -2565,9 +2565,21 @@ get_basic_select_query(Query *query, deparse_context *context, { SortGroupClause *srt = (SortGroupClause *) lfirst(l); - appendStringInfoString(buf, sep); - get_rule_sortgroupclause(srt->tleSortGroupRef, query->targetList, - false, context); + /* Patch: Ensure the correct target list is used without alias swapping */ + TargetEntry *tle = get_sortgroupclause_tle(srt, query->targetList); + + /* Ensure the attribute names are preserved */ + if (tle && tle->resname != NULL) + { + appendStringInfoString(buf, sep); + appendStringInfoString(buf, quote_identifier(tle->resname)); + } + else + { + /* Fallback to the default behavior */ + get_rule_sortgroupclause(srt->tleSortGroupRef, query->targetList, false, context); + } + sep = ", "; } appendStringInfoChar(buf, ')');