diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2016-11-22 15:19:57 -0500 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2016-11-22 15:20:10 -0500 |
commit | 906bfcad7ba7cb3863fe0e2a7810be8e3cd84fbd (patch) | |
tree | cfed5faaab5c2de39eedf658b804df67ad8c08aa /src/backend/parser/parse_target.c | |
parent | e8ac886c24776295dd9b025386a821061da8e4d1 (diff) | |
download | postgresql-906bfcad7ba7cb3863fe0e2a7810be8e3cd84fbd.tar.gz postgresql-906bfcad7ba7cb3863fe0e2a7810be8e3cd84fbd.zip |
Improve handling of "UPDATE ... SET (column_list) = row_constructor".
Previously, the right-hand side of a multiple-column assignment, if it
wasn't a sub-SELECT, had to be a simple parenthesized expression list,
because gram.y was responsible for "bursting" the construct into
independent column assignments. This had the minor defect that you
couldn't write ROW (though you should be able to, since the standard says
this is a row constructor), and the rather larger defect that unlike other
uses of row constructors, we would not expand a "foo.*" item into multiple
columns.
Fix that by changing the RHS to be just "a_expr" in the grammar, leaving
it to transformMultiAssignRef to separate the elements of a RowExpr;
which it will do only after performing standard transformation of the
RowExpr, so that "foo.*" behaves as expected.
The key reason we didn't do that before was the hard-wired handling of
DEFAULT tokens (SetToDefault nodes). This patch deals with that issue by
allowing DEFAULT in any a_expr and having parse analysis throw an error
if SetToDefault is found in an unexpected place. That's an improvement
anyway since the error can be more specific than just "syntax error".
The SQL standard suggests that the RHS could be any a_expr yielding a
suitable row value. This patch doesn't really move the goal posts in that
respect --- you're still limited to RowExpr or a sub-SELECT --- but it does
fix the grammar restriction, so it provides some tangible progress towards
a full implementation. And the limitation is now documented by an explicit
error message rather than an unhelpful "syntax error".
Discussion: <8542.1479742008@sss.pgh.pa.us>
Diffstat (limited to 'src/backend/parser/parse_target.c')
-rw-r--r-- | src/backend/parser/parse_target.c | 30 |
1 files changed, 25 insertions, 5 deletions
diff --git a/src/backend/parser/parse_target.c b/src/backend/parser/parse_target.c index a76c33f40ea..d440dec5564 100644 --- a/src/backend/parser/parse_target.c +++ b/src/backend/parser/parse_target.c @@ -91,7 +91,17 @@ transformTargetEntry(ParseState *pstate, { /* Transform the node if caller didn't do it already */ if (expr == NULL) - expr = transformExpr(pstate, node, exprKind); + { + /* + * If it's a SetToDefault node and we should allow that, pass it + * through unmodified. (transformExpr will throw the appropriate + * error if we're disallowing it.) + */ + if (exprKind == EXPR_KIND_UPDATE_SOURCE && IsA(node, SetToDefault)) + expr = node; + else + expr = transformExpr(pstate, node, exprKind); + } if (colname == NULL && !resjunk) { @@ -210,10 +220,13 @@ transformTargetList(ParseState *pstate, List *targetlist, * the input list elements are bare expressions without ResTarget decoration, * and the output elements are likewise just expressions without TargetEntry * decoration. We use this for ROW() and VALUES() constructs. + * + * exprKind is not enough to tell us whether to allow SetToDefault, so + * an additional flag is needed for that. */ List * transformExpressionList(ParseState *pstate, List *exprlist, - ParseExprKind exprKind) + ParseExprKind exprKind, bool allowDefault) { List *result = NIL; ListCell *lc; @@ -255,10 +268,17 @@ transformExpressionList(ParseState *pstate, List *exprlist, } /* - * Not "something.*", so transform as a single expression + * Not "something.*", so transform as a single expression. If it's a + * SetToDefault node and we should allow that, pass it through + * unmodified. (transformExpr will throw the appropriate error if + * we're disallowing it.) */ - result = lappend(result, - transformExpr(pstate, e, exprKind)); + if (allowDefault && IsA(e, SetToDefault)) + /* do nothing */ ; + else + e = transformExpr(pstate, e, exprKind); + + result = lappend(result, e); } /* Shouldn't have any multiassign items here */ |