|
6 | 6 | * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
|
7 | 7 | * Portions Copyright (c) 1994, Regents of the University of California
|
8 | 8 | *
|
9 |
| - * $Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.209 2001/11/04 03:08:11 momjian Exp $ |
| 9 | + * $Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.210 2001/11/05 05:00:14 tgl Exp $ |
10 | 10 | *
|
11 | 11 | *-------------------------------------------------------------------------
|
12 | 12 | */
|
@@ -88,6 +88,7 @@ static void transformFKConstraints(ParseState *pstate,
|
88 | 88 | CreateStmtContext *cxt);
|
89 | 89 | static Node *transformTypeRefs(ParseState *pstate, Node *stmt);
|
90 | 90 |
|
| 91 | +static void applyColumnNames(List *dst, List *src); |
91 | 92 | static void transformTypeRefsList(ParseState *pstate, List *l);
|
92 | 93 | static void transformTypeRef(ParseState *pstate, TypeName *tn);
|
93 | 94 | static List *getSetColTypes(ParseState *pstate, Node *node);
|
@@ -1942,9 +1943,13 @@ transformSelectStmt(ParseState *pstate, SelectStmt *stmt)
|
1942 | 1943 | /* process the FROM clause */
|
1943 | 1944 | transformFromClause(pstate, stmt->fromClause);
|
1944 | 1945 |
|
1945 |
| - /* transform targetlist and WHERE */ |
| 1946 | + /* transform targetlist */ |
1946 | 1947 | qry->targetList = transformTargetList(pstate, stmt->targetList);
|
1947 | 1948 |
|
| 1949 | + if (stmt->intoColNames) |
| 1950 | + applyColumnNames(qry->targetList, stmt->intoColNames); |
| 1951 | + |
| 1952 | + /* transform WHERE */ |
1948 | 1953 | qual = transformWhereClause(pstate, stmt->whereClause);
|
1949 | 1954 |
|
1950 | 1955 | /*
|
@@ -2003,6 +2008,7 @@ transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt)
|
2003 | 2008 | SetOperationStmt *sostmt;
|
2004 | 2009 | char *into;
|
2005 | 2010 | bool istemp;
|
| 2011 | + List *intoColNames; |
2006 | 2012 | char *portalname;
|
2007 | 2013 | bool binary;
|
2008 | 2014 | List *sortClause;
|
@@ -2031,12 +2037,14 @@ transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt)
|
2031 | 2037 | leftmostSelect->larg == NULL);
|
2032 | 2038 | into = leftmostSelect->into;
|
2033 | 2039 | istemp = leftmostSelect->istemp;
|
| 2040 | + intoColNames = leftmostSelect->intoColNames; |
2034 | 2041 | portalname = stmt->portalname;
|
2035 | 2042 | binary = stmt->binary;
|
2036 | 2043 |
|
2037 | 2044 | /* clear them to prevent complaints in transformSetOperationTree() */
|
2038 | 2045 | leftmostSelect->into = NULL;
|
2039 | 2046 | leftmostSelect->istemp = false;
|
| 2047 | + leftmostSelect->intoColNames = NIL; |
2040 | 2048 | stmt->portalname = NULL;
|
2041 | 2049 | stmt->binary = false;
|
2042 | 2050 |
|
@@ -2149,6 +2157,9 @@ transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt)
|
2149 | 2157 | qry->isBinary = FALSE;
|
2150 | 2158 | }
|
2151 | 2159 |
|
| 2160 | + if (intoColNames) |
| 2161 | + applyColumnNames(qry->targetList, intoColNames); |
| 2162 | + |
2152 | 2163 | /*
|
2153 | 2164 | * As a first step towards supporting sort clauses that are
|
2154 | 2165 | * expressions using the output columns, generate a namespace entry
|
@@ -2377,6 +2388,27 @@ getSetColTypes(ParseState *pstate, Node *node)
|
2377 | 2388 | }
|
2378 | 2389 | }
|
2379 | 2390 |
|
| 2391 | +/* Attach column names from a ColumnDef list to a TargetEntry list */ |
| 2392 | +static void |
| 2393 | +applyColumnNames(List *dst, List *src) |
| 2394 | +{ |
| 2395 | + if (length(src) > length(dst)) |
| 2396 | + elog(ERROR,"CREATE TABLE AS specifies too many column names"); |
| 2397 | + |
| 2398 | + while (src != NIL && dst != NIL) |
| 2399 | + { |
| 2400 | + TargetEntry *d = (TargetEntry *) lfirst(dst); |
| 2401 | + ColumnDef *s = (ColumnDef *) lfirst(src); |
| 2402 | + |
| 2403 | + Assert(d->resdom && !d->resdom->resjunk); |
| 2404 | + |
| 2405 | + d->resdom->resname = pstrdup(s->colname); |
| 2406 | + |
| 2407 | + dst = lnext(dst); |
| 2408 | + src = lnext(src); |
| 2409 | + } |
| 2410 | +} |
| 2411 | + |
2380 | 2412 |
|
2381 | 2413 | /*
|
2382 | 2414 | * transformUpdateStmt -
|
|
0 commit comments