sepgsql: update TAP test to use fat comma style
[pgsql.git] / src / backend / access / common / attmap.c
blob4b6cfe05c0230873fccdb1a8a188f621fdd30daf
1 /*-------------------------------------------------------------------------
3 * attmap.c
4 * Attribute mapping support.
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.
13 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
14 * Portions Copyright (c) 1994, Regents of the University of California
17 * IDENTIFICATION
18 * src/backend/access/common/attmap.c
20 *-------------------------------------------------------------------------
23 #include "postgres.h"
25 #include "access/attmap.h"
26 #include "utils/builtins.h"
29 static bool check_attrmap_match(TupleDesc indesc,
30 TupleDesc outdesc,
31 AttrMap *attrMap);
34 * make_attrmap
36 * Utility routine to allocate an attribute map in the current memory
37 * context.
39 AttrMap *
40 make_attrmap(int maplen)
42 AttrMap *res;
44 res = (AttrMap *) palloc0(sizeof(AttrMap));
45 res->maplen = maplen;
46 res->attnums = (AttrNumber *) palloc0(sizeof(AttrNumber) * maplen);
47 return res;
51 * free_attrmap
53 * Utility routine to release an attribute map.
55 void
56 free_attrmap(AttrMap *map)
58 pfree(map->attnums);
59 pfree(map);
63 * build_attrmap_by_position
65 * Return a palloc'd bare attribute map for tuple conversion, matching input
66 * and output columns by position. Dropped columns are ignored in both input
67 * and output, marked as 0. This is normally a subroutine for
68 * convert_tuples_by_position in tupconvert.c, but it can be used standalone.
70 * Note: the errdetail messages speak of indesc as the "returned" rowtype,
71 * outdesc as the "expected" rowtype. This is okay for current uses but
72 * might need generalization in future.
74 AttrMap *
75 build_attrmap_by_position(TupleDesc indesc,
76 TupleDesc outdesc,
77 const char *msg)
79 AttrMap *attrMap;
80 int nincols;
81 int noutcols;
82 int n;
83 int i;
84 int j;
85 bool same;
88 * The length is computed as the number of attributes of the expected
89 * rowtype as it includes dropped attributes in its count.
91 n = outdesc->natts;
92 attrMap = make_attrmap(n);
94 j = 0; /* j is next physical input attribute */
95 nincols = noutcols = 0; /* these count non-dropped attributes */
96 same = true;
97 for (i = 0; i < n; i++)
99 Form_pg_attribute att = TupleDescAttr(outdesc, i);
100 Oid atttypid;
101 int32 atttypmod;
103 if (att->attisdropped)
104 continue; /* attrMap->attnums[i] is already 0 */
105 noutcols++;
106 atttypid = att->atttypid;
107 atttypmod = att->atttypmod;
108 for (; j < indesc->natts; j++)
110 att = TupleDescAttr(indesc, j);
111 if (att->attisdropped)
112 continue;
113 nincols++;
115 /* Found matching column, now check type */
116 if (atttypid != att->atttypid ||
117 (atttypmod != att->atttypmod && atttypmod >= 0))
118 ereport(ERROR,
119 (errcode(ERRCODE_DATATYPE_MISMATCH),
120 errmsg_internal("%s", _(msg)),
121 errdetail("Returned type %s does not match expected type %s in column %d.",
122 format_type_with_typemod(att->atttypid,
123 att->atttypmod),
124 format_type_with_typemod(atttypid,
125 atttypmod),
126 noutcols)));
127 attrMap->attnums[i] = (AttrNumber) (j + 1);
128 j++;
129 break;
131 if (attrMap->attnums[i] == 0)
132 same = false; /* we'll complain below */
135 /* Check for unused input columns */
136 for (; j < indesc->natts; j++)
138 if (TupleDescCompactAttr(indesc, j)->attisdropped)
139 continue;
140 nincols++;
141 same = false; /* we'll complain below */
144 /* Report column count mismatch using the non-dropped-column counts */
145 if (!same)
146 ereport(ERROR,
147 (errcode(ERRCODE_DATATYPE_MISMATCH),
148 errmsg_internal("%s", _(msg)),
149 errdetail("Number of returned columns (%d) does not match "
150 "expected column count (%d).",
151 nincols, noutcols)));
153 /* Check if the map has a one-to-one match */
154 if (check_attrmap_match(indesc, outdesc, attrMap))
156 /* Runtime conversion is not needed */
157 free_attrmap(attrMap);
158 return NULL;
161 return attrMap;
165 * build_attrmap_by_name
167 * Return a palloc'd bare attribute map for tuple conversion, matching input
168 * and output columns by name. (Dropped columns are ignored in both input and
169 * output.) This is normally a subroutine for convert_tuples_by_name in
170 * tupconvert.c, but can be used standalone.
172 * If 'missing_ok' is true, a column from 'outdesc' not being present in
173 * 'indesc' is not flagged as an error; AttrMap.attnums[] entry for such an
174 * outdesc column will be 0 in that case.
176 AttrMap *
177 build_attrmap_by_name(TupleDesc indesc,
178 TupleDesc outdesc,
179 bool missing_ok)
181 AttrMap *attrMap;
182 int outnatts;
183 int innatts;
184 int i;
185 int nextindesc = -1;
187 outnatts = outdesc->natts;
188 innatts = indesc->natts;
190 attrMap = make_attrmap(outnatts);
191 for (i = 0; i < outnatts; i++)
193 Form_pg_attribute outatt = TupleDescAttr(outdesc, i);
194 char *attname;
195 Oid atttypid;
196 int32 atttypmod;
197 int j;
199 if (outatt->attisdropped)
200 continue; /* attrMap->attnums[i] is already 0 */
201 attname = NameStr(outatt->attname);
202 atttypid = outatt->atttypid;
203 atttypmod = outatt->atttypmod;
206 * Now search for an attribute with the same name in the indesc. It
207 * seems likely that a partitioned table will have the attributes in
208 * the same order as the partition, so the search below is optimized
209 * for that case. It is possible that columns are dropped in one of
210 * the relations, but not the other, so we use the 'nextindesc'
211 * counter to track the starting point of the search. If the inner
212 * loop encounters dropped columns then it will have to skip over
213 * them, but it should leave 'nextindesc' at the correct position for
214 * the next outer loop.
216 for (j = 0; j < innatts; j++)
218 Form_pg_attribute inatt;
220 nextindesc++;
221 if (nextindesc >= innatts)
222 nextindesc = 0;
224 inatt = TupleDescAttr(indesc, nextindesc);
225 if (inatt->attisdropped)
226 continue;
227 if (strcmp(attname, NameStr(inatt->attname)) == 0)
229 /* Found it, check type */
230 if (atttypid != inatt->atttypid || atttypmod != inatt->atttypmod)
231 ereport(ERROR,
232 (errcode(ERRCODE_DATATYPE_MISMATCH),
233 errmsg("could not convert row type"),
234 errdetail("Attribute \"%s\" of type %s does not match corresponding attribute of type %s.",
235 attname,
236 format_type_be(outdesc->tdtypeid),
237 format_type_be(indesc->tdtypeid))));
238 attrMap->attnums[i] = inatt->attnum;
239 break;
242 if (attrMap->attnums[i] == 0 && !missing_ok)
243 ereport(ERROR,
244 (errcode(ERRCODE_DATATYPE_MISMATCH),
245 errmsg("could not convert row type"),
246 errdetail("Attribute \"%s\" of type %s does not exist in type %s.",
247 attname,
248 format_type_be(outdesc->tdtypeid),
249 format_type_be(indesc->tdtypeid))));
251 return attrMap;
255 * build_attrmap_by_name_if_req
257 * Returns mapping created by build_attrmap_by_name, or NULL if no
258 * conversion is required. This is a convenience routine for
259 * convert_tuples_by_name() in tupconvert.c and other functions, but it
260 * can be used standalone.
262 AttrMap *
263 build_attrmap_by_name_if_req(TupleDesc indesc,
264 TupleDesc outdesc,
265 bool missing_ok)
267 AttrMap *attrMap;
269 /* Verify compatibility and prepare attribute-number map */
270 attrMap = build_attrmap_by_name(indesc, outdesc, missing_ok);
272 /* Check if the map has a one-to-one match */
273 if (check_attrmap_match(indesc, outdesc, attrMap))
275 /* Runtime conversion is not needed */
276 free_attrmap(attrMap);
277 return NULL;
280 return attrMap;
284 * check_attrmap_match
286 * Check to see if the map is a one-to-one match, in which case we need
287 * not to do a tuple conversion, and the attribute map is not necessary.
289 static bool
290 check_attrmap_match(TupleDesc indesc,
291 TupleDesc outdesc,
292 AttrMap *attrMap)
294 int i;
296 /* no match if attribute numbers are not the same */
297 if (indesc->natts != outdesc->natts)
298 return false;
300 for (i = 0; i < attrMap->maplen; i++)
302 CompactAttribute *inatt = TupleDescCompactAttr(indesc, i);
303 CompactAttribute *outatt;
306 * If the input column has a missing attribute, we need a conversion.
308 if (inatt->atthasmissing)
309 return false;
311 if (attrMap->attnums[i] == (i + 1))
312 continue;
314 outatt = TupleDescCompactAttr(outdesc, i);
317 * If it's a dropped column and the corresponding input column is also
318 * dropped, we don't need a conversion. However, attlen and
319 * attalignby must agree.
321 if (attrMap->attnums[i] == 0 &&
322 inatt->attisdropped &&
323 inatt->attlen == outatt->attlen &&
324 inatt->attalignby == outatt->attalignby)
325 continue;
327 return false;
330 return true;