LCOV - differential code coverage report
Current view: top level - src/backend/access/common - attmap.c (source / functions) Coverage Total Hit UBC CBC
Current: Differential Code Coverage 16@8cea358b128 vs 17@8cea358b128 Lines: 96.9 % 96 93 3 93
Current Date: 2024-04-14 14:21:10 Functions: 100.0 % 6 6 6
Baseline: 16@8cea358b128 Branches: 78.6 % 70 55 15 55
Baseline Date: 2024-04-14 14:21:09 Line coverage date bins:
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed (240..) days: 96.9 % 96 93 3 93
Function coverage date bins:
(240..) days: 100.0 % 6 6 6
Branch coverage date bins:
(240..) days: 78.6 % 70 55 15 55

 Age         Owner                    Branch data    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-2024, 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 "utils/builtins.h"
                                 27                 :                : 
                                 28                 :                : 
                                 29                 :                : static bool check_attrmap_match(TupleDesc indesc,
                                 30                 :                :                                 TupleDesc outdesc,
                                 31                 :                :                                 AttrMap *attrMap);
                                 32                 :                : 
                                 33                 :                : /*
                                 34                 :                :  * make_attrmap
                                 35                 :                :  *
                                 36                 :                :  * Utility routine to allocate an attribute map in the current memory
                                 37                 :                :  * context.
                                 38                 :                :  */
                                 39                 :                : AttrMap *
 1579 michael@paquier.xyz        40                 :CBC       28712 : make_attrmap(int maplen)
                                 41                 :                : {
                                 42                 :                :     AttrMap    *res;
                                 43                 :                : 
                                 44                 :          28712 :     res = (AttrMap *) palloc0(sizeof(AttrMap));
                                 45                 :          28712 :     res->maplen = maplen;
                                 46                 :          28712 :     res->attnums = (AttrNumber *) palloc0(sizeof(AttrNumber) * maplen);
                                 47                 :          28712 :     return res;
                                 48                 :                : }
                                 49                 :                : 
                                 50                 :                : /*
                                 51                 :                :  * free_attrmap
                                 52                 :                :  *
                                 53                 :                :  * Utility routine to release an attribute map.
                                 54                 :                :  */
                                 55                 :                : void
                                 56                 :          16346 : free_attrmap(AttrMap *map)
                                 57                 :                : {
                                 58                 :          16346 :     pfree(map->attnums);
                                 59                 :          16346 :     pfree(map);
                                 60                 :          16346 : }
                                 61                 :                : 
                                 62                 :                : /*
                                 63                 :                :  * build_attrmap_by_position
                                 64                 :                :  *
                                 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.
                                 69                 :                :  *
                                 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.
                                 73                 :                :  */
                                 74                 :                : AttrMap *
                                 75                 :           4761 : build_attrmap_by_position(TupleDesc indesc,
                                 76                 :                :                           TupleDesc outdesc,
                                 77                 :                :                           const char *msg)
                                 78                 :                : {
                                 79                 :                :     AttrMap    *attrMap;
                                 80                 :                :     int         nincols;
                                 81                 :                :     int         noutcols;
                                 82                 :                :     int         n;
                                 83                 :                :     int         i;
                                 84                 :                :     int         j;
                                 85                 :                :     bool        same;
                                 86                 :                : 
                                 87                 :                :     /*
                                 88                 :                :      * The length is computed as the number of attributes of the expected
                                 89                 :                :      * rowtype as it includes dropped attributes in its count.
                                 90                 :                :      */
                                 91                 :           4761 :     n = outdesc->natts;
                                 92                 :           4761 :     attrMap = make_attrmap(n);
                                 93                 :                : 
                                 94                 :           4761 :     j = 0;                      /* j is next physical input attribute */
                                 95                 :           4761 :     nincols = noutcols = 0;     /* these count non-dropped attributes */
                                 96                 :           4761 :     same = true;
                                 97         [ +  + ]:          17251 :     for (i = 0; i < n; i++)
                                 98                 :                :     {
                                 99                 :          12499 :         Form_pg_attribute att = TupleDescAttr(outdesc, i);
                                100                 :                :         Oid         atttypid;
                                101                 :                :         int32       atttypmod;
                                102                 :                : 
                                103         [ +  + ]:          12499 :         if (att->attisdropped)
                                104                 :             74 :             continue;           /* attrMap->attnums[i] is already 0 */
                                105                 :          12425 :         noutcols++;
                                106                 :          12425 :         atttypid = att->atttypid;
                                107                 :          12425 :         atttypmod = att->atttypmod;
                                108         [ +  + ]:          12434 :         for (; j < indesc->natts; j++)
                                109                 :                :         {
                                110                 :          12430 :             att = TupleDescAttr(indesc, j);
                                111         [ +  + ]:          12430 :             if (att->attisdropped)
                                112                 :              9 :                 continue;
                                113                 :          12421 :             nincols++;
                                114                 :                : 
                                115                 :                :             /* Found matching column, now check type */
                                116         [ +  + ]:          12421 :             if (atttypid != att->atttypid ||
                                117   [ -  +  -  - ]:          12412 :                 (atttypmod != att->atttypmod && atttypmod >= 0))
                                118         [ +  - ]:              9 :                 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                 :          12412 :             attrMap->attnums[i] = (AttrNumber) (j + 1);
                                128                 :          12412 :             j++;
                                129                 :          12412 :             break;
                                130                 :                :         }
                                131         [ +  + ]:          12416 :         if (attrMap->attnums[i] == 0)
                                132                 :              4 :             same = false;       /* we'll complain below */
                                133                 :                :     }
                                134                 :                : 
                                135                 :                :     /* Check for unused input columns */
                                136         [ +  + ]:           4755 :     for (; j < indesc->natts; j++)
                                137                 :                :     {
                                138         [ -  + ]:              3 :         if (TupleDescAttr(indesc, j)->attisdropped)
 1579 michael@paquier.xyz       139                 :UBC           0 :             continue;
 1579 michael@paquier.xyz       140                 :CBC           3 :         nincols++;
                                141                 :              3 :         same = false;           /* we'll complain below */
                                142                 :                :     }
                                143                 :                : 
                                144                 :                :     /* Report column count mismatch using the non-dropped-column counts */
                                145         [ +  + ]:           4752 :     if (!same)
                                146         [ +  - ]:              7 :         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)));
                                152                 :                : 
                                153                 :                :     /* Check if the map has a one-to-one match */
                                154         [ +  + ]:           4745 :     if (check_attrmap_match(indesc, outdesc, attrMap))
                                155                 :                :     {
                                156                 :                :         /* Runtime conversion is not needed */
                                157                 :           4698 :         free_attrmap(attrMap);
                                158                 :           4698 :         return NULL;
                                159                 :                :     }
                                160                 :                : 
                                161                 :             47 :     return attrMap;
                                162                 :                : }
                                163                 :                : 
                                164                 :                : /*
                                165                 :                :  * build_attrmap_by_name
                                166                 :                :  *
                                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.
                                171                 :                :  *
                                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.
                                175                 :                :  */
                                176                 :                : AttrMap *
                                177                 :          18309 : build_attrmap_by_name(TupleDesc indesc,
                                178                 :                :                       TupleDesc outdesc,
                                179                 :                :                       bool missing_ok)
                                180                 :                : {
                                181                 :                :     AttrMap    *attrMap;
                                182                 :                :     int         outnatts;
                                183                 :                :     int         innatts;
                                184                 :                :     int         i;
                                185                 :          18309 :     int         nextindesc = -1;
                                186                 :                : 
                                187                 :          18309 :     outnatts = outdesc->natts;
                                188                 :          18309 :     innatts = indesc->natts;
                                189                 :                : 
                                190                 :          18309 :     attrMap = make_attrmap(outnatts);
                                191         [ +  + ]:          65228 :     for (i = 0; i < outnatts; i++)
                                192                 :                :     {
                                193                 :          46919 :         Form_pg_attribute outatt = TupleDescAttr(outdesc, i);
                                194                 :                :         char       *attname;
                                195                 :                :         Oid         atttypid;
                                196                 :                :         int32       atttypmod;
                                197                 :                :         int         j;
                                198                 :                : 
                                199         [ +  + ]:          46919 :         if (outatt->attisdropped)
                                200                 :           1815 :             continue;           /* attrMap->attnums[i] is already 0 */
                                201                 :          45104 :         attname = NameStr(outatt->attname);
                                202                 :          45104 :         atttypid = outatt->atttypid;
                                203                 :          45104 :         atttypmod = outatt->atttypmod;
                                204                 :                : 
                                205                 :                :         /*
                                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.
                                215                 :                :          */
                                216         [ +  + ]:          54193 :         for (j = 0; j < innatts; j++)
                                217                 :                :         {
                                218                 :                :             Form_pg_attribute inatt;
                                219                 :                : 
                                220                 :          54171 :             nextindesc++;
                                221         [ +  + ]:          54171 :             if (nextindesc >= innatts)
                                222                 :           2794 :                 nextindesc = 0;
                                223                 :                : 
                                224                 :          54171 :             inatt = TupleDescAttr(indesc, nextindesc);
                                225         [ +  + ]:          54171 :             if (inatt->attisdropped)
                                226                 :           1707 :                 continue;
                                227         [ +  + ]:          52464 :             if (strcmp(attname, NameStr(inatt->attname)) == 0)
                                228                 :                :             {
                                229                 :                :                 /* Found it, check type */
                                230   [ +  -  -  + ]:          45082 :                 if (atttypid != inatt->atttypid || atttypmod != inatt->atttypmod)
 1579 michael@paquier.xyz       231         [ #  # ]:UBC           0 :                     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))));
 1579 michael@paquier.xyz       238                 :CBC       45082 :                 attrMap->attnums[i] = inatt->attnum;
                                239                 :          45082 :                 break;
                                240                 :                :             }
                                241                 :                :         }
  502 alvherre@alvh.no-ip.      242   [ +  +  -  + ]:          45104 :         if (attrMap->attnums[i] == 0 && !missing_ok)
 1579 michael@paquier.xyz       243         [ #  # ]:UBC           0 :             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))));
                                250                 :                :     }
 1579 michael@paquier.xyz       251                 :CBC       18309 :     return attrMap;
                                252                 :                : }
                                253                 :                : 
                                254                 :                : /*
                                255                 :                :  * build_attrmap_by_name_if_req
                                256                 :                :  *
                                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.
                                261                 :                :  */
                                262                 :                : AttrMap *
                                263                 :           7160 : build_attrmap_by_name_if_req(TupleDesc indesc,
                                264                 :                :                              TupleDesc outdesc,
                                265                 :                :                              bool missing_ok)
                                266                 :                : {
                                267                 :                :     AttrMap    *attrMap;
                                268                 :                : 
                                269                 :                :     /* Verify compatibility and prepare attribute-number map */
  502 alvherre@alvh.no-ip.      270                 :           7160 :     attrMap = build_attrmap_by_name(indesc, outdesc, missing_ok);
                                271                 :                : 
                                272                 :                :     /* Check if the map has a one-to-one match */
 1579 michael@paquier.xyz       273         [ +  + ]:           7160 :     if (check_attrmap_match(indesc, outdesc, attrMap))
                                274                 :                :     {
                                275                 :                :         /* Runtime conversion is not needed */
                                276                 :           5649 :         free_attrmap(attrMap);
                                277                 :           5649 :         return NULL;
                                278                 :                :     }
                                279                 :                : 
                                280                 :           1511 :     return attrMap;
                                281                 :                : }
                                282                 :                : 
                                283                 :                : /*
                                284                 :                :  * check_attrmap_match
                                285                 :                :  *
                                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.
                                288                 :                :  */
                                289                 :                : static bool
                                290                 :          11905 : check_attrmap_match(TupleDesc indesc,
                                291                 :                :                     TupleDesc outdesc,
                                292                 :                :                     AttrMap *attrMap)
                                293                 :                : {
                                294                 :                :     int         i;
                                295                 :                : 
                                296                 :                :     /* no match if attribute numbers are not the same */
                                297         [ +  + ]:          11905 :     if (indesc->natts != outdesc->natts)
                                298                 :            688 :         return false;
                                299                 :                : 
                                300         [ +  + ]:          38414 :     for (i = 0; i < attrMap->maplen; i++)
                                301                 :                :     {
 1530 rhodiumtoad@postgres      302                 :          28067 :         Form_pg_attribute inatt = TupleDescAttr(indesc, i);
                                303                 :          28067 :         Form_pg_attribute outatt = TupleDescAttr(outdesc, i);
                                304                 :                : 
                                305                 :                :         /*
                                306                 :                :          * If the input column has a missing attribute, we need a conversion.
                                307                 :                :          */
                                308         [ +  + ]:          28067 :         if (inatt->atthasmissing)
                                309                 :             26 :             return false;
                                310                 :                : 
 1579 michael@paquier.xyz       311         [ +  + ]:          28041 :         if (attrMap->attnums[i] == (i + 1))
                                312                 :          27166 :             continue;
                                313                 :                : 
                                314                 :                :         /*
                                315                 :                :          * If it's a dropped column and the corresponding input column is also
                                316                 :                :          * dropped, we don't need a conversion.  However, attlen and attalign
                                317                 :                :          * must agree.
                                318                 :                :          */
                                319         [ +  + ]:            875 :         if (attrMap->attnums[i] == 0 &&
                                320         [ +  + ]:             47 :             inatt->attisdropped &&
                                321         [ +  - ]:             31 :             inatt->attlen == outatt->attlen &&
                                322         [ +  - ]:             31 :             inatt->attalign == outatt->attalign)
                                323                 :             31 :             continue;
                                324                 :                : 
                                325                 :            844 :         return false;
                                326                 :                :     }
                                327                 :                : 
                                328                 :          10347 :     return true;
                                329                 :                : }
        

Generated by: LCOV version 2.1-beta2-3-g6141622