PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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, bool hasoid, 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

TupleTableSlot* ExecFilterJunk ( JunkFilter junkfilter,
TupleTableSlot slot 
)

Definition at line 262 of file execJunk.c.

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

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

263 {
264  TupleTableSlot *resultSlot;
265  AttrNumber *cleanMap;
266  TupleDesc cleanTupType;
267  int cleanLength;
268  int i;
269  Datum *values;
270  bool *isnull;
271  Datum *old_values;
272  bool *old_isnull;
273 
274  /*
275  * Extract all the values of the old tuple.
276  */
277  slot_getallattrs(slot);
278  old_values = slot->tts_values;
279  old_isnull = slot->tts_isnull;
280 
281  /*
282  * get info from the junk filter
283  */
284  cleanTupType = junkfilter->jf_cleanTupType;
285  cleanLength = cleanTupType->natts;
286  cleanMap = junkfilter->jf_cleanMap;
287  resultSlot = junkfilter->jf_resultSlot;
288 
289  /*
290  * Prepare to build a virtual result tuple.
291  */
292  ExecClearTuple(resultSlot);
293  values = resultSlot->tts_values;
294  isnull = resultSlot->tts_isnull;
295 
296  /*
297  * Transpose data into proper fields of the new tuple.
298  */
299  for (i = 0; i < cleanLength; i++)
300  {
301  int j = cleanMap[i];
302 
303  if (j == 0)
304  {
305  values[i] = (Datum) 0;
306  isnull[i] = true;
307  }
308  else
309  {
310  values[i] = old_values[j - 1];
311  isnull[i] = old_isnull[j - 1];
312  }
313  }
314 
315  /*
316  * And return the virtual tuple.
317  */
318  return ExecStoreVirtualTuple(resultSlot);
319 }
AttrNumber * jf_cleanMap
Definition: execnodes.h:334
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
TupleTableSlot * jf_resultSlot
Definition: execnodes.h:335
Datum * tts_values
Definition: tuptable.h:125
int natts
Definition: tupdesc.h:73
bool * tts_isnull
Definition: tuptable.h:126
TupleDesc jf_cleanTupType
Definition: execnodes.h:333
void slot_getallattrs(TupleTableSlot *slot)
Definition: heaptuple.c:1238
uintptr_t Datum
Definition: postgres.h:372
static Datum values[MAXATTR]
Definition: bootstrap.c:163
int i
int16 AttrNumber
Definition: attnum.h:21
TupleTableSlot * ExecStoreVirtualTuple(TupleTableSlot *slot)
Definition: execTuples.c:488
AttrNumber ExecFindJunkAttribute ( JunkFilter junkfilter,
const char *  attrName 
)

Definition at line 209 of file execJunk.c.

References ExecFindJunkAttributeInTlist(), and JunkFilter::jf_targetList.

Referenced by ExecInitModifyTable().

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

Definition at line 221 of file execJunk.c.

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

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

222 {
223  ListCell *t;
224 
225  foreach(t, targetlist)
226  {
227  TargetEntry *tle = lfirst(t);
228 
229  if (tle->resjunk && tle->resname &&
230  (strcmp(tle->resname, attrName) == 0))
231  {
232  /* We found it ! */
233  return tle->resno;
234  }
235  }
236 
237  return InvalidAttrNumber;
238 }
char * resname
Definition: primnodes.h:1370
bool resjunk
Definition: primnodes.h:1375
AttrNumber resno
Definition: primnodes.h:1369
#define lfirst(lc)
Definition: pg_list.h:106
#define InvalidAttrNumber
Definition: attnum.h:23
Datum ExecGetJunkAttribute ( TupleTableSlot slot,
AttrNumber  attno,
bool isNull 
)

Definition at line 248 of file execJunk.c.

References Assert, and slot_getattr().

Referenced by EvalPlanQualFetchRowMarks(), ExecLockRows(), ExecModifyTable(), postgresExecForeignDelete(), and postgresExecForeignUpdate().

250 {
251  Assert(attno > 0);
252 
253  return slot_getattr(slot, attno, isNull);
254 }
#define Assert(condition)
Definition: c.h:676
Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull)
Definition: heaptuple.c:1142
JunkFilter* ExecInitJunkFilter ( List targetList,
bool  hasoid,
TupleTableSlot slot 
)

Definition at line 61 of file execJunk.c.

References ExecCleanTypeFromTL(), ExecSetSlotDescriptor(), JunkFilter::jf_cleanMap, JunkFilter::jf_cleanTupType, JunkFilter::jf_resultSlot, JunkFilter::jf_targetList, lfirst, makeNode, MakeSingleTupleTableSlot(), tupleDesc::natts, NULL, palloc(), TargetEntry::resjunk, and TargetEntry::resno.

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

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

Definition at line 136 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(), tupleDesc::natts, NULL, palloc0(), TargetEntry::resjunk, TargetEntry::resno, and TupleDescAttr.

Referenced by check_sql_fn_retval().

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