LCOV - code coverage report
Current view: top level - src/backend/access/common - tupconvert.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13beta1 Lines: 71 71 100.0 %
Date: 2020-05-25 05:06:35 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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 of
       8             :  * dropped columns.
       9             :  *
      10             :  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
      11             :  * Portions Copyright (c) 1994, Regents of the University of California
      12             :  *
      13             :  *
      14             :  * IDENTIFICATION
      15             :  *    src/backend/access/common/tupconvert.c
      16             :  *
      17             :  *-------------------------------------------------------------------------
      18             :  */
      19             : #include "postgres.h"
      20             : 
      21             : #include "access/tupconvert.h"
      22             : #include "executor/tuptable.h"
      23             : 
      24             : 
      25             : /*
      26             :  * The conversion setup routines have the following common API:
      27             :  *
      28             :  * The setup routine checks using attmap.c whether the given source and
      29             :  * destination tuple descriptors are logically compatible.  If not, it throws
      30             :  * an error.  If so, it returns NULL if they are physically compatible (ie, no
      31             :  * conversion is needed), else a TupleConversionMap that can be used by
      32             :  * execute_attr_map_tuple or execute_attr_map_slot to perform the conversion.
      33             :  *
      34             :  * The TupleConversionMap, if needed, is palloc'd in the caller's memory
      35             :  * context.  Also, the given tuple descriptors are referenced by the map,
      36             :  * so they must survive as long as the map is needed.
      37             :  *
      38             :  * The caller must supply a suitable primary error message to be used if
      39             :  * a compatibility error is thrown.  Recommended coding practice is to use
      40             :  * gettext_noop() on this string, so that it is translatable but won't
      41             :  * actually be translated unless the error gets thrown.
      42             :  *
      43             :  *
      44             :  * Implementation notes:
      45             :  *
      46             :  * The key component of a TupleConversionMap is an attrMap[] array with
      47             :  * one entry per output column.  This entry contains the 1-based index of
      48             :  * the corresponding input column, or zero to force a NULL value (for
      49             :  * a dropped output column).  The TupleConversionMap also contains workspace
      50             :  * arrays.
      51             :  */
      52             : 
      53             : 
      54             : /*
      55             :  * Set up for tuple conversion, matching input and output columns by
      56             :  * position.  (Dropped columns are ignored in both input and output.)
      57             :  */
      58             : TupleConversionMap *
      59        1560 : convert_tuples_by_position(TupleDesc indesc,
      60             :                            TupleDesc outdesc,
      61             :                            const char *msg)
      62             : {
      63             :     TupleConversionMap *map;
      64             :     int         n;
      65             :     AttrMap    *attrMap;
      66             : 
      67             :     /* Verify compatibility and prepare attribute-number map */
      68        1560 :     attrMap = build_attrmap_by_position(indesc, outdesc, msg);
      69             : 
      70        1530 :     if (attrMap == NULL)
      71             :     {
      72             :         /* runtime conversion is not needed */
      73        1470 :         return NULL;
      74             :     }
      75             : 
      76             :     /* Prepare the map structure */
      77          60 :     map = (TupleConversionMap *) palloc(sizeof(TupleConversionMap));
      78          60 :     map->indesc = indesc;
      79          60 :     map->outdesc = outdesc;
      80          60 :     map->attrMap = attrMap;
      81             :     /* preallocate workspace for Datum arrays */
      82          60 :     n = outdesc->natts + 1;      /* +1 for NULL */
      83          60 :     map->outvalues = (Datum *) palloc(n * sizeof(Datum));
      84          60 :     map->outisnull = (bool *) palloc(n * sizeof(bool));
      85          60 :     n = indesc->natts + 1;       /* +1 for NULL */
      86          60 :     map->invalues = (Datum *) palloc(n * sizeof(Datum));
      87          60 :     map->inisnull = (bool *) palloc(n * sizeof(bool));
      88          60 :     map->invalues[0] = (Datum) 0;    /* set up the NULL entry */
      89          60 :     map->inisnull[0] = true;
      90             : 
      91          60 :     return map;
      92             : }
      93             : 
      94             : /*
      95             :  * Set up for tuple conversion, matching input and output columns by name.
      96             :  * (Dropped columns are ignored in both input and output.)  This is intended
      97             :  * for use when the rowtypes are related by inheritance, so we expect an exact
      98             :  * match of both type and typmod.  The error messages will be a bit unhelpful
      99             :  * unless both rowtypes are named composite types.
     100             :  */
     101             : TupleConversionMap *
     102        5964 : convert_tuples_by_name(TupleDesc indesc,
     103             :                        TupleDesc outdesc)
     104             : {
     105             :     TupleConversionMap *map;
     106             :     AttrMap    *attrMap;
     107        5964 :     int         n = outdesc->natts;
     108             : 
     109             :     /* Verify compatibility and prepare attribute-number map */
     110        5964 :     attrMap = build_attrmap_by_name_if_req(indesc, outdesc);
     111             : 
     112        5964 :     if (attrMap == NULL)
     113             :     {
     114             :         /* runtime conversion is not needed */
     115        4364 :         return NULL;
     116             :     }
     117             : 
     118             :     /* Prepare the map structure */
     119        1600 :     map = (TupleConversionMap *) palloc(sizeof(TupleConversionMap));
     120        1600 :     map->indesc = indesc;
     121        1600 :     map->outdesc = outdesc;
     122        1600 :     map->attrMap = attrMap;
     123             :     /* preallocate workspace for Datum arrays */
     124        1600 :     map->outvalues = (Datum *) palloc(n * sizeof(Datum));
     125        1600 :     map->outisnull = (bool *) palloc(n * sizeof(bool));
     126        1600 :     n = indesc->natts + 1;       /* +1 for NULL */
     127        1600 :     map->invalues = (Datum *) palloc(n * sizeof(Datum));
     128        1600 :     map->inisnull = (bool *) palloc(n * sizeof(bool));
     129        1600 :     map->invalues[0] = (Datum) 0;    /* set up the NULL entry */
     130        1600 :     map->inisnull[0] = true;
     131             : 
     132        1600 :     return map;
     133             : }
     134             : 
     135             : /*
     136             :  * Perform conversion of a tuple according to the map.
     137             :  */
     138             : HeapTuple
     139       72998 : execute_attr_map_tuple(HeapTuple tuple, TupleConversionMap *map)
     140             : {
     141       72998 :     AttrMap    *attrMap = map->attrMap;
     142       72998 :     Datum      *invalues = map->invalues;
     143       72998 :     bool       *inisnull = map->inisnull;
     144       72998 :     Datum      *outvalues = map->outvalues;
     145       72998 :     bool       *outisnull = map->outisnull;
     146             :     int         i;
     147             : 
     148             :     /*
     149             :      * Extract all the values of the old tuple, offsetting the arrays so that
     150             :      * invalues[0] is left NULL and invalues[1] is the first source attribute;
     151             :      * this exactly matches the numbering convention in attrMap.
     152             :      */
     153       72998 :     heap_deform_tuple(tuple, map->indesc, invalues + 1, inisnull + 1);
     154             : 
     155             :     /*
     156             :      * Transpose into proper fields of the new tuple.
     157             :      */
     158             :     Assert(attrMap->maplen == map->outdesc->natts);
     159      290412 :     for (i = 0; i < attrMap->maplen; i++)
     160             :     {
     161      217414 :         int         j = attrMap->attnums[i];
     162             : 
     163      217414 :         outvalues[i] = invalues[j];
     164      217414 :         outisnull[i] = inisnull[j];
     165             :     }
     166             : 
     167             :     /*
     168             :      * Now form the new tuple.
     169             :      */
     170       72998 :     return heap_form_tuple(map->outdesc, outvalues, outisnull);
     171             : }
     172             : 
     173             : /*
     174             :  * Perform conversion of a tuple slot according to the map.
     175             :  */
     176             : TupleTableSlot *
     177       95088 : execute_attr_map_slot(AttrMap *attrMap,
     178             :                       TupleTableSlot *in_slot,
     179             :                       TupleTableSlot *out_slot)
     180             : {
     181             :     Datum      *invalues;
     182             :     bool       *inisnull;
     183             :     Datum      *outvalues;
     184             :     bool       *outisnull;
     185             :     int         outnatts;
     186             :     int         i;
     187             : 
     188             :     /* Sanity checks */
     189             :     Assert(in_slot->tts_tupleDescriptor != NULL &&
     190             :            out_slot->tts_tupleDescriptor != NULL);
     191             :     Assert(in_slot->tts_values != NULL && out_slot->tts_values != NULL);
     192             : 
     193       95088 :     outnatts = out_slot->tts_tupleDescriptor->natts;
     194             : 
     195             :     /* Extract all the values of the in slot. */
     196       95088 :     slot_getallattrs(in_slot);
     197             : 
     198             :     /* Before doing the mapping, clear any old contents from the out slot */
     199       95088 :     ExecClearTuple(out_slot);
     200             : 
     201       95088 :     invalues = in_slot->tts_values;
     202       95088 :     inisnull = in_slot->tts_isnull;
     203       95088 :     outvalues = out_slot->tts_values;
     204       95088 :     outisnull = out_slot->tts_isnull;
     205             : 
     206             :     /* Transpose into proper fields of the out slot. */
     207      383304 :     for (i = 0; i < outnatts; i++)
     208             :     {
     209      288216 :         int         j = attrMap->attnums[i] - 1;
     210             : 
     211             :         /* attrMap->attnums[i] == 0 means it's a NULL datum. */
     212      288216 :         if (j == -1)
     213             :         {
     214        1368 :             outvalues[i] = (Datum) 0;
     215        1368 :             outisnull[i] = true;
     216             :         }
     217             :         else
     218             :         {
     219      286848 :             outvalues[i] = invalues[j];
     220      286848 :             outisnull[i] = inisnull[j];
     221             :         }
     222             :     }
     223             : 
     224       95088 :     ExecStoreVirtualTuple(out_slot);
     225             : 
     226       95088 :     return out_slot;
     227             : }
     228             : 
     229             : /*
     230             :  * Free a TupleConversionMap structure.
     231             :  */
     232             : void
     233         216 : free_conversion_map(TupleConversionMap *map)
     234             : {
     235             :     /* indesc and outdesc are not ours to free */
     236         216 :     free_attrmap(map->attrMap);
     237         216 :     pfree(map->invalues);
     238         216 :     pfree(map->inisnull);
     239         216 :     pfree(map->outvalues);
     240         216 :     pfree(map->outisnull);
     241         216 :     pfree(map);
     242         216 : }

Generated by: LCOV version 1.13