From: Tom Lane Date: Sat, 11 Aug 2001 00:02:13 +0000 (+0000) Subject: CREATE VIEW with optional column name list wasn't quite right for the X-Git-Tag: REL7_2_BETA1~716 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=dfb8e3f1155d1ab3ce5ef16e203ec1eabc33f2b1;p=postgresql CREATE VIEW with optional column name list wasn't quite right for the case where there are resjunk columns in the query. --- diff --git a/src/backend/parser/analyze.c b/src/backend/parser/analyze.c index 186fa12450..c4c0aa1a87 100644 --- a/src/backend/parser/analyze.c +++ b/src/backend/parser/analyze.c @@ -6,7 +6,7 @@ * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.193 2001/07/16 05:06:58 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.194 2001/08/11 00:02:13 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -161,31 +161,36 @@ transformStmt(ParseState *pstate, Node *parseTree) * If a list of column names was given, run through and * insert these into the actual query tree. - thomas * 2000-03-08 + * + * Outer loop is over targetlist to make it easier to + * skip junk targetlist entries. */ if (n->aliases != NIL) { - int i; - List *targetList = n->query->targetList; - - if (length(targetList) < length(n->aliases)) - elog(ERROR, "CREATE VIEW specifies %d columns" - " but only %d columns are present", - length(targetList), length(n->aliases)); + List *aliaslist = n->aliases; + List *targetList; - for (i = 0; i < length(n->aliases); i++) + foreach(targetList, n->query->targetList) { - Ident *id; - TargetEntry *te; + TargetEntry *te = (TargetEntry *) lfirst(targetList); Resdom *rd; + Ident *id; - id = nth(i, n->aliases); - Assert(IsA(id, Ident)); - te = nth(i, targetList); Assert(IsA(te, TargetEntry)); rd = te->resdom; Assert(IsA(rd, Resdom)); + if (rd->resjunk) /* junk columns don't get aliases */ + continue; + id = (Ident *) lfirst(aliaslist); + Assert(IsA(id, Ident)); rd->resname = pstrdup(id->name); + aliaslist = lnext(aliaslist); + if (aliaslist == NIL) + break; /* done assigning aliases */ } + + if (aliaslist != NIL) + elog(ERROR, "CREATE VIEW specifies more column names than columns"); } result = makeNode(Query); result->commandType = CMD_UTILITY;