PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
nodeGroup.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * nodeGroup.c
4  * Routines to handle group nodes (used for queries with GROUP BY clause).
5  *
6  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * DESCRIPTION
11  * The Group node is designed for handling queries with a GROUP BY clause.
12  * Its outer plan must deliver tuples that are sorted in the order
13  * specified by the grouping columns (ie. tuples from the same group are
14  * consecutive). That way, we just have to compare adjacent tuples to
15  * locate group boundaries.
16  *
17  * IDENTIFICATION
18  * src/backend/executor/nodeGroup.c
19  *
20  *-------------------------------------------------------------------------
21  */
22 
23 #include "postgres.h"
24 
25 #include "executor/executor.h"
26 #include "executor/nodeGroup.h"
27 
28 
29 /*
30  * ExecGroup -
31  *
32  * Return one tuple for each group of matching input tuples.
33  */
36 {
37  ExprContext *econtext;
38  int numCols;
39  AttrNumber *grpColIdx;
40  TupleTableSlot *firsttupleslot;
41  TupleTableSlot *outerslot;
42 
43  /*
44  * get state info from node
45  */
46  if (node->grp_done)
47  return NULL;
48  econtext = node->ss.ps.ps_ExprContext;
49  numCols = ((Group *) node->ss.ps.plan)->numCols;
50  grpColIdx = ((Group *) node->ss.ps.plan)->grpColIdx;
51 
52  /*
53  * The ScanTupleSlot holds the (copied) first tuple of each group.
54  */
55  firsttupleslot = node->ss.ss_ScanTupleSlot;
56 
57  /*
58  * We need not call ResetExprContext here because execTuplesMatch will
59  * reset the per-tuple memory context once per input tuple.
60  */
61 
62  /*
63  * If first time through, acquire first input tuple and determine whether
64  * to return it or not.
65  */
66  if (TupIsNull(firsttupleslot))
67  {
68  outerslot = ExecProcNode(outerPlanState(node));
69  if (TupIsNull(outerslot))
70  {
71  /* empty input, so return nothing */
72  node->grp_done = TRUE;
73  return NULL;
74  }
75  /* Copy tuple into firsttupleslot */
76  ExecCopySlot(firsttupleslot, outerslot);
77 
78  /*
79  * Set it up as input for qual test and projection. The expressions
80  * will access the input tuple as varno OUTER.
81  */
82  econtext->ecxt_outertuple = firsttupleslot;
83 
84  /*
85  * Check the qual (HAVING clause); if the group does not match, ignore
86  * it and fall into scan loop.
87  */
88  if (ExecQual(node->ss.ps.qual, econtext, false))
89  {
90  /*
91  * Form and return a projection tuple using the first input tuple.
92  */
93  return ExecProject(node->ss.ps.ps_ProjInfo);
94  }
95  else
96  InstrCountFiltered1(node, 1);
97  }
98 
99  /*
100  * This loop iterates once per input tuple group. At the head of the
101  * loop, we have finished processing the first tuple of the group and now
102  * need to scan over all the other group members.
103  */
104  for (;;)
105  {
106  /*
107  * Scan over all remaining tuples that belong to this group
108  */
109  for (;;)
110  {
111  outerslot = ExecProcNode(outerPlanState(node));
112  if (TupIsNull(outerslot))
113  {
114  /* no more groups, so we're done */
115  node->grp_done = TRUE;
116  return NULL;
117  }
118 
119  /*
120  * Compare with first tuple and see if this tuple is of the same
121  * group. If so, ignore it and keep scanning.
122  */
123  if (!execTuplesMatch(firsttupleslot, outerslot,
124  numCols, grpColIdx,
125  node->eqfunctions,
126  econtext->ecxt_per_tuple_memory))
127  break;
128  }
129 
130  /*
131  * We have the first tuple of the next input group. See if we want to
132  * return it.
133  */
134  /* Copy tuple, set up as input for qual test and projection */
135  ExecCopySlot(firsttupleslot, outerslot);
136  econtext->ecxt_outertuple = firsttupleslot;
137 
138  /*
139  * Check the qual (HAVING clause); if the group does not match, ignore
140  * it and loop back to scan the rest of the group.
141  */
142  if (ExecQual(node->ss.ps.qual, econtext, false))
143  {
144  /*
145  * Form and return a projection tuple using the first input tuple.
146  */
147  return ExecProject(node->ss.ps.ps_ProjInfo);
148  }
149  else
150  InstrCountFiltered1(node, 1);
151  }
152 }
153 
154 /* -----------------
155  * ExecInitGroup
156  *
157  * Creates the run-time information for the group node produced by the
158  * planner and initializes its outer subtree
159  * -----------------
160  */
161 GroupState *
162 ExecInitGroup(Group *node, EState *estate, int eflags)
163 {
164  GroupState *grpstate;
165 
166  /* check for unsupported flags */
167  Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
168 
169  /*
170  * create state structure
171  */
172  grpstate = makeNode(GroupState);
173  grpstate->ss.ps.plan = (Plan *) node;
174  grpstate->ss.ps.state = estate;
175  grpstate->grp_done = FALSE;
176 
177  /*
178  * create expression context
179  */
180  ExecAssignExprContext(estate, &grpstate->ss.ps);
181 
182  /*
183  * tuple table initialization
184  */
185  ExecInitScanTupleSlot(estate, &grpstate->ss);
186  ExecInitResultTupleSlot(estate, &grpstate->ss.ps);
187 
188  /*
189  * initialize child expressions
190  */
191  grpstate->ss.ps.targetlist = (List *)
192  ExecInitExpr((Expr *) node->plan.targetlist,
193  (PlanState *) grpstate);
194  grpstate->ss.ps.qual = (List *)
195  ExecInitExpr((Expr *) node->plan.qual,
196  (PlanState *) grpstate);
197 
198  /*
199  * initialize child nodes
200  */
201  outerPlanState(grpstate) = ExecInitNode(outerPlan(node), estate, eflags);
202 
203  /*
204  * initialize tuple type.
205  */
207 
208  /*
209  * Initialize result tuple type and projection info.
210  */
211  ExecAssignResultTypeFromTL(&grpstate->ss.ps);
212  ExecAssignProjectionInfo(&grpstate->ss.ps, NULL);
213 
214  /*
215  * Precompute fmgr lookup data for inner loop
216  */
217  grpstate->eqfunctions =
219  node->grpOperators);
220 
221  return grpstate;
222 }
223 
224 /* ------------------------
225  * ExecEndGroup(node)
226  *
227  * -----------------------
228  */
229 void
231 {
233 
234  ExecFreeExprContext(&node->ss.ps);
235 
236  /* clean up tuple table */
238 
239  outerPlan = outerPlanState(node);
240  ExecEndNode(outerPlan);
241 }
242 
243 void
245 {
247 
248  node->grp_done = FALSE;
249  /* must clear first tuple */
251 
252  /*
253  * if chgParam of subnode is not null then plan will be re-scanned by
254  * first ExecProcNode.
255  */
256  if (outerPlan->chgParam == NULL)
257  ExecReScan(outerPlan);
258 }
void ExecEndGroup(GroupState *node)
Definition: nodeGroup.c:230
List * qual
Definition: plannodes.h:130
TupleTableSlot * ExecProject(ProjectionInfo *projInfo)
Definition: execQual.c:5214
TupleTableSlot * ExecProcNode(PlanState *node)
Definition: execProcnode.c:380
void ExecReScanGroup(GroupState *node)
Definition: nodeGroup.c:244
void ExecInitScanTupleSlot(EState *estate, ScanState *scanstate)
Definition: execTuples.c:842
ProjectionInfo * ps_ProjInfo
Definition: execnodes.h:1079
TupleTableSlot * ExecGroup(GroupState *node)
Definition: nodeGroup.c:35
GroupState * ExecInitGroup(Group *node, EState *estate, int eflags)
Definition: nodeGroup.c:162
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:624
ExprContext * ps_ExprContext
Definition: execnodes.h:1078
MemoryContext ecxt_per_tuple_memory
Definition: execnodes.h:134
void ExecReScan(PlanState *node)
Definition: execAmi.c:73
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
List * qual
Definition: execnodes.h:1062
bool execTuplesMatch(TupleTableSlot *slot1, TupleTableSlot *slot2, int numCols, AttrNumber *matchColIdx, FmgrInfo *eqfunctions, MemoryContext evalContext)
Definition: execGrouping.c:69
List * targetlist
Definition: execnodes.h:1061
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1291
EState * state
Definition: execnodes.h:1049
bool grp_done
Definition: execnodes.h:1844
void ExecFreeExprContext(PlanState *planstate)
Definition: execUtils.c:685
void ExecAssignResultTypeFromTL(PlanState *planstate)
Definition: execUtils.c:430
ScanState ss
Definition: execnodes.h:1842
PlanState ps
Definition: execnodes.h:1288
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition: execQual.c:4266
#define FALSE
Definition: c.h:218
void ExecInitResultTupleSlot(EState *estate, PlanState *planstate)
Definition: execTuples.c:832
#define EXEC_FLAG_BACKWARD
Definition: executor.h:60
#define outerPlanState(node)
Definition: execnodes.h:1090
void ExecAssignProjectionInfo(PlanState *planstate, TupleDesc inputDesc)
Definition: execUtils.c:657
bool ExecQual(List *qual, ExprContext *econtext, bool resultForNull)
Definition: execQual.c:5055
#define TupIsNull(slot)
Definition: tuptable.h:138
int numCols
Definition: plannodes.h:711
#define InstrCountFiltered1(node, delta)
Definition: execnodes.h:1093
Bitmapset * chgParam
Definition: execnodes.h:1072
#define outerPlan(node)
Definition: plannodes.h:159
Plan * plan
Definition: execnodes.h:1047
TupleTableSlot * ExecCopySlot(TupleTableSlot *dstslot, TupleTableSlot *srcslot)
Definition: execTuples.c:795
#define makeNode(_type_)
Definition: nodes.h:557
TupleTableSlot * ecxt_outertuple
Definition: execnodes.h:130
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define EXEC_FLAG_MARK
Definition: executor.h:61
Plan plan
Definition: plannodes.h:710
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:408
List * targetlist
Definition: plannodes.h:129
void ExecAssignScanTypeFromOuterPlan(ScanState *scanstate)
Definition: execUtils.c:721
FmgrInfo * eqfunctions
Definition: execnodes.h:1843
#define TRUE
Definition: c.h:214
FmgrInfo * execTuplesMatchPrepare(int numCols, Oid *eqOperators)
Definition: execGrouping.c:204
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:137
Definition: pg_list.h:45
int16 AttrNumber
Definition: attnum.h:21
Oid * grpOperators
Definition: plannodes.h:713