1 /*-------------------------------------------------------------------------
4 * Routines to handle group nodes (used for queries with GROUP BY clause).
6 * Portions Copyright (c) 1996-2018, 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"
27 #include "miscadmin.h"
28 #include "utils/memutils.h"
34 * Return one tuple for each group of matching input tuples.
36 static TupleTableSlot *
37 ExecGroup(PlanState *pstate)
39 GroupState *node = castNode(GroupState, pstate);
40 ExprContext *econtext;
41 TupleTableSlot *firsttupleslot;
42 TupleTableSlot *outerslot;
44 CHECK_FOR_INTERRUPTS();
47 * get state info from node
51 econtext = node->ss.ps.ps_ExprContext;
54 * The ScanTupleSlot holds the (copied) first tuple of each group.
56 firsttupleslot = node->ss.ss_ScanTupleSlot;
59 * We need not call ResetExprContext here because ExecQualAndReset() will
60 * reset the per-tuple memory context once per input tuple.
64 * If first time through, acquire first input tuple and determine whether
65 * to return it or not.
67 if (TupIsNull(firsttupleslot))
69 outerslot = ExecProcNode(outerPlanState(node));
70 if (TupIsNull(outerslot))
72 /* empty input, so return nothing */
73 node->grp_done = true;
76 /* Copy tuple into firsttupleslot */
77 ExecCopySlot(firsttupleslot, outerslot);
80 * Set it up as input for qual test and projection. The expressions
81 * will access the input tuple as varno OUTER.
83 econtext->ecxt_outertuple = firsttupleslot;
86 * Check the qual (HAVING clause); if the group does not match, ignore
87 * it and fall into scan loop.
89 if (ExecQual(node->ss.ps.qual, econtext))
92 * Form and return a projection tuple using the first input tuple.
94 return ExecProject(node->ss.ps.ps_ProjInfo);
97 InstrCountFiltered1(node, 1);
101 * This loop iterates once per input tuple group. At the head of the
102 * loop, we have finished processing the first tuple of the group and now
103 * need to scan over all the other group members.
108 * Scan over all remaining tuples that belong to this group
112 outerslot = ExecProcNode(outerPlanState(node));
113 if (TupIsNull(outerslot))
115 /* no more groups, so we're done */
116 node->grp_done = true;
121 * Compare with first tuple and see if this tuple is of the same
122 * group. If so, ignore it and keep scanning.
124 econtext->ecxt_innertuple = firsttupleslot;
125 econtext->ecxt_outertuple = outerslot;
126 if (!ExecQualAndReset(node->eqfunction, econtext))
131 * We have the first tuple of the next input group. See if we want to
134 /* Copy tuple, set up as input for qual test and projection */
135 ExecCopySlot(firsttupleslot, outerslot);
136 econtext->ecxt_outertuple = firsttupleslot;
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.
142 if (ExecQual(node->ss.ps.qual, econtext))
145 * Form and return a projection tuple using the first input tuple.
147 return ExecProject(node->ss.ps.ps_ProjInfo);
150 InstrCountFiltered1(node, 1);
157 * Creates the run-time information for the group node produced by the
158 * planner and initializes its outer subtree
162 ExecInitGroup(Group *node, EState *estate, int eflags)
164 GroupState *grpstate;
166 /* check for unsupported flags */
167 Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
170 * create state structure
172 grpstate = makeNode(GroupState);
173 grpstate->ss.ps.plan = (Plan *) node;
174 grpstate->ss.ps.state = estate;
175 grpstate->ss.ps.ExecProcNode = ExecGroup;
176 grpstate->grp_done = false;
179 * create expression context
181 ExecAssignExprContext(estate, &grpstate->ss.ps);
184 * initialize child nodes
186 outerPlanState(grpstate) = ExecInitNode(outerPlan(node), estate, eflags);
189 * Initialize scan slot and type.
191 ExecCreateScanSlotFromOuterPlan(estate, &grpstate->ss);
194 * Initialize result slot, type and projection.
196 ExecInitResultTupleSlotTL(estate, &grpstate->ss.ps);
197 ExecAssignProjectionInfo(&grpstate->ss.ps, NULL);
200 * initialize child expressions
202 grpstate->ss.ps.qual =
203 ExecInitQual(node->plan.qual, (PlanState *) grpstate);
206 * Precompute fmgr lookup data for inner loop
208 grpstate->eqfunction =
209 execTuplesMatchPrepare(ExecGetResultType(outerPlanState(grpstate)),
218 /* ------------------------
221 * -----------------------
224 ExecEndGroup(GroupState *node)
226 PlanState *outerPlan;
228 ExecFreeExprContext(&node->ss.ps);
230 /* clean up tuple table */
231 ExecClearTuple(node->ss.ss_ScanTupleSlot);
233 outerPlan = outerPlanState(node);
234 ExecEndNode(outerPlan);
238 ExecReScanGroup(GroupState *node)
240 PlanState *outerPlan = outerPlanState(node);
242 node->grp_done = false;
243 /* must clear first tuple */
244 ExecClearTuple(node->ss.ss_ScanTupleSlot);
247 * if chgParam of subnode is not null then plan will be re-scanned by
248 * first ExecProcNode.
250 if (outerPlan->chgParam == NULL)
251 ExecReScan(outerPlan);