Skip to content

Commit 628ea7e

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 14aa55d commit 628ea7e

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
@@ -12728,6 +12728,7 @@ makeXmlExpr(XmlExprOp op, char *name, List *named_args, List *args,
1272812728
x->args = args;
1272912729
/* xmloption, if relevant, must be filled in by caller */
1273012730
/* type and typmod will be filled in during parse analysis */
12731+
x->type = InvalidOid; /* marks the node as not analyzed */
1273112732
x->location = location;
1273212733
return (Node *) x;
1273312734
}

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
@@ -1691,7 +1693,13 @@ transformArrayExpr(ParseState *pstate, A_ArrayExpr *a,
16911693
static Node *
16921694
transformRowExpr(ParseState *pstate, RowExpr *r)
16931695
{
1694-
RowExpr *newr = makeNode(RowExpr);
1696+
RowExpr *newr;
1697+
1698+
/* If we already transformed this node, do nothing */
1699+
if (OidIsValid(r->row_typeid))
1700+
return (Node *) r;
1701+
1702+
newr = makeNode(RowExpr);
16951703

16961704
/* Transform the field expressions */
16971705
newr->args = transformExpressionList(pstate, r->args);
@@ -1784,16 +1792,23 @@ transformMinMaxExpr(ParseState *pstate, MinMaxExpr *m)
17841792
static Node *
17851793
transformXmlExpr(ParseState *pstate, XmlExpr *x)
17861794
{
1787-
XmlExpr *newx = makeNode(XmlExpr);
1795+
XmlExpr *newx;
17881796
ListCell *lc;
17891797
int i;
17901798

1799+
/* If we already transformed this node, do nothing */
1800+
if (OidIsValid(x->type))
1801+
return (Node *) x;
1802+
1803+
newx = makeNode(XmlExpr);
17911804
newx->op = x->op;
17921805
if (x->name)
17931806
newx->name = map_sql_identifier_to_xml_name(x->name, false, false);
17941807
else
17951808
newx->name = NULL;
17961809
newx->xmloption = x->xmloption;
1810+
newx->type = XMLOID; /* this just marks the node as transformed */
1811+
newx->typmod = -1;
17971812
newx->location = x->location;
17981813

17991814
/*

src/include/nodes/primnodes.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -944,7 +944,8 @@ typedef struct MinMaxExpr
944944
*
945945
* Note: result type/typmod/collation are not stored, but can be deduced
946946
* from the XmlExprOp. The type/typmod fields are just used for display
947-
* purposes, and are NOT the true result type of the node.
947+
* purposes, and are NOT necessarily the true result type of the node.
948+
* (We also use type == InvalidOid to mark a not-yet-parse-analyzed XmlExpr.)
948949
*/
949950
typedef enum XmlExprOp
950951
{

0 commit comments

Comments
 (0)