diff options
author | Neil Conway <neilc@samurai.com> | 2004-05-26 04:41:50 +0000 |
---|---|---|
committer | Neil Conway <neilc@samurai.com> | 2004-05-26 04:41:50 +0000 |
commit | d0b4399d81f39decccb23fa38f772b71b51bf96a (patch) | |
tree | 71d3b737f5d93f6c3984412a4910b5810156c5ca /src/backend/parser | |
parent | 18d0d105635fbc7e476063e662b449f296953a04 (diff) | |
download | postgresql-d0b4399d81f39decccb23fa38f772b71b51bf96a.tar.gz postgresql-d0b4399d81f39decccb23fa38f772b71b51bf96a.zip |
Reimplement the linked list data structure used throughout the backend.
In the past, we used a 'Lispy' linked list implementation: a "list" was
merely a pointer to the head node of the list. The problem with that
design is that it makes lappend() and length() linear time. This patch
fixes that problem (and others) by maintaining a count of the list
length and a pointer to the tail node along with each head node pointer.
A "list" is now a pointer to a structure containing some meta-data
about the list; the head and tail pointers in that structure refer
to ListCell structures that maintain the actual linked list of nodes.
The function names of the list API have also been changed to, I hope,
be more logically consistent. By default, the old function names are
still available; they will be disabled-by-default once the rest of
the tree has been updated to use the new API names.
Diffstat (limited to 'src/backend/parser')
-rw-r--r-- | src/backend/parser/analyze.c | 217 | ||||
-rw-r--r-- | src/backend/parser/gram.y | 22 | ||||
-rw-r--r-- | src/backend/parser/parse_agg.c | 18 | ||||
-rw-r--r-- | src/backend/parser/parse_clause.c | 81 | ||||
-rw-r--r-- | src/backend/parser/parse_coerce.c | 14 | ||||
-rw-r--r-- | src/backend/parser/parse_expr.c | 114 | ||||
-rw-r--r-- | src/backend/parser/parse_func.c | 116 | ||||
-rw-r--r-- | src/backend/parser/parse_node.c | 4 | ||||
-rw-r--r-- | src/backend/parser/parse_relation.c | 91 | ||||
-rw-r--r-- | src/backend/parser/parse_target.c | 16 | ||||
-rw-r--r-- | src/backend/parser/parse_type.c | 16 |
11 files changed, 345 insertions, 364 deletions
diff --git a/src/backend/parser/analyze.c b/src/backend/parser/analyze.c index 2b1eddbcf09..9da8937dc7b 100644 --- a/src/backend/parser/analyze.c +++ b/src/backend/parser/analyze.c @@ -6,7 +6,7 @@ * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/backend/parser/analyze.c,v 1.300 2004/05/23 17:10:54 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/parser/analyze.c,v 1.301 2004/05/26 04:41:29 neilc Exp $ * *------------------------------------------------------------------------- */ @@ -240,28 +240,20 @@ do_parse_analyze(Node *parseTree, ParseState *pstate) List *extras_before = NIL; List *extras_after = NIL; Query *query; - List *listscan; + ListCell *l; query = transformStmt(pstate, parseTree, &extras_before, &extras_after); /* don't need to access result relation any more */ release_pstate_resources(pstate); - while (extras_before != NIL) - { - result = nconc(result, - parse_sub_analyze(lfirst(extras_before), pstate)); - extras_before = lnext(extras_before); - } + foreach(l, extras_before) + result = nconc(result, parse_sub_analyze(lfirst(l), pstate)); result = lappend(result, query); - while (extras_after != NIL) - { - result = nconc(result, - parse_sub_analyze(lfirst(extras_after), pstate)); - extras_after = lnext(extras_after); - } + foreach(l, extras_after) + result = nconc(result, parse_sub_analyze(lfirst(l), pstate)); /* * Make sure that only the original query is marked original. We have @@ -270,9 +262,9 @@ do_parse_analyze(Node *parseTree, ParseState *pstate) * mark only the original query as allowed to set the command-result * tag. */ - foreach(listscan, result) + foreach(l, result) { - Query *q = lfirst(listscan); + Query *q = lfirst(l); if (q == query) { @@ -428,8 +420,8 @@ transformViewStmt(ParseState *pstate, ViewStmt *stmt, */ if (stmt->aliases != NIL) { - List *aliaslist = stmt->aliases; - List *targetList; + ListCell *alist_item = list_head(stmt->aliases); + ListCell *targetList; foreach(targetList, stmt->query->targetList) { @@ -442,13 +434,13 @@ transformViewStmt(ParseState *pstate, ViewStmt *stmt, /* junk columns don't get aliases */ if (rd->resjunk) continue; - rd->resname = pstrdup(strVal(lfirst(aliaslist))); - aliaslist = lnext(aliaslist); - if (aliaslist == NIL) + rd->resname = pstrdup(strVal(lfirst(alist_item))); + alist_item = lnext(alist_item); + if (alist_item == NULL) break; /* done assigning aliases */ } - if (aliaslist != NIL) + if (alist_item != NULL) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("CREATE VIEW specifies more column " @@ -506,9 +498,9 @@ transformInsertStmt(ParseState *pstate, InsertStmt *stmt, List *sub_namespace; List *icolumns; List *attrnos; - List *icols; /* to become ListCell */ - List *attnos; /* to become ListCell */ - List *tl; + ListCell *icols; + ListCell *attnos; + ListCell *tl; qry->commandType = CMD_INSERT; pstate->p_is_insert = true; @@ -666,14 +658,14 @@ transformInsertStmt(ParseState *pstate, InsertStmt *stmt, /* * Prepare columns for assignment to target table. */ - icols = icolumns; - attnos = attrnos; + icols = list_head(icolumns); + attnos = list_head(attrnos); foreach(tl, qry->targetList) { TargetEntry *tle = (TargetEntry *) lfirst(tl); ResTarget *col; - if (icols == NIL || attnos == NIL) + if (icols == NULL || attnos == NULL) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("INSERT has more expressions than target columns"))); @@ -694,7 +686,7 @@ transformInsertStmt(ParseState *pstate, InsertStmt *stmt, * present in the columns list. Don't do the check unless an explicit * columns list was given, though. */ - if (stmt->cols != NIL && (icols != NIL || attnos != NIL)) + if (stmt->cols != NIL && (icols != NULL || attnos != NULL)) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("INSERT has more target columns than expressions"))); @@ -810,7 +802,7 @@ transformCreateStmt(ParseState *pstate, CreateStmt *stmt, { CreateStmtContext cxt; Query *q; - List *elements; + ListCell *elements; cxt.stmtType = "CREATE TABLE"; cxt.relation = stmt->relation; @@ -895,7 +887,7 @@ transformColumnDefinition(ParseState *pstate, CreateStmtContext *cxt, bool is_serial; bool saw_nullable; Constraint *constraint; - List *clist; + ListCell *clist; cxt->columns = lappend(cxt->columns, column); @@ -903,7 +895,7 @@ transformColumnDefinition(ParseState *pstate, CreateStmtContext *cxt, is_serial = false; if (length(column->typename->names) == 1) { - char *typname = strVal(lfirst(column->typename->names)); + char *typname = strVal(linitial(column->typename->names)); if (strcmp(typname, "serial") == 0 || strcmp(typname, "serial4") == 0) @@ -1256,13 +1248,10 @@ transformInhRelation(ParseState *pstate, CreateStmtContext *cxt, static void transformIndexConstraints(ParseState *pstate, CreateStmtContext *cxt) { - List *listptr; - List *keys; IndexStmt *index; - IndexElem *iparam; - ColumnDef *column; - List *columns; List *indexlist = NIL; + ListCell *listptr; + ListCell *l; /* * Run through the constraints that need to generate an index. For @@ -1273,6 +1262,8 @@ transformIndexConstraints(ParseState *pstate, CreateStmtContext *cxt) foreach(listptr, cxt->ixconstraints) { Constraint *constraint = lfirst(listptr); + ListCell *keys; + IndexElem *iparam; Assert(IsA(constraint, Constraint)); Assert((constraint->contype == CONSTR_PRIMARY) @@ -1317,11 +1308,12 @@ transformIndexConstraints(ParseState *pstate, CreateStmtContext *cxt) { char *key = strVal(lfirst(keys)); bool found = false; + ColumnDef *column = NULL; + ListCell *columns; - column = NULL; foreach(columns, cxt->columns) { - column = lfirst(columns); + column = (ColumnDef *) lfirst(columns); Assert(IsA(column, ColumnDef)); if (strcmp(column->colname, key) == 0) { @@ -1347,11 +1339,11 @@ transformIndexConstraints(ParseState *pstate, CreateStmtContext *cxt) else if (cxt->inhRelations) { /* try inherited tables */ - List *inher; + ListCell *inher; foreach(inher, cxt->inhRelations) { - RangeVar *inh = lfirst(inher); + RangeVar *inh = (RangeVar *) lfirst(inher); Relation rel; int count; @@ -1447,41 +1439,40 @@ transformIndexConstraints(ParseState *pstate, CreateStmtContext *cxt) /* Make sure we keep the PKEY index in preference to others... */ cxt->alist = makeList1(cxt->pkey); } - while (indexlist != NIL) + + foreach(l, indexlist) { - index = lfirst(indexlist); + bool keep = true; + ListCell *k; + + index = lfirst(l); /* if it's pkey, it's already in cxt->alist */ - if (index != cxt->pkey) + if (index == cxt->pkey) + continue; + + foreach(k, cxt->alist) { - bool keep = true; - List *priorlist; + IndexStmt *priorindex = lfirst(k); - foreach(priorlist, cxt->alist) + if (equal(index->indexParams, priorindex->indexParams)) { - IndexStmt *priorindex = lfirst(priorlist); - - if (equal(index->indexParams, priorindex->indexParams)) - { - /* - * If the prior index is as yet unnamed, and this one - * is named, then transfer the name to the prior - * index. This ensures that if we have named and - * unnamed constraints, we'll use (at least one of) - * the names for the index. - */ - if (priorindex->idxname == NULL) - priorindex->idxname = index->idxname; - keep = false; - break; - } + /* + * If the prior index is as yet unnamed, and this one + * is named, then transfer the name to the prior + * index. This ensures that if we have named and + * unnamed constraints, we'll use (at least one of) + * the names for the index. + */ + if (priorindex->idxname == NULL) + priorindex->idxname = index->idxname; + keep = false; + break; } - - if (keep) - cxt->alist = lappend(cxt->alist, index); } - indexlist = lnext(indexlist); + if (keep) + cxt->alist = lappend(cxt->alist, index); } } @@ -1489,7 +1480,7 @@ static void transformFKConstraints(ParseState *pstate, CreateStmtContext *cxt, bool skipValidation, bool isAddConstraint) { - List *fkclist; + ListCell *fkclist; if (cxt->fkconstraints == NIL) return; @@ -1550,7 +1541,7 @@ transformIndexStmt(ParseState *pstate, IndexStmt *stmt) { Query *qry; RangeTblEntry *rte = NULL; - List *l; + ListCell *l; qry = makeNode(Query); qry->commandType = CMD_UTILITY; @@ -1721,15 +1712,15 @@ transformRuleStmt(ParseState *pstate, RuleStmt *stmt, } else { - List *oldactions; + ListCell *l; List *newactions = NIL; /* * transform each statement, like parse_sub_analyze() */ - foreach(oldactions, stmt->actions) + foreach(l, stmt->actions) { - Node *action = (Node *) lfirst(oldactions); + Node *action = (Node *) lfirst(l); ParseState *sub_pstate = make_parsestate(pstate->parentParseState); Query *sub_qry, *top_subqry; @@ -1986,9 +1977,9 @@ transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt) Node *limitCount; List *forUpdate; Node *node; - List *lefttl, - *dtlist, - *targetvars, + ListCell *left_tlist, + *dtlist; + List *targetvars, *targetnames, *sv_namespace, *sv_rtable; @@ -2067,15 +2058,17 @@ transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt) qry->targetList = NIL; targetvars = NIL; targetnames = NIL; - lefttl = leftmostQuery->targetList; + left_tlist = list_head(leftmostQuery->targetList); + foreach(dtlist, sostmt->colTypes) { Oid colType = lfirsto(dtlist); - Resdom *leftResdom = ((TargetEntry *) lfirst(lefttl))->resdom; + Resdom *leftResdom; char *colName; Resdom *resdom; Expr *expr; + leftResdom = ((TargetEntry *) lfirst(left_tlist))->resdom; Assert(!leftResdom->resjunk); colName = pstrdup(leftResdom->resname); resdom = makeResdom((AttrNumber) pstate->p_next_resno++, @@ -2092,7 +2085,7 @@ transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt) makeTargetEntry(resdom, expr)); targetvars = lappend(targetvars, expr); targetnames = lappend(targetnames, makeString(colName)); - lefttl = lnext(lefttl); + left_tlist = lnext(left_tlist); } /* @@ -2239,7 +2232,7 @@ transformSetOperationTree(ParseState *pstate, SelectStmt *stmt) selectList = parse_sub_analyze((Node *) stmt, pstate); Assert(length(selectList) == 1); - selectQuery = (Query *) lfirst(selectList); + selectQuery = (Query *) linitial(selectList); Assert(IsA(selectQuery, Query)); /* @@ -2282,6 +2275,8 @@ transformSetOperationTree(ParseState *pstate, SelectStmt *stmt) SetOperationStmt *op = makeNode(SetOperationStmt); List *lcoltypes; List *rcoltypes; + ListCell *l; + ListCell *r; const char *context; context = (stmt->op == SETOP_UNION ? "UNION" : @@ -2308,18 +2303,17 @@ transformSetOperationTree(ParseState *pstate, SelectStmt *stmt) (errcode(ERRCODE_SYNTAX_ERROR), errmsg("each %s query must have the same number of columns", context))); + op->colTypes = NIL; - while (lcoltypes != NIL) + forboth(l, lcoltypes, r, rcoltypes) { - Oid lcoltype = lfirsto(lcoltypes); - Oid rcoltype = lfirsto(rcoltypes); + Oid lcoltype = lfirsto(l); + Oid rcoltype = lfirsto(r); Oid rescoltype; rescoltype = select_common_type(makeListo2(lcoltype, rcoltype), context); op->colTypes = lappendo(op->colTypes, rescoltype); - lcoltypes = lnext(lcoltypes); - rcoltypes = lnext(rcoltypes); } return (Node *) op; @@ -2339,7 +2333,7 @@ getSetColTypes(ParseState *pstate, Node *node) RangeTblEntry *rte = rt_fetch(rtr->rtindex, pstate->p_rtable); Query *selectQuery = rte->subquery; List *result = NIL; - List *tl; + ListCell *tl; Assert(selectQuery != NULL); /* Get types of non-junk columns */ @@ -2373,22 +2367,25 @@ getSetColTypes(ParseState *pstate, Node *node) static void applyColumnNames(List *dst, List *src) { + ListCell *dst_item = list_head(dst); + ListCell *src_item = list_head(src); + if (length(src) > length(dst)) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("CREATE TABLE AS specifies too many column names"))); - while (src != NIL && dst != NIL) + while (src_item != NULL && dst_item != NULL) { - TargetEntry *d = (TargetEntry *) lfirst(dst); - ColumnDef *s = (ColumnDef *) lfirst(src); + TargetEntry *d = (TargetEntry *) lfirst(dst_item); + ColumnDef *s = (ColumnDef *) lfirst(src_item); Assert(d->resdom && !d->resdom->resjunk); d->resdom->resname = pstrdup(s->colname); - dst = lnext(dst); - src = lnext(src); + dst_item = lnext(dst_item); + src_item = lnext(src_item); } } @@ -2402,8 +2399,8 @@ transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt) { Query *qry = makeNode(Query); Node *qual; - List *origTargetList; - List *tl; + ListCell *origTargetList; + ListCell *tl; qry->commandType = CMD_UPDATE; pstate->p_is_update = true; @@ -2441,7 +2438,8 @@ transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt) pstate->p_next_resno = pstate->p_target_relation->rd_rel->relnatts + 1; /* Prepare non-junk columns for assignment to target table */ - origTargetList = stmt->targetList; + origTargetList = list_head(stmt->targetList); + foreach(tl, qry->targetList) { TargetEntry *tle = (TargetEntry *) lfirst(tl); @@ -2460,7 +2458,7 @@ transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt) resnode->resname = NULL; continue; } - if (origTargetList == NIL) + if (origTargetList == NULL) elog(ERROR, "UPDATE target count mismatch --- internal error"); origTarget = (ResTarget *) lfirst(origTargetList); @@ -2471,7 +2469,7 @@ transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt) origTargetList = lnext(origTargetList); } - if (origTargetList != NIL) + if (origTargetList != NULL) elog(ERROR, "UPDATE target count mismatch --- internal error"); return qry; @@ -2487,7 +2485,7 @@ transformAlterTableStmt(ParseState *pstate, AlterTableStmt *stmt, { CreateStmtContext cxt; Query *qry; - List *lcmd, + ListCell *lcmd, *l; List *newcmds = NIL; bool skipValidation = true; @@ -2687,7 +2685,7 @@ transformPrepareStmt(ParseState *pstate, PrepareStmt *stmt) if (nargs) { - List *l; + ListCell *l; int i = 0; argtoids = (Oid *) palloc(nargs * sizeof(Oid)); @@ -2717,7 +2715,7 @@ transformPrepareStmt(ParseState *pstate, PrepareStmt *stmt) if (length(queries) != 1) elog(ERROR, "unexpected extra stuff in prepared statement"); - stmt->query = lfirst(queries); + stmt->query = linitial(queries); return result; } @@ -2737,7 +2735,7 @@ transformExecuteStmt(ParseState *pstate, ExecuteStmt *stmt) { int nparams = length(stmt->params); int nexpected = length(paramtypes); - List *l; + ListCell *l, *l2; int i = 1; if (nparams != nexpected) @@ -2748,11 +2746,11 @@ transformExecuteStmt(ParseState *pstate, ExecuteStmt *stmt) errdetail("Expected %d parameters but got %d.", nexpected, nparams))); - foreach(l, stmt->params) + forboth(l, stmt->params, l2, paramtypes) { Node *expr = lfirst(l); - Oid expected_type_id, - given_type_id; + Oid expected_type_id = lfirsto(l2); + Oid given_type_id; expr = transformExpr(pstate, expr); @@ -2767,7 +2765,6 @@ transformExecuteStmt(ParseState *pstate, ExecuteStmt *stmt) errmsg("cannot use aggregate function in EXECUTE parameter"))); given_type_id = exprType(expr); - expected_type_id = lfirsto(paramtypes); expr = coerce_to_target_type(pstate, expr, given_type_id, expected_type_id, -1, @@ -2784,8 +2781,6 @@ transformExecuteStmt(ParseState *pstate, ExecuteStmt *stmt) errhint("You will need to rewrite or cast the expression."))); lfirst(l) = expr; - - paramtypes = lnext(paramtypes); i++; } } @@ -2825,13 +2820,13 @@ static void transformForUpdate(Query *qry, List *forUpdate) { List *rowMarks = qry->rowMarks; - List *l; - List *rt; + ListCell *l; + ListCell *rt; Index i; CheckSelectForUpdate(qry); - if (lfirst(forUpdate) == NULL) + if (linitial(forUpdate) == NULL) { /* all regular tables used in query */ i = 0; @@ -2912,7 +2907,7 @@ transformForUpdate(Query *qry, List *forUpdate) break; /* out of foreach loop */ } } - if (rt == NIL) + if (rt == NULL) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_TABLE), errmsg("relation \"%s\" in FOR UPDATE clause not found in FROM clause", @@ -2938,7 +2933,7 @@ transformConstraintAttrs(List *constraintList) Node *lastprimarynode = NULL; bool saw_deferrability = false; bool saw_initially = false; - List *clist; + ListCell *clist; foreach(clist, constraintList) { @@ -3113,7 +3108,7 @@ analyzeCreateSchemaStmt(CreateSchemaStmt *stmt) { CreateSchemaStmtContext cxt; List *result; - List *elements; + ListCell *elements; cxt.stmtType = "CREATE SCHEMA"; cxt.schemaname = stmt->schemaname; diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y index b9becf91dbb..654341ddfe8 100644 --- a/src/backend/parser/gram.y +++ b/src/backend/parser/gram.y @@ -11,7 +11,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.454 2004/05/10 22:44:45 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.455 2004/05/26 04:41:29 neilc Exp $ * * HISTORY * AUTHOR DATE MAJOR EVENT @@ -2385,15 +2385,15 @@ DefineStmt: case 1: r->catalogname = NULL; r->schemaname = NULL; - r->relname = strVal(lfirst($3)); + r->relname = strVal(linitial($3)); break; case 2: r->catalogname = NULL; - r->schemaname = strVal(lfirst($3)); + r->schemaname = strVal(linitial($3)); r->relname = strVal(lsecond($3)); break; case 3: - r->catalogname = strVal(lfirst($3)); + r->catalogname = strVal(linitial($3)); r->schemaname = strVal(lsecond($3)); r->relname = strVal(lthird($3)); break; @@ -6020,7 +6020,7 @@ a_expr: c_expr { $$ = $1; } else { Node *n = NULL; - List *l; + ListCell *l; foreach(l, (List *) $3) { Node *cmp; @@ -6052,7 +6052,7 @@ a_expr: c_expr { $$ = $1; } else { Node *n = NULL; - List *l; + ListCell *l; foreach(l, (List *) $4) { Node *cmp; @@ -7168,11 +7168,11 @@ qualified_name: { case 2: $$->catalogname = NULL; - $$->schemaname = strVal(lfirst($1)); + $$->schemaname = strVal(linitial($1)); $$->relname = strVal(lsecond($1)); break; case 3: - $$->catalogname = strVal(lfirst($1)); + $$->catalogname = strVal(linitial($1)); $$->schemaname = strVal(lsecond($1)); $$->relname = strVal(lthird($1)); break; @@ -7864,8 +7864,8 @@ makeBoolAConst(bool state) static Node * makeRowNullTest(NullTestType test, RowExpr *row) { - Node *result = NULL; - List *arg; + Node *result = NULL; + ListCell *arg; foreach(arg, row->args) { @@ -7928,7 +7928,7 @@ static List * extractArgTypes(List *parameters) { List *result = NIL; - List *i; + ListCell *i; foreach(i, parameters) { diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c index 6f85595610d..e9c73ee946a 100644 --- a/src/backend/parser/parse_agg.c +++ b/src/backend/parser/parse_agg.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/parser/parse_agg.c,v 1.61 2004/01/28 07:46:44 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/parser/parse_agg.c,v 1.62 2004/05/26 04:41:29 neilc Exp $ * *------------------------------------------------------------------------- */ @@ -99,7 +99,7 @@ parseCheckAggregates(ParseState *pstate, Query *qry) { List *groupClauses = NIL; bool have_non_var_grouping; - List *lst; + ListCell *l; bool hasJoinRTEs; Node *clause; @@ -129,9 +129,9 @@ parseCheckAggregates(ParseState *pstate, Query *qry) * While we are at it, build a list of the acceptable GROUP BY * expressions for use by check_ungrouped_columns(). */ - foreach(lst, qry->groupClause) + foreach(l, qry->groupClause) { - GroupClause *grpcl = (GroupClause *) lfirst(lst); + GroupClause *grpcl = (GroupClause *) lfirst(l); Node *expr; expr = get_sortgroupclause_expr(grpcl, qry->targetList); @@ -151,9 +151,9 @@ parseCheckAggregates(ParseState *pstate, Query *qry) * no rangetable entries are RTE_JOIN kind. */ hasJoinRTEs = false; - foreach(lst, pstate->p_rtable) + foreach(l, pstate->p_rtable) { - RangeTblEntry *rte = (RangeTblEntry *) lfirst(lst); + RangeTblEntry *rte = (RangeTblEntry *) lfirst(l); if (rte->rtekind == RTE_JOIN) { @@ -172,9 +172,9 @@ parseCheckAggregates(ParseState *pstate, Query *qry) * recursive scans. (Note we have to flatten aliases before this.) */ have_non_var_grouping = false; - foreach(lst, groupClauses) + foreach(l, groupClauses) { - if (!IsA((Node *) lfirst(lst), Var)) + if (!IsA((Node *) lfirst(l), Var)) { have_non_var_grouping = true; break; @@ -236,7 +236,7 @@ static bool check_ungrouped_columns_walker(Node *node, check_ungrouped_columns_context *context) { - List *gl; + ListCell *gl; if (node == NULL) return false; diff --git a/src/backend/parser/parse_clause.c b/src/backend/parser/parse_clause.c index 2c6e9942899..610c4fa2960 100644 --- a/src/backend/parser/parse_clause.c +++ b/src/backend/parser/parse_clause.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/parser/parse_clause.c,v 1.129 2004/05/23 17:10:54 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/parser/parse_clause.c,v 1.130 2004/05/26 04:41:29 neilc Exp $ * *------------------------------------------------------------------------- */ @@ -79,7 +79,7 @@ static TargetEntry *findTargetlistEntry(ParseState *pstate, Node *node, void transformFromClause(ParseState *pstate, List *frmList) { - List *fl; + ListCell *fl; /* * The grammar will have produced a list of RangeVars, @@ -231,14 +231,15 @@ extractRemainingColumns(List *common_colnames, { List *new_colnames = NIL; List *new_colvars = NIL; - List *lnames, - *lvars = src_colvars; + ListCell *lnames, *lvars; - foreach(lnames, src_colnames) + Assert(length(src_colnames) == length(src_colvars)); + + forboth(lnames, src_colnames, lvars, src_colvars) { char *colname = strVal(lfirst(lnames)); bool match = false; - List *cnames; + ListCell *cnames; foreach(cnames, common_colnames) { @@ -256,8 +257,6 @@ extractRemainingColumns(List *common_colnames, new_colnames = lappend(new_colnames, lfirst(lnames)); new_colvars = lappend(new_colvars, lfirst(lvars)); } - - lvars = lnext(lvars); } *res_colnames = new_colnames; @@ -273,8 +272,7 @@ static Node * transformJoinUsingClause(ParseState *pstate, List *leftVars, List *rightVars) { Node *result = NULL; - List *lvars, - *rvars = rightVars; + ListCell *lvars, *rvars; /* * We cheat a little bit here by building an untransformed operator @@ -282,7 +280,7 @@ transformJoinUsingClause(ParseState *pstate, List *leftVars, List *rightVars) * because transformExpr() won't complain about already-transformed * subnodes. */ - foreach(lvars, leftVars) + forboth(lvars, leftVars, rvars, rightVars) { Node *lvar = (Node *) lfirst(lvars); Node *rvar = (Node *) lfirst(rvars); @@ -299,8 +297,6 @@ transformJoinUsingClause(ParseState *pstate, List *leftVars, List *rightVars) a = makeA_Expr(AEXPR_AND, NIL, result, (Node *) e); result = (Node *) a; } - - rvars = lnext(rvars); } /* @@ -314,7 +310,7 @@ transformJoinUsingClause(ParseState *pstate, List *leftVars, List *rightVars) result = coerce_to_boolean(pstate, result, "JOIN/USING"); return result; -} /* transformJoinUsingClause() */ +} /* transformJoinOnClause() * Transform the qual conditions for JOIN/ON. @@ -435,7 +431,7 @@ transformRangeSubselect(ParseState *pstate, RangeSubselect *r) */ if (length(parsetrees) != 1) elog(ERROR, "unexpected parse analysis result for subquery in FROM"); - query = (Query *) lfirst(parsetrees); + query = (Query *) linitial(parsetrees); if (query == NULL || !IsA(query, Query)) elog(ERROR, "unexpected parse analysis result for subquery in FROM"); @@ -686,7 +682,7 @@ transformFromClauseItem(ParseState *pstate, Node *n, List **containedRels) if (j->isNatural) { List *rlist = NIL; - List *lx, + ListCell *lx, *rx; Assert(j->using == NIL); /* shouldn't have USING() too */ @@ -731,14 +727,14 @@ transformFromClauseItem(ParseState *pstate, Node *n, List **containedRels) List *ucols = j->using; List *l_usingvars = NIL; List *r_usingvars = NIL; - List *ucol; + ListCell *ucol; Assert(j->quals == NULL); /* shouldn't have ON() too */ foreach(ucol, ucols) { char *u_colname = strVal(lfirst(ucol)); - List *col; + ListCell *col; int ndx; int l_index = -1; int r_index = -1; @@ -1083,7 +1079,7 @@ static TargetEntry * findTargetlistEntry(ParseState *pstate, Node *node, List **tlist, int clause) { TargetEntry *target_result = NULL; - List *tl; + ListCell *tl; Node *expr; /*---------- @@ -1129,7 +1125,7 @@ findTargetlistEntry(ParseState *pstate, Node *node, List **tlist, int clause) length(((ColumnRef *) node)->fields) == 1 && ((ColumnRef *) node)->indirection == NIL) { - char *name = strVal(lfirst(((ColumnRef *) node)->fields)); + char *name = strVal(linitial(((ColumnRef *) node)->fields)); if (clause == GROUP_CLAUSE) { @@ -1255,8 +1251,11 @@ List * transformGroupClause(ParseState *pstate, List *grouplist, List **targetlist, List *sortClause) { - List *glist = NIL, - *gl; + List *glist = NIL; + ListCell *gl; + ListCell *sortItem; + + sortItem = list_head(sortClause); foreach(gl, grouplist) { @@ -1293,17 +1292,17 @@ transformGroupClause(ParseState *pstate, List *grouplist, * any user-supplied ordering operator will bring equal values * together, which is all that GROUP BY needs. */ - if (sortClause && - ((SortClause *) lfirst(sortClause))->tleSortGroupRef == + if (sortItem && + ((SortClause *) lfirst(sortItem))->tleSortGroupRef == tle->resdom->ressortgroupref) { - ordering_op = ((SortClause *) lfirst(sortClause))->sortop; - sortClause = lnext(sortClause); + ordering_op = ((SortClause *) lfirst(sortItem))->sortop; + sortItem = lnext(sortItem); } else { ordering_op = ordering_oper_opid(restype); - sortClause = NIL; /* disregard ORDER BY once match fails */ + sortItem = NULL; /* disregard ORDER BY once match fails */ } grpcl = makeNode(GroupClause); @@ -1329,7 +1328,7 @@ transformSortClause(ParseState *pstate, bool resolveUnknown) { List *sortlist = NIL; - List *olitem; + ListCell *olitem; foreach(olitem, orderlist) { @@ -1361,14 +1360,14 @@ transformDistinctClause(ParseState *pstate, List *distinctlist, List **targetlist, List **sortClause) { List *result = NIL; - List *slitem; - List *dlitem; + ListCell *slitem; + ListCell *dlitem; /* No work if there was no DISTINCT clause */ if (distinctlist == NIL) return NIL; - if (lfirst(distinctlist) == NIL) + if (linitial(distinctlist) == NULL) { /* We had SELECT DISTINCT */ @@ -1423,7 +1422,7 @@ transformDistinctClause(ParseState *pstate, List *distinctlist, * match in any order, but implementing that check seems like more * trouble than it's worth. */ - List *nextsortlist = *sortClause; + ListCell *nextsortlist = list_head(*sortClause); foreach(dlitem, distinctlist) { @@ -1432,7 +1431,7 @@ transformDistinctClause(ParseState *pstate, List *distinctlist, tle = findTargetlistEntry(pstate, lfirst(dlitem), targetlist, DISTINCT_ON_CLAUSE); - if (nextsortlist != NIL) + if (nextsortlist != NULL) { SortClause *scl = (SortClause *) lfirst(nextsortlist); @@ -1463,7 +1462,7 @@ transformDistinctClause(ParseState *pstate, List *distinctlist, break; } } - if (slitem == NIL) /* should not happen */ + if (slitem == NULL) /* should not happen */ elog(ERROR, "failed to add DISTINCT ON clause to target list"); } } @@ -1486,11 +1485,11 @@ List * addAllTargetsToSortList(ParseState *pstate, List *sortlist, List *targetlist, bool resolveUnknown) { - List *i; + ListCell *l; - foreach(i, targetlist) + foreach(l, targetlist) { - TargetEntry *tle = (TargetEntry *) lfirst(i); + TargetEntry *tle = (TargetEntry *) lfirst(l); if (!tle->resdom->resjunk) sortlist = addTargetToSortList(pstate, tle, @@ -1575,7 +1574,7 @@ Index assignSortGroupRef(TargetEntry *tle, List *tlist) { Index maxRef; - List *l; + ListCell *l; if (tle->resdom->ressortgroupref) /* already has one? */ return tle->resdom->ressortgroupref; @@ -1605,15 +1604,15 @@ bool targetIsInSortList(TargetEntry *tle, List *sortList) { Index ref = tle->resdom->ressortgroupref; - List *i; + ListCell *l; /* no need to scan list if tle has no marker */ if (ref == 0) return false; - foreach(i, sortList) + foreach(l, sortList) { - SortClause *scl = (SortClause *) lfirst(i); + SortClause *scl = (SortClause *) lfirst(l); if (scl->tleSortGroupRef == ref) return true; diff --git a/src/backend/parser/parse_coerce.c b/src/backend/parser/parse_coerce.c index c7a8c3c83bb..ba3210946ab 100644 --- a/src/backend/parser/parse_coerce.c +++ b/src/backend/parser/parse_coerce.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/parser/parse_coerce.c,v 2.115 2004/05/10 22:44:46 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/parser/parse_coerce.c,v 2.116 2004/05/26 04:41:30 neilc Exp $ * *------------------------------------------------------------------------- */ @@ -546,7 +546,7 @@ coerce_record_to_complex(ParseState *pstate, Node *node, List *args = NIL; List *newargs; int i; - List *arg; + ListCell *arg; if (node && IsA(node, RowExpr)) { @@ -720,14 +720,15 @@ select_common_type(List *typeids, const char *context) { Oid ptype; CATEGORY pcategory; - List *l; + ListCell *type_item; Assert(typeids != NIL); - ptype = getBaseType(lfirsto(typeids)); + ptype = getBaseType(linitial_oid(typeids)); pcategory = TypeCategory(ptype); - foreach(l, lnext(typeids)) + + for_each_cell(type_item, lnext(list_head(typeids))) { - Oid ntype = getBaseType(lfirsto(l)); + Oid ntype = getBaseType(lfirsto(type_item)); /* move on to next one if no new information... */ if ((ntype != InvalidOid) && (ntype != UNKNOWNOID) && (ntype != ptype)) @@ -746,7 +747,6 @@ select_common_type(List *typeids, const char *context) */ ereport(ERROR, (errcode(ERRCODE_DATATYPE_MISMATCH), - /* * translator: first %s is name of a SQL construct, eg * CASE diff --git a/src/backend/parser/parse_expr.c b/src/backend/parser/parse_expr.c index d4a8cdcc8b6..9efebdfdb48 100644 --- a/src/backend/parser/parse_expr.c +++ b/src/backend/parser/parse_expr.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/parser/parse_expr.c,v 1.170 2004/05/10 22:44:46 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/parser/parse_expr.c,v 1.171 2004/05/26 04:41:30 neilc Exp $ * *------------------------------------------------------------------------- */ @@ -100,7 +100,7 @@ transformExpr(ParseState *pstate, Node *expr) int paramno = pref->number; ParseState *toppstate; Param *param; - List *fields; + ListCell *fields; /* * Find topmost ParseState, which is where paramtype info @@ -176,7 +176,7 @@ transformExpr(ParseState *pstate, Node *expr) case T_ExprFieldSelect: { ExprFieldSelect *efs = (ExprFieldSelect *) expr; - List *fields; + ListCell *fields; result = transformExpr(pstate, efs->arg); /* handle qualification, if any */ @@ -219,7 +219,7 @@ transformExpr(ParseState *pstate, Node *expr) */ if (Transform_null_equals && length(a->name) == 1 && - strcmp(strVal(lfirst(a->name)), "=") == 0 && + strcmp(strVal(linitial(a->name)), "=") == 0 && (exprIsNullConstant(lexpr) || exprIsNullConstant(rexpr))) { @@ -396,7 +396,7 @@ transformExpr(ParseState *pstate, Node *expr) * Checking an expression for match to type. * Will result in a boolean constant node. */ - List *telem; + ListCell *telem; A_Const *n; Oid ltype, rtype; @@ -418,7 +418,7 @@ transformExpr(ParseState *pstate, Node *expr) * Flip the sense of the result for not * equals. */ - if (strcmp(strVal(lfirst(a->name)), "!=") == 0) + if (strcmp(strVal(linitial(a->name)), "!=") == 0) matched = (!matched); n = makeNode(A_Const); @@ -436,12 +436,15 @@ transformExpr(ParseState *pstate, Node *expr) { FuncCall *fn = (FuncCall *) expr; List *targs; - List *args; + ListCell *args; /* * Transform the list of arguments. We use a shallow list * copy and then transform-in-place to avoid O(N^2) * behavior from repeated lappend's. + * + * XXX: repeated lappend() would no longer result in + * O(n^2) behavior; worth reconsidering this design? */ targs = listCopy(fn->args); foreach(args, targs) @@ -473,7 +476,7 @@ transformExpr(ParseState *pstate, Node *expr) qtrees = parse_sub_analyze(sublink->subselect, pstate); if (length(qtrees) != 1) elog(ERROR, "bad query in sub-select"); - qtree = (Query *) lfirst(qtrees); + qtree = (Query *) linitial(qtrees); if (qtree->commandType != CMD_SELECT || qtree->resultRelation != 0) elog(ERROR, "bad query in sub-select"); @@ -493,20 +496,20 @@ transformExpr(ParseState *pstate, Node *expr) else if (sublink->subLinkType == EXPR_SUBLINK || sublink->subLinkType == ARRAY_SUBLINK) { - List *tlist = qtree->targetList; + ListCell *tlist_item = list_head(qtree->targetList); /* * Make sure the subselect delivers a single column * (ignoring resjunk targets). */ - if (tlist == NIL || - ((TargetEntry *) lfirst(tlist))->resdom->resjunk) + if (tlist_item == NULL || + ((TargetEntry *) lfirst(tlist_item))->resdom->resjunk) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("subquery must return a column"))); - while ((tlist = lnext(tlist)) != NIL) + while ((tlist_item = lnext(tlist_item)) != NULL) { - if (!((TargetEntry *) lfirst(tlist))->resdom->resjunk) + if (!((TargetEntry *) lfirst(tlist_item))->resdom->resjunk) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("subquery must return only one column"))); @@ -531,11 +534,12 @@ transformExpr(ParseState *pstate, Node *expr) bool needNot = false; List *op = sublink->operName; char *opname = strVal(llast(op)); - List *elist; + ListCell *l; + ListCell *ll_item; /* transform lefthand expressions */ - foreach(elist, left_list) - lfirst(elist) = transformExpr(pstate, lfirst(elist)); + foreach(l, left_list) + lfirst(l) = transformExpr(pstate, lfirst(l)); /* * If the expression is "<> ALL" (with unqualified @@ -578,23 +582,23 @@ transformExpr(ParseState *pstate, Node *expr) */ sublink->operOids = NIL; - while (right_list != NIL) + ll_item = list_head(left_list); + foreach(l, right_list) { - TargetEntry *tent = (TargetEntry *) lfirst(right_list); + TargetEntry *tent = (TargetEntry *) lfirst(l); Node *lexpr; Operator optup; Form_pg_operator opform; - right_list = lnext(right_list); if (tent->resdom->resjunk) continue; - if (left_list == NIL) + if (ll_item == NULL) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("subquery has too many columns"))); - lexpr = lfirst(left_list); - left_list = lnext(left_list); + lexpr = lfirst(ll_item); + ll_item = lnext(ll_item); /* * It's OK to use oper() not compatible_oper() @@ -627,7 +631,7 @@ transformExpr(ParseState *pstate, Node *expr) ReleaseSysCache(optup); } - if (left_list != NIL) + if (ll_item != NULL) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("subquery has too few columns"))); @@ -651,7 +655,7 @@ transformExpr(ParseState *pstate, Node *expr) CaseTestExpr *placeholder; List *newargs; List *typeids; - List *args; + ListCell *l; Node *defresult; Oid ptype; @@ -679,9 +683,9 @@ transformExpr(ParseState *pstate, Node *expr) /* transform the list of arguments */ newargs = NIL; typeids = NIL; - foreach(args, c->args) + foreach(l, c->args) { - CaseWhen *w = (CaseWhen *) lfirst(args); + CaseWhen *w = (CaseWhen *) lfirst(l); CaseWhen *neww = makeNode(CaseWhen); Node *warg; @@ -741,9 +745,9 @@ transformExpr(ParseState *pstate, Node *expr) "CASE/ELSE"); /* Convert when-clause results, if necessary */ - foreach(args, newc->args) + foreach(l, newc->args) { - CaseWhen *w = (CaseWhen *) lfirst(args); + CaseWhen *w = (CaseWhen *) lfirst(l); w->result = (Expr *) coerce_to_common_type(pstate, @@ -763,7 +767,7 @@ transformExpr(ParseState *pstate, Node *expr) List *newelems = NIL; List *newcoercedelems = NIL; List *typeids = NIL; - List *element; + ListCell *element; Oid array_type; Oid element_type; @@ -827,7 +831,7 @@ transformExpr(ParseState *pstate, Node *expr) RowExpr *r = (RowExpr *) expr; RowExpr *newr = makeNode(RowExpr); List *newargs = NIL; - List *arg; + ListCell *arg; /* Transform the field expressions */ foreach(arg, r->args) @@ -855,7 +859,7 @@ transformExpr(ParseState *pstate, Node *expr) List *newargs = NIL; List *newcoercedargs = NIL; List *typeids = NIL; - List *args; + ListCell *args; foreach(args, c->args) { @@ -1024,7 +1028,7 @@ transformColumnRef(ParseState *pstate, ColumnRef *cref) { case 1: { - char *name = strVal(lfirst(cref->fields)); + char *name = strVal(linitial(cref->fields)); /* Try to identify as an unqualified column */ node = colNameToVar(pstate, name, false); @@ -1070,7 +1074,7 @@ transformColumnRef(ParseState *pstate, ColumnRef *cref) } case 2: { - char *name1 = strVal(lfirst(cref->fields)); + char *name1 = strVal(linitial(cref->fields)); char *name2 = strVal(lsecond(cref->fields)); /* Whole-row reference? */ @@ -1099,7 +1103,7 @@ transformColumnRef(ParseState *pstate, ColumnRef *cref) } case 3: { - char *name1 = strVal(lfirst(cref->fields)); + char *name1 = strVal(linitial(cref->fields)); char *name2 = strVal(lsecond(cref->fields)); char *name3 = strVal(lthird(cref->fields)); @@ -1125,7 +1129,7 @@ transformColumnRef(ParseState *pstate, ColumnRef *cref) } case 4: { - char *name1 = strVal(lfirst(cref->fields)); + char *name1 = strVal(linitial(cref->fields)); char *name2 = strVal(lsecond(cref->fields)); char *name3 = strVal(lthird(cref->fields)); char *name4 = strVal(lfourth(cref->fields)); @@ -1316,7 +1320,7 @@ exprType(Node *expr) if (!qtree || !IsA(qtree, Query)) elog(ERROR, "cannot get type for untransformed sublink"); - tent = (TargetEntry *) lfirst(qtree->targetList); + tent = (TargetEntry *) linitial(qtree->targetList); Assert(IsA(tent, TargetEntry)); Assert(!tent->resdom->resjunk); if (sublink->subLinkType == EXPR_SUBLINK) @@ -1355,7 +1359,7 @@ exprType(Node *expr) /* get the type of the subselect's first target column */ TargetEntry *tent; - tent = (TargetEntry *) lfirst(subplan->plan->targetlist); + tent = (TargetEntry *) linitial(subplan->plan->targetlist); Assert(IsA(tent, TargetEntry)); Assert(!tent->resdom->resjunk); if (subplan->subLinkType == EXPR_SUBLINK) @@ -1403,7 +1407,7 @@ exprType(Node *expr) type = ((CoalesceExpr *) expr)->coalescetype; break; case T_NullIfExpr: - type = exprType((Node *) lfirst(((NullIfExpr *) expr)->args)); + type = exprType((Node *) linitial(((NullIfExpr *) expr)->args)); break; case T_NullTest: type = BOOLOID; @@ -1481,7 +1485,7 @@ exprTypmod(Node *expr) CaseExpr *cexpr = (CaseExpr *) expr; Oid casetype = cexpr->casetype; int32 typmod; - List *arg; + ListCell *arg; if (!cexpr->defresult) return -1; @@ -1514,9 +1518,9 @@ exprTypmod(Node *expr) CoalesceExpr *cexpr = (CoalesceExpr *) expr; Oid coalescetype = cexpr->coalescetype; int32 typmod; - List *arg; + ListCell *arg; - typmod = exprTypmod((Node *) lfirst(cexpr->args)); + typmod = exprTypmod((Node *) linitial(cexpr->args)); foreach(arg, cexpr->args) { Node *e = (Node *) lfirst(arg); @@ -1533,7 +1537,7 @@ exprTypmod(Node *expr) { NullIfExpr *nexpr = (NullIfExpr *) expr; - return exprTypmod((Node *) lfirst(nexpr->args)); + return exprTypmod((Node *) linitial(nexpr->args)); } break; case T_CoerceToDomain: @@ -1644,8 +1648,8 @@ make_row_op(ParseState *pstate, List *opname, Node *ltree, Node *rtree) *rrow; List *largs, *rargs; - List *largl, - *rargl; + ListCell *l, + *r; char *oprname; BoolExprType boolop; @@ -1690,15 +1694,12 @@ make_row_op(ParseState *pstate, List *opname, Node *ltree, Node *rtree) boolop = 0; /* keep compiler quiet */ } - /* XXX use forboth */ - rargl = rargs; - foreach(largl, largs) + forboth(l, largs, r, rargs) { - Node *larg = (Node *) lfirst(largl); - Node *rarg = (Node *) lfirst(rargl); + Node *larg = (Node *) lfirst(l); + Node *rarg = (Node *) lfirst(r); Node *cmp; - rargl = lnext(rargl); cmp = (Node *) make_op(pstate, opname, larg, rarg); cmp = coerce_to_boolean(pstate, cmp, "row comparison"); if (result == NULL) @@ -1732,8 +1733,8 @@ make_row_distinct_op(ParseState *pstate, List *opname, *rrow; List *largs, *rargs; - List *largl, - *rargl; + ListCell *l, + *r; /* Inputs are untransformed RowExprs */ lrow = (RowExpr *) transformExpr(pstate, ltree); @@ -1748,15 +1749,12 @@ make_row_distinct_op(ParseState *pstate, List *opname, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("unequal number of entries in row expression"))); - /* XXX use forboth */ - rargl = rargs; - foreach(largl, largs) + forboth(l, largs, r, rargs) { - Node *larg = (Node *) lfirst(largl); - Node *rarg = (Node *) lfirst(rargl); + Node *larg = (Node *) lfirst(l); + Node *rarg = (Node *) lfirst(r); Node *cmp; - rargl = lnext(rargl); cmp = (Node *) make_distinct_op(pstate, opname, larg, rarg); if (result == NULL) result = cmp; diff --git a/src/backend/parser/parse_func.c b/src/backend/parser/parse_func.c index 64da3bb8425..2b48ef488d7 100644 --- a/src/backend/parser/parse_func.c +++ b/src/backend/parser/parse_func.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/parser/parse_func.c,v 1.168 2004/04/02 21:30:44 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/parser/parse_func.c,v 1.169 2004/05/26 04:41:30 neilc Exp $ * *------------------------------------------------------------------------- */ @@ -67,7 +67,7 @@ ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs, { Oid rettype; Oid funcid; - List *i; + ListCell *l; Node *first_arg = NULL; int nargs = length(fargs); int argn; @@ -91,7 +91,7 @@ ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs, if (fargs) { - first_arg = lfirst(fargs); + first_arg = linitial(fargs); Assert(first_arg != NULL); } @@ -108,7 +108,7 @@ ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs, if (argtype == RECORDOID || ISCOMPLEX(argtype)) { retval = ParseComplexProjection(pstate, - strVal(lfirst(funcname)), + strVal(linitial(funcname)), first_arg); if (retval) return retval; @@ -126,9 +126,9 @@ ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs, MemSet(actual_arg_types, 0, FUNC_MAX_ARGS * sizeof(Oid)); argn = 0; - foreach(i, fargs) + foreach(l, fargs) { - Node *arg = lfirst(i); + Node *arg = lfirst(l); actual_arg_types[argn++] = exprType(arg); } @@ -149,8 +149,8 @@ ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs, * We can do it as a trivial coercion. coerce_type can handle * these cases, so why duplicate code... */ - return coerce_type(pstate, lfirst(fargs), actual_arg_types[0], - rettype, + return coerce_type(pstate, linitial(fargs), + actual_arg_types[0], rettype, COERCION_EXPLICIT, COERCE_EXPLICIT_CALL); } else if (fdresult == FUNCDETAIL_NORMAL) @@ -183,7 +183,7 @@ ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs, { Assert(nargs == 1); Assert(length(funcname) == 1); - unknown_attribute(pstate, first_arg, strVal(lfirst(funcname))); + unknown_attribute(pstate, first_arg, strVal(linitial(funcname))); } /* @@ -240,7 +240,7 @@ ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs, aggref->aggfnoid = funcid; aggref->aggtype = rettype; - aggref->target = lfirst(fargs); + aggref->target = linitial(fargs); aggref->aggstar = agg_star; aggref->aggdistinct = agg_distinct; @@ -725,7 +725,7 @@ func_get_detail(List *funcname, !ISCOMPLEX(targetType)) { Oid sourceType = argtypes[0]; - Node *arg1 = lfirst(fargs); + Node *arg1 = linitial(fargs); if ((sourceType == UNKNOWNOID && IsA(arg1, Const)) || (find_coercion_pathway(targetType, sourceType, @@ -896,37 +896,51 @@ static int find_inheritors(Oid relid, Oid **supervec) { Relation inhrel; - HeapScanDesc inhscan; - ScanKeyData skey; - HeapTuple inhtup; - Oid *relidvec; int nvisited; List *visited, *queue; - List *elt; - bool newrelid; + ListCell *queue_item; - nvisited = 0; - queue = NIL; + /* + * Begin the search at the relation itself, so add relid to the + * queue. + */ + queue = list_make1_oid(relid); visited = NIL; inhrel = heap_openr(InheritsRelationName, AccessShareLock); /* - * Use queue to do a breadth-first traversal of the inheritance graph - * from the relid supplied up to the root. At the top of the loop, - * relid is the OID of the reltype to check next, queue is the list of - * pending relids to check after this one, and visited is the list of - * relids we need to output. + * Use queue to do a breadth-first traversal of the inheritance + * graph from the relid supplied up to the root. Notice that we + * append to the queue inside the loop --- this is okay because + * the foreach() macro doesn't advance queue_item until the next + * loop iteration begins. */ - do + foreach(queue_item, queue) { - /* find all types this relid inherits from, and add them to queue */ + Oid this_relid = lfirst_oid(queue_item); + ScanKeyData skey; + HeapScanDesc inhscan; + HeapTuple inhtup; + + /* If we've seen this relid already, skip it */ + if (list_member_oid(visited, this_relid)) + continue; + + /* + * Okay, this is a not-yet-seen relid. Add it to the list of + * already-visited OIDs, then find all the types this relid + * inherits from and add them to the queue. The one exception + * is we don't add the original relation to 'visited'. + */ + if (queue_item != list_head(queue)) + visited = lappend_oid(visited, this_relid); ScanKeyInit(&skey, Anum_pg_inherits_inhrelid, BTEqualStrategyNumber, F_OIDEQ, - ObjectIdGetDatum(relid)); + ObjectIdGetDatum(this_relid)); inhscan = heap_beginscan(inhrel, SnapshotNow, 1, &skey); @@ -934,54 +948,34 @@ find_inheritors(Oid relid, Oid **supervec) { Form_pg_inherits inh = (Form_pg_inherits) GETSTRUCT(inhtup); - queue = lappendo(queue, inh->inhparent); + queue = lappend_oid(queue, inh->inhparent); } heap_endscan(inhscan); - - /* pull next unvisited relid off the queue */ - - newrelid = false; - while (queue != NIL) - { - relid = lfirsto(queue); - queue = lnext(queue); - if (!oidMember(relid, visited)) - { - newrelid = true; - break; - } - } - - if (newrelid) - { - visited = lappendo(visited, relid); - nvisited++; - } - } while (newrelid); + } heap_close(inhrel, AccessShareLock); + nvisited = list_length(visited); if (nvisited > 0) { - relidvec = (Oid *) palloc(nvisited * sizeof(Oid)); + Oid *relidvec; + ListCell *l; + + relidvec = (Oid *) palloc(nvisited * sizeof(*relidvec)); *supervec = relidvec; - foreach(elt, visited) + foreach(l, visited) { /* return the type id, rather than the relation id */ - *relidvec++ = get_rel_type_id(lfirsto(elt)); + *relidvec++ = get_rel_type_id(lfirst_oid(l)); } } else *supervec = NULL; freeList(visited); - - /* - * there doesn't seem to be any equally easy way to release the queue - * list cells, but since they're palloc'd space it's not critical. - */ + freeList(queue); return nvisited; } @@ -1117,7 +1111,7 @@ make_fn_arguments(ParseState *pstate, Oid *actual_arg_types, Oid *declared_arg_types) { - List *current_fargs; + ListCell *current_fargs; int i = 0; foreach(current_fargs, fargs) @@ -1403,6 +1397,7 @@ LookupFuncNameTypeNames(List *funcname, List *argtypes, bool noError) Oid argoids[FUNC_MAX_ARGS]; int argcount; int i; + ListCell *args_item; MemSet(argoids, 0, FUNC_MAX_ARGS * sizeof(Oid)); argcount = length(argtypes); @@ -1412,9 +1407,10 @@ LookupFuncNameTypeNames(List *funcname, List *argtypes, bool noError) errmsg("functions cannot have more than %d arguments", FUNC_MAX_ARGS))); + args_item = list_head(argtypes); for (i = 0; i < argcount; i++) { - TypeName *t = (TypeName *) lfirst(argtypes); + TypeName *t = (TypeName *) lfirst(args_item); argoids[i] = LookupTypeName(t); @@ -1424,7 +1420,7 @@ LookupFuncNameTypeNames(List *funcname, List *argtypes, bool noError) errmsg("type \"%s\" does not exist", TypeNameToString(t)))); - argtypes = lnext(argtypes); + args_item = lnext(args_item); } return LookupFuncName(funcname, argcount, argoids, noError); diff --git a/src/backend/parser/parse_node.c b/src/backend/parser/parse_node.c index 00b4c8e513e..c46c27481a5 100644 --- a/src/backend/parser/parse_node.c +++ b/src/backend/parser/parse_node.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/parser/parse_node.c,v 1.82 2003/11/29 19:51:52 pgsql Exp $ + * $PostgreSQL: pgsql/src/backend/parser/parse_node.c,v 1.83 2004/05/26 04:41:30 neilc Exp $ * *------------------------------------------------------------------------- */ @@ -106,7 +106,7 @@ transformArraySubscripts(ParseState *pstate, bool isSlice = forceSlice; List *upperIndexpr = NIL; List *lowerIndexpr = NIL; - List *idx; + ListCell *idx; ArrayRef *aref; /* Get the type tuple for the array */ diff --git a/src/backend/parser/parse_relation.c b/src/backend/parser/parse_relation.c index de4bf093ee9..d7d50e187e3 100644 --- a/src/backend/parser/parse_relation.c +++ b/src/backend/parser/parse_relation.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/parser/parse_relation.c,v 1.94 2004/04/18 18:12:58 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/parser/parse_relation.c,v 1.95 2004/05/26 04:41:30 neilc Exp $ * *------------------------------------------------------------------------- */ @@ -140,7 +140,7 @@ scanNameSpaceForRefname(ParseState *pstate, Node *nsnode, return NULL; if (IsA(nsnode, RangeTblRef)) { - int varno = ((RangeTblRef *) nsnode)->rtindex; + int varno = ((RangeTblRef *) nsnode)->rtindex; RangeTblEntry *rte = rt_fetch(varno, pstate->p_rtable); if (strcmp(rte->eref->aliasname, refname) == 0) @@ -174,7 +174,7 @@ scanNameSpaceForRefname(ParseState *pstate, Node *nsnode, } else if (IsA(nsnode, List)) { - List *l; + ListCell *l; foreach(l, (List *) nsnode) { @@ -249,7 +249,7 @@ scanNameSpaceForRelid(ParseState *pstate, Node *nsnode, Oid relid) } else if (IsA(nsnode, List)) { - List *l; + ListCell *l; foreach(l, (List *) nsnode) { @@ -321,7 +321,7 @@ checkNameSpaceConflicts(ParseState *pstate, Node *namespace1, } else if (IsA(namespace1, List)) { - List *l; + ListCell *l; foreach(l, (List *) namespace1) checkNameSpaceConflicts(pstate, lfirst(l), namespace2); @@ -378,7 +378,7 @@ scanNameSpaceForConflict(ParseState *pstate, Node *nsnode, } else if (IsA(nsnode, List)) { - List *l; + ListCell *l; foreach(l, (List *) nsnode) scanNameSpaceForConflict(pstate, lfirst(l), rte1, aliasname1); @@ -397,7 +397,7 @@ int RTERangeTablePosn(ParseState *pstate, RangeTblEntry *rte, int *sublevels_up) { int index; - List *temp; + ListCell *l; if (sublevels_up) *sublevels_up = 0; @@ -405,9 +405,9 @@ RTERangeTablePosn(ParseState *pstate, RangeTblEntry *rte, int *sublevels_up) while (pstate != NULL) { index = 1; - foreach(temp, pstate->p_rtable) + foreach(l, pstate->p_rtable) { - if (rte == (RangeTblEntry *) lfirst(temp)) + if (rte == (RangeTblEntry *) lfirst(l)) return index; index++; } @@ -460,7 +460,7 @@ scanRTEForColumn(ParseState *pstate, RangeTblEntry *rte, char *colname) { Node *result = NULL; int attnum = 0; - List *c; + ListCell *c; /* * Scan the user column names (or aliases) for a match. Complain if @@ -546,7 +546,7 @@ colNameToVar(ParseState *pstate, char *colname, bool localonly) while (pstate != NULL) { - List *ns; + ListCell *ns; /* * We need to look only at top-level namespace items, and even for @@ -841,7 +841,7 @@ addRangeTableEntryForSubquery(ParseState *pstate, Alias *eref; int numaliases; int varattno; - List *tlistitem; + ListCell *tlistitem; rte->rtekind = RTE_SUBQUERY; rte->relid = InvalidOid; @@ -1027,7 +1027,7 @@ addRangeTableEntryForFunction(ParseState *pstate, } else if (functyptype == 'p' && funcrettype == RECORDOID) { - List *col; + ListCell *col; /* Use the column definition list to form the alias list */ eref->colnames = NIL; @@ -1101,11 +1101,8 @@ addRangeTableEntryForJoin(ParseState *pstate, /* fill in any unspecified alias columns */ if (numaliases < length(colnames)) - { - while (numaliases-- > 0) - colnames = lnext(colnames); - eref->colnames = nconc(eref->colnames, colnames); - } + eref->colnames = nconc(eref->colnames, + list_copy_tail(colnames, numaliases)); rte->eref = eref; @@ -1145,7 +1142,7 @@ isForUpdate(ParseState *pstate, char *refname) { if (pstate->p_forUpdate != NIL) { - if (lfirst(pstate->p_forUpdate) == NULL) + if (linitial(pstate->p_forUpdate) == NULL) { /* all tables used in query */ return true; @@ -1153,7 +1150,7 @@ isForUpdate(ParseState *pstate, char *refname) else { /* just the named tables */ - List *l; + ListCell *l; foreach(l, pstate->p_forUpdate) { @@ -1282,8 +1279,8 @@ expandRTE(ParseState *pstate, RangeTblEntry *rte, case RTE_SUBQUERY: { /* Subquery RTE */ - List *aliasp = rte->eref->colnames; - List *tlistitem; + ListCell *aliasp_item = list_head(rte->eref->colnames); + ListCell *tlistitem; varattno = 0; foreach(tlistitem, rte->subquery->targetList) @@ -1298,10 +1295,10 @@ expandRTE(ParseState *pstate, RangeTblEntry *rte, if (colnames) { /* Assume there is one alias per target item */ - char *label = strVal(lfirst(aliasp)); + char *label = strVal(lfirst(aliasp_item)); *colnames = lappend(*colnames, makeString(pstrdup(label))); - aliasp = lnext(aliasp); + aliasp_item = lnext(aliasp_item); } if (colvars) @@ -1385,7 +1382,7 @@ expandRTE(ParseState *pstate, RangeTblEntry *rte, */ if (colnames) *colnames = lappend(*colnames, - lfirst(rte->eref->colnames)); + linitial(rte->eref->colnames)); if (colvars) { @@ -1400,7 +1397,7 @@ expandRTE(ParseState *pstate, RangeTblEntry *rte, } else if (functyptype == 'p' && funcrettype == RECORDOID) { - List *col; + ListCell *col; int attnum = 0; foreach(col, coldeflist) @@ -1442,39 +1439,36 @@ expandRTE(ParseState *pstate, RangeTblEntry *rte, case RTE_JOIN: { /* Join RTE */ - List *aliasp = rte->eref->colnames; - List *aliasvars = rte->joinaliasvars; + ListCell *colname; + ListCell *aliasvar; + + Assert(length(rte->eref->colnames) == length(rte->joinaliasvars)); varattno = 0; - while (aliasp) + forboth (colname, rte->eref->colnames, aliasvar, rte->joinaliasvars) { - Assert(aliasvars); varattno++; if (colnames) { - char *label = strVal(lfirst(aliasp)); + char *label = strVal(lfirst(colname)); *colnames = lappend(*colnames, makeString(pstrdup(label))); } if (colvars) { - Node *aliasvar = (Node *) lfirst(aliasvars); + Node *avar = (Node *) lfirst(aliasvar); Var *varnode; varnode = makeVar(rtindex, varattno, - exprType(aliasvar), - exprTypmod(aliasvar), + exprType(avar), + exprTypmod(avar), sublevels_up); *colvars = lappend(*colvars, varnode); } - - aliasp = lnext(aliasp); - aliasvars = lnext(aliasvars); } - Assert(aliasvars == NIL); } break; default: @@ -1492,14 +1486,16 @@ expandRelAttrs(ParseState *pstate, RangeTblEntry *rte) { List *names, *vars; + ListCell *name, + *var; List *te_list = NIL; expandRTE(pstate, rte, &names, &vars); - while (names) + forboth (name, names, var, vars) { - char *label = strVal(lfirst(names)); - Node *varnode = (Node *) lfirst(vars); + char *label = strVal(lfirst(name)); + Node *varnode = (Node *) lfirst(var); TargetEntry *te = makeNode(TargetEntry); te->resdom = makeResdom((AttrNumber) pstate->p_next_resno++, @@ -1509,12 +1505,9 @@ expandRelAttrs(ParseState *pstate, RangeTblEntry *rte) false); te->expr = (Expr *) varnode; te_list = lappend(te_list, te); - - names = lnext(names); - vars = lnext(vars); } - Assert(vars == NIL); /* lists not same length? */ + Assert(name == NULL && var == NULL); /* lists not the same length? */ return te_list; } @@ -1790,11 +1783,11 @@ get_rte_attribute_is_dropped(RangeTblEntry *rte, AttrNumber attnum) TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno) { - List *i; + ListCell *l; - foreach(i, tlist) + foreach(l, tlist) { - TargetEntry *tle = (TargetEntry *) lfirst(i); + TargetEntry *tle = (TargetEntry *) lfirst(l); if (tle->resdom->resno == resno) return tle; @@ -1917,7 +1910,7 @@ static void warnAutoRange(ParseState *pstate, RangeVar *relation) { bool foundInFromCl = false; - List *temp; + ListCell *temp; if (!add_missing_from) { diff --git a/src/backend/parser/parse_target.c b/src/backend/parser/parse_target.c index fbee22b37d3..9eaf9eb53a9 100644 --- a/src/backend/parser/parse_target.c +++ b/src/backend/parser/parse_target.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/parser/parse_target.c,v 1.117 2004/05/10 22:44:46 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/parser/parse_target.c,v 1.118 2004/05/26 04:41:30 neilc Exp $ * *------------------------------------------------------------------------- */ @@ -93,7 +93,7 @@ List * transformTargetList(ParseState *pstate, List *targetlist) { FastList p_target; - List *o_target; + ListCell *o_target; FastListInit(&p_target); @@ -134,15 +134,15 @@ transformTargetList(ParseState *pstate, List *targetlist) { case 2: schemaname = NULL; - relname = strVal(lfirst(fields)); + relname = strVal(linitial(fields)); break; case 3: - schemaname = strVal(lfirst(fields)); + schemaname = strVal(linitial(fields)); relname = strVal(lsecond(fields)); break; case 4: { - char *name1 = strVal(lfirst(fields)); + char *name1 = strVal(linitial(fields)); /* * We check the catalog name and then ignore @@ -215,7 +215,7 @@ transformTargetList(ParseState *pstate, List *targetlist) void markTargetListOrigins(ParseState *pstate, List *targetlist) { - List *l; + ListCell *l; foreach(l, targetlist) { @@ -468,7 +468,7 @@ checkInsertTargets(ParseState *pstate, List *cols, List **attrnos) /* * Do initial validation of user-supplied INSERT column list. */ - List *tl; + ListCell *tl; foreach(tl, cols) { @@ -502,7 +502,7 @@ ExpandAllTables(ParseState *pstate) { List *target = NIL; bool found_table = false; - List *ns; + ListCell *ns; foreach(ns, pstate->p_namespace) { diff --git a/src/backend/parser/parse_type.c b/src/backend/parser/parse_type.c index e6e8b00cda4..473d52399e3 100644 --- a/src/backend/parser/parse_type.c +++ b/src/backend/parser/parse_type.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/parser/parse_type.c,v 1.65 2004/03/21 22:29:11 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/parser/parse_type.c,v 1.66 2004/05/26 04:41:30 neilc Exp $ * *------------------------------------------------------------------------- */ @@ -63,16 +63,16 @@ LookupTypeName(const TypeName *typename) NameListToString(typename->names)))); break; case 2: - rel->relname = strVal(lfirst(typename->names)); + rel->relname = strVal(linitial(typename->names)); field = strVal(lsecond(typename->names)); break; case 3: - rel->schemaname = strVal(lfirst(typename->names)); + rel->schemaname = strVal(linitial(typename->names)); rel->relname = strVal(lsecond(typename->names)); field = strVal(lthird(typename->names)); break; case 4: - rel->catalogname = strVal(lfirst(typename->names)); + rel->catalogname = strVal(linitial(typename->names)); rel->schemaname = strVal(lsecond(typename->names)); rel->relname = strVal(lthird(typename->names)); field = strVal(lfourth(typename->names)); @@ -155,11 +155,11 @@ TypeNameToString(const TypeName *typename) if (typename->names != NIL) { /* Emit possibly-qualified name as-is */ - List *l; + ListCell *l; foreach(l, typename->names) { - if (l != typename->names) + if (l != list_head(typename->names)) appendStringInfoChar(&string, '.'); appendStringInfoString(&string, strVal(lfirst(l))); } @@ -488,7 +488,7 @@ parseTypeString(const char *str, Oid *type_id, int32 *typmod) */ if (length(raw_parsetree_list) != 1) goto fail; - stmt = (SelectStmt *) lfirst(raw_parsetree_list); + stmt = (SelectStmt *) linitial(raw_parsetree_list); if (stmt == NULL || !IsA(stmt, SelectStmt) || stmt->distinctClause != NIL || @@ -505,7 +505,7 @@ parseTypeString(const char *str, Oid *type_id, int32 *typmod) goto fail; if (length(stmt->targetList) != 1) goto fail; - restarget = (ResTarget *) lfirst(stmt->targetList); + restarget = (ResTarget *) linitial(stmt->targetList); if (restarget == NULL || !IsA(restarget, ResTarget) || restarget->name != NULL || |