Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 79c2576

Browse files
Jan WieckJan Wieck
Jan Wieck
authored and
Jan Wieck
committed
Replaced targetlist entry in GroupClause by reference number
in Resdom and GroupClause so changing of resno's doesn't confuse the grouping any more. Jan
1 parent 1a87c14 commit 79c2576

File tree

22 files changed

+242
-199
lines changed

22 files changed

+242
-199
lines changed

src/backend/nodes/copyfuncs.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/nodes/copyfuncs.c,v 1.78 1999/04/27 09:49:36 ishii Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/nodes/copyfuncs.c,v 1.79 1999/05/12 15:01:31 wieck Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -487,8 +487,8 @@ _copyGroupClause(GroupClause *from)
487487
{
488488
GroupClause *newnode = makeNode(GroupClause);
489489

490-
Node_Copy(from, newnode, entry);
491490
newnode->grpOpoid = from->grpOpoid;
491+
newnode->tleGroupref = from->tleGroupref;
492492

493493
return newnode;
494494
}
@@ -589,6 +589,7 @@ _copyResdom(Resdom *from)
589589
newnode->resname = pstrdup(from->resname);
590590
newnode->reskey = from->reskey;
591591
newnode->reskeyop = from->reskeyop;
592+
newnode->resgroupref = from->resgroupref;
592593
newnode->resjunk = from->resjunk;
593594

594595
return newnode;

src/backend/nodes/equalfuncs.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/nodes/equalfuncs.c,v 1.35 1999/02/18 00:49:14 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/nodes/equalfuncs.c,v 1.36 1999/05/12 15:01:33 wieck Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -48,6 +48,8 @@ _equalResdom(Resdom *a, Resdom *b)
4848
return false;
4949
if (a->reskey != b->reskey)
5050
return false;
51+
if (a->resgroupref != b->resgroupref)
52+
return false;
5153
if (a->reskeyop != b->reskeyop)
5254
return false;
5355

src/backend/nodes/freefuncs.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/nodes/Attic/freefuncs.c,v 1.15 1999/03/01 00:10:31 tgl Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/nodes/Attic/freefuncs.c,v 1.16 1999/05/12 15:01:33 wieck Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -396,8 +396,6 @@ _freeAgg(Agg *node)
396396
static void
397397
_freeGroupClause(GroupClause *node)
398398
{
399-
freeObject(node->entry);
400-
401399
pfree(node);
402400
}
403401

src/backend/nodes/makefuncs.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/nodes/makefuncs.c,v 1.13 1999/02/13 23:15:58 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/nodes/makefuncs.c,v 1.14 1999/05/12 15:01:34 wieck Exp $
1111
*
1212
* NOTES
1313
* Creator functions in POSTGRES 4.2 are generated automatically. Most of
@@ -107,6 +107,7 @@ makeResdom(AttrNumber resno,
107107
resdom->resname = resname;
108108
resdom->reskey = reskey;
109109
resdom->reskeyop = reskeyop;
110+
resdom->resgroupref = 0;
110111
resdom->resjunk = resjunk;
111112
return resdom;
112113
}

src/backend/nodes/outfuncs.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* Copyright (c) 1994, Regents of the University of California
77
*
8-
* $Id: outfuncs.c,v 1.80 1999/05/10 00:45:10 momjian Exp $
8+
* $Id: outfuncs.c,v 1.81 1999/05/12 15:01:34 wieck Exp $
99
*
1010
* NOTES
1111
* Every (plan) node in POSTGRES has an associated "out" routine which
@@ -255,10 +255,9 @@ _outSortClause(StringInfo str, SortClause *node)
255255
static void
256256
_outGroupClause(StringInfo str, GroupClause *node)
257257
{
258-
appendStringInfo(str, " GROUPCLAUSE :entry ");
259-
_outNode(str, node->entry);
260-
261-
appendStringInfo(str, " :grpOpoid %u ", node->grpOpoid);
258+
appendStringInfo(str, " GROUPCLAUSE :grpOpoid %u :tleGroupref %d",
259+
node->grpOpoid,
260+
node->tleGroupref);
262261
}
263262

264263
/*
@@ -556,15 +555,18 @@ _outHash(StringInfo str, Hash *node)
556555
static void
557556
_outResdom(StringInfo str, Resdom *node)
558557
{
559-
appendStringInfo(str, " RESDOM :resno %d :restype %u :restypmod %d ",
558+
appendStringInfo(str, " RESDOM :resno %d :restype %u :restypmod %d",
560559
node->resno,
561560
node->restype,
562561
node->restypmod);
563562

564-
appendStringInfo(str, " :resname \"%s\" :reskey %d :reskeyop %u :resjunk %d",
563+
appendStringInfo(str, " :resname \"%s\" :reskey %d :reskeyop %u",
565564
stringStringInfo(node->resname),
566565
node->reskey,
567-
node->reskeyop,
566+
node->reskeyop);
567+
568+
appendStringInfo(str, " :resgroupref %d :resjunk %d",
569+
node->resgroupref,
568570
node->resjunk);
569571
}
570572

src/backend/nodes/readfuncs.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/nodes/readfuncs.c,v 1.60 1999/03/01 00:10:31 tgl Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/nodes/readfuncs.c,v 1.61 1999/05/12 15:01:35 wieck Exp $
1111
*
1212
* NOTES
1313
* Most of the read functions for plan nodes are tested. (In fact, they
@@ -221,13 +221,14 @@ _readGroupClause()
221221

222222
local_node = makeNode(GroupClause);
223223

224-
token = lsptok(NULL, &length); /* skip the :entry */
225-
local_node->entry = nodeRead(true);
226-
227224
token = lsptok(NULL, &length); /* skip :grpOpoid */
228225
token = lsptok(NULL, &length); /* get grpOpoid */
229226
local_node->grpOpoid = strtoul(token, NULL, 10);
230227

228+
token = lsptok(NULL, &length); /* skip :tleGroupref */
229+
token = lsptok(NULL, &length); /* get tleGroupref */
230+
local_node->tleGroupref = strtoul(token, NULL, 10);
231+
231232
return local_node;
232233
}
233234

@@ -744,6 +745,10 @@ _readResdom()
744745
token = lsptok(NULL, &length); /* get reskeyop */
745746
local_node->reskeyop = (Oid) atol(token);
746747

748+
token = lsptok(NULL, &length); /* eat :resgroupref */
749+
token = lsptok(NULL, &length); /* get resgroupref */
750+
local_node->resgroupref = strtoul(token, NULL, 10);
751+
747752
token = lsptok(NULL, &length); /* eat :resjunk */
748753
token = lsptok(NULL, &length); /* get resjunk */
749754
local_node->resjunk = atoi(token);

src/backend/optimizer/plan/planner.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planner.c,v 1.50 1999/05/10 00:45:20 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planner.c,v 1.51 1999/05/12 15:01:37 wieck Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -260,7 +260,8 @@ union_planner(Query *parse)
260260
* belong to?)
261261
*/
262262
check_having_for_ungrouped_vars(parse->havingQual,
263-
parse->groupClause);
263+
parse->groupClause,
264+
parse->targetList);
264265
}
265266

266267
/* Calculate the opfids from the opnos */
@@ -426,8 +427,7 @@ make_subplanTargetList(Query *parse,
426427
GroupClause *grpcl = (GroupClause *) lfirst(gl);
427428

428429
keyno++; /* sort key # for this GroupClause */
429-
/* Is it safe to use just resno to match tlist and glist items?? */
430-
if (grpcl->entry->resdom->resno == resdom->resno)
430+
if (grpcl->tleGroupref == resdom->resgroupref)
431431
{
432432
/* Found a matching groupclause; record info for sorting */
433433
foundGroupClause = true;

src/backend/optimizer/plan/setrefs.c

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/setrefs.c,v 1.45 1999/05/06 23:07:33 tgl Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/setrefs.c,v 1.46 1999/05/12 15:01:39 wieck Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -961,7 +961,8 @@ del_agg_clause(Node *clause)
961961
*/
962962

963963
void
964-
check_having_for_ungrouped_vars(Node *clause, List *groupClause)
964+
check_having_for_ungrouped_vars(Node *clause, List *groupClause,
965+
List *targetList)
965966
{
966967
List *t;
967968

@@ -981,7 +982,7 @@ check_having_for_ungrouped_vars(Node *clause, List *groupClause)
981982
else if (IsA(clause, Iter))
982983
{
983984
check_having_for_ungrouped_vars(((Iter *) clause)->iterexpr,
984-
groupClause);
985+
groupClause, targetList);
985986
}
986987
else if (is_subplan(clause))
987988
{
@@ -997,7 +998,8 @@ check_having_for_ungrouped_vars(Node *clause, List *groupClause)
997998
foreach(gl, groupClause)
998999
{
9991000
if (var_equal(lfirst(t),
1000-
get_expr(((GroupClause *) lfirst(gl))->entry)))
1001+
get_groupclause_expr((GroupClause *)
1002+
lfirst(gl), targetList)))
10011003
{
10021004
contained_in_group_clause = true;
10031005
break;
@@ -1016,20 +1018,22 @@ check_having_for_ungrouped_vars(Node *clause, List *groupClause)
10161018
* subplan is a kind of Expr node.
10171019
*/
10181020
foreach(t, ((Expr *) clause)->args)
1019-
check_having_for_ungrouped_vars(lfirst(t), groupClause);
1021+
check_having_for_ungrouped_vars(lfirst(t), groupClause,
1022+
targetList);
10201023
}
10211024
else if (IsA(clause, List))
10221025
{
10231026
/*
10241027
* Recursively scan AND subclauses (see NOTE above).
10251028
*/
10261029
foreach(t, ((List *) clause))
1027-
check_having_for_ungrouped_vars(lfirst(t), groupClause);
1030+
check_having_for_ungrouped_vars(lfirst(t), groupClause,
1031+
targetList);
10281032
}
10291033
else if (IsA(clause, Aggref))
10301034
{
10311035
check_having_for_ungrouped_vars(((Aggref *) clause)->target,
1032-
groupClause);
1036+
groupClause, targetList);
10331037
}
10341038
else if (IsA(clause, ArrayRef))
10351039
{
@@ -1040,22 +1044,28 @@ check_having_for_ungrouped_vars(Node *clause, List *groupClause)
10401044
* expression and its index expression...
10411045
*/
10421046
foreach(t, aref->refupperindexpr)
1043-
check_having_for_ungrouped_vars(lfirst(t), groupClause);
1047+
check_having_for_ungrouped_vars(lfirst(t), groupClause,
1048+
targetList);
10441049
foreach(t, aref->reflowerindexpr)
1045-
check_having_for_ungrouped_vars(lfirst(t), groupClause);
1046-
check_having_for_ungrouped_vars(aref->refexpr, groupClause);
1047-
check_having_for_ungrouped_vars(aref->refassgnexpr, groupClause);
1050+
check_having_for_ungrouped_vars(lfirst(t), groupClause,
1051+
targetList);
1052+
check_having_for_ungrouped_vars(aref->refexpr, groupClause,
1053+
targetList);
1054+
check_having_for_ungrouped_vars(aref->refassgnexpr, groupClause,
1055+
targetList);
10481056
}
10491057
else if (case_clause(clause))
10501058
{
10511059
foreach(t, ((CaseExpr *) clause)->args)
10521060
{
10531061
CaseWhen *when = (CaseWhen *) lfirst(t);
1054-
check_having_for_ungrouped_vars(when->expr, groupClause);
1055-
check_having_for_ungrouped_vars(when->result, groupClause);
1062+
check_having_for_ungrouped_vars(when->expr, groupClause,
1063+
targetList);
1064+
check_having_for_ungrouped_vars(when->result, groupClause,
1065+
targetList);
10561066
}
10571067
check_having_for_ungrouped_vars(((CaseExpr *) clause)->defresult,
1058-
groupClause);
1068+
groupClause, targetList);
10591069
}
10601070
else
10611071
{

src/backend/optimizer/prep/preptlist.c

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/optimizer/prep/preptlist.c,v 1.18 1999/02/13 23:16:38 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/optimizer/prep/preptlist.c,v 1.19 1999/05/12 15:01:41 wieck Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -229,6 +229,49 @@ replace_matching_resname(List *new_tlist, List *old_tlist)
229229
new_tl = makeTargetEntry(newresno, old_tle->expr);
230230
t_list = lappend(t_list, new_tl);
231231
}
232+
233+
/*
234+
* Also it is possible that the parser or rewriter added
235+
* some junk attributes to hold GROUP BY expressions which
236+
* are not part of the result attributes.
237+
* We can simply identify them by looking at the resgroupref
238+
* in the TLE's resdom, which is a unique number telling which
239+
* TLE belongs to which GroupClause.
240+
*/
241+
if (old_tle->resdom->resgroupref > 0)
242+
{
243+
bool already_there = FALSE;
244+
TargetEntry *new_tle;
245+
Resdom *newresno;
246+
247+
/*
248+
* Check if the tle is already in the new list
249+
*/
250+
foreach(i, t_list)
251+
{
252+
new_tle = (TargetEntry *)lfirst(i);
253+
254+
if (new_tle->resdom->resgroupref ==
255+
old_tle->resdom->resgroupref)
256+
{
257+
already_there = TRUE;
258+
break;
259+
}
260+
261+
}
262+
263+
/*
264+
* If not, add it and make sure it is now a junk attribute
265+
*/
266+
if (!already_there)
267+
{
268+
newresno = (Resdom *) copyObject((Node *) old_tle->resdom);
269+
newresno->resno = length(t_list) + 1;
270+
newresno->resjunk = 1;
271+
new_tl = makeTargetEntry(newresno, old_tle->expr);
272+
t_list = lappend(t_list, new_tl);
273+
}
274+
}
232275
}
233276

234277
return t_list;

src/backend/optimizer/util/tlist.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/optimizer/util/tlist.c,v 1.29 1999/05/06 23:07:33 tgl Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/optimizer/util/tlist.c,v 1.30 1999/05/12 15:01:44 wieck Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -518,6 +518,25 @@ get_expr(TargetEntry *tle)
518518
}
519519

520520

521+
Var *
522+
get_groupclause_expr(GroupClause *groupClause, List *targetList)
523+
{
524+
List *l;
525+
TargetEntry *tle;
526+
527+
foreach(l, targetList)
528+
{
529+
tle = (TargetEntry *)lfirst(l);
530+
if (tle->resdom->resgroupref == groupClause->tleGroupref)
531+
return get_expr(tle);
532+
}
533+
534+
elog(ERROR,
535+
"get_groupclause_expr: GROUP BY expression not found in targetlist");
536+
return NULL;
537+
}
538+
539+
521540
/*****************************************************************************
522541
*
523542
*****************************************************************************/
@@ -528,6 +547,11 @@ get_expr(TargetEntry *tle)
528547
* in there.
529548
*/
530549
#ifdef NOT_USED
550+
/*
551+
* WARNING!!! If this ever get's used again, the new reference
552+
* mechanism from group clause to targetlist entry must be implemented
553+
* here too. Jan
554+
*/
531555
void
532556
AddGroupAttrToTlist(List *tlist, List *grpCl)
533557
{

0 commit comments

Comments
 (0)