Fix casts in insert into local table select from distributed

onder_ins_select
Marco Slot 2022-07-13 16:13:33 +02:00
parent 14c56ce0dd
commit 8182d2d095
1 changed files with 22 additions and 16 deletions

View File

@ -370,14 +370,27 @@ CreateDistributedInsertSelectPlan(Query *originalQuery,
* combineQuery, this function also creates a dummy combineQuery for that. * combineQuery, this function also creates a dummy combineQuery for that.
*/ */
DistributedPlan * DistributedPlan *
CreateInsertSelectIntoLocalTablePlan(uint64 planId, Query *originalQuery, ParamListInfo CreateInsertSelectIntoLocalTablePlan(uint64 planId, Query *insertSelectQuery,
boundParams, bool hasUnresolvedParams, ParamListInfo boundParams, bool hasUnresolvedParams,
PlannerRestrictionContext *plannerRestrictionContext) PlannerRestrictionContext *plannerRestrictionContext)
{ {
RangeTblEntry *selectRte = ExtractSelectRangeTableEntry(originalQuery); RangeTblEntry *insertRte = ExtractResultRelationRTEOrError(insertSelectQuery);
Oid targetRelationId = insertRte->relid;
RangeTblEntry *selectRte = ExtractSelectRangeTableEntry(insertSelectQuery);
Query *selectQuery = BuildSelectForInsertSelect(insertSelectQuery);
/*
* Cast types of insert target list and select projection list to
* match the column types of the target relation.
*/
selectQuery->targetList =
AddInsertSelectCasts(insertSelectQuery->targetList,
selectQuery->targetList,
targetRelationId);
insertSelectQuery->cteList = NIL;
Query *selectQuery = BuildSelectForInsertSelect(originalQuery);
originalQuery->cteList = NIL;
DistributedPlan *distPlan = CreateDistributedPlan(planId, selectQuery, DistributedPlan *distPlan = CreateDistributedPlan(planId, selectQuery,
copyObject(selectQuery), copyObject(selectQuery),
boundParams, hasUnresolvedParams, boundParams, hasUnresolvedParams,
@ -417,7 +430,7 @@ CreateInsertSelectIntoLocalTablePlan(uint64 planId, Query *originalQuery, ParamL
* distributed select instead of returning it. * distributed select instead of returning it.
*/ */
selectRte->subquery = distPlan->combineQuery; selectRte->subquery = distPlan->combineQuery;
distPlan->combineQuery = originalQuery; distPlan->combineQuery = insertSelectQuery;
return distPlan; return distPlan;
} }
@ -1536,30 +1549,23 @@ AddInsertSelectCasts(List *insertTargetList, List *selectTargetList,
List *projectedEntries = NIL; List *projectedEntries = NIL;
List *nonProjectedEntries = NIL; List *nonProjectedEntries = NIL;
/*
* ReorderInsertSelectTargetLists() makes sure that first few columns of
* the SELECT query match the insert targets. It might contain additional
* items for GROUP BY, etc.
*/
Assert(list_length(insertTargetList) <= list_length(selectTargetList));
Relation distributedRelation = table_open(targetRelationId, RowExclusiveLock); Relation distributedRelation = table_open(targetRelationId, RowExclusiveLock);
TupleDesc destTupleDescriptor = RelationGetDescr(distributedRelation); TupleDesc destTupleDescriptor = RelationGetDescr(distributedRelation);
int targetEntryIndex = 0; int targetEntryIndex = 0;
TargetEntry *insertEntry = NULL; TargetEntry *insertEntry = NULL;
TargetEntry *selectEntry = NULL; TargetEntry *selectEntry = NULL;
forboth_ptr(insertEntry, insertTargetList, selectEntry, selectTargetList) forboth_ptr(insertEntry, insertTargetList, selectEntry, selectTargetList)
{ {
Var *insertColumn = (Var *) insertEntry->expr;
Form_pg_attribute attr = TupleDescAttr(destTupleDescriptor, Form_pg_attribute attr = TupleDescAttr(destTupleDescriptor,
insertEntry->resno - 1); insertEntry->resno - 1);
Oid sourceType = insertColumn->vartype; Oid sourceType = exprType((Node *) selectEntry->expr);
Oid targetType = attr->atttypid; Oid targetType = attr->atttypid;
if (sourceType != targetType) if (sourceType != targetType)
{ {
insertEntry->expr = CastExpr((Expr *) insertColumn, sourceType, targetType, insertEntry->expr = CastExpr(insertEntry->expr, sourceType, targetType,
attr->attcollation, attr->atttypmod); attr->attcollation, attr->atttypmod);
/* /*