PostgreSQL Source Code  git master
tupconvert.h File Reference
#include "access/attmap.h"
#include "access/htup.h"
#include "access/tupdesc.h"
#include "executor/tuptable.h"
Include dependency graph for tupconvert.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  TupleConversionMap
 

Typedefs

typedef struct TupleConversionMap TupleConversionMap
 

Functions

TupleConversionMapconvert_tuples_by_position (TupleDesc indesc, TupleDesc outdesc, const char *msg)
 
TupleConversionMapconvert_tuples_by_name (TupleDesc indesc, TupleDesc outdesc)
 
HeapTuple execute_attr_map_tuple (HeapTuple tuple, TupleConversionMap *map)
 
TupleTableSlotexecute_attr_map_slot (AttrMap *attrMap, TupleTableSlot *in_slot, TupleTableSlot *out_slot)
 
void free_conversion_map (TupleConversionMap *map)
 

Typedef Documentation

◆ TupleConversionMap

Function Documentation

◆ convert_tuples_by_name()

TupleConversionMap* convert_tuples_by_name ( TupleDesc  indesc,
TupleDesc  outdesc 
)

Definition at line 102 of file tupconvert.c.

References TupleConversionMap::attrMap, build_attrmap_by_name_if_req(), TupleConversionMap::indesc, TupleConversionMap::inisnull, TupleConversionMap::invalues, TupleDescData::natts, TupleConversionMap::outdesc, TupleConversionMap::outisnull, TupleConversionMap::outvalues, and palloc().

Referenced by acquire_inherited_sample_rows(), apply_handle_tuple_routing(), ExecEvalConvertRowtype(), ExecInitRoutingInfo(), ExecSetupChildParentMapForSubplan(), and maybe_send_schema().

104 {
105  TupleConversionMap *map;
106  AttrMap *attrMap;
107  int n = outdesc->natts;
108 
109  /* Verify compatibility and prepare attribute-number map */
110  attrMap = build_attrmap_by_name_if_req(indesc, outdesc);
111 
112  if (attrMap == NULL)
113  {
114  /* runtime conversion is not needed */
115  return NULL;
116  }
117 
118  /* Prepare the map structure */
119  map = (TupleConversionMap *) palloc(sizeof(TupleConversionMap));
120  map->indesc = indesc;
121  map->outdesc = outdesc;
122  map->attrMap = attrMap;
123  /* preallocate workspace for Datum arrays */
124  map->outvalues = (Datum *) palloc(n * sizeof(Datum));
125  map->outisnull = (bool *) palloc(n * sizeof(bool));
126  n = indesc->natts + 1; /* +1 for NULL */
127  map->invalues = (Datum *) palloc(n * sizeof(Datum));
128  map->inisnull = (bool *) palloc(n * sizeof(bool));
129  map->invalues[0] = (Datum) 0; /* set up the NULL entry */
130  map->inisnull[0] = true;
131 
132  return map;
133 }
TupleDesc indesc
Definition: tupconvert.h:25
TupleDesc outdesc
Definition: tupconvert.h:26
Definition: attmap.h:34
AttrMap * build_attrmap_by_name_if_req(TupleDesc indesc, TupleDesc outdesc)
Definition: attmap.c:259
AttrMap * attrMap
Definition: tupconvert.h:27
uintptr_t Datum
Definition: postgres.h:367
void * palloc(Size size)
Definition: mcxt.c:949

◆ convert_tuples_by_position()

TupleConversionMap* convert_tuples_by_position ( TupleDesc  indesc,
TupleDesc  outdesc,
const char *  msg 
)

Definition at line 59 of file tupconvert.c.

References TupleConversionMap::attrMap, build_attrmap_by_position(), TupleConversionMap::indesc, TupleConversionMap::inisnull, TupleConversionMap::invalues, TupleDescData::natts, TupleConversionMap::outdesc, TupleConversionMap::outisnull, TupleConversionMap::outvalues, and palloc().

Referenced by coerce_function_result_tuple(), exec_stmt_return_next(), plpgsql_exec_trigger(), and tstoreStartupReceiver().

62 {
63  TupleConversionMap *map;
64  int n;
65  AttrMap *attrMap;
66 
67  /* Verify compatibility and prepare attribute-number map */
68  attrMap = build_attrmap_by_position(indesc, outdesc, msg);
69 
70  if (attrMap == NULL)
71  {
72  /* runtime conversion is not needed */
73  return NULL;
74  }
75 
76  /* Prepare the map structure */
77  map = (TupleConversionMap *) palloc(sizeof(TupleConversionMap));
78  map->indesc = indesc;
79  map->outdesc = outdesc;
80  map->attrMap = attrMap;
81  /* preallocate workspace for Datum arrays */
82  n = outdesc->natts + 1; /* +1 for NULL */
83  map->outvalues = (Datum *) palloc(n * sizeof(Datum));
84  map->outisnull = (bool *) palloc(n * sizeof(bool));
85  n = indesc->natts + 1; /* +1 for NULL */
86  map->invalues = (Datum *) palloc(n * sizeof(Datum));
87  map->inisnull = (bool *) palloc(n * sizeof(bool));
88  map->invalues[0] = (Datum) 0; /* set up the NULL entry */
89  map->inisnull[0] = true;
90 
91  return map;
92 }
TupleDesc indesc
Definition: tupconvert.h:25
TupleDesc outdesc
Definition: tupconvert.h:26
Definition: attmap.h:34
AttrMap * attrMap
Definition: tupconvert.h:27
uintptr_t Datum
Definition: postgres.h:367
AttrMap * build_attrmap_by_position(TupleDesc indesc, TupleDesc outdesc, const char *msg)
Definition: attmap.c:76
void * palloc(Size size)
Definition: mcxt.c:949

◆ execute_attr_map_slot()

TupleTableSlot* execute_attr_map_slot ( AttrMap attrMap,
TupleTableSlot in_slot,
TupleTableSlot out_slot 
)

Definition at line 177 of file tupconvert.c.

References Assert, AttrMap::attnums, ExecClearTuple(), ExecStoreVirtualTuple(), i, TupleDescData::natts, slot_getallattrs(), TupleTableSlot::tts_isnull, TupleTableSlot::tts_tupleDescriptor, and TupleTableSlot::tts_values.

Referenced by AfterTriggerSaveEvent(), apply_handle_tuple_routing(), CopyFrom(), ExecConstraints(), ExecFindPartition(), ExecPartitionCheckEmitError(), ExecPrepareTupleRouting(), ExecUpdate(), ExecWithCheckOptions(), and tstoreReceiveSlot_tupmap().

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  outnatts = out_slot->tts_tupleDescriptor->natts;
194 
195  /* Extract all the values of the in slot. */
196  slot_getallattrs(in_slot);
197 
198  /* Before doing the mapping, clear any old contents from the out slot */
199  ExecClearTuple(out_slot);
200 
201  invalues = in_slot->tts_values;
202  inisnull = in_slot->tts_isnull;
203  outvalues = out_slot->tts_values;
204  outisnull = out_slot->tts_isnull;
205 
206  /* Transpose into proper fields of the out slot. */
207  for (i = 0; i < outnatts; i++)
208  {
209  int j = attrMap->attnums[i] - 1;
210 
211  /* attrMap->attnums[i] == 0 means it's a NULL datum. */
212  if (j == -1)
213  {
214  outvalues[i] = (Datum) 0;
215  outisnull[i] = true;
216  }
217  else
218  {
219  outvalues[i] = invalues[j];
220  outisnull[i] = inisnull[j];
221  }
222  }
223 
224  ExecStoreVirtualTuple(out_slot);
225 
226  return out_slot;
227 }
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:425
Datum * tts_values
Definition: tuptable.h:126
static void slot_getallattrs(TupleTableSlot *slot)
Definition: tuptable.h:354
bool * tts_isnull
Definition: tuptable.h:128
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:124
uintptr_t Datum
Definition: postgres.h:367
#define Assert(condition)
Definition: c.h:738
AttrNumber * attnums
Definition: attmap.h:36
int i
TupleTableSlot * ExecStoreVirtualTuple(TupleTableSlot *slot)
Definition: execTuples.c:1522

◆ execute_attr_map_tuple()

HeapTuple execute_attr_map_tuple ( HeapTuple  tuple,
TupleConversionMap map 
)

Definition at line 139 of file tupconvert.c.

References Assert, AttrMap::attnums, TupleConversionMap::attrMap, heap_deform_tuple(), heap_form_tuple(), i, TupleConversionMap::indesc, TupleConversionMap::inisnull, TupleConversionMap::invalues, AttrMap::maplen, TupleDescData::natts, TupleConversionMap::outdesc, TupleConversionMap::outisnull, and TupleConversionMap::outvalues.

Referenced by acquire_inherited_sample_rows(), coerce_function_result_tuple(), exec_stmt_return_next(), ExecEvalConvertRowtype(), pgoutput_change(), and plpgsql_exec_trigger().

140 {
141  AttrMap *attrMap = map->attrMap;
142  Datum *invalues = map->invalues;
143  bool *inisnull = map->inisnull;
144  Datum *outvalues = map->outvalues;
145  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  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  for (i = 0; i < attrMap->maplen; i++)
160  {
161  int j = attrMap->attnums[i];
162 
163  outvalues[i] = invalues[j];
164  outisnull[i] = inisnull[j];
165  }
166 
167  /*
168  * Now form the new tuple.
169  */
170  return heap_form_tuple(map->outdesc, outvalues, outisnull);
171 }
TupleDesc indesc
Definition: tupconvert.h:25
TupleDesc outdesc
Definition: tupconvert.h:26
int maplen
Definition: attmap.h:37
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
Definition: attmap.h:34
AttrMap * attrMap
Definition: tupconvert.h:27
uintptr_t Datum
Definition: postgres.h:367
#define Assert(condition)
Definition: c.h:738
AttrNumber * attnums
Definition: attmap.h:36
void heap_deform_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *values, bool *isnull)
Definition: heaptuple.c:1249
int i

◆ free_conversion_map()

void free_conversion_map ( TupleConversionMap map)

Definition at line 233 of file tupconvert.c.

References TupleConversionMap::attrMap, free_attrmap(), TupleConversionMap::inisnull, TupleConversionMap::invalues, TupleConversionMap::outisnull, TupleConversionMap::outvalues, and pfree().

Referenced by acquire_inherited_sample_rows(), and tstoreShutdownReceiver().

234 {
235  /* indesc and outdesc are not ours to free */
236  free_attrmap(map->attrMap);
237  pfree(map->invalues);
238  pfree(map->inisnull);
239  pfree(map->outvalues);
240  pfree(map->outisnull);
241  pfree(map);
242 }
void free_attrmap(AttrMap *map)
Definition: attmap.c:57
void pfree(void *pointer)
Definition: mcxt.c:1056
AttrMap * attrMap
Definition: tupconvert.h:27