PostgreSQL Source Code  git master
execJunk.c File Reference
#include "postgres.h"
#include "executor/executor.h"
Include dependency graph for execJunk.c:

Go to the source code of this file.

Functions

JunkFilterExecInitJunkFilter (List *targetList, TupleTableSlot *slot)
 
JunkFilterExecInitJunkFilterInsertion (List *targetList, TupleDesc cleanTupType, TupleTableSlot *slot)
 
JunkFilterExecInitJunkFilterConversion (List *targetList, TupleDesc cleanTupType, TupleTableSlot *slot)
 
AttrNumber ExecFindJunkAttribute (JunkFilter *junkfilter, const char *attrName)
 
AttrNumber ExecFindJunkAttributeInTlist (List *targetlist, const char *attrName)
 
Datum ExecGetJunkAttribute (TupleTableSlot *slot, AttrNumber attno, bool *isNull)
 
TupleTableSlotExecFilterJunk (JunkFilter *junkfilter, TupleTableSlot *slot)
 

Function Documentation

◆ ExecFilterJunk()

TupleTableSlot* ExecFilterJunk ( JunkFilter junkfilter,
TupleTableSlot slot 
)

Definition at line 287 of file execJunk.c.

References ExecClearTuple(), ExecStoreVirtualTuple(), i, JunkFilter::jf_cleanMap, JunkFilter::jf_cleanTupType, JunkFilter::jf_resultSlot, TupleDescData::natts, slot_getallattrs(), TupleTableSlot::tts_isnull, TupleTableSlot::tts_values, and values.

Referenced by ExecBRUpdateTriggers(), ExecCrossPartitionUpdate(), ExecEvalWholeRowVar(), ExecModifyTable(), ExecUpdate(), ExecutePlan(), and sqlfunction_receive().

288 {
289  TupleTableSlot *resultSlot;
290  AttrNumber *cleanMap;
291  TupleDesc cleanTupType;
292  int cleanLength;
293  int i;
294  Datum *values;
295  bool *isnull;
296  Datum *old_values;
297  bool *old_isnull;
298 
299  /*
300  * Extract all the values of the old tuple.
301  */
302  slot_getallattrs(slot);
303  old_values = slot->tts_values;
304  old_isnull = slot->tts_isnull;
305 
306  /*
307  * get info from the junk filter
308  */
309  cleanTupType = junkfilter->jf_cleanTupType;
310  cleanLength = cleanTupType->natts;
311  cleanMap = junkfilter->jf_cleanMap;
312  resultSlot = junkfilter->jf_resultSlot;
313 
314  /*
315  * Prepare to build a virtual result tuple.
316  */
317  ExecClearTuple(resultSlot);
318  values = resultSlot->tts_values;
319  isnull = resultSlot->tts_isnull;
320 
321  /*
322  * Transpose data into proper fields of the new tuple.
323  */
324  for (i = 0; i < cleanLength; i++)
325  {
326  int j = cleanMap[i];
327 
328  if (j == 0)
329  {
330  values[i] = (Datum) 0;
331  isnull[i] = true;
332  }
333  else
334  {
335  values[i] = old_values[j - 1];
336  isnull[i] = old_isnull[j - 1];
337  }
338  }
339 
340  /*
341  * And return the virtual tuple.
342  */
343  return ExecStoreVirtualTuple(resultSlot);
344 }
AttrNumber * jf_cleanMap
Definition: execnodes.h:370
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:425
TupleTableSlot * jf_resultSlot
Definition: execnodes.h:371
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 jf_cleanTupType
Definition: execnodes.h:369
uintptr_t Datum
Definition: postgres.h:367
static Datum values[MAXATTR]
Definition: bootstrap.c:165
int i
int16 AttrNumber
Definition: attnum.h:21
TupleTableSlot * ExecStoreVirtualTuple(TupleTableSlot *slot)
Definition: execTuples.c:1522

◆ ExecFindJunkAttribute()

AttrNumber ExecFindJunkAttribute ( JunkFilter junkfilter,
const char *  attrName 
)

Definition at line 234 of file execJunk.c.

References ExecFindJunkAttributeInTlist(), and JunkFilter::jf_targetList.

Referenced by ExecInitModifyTable().

235 {
236  return ExecFindJunkAttributeInTlist(junkfilter->jf_targetList, attrName);
237 }
AttrNumber ExecFindJunkAttributeInTlist(List *targetlist, const char *attrName)
Definition: execJunk.c:246
List * jf_targetList
Definition: execnodes.h:368

◆ ExecFindJunkAttributeInTlist()

AttrNumber ExecFindJunkAttributeInTlist ( List targetlist,
const char *  attrName 
)

Definition at line 246 of file execJunk.c.

References InvalidAttrNumber, lfirst, TargetEntry::resjunk, TargetEntry::resname, and TargetEntry::resno.

Referenced by create_foreign_modify(), ExecBuildAuxRowMark(), and ExecFindJunkAttribute().

247 {
248  ListCell *t;
249 
250  foreach(t, targetlist)
251  {
252  TargetEntry *tle = lfirst(t);
253 
254  if (tle->resjunk && tle->resname &&
255  (strcmp(tle->resname, attrName) == 0))
256  {
257  /* We found it ! */
258  return tle->resno;
259  }
260  }
261 
262  return InvalidAttrNumber;
263 }
char * resname
Definition: primnodes.h:1412
bool resjunk
Definition: primnodes.h:1417
AttrNumber resno
Definition: primnodes.h:1411
#define lfirst(lc)
Definition: pg_list.h:169
#define InvalidAttrNumber
Definition: attnum.h:23

◆ ExecGetJunkAttribute()

Datum ExecGetJunkAttribute ( TupleTableSlot slot,
AttrNumber  attno,
bool isNull 
)

Definition at line 273 of file execJunk.c.

References Assert, and slot_getattr().

Referenced by EvalPlanQualFetchRowMark(), ExecLockRows(), ExecModifyTable(), and execute_foreign_modify().

275 {
276  Assert(attno > 0);
277 
278  return slot_getattr(slot, attno, isNull);
279 }
static Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull)
Definition: tuptable.h:381
#define Assert(condition)
Definition: c.h:746

◆ ExecInitJunkFilter()

JunkFilter* ExecInitJunkFilter ( List targetList,
TupleTableSlot slot 
)

Definition at line 60 of file execJunk.c.

References ExecCleanTypeFromTL(), and ExecInitJunkFilterInsertion().

Referenced by ExecInitModifyTable(), ExecInitWholeRowVar(), init_sql_fcache(), and InitPlan().

61 {
62  TupleDesc cleanTupType;
63 
64  /*
65  * Compute the tuple descriptor for the cleaned tuple.
66  */
67  cleanTupType = ExecCleanTypeFromTL(targetList);
68 
69  /*
70  * The rest is the same as ExecInitJunkFilterInsertion, ie, we want to map
71  * every non-junk targetlist column into the output tuple.
72  */
73  return ExecInitJunkFilterInsertion(targetList, cleanTupType, slot);
74 }
TupleDesc ExecCleanTypeFromTL(List *targetList)
Definition: execTuples.c:1920
JunkFilter * ExecInitJunkFilterInsertion(List *targetList, TupleDesc cleanTupType, TupleTableSlot *slot)
Definition: execJunk.c:89

◆ ExecInitJunkFilterConversion()

JunkFilter* ExecInitJunkFilterConversion ( List targetList,
TupleDesc  cleanTupType,
TupleTableSlot slot 
)

Definition at line 161 of file execJunk.c.

References ExecSetSlotDescriptor(), i, JunkFilter::jf_cleanMap, JunkFilter::jf_cleanTupType, JunkFilter::jf_resultSlot, JunkFilter::jf_targetList, lfirst, list_head(), lnext(), makeNode, MakeSingleTupleTableSlot(), TupleDescData::natts, palloc0(), TargetEntry::resjunk, TargetEntry::resno, TTSOpsVirtual, and TupleDescAttr.

Referenced by init_sql_fcache().

164 {
165  JunkFilter *junkfilter;
166  int cleanLength;
167  AttrNumber *cleanMap;
168  ListCell *t;
169  int i;
170 
171  /*
172  * Use the given slot, or make a new slot if we weren't given one.
173  */
174  if (slot)
175  ExecSetSlotDescriptor(slot, cleanTupType);
176  else
177  slot = MakeSingleTupleTableSlot(cleanTupType, &TTSOpsVirtual);
178 
179  /*
180  * Calculate the mapping between the original tuple's attributes and the
181  * "clean" tuple's attributes.
182  *
183  * The "map" is an array of "cleanLength" attribute numbers, i.e. one
184  * entry for every attribute of the "clean" tuple. The value of this entry
185  * is the attribute number of the corresponding attribute of the
186  * "original" tuple. We store zero for any deleted attributes, marking
187  * that a NULL is needed in the output tuple.
188  */
189  cleanLength = cleanTupType->natts;
190  if (cleanLength > 0)
191  {
192  cleanMap = (AttrNumber *) palloc0(cleanLength * sizeof(AttrNumber));
193  t = list_head(targetList);
194  for (i = 0; i < cleanLength; i++)
195  {
196  if (TupleDescAttr(cleanTupType, i)->attisdropped)
197  continue; /* map entry is already zero */
198  for (;;)
199  {
200  TargetEntry *tle = lfirst(t);
201 
202  t = lnext(targetList, t);
203  if (!tle->resjunk)
204  {
205  cleanMap[i] = tle->resno;
206  break;
207  }
208  }
209  }
210  }
211  else
212  cleanMap = NULL;
213 
214  /*
215  * Finally create and initialize the JunkFilter struct.
216  */
217  junkfilter = makeNode(JunkFilter);
218 
219  junkfilter->jf_targetList = targetList;
220  junkfilter->jf_cleanTupType = cleanTupType;
221  junkfilter->jf_cleanMap = cleanMap;
222  junkfilter->jf_resultSlot = slot;
223 
224  return junkfilter;
225 }
AttrNumber * jf_cleanMap
Definition: execnodes.h:370
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:310
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1208
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
const TupleTableSlotOps TTSOpsVirtual
Definition: execTuples.c:83
List * jf_targetList
Definition: execnodes.h:368
TupleTableSlot * jf_resultSlot
Definition: execnodes.h:371
bool resjunk
Definition: primnodes.h:1417
TupleDesc jf_cleanTupType
Definition: execnodes.h:369
AttrNumber resno
Definition: primnodes.h:1411
static ListCell * list_head(const List *l)
Definition: pg_list.h:125
void * palloc0(Size size)
Definition: mcxt.c:981
void ExecSetSlotDescriptor(TupleTableSlot *slot, TupleDesc tupdesc)
Definition: execTuples.c:1259
#define makeNode(_type_)
Definition: nodes.h:576
#define lfirst(lc)
Definition: pg_list.h:169
int i
int16 AttrNumber
Definition: attnum.h:21

◆ ExecInitJunkFilterInsertion()

JunkFilter* ExecInitJunkFilterInsertion ( List targetList,
TupleDesc  cleanTupType,
TupleTableSlot slot 
)

Definition at line 89 of file execJunk.c.

References Assert, ExecSetSlotDescriptor(), JunkFilter::jf_cleanMap, JunkFilter::jf_cleanTupType, JunkFilter::jf_resultSlot, JunkFilter::jf_targetList, lfirst, makeNode, MakeSingleTupleTableSlot(), TupleDescData::natts, palloc(), TargetEntry::resjunk, TargetEntry::resno, and TTSOpsVirtual.

Referenced by ExecInitJunkFilter(), and ExecInitModifyTable().

92 {
93  JunkFilter *junkfilter;
94  int cleanLength;
95  AttrNumber *cleanMap;
96  ListCell *t;
97  AttrNumber cleanResno;
98 
99  /*
100  * Use the given slot, or make a new slot if we weren't given one.
101  */
102  if (slot)
103  ExecSetSlotDescriptor(slot, cleanTupType);
104  else
105  slot = MakeSingleTupleTableSlot(cleanTupType, &TTSOpsVirtual);
106 
107  /*
108  * Now calculate the mapping between the original tuple's attributes and
109  * the "clean" tuple's attributes.
110  *
111  * The "map" is an array of "cleanLength" attribute numbers, i.e. one
112  * entry for every attribute of the "clean" tuple. The value of this entry
113  * is the attribute number of the corresponding attribute of the
114  * "original" tuple. (Zero indicates a NULL output attribute, but we do
115  * not use that feature in this routine.)
116  */
117  cleanLength = cleanTupType->natts;
118  if (cleanLength > 0)
119  {
120  cleanMap = (AttrNumber *) palloc(cleanLength * sizeof(AttrNumber));
121  cleanResno = 0;
122  foreach(t, targetList)
123  {
124  TargetEntry *tle = lfirst(t);
125 
126  if (!tle->resjunk)
127  {
128  cleanMap[cleanResno] = tle->resno;
129  cleanResno++;
130  }
131  }
132  Assert(cleanResno == cleanLength);
133  }
134  else
135  cleanMap = NULL;
136 
137  /*
138  * Finally create and initialize the JunkFilter struct.
139  */
140  junkfilter = makeNode(JunkFilter);
141 
142  junkfilter->jf_targetList = targetList;
143  junkfilter->jf_cleanTupType = cleanTupType;
144  junkfilter->jf_cleanMap = cleanMap;
145  junkfilter->jf_resultSlot = slot;
146 
147  return junkfilter;
148 }
AttrNumber * jf_cleanMap
Definition: execnodes.h:370
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1208
const TupleTableSlotOps TTSOpsVirtual
Definition: execTuples.c:83
List * jf_targetList
Definition: execnodes.h:368
TupleTableSlot * jf_resultSlot
Definition: execnodes.h:371
bool resjunk
Definition: primnodes.h:1417
TupleDesc jf_cleanTupType
Definition: execnodes.h:369
AttrNumber resno
Definition: primnodes.h:1411
void ExecSetSlotDescriptor(TupleTableSlot *slot, TupleDesc tupdesc)
Definition: execTuples.c:1259
#define makeNode(_type_)
Definition: nodes.h:576
#define Assert(condition)
Definition: c.h:746
#define lfirst(lc)
Definition: pg_list.h:169
void * palloc(Size size)
Definition: mcxt.c:950
int16 AttrNumber
Definition: attnum.h:21