mirror of
https://github.com/postgres/postgres.git
synced 2025-06-29 10:41:53 +03:00
Fix oversight in planning of GROUP queries: when an expression is used
as both a GROUP BY item and an output expression, the top-level Group node should just copy up the evaluated expression value from its input, rather than re-evaluating the expression. Aside from any performance benefit this might offer, this avoids a crash when there is a sub-SELECT in said expression.
This commit is contained in:
@ -9,7 +9,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/setrefs.c,v 1.68 2000/10/26 21:36:09 tgl Exp $
|
||||
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/setrefs.c,v 1.69 2001/01/09 03:48:51 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@ -17,6 +17,7 @@
|
||||
|
||||
#include "postgres.h"
|
||||
|
||||
#include "nodes/makefuncs.h"
|
||||
#include "nodes/nodeFuncs.h"
|
||||
#include "optimizer/clauses.h"
|
||||
#include "optimizer/planmain.h"
|
||||
@ -270,22 +271,75 @@ set_join_references(Join *join)
|
||||
* to refer to the tuples returned by its lefttree subplan.
|
||||
*
|
||||
* This is used for single-input plan types like Agg, Group, Result.
|
||||
*
|
||||
* In most cases, we have to match up individual Vars in the tlist and
|
||||
* qual expressions with elements of the subplan's tlist (which was
|
||||
* generated by flatten_tlist() from these selfsame expressions, so it
|
||||
* should have all the required variables). There is an important exception,
|
||||
* however: a GROUP BY expression that is also an output expression will
|
||||
* have been pushed into the subplan tlist unflattened. We want to detect
|
||||
* this case and reference the subplan output directly. Therefore, check
|
||||
* for equality of the whole tlist expression to any subplan element before
|
||||
* we resort to picking the expression apart for individual Vars.
|
||||
*/
|
||||
static void
|
||||
set_uppernode_references(Plan *plan, Index subvarno)
|
||||
{
|
||||
Plan *subplan = plan->lefttree;
|
||||
List *subplanTargetList;
|
||||
List *subplanTargetList,
|
||||
*outputTargetList,
|
||||
*l;
|
||||
|
||||
if (subplan != NULL)
|
||||
subplanTargetList = subplan->targetlist;
|
||||
else
|
||||
subplanTargetList = NIL;
|
||||
|
||||
plan->targetlist = (List *)
|
||||
replace_vars_with_subplan_refs((Node *) plan->targetlist,
|
||||
subvarno,
|
||||
subplanTargetList);
|
||||
outputTargetList = NIL;
|
||||
foreach (l, plan->targetlist)
|
||||
{
|
||||
TargetEntry *tle = (TargetEntry *) lfirst(l);
|
||||
TargetEntry *subplantle;
|
||||
Node *newexpr;
|
||||
|
||||
subplantle = tlistentry_member(tle->expr, subplanTargetList);
|
||||
if (subplantle)
|
||||
{
|
||||
/* Found a matching subplan output expression */
|
||||
Resdom *resdom = subplantle->resdom;
|
||||
Var *newvar;
|
||||
|
||||
newvar = makeVar(subvarno,
|
||||
resdom->resno,
|
||||
resdom->restype,
|
||||
resdom->restypmod,
|
||||
0);
|
||||
/* If we're just copying a simple Var, copy up original info */
|
||||
if (subplantle->expr && IsA(subplantle->expr, Var))
|
||||
{
|
||||
Var *subvar = (Var *) subplantle->expr;
|
||||
|
||||
newvar->varnoold = subvar->varnoold;
|
||||
newvar->varoattno = subvar->varoattno;
|
||||
}
|
||||
else
|
||||
{
|
||||
newvar->varnoold = 0;
|
||||
newvar->varoattno = 0;
|
||||
}
|
||||
newexpr = (Node *) newvar;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* No matching expression, so replace individual Vars */
|
||||
newexpr = replace_vars_with_subplan_refs(tle->expr,
|
||||
subvarno,
|
||||
subplanTargetList);
|
||||
}
|
||||
outputTargetList = lappend(outputTargetList,
|
||||
makeTargetEntry(tle->resdom, newexpr));
|
||||
}
|
||||
plan->targetlist = outputTargetList;
|
||||
|
||||
plan->qual = (List *)
|
||||
replace_vars_with_subplan_refs((Node *) plan->qual,
|
||||
|
Reference in New Issue
Block a user