|
| 1 | +/*------------------------------------------------------------------------- |
| 2 | + * |
| 3 | + * tupconvert.c |
| 4 | + * Tuple conversion support. |
| 5 | + * |
| 6 | + * These functions provide conversion between rowtypes that are logically |
| 7 | + * equivalent but might have columns in a different order or different sets |
| 8 | + * of dropped columns. There is some overlap of functionality with the |
| 9 | + * executor's "junkfilter" routines, but these functions work on bare |
| 10 | + * HeapTuples rather than TupleTableSlots. |
| 11 | + * |
| 12 | + * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group |
| 13 | + * Portions Copyright (c) 1994, Regents of the University of California |
| 14 | + * |
| 15 | + * |
| 16 | + * IDENTIFICATION |
| 17 | + * $PostgreSQL: pgsql/src/backend/access/common/tupconvert.c,v 1.1 2009/08/06 20:44:31 tgl Exp $ |
| 18 | + * |
| 19 | + *------------------------------------------------------------------------- |
| 20 | + */ |
| 21 | +#include"postgres.h" |
| 22 | + |
| 23 | +#include"access/tupconvert.h" |
| 24 | +#include"utils/builtins.h" |
| 25 | + |
| 26 | + |
| 27 | +/* |
| 28 | + * The conversion setup routines have the following common API: |
| 29 | + * |
| 30 | + * The setup routine checks whether the given source and destination tuple |
| 31 | + * descriptors are logically compatible. If not, it throws an error. |
| 32 | + * If so, it returns NULL if they are physically compatible (ie, no conversion |
| 33 | + * is needed), else a TupleConversionMap that can be used by do_convert_tuple |
| 34 | + * to perform the conversion. |
| 35 | + * |
| 36 | + * The TupleConversionMap, if needed, is palloc'd in the caller's memory |
| 37 | + * context. Also, the given tuple descriptors are referenced by the map, |
| 38 | + * so they must survive as long as the map is needed. |
| 39 | + * |
| 40 | + * The caller must supply a suitable primary error message to be used if |
| 41 | + * a compatibility error is thrown. Recommended coding practice is to use |
| 42 | + * gettext_noop() on this string, so that it is translatable but won't |
| 43 | + * actually be translated unless the error gets thrown. |
| 44 | + * |
| 45 | + * |
| 46 | + * Implementation notes: |
| 47 | + * |
| 48 | + * The key component of a TupleConversionMap is an attrMap[] array with |
| 49 | + * one entry per output column. This entry contains the 1-based index of |
| 50 | + * the corresponding input column, or zero to force a NULL value (for |
| 51 | + * a dropped output column). The TupleConversionMap also contains workspace |
| 52 | + * arrays. |
| 53 | + */ |
| 54 | + |
| 55 | + |
| 56 | +/* |
| 57 | + * Set up for tuple conversion, matching input and output columns by |
| 58 | + * position. (Dropped columns are ignored in both input and output.) |
| 59 | + * |
| 60 | + * Note: the errdetail messages speak of indesc as the "returned" rowtype, |
| 61 | + * outdesc as the "expected" rowtype. This is okay for current uses but |
| 62 | + * might need generalization in future. |
| 63 | + */ |
| 64 | +TupleConversionMap* |
| 65 | +convert_tuples_by_position(TupleDescindesc, |
| 66 | +TupleDescoutdesc, |
| 67 | +constchar*msg) |
| 68 | +{ |
| 69 | +TupleConversionMap*map; |
| 70 | +AttrNumber*attrMap; |
| 71 | +intnincols; |
| 72 | +intnoutcols; |
| 73 | +intn; |
| 74 | +inti; |
| 75 | +intj; |
| 76 | +boolsame; |
| 77 | + |
| 78 | +/* Verify compatibility and prepare attribute-number map */ |
| 79 | +n=outdesc->natts; |
| 80 | +attrMap= (AttrNumber*)palloc0(n*sizeof(AttrNumber)); |
| 81 | +j=0;/* j is next physical input attribute */ |
| 82 | +nincols=noutcols=0;/* these count non-dropped attributes */ |
| 83 | +same= true; |
| 84 | +for (i=0;i<n;i++) |
| 85 | +{ |
| 86 | +Form_pg_attributeatt=outdesc->attrs[i]; |
| 87 | +Oidatttypid; |
| 88 | +int32atttypmod; |
| 89 | + |
| 90 | +if (att->attisdropped) |
| 91 | +continue;/* attrMap[i] is already 0 */ |
| 92 | +noutcols++; |
| 93 | +atttypid=att->atttypid; |
| 94 | +atttypmod=att->atttypmod; |
| 95 | +for (;j<indesc->natts;j++) |
| 96 | +{ |
| 97 | +att=indesc->attrs[j]; |
| 98 | +if (att->attisdropped) |
| 99 | +continue; |
| 100 | +nincols++; |
| 101 | +/* Found matching column, check type */ |
| 102 | +if (atttypid!=att->atttypid|| |
| 103 | +(atttypmod!=att->atttypmod&&atttypmod >=0)) |
| 104 | +ereport(ERROR, |
| 105 | +(errcode(ERRCODE_DATATYPE_MISMATCH), |
| 106 | +errmsg_internal("%s",_(msg)), |
| 107 | +errdetail("Returned type %s does not match expected type %s in column %d.", |
| 108 | +format_type_with_typemod(att->atttypid, |
| 109 | +att->atttypmod), |
| 110 | +format_type_with_typemod(atttypid, |
| 111 | +atttypmod), |
| 112 | +noutcols))); |
| 113 | +attrMap[i]= (AttrNumber) (j+1); |
| 114 | +j++; |
| 115 | +break; |
| 116 | +} |
| 117 | +if (attrMap[i]==0) |
| 118 | +same= false;/* we'll complain below */ |
| 119 | +} |
| 120 | + |
| 121 | +/* Check for unused input columns */ |
| 122 | +for (;j<indesc->natts;j++) |
| 123 | +{ |
| 124 | +if (indesc->attrs[j]->attisdropped) |
| 125 | +continue; |
| 126 | +nincols++; |
| 127 | +same= false;/* we'll complain below */ |
| 128 | +} |
| 129 | + |
| 130 | +/* Report column count mismatch using the non-dropped-column counts */ |
| 131 | +if (!same) |
| 132 | +ereport(ERROR, |
| 133 | +(errcode(ERRCODE_DATATYPE_MISMATCH), |
| 134 | +errmsg_internal("%s",_(msg)), |
| 135 | +errdetail("Number of returned columns (%d) does not match " |
| 136 | +"expected column count (%d).", |
| 137 | +nincols,noutcols))); |
| 138 | + |
| 139 | +/* |
| 140 | + * Check to see if the map is one-to-one and the tuple types are the |
| 141 | + * same. (We check the latter because if they're not, we want to do |
| 142 | + * conversion to inject the right OID into the tuple datum.) |
| 143 | + */ |
| 144 | +if (indesc->natts==outdesc->natts&& |
| 145 | +indesc->tdtypeid==outdesc->tdtypeid) |
| 146 | +{ |
| 147 | +for (i=0;i<n;i++) |
| 148 | +{ |
| 149 | +if (attrMap[i]!= (i+1)) |
| 150 | +{ |
| 151 | +same= false; |
| 152 | +break; |
| 153 | +} |
| 154 | +} |
| 155 | +} |
| 156 | +else |
| 157 | +same= false; |
| 158 | + |
| 159 | +if (same) |
| 160 | +{ |
| 161 | +/* Runtime conversion is not needed */ |
| 162 | +pfree(attrMap); |
| 163 | +returnNULL; |
| 164 | +} |
| 165 | + |
| 166 | +/* Prepare the map structure */ |
| 167 | +map= (TupleConversionMap*)palloc(sizeof(TupleConversionMap)); |
| 168 | +map->indesc=indesc; |
| 169 | +map->outdesc=outdesc; |
| 170 | +map->attrMap=attrMap; |
| 171 | +/* preallocate workspace for Datum arrays */ |
| 172 | +map->outvalues= (Datum*)palloc(n*sizeof(Datum)); |
| 173 | +map->outisnull= (bool*)palloc(n*sizeof(bool)); |
| 174 | +n=indesc->natts+1;/* +1 for NULL */ |
| 175 | +map->invalues= (Datum*)palloc(n*sizeof(Datum)); |
| 176 | +map->inisnull= (bool*)palloc(n*sizeof(bool)); |
| 177 | +map->invalues[0]= (Datum)0;/* set up the NULL entry */ |
| 178 | +map->inisnull[0]= true; |
| 179 | + |
| 180 | +returnmap; |
| 181 | +} |
| 182 | + |
| 183 | +/* |
| 184 | + * Set up for tuple conversion, matching input and output columns by name. |
| 185 | + * (Dropped columns are ignored in both input and output.) This is intended |
| 186 | + * for use when the rowtypes are related by inheritance, so we expect an exact |
| 187 | + * match of both type and typmod. The error messages will be a bit unhelpful |
| 188 | + * unless both rowtypes are named composite types. |
| 189 | + */ |
| 190 | +TupleConversionMap* |
| 191 | +convert_tuples_by_name(TupleDescindesc, |
| 192 | +TupleDescoutdesc, |
| 193 | +constchar*msg) |
| 194 | +{ |
| 195 | +TupleConversionMap*map; |
| 196 | +AttrNumber*attrMap; |
| 197 | +intn; |
| 198 | +inti; |
| 199 | +boolsame; |
| 200 | + |
| 201 | +/* Verify compatibility and prepare attribute-number map */ |
| 202 | +n=outdesc->natts; |
| 203 | +attrMap= (AttrNumber*)palloc0(n*sizeof(AttrNumber)); |
| 204 | +for (i=0;i<n;i++) |
| 205 | +{ |
| 206 | +Form_pg_attributeatt=outdesc->attrs[i]; |
| 207 | +char*attname; |
| 208 | +Oidatttypid; |
| 209 | +int32atttypmod; |
| 210 | +intj; |
| 211 | + |
| 212 | +if (att->attisdropped) |
| 213 | +continue;/* attrMap[i] is already 0 */ |
| 214 | +attname=NameStr(att->attname); |
| 215 | +atttypid=att->atttypid; |
| 216 | +atttypmod=att->atttypmod; |
| 217 | +for (j=0;j<indesc->natts;j++) |
| 218 | +{ |
| 219 | +att=indesc->attrs[j]; |
| 220 | +if (att->attisdropped) |
| 221 | +continue; |
| 222 | +if (strcmp(attname,NameStr(att->attname))==0) |
| 223 | +{ |
| 224 | +/* Found it, check type */ |
| 225 | +if (atttypid!=att->atttypid||atttypmod!=att->atttypmod) |
| 226 | +ereport(ERROR, |
| 227 | +(errcode(ERRCODE_DATATYPE_MISMATCH), |
| 228 | +errmsg_internal("%s",_(msg)), |
| 229 | +errdetail("Attribute \"%s\" of type %s does not match corresponding attribute of type %s.", |
| 230 | +attname, |
| 231 | +format_type_be(outdesc->tdtypeid), |
| 232 | +format_type_be(indesc->tdtypeid)))); |
| 233 | +attrMap[i]= (AttrNumber) (j+1); |
| 234 | +break; |
| 235 | +} |
| 236 | +} |
| 237 | +if (attrMap[i]==0) |
| 238 | +ereport(ERROR, |
| 239 | +(errcode(ERRCODE_DATATYPE_MISMATCH), |
| 240 | +errmsg_internal("%s",_(msg)), |
| 241 | +errdetail("Attribute \"%s\" of type %s does not exist in type %s.", |
| 242 | +attname, |
| 243 | +format_type_be(outdesc->tdtypeid), |
| 244 | +format_type_be(indesc->tdtypeid)))); |
| 245 | +} |
| 246 | + |
| 247 | +/* |
| 248 | + * Check to see if the map is one-to-one and the tuple types are the |
| 249 | + * same. (We check the latter because if they're not, we want to do |
| 250 | + * conversion to inject the right OID into the tuple datum.) |
| 251 | + */ |
| 252 | +if (indesc->natts==outdesc->natts&& |
| 253 | +indesc->tdtypeid==outdesc->tdtypeid) |
| 254 | +{ |
| 255 | +same= true; |
| 256 | +for (i=0;i<n;i++) |
| 257 | +{ |
| 258 | +if (attrMap[i]!= (i+1)) |
| 259 | +{ |
| 260 | +same= false; |
| 261 | +break; |
| 262 | +} |
| 263 | +} |
| 264 | +} |
| 265 | +else |
| 266 | +same= false; |
| 267 | + |
| 268 | +if (same) |
| 269 | +{ |
| 270 | +/* Runtime conversion is not needed */ |
| 271 | +pfree(attrMap); |
| 272 | +returnNULL; |
| 273 | +} |
| 274 | + |
| 275 | +/* Prepare the map structure */ |
| 276 | +map= (TupleConversionMap*)palloc(sizeof(TupleConversionMap)); |
| 277 | +map->indesc=indesc; |
| 278 | +map->outdesc=outdesc; |
| 279 | +map->attrMap=attrMap; |
| 280 | +/* preallocate workspace for Datum arrays */ |
| 281 | +map->outvalues= (Datum*)palloc(n*sizeof(Datum)); |
| 282 | +map->outisnull= (bool*)palloc(n*sizeof(bool)); |
| 283 | +n=indesc->natts+1;/* +1 for NULL */ |
| 284 | +map->invalues= (Datum*)palloc(n*sizeof(Datum)); |
| 285 | +map->inisnull= (bool*)palloc(n*sizeof(bool)); |
| 286 | +map->invalues[0]= (Datum)0;/* set up the NULL entry */ |
| 287 | +map->inisnull[0]= true; |
| 288 | + |
| 289 | +returnmap; |
| 290 | +} |
| 291 | + |
| 292 | +/* |
| 293 | + * Perform conversion of a tuple according to the map. |
| 294 | + */ |
| 295 | +HeapTuple |
| 296 | +do_convert_tuple(HeapTupletuple,TupleConversionMap*map) |
| 297 | +{ |
| 298 | +AttrNumber*attrMap=map->attrMap; |
| 299 | +Datum*invalues=map->invalues; |
| 300 | +bool*inisnull=map->inisnull; |
| 301 | +Datum*outvalues=map->outvalues; |
| 302 | +bool*outisnull=map->outisnull; |
| 303 | +intoutnatts=map->outdesc->natts; |
| 304 | +inti; |
| 305 | + |
| 306 | +/* |
| 307 | + * Extract all the values of the old tuple, offsetting the arrays so that |
| 308 | + * invalues[0] is left NULL and invalues[1] is the first source attribute; |
| 309 | + * this exactly matches the numbering convention in attrMap. |
| 310 | + */ |
| 311 | +heap_deform_tuple(tuple,map->indesc,invalues+1,inisnull+1); |
| 312 | + |
| 313 | +/* |
| 314 | + * Transpose into proper fields of the new tuple. |
| 315 | + */ |
| 316 | +for (i=0;i<outnatts;i++) |
| 317 | +{ |
| 318 | +intj=attrMap[i]; |
| 319 | + |
| 320 | +outvalues[i]=invalues[j]; |
| 321 | +outisnull[i]=inisnull[j]; |
| 322 | +} |
| 323 | + |
| 324 | +/* |
| 325 | + * Now form the new tuple. |
| 326 | + */ |
| 327 | +returnheap_form_tuple(map->outdesc,outvalues,outisnull); |
| 328 | +} |
| 329 | + |
| 330 | +/* |
| 331 | + * Free a TupleConversionMap structure. |
| 332 | + */ |
| 333 | +void |
| 334 | +free_conversion_map(TupleConversionMap*map) |
| 335 | +{ |
| 336 | +/* indesc and outdesc are not ours to free */ |
| 337 | +pfree(map->attrMap); |
| 338 | +pfree(map->invalues); |
| 339 | +pfree(map->inisnull); |
| 340 | +pfree(map->outvalues); |
| 341 | +pfree(map->outisnull); |
| 342 | +pfree(map); |
| 343 | +} |