15
15
#include "access/sysattr.h"
16
16
#include "optimizer/restrictinfo.h"
17
17
#include "optimizer/var.h"
18
+ #include "rewrite/rewriteManip.h"
18
19
#include "utils/memutils.h"
19
20
20
21
@@ -112,31 +113,19 @@ select_required_plans(HTAB *children_table, Oid *parts, int nparts, int *nres)
112
113
return result ;
113
114
}
114
115
115
- /* Replace Vars' varnos with the value provided by 'parent ' */
116
+ /* Replace 'varno' of child's Vars with the 'append_rel_rti ' */
116
117
static List *
117
- replace_tlist_varnos (List * child_tlist , RelOptInfo * parent )
118
+ replace_tlist_varnos (List * tlist , Index old_varno , Index new_varno )
118
119
{
119
- ListCell * lc ;
120
- List * result = NIL ;
121
- int i = 1 ; /* resnos begin with 1 */
122
-
123
- foreach (lc , child_tlist )
124
- {
125
- Var * var = (Var * ) ((TargetEntry * ) lfirst (lc ))-> expr ;
126
- Var * newvar = (Var * ) palloc (sizeof (Var ));
120
+ List * temp_tlist ;
127
121
128
- Assert (IsA (var , Var ));
122
+ AssertArg (old_varno != 0 );
123
+ AssertArg (new_varno != 0 );
129
124
130
- * newvar = * var ;
131
- newvar -> varno = parent -> relid ;
132
- newvar -> varnoold = parent -> relid ;
125
+ temp_tlist = copyObject (tlist );
126
+ ChangeVarNodes ((Node * ) temp_tlist , old_varno , new_varno , 0 );
133
127
134
- result = lappend (result , makeTargetEntry ((Expr * ) newvar ,
135
- i ++ , /* item's index */
136
- NULL , false));
137
- }
138
-
139
- return result ;
128
+ return temp_tlist ;
140
129
}
141
130
142
131
/* Append partition attribute in case it's not present in target list */
@@ -421,7 +410,9 @@ create_append_plan_common(PlannerInfo *root, RelOptInfo *rel,
421
410
422
411
/* Replace rel's tlist with a matching one */
423
412
if (!cscan -> scan .plan .targetlist )
424
- tlist = replace_tlist_varnos (child_plan -> targetlist , rel );
413
+ tlist = replace_tlist_varnos (child_plan -> targetlist ,
414
+ child_rel -> relid ,
415
+ rel -> relid );
425
416
426
417
/* Add partition attribute if necessary (for ExecQual()) */
427
418
child_plan -> targetlist = append_part_attr_to_tlist (child_plan -> targetlist ,
@@ -431,7 +422,8 @@ create_append_plan_common(PlannerInfo *root, RelOptInfo *rel,
431
422
/* Now make custom_scan_tlist match child plans' targetlists */
432
423
if (!cscan -> custom_scan_tlist )
433
424
cscan -> custom_scan_tlist = replace_tlist_varnos (child_plan -> targetlist ,
434
- rel );
425
+ child_rel -> relid ,
426
+ rel -> relid );
435
427
}
436
428
}
437
429
0 commit comments