|
| 1 | +/*------------------------------------------------------------------------- |
| 2 | + * |
| 3 | + * attmap.c |
| 4 | + * Attribute mapping support. |
| 5 | + * |
| 6 | + * This file provides utility routines to build and manage attribute |
| 7 | + * mappings by comparing input and output TupleDescs. Such mappings |
| 8 | + * are typically used by DDL operating on inheritance and partition trees |
| 9 | + * to do a conversion between rowtypes logically equivalent but with |
| 10 | + * columns in a different order, taking into account dropped columns. |
| 11 | + * They are also used by the tuple conversion routines in tupconvert.c. |
| 12 | + * |
| 13 | + * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group |
| 14 | + * Portions Copyright (c) 1994, Regents of the University of California |
| 15 | + * |
| 16 | + * |
| 17 | + * IDENTIFICATION |
| 18 | + * src/backend/access/common/attmap.c |
| 19 | + * |
| 20 | + *------------------------------------------------------------------------- |
| 21 | + */ |
| 22 | + |
| 23 | +#include"postgres.h" |
| 24 | + |
| 25 | +#include"access/attmap.h" |
| 26 | +#include"access/htup_details.h" |
| 27 | +#include"utils/builtins.h" |
| 28 | + |
| 29 | + |
| 30 | +staticboolcheck_attrmap_match(TupleDescindesc, |
| 31 | +TupleDescoutdesc, |
| 32 | +AttrMap*attrMap); |
| 33 | + |
| 34 | +/* |
| 35 | + * make_attrmap |
| 36 | + * |
| 37 | + * Utility routine to allocate an attribute map in the current memory |
| 38 | + * context. |
| 39 | + */ |
| 40 | +AttrMap* |
| 41 | +make_attrmap(intmaplen) |
| 42 | +{ |
| 43 | +AttrMap*res; |
| 44 | + |
| 45 | +res= (AttrMap*)palloc0(sizeof(AttrMap)); |
| 46 | +res->maplen=maplen; |
| 47 | +res->attnums= (AttrNumber*)palloc0(sizeof(AttrNumber)*maplen); |
| 48 | +returnres; |
| 49 | +} |
| 50 | + |
| 51 | +/* |
| 52 | + * free_attrmap |
| 53 | + * |
| 54 | + * Utility routine to release an attribute map. |
| 55 | + */ |
| 56 | +void |
| 57 | +free_attrmap(AttrMap*map) |
| 58 | +{ |
| 59 | +pfree(map->attnums); |
| 60 | +pfree(map); |
| 61 | +} |
| 62 | + |
| 63 | +/* |
| 64 | + * build_attrmap_by_position |
| 65 | + * |
| 66 | + * Return a palloc'd bare attribute map for tuple conversion, matching input |
| 67 | + * and output columns by position. Dropped columns are ignored in both input |
| 68 | + * and output, marked as 0. This is normally a subroutine for |
| 69 | + * convert_tuples_by_position in tupconvert.c, but it can be used standalone. |
| 70 | + * |
| 71 | + * Note: the errdetail messages speak of indesc as the "returned" rowtype, |
| 72 | + * outdesc as the "expected" rowtype. This is okay for current uses but |
| 73 | + * might need generalization in future. |
| 74 | + */ |
| 75 | +AttrMap* |
| 76 | +build_attrmap_by_position(TupleDescindesc, |
| 77 | +TupleDescoutdesc, |
| 78 | +constchar*msg) |
| 79 | +{ |
| 80 | +AttrMap*attrMap; |
| 81 | +intnincols; |
| 82 | +intnoutcols; |
| 83 | +intn; |
| 84 | +inti; |
| 85 | +intj; |
| 86 | +boolsame; |
| 87 | + |
| 88 | +/* |
| 89 | + * The length is computed as the number of attributes of the expected |
| 90 | + * rowtype as it includes dropped attributes in its count. |
| 91 | + */ |
| 92 | +n=outdesc->natts; |
| 93 | +attrMap=make_attrmap(n); |
| 94 | + |
| 95 | +j=0;/* j is next physical input attribute */ |
| 96 | +nincols=noutcols=0;/* these count non-dropped attributes */ |
| 97 | +same= true; |
| 98 | +for (i=0;i<n;i++) |
| 99 | +{ |
| 100 | +Form_pg_attributeatt=TupleDescAttr(outdesc,i); |
| 101 | +Oidatttypid; |
| 102 | +int32atttypmod; |
| 103 | + |
| 104 | +if (att->attisdropped) |
| 105 | +continue;/* attrMap->attnums[i] is already 0 */ |
| 106 | +noutcols++; |
| 107 | +atttypid=att->atttypid; |
| 108 | +atttypmod=att->atttypmod; |
| 109 | +for (;j<indesc->natts;j++) |
| 110 | +{ |
| 111 | +att=TupleDescAttr(indesc,j); |
| 112 | +if (att->attisdropped) |
| 113 | +continue; |
| 114 | +nincols++; |
| 115 | + |
| 116 | +/* Found matching column, now check type */ |
| 117 | +if (atttypid!=att->atttypid|| |
| 118 | +(atttypmod!=att->atttypmod&&atttypmod >=0)) |
| 119 | +ereport(ERROR, |
| 120 | +(errcode(ERRCODE_DATATYPE_MISMATCH), |
| 121 | +errmsg_internal("%s",_(msg)), |
| 122 | +errdetail("Returned type %s does not match expected type %s in column %d.", |
| 123 | +format_type_with_typemod(att->atttypid, |
| 124 | +att->atttypmod), |
| 125 | +format_type_with_typemod(atttypid, |
| 126 | +atttypmod), |
| 127 | +noutcols))); |
| 128 | +attrMap->attnums[i]= (AttrNumber) (j+1); |
| 129 | +j++; |
| 130 | +break; |
| 131 | +} |
| 132 | +if (attrMap->attnums[i]==0) |
| 133 | +same= false;/* we'll complain below */ |
| 134 | +} |
| 135 | + |
| 136 | +/* Check for unused input columns */ |
| 137 | +for (;j<indesc->natts;j++) |
| 138 | +{ |
| 139 | +if (TupleDescAttr(indesc,j)->attisdropped) |
| 140 | +continue; |
| 141 | +nincols++; |
| 142 | +same= false;/* we'll complain below */ |
| 143 | +} |
| 144 | + |
| 145 | +/* Report column count mismatch using the non-dropped-column counts */ |
| 146 | +if (!same) |
| 147 | +ereport(ERROR, |
| 148 | +(errcode(ERRCODE_DATATYPE_MISMATCH), |
| 149 | +errmsg_internal("%s",_(msg)), |
| 150 | +errdetail("Number of returned columns (%d) does not match " |
| 151 | +"expected column count (%d).", |
| 152 | +nincols,noutcols))); |
| 153 | + |
| 154 | +/* Check if the map has a one-to-one match */ |
| 155 | +if (check_attrmap_match(indesc,outdesc,attrMap)) |
| 156 | +{ |
| 157 | +/* Runtime conversion is not needed */ |
| 158 | +free_attrmap(attrMap); |
| 159 | +returnNULL; |
| 160 | +} |
| 161 | + |
| 162 | +returnattrMap; |
| 163 | +} |
| 164 | + |
| 165 | +/* |
| 166 | + * build_attrmap_by_name |
| 167 | + * |
| 168 | + * Return a palloc'd bare attribute map for tuple conversion, matching input |
| 169 | + * and output columns by name. (Dropped columns are ignored in both input and |
| 170 | + * output.) This is normally a subroutine for convert_tuples_by_name in |
| 171 | + * tupconvert.c, but can be used standalone. |
| 172 | + */ |
| 173 | +AttrMap* |
| 174 | +build_attrmap_by_name(TupleDescindesc, |
| 175 | +TupleDescoutdesc) |
| 176 | +{ |
| 177 | +AttrMap*attrMap; |
| 178 | +intoutnatts; |
| 179 | +intinnatts; |
| 180 | +inti; |
| 181 | +intnextindesc=-1; |
| 182 | + |
| 183 | +outnatts=outdesc->natts; |
| 184 | +innatts=indesc->natts; |
| 185 | + |
| 186 | +attrMap=make_attrmap(outnatts); |
| 187 | +for (i=0;i<outnatts;i++) |
| 188 | +{ |
| 189 | +Form_pg_attributeoutatt=TupleDescAttr(outdesc,i); |
| 190 | +char*attname; |
| 191 | +Oidatttypid; |
| 192 | +int32atttypmod; |
| 193 | +intj; |
| 194 | + |
| 195 | +if (outatt->attisdropped) |
| 196 | +continue;/* attrMap->attnums[i] is already 0 */ |
| 197 | +attname=NameStr(outatt->attname); |
| 198 | +atttypid=outatt->atttypid; |
| 199 | +atttypmod=outatt->atttypmod; |
| 200 | + |
| 201 | +/* |
| 202 | + * Now search for an attribute with the same name in the indesc. It |
| 203 | + * seems likely that a partitioned table will have the attributes in |
| 204 | + * the same order as the partition, so the search below is optimized |
| 205 | + * for that case. It is possible that columns are dropped in one of |
| 206 | + * the relations, but not the other, so we use the 'nextindesc' |
| 207 | + * counter to track the starting point of the search. If the inner |
| 208 | + * loop encounters dropped columns then it will have to skip over |
| 209 | + * them, but it should leave 'nextindesc' at the correct position for |
| 210 | + * the next outer loop. |
| 211 | + */ |
| 212 | +for (j=0;j<innatts;j++) |
| 213 | +{ |
| 214 | +Form_pg_attributeinatt; |
| 215 | + |
| 216 | +nextindesc++; |
| 217 | +if (nextindesc >=innatts) |
| 218 | +nextindesc=0; |
| 219 | + |
| 220 | +inatt=TupleDescAttr(indesc,nextindesc); |
| 221 | +if (inatt->attisdropped) |
| 222 | +continue; |
| 223 | +if (strcmp(attname,NameStr(inatt->attname))==0) |
| 224 | +{ |
| 225 | +/* Found it, check type */ |
| 226 | +if (atttypid!=inatt->atttypid||atttypmod!=inatt->atttypmod) |
| 227 | +ereport(ERROR, |
| 228 | +(errcode(ERRCODE_DATATYPE_MISMATCH), |
| 229 | +errmsg("could not convert row type"), |
| 230 | +errdetail("Attribute \"%s\" of type %s does not match corresponding attribute of type %s.", |
| 231 | +attname, |
| 232 | +format_type_be(outdesc->tdtypeid), |
| 233 | +format_type_be(indesc->tdtypeid)))); |
| 234 | +attrMap->attnums[i]=inatt->attnum; |
| 235 | +break; |
| 236 | +} |
| 237 | +} |
| 238 | +if (attrMap->attnums[i]==0) |
| 239 | +ereport(ERROR, |
| 240 | +(errcode(ERRCODE_DATATYPE_MISMATCH), |
| 241 | +errmsg("could not convert row type"), |
| 242 | +errdetail("Attribute \"%s\" of type %s does not exist in type %s.", |
| 243 | +attname, |
| 244 | +format_type_be(outdesc->tdtypeid), |
| 245 | +format_type_be(indesc->tdtypeid)))); |
| 246 | +} |
| 247 | +returnattrMap; |
| 248 | +} |
| 249 | + |
| 250 | +/* |
| 251 | + * build_attrmap_by_name_if_req |
| 252 | + * |
| 253 | + * Returns mapping created by build_attrmap_by_name, or NULL if no |
| 254 | + * conversion is required. This is a convenience routine for |
| 255 | + * convert_tuples_by_name() in tupconvert.c and other functions, but it |
| 256 | + * can be used standalone. |
| 257 | + */ |
| 258 | +AttrMap* |
| 259 | +build_attrmap_by_name_if_req(TupleDescindesc, |
| 260 | +TupleDescoutdesc) |
| 261 | +{ |
| 262 | +AttrMap*attrMap; |
| 263 | + |
| 264 | +/* Verify compatibility and prepare attribute-number map */ |
| 265 | +attrMap=build_attrmap_by_name(indesc,outdesc); |
| 266 | + |
| 267 | +/* Check if the map has a one-to-one match */ |
| 268 | +if (check_attrmap_match(indesc,outdesc,attrMap)) |
| 269 | +{ |
| 270 | +/* Runtime conversion is not needed */ |
| 271 | +free_attrmap(attrMap); |
| 272 | +returnNULL; |
| 273 | +} |
| 274 | + |
| 275 | +returnattrMap; |
| 276 | +} |
| 277 | + |
| 278 | +/* |
| 279 | + * check_attrmap_match |
| 280 | + * |
| 281 | + * Check to see if the map is a one-to-one match, in which case we need |
| 282 | + * not to do a tuple conversion, and the attribute map is not necessary. |
| 283 | + */ |
| 284 | +staticbool |
| 285 | +check_attrmap_match(TupleDescindesc, |
| 286 | +TupleDescoutdesc, |
| 287 | +AttrMap*attrMap) |
| 288 | +{ |
| 289 | +inti; |
| 290 | + |
| 291 | +/* no match if attribute numbers are not the same */ |
| 292 | +if (indesc->natts!=outdesc->natts) |
| 293 | +return false; |
| 294 | + |
| 295 | +for (i=0;i<attrMap->maplen;i++) |
| 296 | +{ |
| 297 | +Form_pg_attributeinatt; |
| 298 | +Form_pg_attributeoutatt; |
| 299 | + |
| 300 | +if (attrMap->attnums[i]== (i+1)) |
| 301 | +continue; |
| 302 | + |
| 303 | +/* |
| 304 | + * If it's a dropped column and the corresponding input column is also |
| 305 | + * dropped, we don't need a conversion. However, attlen and attalign |
| 306 | + * must agree. |
| 307 | + */ |
| 308 | +inatt=TupleDescAttr(indesc,i); |
| 309 | +outatt=TupleDescAttr(outdesc,i); |
| 310 | +if (attrMap->attnums[i]==0&& |
| 311 | +inatt->attisdropped&& |
| 312 | +inatt->attlen==outatt->attlen&& |
| 313 | +inatt->attalign==outatt->attalign) |
| 314 | +continue; |
| 315 | + |
| 316 | +return false; |
| 317 | +} |
| 318 | + |
| 319 | +return true; |
| 320 | +} |