Commit 18a96377 authored by Tom Lane's avatar Tom Lane

Fix CREATE TABLE ... AS VALUES ... to work rather than Assert'ing;

oversight in original implementation of VALUES.  Also fix an oversight
in recent addition of options to CREATE TABLE AS: they weren't getting
propagated if the query was a set-operation such as UNION.
parent da7540b9
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $PostgreSQL: pgsql/src/backend/parser/analyze.c,v 1.349 2006/08/30 23:34:21 tgl Exp $ * $PostgreSQL: pgsql/src/backend/parser/analyze.c,v 1.350 2006/09/18 00:52:14 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -2097,7 +2097,6 @@ transformSelectStmt(ParseState *pstate, SelectStmt *stmt) ...@@ -2097,7 +2097,6 @@ transformSelectStmt(ParseState *pstate, SelectStmt *stmt)
qry->into = stmt->into; qry->into = stmt->into;
if (stmt->intoColNames) if (stmt->intoColNames)
applyColumnNames(qry->targetList, stmt->intoColNames); applyColumnNames(qry->targetList, stmt->intoColNames);
qry->intoOptions = copyObject(stmt->intoOptions); qry->intoOptions = copyObject(stmt->intoOptions);
qry->intoOnCommit = stmt->intoOnCommit; qry->intoOnCommit = stmt->intoOnCommit;
qry->intoTableSpaceName = stmt->intoTableSpaceName; qry->intoTableSpaceName = stmt->intoTableSpaceName;
...@@ -2180,8 +2179,6 @@ transformValuesClause(ParseState *pstate, SelectStmt *stmt) ...@@ -2180,8 +2179,6 @@ transformValuesClause(ParseState *pstate, SelectStmt *stmt)
/* Most SELECT stuff doesn't apply in a VALUES clause */ /* Most SELECT stuff doesn't apply in a VALUES clause */
Assert(stmt->distinctClause == NIL); Assert(stmt->distinctClause == NIL);
Assert(stmt->into == NULL);
Assert(stmt->intoColNames == NIL);
Assert(stmt->targetList == NIL); Assert(stmt->targetList == NIL);
Assert(stmt->fromClause == NIL); Assert(stmt->fromClause == NIL);
Assert(stmt->whereClause == NULL); Assert(stmt->whereClause == NULL);
...@@ -2281,8 +2278,16 @@ transformValuesClause(ParseState *pstate, SelectStmt *stmt) ...@@ -2281,8 +2278,16 @@ transformValuesClause(ParseState *pstate, SelectStmt *stmt)
Assert(pstate->p_next_resno == 1); Assert(pstate->p_next_resno == 1);
qry->targetList = expandRelAttrs(pstate, rte, rtr->rtindex, 0); qry->targetList = expandRelAttrs(pstate, rte, rtr->rtindex, 0);
/* handle any CREATE TABLE AS spec */
qry->into = stmt->into;
if (stmt->intoColNames)
applyColumnNames(qry->targetList, stmt->intoColNames);
qry->intoOptions = copyObject(stmt->intoOptions);
qry->intoOnCommit = stmt->intoOnCommit;
qry->intoTableSpaceName = stmt->intoTableSpaceName;
/* /*
* The grammar does allow attaching ORDER BY, LIMIT, and FOR UPDATE * The grammar allows attaching ORDER BY, LIMIT, and FOR UPDATE
* to a VALUES, so cope. * to a VALUES, so cope.
*/ */
qry->sortClause = transformSortClause(pstate, qry->sortClause = transformSortClause(pstate,
...@@ -2355,7 +2360,6 @@ transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt) ...@@ -2355,7 +2360,6 @@ transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt)
int leftmostRTI; int leftmostRTI;
Query *leftmostQuery; Query *leftmostQuery;
SetOperationStmt *sostmt; SetOperationStmt *sostmt;
RangeVar *into;
List *intoColNames; List *intoColNames;
List *sortClause; List *sortClause;
Node *limitOffset; Node *limitOffset;
...@@ -2378,19 +2382,23 @@ transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt) ...@@ -2378,19 +2382,23 @@ transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt)
/* /*
* Find leftmost leaf SelectStmt; extract the one-time-only items from it * Find leftmost leaf SelectStmt; extract the one-time-only items from it
* and from the top-level node. * and from the top-level node. (Most of the INTO options can be
* transferred to the Query immediately, but intoColNames has to be
* saved to apply below.)
*/ */
leftmostSelect = stmt->larg; leftmostSelect = stmt->larg;
while (leftmostSelect && leftmostSelect->op != SETOP_NONE) while (leftmostSelect && leftmostSelect->op != SETOP_NONE)
leftmostSelect = leftmostSelect->larg; leftmostSelect = leftmostSelect->larg;
Assert(leftmostSelect && IsA(leftmostSelect, SelectStmt) && Assert(leftmostSelect && IsA(leftmostSelect, SelectStmt) &&
leftmostSelect->larg == NULL); leftmostSelect->larg == NULL);
into = leftmostSelect->into; qry->into = leftmostSelect->into;
intoColNames = leftmostSelect->intoColNames; intoColNames = leftmostSelect->intoColNames;
qry->intoOptions = copyObject(leftmostSelect->intoOptions);
qry->intoOnCommit = leftmostSelect->intoOnCommit;
qry->intoTableSpaceName = leftmostSelect->intoTableSpaceName;
/* clear them to prevent complaints in transformSetOperationTree() */ /* clear this to prevent complaints in transformSetOperationTree() */
leftmostSelect->into = NULL; leftmostSelect->into = NULL;
leftmostSelect->intoColNames = NIL;
/* /*
* These are not one-time, exactly, but we want to process them here and * These are not one-time, exactly, but we want to process them here and
...@@ -2480,7 +2488,6 @@ transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt) ...@@ -2480,7 +2488,6 @@ transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt)
* top level and the leftmost subquery. We do not do this earlier because * top level and the leftmost subquery. We do not do this earlier because
* we do *not* want the targetnames list to be affected. * we do *not* want the targetnames list to be affected.
*/ */
qry->into = into;
if (intoColNames) if (intoColNames)
{ {
applyColumnNames(qry->targetList, intoColNames); applyColumnNames(qry->targetList, intoColNames);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment