1 /*-------------------------------------------------------------------------
4 * Routines to handle group nodes (used for queries with GROUP BY clause).
6 * Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
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.
18 * src/backend/executor/nodeGroup.c
20 *-------------------------------------------------------------------------
25 #include "executor/executor.h"
26 #include "executor/nodeGroup.h"
32 * Return one tuple for each group of matching input tuples.
35 ExecGroup(GroupState *node)
37 ExprContext *econtext;
39 AttrNumber *grpColIdx;
40 TupleTableSlot *firsttupleslot;
41 TupleTableSlot *outerslot;
44 * get state info from node
48 econtext = node->ss.ps.ps_ExprContext;
49 numCols = ((Group *) node->ss.ps.plan)->numCols;
50 grpColIdx = ((Group *) node->ss.ps.plan)->grpColIdx;
53 * Check to see if we're still projecting out tuples from a previous group
54 * tuple (because there is a function-returning-set in the projection
55 * expressions). If so, try to project another one.
57 if (node->ss.ps.ps_TupFromTlist)
59 TupleTableSlot *result;
62 result = ExecProject(node->ss.ps.ps_ProjInfo, &isDone);
63 if (isDone == ExprMultipleResult)
65 /* Done with that source tuple... */
66 node->ss.ps.ps_TupFromTlist = false;
70 * The ScanTupleSlot holds the (copied) first tuple of each group.
72 firsttupleslot = node->ss.ss_ScanTupleSlot;
75 * We need not call ResetExprContext here because execTuplesMatch will
76 * reset the per-tuple memory context once per input tuple.
80 * If first time through, acquire first input tuple and determine whether
81 * to return it or not.
83 if (TupIsNull(firsttupleslot))
85 outerslot = ExecProcNode(outerPlanState(node));
86 if (TupIsNull(outerslot))
88 /* empty input, so return nothing */
89 node->grp_done = TRUE;
92 /* Copy tuple into firsttupleslot */
93 ExecCopySlot(firsttupleslot, outerslot);
96 * Set it up as input for qual test and projection. The expressions
97 * will access the input tuple as varno OUTER.
99 econtext->ecxt_outertuple = firsttupleslot;
102 * Check the qual (HAVING clause); if the group does not match, ignore
103 * it and fall into scan loop.
105 if (ExecQual(node->ss.ps.qual, econtext, false))
108 * Form and return a projection tuple using the first input tuple.
110 TupleTableSlot *result;
113 result = ExecProject(node->ss.ps.ps_ProjInfo, &isDone);
115 if (isDone != ExprEndResult)
117 node->ss.ps.ps_TupFromTlist = (isDone == ExprMultipleResult);
122 InstrCountFiltered1(node, 1);
126 * This loop iterates once per input tuple group. At the head of the
127 * loop, we have finished processing the first tuple of the group and now
128 * need to scan over all the other group members.
133 * Scan over all remaining tuples that belong to this group
137 outerslot = ExecProcNode(outerPlanState(node));
138 if (TupIsNull(outerslot))
140 /* no more groups, so we're done */
141 node->grp_done = TRUE;
146 * Compare with first tuple and see if this tuple is of the same
147 * group. If so, ignore it and keep scanning.
149 if (!execTuplesMatch(firsttupleslot, outerslot,
152 econtext->ecxt_per_tuple_memory))
157 * We have the first tuple of the next input group. See if we want to
160 /* Copy tuple, set up as input for qual test and projection */
161 ExecCopySlot(firsttupleslot, outerslot);
162 econtext->ecxt_outertuple = firsttupleslot;
165 * Check the qual (HAVING clause); if the group does not match, ignore
166 * it and loop back to scan the rest of the group.
168 if (ExecQual(node->ss.ps.qual, econtext, false))
171 * Form and return a projection tuple using the first input tuple.
173 TupleTableSlot *result;
176 result = ExecProject(node->ss.ps.ps_ProjInfo, &isDone);
178 if (isDone != ExprEndResult)
180 node->ss.ps.ps_TupFromTlist = (isDone == ExprMultipleResult);
185 InstrCountFiltered1(node, 1);
192 * Creates the run-time information for the group node produced by the
193 * planner and initializes its outer subtree
197 ExecInitGroup(Group *node, EState *estate, int eflags)
199 GroupState *grpstate;
201 /* check for unsupported flags */
202 Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
205 * create state structure
207 grpstate = makeNode(GroupState);
208 grpstate->ss.ps.plan = (Plan *) node;
209 grpstate->ss.ps.state = estate;
210 grpstate->grp_done = FALSE;
213 * create expression context
215 ExecAssignExprContext(estate, &grpstate->ss.ps);
218 * tuple table initialization
220 ExecInitScanTupleSlot(estate, &grpstate->ss);
221 ExecInitResultTupleSlot(estate, &grpstate->ss.ps);
224 * initialize child expressions
226 grpstate->ss.ps.targetlist = (List *)
227 ExecInitExpr((Expr *) node->plan.targetlist,
228 (PlanState *) grpstate);
229 grpstate->ss.ps.qual = (List *)
230 ExecInitExpr((Expr *) node->plan.qual,
231 (PlanState *) grpstate);
234 * initialize child nodes
236 outerPlanState(grpstate) = ExecInitNode(outerPlan(node), estate, eflags);
239 * initialize tuple type.
241 ExecAssignScanTypeFromOuterPlan(&grpstate->ss);
244 * Initialize result tuple type and projection info.
246 ExecAssignResultTypeFromTL(&grpstate->ss.ps);
247 ExecAssignProjectionInfo(&grpstate->ss.ps, NULL);
249 grpstate->ss.ps.ps_TupFromTlist = false;
252 * Precompute fmgr lookup data for inner loop
254 grpstate->eqfunctions =
255 execTuplesMatchPrepare(node->numCols,
261 /* ------------------------
264 * -----------------------
267 ExecEndGroup(GroupState *node)
269 PlanState *outerPlan;
271 ExecFreeExprContext(&node->ss.ps);
273 /* clean up tuple table */
274 ExecClearTuple(node->ss.ss_ScanTupleSlot);
276 outerPlan = outerPlanState(node);
277 ExecEndNode(outerPlan);
281 ExecReScanGroup(GroupState *node)
283 node->grp_done = FALSE;
284 node->ss.ps.ps_TupFromTlist = false;
285 /* must clear first tuple */
286 ExecClearTuple(node->ss.ss_ScanTupleSlot);
289 * if chgParam of subnode is not null then plan will be re-scanned by
290 * first ExecProcNode.
292 if (node->ss.ps.lefttree &&
293 node->ss.ps.lefttree->chgParam == NULL)
294 ExecReScan(node->ss.ps.lefttree);