LCOV - differential code coverage report
Current view: top level - src/backend/access/common - attmap.c (source / functions) Coverage Total Hit LBC UBC GIC GNC CBC EUB ECB
Current: Differential Code Coverage HEAD vs 15 Lines: 96.9 % 96 93 2 1 33 2 58 2 33
Current Date: 2023-04-08 17:13:01 Functions: 100.0 % 6 6 3 3 3
Baseline: 15 Line coverage date bins:
Baseline Date: 2023-04-08 15:09:40 (120,180] days: 100.0 % 2 2 2
Legend: Lines: hit not hit (240..) days: 96.8 % 94 91 2 1 33 58 2 31
Function coverage date bins:
(240..) days: 66.7 % 9 6 3 3 3

 Age         Owner                  TLA  Line data    Source code
                                  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-2023, 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                 : static bool check_attrmap_match(TupleDesc indesc,
                                 31                 :                                 TupleDesc outdesc,
                                 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 *
 1208 michael                    41 CBC       26069 : make_attrmap(int maplen)
                                 42                 : {
                                 43                 :     AttrMap    *res;
                                 44                 : 
                                 45           26069 :     res = (AttrMap *) palloc0(sizeof(AttrMap));
                                 46           26069 :     res->maplen = maplen;
                                 47           26069 :     res->attnums = (AttrNumber *) palloc0(sizeof(AttrNumber) * maplen);
                                 48           26069 :     return res;
                                 49                 : }
                                 50                 : 
                                 51                 : /*
                                 52                 :  * free_attrmap
                                 53                 :  *
                                 54                 :  * Utility routine to release an attribute map.
                                 55                 :  */
                                 56                 : void
                                 57           15312 : free_attrmap(AttrMap *map)
                                 58                 : {
                                 59           15312 :     pfree(map->attnums);
                                 60           15312 :     pfree(map);
                                 61           15312 : }
                                 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            4750 : build_attrmap_by_position(TupleDesc indesc,
                                 77                 :                           TupleDesc outdesc,
                                 78                 :                           const char *msg)
                                 79                 : {
                                 80                 :     AttrMap    *attrMap;
                                 81                 :     int         nincols;
                                 82                 :     int         noutcols;
                                 83                 :     int         n;
                                 84                 :     int         i;
                                 85                 :     int         j;
                                 86                 :     bool        same;
                                 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            4750 :     n = outdesc->natts;
                                 93            4750 :     attrMap = make_attrmap(n);
                                 94                 : 
                                 95            4750 :     j = 0;                      /* j is next physical input attribute */
                                 96            4750 :     nincols = noutcols = 0;     /* these count non-dropped attributes */
                                 97            4750 :     same = true;
                                 98           17209 :     for (i = 0; i < n; i++)
                                 99                 :     {
                                100           12468 :         Form_pg_attribute att = TupleDescAttr(outdesc, i);
                                101                 :         Oid         atttypid;
                                102                 :         int32       atttypmod;
                                103                 : 
                                104           12468 :         if (att->attisdropped)
                                105              74 :             continue;           /* attrMap->attnums[i] is already 0 */
                                106           12394 :         noutcols++;
                                107           12394 :         atttypid = att->atttypid;
                                108           12394 :         atttypmod = att->atttypmod;
                                109           12403 :         for (; j < indesc->natts; j++)
                                110                 :         {
                                111           12399 :             att = TupleDescAttr(indesc, j);
                                112           12399 :             if (att->attisdropped)
                                113               9 :                 continue;
                                114           12390 :             nincols++;
                                115                 : 
                                116                 :             /* Found matching column, now check type */
                                117           12390 :             if (atttypid != att->atttypid ||
                                118           12381 :                 (atttypmod != att->atttypmod && atttypmod >= 0))
                                119               9 :                 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           12381 :             attrMap->attnums[i] = (AttrNumber) (j + 1);
                                129           12381 :             j++;
                                130           12381 :             break;
                                131                 :         }
                                132           12385 :         if (attrMap->attnums[i] == 0)
                                133               4 :             same = false;       /* we'll complain below */
                                134                 :     }
                                135                 : 
                                136                 :     /* Check for unused input columns */
                                137            4744 :     for (; j < indesc->natts; j++)
                                138                 :     {
                                139               3 :         if (TupleDescAttr(indesc, j)->attisdropped)
 1208 michael                   140 UBC           0 :             continue;
 1208 michael                   141 CBC           3 :         nincols++;
                                142               3 :         same = false;           /* we'll complain below */
                                143                 :     }
                                144                 : 
                                145                 :     /* Report column count mismatch using the non-dropped-column counts */
                                146            4741 :     if (!same)
                                147               7 :         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            4734 :     if (check_attrmap_match(indesc, outdesc, attrMap))
                                156                 :     {
                                157                 :         /* Runtime conversion is not needed */
                                158            4687 :         free_attrmap(attrMap);
                                159            4687 :         return NULL;
                                160                 :     }
                                161                 : 
                                162              47 :     return attrMap;
                                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                 :  * If 'missing_ok' is true, a column from 'outdesc' not being present in
                                174                 :  * 'indesc' is not flagged as an error; AttrMap.attnums[] entry for such an
                                175                 :  * outdesc column will be 0 in that case.
                                176                 :  */
                                177                 : AttrMap *
 1208 michael                   178 GIC       16410 : build_attrmap_by_name(TupleDesc indesc,
                                179                 :                       TupleDesc outdesc,
                                180                 :                       bool missing_ok)
                                181                 : {
                                182                 :     AttrMap    *attrMap;
 1208 michael                   183 ECB             :     int         outnatts;
                                184                 :     int         innatts;
                                185                 :     int         i;
 1208 michael                   186 GIC       16410 :     int         nextindesc = -1;
                                187                 : 
                                188           16410 :     outnatts = outdesc->natts;
                                189           16410 :     innatts = indesc->natts;
                                190                 : 
 1208 michael                   191 CBC       16410 :     attrMap = make_attrmap(outnatts);
 1208 michael                   192 GIC       55401 :     for (i = 0; i < outnatts; i++)
 1208 michael                   193 ECB             :     {
 1208 michael                   194 CBC       38991 :         Form_pg_attribute outatt = TupleDescAttr(outdesc, i);
                                195                 :         char       *attname;
 1208 michael                   196 ECB             :         Oid         atttypid;
                                197                 :         int32       atttypmod;
                                198                 :         int         j;
                                199                 : 
 1208 michael                   200 GIC       38991 :         if (outatt->attisdropped)
                                201            1779 :             continue;           /* attrMap->attnums[i] is already 0 */
                                202           37212 :         attname = NameStr(outatt->attname);
                                203           37212 :         atttypid = outatt->atttypid;
                                204           37212 :         atttypmod = outatt->atttypmod;
 1208 michael                   205 ECB             : 
                                206                 :         /*
                                207                 :          * Now search for an attribute with the same name in the indesc. It
                                208                 :          * seems likely that a partitioned table will have the attributes in
                                209                 :          * the same order as the partition, so the search below is optimized
                                210                 :          * for that case.  It is possible that columns are dropped in one of
                                211                 :          * the relations, but not the other, so we use the 'nextindesc'
                                212                 :          * counter to track the starting point of the search.  If the inner
                                213                 :          * loop encounters dropped columns then it will have to skip over
                                214                 :          * them, but it should leave 'nextindesc' at the correct position for
                                215                 :          * the next outer loop.
                                216                 :          */
 1208 michael                   217 GIC       46109 :         for (j = 0; j < innatts; j++)
                                218                 :         {
                                219                 :             Form_pg_attribute inatt;
                                220                 : 
                                221           46090 :             nextindesc++;
 1208 michael                   222 CBC       46090 :             if (nextindesc >= innatts)
 1208 michael                   223 GIC        2746 :                 nextindesc = 0;
                                224                 : 
                                225           46090 :             inatt = TupleDescAttr(indesc, nextindesc);
 1208 michael                   226 CBC       46090 :             if (inatt->attisdropped)
                                227            1584 :                 continue;
                                228           44506 :             if (strcmp(attname, NameStr(inatt->attname)) == 0)
                                229                 :             {
 1208 michael                   230 ECB             :                 /* Found it, check type */
 1208 michael                   231 CBC       37193 :                 if (atttypid != inatt->atttypid || atttypmod != inatt->atttypmod)
 1208 michael                   232 LBC           0 :                     ereport(ERROR,
 1208 michael                   233 ECB             :                             (errcode(ERRCODE_DATATYPE_MISMATCH),
                                234                 :                              errmsg("could not convert row type"),
                                235                 :                              errdetail("Attribute \"%s\" of type %s does not match corresponding attribute of type %s.",
                                236                 :                                        attname,
 1208 michael                   237 EUB             :                                        format_type_be(outdesc->tdtypeid),
                                238                 :                                        format_type_be(indesc->tdtypeid))));
 1208 michael                   239 GIC       37193 :                 attrMap->attnums[i] = inatt->attnum;
                                240           37193 :                 break;
                                241                 :             }
                                242                 :         }
  131 alvherre                  243 GNC       37212 :         if (attrMap->attnums[i] == 0 && !missing_ok)
 1208 michael                   244 LBC           0 :             ereport(ERROR,
 1208 michael                   245 ECB             :                     (errcode(ERRCODE_DATATYPE_MISMATCH),
                                246                 :                      errmsg("could not convert row type"),
                                247                 :                      errdetail("Attribute \"%s\" of type %s does not exist in type %s.",
                                248                 :                                attname,
 1208 michael                   249 EUB             :                                format_type_be(outdesc->tdtypeid),
                                250                 :                                format_type_be(indesc->tdtypeid))));
                                251                 :     }
 1208 michael                   252 GIC       16410 :     return attrMap;
                                253                 : }
                                254                 : 
                                255                 : /*
                                256                 :  * build_attrmap_by_name_if_req
 1208 michael                   257 ECB             :  *
                                258                 :  * Returns mapping created by build_attrmap_by_name, or NULL if no
                                259                 :  * conversion is required.  This is a convenience routine for
                                260                 :  * convert_tuples_by_name() in tupconvert.c and other functions, but it
                                261                 :  * can be used standalone.
                                262                 :  */
                                263                 : AttrMap *
 1208 michael                   264 GIC        6892 : build_attrmap_by_name_if_req(TupleDesc indesc,
                                265                 :                              TupleDesc outdesc,
                                266                 :                              bool missing_ok)
                                267                 : {
                                268                 :     AttrMap    *attrMap;
                                269                 : 
 1208 michael                   270 ECB             :     /* Verify compatibility and prepare attribute-number map */
  131 alvherre                  271 GNC        6892 :     attrMap = build_attrmap_by_name(indesc, outdesc, missing_ok);
                                272                 : 
                                273                 :     /* Check if the map has a one-to-one match */
 1208 michael                   274 GIC        6892 :     if (check_attrmap_match(indesc, outdesc, attrMap))
                                275                 :     {
                                276                 :         /* Runtime conversion is not needed */
 1208 michael                   277 CBC        5447 :         free_attrmap(attrMap);
 1208 michael                   278 GIC        5447 :         return NULL;
                                279                 :     }
 1208 michael                   280 ECB             : 
 1208 michael                   281 GIC        1445 :     return attrMap;
                                282                 : }
 1208 michael                   283 ECB             : 
                                284                 : /*
                                285                 :  * check_attrmap_match
                                286                 :  *
                                287                 :  * Check to see if the map is a one-to-one match, in which case we need
                                288                 :  * not to do a tuple conversion, and the attribute map is not necessary.
                                289                 :  */
                                290                 : static bool
 1208 michael                   291 GIC       11626 : check_attrmap_match(TupleDesc indesc,
                                292                 :                     TupleDesc outdesc,
                                293                 :                     AttrMap *attrMap)
                                294                 : {
                                295                 :     int         i;
                                296                 : 
 1208 michael                   297 ECB             :     /* no match if attribute numbers are not the same */
 1208 michael                   298 GIC       11626 :     if (indesc->natts != outdesc->natts)
                                299             651 :         return false;
                                300                 : 
                                301           35856 :     for (i = 0; i < attrMap->maplen; i++)
                                302                 :     {
 1159 rhodiumtoad               303           25722 :         Form_pg_attribute inatt = TupleDescAttr(indesc, i);
 1159 rhodiumtoad               304 CBC       25722 :         Form_pg_attribute outatt = TupleDescAttr(outdesc, i);
 1159 rhodiumtoad               305 ECB             : 
                                306                 :         /*
                                307                 :          * If the input column has a missing attribute, we need a conversion.
                                308                 :          */
 1159 rhodiumtoad               309 CBC       25722 :         if (inatt->atthasmissing)
                                310              24 :             return false;
                                311                 : 
 1208 michael                   312 GIC       25698 :         if (attrMap->attnums[i] == (i + 1))
                                313           24851 :             continue;
                                314                 : 
 1208 michael                   315 ECB             :         /*
                                316                 :          * If it's a dropped column and the corresponding input column is also
                                317                 :          * dropped, we don't need a conversion.  However, attlen and attalign
                                318                 :          * must agree.
                                319                 :          */
 1208 michael                   320 GIC         847 :         if (attrMap->attnums[i] == 0 &&
                                321              46 :             inatt->attisdropped &&
                                322              30 :             inatt->attlen == outatt->attlen &&
                                323              30 :             inatt->attalign == outatt->attalign)
                                324              30 :             continue;
                                325                 : 
 1208 michael                   326 CBC         817 :         return false;
 1208 michael                   327 ECB             :     }
                                328                 : 
 1208 michael                   329 CBC       10134 :     return true;
 1208 michael                   330 ECB             : }
        

Generated by: LCOV version v1.16-55-g56c0a2a