From 8db2f4144b03e4ac093c3f46924f95b40e283e33 Mon Sep 17 00:00:00 2001 From: Mehmet Yilmaz Date: Tue, 17 Jun 2025 12:06:04 +0000 Subject: [PATCH] Introduce an RTE for the grouping step 247dea89f7616fdf06b7272b74abafc29e8e5860 --- .../distributed/deparser/ruleutils_18.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/backend/distributed/deparser/ruleutils_18.c b/src/backend/distributed/deparser/ruleutils_18.c index 2d01b4732..deecebb27 100644 --- a/src/backend/distributed/deparser/ruleutils_18.c +++ b/src/backend/distributed/deparser/ruleutils_18.c @@ -2218,11 +2218,28 @@ get_query_def_extended(Query *query, StringInfo buf, List *parentnamespace, { deparse_context context; deparse_namespace dpns; + int rtable_size; /* Guard against excessively long or deeply-nested queries */ CHECK_FOR_INTERRUPTS(); check_stack_depth(); + rtable_size = query->hasGroupRTE ? + list_length(query->rtable) - 1 : + list_length(query->rtable); + + /* + * Replace any Vars in the query's targetlist and havingQual that + * reference GROUP outputs with the underlying grouping expressions. + */ + if (query->hasGroupRTE) + { + query->targetList = (List *) + flatten_group_exprs(NULL, query, (Node *) query->targetList); + query->havingQual = + flatten_group_exprs(NULL, query, query->havingQual); + } + /* * Before we begin to examine the query, acquire locks on referenced * relations, and fix up deleted columns in JOIN RTEs. This ensures @@ -2247,7 +2264,7 @@ get_query_def_extended(Query *query, StringInfo buf, List *parentnamespace, context.targetList = NIL; context.windowClause = NIL; context.varprefix = (parentnamespace != NIL || - list_length(query->rtable) != 1); + rtable_size != 1); context.prettyFlags = prettyFlags; context.wrapColumn = wrapColumn; context.indentLevel = startIndent;