66 * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
77 * Portions Copyright (c) 1994, Regents of the University of California
88 *
9- *$PostgreSQL: pgsql/src/backend/parser/analyze.c,v 1.349 2006/08/30 23:34:21 tgl Exp $
9+ *$PostgreSQL: pgsql/src/backend/parser/analyze.c,v 1.350 2006/09/18 00:52:14 tgl Exp $
1010 *
1111 *-------------------------------------------------------------------------
1212 */
@@ -2097,7 +2097,6 @@ transformSelectStmt(ParseState *pstate, SelectStmt *stmt)
20972097qry -> into = stmt -> into ;
20982098if (stmt -> intoColNames )
20992099applyColumnNames (qry -> targetList ,stmt -> intoColNames );
2100-
21012100qry -> intoOptions = copyObject (stmt -> intoOptions );
21022101qry -> intoOnCommit = stmt -> intoOnCommit ;
21032102qry -> intoTableSpaceName = stmt -> intoTableSpaceName ;
@@ -2180,8 +2179,6 @@ transformValuesClause(ParseState *pstate, SelectStmt *stmt)
21802179
21812180/* Most SELECT stuff doesn't apply in a VALUES clause */
21822181Assert (stmt -> distinctClause == NIL );
2183- Assert (stmt -> into == NULL );
2184- Assert (stmt -> intoColNames == NIL );
21852182Assert (stmt -> targetList == NIL );
21862183Assert (stmt -> fromClause == NIL );
21872184Assert (stmt -> whereClause == NULL );
@@ -2281,8 +2278,16 @@ transformValuesClause(ParseState *pstate, SelectStmt *stmt)
22812278Assert (pstate -> p_next_resno == 1 );
22822279qry -> targetList = expandRelAttrs (pstate ,rte ,rtr -> rtindex ,0 );
22832280
2281+ /* handle any CREATE TABLE AS spec */
2282+ qry -> into = stmt -> into ;
2283+ if (stmt -> intoColNames )
2284+ applyColumnNames (qry -> targetList ,stmt -> intoColNames );
2285+ qry -> intoOptions = copyObject (stmt -> intoOptions );
2286+ qry -> intoOnCommit = stmt -> intoOnCommit ;
2287+ qry -> intoTableSpaceName = stmt -> intoTableSpaceName ;
2288+
22842289/*
2285- * The grammardoes allow attaching ORDER BY, LIMIT, and FOR UPDATE
2290+ * The grammarallows attaching ORDER BY, LIMIT, and FOR UPDATE
22862291 * to a VALUES, so cope.
22872292 */
22882293qry -> sortClause = transformSortClause (pstate ,
@@ -2355,7 +2360,6 @@ transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt)
23552360int leftmostRTI ;
23562361Query * leftmostQuery ;
23572362SetOperationStmt * sostmt ;
2358- RangeVar * into ;
23592363List * intoColNames ;
23602364List * sortClause ;
23612365Node * limitOffset ;
@@ -2378,19 +2382,23 @@ transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt)
23782382
23792383/*
23802384 * Find leftmost leaf SelectStmt; extract the one-time-only items from it
2381- * and from the top-level node.
2385+ * and from the top-level node. (Most of the INTO options can be
2386+ * transferred to the Query immediately, but intoColNames has to be
2387+ * saved to apply below.)
23822388 */
23832389leftmostSelect = stmt -> larg ;
23842390while (leftmostSelect && leftmostSelect -> op != SETOP_NONE )
23852391leftmostSelect = leftmostSelect -> larg ;
23862392Assert (leftmostSelect && IsA (leftmostSelect ,SelectStmt )&&
23872393leftmostSelect -> larg == NULL );
2388- into = leftmostSelect -> into ;
2394+ qry -> into = leftmostSelect -> into ;
23892395intoColNames = leftmostSelect -> intoColNames ;
2396+ qry -> intoOptions = copyObject (leftmostSelect -> intoOptions );
2397+ qry -> intoOnCommit = leftmostSelect -> intoOnCommit ;
2398+ qry -> intoTableSpaceName = leftmostSelect -> intoTableSpaceName ;
23902399
2391- /* clearthem to prevent complaints in transformSetOperationTree() */
2400+ /* clearthis to prevent complaints in transformSetOperationTree() */
23922401leftmostSelect -> into = NULL ;
2393- leftmostSelect -> intoColNames = NIL ;
23942402
23952403/*
23962404 * These are not one-time, exactly, but we want to process them here and
@@ -2480,7 +2488,6 @@ transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt)
24802488 * top level and the leftmost subquery. We do not do this earlier because
24812489 * we do *not* want the targetnames list to be affected.
24822490 */
2483- qry -> into = into ;
24842491if (intoColNames )
24852492{
24862493applyColumnNames (qry -> targetList ,intoColNames );