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)
 
TupleTableSlotExecFilterJunk (JunkFilter *junkfilter, TupleTableSlot *slot)
 

Function Documentation

◆ ExecFilterJunk()

TupleTableSlot* ExecFilterJunk ( JunkFilter junkfilter,
TupleTableSlot slot 
)

Definition at line 247 of file execJunk.c.

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

Referenced by ExecEvalWholeRowVar(), ExecutePlan(), and sqlfunction_receive().

248 {
249  TupleTableSlot *resultSlot;
250  AttrNumber *cleanMap;
251  TupleDesc cleanTupType;
252  int cleanLength;
253  int i;
254  Datum *values;
255  bool *isnull;
256  Datum *old_values;
257  bool *old_isnull;
258 
259  /*
260  * Extract all the values of the old tuple.
261  */
262  slot_getallattrs(slot);
263  old_values = slot->tts_values;
264  old_isnull = slot->tts_isnull;
265 
266  /*
267  * get info from the junk filter
268  */
269  cleanTupType = junkfilter->jf_cleanTupType;
270  cleanLength = cleanTupType->natts;
271  cleanMap = junkfilter->jf_cleanMap;
272  resultSlot = junkfilter->jf_resultSlot;
273 
274  /*
275  * Prepare to build a virtual result tuple.
276  */
277  ExecClearTuple(resultSlot);
278  values = resultSlot->tts_values;
279  isnull = resultSlot->tts_isnull;
280 
281  /*
282  * Transpose data into proper fields of the new tuple.
283  */
284  for (i = 0; i < cleanLength; i++)
285  {
286  int j = cleanMap[i];
287 
288  if (j == 0)
289  {
290  values[i] = (Datum) 0;
291  isnull[i] = true;
292  }
293  else
294  {
295  values[i] = old_values[j - 1];
296  isnull[i] = old_isnull[j - 1];
297  }
298  }
299 
300  /*
301  * And return the virtual tuple.
302  */
303  return ExecStoreVirtualTuple(resultSlot);
304 }
AttrNumber * jf_cleanMap
Definition: execnodes.h:367
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:425
TupleTableSlot * jf_resultSlot
Definition: execnodes.h:368
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:366
uintptr_t Datum
Definition: postgres.h:411
static Datum values[MAXATTR]
Definition: bootstrap.c:166
int i
int16 AttrNumber
Definition: attnum.h:21
TupleTableSlot * ExecStoreVirtualTuple(TupleTableSlot *slot)
Definition: execTuples.c:1552

◆ ExecFindJunkAttribute()

AttrNumber ExecFindJunkAttribute ( JunkFilter junkfilter,
const char *  attrName 
)

Definition at line 210 of file execJunk.c.

References ExecFindJunkAttributeInTlist(), and JunkFilter::jf_targetList.

211 {
212  return ExecFindJunkAttributeInTlist(junkfilter->jf_targetList, attrName);
213 }
AttrNumber ExecFindJunkAttributeInTlist(List *targetlist, const char *attrName)
Definition: execJunk.c:222
List * jf_targetList
Definition: execnodes.h:365

◆ ExecFindJunkAttributeInTlist()

AttrNumber ExecFindJunkAttributeInTlist ( List targetlist,
const char *  attrName 
)

Definition at line 222 of file execJunk.c.

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

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

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

◆ ExecInitJunkFilter()

JunkFilter* ExecInitJunkFilter ( List targetList,
TupleTableSlot slot 
)

Definition at line 60 of file execJunk.c.

References Assert, 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 ExecInitWholeRowVar(), init_sql_fcache(), and InitPlan().

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

◆ ExecInitJunkFilterConversion()

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

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

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