aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/adt/ruleutils.c
diff options
context:
space:
mode:
authorRichard Guo <rguo@postgresql.org>2024-09-10 12:35:34 +0900
committerRichard Guo <rguo@postgresql.org>2024-09-10 12:35:34 +0900
commit247dea89f7616fdf06b7272b74abafc29e8e5860 (patch)
tree8f4f7c0acb5b9842df4517019805d7531aec1b46 /src/backend/utils/adt/ruleutils.c
parentfba49d5293b4455b25485450baf02af42bf543d7 (diff)
downloadpostgresql-247dea89f7616fdf06b7272b74abafc29e8e5860.tar.gz
postgresql-247dea89f7616fdf06b7272b74abafc29e8e5860.zip
Introduce an RTE for the grouping step
If there are subqueries in the grouping expressions, each of these subqueries in the targetlist and HAVING clause is expanded into distinct SubPlan nodes. As a result, only one of these SubPlan nodes would be converted to reference to the grouping key column output by the Agg node; others would have to get evaluated afresh. This is not efficient, and with grouping sets this can cause wrong results issues in cases where they should go to NULL because they are from the wrong grouping set. Furthermore, during re-evaluation, these SubPlan nodes might use nulled column values from grouping sets, which is not correct. This issue is not limited to subqueries. For other types of expressions that are part of grouping items, if they are transformed into another form during preprocessing, they may fail to match lower target items. This can also lead to wrong results with grouping sets. To fix this issue, we introduce a new kind of RTE representing the output of the grouping step, with columns that are the Vars or expressions being grouped on. In the parser, we replace the grouping expressions in the targetlist and HAVING clause with Vars referencing this new RTE, so that the output of the parser directly expresses the semantic requirement that the grouping expressions be gotten from the grouping output rather than computed some other way. In the planner, we first preprocess all the columns of this new RTE and then replace any Vars in the targetlist and HAVING clause that reference this new RTE with the underlying grouping expressions, so that we will have only one instance of a SubPlan node for each subquery contained in the grouping expressions. Bump catversion because this changes the querytree produced by the parser. Thanks to Tom Lane for the idea to invent a new kind of RTE. Per reports from Geoff Winkless, Tobias Wendorff, Richard Guo from various threads. Author: Richard Guo Reviewed-by: Ashutosh Bapat, Sutou Kouhei Discussion: https://postgr.es/m/CAMbWs4_dp7e7oTwaiZeBX8+P1rXw4ThkZxh1QG81rhu9Z47VsQ@mail.gmail.com
Diffstat (limited to 'src/backend/utils/adt/ruleutils.c')
-rw-r--r--src/backend/utils/adt/ruleutils.c27
1 files changed, 26 insertions, 1 deletions
diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c
index 54b3542894b..ee1b7f3dc94 100644
--- a/src/backend/utils/adt/ruleutils.c
+++ b/src/backend/utils/adt/ruleutils.c
@@ -5433,11 +5433,28 @@ get_query_def(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
@@ -5455,7 +5472,7 @@ get_query_def(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;
@@ -8115,6 +8132,14 @@ get_name_for_var_field(Var *var, int fieldno,
}
}
break;
+ case RTE_GROUP:
+
+ /*
+ * We couldn't get here: any Vars that reference the RTE_GROUP RTE
+ * should have been replaced with the underlying grouping
+ * expressions.
+ */
+ break;
}
/*