Skip to content

Commit de4cf42

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 97e1db7 commit de4cf42

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

src/backend/parser/gram.y

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13135,6 +13135,7 @@ makeXmlExpr(XmlExprOp op, char *name, List *named_args, List *args,
1313513135
x->args = args;
1313613136
/* xmloption, if relevant, must be filled in by caller */
1313713137
/* type and typmod will be filled in during parse analysis */
13138+
x->type = InvalidOid; /* marks the node as not analyzed */
1313813139
x->location = location;
1313913140
return (Node *) x;
1314013141
}

src/backend/parser/parse_expr.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ static Expr *make_distinct_op(ParseState *pstate, List *opname,
9191
* function argument to the required type (via coerce_type())
9292
* can apply transformExpr to an already-transformed subexpression.
9393
* An example here is "SELECT count(*) + 1.0 FROM table".
94+
* 3. CREATE TABLE t1 (LIKE t2 INCLUDING INDEXES) can pass in
95+
* already-transformed index expressions.
9496
* While it might be possible to eliminate these cases, the path of
9597
* least resistance so far has been to ensure that transformExpr() does
9698
* no damage if applied to an already-transformed tree. This is pretty
@@ -1685,11 +1687,17 @@ transformArrayExpr(ParseState *pstate, A_ArrayExpr *a,
16851687
static Node *
16861688
transformRowExpr(ParseState *pstate, RowExpr *r)
16871689
{
1688-
RowExpr *newr = makeNode(RowExpr);
1690+
RowExpr *newr;
16891691
char fname[16];
16901692
int fnum;
16911693
ListCell *lc;
16921694

1695+
/* If we already transformed this node, do nothing */
1696+
if (OidIsValid(r->row_typeid))
1697+
return (Node *) r;
1698+
1699+
newr = makeNode(RowExpr);
1700+
16931701
/* Transform the field expressions */
16941702
newr->args = transformExpressionList(pstate, r->args);
16951703

@@ -1790,16 +1798,23 @@ transformMinMaxExpr(ParseState *pstate, MinMaxExpr *m)
17901798
static Node *
17911799
transformXmlExpr(ParseState *pstate, XmlExpr *x)
17921800
{
1793-
XmlExpr *newx = makeNode(XmlExpr);
1801+
XmlExpr *newx;
17941802
ListCell *lc;
17951803
int i;
17961804

1805+
/* If we already transformed this node, do nothing */
1806+
if (OidIsValid(x->type))
1807+
return (Node *) x;
1808+
1809+
newx = makeNode(XmlExpr);
17971810
newx->op = x->op;
17981811
if (x->name)
17991812
newx->name = map_sql_identifier_to_xml_name(x->name, false, false);
18001813
else
18011814
newx->name = NULL;
18021815
newx->xmloption = x->xmloption;
1816+
newx->type = XMLOID; /* this just marks the node as transformed */
1817+
newx->typmod = -1;
18031818
newx->location = x->location;
18041819

18051820
/*

src/include/nodes/primnodes.h

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

0 commit comments

Comments
 (0)