You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
If there are subqueries in the grouping expressions, each of thesesubqueries in the targetlist and HAVING clause is expanded intodistinct SubPlan nodes. As a result, only one of these SubPlan nodeswould be converted to reference to the grouping key column output bythe Agg node; others would have to get evaluated afresh. This is notefficient, and with grouping sets this can cause wrong results issuesin cases where they should go to NULL because they are from the wronggrouping set. Furthermore, during re-evaluation, these SubPlan nodesmight use nulled column values from grouping sets, which is notcorrect.This issue is not limited to subqueries. For other types ofexpressions that are part of grouping items, if they are transformedinto another form during preprocessing, they may fail to match lowertarget items. This can also lead to wrong results with grouping sets.To fix this issue, we introduce a new kind of RTE representing theoutput of the grouping step, with columns that are the Vars orexpressions being grouped on. In the parser, we replace the groupingexpressions in the targetlist and HAVING clause with Vars referencingthis new RTE, so that the output of the parser directly expresses thesemantic requirement that the grouping expressions be gotten from thegrouping output rather than computed some other way. In the planner,we first preprocess all the columns of this new RTE and then replaceany Vars in the targetlist and HAVING clause that reference this newRTE with the underlying grouping expressions, so that we will haveonly one instance of a SubPlan node for each subquery contained in thegrouping expressions.Bump catversion because this changes the querytree produced by theparser.Thanks to Tom Lane for the idea to invent a new kind of RTE.Per reports from Geoff Winkless, Tobias Wendorff, Richard Guo fromvarious threads.Author: Richard GuoReviewed-by: Ashutosh Bapat, Sutou KouheiDiscussion:https://postgr.es/m/CAMbWs4_dp7e7oTwaiZeBX8+P1rXw4ThkZxh1QG81rhu9Z47VsQ@mail.gmail.com