1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
|
/*-------------------------------------------------------------------------
*
* tupconvert.c
* Tuple conversion support.
*
* These functions provide conversion between rowtypes that are logically
* equivalent but might have columns in a different order or different sets of
* dropped columns.
*
* Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* src/backend/access/common/tupconvert.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "access/htup_details.h"
#include "access/tupconvert.h"
#include "executor/tuptable.h"
#include "utils/builtins.h"
/*
* The conversion setup routines have the following common API:
*
* The setup routine checks whether the given source and destination tuple
* descriptors are logically compatible. If not, it throws an error.
* If so, it returns NULL if they are physically compatible (ie, no conversion
* is needed), else a TupleConversionMap that can be used by execute_attr_map_tuple
* to perform the conversion.
*
* The TupleConversionMap, if needed, is palloc'd in the caller's memory
* context. Also, the given tuple descriptors are referenced by the map,
* so they must survive as long as the map is needed.
*
* The caller must supply a suitable primary error message to be used if
* a compatibility error is thrown. Recommended coding practice is to use
* gettext_noop() on this string, so that it is translatable but won't
* actually be translated unless the error gets thrown.
*
*
* Implementation notes:
*
* The key component of a TupleConversionMap is an attrMap[] array with
* one entry per output column. This entry contains the 1-based index of
* the corresponding input column, or zero to force a NULL value (for
* a dropped output column). The TupleConversionMap also contains workspace
* arrays.
*/
/*
* Set up for tuple conversion, matching input and output columns by
* position. (Dropped columns are ignored in both input and output.)
*
* Note: the errdetail messages speak of indesc as the "returned" rowtype,
* outdesc as the "expected" rowtype. This is okay for current uses but
* might need generalization in future.
*/
TupleConversionMap *
convert_tuples_by_position(TupleDesc indesc,
TupleDesc outdesc,
const char *msg)
{
TupleConversionMap *map;
AttrNumber *attrMap;
int nincols;
int noutcols;
int n;
int i;
int j;
bool same;
/* Verify compatibility and prepare attribute-number map */
n = outdesc->natts;
attrMap = (AttrNumber *) palloc0(n * sizeof(AttrNumber));
j = 0; /* j is next physical input attribute */
nincols = noutcols = 0; /* these count non-dropped attributes */
same = true;
for (i = 0; i < n; i++)
{
Form_pg_attribute att = TupleDescAttr(outdesc, i);
Oid atttypid;
int32 atttypmod;
if (att->attisdropped)
continue; /* attrMap[i] is already 0 */
noutcols++;
atttypid = att->atttypid;
atttypmod = att->atttypmod;
for (; j < indesc->natts; j++)
{
att = TupleDescAttr(indesc, j);
if (att->attisdropped)
continue;
nincols++;
/* Found matching column, check type */
if (atttypid != att->atttypid ||
(atttypmod != att->atttypmod && atttypmod >= 0))
ereport(ERROR,
(errcode(ERRCODE_DATATYPE_MISMATCH),
errmsg_internal("%s", _(msg)),
errdetail("Returned type %s does not match expected type %s in column %d.",
format_type_with_typemod(att->atttypid,
att->atttypmod),
format_type_with_typemod(atttypid,
atttypmod),
noutcols)));
attrMap[i] = (AttrNumber) (j + 1);
j++;
break;
}
if (attrMap[i] == 0)
same = false; /* we'll complain below */
}
/* Check for unused input columns */
for (; j < indesc->natts; j++)
{
if (TupleDescAttr(indesc, j)->attisdropped)
continue;
nincols++;
same = false; /* we'll complain below */
}
/* Report column count mismatch using the non-dropped-column counts */
if (!same)
ereport(ERROR,
(errcode(ERRCODE_DATATYPE_MISMATCH),
errmsg_internal("%s", _(msg)),
errdetail("Number of returned columns (%d) does not match "
"expected column count (%d).",
nincols, noutcols)));
/*
* Check to see if the map is one-to-one, in which case we need not do a
* tuple conversion.
*/
if (indesc->natts == outdesc->natts)
{
for (i = 0; i < n; i++)
{
Form_pg_attribute inatt = TupleDescAttr(indesc, i);
Form_pg_attribute outatt = TupleDescAttr(outdesc, i);
/*
* If the input column has a missing attribute, we need a
* conversion.
*/
if (inatt->atthasmissing)
{
same = false;
break;
}
if (attrMap[i] == (i + 1))
continue;
/*
* If it's a dropped column and the corresponding input column is
* also dropped, we needn't convert. However, attlen and attalign
* must agree.
*/
if (attrMap[i] == 0 &&
inatt->attisdropped &&
inatt->attlen == outatt->attlen &&
inatt->attalign == outatt->attalign)
continue;
same = false;
break;
}
}
else
same = false;
if (same)
{
/* Runtime conversion is not needed */
pfree(attrMap);
return NULL;
}
/* Prepare the map structure */
map = (TupleConversionMap *) palloc(sizeof(TupleConversionMap));
map->indesc = indesc;
map->outdesc = outdesc;
map->attrMap = attrMap;
/* preallocate workspace for Datum arrays */
map->outvalues = (Datum *) palloc(n * sizeof(Datum));
map->outisnull = (bool *) palloc(n * sizeof(bool));
n = indesc->natts + 1; /* +1 for NULL */
map->invalues = (Datum *) palloc(n * sizeof(Datum));
map->inisnull = (bool *) palloc(n * sizeof(bool));
map->invalues[0] = (Datum) 0; /* set up the NULL entry */
map->inisnull[0] = true;
return map;
}
/*
* Set up for tuple conversion, matching input and output columns by name.
* (Dropped columns are ignored in both input and output.) This is intended
* for use when the rowtypes are related by inheritance, so we expect an exact
* match of both type and typmod. The error messages will be a bit unhelpful
* unless both rowtypes are named composite types.
*/
TupleConversionMap *
convert_tuples_by_name(TupleDesc indesc,
TupleDesc outdesc,
const char *msg)
{
TupleConversionMap *map;
AttrNumber *attrMap;
int n = outdesc->natts;
/* Verify compatibility and prepare attribute-number map */
attrMap = convert_tuples_by_name_map_if_req(indesc, outdesc, msg);
if (attrMap == NULL)
{
/* runtime conversion is not needed */
return NULL;
}
/* Prepare the map structure */
map = (TupleConversionMap *) palloc(sizeof(TupleConversionMap));
map->indesc = indesc;
map->outdesc = outdesc;
map->attrMap = attrMap;
/* preallocate workspace for Datum arrays */
map->outvalues = (Datum *) palloc(n * sizeof(Datum));
map->outisnull = (bool *) palloc(n * sizeof(bool));
n = indesc->natts + 1; /* +1 for NULL */
map->invalues = (Datum *) palloc(n * sizeof(Datum));
map->inisnull = (bool *) palloc(n * sizeof(bool));
map->invalues[0] = (Datum) 0; /* set up the NULL entry */
map->inisnull[0] = true;
return map;
}
/*
* Return a palloc'd bare attribute map for tuple conversion, matching input
* and output columns by name. (Dropped columns are ignored in both input and
* output.) This is normally a subroutine for convert_tuples_by_name, but can
* be used standalone.
*/
AttrNumber *
convert_tuples_by_name_map(TupleDesc indesc,
TupleDesc outdesc,
const char *msg)
{
AttrNumber *attrMap;
int outnatts;
int innatts;
int i;
int nextindesc = -1;
outnatts = outdesc->natts;
innatts = indesc->natts;
attrMap = (AttrNumber *) palloc0(outnatts * sizeof(AttrNumber));
for (i = 0; i < outnatts; i++)
{
Form_pg_attribute outatt = TupleDescAttr(outdesc, i);
char *attname;
Oid atttypid;
int32 atttypmod;
int j;
if (outatt->attisdropped)
continue; /* attrMap[i] is already 0 */
attname = NameStr(outatt->attname);
atttypid = outatt->atttypid;
atttypmod = outatt->atttypmod;
/*
* Now search for an attribute with the same name in the indesc. It
* seems likely that a partitioned table will have the attributes in
* the same order as the partition, so the search below is optimized
* for that case. It is possible that columns are dropped in one of
* the relations, but not the other, so we use the 'nextindesc'
* counter to track the starting point of the search. If the inner
* loop encounters dropped columns then it will have to skip over
* them, but it should leave 'nextindesc' at the correct position for
* the next outer loop.
*/
for (j = 0; j < innatts; j++)
{
Form_pg_attribute inatt;
nextindesc++;
if (nextindesc >= innatts)
nextindesc = 0;
inatt = TupleDescAttr(indesc, nextindesc);
if (inatt->attisdropped)
continue;
if (strcmp(attname, NameStr(inatt->attname)) == 0)
{
/* Found it, check type */
if (atttypid != inatt->atttypid || atttypmod != inatt->atttypmod)
ereport(ERROR,
(errcode(ERRCODE_DATATYPE_MISMATCH),
errmsg_internal("%s", _(msg)),
errdetail("Attribute \"%s\" of type %s does not match corresponding attribute of type %s.",
attname,
format_type_be(outdesc->tdtypeid),
format_type_be(indesc->tdtypeid))));
attrMap[i] = inatt->attnum;
break;
}
}
if (attrMap[i] == 0)
ereport(ERROR,
(errcode(ERRCODE_DATATYPE_MISMATCH),
errmsg_internal("%s", _(msg)),
errdetail("Attribute \"%s\" of type %s does not exist in type %s.",
attname,
format_type_be(outdesc->tdtypeid),
format_type_be(indesc->tdtypeid))));
}
return attrMap;
}
/*
* Returns mapping created by convert_tuples_by_name_map, or NULL if no
* conversion not required. This is a convenience routine for
* convert_tuples_by_name() and other functions.
*/
AttrNumber *
convert_tuples_by_name_map_if_req(TupleDesc indesc,
TupleDesc outdesc,
const char *msg)
{
AttrNumber *attrMap;
int n = outdesc->natts;
int i;
bool same;
/* Verify compatibility and prepare attribute-number map */
attrMap = convert_tuples_by_name_map(indesc, outdesc, msg);
/*
* Check to see if the map is one-to-one, in which case we need not do a
* tuple conversion.
*/
if (indesc->natts == outdesc->natts)
{
same = true;
for (i = 0; i < n; i++)
{
Form_pg_attribute inatt = TupleDescAttr(indesc, i);
Form_pg_attribute outatt = TupleDescAttr(outdesc, i);
/*
* If the input column has a missing attribute, we need a
* conversion.
*/
if (inatt->atthasmissing)
{
same = false;
break;
}
if (attrMap[i] == (i + 1))
continue;
/*
* If it's a dropped column and the corresponding input column is
* also dropped, we needn't convert. However, attlen and attalign
* must agree.
*/
if (attrMap[i] == 0 &&
inatt->attisdropped &&
inatt->attlen == outatt->attlen &&
inatt->attalign == outatt->attalign)
continue;
same = false;
break;
}
}
else
same = false;
if (same)
{
/* Runtime conversion is not needed */
pfree(attrMap);
return NULL;
}
else
return attrMap;
}
/*
* Perform conversion of a tuple according to the map.
*/
HeapTuple
execute_attr_map_tuple(HeapTuple tuple, TupleConversionMap *map)
{
AttrNumber *attrMap = map->attrMap;
Datum *invalues = map->invalues;
bool *inisnull = map->inisnull;
Datum *outvalues = map->outvalues;
bool *outisnull = map->outisnull;
int outnatts = map->outdesc->natts;
int i;
/*
* Extract all the values of the old tuple, offsetting the arrays so that
* invalues[0] is left NULL and invalues[1] is the first source attribute;
* this exactly matches the numbering convention in attrMap.
*/
heap_deform_tuple(tuple, map->indesc, invalues + 1, inisnull + 1);
/*
* Transpose into proper fields of the new tuple.
*/
for (i = 0; i < outnatts; i++)
{
int j = attrMap[i];
outvalues[i] = invalues[j];
outisnull[i] = inisnull[j];
}
/*
* Now form the new tuple.
*/
return heap_form_tuple(map->outdesc, outvalues, outisnull);
}
/*
* Perform conversion of a tuple slot according to the map.
*/
TupleTableSlot *
execute_attr_map_slot(AttrNumber *attrMap,
TupleTableSlot *in_slot,
TupleTableSlot *out_slot)
{
Datum *invalues;
bool *inisnull;
Datum *outvalues;
bool *outisnull;
int outnatts;
int i;
/* Sanity checks */
Assert(in_slot->tts_tupleDescriptor != NULL &&
out_slot->tts_tupleDescriptor != NULL);
Assert(in_slot->tts_values != NULL && out_slot->tts_values != NULL);
outnatts = out_slot->tts_tupleDescriptor->natts;
/* Extract all the values of the in slot. */
slot_getallattrs(in_slot);
/* Before doing the mapping, clear any old contents from the out slot */
ExecClearTuple(out_slot);
invalues = in_slot->tts_values;
inisnull = in_slot->tts_isnull;
outvalues = out_slot->tts_values;
outisnull = out_slot->tts_isnull;
/* Transpose into proper fields of the out slot. */
for (i = 0; i < outnatts; i++)
{
int j = attrMap[i] - 1;
/* attrMap[i] == 0 means it's a NULL datum. */
if (j == -1)
{
outvalues[i] = (Datum) 0;
outisnull[i] = true;
}
else
{
outvalues[i] = invalues[j];
outisnull[i] = inisnull[j];
}
}
ExecStoreVirtualTuple(out_slot);
return out_slot;
}
/*
* Perform conversion of bitmap of columns according to the map.
*
* The input and output bitmaps are offset by
* FirstLowInvalidHeapAttributeNumber to accommodate system cols, like the
* column-bitmaps in RangeTblEntry.
*/
Bitmapset *
execute_attr_map_cols(Bitmapset *in_cols, TupleConversionMap *map)
{
AttrNumber *attrMap = map->attrMap;
int maplen = map->outdesc->natts;
Bitmapset *out_cols;
int out_attnum;
/* fast path for the common trivial case */
if (in_cols == NULL)
return NULL;
/*
* For each output column, check which input column it corresponds to.
*/
out_cols = NULL;
for (out_attnum = FirstLowInvalidHeapAttributeNumber + 1;
out_attnum <= maplen;
out_attnum++)
{
int in_attnum;
if (out_attnum < 0)
{
/* System column. No mapping. */
in_attnum = out_attnum;
}
else if (out_attnum == 0)
continue;
else
{
/* normal user column */
in_attnum = attrMap[out_attnum - 1];
if (in_attnum == 0)
continue;
}
if (bms_is_member(in_attnum - FirstLowInvalidHeapAttributeNumber, in_cols))
out_cols = bms_add_member(out_cols, out_attnum - FirstLowInvalidHeapAttributeNumber);
}
return out_cols;
}
/*
* Free a TupleConversionMap structure.
*/
void
free_conversion_map(TupleConversionMap *map)
{
/* indesc and outdesc are not ours to free */
pfree(map->attrMap);
pfree(map->invalues);
pfree(map->inisnull);
pfree(map->outvalues);
pfree(map->outisnull);
pfree(map);
}
|