PostgreSQL Source Code  git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
tupconvert.h File Reference
#include "access/attmap.h"
#include "access/htup.h"
#include "access/tupdesc.h"
#include "executor/tuptable.h"
#include "nodes/bitmapset.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)
 
TupleConversionMapconvert_tuples_by_name_attrmap (TupleDesc indesc, TupleDesc outdesc, AttrMap *attrMap)
 
HeapTuple execute_attr_map_tuple (HeapTuple tuple, TupleConversionMap *map)
 
TupleTableSlotexecute_attr_map_slot (AttrMap *attrMap, TupleTableSlot *in_slot, TupleTableSlot *out_slot)
 
Bitmapsetexecute_attr_map_cols (AttrMap *attrMap, Bitmapset *in_cols)
 
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.

104 {
105  AttrMap *attrMap;
106 
107  /* Verify compatibility and prepare attribute-number map */
108  attrMap = build_attrmap_by_name_if_req(indesc, outdesc, false);
109 
110  if (attrMap == NULL)
111  {
112  /* runtime conversion is not needed */
113  return NULL;
114  }
115 
116  return convert_tuples_by_name_attrmap(indesc, outdesc, attrMap);
117 }
AttrMap * build_attrmap_by_name_if_req(TupleDesc indesc, TupleDesc outdesc, bool missing_ok)
Definition: attmap.c:263
Definition: attmap.h:35
TupleConversionMap * convert_tuples_by_name_attrmap(TupleDesc indesc, TupleDesc outdesc, AttrMap *attrMap)
Definition: tupconvert.c:124

References build_attrmap_by_name_if_req(), and convert_tuples_by_name_attrmap().

Referenced by acquire_inherited_sample_rows(), apply_handle_tuple_routing(), ExecEvalConvertRowtype(), and ExecGetChildToRootMap().

◆ convert_tuples_by_name_attrmap()

TupleConversionMap* convert_tuples_by_name_attrmap ( TupleDesc  indesc,
TupleDesc  outdesc,
AttrMap attrMap 
)

Definition at line 124 of file tupconvert.c.

127 {
128  int n = outdesc->natts;
129  TupleConversionMap *map;
130 
131  Assert(attrMap != NULL);
132 
133  /* Prepare the map structure */
134  map = (TupleConversionMap *) palloc(sizeof(TupleConversionMap));
135  map->indesc = indesc;
136  map->outdesc = outdesc;
137  map->attrMap = attrMap;
138  /* preallocate workspace for Datum arrays */
139  map->outvalues = (Datum *) palloc(n * sizeof(Datum));
140  map->outisnull = (bool *) palloc(n * sizeof(bool));
141  n = indesc->natts + 1; /* +1 for NULL */
142  map->invalues = (Datum *) palloc(n * sizeof(Datum));
143  map->inisnull = (bool *) palloc(n * sizeof(bool));
144  map->invalues[0] = (Datum) 0; /* set up the NULL entry */
145  map->inisnull[0] = true;
146 
147  return map;
148 }
#define Assert(condition)
Definition: c.h:863
void * palloc(Size size)
Definition: mcxt.c:1317
uintptr_t Datum
Definition: postgres.h:64
AttrMap * attrMap
Definition: tupconvert.h:28
TupleDesc outdesc
Definition: tupconvert.h:27
TupleDesc indesc
Definition: tupconvert.h:26

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

Referenced by convert_tuples_by_name(), and ExecGetRootToChildMap().

◆ convert_tuples_by_position()

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

Definition at line 59 of file tupconvert.c.

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 }
AttrMap * build_attrmap_by_position(TupleDesc indesc, TupleDesc outdesc, const char *msg)
Definition: attmap.c:75

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().

◆ execute_attr_map_cols()

Bitmapset* execute_attr_map_cols ( AttrMap attrMap,
Bitmapset in_cols 
)

Definition at line 252 of file tupconvert.c.

253 {
254  Bitmapset *out_cols;
255  int out_attnum;
256 
257  /* fast path for the common trivial case */
258  if (in_cols == NULL)
259  return NULL;
260 
261  /*
262  * For each output column, check which input column it corresponds to.
263  */
264  out_cols = NULL;
265 
266  for (out_attnum = FirstLowInvalidHeapAttributeNumber;
267  out_attnum <= attrMap->maplen;
268  out_attnum++)
269  {
270  int in_attnum;
271 
272  if (out_attnum < 0)
273  {
274  /* System column. No mapping. */
275  in_attnum = out_attnum;
276  }
277  else if (out_attnum == 0)
278  continue;
279  else
280  {
281  /* normal user column */
282  in_attnum = attrMap->attnums[out_attnum - 1];
283 
284  if (in_attnum == 0)
285  continue;
286  }
287 
288  if (bms_is_member(in_attnum - FirstLowInvalidHeapAttributeNumber, in_cols))
289  out_cols = bms_add_member(out_cols, out_attnum - FirstLowInvalidHeapAttributeNumber);
290  }
291 
292  return out_cols;
293 }
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:510
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:815
int maplen
Definition: attmap.h:37
AttrNumber * attnums
Definition: attmap.h:36
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27

References AttrMap::attnums, bms_add_member(), bms_is_member(), FirstLowInvalidHeapAttributeNumber, and AttrMap::maplen.

Referenced by ExecGetInsertedCols(), and ExecGetUpdatedCols().

◆ execute_attr_map_slot()

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

Definition at line 192 of file tupconvert.c.

195 {
196  Datum *invalues;
197  bool *inisnull;
198  Datum *outvalues;
199  bool *outisnull;
200  int outnatts;
201  int i;
202 
203  /* Sanity checks */
204  Assert(in_slot->tts_tupleDescriptor != NULL &&
205  out_slot->tts_tupleDescriptor != NULL);
206  Assert(in_slot->tts_values != NULL && out_slot->tts_values != NULL);
207 
208  outnatts = out_slot->tts_tupleDescriptor->natts;
209 
210  /* Extract all the values of the in slot. */
211  slot_getallattrs(in_slot);
212 
213  /* Before doing the mapping, clear any old contents from the out slot */
214  ExecClearTuple(out_slot);
215 
216  invalues = in_slot->tts_values;
217  inisnull = in_slot->tts_isnull;
218  outvalues = out_slot->tts_values;
219  outisnull = out_slot->tts_isnull;
220 
221  /* Transpose into proper fields of the out slot. */
222  for (i = 0; i < outnatts; i++)
223  {
224  int j = attrMap->attnums[i] - 1;
225 
226  /* attrMap->attnums[i] == 0 means it's a NULL datum. */
227  if (j == -1)
228  {
229  outvalues[i] = (Datum) 0;
230  outisnull[i] = true;
231  }
232  else
233  {
234  outvalues[i] = invalues[j];
235  outisnull[i] = inisnull[j];
236  }
237  }
238 
239  ExecStoreVirtualTuple(out_slot);
240 
241  return out_slot;
242 }
TupleTableSlot * ExecStoreVirtualTuple(TupleTableSlot *slot)
Definition: execTuples.c:1639
int j
Definition: isn.c:73
int i
Definition: isn.c:72
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:123
bool * tts_isnull
Definition: tuptable.h:127
Datum * tts_values
Definition: tuptable.h:125
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:454
static void slot_getallattrs(TupleTableSlot *slot)
Definition: tuptable.h:368

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

Referenced by AfterTriggerExecute(), AfterTriggerSaveEvent(), apply_handle_tuple_routing(), CopyFrom(), ExecConstraints(), ExecCrossPartitionUpdate(), ExecFindPartition(), ExecPartitionCheckEmitError(), ExecPrepareTupleRouting(), ExecWithCheckOptions(), pgoutput_change(), TransitionTableAddTuple(), and tstoreReceiveSlot_tupmap().

◆ execute_attr_map_tuple()

HeapTuple execute_attr_map_tuple ( HeapTuple  tuple,
TupleConversionMap map 
)

Definition at line 154 of file tupconvert.c.

155 {
156  AttrMap *attrMap = map->attrMap;
157  Datum *invalues = map->invalues;
158  bool *inisnull = map->inisnull;
159  Datum *outvalues = map->outvalues;
160  bool *outisnull = map->outisnull;
161  int i;
162 
163  /*
164  * Extract all the values of the old tuple, offsetting the arrays so that
165  * invalues[0] is left NULL and invalues[1] is the first source attribute;
166  * this exactly matches the numbering convention in attrMap.
167  */
168  heap_deform_tuple(tuple, map->indesc, invalues + 1, inisnull + 1);
169 
170  /*
171  * Transpose into proper fields of the new tuple.
172  */
173  Assert(attrMap->maplen == map->outdesc->natts);
174  for (i = 0; i < attrMap->maplen; i++)
175  {
176  int j = attrMap->attnums[i];
177 
178  outvalues[i] = invalues[j];
179  outisnull[i] = inisnull[j];
180  }
181 
182  /*
183  * Now form the new tuple.
184  */
185  return heap_form_tuple(map->outdesc, outvalues, outisnull);
186 }
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition: heaptuple.c:1116
void heap_deform_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *values, bool *isnull)
Definition: heaptuple.c:1345

References Assert, AttrMap::attnums, TupleConversionMap::attrMap, heap_deform_tuple(), heap_form_tuple(), i, TupleConversionMap::indesc, TupleConversionMap::inisnull, TupleConversionMap::invalues, j, 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(), and plpgsql_exec_trigger().

◆ free_conversion_map()

void free_conversion_map ( TupleConversionMap map)

Definition at line 299 of file tupconvert.c.

300 {
301  /* indesc and outdesc are not ours to free */
302  free_attrmap(map->attrMap);
303  pfree(map->invalues);
304  pfree(map->inisnull);
305  pfree(map->outvalues);
306  pfree(map->outisnull);
307  pfree(map);
308 }
void free_attrmap(AttrMap *map)
Definition: attmap.c:56
void pfree(void *pointer)
Definition: mcxt.c:1521

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

Referenced by acquire_inherited_sample_rows(), and tstoreShutdownReceiver().