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))
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))
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.qual =
192  ExecInitQual(node->plan.qual, (PlanState *) grpstate);
193 
194  /*
195  * initialize child nodes
196  */
197  outerPlanState(grpstate) = ExecInitNode(outerPlan(node), estate, eflags);
198 
199  /*
200  * initialize tuple type.
201  */
203 
204  /*
205  * Initialize result tuple type and projection info.
206  */
207  ExecAssignResultTypeFromTL(&grpstate->ss.ps);
208  ExecAssignProjectionInfo(&grpstate->ss.ps, NULL);
209 
210  /*
211  * Precompute fmgr lookup data for inner loop
212  */
213  grpstate->eqfunctions =
215  node->grpOperators);
216 
217  return grpstate;
218 }
219 
220 /* ------------------------
221  * ExecEndGroup(node)
222  *
223  * -----------------------
224  */
225 void
227 {
229 
230  ExecFreeExprContext(&node->ss.ps);
231 
232  /* clean up tuple table */
234 
235  outerPlan = outerPlanState(node);
236  ExecEndNode(outerPlan);
237 }
238 
239 void
241 {
243 
244  node->grp_done = FALSE;
245  /* must clear first tuple */
247 
248  /*
249  * if chgParam of subnode is not null then plan will be re-scanned by
250  * first ExecProcNode.
251  */
252  if (outerPlan->chgParam == NULL)
253  ExecReScan(outerPlan);
254 }
void ExecEndGroup(GroupState *node)
Definition: nodeGroup.c:226
List * qual
Definition: plannodes.h:145
TupleTableSlot * ExecProcNode(PlanState *node)
Definition: execProcnode.c:398
void ExecReScanGroup(GroupState *node)
Definition: nodeGroup.c:240
void ExecInitScanTupleSlot(EState *estate, ScanState *scanstate)
Definition: execTuples.c:842
ProjectionInfo * ps_ProjInfo
Definition: execnodes.h:863
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:654
ExprContext * ps_ExprContext
Definition: execnodes.h:862
MemoryContext ecxt_per_tuple_memory
Definition: execnodes.h:203
void ExecReScan(PlanState *node)
Definition: execAmi.c:75
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
bool execTuplesMatch(TupleTableSlot *slot1, TupleTableSlot *slot2, int numCols, AttrNumber *matchColIdx, FmgrInfo *eqfunctions, MemoryContext evalContext)
Definition: execGrouping.c:69
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1078
EState * state
Definition: execnodes.h:834
static bool ExecQual(ExprState *state, ExprContext *econtext)
Definition: executor.h:346
bool grp_done
Definition: execnodes.h:1734
void ExecFreeExprContext(PlanState *planstate)
Definition: execUtils.c:516
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition: execExpr.c:160
void ExecAssignResultTypeFromTL(PlanState *planstate)
Definition: execUtils.c:440
ScanState ss
Definition: execnodes.h:1732
PlanState ps
Definition: execnodes.h:1075
#define FALSE
Definition: c.h:221
void ExecInitResultTupleSlot(EState *estate, PlanState *planstate)
Definition: execTuples.c:832
#define EXEC_FLAG_BACKWARD
Definition: executor.h:60
#define outerPlanState(node)
Definition: execnodes.h:874
void ExecAssignProjectionInfo(PlanState *planstate, TupleDesc inputDesc)
Definition: execUtils.c:487
#define TupIsNull(slot)
Definition: tuptable.h:138
int numCols
Definition: plannodes.h:761
#define InstrCountFiltered1(node, delta)
Definition: execnodes.h:877
Bitmapset * chgParam
Definition: execnodes.h:856
#define outerPlan(node)
Definition: plannodes.h:174
Plan * plan
Definition: execnodes.h:832
TupleTableSlot * ExecCopySlot(TupleTableSlot *dstslot, TupleTableSlot *srcslot)
Definition: execTuples.c:795
#define makeNode(_type_)
Definition: nodes.h:557
TupleTableSlot * ecxt_outertuple
Definition: execnodes.h:199
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define EXEC_FLAG_MARK
Definition: executor.h:61
Plan plan
Definition: plannodes.h:760
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:418
ExprState * qual
Definition: execnodes.h:846
void ExecAssignScanTypeFromOuterPlan(ScanState *scanstate)
Definition: execUtils.c:552
FmgrInfo * eqfunctions
Definition: execnodes.h:1733
#define TRUE
Definition: c.h:217
FmgrInfo * execTuplesMatchPrepare(int numCols, Oid *eqOperators)
Definition: execGrouping.c:204
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:140
int16 AttrNumber
Definition: attnum.h:21
Oid * grpOperators
Definition: plannodes.h:763
static TupleTableSlot * ExecProject(ProjectionInfo *projInfo)
Definition: executor.h:309