diff --git a/src/backend/rewrite/rewriteManip.c b/src/backend/rewrite/rewriteManip.c index 9290c7f..1601a97 100644 --- a/src/backend/rewrite/rewriteManip.c +++ b/src/backend/rewrite/rewriteManip.c @@ -1224,7 +1224,6 @@ typedef struct /* Target type when converting whole-row vars */ Oid to_rowtype; bool *found_whole_row; /* output flag */ - bool coerced_var; /* var is under ConvertRowTypeExpr */ } map_variable_attnos_context; static Node * @@ -1268,29 +1267,22 @@ map_variable_attnos_mutator(Node *node, /* Don't convert unless necessary. */ if (context->to_rowtype != var->vartype) { + ConvertRowtypeExpr *r; + /* Var itself is converted to the requested type. */ newvar->vartype = context->to_rowtype; /* - * If this var is already under a ConvertRowtypeExpr, - * we don't have to add another one. + * And a conversion node on top to convert back to the + * original type. */ - if (!context->coerced_var) - { - ConvertRowtypeExpr *r; - - /* - * And a conversion node on top to convert back to - * the original type. - */ - r = makeNode(ConvertRowtypeExpr); - r->arg = (Expr *) newvar; - r->resulttype = var->vartype; - r->convertformat = COERCE_IMPLICIT_CAST; - r->location = -1; - - return (Node *) r; - } + r = makeNode(ConvertRowtypeExpr); + r->arg = (Expr *) newvar; + r->resulttype = var->vartype; + r->convertformat = COERCE_IMPLICIT_CAST; + r->location = -1; + + return (Node *) r; } } } @@ -1306,15 +1298,20 @@ map_variable_attnos_mutator(Node *node, * If this is coercing a var (which is typical), convert only the var, * as against adding another ConvertRowtypeExpr over it. */ - if (IsA(r->arg, Var)) + if (IsA(r->arg, Var) && ((Var *)r->arg)->varattno == 0) { ConvertRowtypeExpr *newnode; + Var *var = (Var *) r->arg; + Var *newvar = (Var *) palloc(sizeof(Var)); + + *newvar = *var; + /* Var itself is converted to the requested type. */ + if (OidIsValid(context->to_rowtype)) + newvar->vartype = context->to_rowtype; newnode = (ConvertRowtypeExpr *) palloc(sizeof(ConvertRowtypeExpr)); *newnode = *r; - context->coerced_var = true; - newnode->arg = (Expr *) map_variable_attnos_mutator((Node *) r->arg, context); - context->coerced_var = false; + newnode->arg = (Expr *) newvar; return (Node *) newnode; } @@ -1351,7 +1348,6 @@ map_variable_attnos(Node *node, context.map_length = map_length; context.to_rowtype = to_rowtype; context.found_whole_row = found_whole_row; - context.coerced_var = false; *found_whole_row = false;