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 541 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().

542 {
543  /*
544  * Free hash table
545  */
546  if (node->hj_HashTable)
547  {
549  node->hj_HashTable = NULL;
550  }
551 
552  /*
553  * Free the exprcontext
554  */
555  ExecFreeExprContext(&node->js.ps);
556 
557  /*
558  * clean out the tuple table
559  */
563 
564  /*
565  * clean up subtrees
566  */
569 }
PlanState ps
Definition: execnodes.h:1516
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:644
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
TupleTableSlot * hj_OuterTupleSlot
Definition: execnodes.h:1625
void ExecFreeExprContext(PlanState *planstate)
Definition: execUtils.c:511
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:829
#define outerPlanState(node)
Definition: execnodes.h:842
#define NULL
Definition: c.h:229
TupleTableSlot * hj_HashTupleSlot
Definition: execnodes.h:1626
HashJoinTable hj_HashTable
Definition: execnodes.h:1620
#define innerPlanState(node)
Definition: execnodes.h:841
JoinState js
Definition: execnodes.h:1615
void ExecHashTableDestroy(HashJoinTable hashtable)
Definition: nodeHash.c:559
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, 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  ExprState *joinqual;
67  ExprState *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 == NULL || ExecQual(joinqual, econtext))
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 == NULL || ExecQual(otherqual, econtext))
298  return ExecProject(node->js.ps.ps_ProjInfo);
299  else
300  InstrCountFiltered2(node, 1);
301  }
302  else
303  InstrCountFiltered1(node, 1);
304  break;
305 
306  case HJ_FILL_OUTER_TUPLE:
307 
308  /*
309  * The current outer tuple has run out of matches, so check
310  * whether to emit a dummy outer-join tuple. Whether we emit
311  * one or not, the next state is NEED_NEW_OUTER.
312  */
314 
315  if (!node->hj_MatchedOuter &&
316  HJ_FILL_OUTER(node))
317  {
318  /*
319  * Generate a fake join tuple with nulls for the inner
320  * tuple, and return it if it passes the non-join quals.
321  */
322  econtext->ecxt_innertuple = node->hj_NullInnerTupleSlot;
323 
324  if (otherqual == NULL || ExecQual(otherqual, econtext))
325  return ExecProject(node->js.ps.ps_ProjInfo);
326  else
327  InstrCountFiltered2(node, 1);
328  }
329  break;
330 
332 
333  /*
334  * We have finished a batch, but we are doing right/full join,
335  * so any unmatched inner tuples in the hashtable have to be
336  * emitted before we continue to the next batch.
337  */
338  if (!ExecScanHashTableForUnmatched(node, econtext))
339  {
340  /* no more unmatched tuples */
342  continue;
343  }
344 
345  /*
346  * Generate a fake join tuple with nulls for the outer tuple,
347  * and return it if it passes the non-join quals.
348  */
349  econtext->ecxt_outertuple = node->hj_NullOuterTupleSlot;
350 
351  if (otherqual == NULL || ExecQual(otherqual, econtext))
352  return ExecProject(node->js.ps.ps_ProjInfo);
353  else
354  InstrCountFiltered2(node, 1);
355  break;
356 
357  case HJ_NEED_NEW_BATCH:
358 
359  /*
360  * Try to advance to next batch. Done if there are no more.
361  */
362  if (!ExecHashJoinNewBatch(node))
363  return NULL; /* end of join */
365  break;
366 
367  default:
368  elog(ERROR, "unrecognized hashjoin state: %d",
369  (int) node->hj_JoinState);
370  }
371  }
372 }
JoinType jointype
Definition: execnodes.h:1517
#define HJ_NEED_NEW_BATCH
Definition: nodeHashjoin.c:35
#define INVALID_SKEW_BUCKET_NO
Definition: hashjoin.h:101
TupleTableSlot * ExecProcNode(PlanState *node)
Definition: execProcnode.c:392
#define HJ_SCAN_BUCKET
Definition: nodeHashjoin.c:32
HashJoinTable ExecHashTableCreate(Hash *node, List *hashOperators, bool keepNulls)
Definition: nodeHash.c:242
TupleTableSlot * hj_NullInnerTupleSlot
Definition: execnodes.h:1628
ExprState * joinqual
Definition: execnodes.h:1518
ProjectionInfo * ps_ProjInfo
Definition: execnodes.h:831
PlanState ps
Definition: execnodes.h:1516
bool ExecScanHashTableForUnmatched(HashJoinState *hjstate, ExprContext *econtext)
Definition: nodeHash.c:1141
MinimalTuple ExecFetchSlotMinimalTuple(TupleTableSlot *slot)
Definition: execTuples.c:652
void ExecPrepHashTableForUnmatched(HashJoinState *hjstate)
Definition: nodeHash.c:1117
ExprContext * ps_ExprContext
Definition: execnodes.h:830
HashJoinTable hashtable
Definition: execnodes.h:1888
bool hj_MatchedOuter
Definition: execnodes.h:1631
static TupleTableSlot * ExecHashJoinOuterGetTuple(PlanState *outerNode, HashJoinState *hjstate, uint32 *hashvalue)
Definition: nodeHashjoin.c:584
static bool ExecQual(ExprState *state, ExprContext *econtext)
Definition: executor.h:346
TupleTableSlot * hj_FirstOuterTupleSlot
Definition: execnodes.h:1629
int ExecHashGetSkewBucket(HashJoinTable hashtable, uint32 hashvalue)
Definition: nodeHash.c:1444
#define HJ_FILL_INNER(hjstate)
Definition: nodeHashjoin.c:40
void ExecHashGetBucketAndBatch(HashJoinTable hashtable, uint32 hashvalue, int *bucketno, int *batchno)
Definition: nodeHash.c:1027
uint32 hj_CurHashValue
Definition: execnodes.h:1621
int hj_CurSkewBucketNo
Definition: execnodes.h:1623
#define ERROR
Definition: elog.h:43
TupleTableSlot * hj_NullOuterTupleSlot
Definition: execnodes.h:1627
BufFile ** outerBatchFile
Definition: hashjoin.h:167
#define outerPlanState(node)
Definition: execnodes.h:842
Cost startup_cost
Definition: plannodes.h:114
HashJoinTuple hj_CurTuple
Definition: execnodes.h:1624
bool ExecScanHashBucket(HashJoinState *hjstate, ExprContext *econtext)
Definition: nodeHash.c:1059
TupleTableSlot * ecxt_innertuple
Definition: execnodes.h:197
#define TupIsNull(slot)
Definition: tuptable.h:138
unsigned int uint32
Definition: c.h:268
PlanState ps
Definition: execnodes.h:1887
#define InstrCountFiltered1(node, delta)
Definition: execnodes.h:845
List * hj_HashOperators
Definition: execnodes.h:1619
int hj_CurBucketNo
Definition: execnodes.h:1622
#define HJ_FILL_INNER_TUPLES
Definition: nodeHashjoin.c:34
#define HJ_FILL_OUTER(hjstate)
Definition: nodeHashjoin.c:38
Plan * plan
Definition: execnodes.h:800
double totalTuples
Definition: hashjoin.h:156
#define HJTUPLE_MINTUPLE(hjtup)
Definition: hashjoin.h:72
TupleTableSlot * ecxt_outertuple
Definition: execnodes.h:198
bool hj_OuterNotEmpty
Definition: execnodes.h:1632
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define InstrCountFiltered2(node, delta)
Definition: execnodes.h:850
#define HJ_NEED_NEW_OUTER
Definition: nodeHashjoin.c:31
ExprState * qual
Definition: execnodes.h:814
#define HJ_BUILD_HASHTABLE
Definition: nodeHashjoin.c:30
HashJoinTable hj_HashTable
Definition: execnodes.h:1620
Node * MultiExecProcNode(PlanState *node)
Definition: execProcnode.c:591
Cost total_cost
Definition: plannodes.h:115
#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:841
JoinState js
Definition: execnodes.h:1615
static bool ExecHashJoinNewBatch(HashJoinState *hjstate)
Definition: nodeHashjoin.c:661
void ExecHashJoinSaveTuple(MinimalTuple tuple, uint32 hashvalue, BufFile **fileptr)
Definition: nodeHashjoin.c:808
static TupleTableSlot * ExecProject(ProjectionInfo *projInfo)
Definition: executor.h:309
#define ResetExprContext(econtext)
Definition: executor.h:450
void ExecHashJoinSaveTuple ( MinimalTuple  tuple,
uint32  hashvalue,
BufFile **  fileptr 
)

Definition at line 808 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().

810 {
811  BufFile *file = *fileptr;
812  size_t written;
813 
814  if (file == NULL)
815  {
816  /* First write to this batch file, so open it. */
817  file = BufFileCreateTemp(false);
818  *fileptr = file;
819  }
820 
821  written = BufFileWrite(file, (void *) &hashvalue, sizeof(uint32));
822  if (written != sizeof(uint32))
823  ereport(ERROR,
825  errmsg("could not write to hash-join temporary file: %m")));
826 
827  written = BufFileWrite(file, (void *) tuple, tuple->t_len);
828  if (written != tuple->t_len)
829  ereport(ERROR,
831  errmsg("could not write to hash-join temporary file: %m")));
832 }
#define ERROR
Definition: elog.h:43
BufFile * BufFileCreateTemp(bool interXact)
Definition: buffile.c:167
int errcode_for_file_access(void)
Definition: elog.c:598
unsigned int uint32
Definition: c.h:268
#define ereport(elevel, rest)
Definition: elog.h:122
#define NULL
Definition: c.h:229
int errmsg(const char *fmt,...)
Definition: elog.c:797
size_t BufFileWrite(BufFile *file, void *ptr, size_t size)
Definition: buffile.c:412
HashJoinState* ExecInitHashJoin ( HashJoin node,
EState estate,
int  eflags 
)

Definition at line 381 of file nodeHashjoin.c.

References OpExpr::args, Assert, castNode, elog, ERROR, EXEC_FLAG_BACKWARD, EXEC_FLAG_MARK, ExecAssignExprContext(), ExecAssignProjectionInfo(), ExecAssignResultTypeFromTL(), ExecGetResultType(), ExecInitExpr(), ExecInitExtraTupleSlot(), ExecInitNode(), ExecInitNullTupleSlot(), ExecInitQual(), ExecInitResultTupleSlot(), ExecSetSlotDescriptor(), 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, and PlanState::state.

Referenced by ExecInitNode().

382 {
383  HashJoinState *hjstate;
384  Plan *outerNode;
385  Hash *hashNode;
386  List *lclauses;
387  List *rclauses;
388  List *hoperators;
389  ListCell *l;
390 
391  /* check for unsupported flags */
392  Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
393 
394  /*
395  * create state structure
396  */
397  hjstate = makeNode(HashJoinState);
398  hjstate->js.ps.plan = (Plan *) node;
399  hjstate->js.ps.state = estate;
400 
401  /*
402  * Miscellaneous initialization
403  *
404  * create expression context for node
405  */
406  ExecAssignExprContext(estate, &hjstate->js.ps);
407 
408  /*
409  * initialize child expressions
410  */
411  hjstate->js.ps.qual =
412  ExecInitQual(node->join.plan.qual, (PlanState *) hjstate);
413  hjstate->js.jointype = node->join.jointype;
414  hjstate->js.joinqual =
415  ExecInitQual(node->join.joinqual, (PlanState *) hjstate);
416  hjstate->hashclauses =
417  ExecInitQual(node->hashclauses, (PlanState *) hjstate);
418 
419  /*
420  * initialize child nodes
421  *
422  * Note: we could suppress the REWIND flag for the inner input, which
423  * would amount to betting that the hash will be a single batch. Not
424  * clear if this would be a win or not.
425  */
426  outerNode = outerPlan(node);
427  hashNode = (Hash *) innerPlan(node);
428 
429  outerPlanState(hjstate) = ExecInitNode(outerNode, estate, eflags);
430  innerPlanState(hjstate) = ExecInitNode((Plan *) hashNode, estate, eflags);
431 
432  /*
433  * tuple table initialization
434  */
435  ExecInitResultTupleSlot(estate, &hjstate->js.ps);
436  hjstate->hj_OuterTupleSlot = ExecInitExtraTupleSlot(estate);
437 
438  /* set up null tuples for outer joins, if needed */
439  switch (node->join.jointype)
440  {
441  case JOIN_INNER:
442  case JOIN_SEMI:
443  break;
444  case JOIN_LEFT:
445  case JOIN_ANTI:
446  hjstate->hj_NullInnerTupleSlot =
447  ExecInitNullTupleSlot(estate,
449  break;
450  case JOIN_RIGHT:
451  hjstate->hj_NullOuterTupleSlot =
452  ExecInitNullTupleSlot(estate,
454  break;
455  case JOIN_FULL:
456  hjstate->hj_NullOuterTupleSlot =
457  ExecInitNullTupleSlot(estate,
459  hjstate->hj_NullInnerTupleSlot =
460  ExecInitNullTupleSlot(estate,
462  break;
463  default:
464  elog(ERROR, "unrecognized join type: %d",
465  (int) node->join.jointype);
466  }
467 
468  /*
469  * now for some voodoo. our temporary tuple slot is actually the result
470  * tuple slot of the Hash node (which is our inner plan). we can do this
471  * because Hash nodes don't return tuples via ExecProcNode() -- instead
472  * the hash join node uses ExecScanHashBucket() to get at the contents of
473  * the hash table. -cim 6/9/91
474  */
475  {
476  HashState *hashstate = (HashState *) innerPlanState(hjstate);
477  TupleTableSlot *slot = hashstate->ps.ps_ResultTupleSlot;
478 
479  hjstate->hj_HashTupleSlot = slot;
480  }
481 
482  /*
483  * initialize tuple type and projection info
484  */
485  ExecAssignResultTypeFromTL(&hjstate->js.ps);
486  ExecAssignProjectionInfo(&hjstate->js.ps, NULL);
487 
490 
491  /*
492  * initialize hash-specific info
493  */
494  hjstate->hj_HashTable = NULL;
495  hjstate->hj_FirstOuterTupleSlot = NULL;
496 
497  hjstate->hj_CurHashValue = 0;
498  hjstate->hj_CurBucketNo = 0;
500  hjstate->hj_CurTuple = NULL;
501 
502  /*
503  * Deconstruct the hash clauses into outer and inner argument values, so
504  * that we can evaluate those subexpressions separately. Also make a list
505  * of the hash operator OIDs, in preparation for looking up the hash
506  * functions to use.
507  */
508  lclauses = NIL;
509  rclauses = NIL;
510  hoperators = NIL;
511  foreach(l, node->hashclauses)
512  {
513  OpExpr *hclause = castNode(OpExpr, lfirst(l));
514 
515  lclauses = lappend(lclauses, ExecInitExpr(linitial(hclause->args),
516  (PlanState *) hjstate));
517  rclauses = lappend(rclauses, ExecInitExpr(lsecond(hclause->args),
518  (PlanState *) hjstate));
519  hoperators = lappend_oid(hoperators, hclause->opno);
520  }
521  hjstate->hj_OuterHashKeys = lclauses;
522  hjstate->hj_InnerHashKeys = rclauses;
523  hjstate->hj_HashOperators = hoperators;
524  /* child Hash node needs to evaluate inner hash keys, too */
525  ((HashState *) innerPlanState(hjstate))->hashkeys = rclauses;
526 
527  hjstate->hj_JoinState = HJ_BUILD_HASHTABLE;
528  hjstate->hj_MatchedOuter = false;
529  hjstate->hj_OuterNotEmpty = false;
530 
531  return hjstate;
532 }
JoinType jointype
Definition: execnodes.h:1517
#define NIL
Definition: pg_list.h:69
List * qual
Definition: plannodes.h:133
#define INVALID_SKEW_BUCKET_NO
Definition: hashjoin.h:101
TupleTableSlot * ExecInitExtraTupleSlot(EState *estate)
Definition: execTuples.c:852
TupleTableSlot * hj_NullInnerTupleSlot
Definition: execnodes.h:1628
ExprState * joinqual
Definition: execnodes.h:1518
PlanState ps
Definition: execnodes.h:1516
#define castNode(_type_, nodeptr)
Definition: nodes.h:575
List * hashclauses
Definition: plannodes.h:697
bool hj_MatchedOuter
Definition: execnodes.h:1631
EState * state
Definition: execnodes.h:802
TupleTableSlot * hj_OuterTupleSlot
Definition: execnodes.h:1625
List * hj_OuterHashKeys
Definition: execnodes.h:1617
List * lappend_oid(List *list, Oid datum)
Definition: list.c:164
TupleTableSlot * hj_FirstOuterTupleSlot
Definition: execnodes.h:1629
#define lsecond(l)
Definition: pg_list.h:114
Join join
Definition: plannodes.h:696
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition: execExpr.c:160
void ExecAssignResultTypeFromTL(PlanState *planstate)
Definition: execUtils.c:435
JoinType jointype
Definition: plannodes.h:640
uint32 hj_CurHashValue
Definition: execnodes.h:1621
int hj_CurSkewBucketNo
Definition: execnodes.h:1623
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:829
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:1627
void ExecInitResultTupleSlot(EState *estate, PlanState *planstate)
Definition: execTuples.c:832
#define EXEC_FLAG_BACKWARD
Definition: executor.h:60
#define outerPlanState(node)
Definition: execnodes.h:842
#define innerPlan(node)
Definition: plannodes.h:161
void ExecAssignProjectionInfo(PlanState *planstate, TupleDesc inputDesc)
Definition: execUtils.c:482
HashJoinTuple hj_CurTuple
Definition: execnodes.h:1624
PlanState ps
Definition: execnodes.h:1887
List * hj_HashOperators
Definition: execnodes.h:1619
#define outerPlan(node)
Definition: plannodes.h:162
List * lappend(List *list, void *datum)
Definition: list.c:128
int hj_CurBucketNo
Definition: execnodes.h:1622
void ExecSetSlotDescriptor(TupleTableSlot *slot, TupleDesc tupdesc)
Definition: execTuples.c:247
Plan * plan
Definition: execnodes.h:800
#define makeNode(_type_)
Definition: nodes.h:554
bool hj_OuterNotEmpty
Definition: execnodes.h:1632
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#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:413
TupleDesc ExecGetResultType(PlanState *planstate)
Definition: execUtils.c:464
ExprState * qual
Definition: execnodes.h:814
TupleTableSlot * hj_HashTupleSlot
Definition: execnodes.h:1626
List * hj_InnerHashKeys
Definition: execnodes.h:1618
#define HJ_BUILD_HASHTABLE
Definition: nodeHashjoin.c:30
HashJoinTable hj_HashTable
Definition: execnodes.h:1620
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition: execExpr.c:113
Oid opno
Definition: primnodes.h:495
#define elog
Definition: elog.h:219
List * args
Definition: primnodes.h:501
#define innerPlanState(node)
Definition: execnodes.h:841
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:139
Definition: pg_list.h:45
JoinState js
Definition: execnodes.h:1615
List * joinqual
Definition: plannodes.h:641
ExprState * hashclauses
Definition: execnodes.h:1616
Plan plan
Definition: plannodes.h:639
void ExecReScanHashJoin ( HashJoinState node)

Definition at line 888 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().

889 {
890  /*
891  * In a multi-batch join, we currently have to do rescans the hard way,
892  * primarily because batch temp files may have already been released. But
893  * if it's a single-batch join, and there is no parameter change for the
894  * inner subnode, then we can just re-use the existing hash table without
895  * rebuilding it.
896  */
897  if (node->hj_HashTable != NULL)
898  {
899  if (node->hj_HashTable->nbatch == 1 &&
900  node->js.ps.righttree->chgParam == NULL)
901  {
902  /*
903  * Okay to reuse the hash table; needn't rescan inner, either.
904  *
905  * However, if it's a right/full join, we'd better reset the
906  * inner-tuple match flags contained in the table.
907  */
908  if (HJ_FILL_INNER(node))
910 
911  /*
912  * Also, we need to reset our state about the emptiness of the
913  * outer relation, so that the new scan of the outer will update
914  * it correctly if it turns out to be empty this time. (There's no
915  * harm in clearing it now because ExecHashJoin won't need the
916  * info. In the other cases, where the hash table doesn't exist
917  * or we are destroying it, we leave this state alone because
918  * ExecHashJoin will need it the first time through.)
919  */
920  node->hj_OuterNotEmpty = false;
921 
922  /* ExecHashJoin can skip the BUILD_HASHTABLE step */
924  }
925  else
926  {
927  /* must destroy and rebuild hash table */
929  node->hj_HashTable = NULL;
931 
932  /*
933  * if chgParam of subnode is not null then plan will be re-scanned
934  * by first ExecProcNode.
935  */
936  if (node->js.ps.righttree->chgParam == NULL)
937  ExecReScan(node->js.ps.righttree);
938  }
939  }
940 
941  /* Always reset intra-tuple state */
942  node->hj_CurHashValue = 0;
943  node->hj_CurBucketNo = 0;
945  node->hj_CurTuple = NULL;
946 
947  node->hj_MatchedOuter = false;
949 
950  /*
951  * if chgParam of subnode is not null then plan will be re-scanned by
952  * first ExecProcNode.
953  */
954  if (node->js.ps.lefttree->chgParam == NULL)
955  ExecReScan(node->js.ps.lefttree);
956 }
#define INVALID_SKEW_BUCKET_NO
Definition: hashjoin.h:101
PlanState ps
Definition: execnodes.h:1516
void ExecReScan(PlanState *node)
Definition: execAmi.c:74
bool hj_MatchedOuter
Definition: execnodes.h:1631
struct PlanState * righttree
Definition: execnodes.h:816
TupleTableSlot * hj_FirstOuterTupleSlot
Definition: execnodes.h:1629
struct PlanState * lefttree
Definition: execnodes.h:815
#define HJ_FILL_INNER(hjstate)
Definition: nodeHashjoin.c:40
uint32 hj_CurHashValue
Definition: execnodes.h:1621
int hj_CurSkewBucketNo
Definition: execnodes.h:1623
HashJoinTuple hj_CurTuple
Definition: execnodes.h:1624
Bitmapset * chgParam
Definition: execnodes.h:824
int hj_CurBucketNo
Definition: execnodes.h:1622
bool hj_OuterNotEmpty
Definition: execnodes.h:1632
#define NULL
Definition: c.h:229
#define HJ_NEED_NEW_OUTER
Definition: nodeHashjoin.c:31
#define HJ_BUILD_HASHTABLE
Definition: nodeHashjoin.c:30
HashJoinTable hj_HashTable
Definition: execnodes.h:1620
void ExecHashTableResetMatchFlags(HashJoinTable hashtable)
Definition: nodeHash.c:1238
JoinState js
Definition: execnodes.h:1615
void ExecHashTableDestroy(HashJoinTable hashtable)
Definition: nodeHash.c:559