mirror of
https://github.com/postgres/postgres.git
synced 2025-06-30 21:42:05 +03:00
Make INSERT-from-multiple-VALUES-rows handle targetlist indirection better.
Previously, if an INSERT with multiple rows of VALUES had indirection (array subscripting or field selection) in its target-columns list, the parser handled that by applying transformAssignedExpr() to each element of each VALUES row independently. This led to having ArrayRef assignment nodes or FieldStore nodes in each row of the VALUES RTE. That works for simple cases, but in bug #14265 Nuri Boardman points out that it fails if there are multiple assignments to elements/fields of the same target column. For such cases to work, rewriteTargetListIU() has to nest the ArrayRefs or FieldStores together to produce a single expression to be assigned to the column. But it failed to find them in the top-level targetlist and issued an error about "multiple assignments to same column". We could possibly fix this by teaching the rewriter to apply rewriteTargetListIU to each VALUES row separately, but that would be messy (it would change the output rowtype of the VALUES RTE, for example) and inefficient. Instead, let's fix the parser so that the VALUES RTE outputs are just the user-specified values, cast to the right type if necessary, and then the ArrayRefs or FieldStores are applied in the top-level targetlist to Vars representing the RTE's outputs. This is the same parsetree representation already used for similar cases with INSERT/SELECT syntax, so it allows simplifications in ruleutils.c, which no longer needs to treat INSERT-from-multiple-VALUES as its own special case. This implementation works by applying transformAssignedExpr to the VALUES entries as before, and then stripping off any ArrayRefs or FieldStores it adds. With lots of VALUES rows it would be noticeably more efficient to not add those nodes in the first place. But that's just an optimization not a bug fix, and there doesn't seem to be any good way to do it without significant refactoring. (A non-invasive answer would be to apply transformAssignedExpr + stripping to just the first VALUES row, and then just forcibly cast remaining rows to the same data types exposed in the first row. But this way would lead to different, not-INSERT-specific errors being reported in casting failure cases, so it doesn't seem very nice.) So leave that for later; this patch at least isn't making the per-row parsing work worse, and it does make the finished parsetree smaller, saving rewriter and planner work. Catversion bump because stored rules containing such INSERTs would need to change. Because of that, no back-patch, even though this is a very long-standing bug. Report: <20160727005725.7438.26021@wrigleys.postgresql.org> Discussion: <9578.1469645245@sss.pgh.pa.us>
This commit is contained in:
@ -51,7 +51,8 @@ post_parse_analyze_hook_type post_parse_analyze_hook = NULL;
|
||||
static Query *transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt);
|
||||
static Query *transformInsertStmt(ParseState *pstate, InsertStmt *stmt);
|
||||
static List *transformInsertRow(ParseState *pstate, List *exprlist,
|
||||
List *stmtcols, List *icolumns, List *attrnos);
|
||||
List *stmtcols, List *icolumns, List *attrnos,
|
||||
bool strip_indirection);
|
||||
static OnConflictExpr *transformOnConflictClause(ParseState *pstate,
|
||||
OnConflictClause *onConflictClause);
|
||||
static int count_rowexpr_columns(ParseState *pstate, Node *expr);
|
||||
@ -619,7 +620,8 @@ transformInsertStmt(ParseState *pstate, InsertStmt *stmt)
|
||||
/* Prepare row for assignment to target table */
|
||||
exprList = transformInsertRow(pstate, exprList,
|
||||
stmt->cols,
|
||||
icolumns, attrnos);
|
||||
icolumns, attrnos,
|
||||
false);
|
||||
}
|
||||
else if (list_length(selectStmt->valuesLists) > 1)
|
||||
{
|
||||
@ -663,10 +665,20 @@ transformInsertStmt(ParseState *pstate, InsertStmt *stmt)
|
||||
exprLocation((Node *) sublist))));
|
||||
}
|
||||
|
||||
/* Prepare row for assignment to target table */
|
||||
/*
|
||||
* Prepare row for assignment to target table. We process any
|
||||
* indirection on the target column specs normally but then strip
|
||||
* off the resulting field/array assignment nodes, since we don't
|
||||
* want the parsed statement to contain copies of those in each
|
||||
* VALUES row. (It's annoying to have to transform the
|
||||
* indirection specs over and over like this, but avoiding it
|
||||
* would take some really messy refactoring of
|
||||
* transformAssignmentIndirection.)
|
||||
*/
|
||||
sublist = transformInsertRow(pstate, sublist,
|
||||
stmt->cols,
|
||||
icolumns, attrnos);
|
||||
icolumns, attrnos,
|
||||
true);
|
||||
|
||||
/*
|
||||
* We must assign collations now because assign_query_collations
|
||||
@ -717,6 +729,14 @@ transformInsertStmt(ParseState *pstate, InsertStmt *stmt)
|
||||
* Generate list of Vars referencing the RTE
|
||||
*/
|
||||
expandRTE(rte, rtr->rtindex, 0, -1, false, NULL, &exprList);
|
||||
|
||||
/*
|
||||
* Re-apply any indirection on the target column specs to the Vars
|
||||
*/
|
||||
exprList = transformInsertRow(pstate, exprList,
|
||||
stmt->cols,
|
||||
icolumns, attrnos,
|
||||
false);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -739,7 +759,8 @@ transformInsertStmt(ParseState *pstate, InsertStmt *stmt)
|
||||
/* Prepare row for assignment to target table */
|
||||
exprList = transformInsertRow(pstate, exprList,
|
||||
stmt->cols,
|
||||
icolumns, attrnos);
|
||||
icolumns, attrnos,
|
||||
false);
|
||||
}
|
||||
|
||||
/*
|
||||
@ -808,12 +829,17 @@ transformInsertStmt(ParseState *pstate, InsertStmt *stmt)
|
||||
/*
|
||||
* Prepare an INSERT row for assignment to the target table.
|
||||
*
|
||||
* The row might be either a VALUES row, or variables referencing a
|
||||
* sub-SELECT output.
|
||||
* exprlist: transformed expressions for source values; these might come from
|
||||
* a VALUES row, or be Vars referencing a sub-SELECT or VALUES RTE output.
|
||||
* stmtcols: original target-columns spec for INSERT (we just test for NIL)
|
||||
* icolumns: effective target-columns spec (list of ResTarget)
|
||||
* attrnos: integer column numbers (must be same length as icolumns)
|
||||
* strip_indirection: if true, remove any field/array assignment nodes
|
||||
*/
|
||||
static List *
|
||||
transformInsertRow(ParseState *pstate, List *exprlist,
|
||||
List *stmtcols, List *icolumns, List *attrnos)
|
||||
List *stmtcols, List *icolumns, List *attrnos,
|
||||
bool strip_indirection)
|
||||
{
|
||||
List *result;
|
||||
ListCell *lc;
|
||||
@ -879,6 +905,29 @@ transformInsertRow(ParseState *pstate, List *exprlist,
|
||||
col->indirection,
|
||||
col->location);
|
||||
|
||||
if (strip_indirection)
|
||||
{
|
||||
while (expr)
|
||||
{
|
||||
if (IsA(expr, FieldStore))
|
||||
{
|
||||
FieldStore *fstore = (FieldStore *) expr;
|
||||
|
||||
expr = (Expr *) linitial(fstore->newvals);
|
||||
}
|
||||
else if (IsA(expr, ArrayRef))
|
||||
{
|
||||
ArrayRef *aref = (ArrayRef *) expr;
|
||||
|
||||
if (aref->refassgnexpr == NULL)
|
||||
break;
|
||||
expr = aref->refassgnexpr;
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
result = lappend(result, expr);
|
||||
|
||||
icols = lnext(icols);
|
||||
|
Reference in New Issue
Block a user