1 /*-------------------------------------------------------------------------
4 * Routines to handle group nodes (used for queries with GROUP BY clause).
6 * Portions Copyright (c) 1996-2007, 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 * $PostgreSQL: pgsql/src/backend/executor/nodeGroup.c,v 1.66 2007/01/05 22:19:28 momjian Exp $
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 * The ScanTupleSlot holds the (copied) first tuple of each group.
55 firsttupleslot = node->ss.ss_ScanTupleSlot;
58 * We need not call ResetExprContext here because execTuplesMatch will
59 * reset the per-tuple memory context once per input tuple.
63 * If first time through, acquire first input tuple and determine whether
64 * to return it or not.
66 if (TupIsNull(firsttupleslot))
68 outerslot = ExecProcNode(outerPlanState(node));
69 if (TupIsNull(outerslot))
71 /* empty input, so return nothing */
72 node->grp_done = TRUE;
75 /* Copy tuple, set up as input for qual test and projection */
76 ExecCopySlot(firsttupleslot, outerslot);
77 econtext->ecxt_scantuple = firsttupleslot;
80 * Check the qual (HAVING clause); if the group does not match, ignore
81 * it and fall into scan loop.
83 if (ExecQual(node->ss.ps.qual, econtext, false))
86 * Form and return a projection tuple using the first input tuple.
88 return ExecProject(node->ss.ps.ps_ProjInfo, NULL);
93 * This loop iterates once per input tuple group. At the head of the
94 * loop, we have finished processing the first tuple of the group and now
95 * need to scan over all the other group members.
100 * Scan over all remaining tuples that belong to this group
104 outerslot = ExecProcNode(outerPlanState(node));
105 if (TupIsNull(outerslot))
107 /* no more groups, so we're done */
108 node->grp_done = TRUE;
113 * Compare with first tuple and see if this tuple is of the same
114 * group. If so, ignore it and keep scanning.
116 if (!execTuplesMatch(firsttupleslot, outerslot,
119 econtext->ecxt_per_tuple_memory))
124 * We have the first tuple of the next input group. See if we want to
127 /* Copy tuple, set up as input for qual test and projection */
128 ExecCopySlot(firsttupleslot, outerslot);
129 econtext->ecxt_scantuple = firsttupleslot;
132 * Check the qual (HAVING clause); if the group does not match, ignore
133 * it and loop back to scan the rest of the group.
135 if (ExecQual(node->ss.ps.qual, econtext, false))
138 * Form and return a projection tuple using the first input tuple.
140 return ExecProject(node->ss.ps.ps_ProjInfo, NULL);
151 * Creates the run-time information for the group node produced by the
152 * planner and initializes its outer subtree
156 ExecInitGroup(Group *node, EState *estate, int eflags)
158 GroupState *grpstate;
160 /* check for unsupported flags */
161 Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
164 * create state structure
166 grpstate = makeNode(GroupState);
167 grpstate->ss.ps.plan = (Plan *) node;
168 grpstate->ss.ps.state = estate;
169 grpstate->grp_done = FALSE;
172 * create expression context
174 ExecAssignExprContext(estate, &grpstate->ss.ps);
176 #define GROUP_NSLOTS 2
179 * tuple table initialization
181 ExecInitScanTupleSlot(estate, &grpstate->ss);
182 ExecInitResultTupleSlot(estate, &grpstate->ss.ps);
185 * initialize child expressions
187 grpstate->ss.ps.targetlist = (List *)
188 ExecInitExpr((Expr *) node->plan.targetlist,
189 (PlanState *) grpstate);
190 grpstate->ss.ps.qual = (List *)
191 ExecInitExpr((Expr *) node->plan.qual,
192 (PlanState *) grpstate);
195 * initialize child nodes
197 outerPlanState(grpstate) = ExecInitNode(outerPlan(node), estate, eflags);
200 * initialize tuple type.
202 ExecAssignScanTypeFromOuterPlan(&grpstate->ss);
205 * Initialize result tuple type and projection info.
207 ExecAssignResultTypeFromTL(&grpstate->ss.ps);
208 ExecAssignProjectionInfo(&grpstate->ss.ps);
211 * Precompute fmgr lookup data for inner loop
213 grpstate->eqfunctions =
214 execTuplesMatchPrepare(ExecGetScanType(&grpstate->ss),
222 ExecCountSlotsGroup(Group *node)
224 return ExecCountSlotsNode(outerPlan(node)) + GROUP_NSLOTS;
227 /* ------------------------
230 * -----------------------
233 ExecEndGroup(GroupState *node)
235 PlanState *outerPlan;
237 ExecFreeExprContext(&node->ss.ps);
239 /* clean up tuple table */
240 ExecClearTuple(node->ss.ss_ScanTupleSlot);
242 outerPlan = outerPlanState(node);
243 ExecEndNode(outerPlan);
247 ExecReScanGroup(GroupState *node, ExprContext *exprCtxt)
249 node->grp_done = FALSE;
250 /* must clear first tuple */
251 ExecClearTuple(node->ss.ss_ScanTupleSlot);
253 if (((PlanState *) node)->lefttree &&
254 ((PlanState *) node)->lefttree->chgParam == NULL)
255 ExecReScan(((PlanState *) node)->lefttree, exprCtxt);