PostgreSQL Source Code  git master
execJunk.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * execJunk.c
4  * Junk attribute support stuff....
5  *
6  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/backend/executor/execJunk.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16 
17 #include "executor/executor.h"
18 
19 /*-------------------------------------------------------------------------
20  * XXX this stuff should be rewritten to take advantage
21  * of ExecProject() and the ProjectionInfo node.
22  * -cim 6/3/91
23  *
24  * An attribute of a tuple living inside the executor, can be
25  * either a normal attribute or a "junk" attribute. "junk" attributes
26  * never make it out of the executor, i.e. they are never printed,
27  * returned or stored on disk. Their only purpose in life is to
28  * store some information useful only to the executor, mainly the values
29  * of system attributes like "ctid", or sort key columns that are not to
30  * be output.
31  *
32  * The general idea is the following: A target list consists of a list of
33  * TargetEntry nodes containing expressions. Each TargetEntry has a field
34  * called 'resjunk'. If the value of this field is true then the
35  * corresponding attribute is a "junk" attribute.
36  *
37  * When we initialize a plan we call ExecInitJunkFilter to create a filter.
38  *
39  * We then execute the plan, treating the resjunk attributes like any others.
40  *
41  * Finally, when at the top level we get back a tuple, we can call
42  * ExecFindJunkAttribute/ExecGetJunkAttribute to retrieve the values of the
43  * junk attributes we are interested in, and ExecFilterJunk to remove all the
44  * junk attributes from a tuple. This new "clean" tuple is then printed,
45  * inserted, or updated.
46  *
47  *-------------------------------------------------------------------------
48  */
49 
50 /*
51  * ExecInitJunkFilter
52  *
53  * Initialize the Junk filter.
54  *
55  * The source targetlist is passed in. The output tuple descriptor is
56  * built from the non-junk tlist entries.
57  * An optional resultSlot can be passed as well; otherwise, we create one.
58  */
59 JunkFilter *
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 }
75 
76 /*
77  * ExecInitJunkFilterInsertion
78  *
79  * Initialize a JunkFilter for insertions into a table.
80  *
81  * Here, we are given the target "clean" tuple descriptor rather than
82  * inferring it from the targetlist. Although the target descriptor can
83  * contain deleted columns, that is not of concern here, since the targetlist
84  * should contain corresponding NULL constants (cf. ExecCheckPlanOutput).
85  * It is assumed that the caller has checked that the table's columns match up
86  * with the non-junk columns of the targetlist.
87  */
88 JunkFilter *
90  TupleDesc cleanTupType,
91  TupleTableSlot *slot)
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 }
149 
150 /*
151  * ExecInitJunkFilterConversion
152  *
153  * Initialize a JunkFilter for rowtype conversions.
154  *
155  * Here, we are given the target "clean" tuple descriptor rather than
156  * inferring it from the targetlist. The target descriptor can contain
157  * deleted columns. It is assumed that the caller has checked that the
158  * non-deleted columns match up with the non-junk columns of the targetlist.
159  */
160 JunkFilter *
162  TupleDesc cleanTupType,
163  TupleTableSlot *slot)
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 }
226 
227 /*
228  * ExecFindJunkAttribute
229  *
230  * Locate the specified junk attribute in the junk filter's targetlist,
231  * and return its resno. Returns InvalidAttrNumber if not found.
232  */
234 ExecFindJunkAttribute(JunkFilter *junkfilter, const char *attrName)
235 {
236  return ExecFindJunkAttributeInTlist(junkfilter->jf_targetList, attrName);
237 }
238 
239 /*
240  * ExecFindJunkAttributeInTlist
241  *
242  * Find a junk attribute given a subplan's targetlist (not necessarily
243  * part of a JunkFilter).
244  */
246 ExecFindJunkAttributeInTlist(List *targetlist, const char *attrName)
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 }
264 
265 /*
266  * ExecGetJunkAttribute
267  *
268  * Given a junk filter's input tuple (slot) and a junk attribute's number
269  * previously found by ExecFindJunkAttribute, extract & return the value and
270  * isNull flag of the attribute.
271  */
272 Datum
274  bool *isNull)
275 {
276  Assert(attno > 0);
277 
278  return slot_getattr(slot, attno, isNull);
279 }
280 
281 /*
282  * ExecFilterJunk
283  *
284  * Construct and return a slot with all the junk attributes removed.
285  */
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
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:310
TupleDesc ExecCleanTypeFromTL(List *targetList)
Definition: execTuples.c:1920
AttrNumber ExecFindJunkAttributeInTlist(List *targetlist, const char *attrName)
Definition: execJunk.c:246
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1208
AttrNumber ExecFindJunkAttribute(JunkFilter *junkfilter, const char *attrName)
Definition: execJunk.c:234
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
JunkFilter * ExecInitJunkFilterConversion(List *targetList, TupleDesc cleanTupType, TupleTableSlot *slot)
Definition: execJunk.c:161
const TupleTableSlotOps TTSOpsVirtual
Definition: execTuples.c:83
JunkFilter * ExecInitJunkFilterInsertion(List *targetList, TupleDesc cleanTupType, TupleTableSlot *slot)
Definition: execJunk.c:89
List * jf_targetList
Definition: execnodes.h:368
TupleTableSlot * jf_resultSlot
Definition: execnodes.h:371
Datum * tts_values
Definition: tuptable.h:126
char * resname
Definition: primnodes.h:1412
bool resjunk
Definition: primnodes.h:1417
static void slot_getallattrs(TupleTableSlot *slot)
Definition: tuptable.h:354
bool * tts_isnull
Definition: tuptable.h:128
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
uintptr_t Datum
Definition: postgres.h:367
TupleTableSlot * ExecFilterJunk(JunkFilter *junkfilter, TupleTableSlot *slot)
Definition: execJunk.c:287
void ExecSetSlotDescriptor(TupleTableSlot *slot, TupleDesc tupdesc)
Definition: execTuples.c:1259
static Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull)
Definition: tuptable.h:381
#define makeNode(_type_)
Definition: nodes.h:576
#define Assert(condition)
Definition: c.h:746
#define lfirst(lc)
Definition: pg_list.h:169
#define InvalidAttrNumber
Definition: attnum.h:23
static Datum values[MAXATTR]
Definition: bootstrap.c:165
void * palloc(Size size)
Definition: mcxt.c:950
Datum ExecGetJunkAttribute(TupleTableSlot *slot, AttrNumber attno, bool *isNull)
Definition: execJunk.c:273
int i
Definition: pg_list.h:50
int16 AttrNumber
Definition: attnum.h:21
JunkFilter * ExecInitJunkFilter(List *targetList, TupleTableSlot *slot)
Definition: execJunk.c:60
TupleTableSlot * ExecStoreVirtualTuple(TupleTableSlot *slot)
Definition: execTuples.c:1522