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)
 
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 261 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(), ExecEvalWholeRowVar(), ExecModifyTable(), ExecUpdate(), ExecutePlan(), and sqlfunction_receive().

262 {
263  TupleTableSlot *resultSlot;
264  AttrNumber *cleanMap;
265  TupleDesc cleanTupType;
266  int cleanLength;
267  int i;
268  Datum *values;
269  bool *isnull;
270  Datum *old_values;
271  bool *old_isnull;
272 
273  /*
274  * Extract all the values of the old tuple.
275  */
276  slot_getallattrs(slot);
277  old_values = slot->tts_values;
278  old_isnull = slot->tts_isnull;
279 
280  /*
281  * get info from the junk filter
282  */
283  cleanTupType = junkfilter->jf_cleanTupType;
284  cleanLength = cleanTupType->natts;
285  cleanMap = junkfilter->jf_cleanMap;
286  resultSlot = junkfilter->jf_resultSlot;
287 
288  /*
289  * Prepare to build a virtual result tuple.
290  */
291  ExecClearTuple(resultSlot);
292  values = resultSlot->tts_values;
293  isnull = resultSlot->tts_isnull;
294 
295  /*
296  * Transpose data into proper fields of the new tuple.
297  */
298  for (i = 0; i < cleanLength; i++)
299  {
300  int j = cleanMap[i];
301 
302  if (j == 0)
303  {
304  values[i] = (Datum) 0;
305  isnull[i] = true;
306  }
307  else
308  {
309  values[i] = old_values[j - 1];
310  isnull[i] = old_isnull[j - 1];
311  }
312  }
313 
314  /*
315  * And return the virtual tuple.
316  */
317  return ExecStoreVirtualTuple(resultSlot);
318 }
AttrNumber * jf_cleanMap
Definition: execnodes.h:368
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:425
TupleTableSlot * jf_resultSlot
Definition: execnodes.h:369
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:367
uintptr_t Datum
Definition: postgres.h:367
static Datum values[MAXATTR]
Definition: bootstrap.c:167
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 208 of file execJunk.c.

References ExecFindJunkAttributeInTlist(), and JunkFilter::jf_targetList.

Referenced by ExecInitModifyTable().

209 {
210  return ExecFindJunkAttributeInTlist(junkfilter->jf_targetList, attrName);
211 }
AttrNumber ExecFindJunkAttributeInTlist(List *targetlist, const char *attrName)
Definition: execJunk.c:220
List * jf_targetList
Definition: execnodes.h:366

◆ ExecFindJunkAttributeInTlist()

AttrNumber ExecFindJunkAttributeInTlist ( List targetlist,
const char *  attrName 
)

Definition at line 220 of file execJunk.c.

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

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

221 {
222  ListCell *t;
223 
224  foreach(t, targetlist)
225  {
226  TargetEntry *tle = lfirst(t);
227 
228  if (tle->resjunk && tle->resname &&
229  (strcmp(tle->resname, attrName) == 0))
230  {
231  /* We found it ! */
232  return tle->resno;
233  }
234  }
235 
236  return InvalidAttrNumber;
237 }
char * resname
Definition: primnodes.h:1395
bool resjunk
Definition: primnodes.h:1400
AttrNumber resno
Definition: primnodes.h:1394
#define lfirst(lc)
Definition: pg_list.h:190
#define InvalidAttrNumber
Definition: attnum.h:23

◆ ExecGetJunkAttribute()

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

Definition at line 247 of file execJunk.c.

References Assert, and slot_getattr().

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

249 {
250  Assert(attno > 0);
251 
252  return slot_getattr(slot, attno, isNull);
253 }
static Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull)
Definition: tuptable.h:381
#define Assert(condition)
Definition: c.h:739

◆ ExecInitJunkFilter()

JunkFilter* ExecInitJunkFilter ( List targetList,
TupleTableSlot slot 
)

Definition at line 60 of file execJunk.c.

References ExecCleanTypeFromTL(), 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 check_sql_fn_retval(), ExecInitModifyTable(), ExecInitWholeRowVar(), and InitPlan().

61 {
62  JunkFilter *junkfilter;
63  TupleDesc cleanTupType;
64  int cleanLength;
65  AttrNumber *cleanMap;
66  ListCell *t;
67  AttrNumber cleanResno;
68 
69  /*
70  * Compute the tuple descriptor for the cleaned tuple.
71  */
72  cleanTupType = ExecCleanTypeFromTL(targetList);
73 
74  /*
75  * Use the given slot, or make a new slot if we weren't given one.
76  */
77  if (slot)
78  ExecSetSlotDescriptor(slot, cleanTupType);
79  else
80  slot = MakeSingleTupleTableSlot(cleanTupType, &TTSOpsVirtual);
81 
82  /*
83  * Now calculate the mapping between the original tuple's attributes and
84  * the "clean" tuple's attributes.
85  *
86  * The "map" is an array of "cleanLength" attribute numbers, i.e. one
87  * entry for every attribute of the "clean" tuple. The value of this entry
88  * is the attribute number of the corresponding attribute of the
89  * "original" tuple. (Zero indicates a NULL output attribute, but we do
90  * not use that feature in this routine.)
91  */
92  cleanLength = cleanTupType->natts;
93  if (cleanLength > 0)
94  {
95  cleanMap = (AttrNumber *) palloc(cleanLength * sizeof(AttrNumber));
96  cleanResno = 1;
97  foreach(t, targetList)
98  {
99  TargetEntry *tle = lfirst(t);
100 
101  if (!tle->resjunk)
102  {
103  cleanMap[cleanResno - 1] = tle->resno;
104  cleanResno++;
105  }
106  }
107  }
108  else
109  cleanMap = NULL;
110 
111  /*
112  * Finally create and initialize the JunkFilter struct.
113  */
114  junkfilter = makeNode(JunkFilter);
115 
116  junkfilter->jf_targetList = targetList;
117  junkfilter->jf_cleanTupType = cleanTupType;
118  junkfilter->jf_cleanMap = cleanMap;
119  junkfilter->jf_resultSlot = slot;
120 
121  return junkfilter;
122 }
AttrNumber * jf_cleanMap
Definition: execnodes.h:368
TupleDesc ExecCleanTypeFromTL(List *targetList)
Definition: execTuples.c:1920
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:366
TupleTableSlot * jf_resultSlot
Definition: execnodes.h:369
bool resjunk
Definition: primnodes.h:1400
TupleDesc jf_cleanTupType
Definition: execnodes.h:367
AttrNumber resno
Definition: primnodes.h:1394
void ExecSetSlotDescriptor(TupleTableSlot *slot, TupleDesc tupdesc)
Definition: execTuples.c:1259
#define makeNode(_type_)
Definition: nodes.h:573
#define lfirst(lc)
Definition: pg_list.h:190
void * palloc(Size size)
Definition: mcxt.c:949
int16 AttrNumber
Definition: attnum.h:21

◆ ExecInitJunkFilterConversion()

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

Definition at line 135 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 check_sql_fn_retval().

138 {
139  JunkFilter *junkfilter;
140  int cleanLength;
141  AttrNumber *cleanMap;
142  ListCell *t;
143  int i;
144 
145  /*
146  * Use the given slot, or make a new slot if we weren't given one.
147  */
148  if (slot)
149  ExecSetSlotDescriptor(slot, cleanTupType);
150  else
151  slot = MakeSingleTupleTableSlot(cleanTupType, &TTSOpsVirtual);
152 
153  /*
154  * Calculate the mapping between the original tuple's attributes and the
155  * "clean" tuple's attributes.
156  *
157  * The "map" is an array of "cleanLength" attribute numbers, i.e. one
158  * entry for every attribute of the "clean" tuple. The value of this entry
159  * is the attribute number of the corresponding attribute of the
160  * "original" tuple. We store zero for any deleted attributes, marking
161  * that a NULL is needed in the output tuple.
162  */
163  cleanLength = cleanTupType->natts;
164  if (cleanLength > 0)
165  {
166  cleanMap = (AttrNumber *) palloc0(cleanLength * sizeof(AttrNumber));
167  t = list_head(targetList);
168  for (i = 0; i < cleanLength; i++)
169  {
170  if (TupleDescAttr(cleanTupType, i)->attisdropped)
171  continue; /* map entry is already zero */
172  for (;;)
173  {
174  TargetEntry *tle = lfirst(t);
175 
176  t = lnext(targetList, t);
177  if (!tle->resjunk)
178  {
179  cleanMap[i] = tle->resno;
180  break;
181  }
182  }
183  }
184  }
185  else
186  cleanMap = NULL;
187 
188  /*
189  * Finally create and initialize the JunkFilter struct.
190  */
191  junkfilter = makeNode(JunkFilter);
192 
193  junkfilter->jf_targetList = targetList;
194  junkfilter->jf_cleanTupType = cleanTupType;
195  junkfilter->jf_cleanMap = cleanMap;
196  junkfilter->jf_resultSlot = slot;
197 
198  return junkfilter;
199 }
AttrNumber * jf_cleanMap
Definition: execnodes.h:368
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:321
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:366
TupleTableSlot * jf_resultSlot
Definition: execnodes.h:369
bool resjunk
Definition: primnodes.h:1400
TupleDesc jf_cleanTupType
Definition: execnodes.h:367
AttrNumber resno
Definition: primnodes.h:1394
static ListCell * list_head(const List *l)
Definition: pg_list.h:125
void * palloc0(Size size)
Definition: mcxt.c:980
void ExecSetSlotDescriptor(TupleTableSlot *slot, TupleDesc tupdesc)
Definition: execTuples.c:1259
#define makeNode(_type_)
Definition: nodes.h:573
#define lfirst(lc)
Definition: pg_list.h:190
int i
int16 AttrNumber
Definition: attnum.h:21