summaryrefslogtreecommitdiff
path: root/src/backend/rewrite
diff options
context:
space:
mode:
authorMichael Paquier2019-12-18 07:23:02 +0000
committerMichael Paquier2019-12-18 07:23:02 +0000
commite1551f96e643a52a035c3b35777d968bc073f7fc (patch)
tree9010890c3289462dc42a51a4d03498423359d375 /src/backend/rewrite
parent04c8a69c0cccbc271e0feeb22a74c69fbd87c37e (diff)
Refactor attribute mappings used in logical tuple conversion
Tuple conversion support in tupconvert.c is able to convert rowtypes between two relations, inner and outer, which are logically equivalent but have a different ordering or even dropped columns (used mainly for inheritance tree and partitions). This makes use of attribute mappings, which are simple arrays made of AttrNumber elements with a length matching the number of attributes of the outer relation. The length of the attribute mapping has been treated as completely independent of the mapping itself until now, making it easy to pass down an incorrect mapping length. This commit refactors the code related to attribute mappings and moves it into an independent facility called attmap.c, extracted from tupconvert.c. This merges the attribute mapping with its length, avoiding to try to guess what is the length of a mapping to use as this is computed once, when the map is built. This will avoid mistakes like what has been fixed in dc816e58, which has used an incorrect mapping length by matching it with the number of attributes of an inner relation (a child partition) instead of an outer relation (a partitioned table). Author: Michael Paquier Reviewed-by: Amit Langote Discussion: https://postgr.es/m/20191121042556.GD153437@paquier.xyz
Diffstat (limited to 'src/backend/rewrite')
-rw-r--r--src/backend/rewrite/rewriteManip.c12
1 files changed, 5 insertions, 7 deletions
diff --git a/src/backend/rewrite/rewriteManip.c b/src/backend/rewrite/rewriteManip.c
index 93508c2a87e..12f7cadf3b4 100644
--- a/src/backend/rewrite/rewriteManip.c
+++ b/src/backend/rewrite/rewriteManip.c
@@ -1221,8 +1221,7 @@ typedef struct
{
int target_varno; /* RTE index to search for */
int sublevels_up; /* (current) nesting depth */
- const AttrNumber *attno_map; /* map array for user attnos */
- int map_length; /* number of entries in attno_map[] */
+ const AttrMap *attno_map; /* map array for user attnos */
Oid to_rowtype; /* change whole-row Vars to this type */
bool *found_whole_row; /* output flag */
} map_variable_attnos_context;
@@ -1249,11 +1248,11 @@ map_variable_attnos_mutator(Node *node,
if (attno > 0)
{
/* user-defined column, replace attno */
- if (attno > context->map_length ||
- context->attno_map[attno - 1] == 0)
+ if (attno > context->attno_map->maplen ||
+ context->attno_map->attnums[attno - 1] == 0)
elog(ERROR, "unexpected varattno %d in expression to be mapped",
attno);
- newvar->varattno = newvar->varoattno = context->attno_map[attno - 1];
+ newvar->varattno = newvar->varoattno = context->attno_map->attnums[attno - 1];
}
else if (attno == 0)
{
@@ -1350,7 +1349,7 @@ map_variable_attnos_mutator(Node *node,
Node *
map_variable_attnos(Node *node,
int target_varno, int sublevels_up,
- const AttrNumber *attno_map, int map_length,
+ const AttrMap *attno_map,
Oid to_rowtype, bool *found_whole_row)
{
map_variable_attnos_context context;
@@ -1358,7 +1357,6 @@ map_variable_attnos(Node *node,
context.target_varno = target_varno;
context.sublevels_up = sublevels_up;
context.attno_map = attno_map;
- context.map_length = map_length;
context.to_rowtype = to_rowtype;
context.found_whole_row = found_whole_row;