PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
nodeGroup.c File Reference
#include "postgres.h"
#include "executor/executor.h"
#include "executor/nodeGroup.h"
Include dependency graph for nodeGroup.c:

Go to the source code of this file.

Functions

TupleTableSlotExecGroup (GroupState *node)
 
GroupStateExecInitGroup (Group *node, EState *estate, int eflags)
 
void ExecEndGroup (GroupState *node)
 
void ExecReScanGroup (GroupState *node)
 

Function Documentation

void ExecEndGroup ( GroupState node)

Definition at line 226 of file nodeGroup.c.

References ExecClearTuple(), ExecEndNode(), ExecFreeExprContext(), outerPlan, outerPlanState, ScanState::ps, GroupState::ss, and ScanState::ss_ScanTupleSlot.

Referenced by ExecEndNode().

227 {
229 
230  ExecFreeExprContext(&node->ss.ps);
231 
232  /* clean up tuple table */
234 
235  outerPlan = outerPlanState(node);
236  ExecEndNode(outerPlan);
237 }
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:654
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1050
void ExecFreeExprContext(PlanState *planstate)
Definition: execUtils.c:516
ScanState ss
Definition: execnodes.h:1705
PlanState ps
Definition: execnodes.h:1047
#define outerPlanState(node)
Definition: execnodes.h:845
#define outerPlan(node)
Definition: plannodes.h:164
TupleTableSlot* ExecGroup ( GroupState node)

Definition at line 35 of file nodeGroup.c.

References ExprContext::ecxt_outertuple, ExprContext::ecxt_per_tuple_memory, GroupState::eqfunctions, ExecCopySlot(), ExecProcNode(), ExecProject(), ExecQual(), execTuplesMatch(), GroupState::grp_done, InstrCountFiltered1, NULL, outerPlanState, PlanState::plan, ScanState::ps, PlanState::ps_ExprContext, PlanState::ps_ProjInfo, PlanState::qual, GroupState::ss, ScanState::ss_ScanTupleSlot, TRUE, and TupIsNull.

Referenced by ExecProcNode().

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 }
TupleTableSlot * ExecProcNode(PlanState *node)
Definition: execProcnode.c:398
ProjectionInfo * ps_ProjInfo
Definition: execnodes.h:834
ExprContext * ps_ExprContext
Definition: execnodes.h:833
MemoryContext ecxt_per_tuple_memory
Definition: execnodes.h:203
bool execTuplesMatch(TupleTableSlot *slot1, TupleTableSlot *slot2, int numCols, AttrNumber *matchColIdx, FmgrInfo *eqfunctions, MemoryContext evalContext)
Definition: execGrouping.c:69
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1050
static bool ExecQual(ExprState *state, ExprContext *econtext)
Definition: executor.h:345
bool grp_done
Definition: execnodes.h:1707
ScanState ss
Definition: execnodes.h:1705
PlanState ps
Definition: execnodes.h:1047
#define outerPlanState(node)
Definition: execnodes.h:845
#define TupIsNull(slot)
Definition: tuptable.h:138
#define InstrCountFiltered1(node, delta)
Definition: execnodes.h:848
Plan * plan
Definition: execnodes.h:803
TupleTableSlot * ExecCopySlot(TupleTableSlot *dstslot, TupleTableSlot *srcslot)
Definition: execTuples.c:795
TupleTableSlot * ecxt_outertuple
Definition: execnodes.h:199
#define NULL
Definition: c.h:229
ExprState * qual
Definition: execnodes.h:817
FmgrInfo * eqfunctions
Definition: execnodes.h:1706
#define TRUE
Definition: c.h:217
int16 AttrNumber
Definition: attnum.h:21
static TupleTableSlot * ExecProject(ProjectionInfo *projInfo)
Definition: executor.h:308
GroupState* ExecInitGroup ( Group node,
EState estate,
int  eflags 
)

Definition at line 162 of file nodeGroup.c.

References Assert, GroupState::eqfunctions, EXEC_FLAG_BACKWARD, EXEC_FLAG_MARK, ExecAssignExprContext(), ExecAssignProjectionInfo(), ExecAssignResultTypeFromTL(), ExecAssignScanTypeFromOuterPlan(), ExecInitNode(), ExecInitQual(), ExecInitResultTupleSlot(), ExecInitScanTupleSlot(), execTuplesMatchPrepare(), FALSE, GroupState::grp_done, Group::grpOperators, makeNode, NULL, Group::numCols, outerPlan, outerPlanState, Group::plan, PlanState::plan, ScanState::ps, Plan::qual, PlanState::qual, GroupState::ss, and PlanState::state.

Referenced by ExecInitNode().

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 }
List * qual
Definition: plannodes.h:135
void ExecInitScanTupleSlot(EState *estate, ScanState *scanstate)
Definition: execTuples.c:842
EState * state
Definition: execnodes.h:805
bool grp_done
Definition: execnodes.h:1707
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition: execExpr.c:160
void ExecAssignResultTypeFromTL(PlanState *planstate)
Definition: execUtils.c:440
ScanState ss
Definition: execnodes.h:1705
PlanState ps
Definition: execnodes.h:1047
#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:845
void ExecAssignProjectionInfo(PlanState *planstate, TupleDesc inputDesc)
Definition: execUtils.c:487
int numCols
Definition: plannodes.h:752
#define outerPlan(node)
Definition: plannodes.h:164
Plan * plan
Definition: execnodes.h:803
#define makeNode(_type_)
Definition: nodes.h:557
#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:751
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:418
ExprState * qual
Definition: execnodes.h:817
void ExecAssignScanTypeFromOuterPlan(ScanState *scanstate)
Definition: execUtils.c:552
FmgrInfo * eqfunctions
Definition: execnodes.h:1706
FmgrInfo * execTuplesMatchPrepare(int numCols, Oid *eqOperators)
Definition: execGrouping.c:204
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:140
Oid * grpOperators
Definition: plannodes.h:754
void ExecReScanGroup ( GroupState node)

Definition at line 240 of file nodeGroup.c.

References PlanState::chgParam, ExecClearTuple(), ExecReScan(), FALSE, GroupState::grp_done, NULL, outerPlan, outerPlanState, GroupState::ss, and ScanState::ss_ScanTupleSlot.

Referenced by ExecReScan().

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 ExecReScan(PlanState *node)
Definition: execAmi.c:75
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1050
bool grp_done
Definition: execnodes.h:1707
ScanState ss
Definition: execnodes.h:1705
#define FALSE
Definition: c.h:221
#define outerPlanState(node)
Definition: execnodes.h:845
Bitmapset * chgParam
Definition: execnodes.h:827
#define outerPlan(node)
Definition: plannodes.h:164
#define NULL
Definition: c.h:229