PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
nodeHashjoin.h File Reference
#include "nodes/execnodes.h"
#include "storage/buffile.h"
Include dependency graph for nodeHashjoin.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

HashJoinStateExecInitHashJoin (HashJoin *node, EState *estate, int eflags)
 
TupleTableSlotExecHashJoin (HashJoinState *node)
 
void ExecEndHashJoin (HashJoinState *node)
 
void ExecReScanHashJoin (HashJoinState *node)
 
void ExecHashJoinSaveTuple (MinimalTuple tuple, uint32 hashvalue, BufFile **fileptr)
 

Function Documentation

void ExecEndHashJoin ( HashJoinState node)

Definition at line 549 of file nodeHashjoin.c.

References ExecClearTuple(), ExecEndNode(), ExecFreeExprContext(), ExecHashTableDestroy(), HashJoinState::hj_HashTable, HashJoinState::hj_HashTupleSlot, HashJoinState::hj_OuterTupleSlot, innerPlanState, HashJoinState::js, NULL, outerPlanState, JoinState::ps, and PlanState::ps_ResultTupleSlot.

Referenced by ExecEndNode().

550 {
551  /*
552  * Free hash table
553  */
554  if (node->hj_HashTable)
555  {
557  node->hj_HashTable = NULL;
558  }
559 
560  /*
561  * Free the exprcontext
562  */
563  ExecFreeExprContext(&node->js.ps);
564 
565  /*
566  * clean out the tuple table
567  */
571 
572  /*
573  * clean up subtrees
574  */
577 }
PlanState ps
Definition: execnodes.h:1678
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:624
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
TupleTableSlot * hj_OuterTupleSlot
Definition: execnodes.h:1787
void ExecFreeExprContext(PlanState *planstate)
Definition: execUtils.c:685
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:1077
#define outerPlanState(node)
Definition: execnodes.h:1090
#define NULL
Definition: c.h:226
TupleTableSlot * hj_HashTupleSlot
Definition: execnodes.h:1788
HashJoinTable hj_HashTable
Definition: execnodes.h:1782
#define innerPlanState(node)
Definition: execnodes.h:1089
JoinState js
Definition: execnodes.h:1777
void ExecHashTableDestroy(HashJoinTable hashtable)
Definition: nodeHash.c:563
TupleTableSlot* ExecHashJoin ( HashJoinState node)

Definition at line 62 of file nodeHashjoin.c.

References Assert, CHECK_FOR_INTERRUPTS, HashJoinTableData::curbatch, ExprContext::ecxt_innertuple, ExprContext::ecxt_outertuple, elog, ERROR, ExecFetchSlotMinimalTuple(), ExecHashGetBucketAndBatch(), ExecHashGetSkewBucket(), ExecHashJoinNewBatch(), ExecHashJoinOuterGetTuple(), ExecHashJoinSaveTuple(), ExecHashTableCreate(), ExecPrepHashTableForUnmatched(), ExecProcNode(), ExecProject(), ExecQual(), ExecScanHashBucket(), ExecScanHashTableForUnmatched(), HashState::hashtable, HeapTupleHeaderSetMatch, HJ_BUILD_HASHTABLE, HashJoinState::hj_CurBucketNo, HashJoinState::hj_CurHashValue, HashJoinState::hj_CurSkewBucketNo, HashJoinState::hj_CurTuple, HJ_FILL_INNER, HJ_FILL_INNER_TUPLES, HJ_FILL_OUTER, HJ_FILL_OUTER_TUPLE, HashJoinState::hj_FirstOuterTupleSlot, HashJoinState::hj_HashOperators, HashJoinState::hj_HashTable, HashJoinState::hj_JoinState, HashJoinState::hj_MatchedOuter, HJ_NEED_NEW_BATCH, HJ_NEED_NEW_OUTER, HashJoinState::hj_NullInnerTupleSlot, HashJoinState::hj_NullOuterTupleSlot, HashJoinState::hj_OuterNotEmpty, HJ_SCAN_BUCKET, HJTUPLE_MINTUPLE, innerPlanState, InstrCountFiltered1, InstrCountFiltered2, INVALID_SKEW_BUCKET_NO, JOIN_ANTI, JOIN_SEMI, JoinState::joinqual, JoinState::jointype, HashJoinState::js, MultiExecProcNode(), HashJoinTableData::nbatch, HashJoinTableData::nbatch_outstart, NIL, NULL, HashJoinTableData::outerBatchFile, outerPlanState, PlanState::plan, JoinState::ps, HashState::ps, PlanState::ps_ExprContext, PlanState::ps_ProjInfo, PlanState::qual, ResetExprContext, Plan::startup_cost, Plan::total_cost, HashJoinTableData::totalTuples, and TupIsNull.

Referenced by ExecProcNode().

63 {
64  PlanState *outerNode;
65  HashState *hashNode;
66  List *joinqual;
67  List *otherqual;
68  ExprContext *econtext;
69  HashJoinTable hashtable;
70  TupleTableSlot *outerTupleSlot;
71  uint32 hashvalue;
72  int batchno;
73 
74  /*
75  * get information from HashJoin node
76  */
77  joinqual = node->js.joinqual;
78  otherqual = node->js.ps.qual;
79  hashNode = (HashState *) innerPlanState(node);
80  outerNode = outerPlanState(node);
81  hashtable = node->hj_HashTable;
82  econtext = node->js.ps.ps_ExprContext;
83 
84  /*
85  * Reset per-tuple memory context to free any expression evaluation
86  * storage allocated in the previous tuple cycle.
87  */
88  ResetExprContext(econtext);
89 
90  /*
91  * run the hash join state machine
92  */
93  for (;;)
94  {
95  switch (node->hj_JoinState)
96  {
97  case HJ_BUILD_HASHTABLE:
98 
99  /*
100  * First time through: build hash table for inner relation.
101  */
102  Assert(hashtable == NULL);
103 
104  /*
105  * If the outer relation is completely empty, and it's not
106  * right/full join, we can quit without building the hash
107  * table. However, for an inner join it is only a win to
108  * check this when the outer relation's startup cost is less
109  * than the projected cost of building the hash table.
110  * Otherwise it's best to build the hash table first and see
111  * if the inner relation is empty. (When it's a left join, we
112  * should always make this check, since we aren't going to be
113  * able to skip the join on the strength of an empty inner
114  * relation anyway.)
115  *
116  * If we are rescanning the join, we make use of information
117  * gained on the previous scan: don't bother to try the
118  * prefetch if the previous scan found the outer relation
119  * nonempty. This is not 100% reliable since with new
120  * parameters the outer relation might yield different
121  * results, but it's a good heuristic.
122  *
123  * The only way to make the check is to try to fetch a tuple
124  * from the outer plan node. If we succeed, we have to stash
125  * it away for later consumption by ExecHashJoinOuterGetTuple.
126  */
127  if (HJ_FILL_INNER(node))
128  {
129  /* no chance to not build the hash table */
131  }
132  else if (HJ_FILL_OUTER(node) ||
133  (outerNode->plan->startup_cost < hashNode->ps.plan->total_cost &&
134  !node->hj_OuterNotEmpty))
135  {
136  node->hj_FirstOuterTupleSlot = ExecProcNode(outerNode);
138  {
139  node->hj_OuterNotEmpty = false;
140  return NULL;
141  }
142  else
143  node->hj_OuterNotEmpty = true;
144  }
145  else
147 
148  /*
149  * create the hash table
150  */
151  hashtable = ExecHashTableCreate((Hash *) hashNode->ps.plan,
152  node->hj_HashOperators,
153  HJ_FILL_INNER(node));
154  node->hj_HashTable = hashtable;
155 
156  /*
157  * execute the Hash node, to build the hash table
158  */
159  hashNode->hashtable = hashtable;
160  (void) MultiExecProcNode((PlanState *) hashNode);
161 
162  /*
163  * If the inner relation is completely empty, and we're not
164  * doing a left outer join, we can quit without scanning the
165  * outer relation.
166  */
167  if (hashtable->totalTuples == 0 && !HJ_FILL_OUTER(node))
168  return NULL;
169 
170  /*
171  * need to remember whether nbatch has increased since we
172  * began scanning the outer relation
173  */
174  hashtable->nbatch_outstart = hashtable->nbatch;
175 
176  /*
177  * Reset OuterNotEmpty for scan. (It's OK if we fetched a
178  * tuple above, because ExecHashJoinOuterGetTuple will
179  * immediately set it again.)
180  */
181  node->hj_OuterNotEmpty = false;
182 
184 
185  /* FALL THRU */
186 
187  case HJ_NEED_NEW_OUTER:
188 
189  /*
190  * We don't have an outer tuple, try to get the next one
191  */
192  outerTupleSlot = ExecHashJoinOuterGetTuple(outerNode,
193  node,
194  &hashvalue);
195  if (TupIsNull(outerTupleSlot))
196  {
197  /* end of batch, or maybe whole join */
198  if (HJ_FILL_INNER(node))
199  {
200  /* set up to scan for unmatched inner tuples */
203  }
204  else
206  continue;
207  }
208 
209  econtext->ecxt_outertuple = outerTupleSlot;
210  node->hj_MatchedOuter = false;
211 
212  /*
213  * Find the corresponding bucket for this tuple in the main
214  * hash table or skew hash table.
215  */
216  node->hj_CurHashValue = hashvalue;
217  ExecHashGetBucketAndBatch(hashtable, hashvalue,
218  &node->hj_CurBucketNo, &batchno);
219  node->hj_CurSkewBucketNo = ExecHashGetSkewBucket(hashtable,
220  hashvalue);
221  node->hj_CurTuple = NULL;
222 
223  /*
224  * The tuple might not belong to the current batch (where
225  * "current batch" includes the skew buckets if any).
226  */
227  if (batchno != hashtable->curbatch &&
229  {
230  /*
231  * Need to postpone this outer tuple to a later batch.
232  * Save it in the corresponding outer-batch file.
233  */
234  Assert(batchno > hashtable->curbatch);
236  hashvalue,
237  &hashtable->outerBatchFile[batchno]);
238  /* Loop around, staying in HJ_NEED_NEW_OUTER state */
239  continue;
240  }
241 
242  /* OK, let's scan the bucket for matches */
244 
245  /* FALL THRU */
246 
247  case HJ_SCAN_BUCKET:
248 
249  /*
250  * We check for interrupts here because this corresponds to
251  * where we'd fetch a row from a child plan node in other join
252  * types.
253  */
255 
256  /*
257  * Scan the selected hash bucket for matches to current outer
258  */
259  if (!ExecScanHashBucket(node, econtext))
260  {
261  /* out of matches; check for possible outer-join fill */
263  continue;
264  }
265 
266  /*
267  * We've got a match, but still need to test non-hashed quals.
268  * ExecScanHashBucket already set up all the state needed to
269  * call ExecQual.
270  *
271  * If we pass the qual, then save state for next call and have
272  * ExecProject form the projection, store it in the tuple
273  * table, and return the slot.
274  *
275  * Only the joinquals determine tuple match status, but all
276  * quals must pass to actually return the tuple.
277  */
278  if (joinqual == NIL || ExecQual(joinqual, econtext, false))
279  {
280  node->hj_MatchedOuter = true;
282 
283  /* In an antijoin, we never return a matched tuple */
284  if (node->js.jointype == JOIN_ANTI)
285  {
287  continue;
288  }
289 
290  /*
291  * In a semijoin, we'll consider returning the first
292  * match, but after that we're done with this outer tuple.
293  */
294  if (node->js.jointype == JOIN_SEMI)
296 
297  if (otherqual == NIL ||
298  ExecQual(otherqual, econtext, false))
299  return ExecProject(node->js.ps.ps_ProjInfo);
300  else
301  InstrCountFiltered2(node, 1);
302  }
303  else
304  InstrCountFiltered1(node, 1);
305  break;
306 
307  case HJ_FILL_OUTER_TUPLE:
308 
309  /*
310  * The current outer tuple has run out of matches, so check
311  * whether to emit a dummy outer-join tuple. Whether we emit
312  * one or not, the next state is NEED_NEW_OUTER.
313  */
315 
316  if (!node->hj_MatchedOuter &&
317  HJ_FILL_OUTER(node))
318  {
319  /*
320  * Generate a fake join tuple with nulls for the inner
321  * tuple, and return it if it passes the non-join quals.
322  */
323  econtext->ecxt_innertuple = node->hj_NullInnerTupleSlot;
324 
325  if (otherqual == NIL ||
326  ExecQual(otherqual, econtext, false))
327  return ExecProject(node->js.ps.ps_ProjInfo);
328  else
329  InstrCountFiltered2(node, 1);
330  }
331  break;
332 
334 
335  /*
336  * We have finished a batch, but we are doing right/full join,
337  * so any unmatched inner tuples in the hashtable have to be
338  * emitted before we continue to the next batch.
339  */
340  if (!ExecScanHashTableForUnmatched(node, econtext))
341  {
342  /* no more unmatched tuples */
344  continue;
345  }
346 
347  /*
348  * Generate a fake join tuple with nulls for the outer tuple,
349  * and return it if it passes the non-join quals.
350  */
351  econtext->ecxt_outertuple = node->hj_NullOuterTupleSlot;
352 
353  if (otherqual == NIL ||
354  ExecQual(otherqual, econtext, false))
355  return ExecProject(node->js.ps.ps_ProjInfo);
356  else
357  InstrCountFiltered2(node, 1);
358  break;
359 
360  case HJ_NEED_NEW_BATCH:
361 
362  /*
363  * Try to advance to next batch. Done if there are no more.
364  */
365  if (!ExecHashJoinNewBatch(node))
366  return NULL; /* end of join */
368  break;
369 
370  default:
371  elog(ERROR, "unrecognized hashjoin state: %d",
372  (int) node->hj_JoinState);
373  }
374  }
375 }
JoinType jointype
Definition: execnodes.h:1679
#define NIL
Definition: pg_list.h:69
#define HJ_NEED_NEW_BATCH
Definition: nodeHashjoin.c:35
#define INVALID_SKEW_BUCKET_NO
Definition: hashjoin.h:101
TupleTableSlot * ExecProject(ProjectionInfo *projInfo)
Definition: execQual.c:5214
TupleTableSlot * ExecProcNode(PlanState *node)
Definition: execProcnode.c:380
#define HJ_SCAN_BUCKET
Definition: nodeHashjoin.c:32
HashJoinTable ExecHashTableCreate(Hash *node, List *hashOperators, bool keepNulls)
Definition: nodeHash.c:246
TupleTableSlot * hj_NullInnerTupleSlot
Definition: execnodes.h:1790
ProjectionInfo * ps_ProjInfo
Definition: execnodes.h:1079
PlanState ps
Definition: execnodes.h:1678
bool ExecScanHashTableForUnmatched(HashJoinState *hjstate, ExprContext *econtext)
Definition: nodeHash.c:1145
MinimalTuple ExecFetchSlotMinimalTuple(TupleTableSlot *slot)
Definition: execTuples.c:652
void ExecPrepHashTableForUnmatched(HashJoinState *hjstate)
Definition: nodeHash.c:1121
ExprContext * ps_ExprContext
Definition: execnodes.h:1078
HashJoinTable hashtable
Definition: execnodes.h:2022
List * qual
Definition: execnodes.h:1062
bool hj_MatchedOuter
Definition: execnodes.h:1793
static TupleTableSlot * ExecHashJoinOuterGetTuple(PlanState *outerNode, HashJoinState *hjstate, uint32 *hashvalue)
Definition: nodeHashjoin.c:592
List * joinqual
Definition: execnodes.h:1680
TupleTableSlot * hj_FirstOuterTupleSlot
Definition: execnodes.h:1791
int ExecHashGetSkewBucket(HashJoinTable hashtable, uint32 hashvalue)
Definition: nodeHash.c:1448
#define HJ_FILL_INNER(hjstate)
Definition: nodeHashjoin.c:40
void ExecHashGetBucketAndBatch(HashJoinTable hashtable, uint32 hashvalue, int *bucketno, int *batchno)
Definition: nodeHash.c:1031
uint32 hj_CurHashValue
Definition: execnodes.h:1783
int hj_CurSkewBucketNo
Definition: execnodes.h:1785
#define ERROR
Definition: elog.h:43
TupleTableSlot * hj_NullOuterTupleSlot
Definition: execnodes.h:1789
BufFile ** outerBatchFile
Definition: hashjoin.h:167
#define outerPlanState(node)
Definition: execnodes.h:1090
Cost startup_cost
Definition: plannodes.h:111
HashJoinTuple hj_CurTuple
Definition: execnodes.h:1786
bool ExecScanHashBucket(HashJoinState *hjstate, ExprContext *econtext)
Definition: nodeHash.c:1063
TupleTableSlot * ecxt_innertuple
Definition: execnodes.h:129
bool ExecQual(List *qual, ExprContext *econtext, bool resultForNull)
Definition: execQual.c:5055
#define TupIsNull(slot)
Definition: tuptable.h:138
unsigned int uint32
Definition: c.h:265
PlanState ps
Definition: execnodes.h:2021
#define InstrCountFiltered1(node, delta)
Definition: execnodes.h:1093
List * hj_HashOperators
Definition: execnodes.h:1781
int hj_CurBucketNo
Definition: execnodes.h:1784
#define HJ_FILL_INNER_TUPLES
Definition: nodeHashjoin.c:34
#define HJ_FILL_OUTER(hjstate)
Definition: nodeHashjoin.c:38
Plan * plan
Definition: execnodes.h:1047
double totalTuples
Definition: hashjoin.h:156
#define HJTUPLE_MINTUPLE(hjtup)
Definition: hashjoin.h:72
TupleTableSlot * ecxt_outertuple
Definition: execnodes.h:130
bool hj_OuterNotEmpty
Definition: execnodes.h:1794
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define InstrCountFiltered2(node, delta)
Definition: execnodes.h:1098
#define HJ_NEED_NEW_OUTER
Definition: nodeHashjoin.c:31
#define HJ_BUILD_HASHTABLE
Definition: nodeHashjoin.c:30
HashJoinTable hj_HashTable
Definition: execnodes.h:1782
Node * MultiExecProcNode(PlanState *node)
Definition: execProcnode.c:571
Cost total_cost
Definition: plannodes.h:112
#define HeapTupleHeaderSetMatch(tup)
Definition: htup_details.h:522
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
#define HJ_FILL_OUTER_TUPLE
Definition: nodeHashjoin.c:33
#define elog
Definition: elog.h:219
#define innerPlanState(node)
Definition: execnodes.h:1089
Definition: pg_list.h:45
JoinState js
Definition: execnodes.h:1777
static bool ExecHashJoinNewBatch(HashJoinState *hjstate)
Definition: nodeHashjoin.c:669
void ExecHashJoinSaveTuple(MinimalTuple tuple, uint32 hashvalue, BufFile **fileptr)
Definition: nodeHashjoin.c:816
#define ResetExprContext(econtext)
Definition: executor.h:332
void ExecHashJoinSaveTuple ( MinimalTuple  tuple,
uint32  hashvalue,
BufFile **  fileptr 
)

Definition at line 816 of file nodeHashjoin.c.

References BufFileCreateTemp(), BufFileWrite(), ereport, errcode_for_file_access(), errmsg(), ERROR, NULL, and MinimalTupleData::t_len.

Referenced by ExecHashIncreaseNumBatches(), ExecHashJoin(), ExecHashRemoveNextSkewBucket(), and ExecHashTableInsert().

818 {
819  BufFile *file = *fileptr;
820  size_t written;
821 
822  if (file == NULL)
823  {
824  /* First write to this batch file, so open it. */
825  file = BufFileCreateTemp(false);
826  *fileptr = file;
827  }
828 
829  written = BufFileWrite(file, (void *) &hashvalue, sizeof(uint32));
830  if (written != sizeof(uint32))
831  ereport(ERROR,
833  errmsg("could not write to hash-join temporary file: %m")));
834 
835  written = BufFileWrite(file, (void *) tuple, tuple->t_len);
836  if (written != tuple->t_len)
837  ereport(ERROR,
839  errmsg("could not write to hash-join temporary file: %m")));
840 }
#define ERROR
Definition: elog.h:43
BufFile * BufFileCreateTemp(bool interXact)
Definition: buffile.c:166
int errcode_for_file_access(void)
Definition: elog.c:598
unsigned int uint32
Definition: c.h:265
#define ereport(elevel, rest)
Definition: elog.h:122
#define NULL
Definition: c.h:226
int errmsg(const char *fmt,...)
Definition: elog.c:797
size_t BufFileWrite(BufFile *file, void *ptr, size_t size)
Definition: buffile.c:405
HashJoinState* ExecInitHashJoin ( HashJoin node,
EState estate,
int  eflags 
)

Definition at line 384 of file nodeHashjoin.c.

References FuncExprState::args, Assert, castNode, elog, ERROR, EXEC_FLAG_BACKWARD, EXEC_FLAG_MARK, ExecAssignExprContext(), ExecAssignProjectionInfo(), ExecAssignResultTypeFromTL(), ExecGetResultType(), ExecInitExpr(), ExecInitExtraTupleSlot(), ExecInitNode(), ExecInitNullTupleSlot(), ExecInitResultTupleSlot(), ExecSetSlotDescriptor(), ExprState::expr, HashJoin::hashclauses, HashJoinState::hashclauses, HJ_BUILD_HASHTABLE, HashJoinState::hj_CurBucketNo, HashJoinState::hj_CurHashValue, HashJoinState::hj_CurSkewBucketNo, HashJoinState::hj_CurTuple, HashJoinState::hj_FirstOuterTupleSlot, HashJoinState::hj_HashOperators, HashJoinState::hj_HashTable, HashJoinState::hj_HashTupleSlot, HashJoinState::hj_InnerHashKeys, HashJoinState::hj_JoinState, HashJoinState::hj_MatchedOuter, HashJoinState::hj_NullInnerTupleSlot, HashJoinState::hj_NullOuterTupleSlot, HashJoinState::hj_OuterHashKeys, HashJoinState::hj_OuterNotEmpty, HashJoinState::hj_OuterTupleSlot, innerPlan, innerPlanState, INVALID_SKEW_BUCKET_NO, HashJoin::join, JOIN_ANTI, JOIN_FULL, JOIN_INNER, JOIN_LEFT, JOIN_RIGHT, JOIN_SEMI, Join::joinqual, JoinState::joinqual, Join::jointype, JoinState::jointype, HashJoinState::js, lappend(), lappend_oid(), lfirst, linitial, lsecond, makeNode, NIL, NULL, OpExpr::opno, outerPlan, outerPlanState, Join::plan, PlanState::plan, JoinState::ps, HashState::ps, PlanState::ps_ResultTupleSlot, Plan::qual, PlanState::qual, PlanState::state, Plan::targetlist, PlanState::targetlist, and FuncExprState::xprstate.

Referenced by ExecInitNode().

385 {
386  HashJoinState *hjstate;
387  Plan *outerNode;
388  Hash *hashNode;
389  List *lclauses;
390  List *rclauses;
391  List *hoperators;
392  ListCell *l;
393 
394  /* check for unsupported flags */
395  Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
396 
397  /*
398  * create state structure
399  */
400  hjstate = makeNode(HashJoinState);
401  hjstate->js.ps.plan = (Plan *) node;
402  hjstate->js.ps.state = estate;
403 
404  /*
405  * Miscellaneous initialization
406  *
407  * create expression context for node
408  */
409  ExecAssignExprContext(estate, &hjstate->js.ps);
410 
411  /*
412  * initialize child expressions
413  */
414  hjstate->js.ps.targetlist = (List *)
416  (PlanState *) hjstate);
417  hjstate->js.ps.qual = (List *)
418  ExecInitExpr((Expr *) node->join.plan.qual,
419  (PlanState *) hjstate);
420  hjstate->js.jointype = node->join.jointype;
421  hjstate->js.joinqual = (List *)
422  ExecInitExpr((Expr *) node->join.joinqual,
423  (PlanState *) hjstate);
424  hjstate->hashclauses = (List *)
425  ExecInitExpr((Expr *) node->hashclauses,
426  (PlanState *) hjstate);
427 
428  /*
429  * initialize child nodes
430  *
431  * Note: we could suppress the REWIND flag for the inner input, which
432  * would amount to betting that the hash will be a single batch. Not
433  * clear if this would be a win or not.
434  */
435  outerNode = outerPlan(node);
436  hashNode = (Hash *) innerPlan(node);
437 
438  outerPlanState(hjstate) = ExecInitNode(outerNode, estate, eflags);
439  innerPlanState(hjstate) = ExecInitNode((Plan *) hashNode, estate, eflags);
440 
441  /*
442  * tuple table initialization
443  */
444  ExecInitResultTupleSlot(estate, &hjstate->js.ps);
445  hjstate->hj_OuterTupleSlot = ExecInitExtraTupleSlot(estate);
446 
447  /* set up null tuples for outer joins, if needed */
448  switch (node->join.jointype)
449  {
450  case JOIN_INNER:
451  case JOIN_SEMI:
452  break;
453  case JOIN_LEFT:
454  case JOIN_ANTI:
455  hjstate->hj_NullInnerTupleSlot =
456  ExecInitNullTupleSlot(estate,
458  break;
459  case JOIN_RIGHT:
460  hjstate->hj_NullOuterTupleSlot =
461  ExecInitNullTupleSlot(estate,
463  break;
464  case JOIN_FULL:
465  hjstate->hj_NullOuterTupleSlot =
466  ExecInitNullTupleSlot(estate,
468  hjstate->hj_NullInnerTupleSlot =
469  ExecInitNullTupleSlot(estate,
471  break;
472  default:
473  elog(ERROR, "unrecognized join type: %d",
474  (int) node->join.jointype);
475  }
476 
477  /*
478  * now for some voodoo. our temporary tuple slot is actually the result
479  * tuple slot of the Hash node (which is our inner plan). we can do this
480  * because Hash nodes don't return tuples via ExecProcNode() -- instead
481  * the hash join node uses ExecScanHashBucket() to get at the contents of
482  * the hash table. -cim 6/9/91
483  */
484  {
485  HashState *hashstate = (HashState *) innerPlanState(hjstate);
486  TupleTableSlot *slot = hashstate->ps.ps_ResultTupleSlot;
487 
488  hjstate->hj_HashTupleSlot = slot;
489  }
490 
491  /*
492  * initialize tuple type and projection info
493  */
494  ExecAssignResultTypeFromTL(&hjstate->js.ps);
495  ExecAssignProjectionInfo(&hjstate->js.ps, NULL);
496 
499 
500  /*
501  * initialize hash-specific info
502  */
503  hjstate->hj_HashTable = NULL;
504  hjstate->hj_FirstOuterTupleSlot = NULL;
505 
506  hjstate->hj_CurHashValue = 0;
507  hjstate->hj_CurBucketNo = 0;
509  hjstate->hj_CurTuple = NULL;
510 
511  /*
512  * Deconstruct the hash clauses into outer and inner argument values, so
513  * that we can evaluate those subexpressions separately. Also make a list
514  * of the hash operator OIDs, in preparation for looking up the hash
515  * functions to use.
516  */
517  lclauses = NIL;
518  rclauses = NIL;
519  hoperators = NIL;
520  foreach(l, hjstate->hashclauses)
521  {
523  OpExpr *hclause = castNode(OpExpr, fstate->xprstate.expr);
524 
525  lclauses = lappend(lclauses, linitial(fstate->args));
526  rclauses = lappend(rclauses, lsecond(fstate->args));
527  hoperators = lappend_oid(hoperators, hclause->opno);
528  }
529  hjstate->hj_OuterHashKeys = lclauses;
530  hjstate->hj_InnerHashKeys = rclauses;
531  hjstate->hj_HashOperators = hoperators;
532  /* child Hash node needs to evaluate inner hash keys, too */
533  ((HashState *) innerPlanState(hjstate))->hashkeys = rclauses;
534 
535  hjstate->hj_JoinState = HJ_BUILD_HASHTABLE;
536  hjstate->hj_MatchedOuter = false;
537  hjstate->hj_OuterNotEmpty = false;
538 
539  return hjstate;
540 }
JoinType jointype
Definition: execnodes.h:1679
#define NIL
Definition: pg_list.h:69
List * qual
Definition: plannodes.h:130
#define INVALID_SKEW_BUCKET_NO
Definition: hashjoin.h:101
TupleTableSlot * ExecInitExtraTupleSlot(EState *estate)
Definition: execTuples.c:852
TupleTableSlot * hj_NullInnerTupleSlot
Definition: execnodes.h:1790
PlanState ps
Definition: execnodes.h:1678
#define castNode(_type_, nodeptr)
Definition: nodes.h:577
List * hashclauses
Definition: plannodes.h:676
List * args
Definition: execnodes.h:697
List * qual
Definition: execnodes.h:1062
bool hj_MatchedOuter
Definition: execnodes.h:1793
List * targetlist
Definition: execnodes.h:1061
EState * state
Definition: execnodes.h:1049
List * joinqual
Definition: execnodes.h:1680
TupleTableSlot * hj_OuterTupleSlot
Definition: execnodes.h:1787
List * hj_OuterHashKeys
Definition: execnodes.h:1779
List * lappend_oid(List *list, Oid datum)
Definition: list.c:164
TupleTableSlot * hj_FirstOuterTupleSlot
Definition: execnodes.h:1791
#define lsecond(l)
Definition: pg_list.h:114
Join join
Definition: plannodes.h:675
void ExecAssignResultTypeFromTL(PlanState *planstate)
Definition: execUtils.c:430
JoinType jointype
Definition: plannodes.h:619
uint32 hj_CurHashValue
Definition: execnodes.h:1783
int hj_CurSkewBucketNo
Definition: execnodes.h:1785
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:1077
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition: execQual.c:4266
TupleTableSlot * ExecInitNullTupleSlot(EState *estate, TupleDesc tupType)
Definition: execTuples.c:866
#define linitial(l)
Definition: pg_list.h:110
#define ERROR
Definition: elog.h:43
TupleTableSlot * hj_NullOuterTupleSlot
Definition: execnodes.h:1789
Expr * expr
Definition: execnodes.h:598
void ExecInitResultTupleSlot(EState *estate, PlanState *planstate)
Definition: execTuples.c:832
#define EXEC_FLAG_BACKWARD
Definition: executor.h:60
#define outerPlanState(node)
Definition: execnodes.h:1090
#define innerPlan(node)
Definition: plannodes.h:158
void ExecAssignProjectionInfo(PlanState *planstate, TupleDesc inputDesc)
Definition: execUtils.c:657
HashJoinTuple hj_CurTuple
Definition: execnodes.h:1786
PlanState ps
Definition: execnodes.h:2021
List * hj_HashOperators
Definition: execnodes.h:1781
#define outerPlan(node)
Definition: plannodes.h:159
List * lappend(List *list, void *datum)
Definition: list.c:128
int hj_CurBucketNo
Definition: execnodes.h:1784
void ExecSetSlotDescriptor(TupleTableSlot *slot, TupleDesc tupdesc)
Definition: execTuples.c:247
Plan * plan
Definition: execnodes.h:1047
#define makeNode(_type_)
Definition: nodes.h:556
bool hj_OuterNotEmpty
Definition: execnodes.h:1794
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define lfirst(lc)
Definition: pg_list.h:106
#define EXEC_FLAG_MARK
Definition: executor.h:61
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:408
ExprState xprstate
Definition: execnodes.h:696
TupleDesc ExecGetResultType(PlanState *planstate)
Definition: execUtils.c:459
List * targetlist
Definition: plannodes.h:129
List * hashclauses
Definition: execnodes.h:1778
TupleTableSlot * hj_HashTupleSlot
Definition: execnodes.h:1788
List * hj_InnerHashKeys
Definition: execnodes.h:1780
#define HJ_BUILD_HASHTABLE
Definition: nodeHashjoin.c:30
HashJoinTable hj_HashTable
Definition: execnodes.h:1782
Oid opno
Definition: primnodes.h:473
#define elog
Definition: elog.h:219
#define innerPlanState(node)
Definition: execnodes.h:1089
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:137
Definition: pg_list.h:45
JoinState js
Definition: execnodes.h:1777
List * joinqual
Definition: plannodes.h:620
Plan plan
Definition: plannodes.h:618
void ExecReScanHashJoin ( HashJoinState node)

Definition at line 896 of file nodeHashjoin.c.

References PlanState::chgParam, ExecHashTableDestroy(), ExecHashTableResetMatchFlags(), ExecReScan(), HJ_BUILD_HASHTABLE, HashJoinState::hj_CurBucketNo, HashJoinState::hj_CurHashValue, HashJoinState::hj_CurSkewBucketNo, HashJoinState::hj_CurTuple, HJ_FILL_INNER, HashJoinState::hj_FirstOuterTupleSlot, HashJoinState::hj_HashTable, HashJoinState::hj_JoinState, HashJoinState::hj_MatchedOuter, HJ_NEED_NEW_OUTER, HashJoinState::hj_OuterNotEmpty, INVALID_SKEW_BUCKET_NO, HashJoinState::js, PlanState::lefttree, HashJoinTableData::nbatch, NULL, JoinState::ps, and PlanState::righttree.

Referenced by ExecReScan().

897 {
898  /*
899  * In a multi-batch join, we currently have to do rescans the hard way,
900  * primarily because batch temp files may have already been released. But
901  * if it's a single-batch join, and there is no parameter change for the
902  * inner subnode, then we can just re-use the existing hash table without
903  * rebuilding it.
904  */
905  if (node->hj_HashTable != NULL)
906  {
907  if (node->hj_HashTable->nbatch == 1 &&
908  node->js.ps.righttree->chgParam == NULL)
909  {
910  /*
911  * Okay to reuse the hash table; needn't rescan inner, either.
912  *
913  * However, if it's a right/full join, we'd better reset the
914  * inner-tuple match flags contained in the table.
915  */
916  if (HJ_FILL_INNER(node))
918 
919  /*
920  * Also, we need to reset our state about the emptiness of the
921  * outer relation, so that the new scan of the outer will update
922  * it correctly if it turns out to be empty this time. (There's no
923  * harm in clearing it now because ExecHashJoin won't need the
924  * info. In the other cases, where the hash table doesn't exist
925  * or we are destroying it, we leave this state alone because
926  * ExecHashJoin will need it the first time through.)
927  */
928  node->hj_OuterNotEmpty = false;
929 
930  /* ExecHashJoin can skip the BUILD_HASHTABLE step */
932  }
933  else
934  {
935  /* must destroy and rebuild hash table */
937  node->hj_HashTable = NULL;
939 
940  /*
941  * if chgParam of subnode is not null then plan will be re-scanned
942  * by first ExecProcNode.
943  */
944  if (node->js.ps.righttree->chgParam == NULL)
945  ExecReScan(node->js.ps.righttree);
946  }
947  }
948 
949  /* Always reset intra-tuple state */
950  node->hj_CurHashValue = 0;
951  node->hj_CurBucketNo = 0;
953  node->hj_CurTuple = NULL;
954 
955  node->hj_MatchedOuter = false;
957 
958  /*
959  * if chgParam of subnode is not null then plan will be re-scanned by
960  * first ExecProcNode.
961  */
962  if (node->js.ps.lefttree->chgParam == NULL)
963  ExecReScan(node->js.ps.lefttree);
964 }
#define INVALID_SKEW_BUCKET_NO
Definition: hashjoin.h:101
PlanState ps
Definition: execnodes.h:1678
void ExecReScan(PlanState *node)
Definition: execAmi.c:73
bool hj_MatchedOuter
Definition: execnodes.h:1793
struct PlanState * righttree
Definition: execnodes.h:1064
TupleTableSlot * hj_FirstOuterTupleSlot
Definition: execnodes.h:1791
struct PlanState * lefttree
Definition: execnodes.h:1063
#define HJ_FILL_INNER(hjstate)
Definition: nodeHashjoin.c:40
uint32 hj_CurHashValue
Definition: execnodes.h:1783
int hj_CurSkewBucketNo
Definition: execnodes.h:1785
HashJoinTuple hj_CurTuple
Definition: execnodes.h:1786
Bitmapset * chgParam
Definition: execnodes.h:1072
int hj_CurBucketNo
Definition: execnodes.h:1784
bool hj_OuterNotEmpty
Definition: execnodes.h:1794
#define NULL
Definition: c.h:226
#define HJ_NEED_NEW_OUTER
Definition: nodeHashjoin.c:31
#define HJ_BUILD_HASHTABLE
Definition: nodeHashjoin.c:30
HashJoinTable hj_HashTable
Definition: execnodes.h:1782
void ExecHashTableResetMatchFlags(HashJoinTable hashtable)
Definition: nodeHash.c:1242
JoinState js
Definition: execnodes.h:1777
void ExecHashTableDestroy(HashJoinTable hashtable)
Definition: nodeHash.c:563