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

Commit dfb8e3f

Browse files
committed
CREATE VIEW with optional column name list wasn't quite right for the
case where there are resjunk columns in the query.
1 parent 548512a commit dfb8e3f

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

src/backend/parser/analyze.c

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.193 2001/07/16 05:06:58 tgl Exp $
9+
* $Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.194 2001/08/11 00:02:13 tgl Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -161,31 +161,36 @@ transformStmt(ParseState *pstate, Node *parseTree)
161161
* If a list of column names was given, run through and
162162
* insert these into the actual query tree. - thomas
163163
* 2000-03-08
164+
*
165+
* Outer loop is over targetlist to make it easier to
166+
* skip junk targetlist entries.
164167
*/
165168
if (n->aliases != NIL)
166169
{
167-
int i;
168-
List *targetList = n->query->targetList;
169-
170-
if (length(targetList) < length(n->aliases))
171-
elog(ERROR, "CREATE VIEW specifies %d columns"
172-
" but only %d columns are present",
173-
length(targetList), length(n->aliases));
170+
List *aliaslist = n->aliases;
171+
List *targetList;
174172

175-
for (i = 0; i < length(n->aliases); i++)
173+
foreach(targetList, n->query->targetList)
176174
{
177-
Ident *id;
178-
TargetEntry *te;
175+
TargetEntry *te = (TargetEntry *) lfirst(targetList);
179176
Resdom *rd;
177+
Ident *id;
180178

181-
id = nth(i, n->aliases);
182-
Assert(IsA(id, Ident));
183-
te = nth(i, targetList);
184179
Assert(IsA(te, TargetEntry));
185180
rd = te->resdom;
186181
Assert(IsA(rd, Resdom));
182+
if (rd->resjunk) /* junk columns don't get aliases */
183+
continue;
184+
id = (Ident *) lfirst(aliaslist);
185+
Assert(IsA(id, Ident));
187186
rd->resname = pstrdup(id->name);
187+
aliaslist = lnext(aliaslist);
188+
if (aliaslist == NIL)
189+
break; /* done assigning aliases */
188190
}
191+
192+
if (aliaslist != NIL)
193+
elog(ERROR, "CREATE VIEW specifies more column names than columns");
189194
}
190195
result = makeNode(Query);
191196
result->commandType = CMD_UTILITY;

0 commit comments

Comments
 (0)