Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 88381ad

Browse files
committed
Code cleanup inspired by recent resname bug report (doesn't fix the bug
yet, though). Avoid using nth() to fetch tlist entries; provide a common routine get_tle_by_resno() to search a tlist for a particular resno. This replaces a couple uses of nth() and a dozen hand-coded search loops. Also, replace a few uses of nth(length-1, list) with llast().
1 parent cae912d commit 88381ad

File tree

12 files changed

+162
-317
lines changed

12 files changed

+162
-317
lines changed

src/backend/catalog/pg_proc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/catalog/pg_proc.c,v 1.104 2003/08/04 02:39:58 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/catalog/pg_proc.c,v 1.105 2003/08/11 20:46:46 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -371,7 +371,7 @@ check_sql_fn_retval(Oid rettype, char fn_typtype, List *queryTreeList)
371371
}
372372

373373
/* find the final query */
374-
parse = (Query *) nth(length(queryTreeList) - 1, queryTreeList);
374+
parse = (Query *) llast(queryTreeList);
375375

376376
cmd = parse->commandType;
377377
tlist = parse->targetList;

src/backend/commands/comment.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Copyright (c) 1996-2003, PostgreSQL Global Development Group
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/commands/comment.c,v 1.69 2003/08/04 23:59:37 tgl Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/commands/comment.c,v 1.70 2003/08/11 20:46:46 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -365,7 +365,7 @@ CommentAttribute(List *qualname, char *comment)
365365
if (nnames < 2) /* parser messed up */
366366
elog(ERROR, "must specify relation and attribute");
367367
relname = ltruncate(nnames - 1, listCopy(qualname));
368-
attrname = strVal(nth(nnames - 1, qualname));
368+
attrname = strVal(llast(qualname));
369369

370370
/* Open the containing relation to ensure it won't go away meanwhile */
371371
rel = makeRangeVarFromNameList(relname);
@@ -583,7 +583,7 @@ CommentRule(List *qualname, char *comment)
583583
/* New-style: rule and relname both provided */
584584
Assert(nnames >= 2);
585585
relname = ltruncate(nnames - 1, listCopy(qualname));
586-
rulename = strVal(nth(nnames - 1, qualname));
586+
rulename = strVal(llast(qualname));
587587

588588
/* Open the owning relation to ensure it won't go away meanwhile */
589589
rel = makeRangeVarFromNameList(relname);
@@ -778,7 +778,7 @@ CommentTrigger(List *qualname, char *comment)
778778
if (nnames < 2) /* parser messed up */
779779
elog(ERROR, "must specify relation and trigger");
780780
relname = ltruncate(nnames - 1, listCopy(qualname));
781-
trigname = strVal(nth(nnames - 1, qualname));
781+
trigname = strVal(llast(qualname));
782782

783783
/* Open the owning relation to ensure it won't go away meanwhile */
784784
rel = makeRangeVarFromNameList(relname);
@@ -856,7 +856,7 @@ CommentConstraint(List *qualname, char *comment)
856856
if (nnames < 2) /* parser messed up */
857857
elog(ERROR, "must specify relation and constraint");
858858
relName = ltruncate(nnames - 1, listCopy(qualname));
859-
conName = strVal(nth(nnames - 1, qualname));
859+
conName = strVal(llast(qualname));
860860

861861
/* Open the owning relation to ensure it won't go away meanwhile */
862862
rel = makeRangeVarFromNameList(relName);

src/backend/commands/explain.c

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1994-5, Regents of the University of California
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/commands/explain.c,v 1.114 2003/08/08 21:41:30 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/commands/explain.c,v 1.115 2003/08/11 20:46:46 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -947,7 +947,6 @@ show_sort_keys(List *tlist, int nkeys, AttrNumber *keycols,
947947
List *context;
948948
bool useprefix;
949949
int keyno;
950-
List *tl;
951950
char *exprstr;
952951
Relids varnos;
953952
int i;
@@ -993,25 +992,17 @@ show_sort_keys(List *tlist, int nkeys, AttrNumber *keycols,
993992
{
994993
/* find key expression in tlist */
995994
AttrNumber keyresno = keycols[keyno];
995+
TargetEntry *target = get_tle_by_resno(tlist, keyresno);
996996

997-
foreach(tl, tlist)
998-
{
999-
TargetEntry *target = (TargetEntry *) lfirst(tl);
1000-
1001-
if (target->resdom->resno == keyresno)
1002-
{
1003-
/* Deparse the expression, showing any top-level cast */
1004-
exprstr = deparse_expression((Node *) target->expr, context,
1005-
useprefix, true);
1006-
/* And add to str */
1007-
if (keyno > 0)
1008-
appendStringInfo(str, ", ");
1009-
appendStringInfo(str, "%s", exprstr);
1010-
break;
1011-
}
1012-
}
1013-
if (tl == NIL)
997+
if (!target)
1014998
elog(ERROR, "no tlist entry for key %d", keyresno);
999+
/* Deparse the expression, showing any top-level cast */
1000+
exprstr = deparse_expression((Node *) target->expr, context,
1001+
useprefix, true);
1002+
/* And add to str */
1003+
if (keyno > 0)
1004+
appendStringInfo(str, ", ");
1005+
appendStringInfo(str, "%s", exprstr);
10151006
}
10161007

10171008
appendStringInfo(str, "\n");

src/backend/executor/execJunk.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/executor/execJunk.c,v 1.35 2003/08/04 02:39:58 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/executor/execJunk.c,v 1.36 2003/08/11 20:46:46 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -185,10 +185,7 @@ ExecGetJunkAttribute(JunkFilter *junkfilter,
185185
{
186186
List *targetList;
187187
List *t;
188-
Resdom *resdom;
189188
AttrNumber resno;
190-
char *resname;
191-
bool resjunk;
192189
TupleDesc tupType;
193190
HeapTuple tuple;
194191

@@ -202,11 +199,10 @@ ExecGetJunkAttribute(JunkFilter *junkfilter,
202199
foreach(t, targetList)
203200
{
204201
TargetEntry *tle = lfirst(t);
202+
Resdom *resdom = tle->resdom;
205203

206-
resdom = tle->resdom;
207-
resname = resdom->resname;
208-
resjunk = resdom->resjunk;
209-
if (resjunk && (strcmp(resname, attrName) == 0))
204+
if (resdom->resjunk && resdom->resname &&
205+
(strcmp(resdom->resname, attrName) == 0))
210206
{
211207
/* We found it ! */
212208
resno = resdom->resno;

src/backend/optimizer/path/allpaths.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/optimizer/path/allpaths.c,v 1.106 2003/08/04 02:40:00 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/optimizer/path/allpaths.c,v 1.107 2003/08/11 20:46:46 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -726,8 +726,7 @@ qual_is_pushdown_safe(Query *subquery, Index rti, Node *qual,
726726
foreach(vl, vars)
727727
{
728728
Var *var = (Var *) lfirst(vl);
729-
List *tl;
730-
TargetEntry *tle = NULL;
729+
TargetEntry *tle;
731730

732731
Assert(var->varno == rti);
733732

@@ -748,13 +747,8 @@ qual_is_pushdown_safe(Query *subquery, Index rti, Node *qual,
748747
}
749748

750749
/* Must find the tlist element referenced by the Var */
751-
foreach(tl, subquery->targetList)
752-
{
753-
tle = (TargetEntry *) lfirst(tl);
754-
if (tle->resdom->resno == var->varattno)
755-
break;
756-
}
757-
Assert(tl != NIL);
750+
tle = get_tle_by_resno(subquery->targetList, var->varattno);
751+
Assert(tle != NULL);
758752
Assert(!tle->resdom->resjunk);
759753

760754
/* If subquery uses DISTINCT or DISTINCT ON, check point 3 */

src/backend/optimizer/plan/createplan.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/createplan.c,v 1.153 2003/08/08 21:41:48 momjian Exp $
13+
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/createplan.c,v 1.154 2003/08/11 20:46:46 tgl Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -28,6 +28,7 @@
2828
#include "optimizer/restrictinfo.h"
2929
#include "optimizer/tlist.h"
3030
#include "optimizer/var.h"
31+
#include "parser/parsetree.h"
3132
#include "parser/parse_clause.h"
3233
#include "parser/parse_expr.h"
3334
#include "utils/lsyscache.h"
@@ -626,8 +627,8 @@ create_unique_plan(Query *root, UniquePath *best_path)
626627
{
627628
TargetEntry *tle;
628629

629-
tle = nth(groupColIdx[groupColPos] - 1, my_tlist);
630-
Assert(tle->resdom->resno == groupColIdx[groupColPos]);
630+
tle = get_tle_by_resno(my_tlist, groupColIdx[groupColPos]);
631+
Assert(tle != NULL);
631632
sortList = addTargetToSortList(NULL, tle, sortList,
632633
my_tlist, NIL, false);
633634
}
@@ -1975,7 +1976,7 @@ make_sort_from_groupcols(Query *root,
19751976
foreach(i, groupcls)
19761977
{
19771978
GroupClause *grpcl = (GroupClause *) lfirst(i);
1978-
TargetEntry *tle = nth(grpColIdx[grpno] - 1, sub_tlist);
1979+
TargetEntry *tle = get_tle_by_resno(sub_tlist, grpColIdx[grpno]);
19791980
Resdom *resdom = tle->resdom;
19801981

19811982
/*

src/backend/parser/parse_relation.c

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/parser/parse_relation.c,v 1.87 2003/08/04 02:40:02 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/parser/parse_relation.c,v 1.88 2003/08/11 20:46:46 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -1599,21 +1599,14 @@ get_rte_attribute_type(RangeTblEntry *rte, AttrNumber attnum,
15991599
case RTE_SUBQUERY:
16001600
{
16011601
/* Subselect RTE --- get type info from subselect's tlist */
1602-
List *tlistitem;
1603-
1604-
foreach(tlistitem, rte->subquery->targetList)
1605-
{
1606-
TargetEntry *te = (TargetEntry *) lfirst(tlistitem);
1607-
1608-
if (te->resdom->resjunk || te->resdom->resno != attnum)
1609-
continue;
1610-
*vartype = te->resdom->restype;
1611-
*vartypmod = te->resdom->restypmod;
1612-
return;
1613-
}
1614-
/* falling off end of list shouldn't happen... */
1615-
elog(ERROR, "subquery %s does not have attribute %d",
1616-
rte->eref->aliasname, attnum);
1602+
TargetEntry *te = get_tle_by_resno(rte->subquery->targetList,
1603+
attnum);
1604+
1605+
if (te == NULL || te->resdom->resjunk)
1606+
elog(ERROR, "subquery %s does not have attribute %d",
1607+
rte->eref->aliasname, attnum);
1608+
*vartype = te->resdom->restype;
1609+
*vartypmod = te->resdom->restypmod;
16171610
}
16181611
break;
16191612
case RTE_FUNCTION:
@@ -1777,6 +1770,29 @@ get_rte_attribute_is_dropped(RangeTblEntry *rte, AttrNumber attnum)
17771770
return result;
17781771
}
17791772

1773+
/*
1774+
* Given a targetlist and a resno, return the matching TargetEntry
1775+
*
1776+
* Returns NULL if resno is not present in list.
1777+
*
1778+
* Note: we need to search, rather than just indexing with nth(), because
1779+
* not all tlists are sorted by resno.
1780+
*/
1781+
TargetEntry *
1782+
get_tle_by_resno(List *tlist, AttrNumber resno)
1783+
{
1784+
List *i;
1785+
1786+
foreach(i, tlist)
1787+
{
1788+
TargetEntry *tle = (TargetEntry *) lfirst(i);
1789+
1790+
if (tle->resdom->resno == resno)
1791+
return tle;
1792+
}
1793+
return NULL;
1794+
}
1795+
17801796
/*
17811797
* given relation and att name, return id of variable
17821798
*

0 commit comments

Comments
 (0)