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

Commit 31bc839

Browse files
committed
Prevent failure when RowExpr or XmlExpr is parse-analyzed twice.
transformExpr() is required to cope with already-transformed expression trees, for various ugly-but-not-quite-worth-cleaning-up reasons. However, some of its newer subroutines hadn't gotten the memo. This accounts for bug #7763 from Norbert Buchmuller: transformRowExpr() was overwriting the previously determined type of a RowExpr during CREATE TABLE LIKE INCLUDING INDEXES. Additional investigation showed that transformXmlExpr had the same kind of problem, but all the other cases seem to be safe. Andres Freund and Tom Lane
1 parent eb03506 commit 31bc839

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

src/backend/parser/gram.y

+1
Original file line numberDiff line numberDiff line change
@@ -13341,6 +13341,7 @@ makeXmlExpr(XmlExprOp op, char *name, List *named_args, List *args,
1334113341
x->args = args;
1334213342
/* xmloption, if relevant, must be filled in by caller */
1334313343
/* type and typmod will be filled in during parse analysis */
13344+
x->type = InvalidOid; /* marks the node as not analyzed */
1334413345
x->location = location;
1334513346
return (Node *) x;
1334613347
}

src/backend/parser/parse_expr.c

+17-2
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ static Expr *make_distinct_op(ParseState *pstate, List *opname,
9292
* function argument to the required type (via coerce_type())
9393
* can apply transformExpr to an already-transformed subexpression.
9494
* An example here is "SELECT count(*) + 1.0 FROM table".
95+
* 3. CREATE TABLE t1 (LIKE t2 INCLUDING INDEXES) can pass in
96+
* already-transformed index expressions.
9597
* While it might be possible to eliminate these cases, the path of
9698
* least resistance so far has been to ensure that transformExpr() does
9799
* no damage if applied to an already-transformed tree. This is pretty
@@ -1775,11 +1777,17 @@ transformArrayExpr(ParseState *pstate, A_ArrayExpr *a,
17751777
static Node *
17761778
transformRowExpr(ParseState *pstate, RowExpr *r)
17771779
{
1778-
RowExpr *newr = makeNode(RowExpr);
1780+
RowExpr *newr;
17791781
char fname[16];
17801782
int fnum;
17811783
ListCell *lc;
17821784

1785+
/* If we already transformed this node, do nothing */
1786+
if (OidIsValid(r->row_typeid))
1787+
return (Node *) r;
1788+
1789+
newr = makeNode(RowExpr);
1790+
17831791
/* Transform the field expressions */
17841792
newr->args = transformExpressionList(pstate, r->args, pstate->p_expr_kind);
17851793

@@ -1880,16 +1888,23 @@ transformMinMaxExpr(ParseState *pstate, MinMaxExpr *m)
18801888
static Node *
18811889
transformXmlExpr(ParseState *pstate, XmlExpr *x)
18821890
{
1883-
XmlExpr *newx = makeNode(XmlExpr);
1891+
XmlExpr *newx;
18841892
ListCell *lc;
18851893
int i;
18861894

1895+
/* If we already transformed this node, do nothing */
1896+
if (OidIsValid(x->type))
1897+
return (Node *) x;
1898+
1899+
newx = makeNode(XmlExpr);
18871900
newx->op = x->op;
18881901
if (x->name)
18891902
newx->name = map_sql_identifier_to_xml_name(x->name, false, false);
18901903
else
18911904
newx->name = NULL;
18921905
newx->xmloption = x->xmloption;
1906+
newx->type = XMLOID; /* this just marks the node as transformed */
1907+
newx->typmod = -1;
18931908
newx->location = x->location;
18941909

18951910
/*

src/include/nodes/primnodes.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,8 @@ typedef struct MinMaxExpr
958958
*
959959
* Note: result type/typmod/collation are not stored, but can be deduced
960960
* from the XmlExprOp. The type/typmod fields are just used for display
961-
* purposes, and are NOT the true result type of the node.
961+
* purposes, and are NOT necessarily the true result type of the node.
962+
* (We also use type == InvalidOid to mark a not-yet-parse-analyzed XmlExpr.)
962963
*/
963964
typedef enum XmlExprOp
964965
{

0 commit comments

Comments
 (0)