nodeHashjoin.c 19.4 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * nodeHashjoin.c
4
 *	  Routines to handle hash join nodes
5
 *
B
Bruce Momjian 已提交
6
 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
B
Add:  
Bruce Momjian 已提交
7
 * Portions Copyright (c) 1994, Regents of the University of California
8 9 10
 *
 *
 * IDENTIFICATION
11
 *	  $Header: /cvsroot/pgsql/src/backend/executor/nodeHashjoin.c,v 1.52 2003/06/22 22:04:54 tgl Exp $
12 13 14
 *
 *-------------------------------------------------------------------------
 */
15

B
Bruce Momjian 已提交
16
#include "postgres.h"
17 18

#include "executor/executor.h"
19 20
#include "executor/nodeHash.h"
#include "executor/nodeHashjoin.h"
B
Bruce Momjian 已提交
21
#include "optimizer/clauses.h"
22 23
#include "utils/memutils.h"

24

25 26
static TupleTableSlot *ExecHashJoinOuterGetTuple(PlanState *node,
												 HashJoinState *hjstate);
27
static TupleTableSlot *ExecHashJoinGetSavedTuple(HashJoinState *hjstate,
28
						  BufFile *file,
B
Bruce Momjian 已提交
29
						  TupleTableSlot *tupleSlot);
30
static int	ExecHashJoinNewBatch(HashJoinState *hjstate);
31 32


33
/* ----------------------------------------------------------------
34
 *		ExecHashJoin
35
 *
36 37 38 39
 *		This function implements the Hybrid Hashjoin algorithm.
 *		recursive partitioning remains to be added.
 *		Note: the relation we build hash table on is the inner
 *			  the other one is outer.
40 41
 * ----------------------------------------------------------------
 */
42
TupleTableSlot *				/* return: a tuple or NULL */
43
ExecHashJoin(HashJoinState *node)
44
{
45
	EState	   *estate;
46 47
	PlanState  *outerNode;
	HashState  *hashNode;
48
	List	   *hjclauses;
49
	List	   *outerkeys;
50 51
	List	   *joinqual;
	List	   *otherqual;
52
	ScanDirection dir;
53
	TupleTableSlot *inntuple;
54
	ExprContext *econtext;
55
	ExprDoneCond isDone;
56 57
	HashJoinTable hashtable;
	HeapTuple	curtuple;
58
	TupleTableSlot *outerTupleSlot;
59
	int			i;
60

61 62
	/*
	 * get information from HashJoin node
63 64
	 */
	hjclauses = node->hashclauses;
65 66 67 68 69
	estate = node->js.ps.state;
	joinqual = node->js.joinqual;
	otherqual = node->js.ps.qual;
	hashNode = (HashState *) innerPlanState(node);
	outerNode = outerPlanState(node);
70 71
	dir = estate->es_direction;

72
	/*
73
	 * get information from HashJoin state
74
	 */
75 76 77
	hashtable = node->hj_HashTable;
	outerkeys = node->hj_OuterHashKeys;
	econtext = node->js.ps.ps_ExprContext;
78

79 80 81 82
	/*
	 * Check to see if we're still projecting out tuples from a previous
	 * join tuple (because there is a function-returning-set in the
	 * projection expressions).  If so, try to project another one.
83
	 */
84
	if (node->js.ps.ps_TupFromTlist)
85 86 87
	{
		TupleTableSlot *result;

88
		result = ExecProject(node->js.ps.ps_ProjInfo, &isDone);
89
		if (isDone == ExprMultipleResult)
90
			return result;
91
		/* Done with that source tuple... */
92
		node->js.ps.ps_TupFromTlist = false;
93
	}
94

95 96 97 98 99 100 101 102 103
	/*
	 * If we're doing an IN join, we want to return at most one row per
	 * outer tuple; so we can stop scanning the inner scan if we matched on
	 * the previous try.
	 */
	if (node->js.jointype == JOIN_IN && 
		node->hj_MatchedOuter)
		node->hj_NeedNewOuter = true;

104 105 106 107
	/*
	 * Reset per-tuple memory context to free any expression evaluation
	 * storage allocated in the previous tuple cycle.  Note this can't
	 * happen until we're done projecting out tuples from a join tuple.
108 109 110
	 */
	ResetExprContext(econtext);

111 112
	/*
	 * if this is the first call, build the hash table for inner relation
113
	 */
114 115 116 117 118 119
	if (!node->hj_hashdone)
	{
		/*
		 * create the hash table
		 */
		Assert(hashtable == NULL);
120 121
		hashtable = ExecHashTableCreate((Hash *) hashNode->ps.plan,
										node->hj_HashOperators);
122
		node->hj_HashTable = hashtable;
123

124 125 126 127 128
		/*
		 * execute the Hash node, to build the hash table
		 */
		hashNode->hashtable = hashtable;
		(void) ExecProcNode((PlanState *) hashNode);
129 130 131 132

		/*
		 * Open temp files for outer batches, if needed. Note that file
		 * buffers are palloc'd in regular executor context.
133
		 */
134
		for (i = 0; i < hashtable->nbatch; i++)
135
			hashtable->outerBatchFile[i] = BufFileCreateTemp(false);
136 137

		node->hj_hashdone = true;
138
	}
139

140 141
	/*
	 * Now get an outer tuple and probe into the hash table for matches
142
	 */
143
	outerTupleSlot = node->js.ps.ps_OuterTupleSlot;
144

145
	for (;;)
146
	{
147
		/*
148
		 * If we don't have an outer tuple, get the next one
149
		 */
150
		if (node->hj_NeedNewOuter)
151
		{
152
			outerTupleSlot = ExecHashJoinOuterGetTuple(outerNode,
153
													   node);
154
			if (TupIsNull(outerTupleSlot))
155
			{
156
				/* end of join */
157 158 159
				return NULL;
			}

160
			node->js.ps.ps_OuterTupleSlot = outerTupleSlot;
161
			econtext->ecxt_outertuple = outerTupleSlot;
162 163
			node->hj_NeedNewOuter = false;
			node->hj_MatchedOuter = false;
164

165
			/*
B
Bruce Momjian 已提交
166 167
			 * now we have an outer tuple, find the corresponding bucket
			 * for this tuple from the hash table
168
			 */
169
			node->hj_CurBucketNo = ExecHashGetBucket(hashtable, econtext,
170
													 outerkeys);
171
			node->hj_CurTuple = NULL;
172

173 174 175 176
			/*
			 * Now we've got an outer tuple and the corresponding hash
			 * bucket, but this tuple may not belong to the current batch.
			 * This need only be checked in the first pass.
177 178 179
			 */
			if (hashtable->curbatch == 0)
			{
180 181
				int			batchno = ExecHashGetBatch(node->hj_CurBucketNo,
													   hashtable);
B
Bruce Momjian 已提交
182

183
				if (batchno >= 0)
184 185
				{
					/*
186 187
					 * Need to postpone this outer tuple to a later batch.
					 * Save it in the corresponding outer-batch file.
188
					 */
189 190
					hashtable->outerBatchSize[batchno]++;
					ExecHashJoinSaveTuple(outerTupleSlot->val,
B
Bruce Momjian 已提交
191
									 hashtable->outerBatchFile[batchno]);
192
					node->hj_NeedNewOuter = true;
B
Bruce Momjian 已提交
193
					continue;	/* loop around for a new outer tuple */
194 195 196 197
				}
			}
		}

198 199
		/*
		 * OK, scan the selected hash bucket for matches
200
		 */
201
		for (;;)
202
		{
203
			curtuple = ExecScanHashBucket(node,
204 205 206 207
										  hjclauses,
										  econtext);
			if (curtuple == NULL)
				break;			/* out of matches */
B
Bruce Momjian 已提交
208

209
			/*
210
			 * we've got a match, but still need to test non-hashed quals
211
			 */
212
			inntuple = ExecStoreTuple(curtuple,
213
									  node->hj_HashTupleSlot,
214 215 216
									  InvalidBuffer,
									  false);	/* don't pfree this tuple */
			econtext->ecxt_innertuple = inntuple;
217

218
			/* reset temp memory each time to avoid leaks from qual expr */
219 220
			ResetExprContext(econtext);

221 222 223 224
			/*
			 * if we pass the qual, then save state for next call and have
			 * ExecProject form the projection, store it in the tuple
			 * table, and return the slot.
225
			 *
226 227
			 * Only the joinquals determine MatchedOuter status, but all
			 * quals must pass to actually return the tuple.
228
			 */
229
			if (ExecQual(joinqual, econtext, false))
230
			{
231
				node->hj_MatchedOuter = true;
232

233
				if (otherqual == NIL || ExecQual(otherqual, econtext, false))
234
				{
235 236
					TupleTableSlot *result;

237
					result = ExecProject(node->js.ps.ps_ProjInfo, &isDone);
238 239 240

					if (isDone != ExprEndResult)
					{
241
						node->js.ps.ps_TupFromTlist =
242 243 244
							(isDone == ExprMultipleResult);
						return result;
					}
245
				}
246 247 248 249 250 251 252

				/* If we didn't return a tuple, may need to set NeedNewOuter */
				if (node->js.jointype == JOIN_IN)
				{
					node->hj_NeedNewOuter = true;
					break;		/* out of loop over hash bucket */
				}
253 254 255
			}
		}

256 257 258 259
		/*
		 * Now the current outer tuple has run out of matches, so check
		 * whether to emit a dummy outer-join tuple. If not, loop around
		 * to get a new outer tuple.
260
		 */
261
		node->hj_NeedNewOuter = true;
262

263 264
		if (!node->hj_MatchedOuter &&
			node->js.jointype == JOIN_LEFT)
265 266 267 268
		{
			/*
			 * We are doing an outer join and there were no join matches
			 * for this outer tuple.  Generate a fake join tuple with
B
Bruce Momjian 已提交
269 270
			 * nulls for the inner tuple, and return it if it passes the
			 * non-join quals.
271
			 */
272
			econtext->ecxt_innertuple = node->hj_NullInnerTupleSlot;
273 274 275

			if (ExecQual(otherqual, econtext, false))
			{
276 277 278 279
				/*
				 * qualification was satisfied so we project and return
				 * the slot containing the result tuple using
				 * ExecProject().
280 281 282
				 */
				TupleTableSlot *result;

283
				result = ExecProject(node->js.ps.ps_ProjInfo, &isDone);
284 285 286

				if (isDone != ExprEndResult)
				{
287
					node->js.ps.ps_TupFromTlist =
288 289 290 291 292
						(isDone == ExprMultipleResult);
					return result;
				}
			}
		}
293
	}
294 295 296
}

/* ----------------------------------------------------------------
297
 *		ExecInitHashJoin
298
 *
299
 *		Init routine for HashJoin node.
300 301
 * ----------------------------------------------------------------
 */
302 303
HashJoinState *
ExecInitHashJoin(HashJoin *node, EState *estate)
304
{
305 306 307
	HashJoinState *hjstate;
	Plan	   *outerNode;
	Hash	   *hashNode;
308
	List	   *hclauses;
309
	List	   *hoperators;
310
	List	   *hcl;
311

312
	/*
313 314 315
	 * create state structure
	 */
	hjstate = makeNode(HashJoinState);
316 317
	hjstate->js.ps.plan = (Plan *) node;
	hjstate->js.ps.state = estate;
318

319 320
	/*
	 * Miscellaneous initialization
321
	 *
322
	 * create expression context for node
323
	 */
324 325 326 327 328 329
	ExecAssignExprContext(estate, &hjstate->js.ps);

	/*
	 * initialize child expressions
	 */
	hjstate->js.ps.targetlist = (List *)
330
		ExecInitExpr((Expr *) node->join.plan.targetlist,
331 332
					 (PlanState *) hjstate);
	hjstate->js.ps.qual = (List *)
333
		ExecInitExpr((Expr *) node->join.plan.qual,
334 335 336
					 (PlanState *) hjstate);
	hjstate->js.jointype = node->join.jointype;
	hjstate->js.joinqual = (List *)
337
		ExecInitExpr((Expr *) node->join.joinqual,
338 339
					 (PlanState *) hjstate);
	hjstate->hashclauses = (List *)
340
		ExecInitExpr((Expr *) node->hashclauses,
341
					 (PlanState *) hjstate);
342

343
	/*
344
	 * initialize child nodes
345
	 */
346 347
	outerNode = outerPlan(node);
	hashNode = (Hash *) innerPlan(node);
348

349 350
	outerPlanState(hjstate) = ExecInitNode(outerNode, estate);
	innerPlanState(hjstate) = ExecInitNode((Plan *) hashNode, estate);
351

352
#define HASHJOIN_NSLOTS 3
353 354 355

	/*
	 * tuple table initialization
356
	 */
357
	ExecInitResultTupleSlot(estate, &hjstate->js.ps);
358 359 360 361 362
	hjstate->hj_OuterTupleSlot = ExecInitExtraTupleSlot(estate);

	switch (node->join.jointype)
	{
		case JOIN_INNER:
363
		case JOIN_IN:
364 365 366 367
			break;
		case JOIN_LEFT:
			hjstate->hj_NullInnerTupleSlot =
				ExecInitNullTupleSlot(estate,
368
									  ExecGetResultType(innerPlanState(hjstate)));
369 370 371 372 373 374
			break;
		default:
			elog(ERROR, "ExecInitHashJoin: unsupported join type %d",
				 (int) node->join.jointype);
	}

375 376 377 378 379 380
	/*
	 * now for some voodoo.  our temporary tuple slot is actually the
	 * result tuple slot of the Hash node (which is our inner plan).  we
	 * do this because Hash nodes don't return tuples via ExecProcNode()
	 * -- instead the hash join node uses ExecScanHashBucket() to get at
	 * the contents of the hash table.	-cim 6/9/91
381 382
	 */
	{
383 384
		HashState  *hashstate = (HashState *) innerPlanState(hjstate);
		TupleTableSlot *slot = hashstate->ps.ps_ResultTupleSlot;
385 386 387 388

		hjstate->hj_HashTupleSlot = slot;
	}

389 390
	/*
	 * initialize tuple type and projection info
391
	 */
392 393
	ExecAssignResultTypeFromTL(&hjstate->js.ps);
	ExecAssignProjectionInfo(&hjstate->js.ps);
394

395
	ExecSetSlotDescriptor(hjstate->hj_OuterTupleSlot,
396
						  ExecGetResultType(outerPlanState(hjstate)),
397
						  false);
398

399 400
	/*
	 * initialize hash-specific info
401 402
	 */

403
	hjstate->hj_hashdone = false;
404
	hjstate->hj_HashTable = (HashJoinTable) NULL;
405

406 407
	hjstate->hj_CurBucketNo = 0;
	hjstate->hj_CurTuple = (HashJoinTuple) NULL;
408 409 410

	/*
	 * The planner already made a list of the inner hashkeys for us,
411 412 413
	 * but we also need a list of the outer hashkeys, as well as a list
	 * of the hash operator OIDs.  Both lists of exprs must then be prepared
	 * for execution.
414
	 */
415 416 417 418 419 420 421
	hjstate->hj_InnerHashKeys = (List *)
		ExecInitExpr((Expr *) hashNode->hashkeys,
					 innerPlanState(hjstate));
	((HashState *) innerPlanState(hjstate))->hashkeys =
		hjstate->hj_InnerHashKeys;

	hclauses = NIL;
422
	hoperators = NIL;
423 424
	foreach(hcl, node->hashclauses)
	{
425 426 427 428 429
		OpExpr	   *hclause = (OpExpr *) lfirst(hcl);

		Assert(IsA(hclause, OpExpr));
		hclauses = lappend(hclauses, get_leftop((Expr *) hclause));
		hoperators = lappendo(hoperators, hclause->opno);
430
	}
431 432 433
	hjstate->hj_OuterHashKeys = (List *)
		ExecInitExpr((Expr *) hclauses,
					 (PlanState *) hjstate);
434
	hjstate->hj_HashOperators = hoperators;
435

436 437
	hjstate->js.ps.ps_OuterTupleSlot = NULL;
	hjstate->js.ps.ps_TupFromTlist = false;
438 439
	hjstate->hj_NeedNewOuter = true;
	hjstate->hj_MatchedOuter = false;
440

441
	return hjstate;
442 443 444
}

int
445
ExecCountSlotsHashJoin(HashJoin *node)
446
{
447
	return ExecCountSlotsNode(outerPlan(node)) +
448 449
		ExecCountSlotsNode(innerPlan(node)) +
		HASHJOIN_NSLOTS;
450 451 452
}

/* ----------------------------------------------------------------
453
 *		ExecEndHashJoin
454
 *
455
 *		clean up routine for HashJoin node
456 457 458
 * ----------------------------------------------------------------
 */
void
459
ExecEndHashJoin(HashJoinState *node)
460
{
461
	/*
462
	 * Free hash table
463
	 */
464
	if (node->hj_HashTable)
465
	{
466 467
		ExecHashTableDestroy(node->hj_HashTable);
		node->hj_HashTable = NULL;
468 469
	}

470
	/*
471
	 * Free the exprcontext
472
	 */
473
	ExecFreeExprContext(&node->js.ps);
474

475 476
	/*
	 * clean out the tuple table
477
	 */
478 479 480
	ExecClearTuple(node->js.ps.ps_ResultTupleSlot);
	ExecClearTuple(node->hj_OuterTupleSlot);
	ExecClearTuple(node->hj_HashTupleSlot);
481

482 483 484 485 486
	/*
	 * clean up subtrees
	 */
	ExecEndNode(outerPlanState(node));
	ExecEndNode(innerPlanState(node));
487 488 489
}

/* ----------------------------------------------------------------
490
 *		ExecHashJoinOuterGetTuple
491
 *
492 493 494
 *		get the next outer tuple for hashjoin: either by
 *		executing a plan node as in the first pass, or from
 *		the tmp files for the hashjoin batches.
495 496 497 498
 * ----------------------------------------------------------------
 */

static TupleTableSlot *
499
ExecHashJoinOuterGetTuple(PlanState *node, HashJoinState *hjstate)
500
{
B
Bruce Momjian 已提交
501 502
	HashJoinTable hashtable = hjstate->hj_HashTable;
	int			curbatch = hashtable->curbatch;
503 504 505 506
	TupleTableSlot *slot;

	if (curbatch == 0)
	{							/* if it is the first pass */
507
		slot = ExecProcNode(node);
B
Bruce Momjian 已提交
508
		if (!TupIsNull(slot))
509
			return slot;
B
Bruce Momjian 已提交
510

511
		/*
B
Bruce Momjian 已提交
512 513
		 * We have just reached the end of the first pass. Try to switch
		 * to a saved batch.
514 515
		 */
		curbatch = ExecHashJoinNewBatch(hjstate);
516 517 518
	}

	/*
B
Bruce Momjian 已提交
519 520
	 * Try to read from a temp file. Loop allows us to advance to new
	 * batch as needed.
521
	 */
522 523 524
	while (curbatch <= hashtable->nbatch)
	{
		slot = ExecHashJoinGetSavedTuple(hjstate,
B
Bruce Momjian 已提交
525
								 hashtable->outerBatchFile[curbatch - 1],
526
										 hjstate->hj_OuterTupleSlot);
B
Bruce Momjian 已提交
527
		if (!TupIsNull(slot))
528 529 530 531 532 533
			return slot;
		curbatch = ExecHashJoinNewBatch(hjstate);
	}

	/* Out of batches... */
	return NULL;
534 535 536
}

/* ----------------------------------------------------------------
537
 *		ExecHashJoinGetSavedTuple
538
 *
539
 *		read the next tuple from a tmp file
540 541 542 543
 * ----------------------------------------------------------------
 */

static TupleTableSlot *
544
ExecHashJoinGetSavedTuple(HashJoinState *hjstate,
545
						  BufFile *file,
546
						  TupleTableSlot *tupleSlot)
547
{
B
Bruce Momjian 已提交
548 549 550
	HeapTupleData htup;
	size_t		nread;
	HeapTuple	heapTuple;
551 552 553 554 555 556 557 558

	nread = BufFileRead(file, (void *) &htup, sizeof(HeapTupleData));
	if (nread == 0)
		return NULL;			/* end of file */
	if (nread != sizeof(HeapTupleData))
		elog(ERROR, "Read from hashjoin temp file failed");
	heapTuple = palloc(HEAPTUPLESIZE + htup.t_len);
	memcpy((char *) heapTuple, (char *) &htup, sizeof(HeapTupleData));
559
	heapTuple->t_datamcxt = CurrentMemoryContext;
B
Bruce Momjian 已提交
560 561
	heapTuple->t_data = (HeapTupleHeader)
		((char *) heapTuple + HEAPTUPLESIZE);
562 563 564 565
	nread = BufFileRead(file, (void *) heapTuple->t_data, htup.t_len);
	if (nread != (size_t) htup.t_len)
		elog(ERROR, "Read from hashjoin temp file failed");
	return ExecStoreTuple(heapTuple, tupleSlot, InvalidBuffer, true);
566 567 568
}

/* ----------------------------------------------------------------
569
 *		ExecHashJoinNewBatch
570
 *
571
 *		switch to a new hashjoin batch
572 573
 * ----------------------------------------------------------------
 */
574
static int
575
ExecHashJoinNewBatch(HashJoinState *hjstate)
576
{
577 578 579 580 581
	HashJoinTable hashtable = hjstate->hj_HashTable;
	int			nbatch = hashtable->nbatch;
	int			newbatch = hashtable->curbatch + 1;
	long	   *innerBatchSize = hashtable->innerBatchSize;
	long	   *outerBatchSize = hashtable->outerBatchSize;
B
Bruce Momjian 已提交
582
	BufFile    *innerFile;
583
	TupleTableSlot *slot;
584
	ExprContext *econtext;
585
	List	   *innerhashkeys;
586 587 588 589

	if (newbatch > 1)
	{
		/*
B
Bruce Momjian 已提交
590 591
		 * We no longer need the previous outer batch file; close it right
		 * away to free disk space.
592
		 */
593 594
		BufFileClose(hashtable->outerBatchFile[newbatch - 2]);
		hashtable->outerBatchFile[newbatch - 2] = NULL;
595 596
	}

597 598 599
	/*
	 * We can skip over any batches that are empty on either side. Release
	 * associated temp files right away.
600
	 */
601 602 603
	while (newbatch <= nbatch &&
		   (innerBatchSize[newbatch - 1] == 0L ||
			outerBatchSize[newbatch - 1] == 0L))
604
	{
605 606 607 608
		BufFileClose(hashtable->innerBatchFile[newbatch - 1]);
		hashtable->innerBatchFile[newbatch - 1] = NULL;
		BufFileClose(hashtable->outerBatchFile[newbatch - 1]);
		hashtable->outerBatchFile[newbatch - 1] = NULL;
609
		newbatch++;
610
	}
611

612
	if (newbatch > nbatch)
613
		return newbatch;		/* no more batches */
614

615
	/*
B
Bruce Momjian 已提交
616 617
	 * Rewind inner and outer batch files for this batch, so that we can
	 * start reading them.
618
	 */
619
	if (BufFileSeek(hashtable->outerBatchFile[newbatch - 1], 0, 0L, SEEK_SET))
620 621 622
		elog(ERROR, "Failed to rewind hash temp file");

	innerFile = hashtable->innerBatchFile[newbatch - 1];
623

624
	if (BufFileSeek(innerFile, 0, 0L, SEEK_SET))
625 626 627 628 629 630
		elog(ERROR, "Failed to rewind hash temp file");

	/*
	 * Reload the hash table with the new inner batch
	 */
	ExecHashTableReset(hashtable, innerBatchSize[newbatch - 1]);
631

632
	econtext = hjstate->js.ps.ps_ExprContext;
633
	innerhashkeys = hjstate->hj_InnerHashKeys;
634 635

	while ((slot = ExecHashJoinGetSavedTuple(hjstate,
636 637
											 innerFile,
											 hjstate->hj_HashTupleSlot))
638 639 640
		   && !TupIsNull(slot))
	{
		econtext->ecxt_innertuple = slot;
641
		ExecHashTableInsert(hashtable, econtext, innerhashkeys);
642 643 644
	}

	/*
B
Bruce Momjian 已提交
645 646
	 * after we build the hash table, the inner batch file is no longer
	 * needed
647
	 */
648 649
	BufFileClose(innerFile);
	hashtable->innerBatchFile[newbatch - 1] = NULL;
650

651
	hashtable->curbatch = newbatch;
652 653 654 655
	return newbatch;
}

/* ----------------------------------------------------------------
656
 *		ExecHashJoinSaveTuple
657
 *
658 659 660 661 662
 *		save a tuple to a tmp file.
 *
 * The data recorded in the file for each tuple is an image of its
 * HeapTupleData (with meaningless t_data pointer) followed by the
 * HeapTupleHeader and tuple data.
663 664 665
 * ----------------------------------------------------------------
 */

666
void
667
ExecHashJoinSaveTuple(HeapTuple heapTuple,
668
					  BufFile *file)
669
{
B
Bruce Momjian 已提交
670
	size_t		written;
671 672 673 674 675 676 677

	written = BufFileWrite(file, (void *) heapTuple, sizeof(HeapTupleData));
	if (written != sizeof(HeapTupleData))
		elog(ERROR, "Write to hashjoin temp file failed");
	written = BufFileWrite(file, (void *) heapTuple->t_data, heapTuple->t_len);
	if (written != (size_t) heapTuple->t_len)
		elog(ERROR, "Write to hashjoin temp file failed");
678
}
V
Vadim B. Mikheev 已提交
679 680

void
681
ExecReScanHashJoin(HashJoinState *node, ExprContext *exprCtxt)
V
Vadim B. Mikheev 已提交
682
{
683 684 685 686
	/*
	 * If we haven't yet built the hash table then we can just return;
	 * nothing done yet, so nothing to undo.
	 */
687
	if (!node->hj_hashdone)
V
Vadim B. Mikheev 已提交
688
		return;
689
	Assert(node->hj_HashTable != NULL);
690 691

	/*
692 693 694 695 696
	 * In a multi-batch join, we currently have to do rescans the hard way,
	 * primarily because batch temp files may have already been released.
	 * But if it's a single-batch join, and there is no parameter change
	 * for the inner subnode, then we can just re-use the existing hash
	 * table without rebuilding it.
V
Vadim B. Mikheev 已提交
697
	 */
698 699 700 701 702 703
	if (node->hj_HashTable->nbatch == 0 &&
		((PlanState *) node)->righttree->chgParam == NULL)
	{
		/* okay to reuse the hash table; needn't rescan inner, either */
	}
	else
V
Vadim B. Mikheev 已提交
704
	{
705 706
		/* must destroy and rebuild hash table */
		node->hj_hashdone = false;
707 708
		ExecHashTableDestroy(node->hj_HashTable);
		node->hj_HashTable = NULL;
709 710 711 712 713 714
		/*
		 * if chgParam of subnode is not null then plan will be re-scanned
		 * by first ExecProcNode.
		 */
		if (((PlanState *) node)->righttree->chgParam == NULL)
			ExecReScan(((PlanState *) node)->righttree, exprCtxt);
V
Vadim B. Mikheev 已提交
715
	}
716

717
	/* Always reset intra-tuple state */
718 719
	node->hj_CurBucketNo = 0;
	node->hj_CurTuple = (HashJoinTuple) NULL;
V
Vadim B. Mikheev 已提交
720

721 722 723 724
	node->js.ps.ps_OuterTupleSlot = (TupleTableSlot *) NULL;
	node->js.ps.ps_TupFromTlist = false;
	node->hj_NeedNewOuter = true;
	node->hj_MatchedOuter = false;
725 726

	/*
727
	 * if chgParam of subnode is not null then plan will be re-scanned
728
	 * by first ExecProcNode.
V
Vadim B. Mikheev 已提交
729
	 */
730 731
	if (((PlanState *) node)->lefttree->chgParam == NULL)
		ExecReScan(((PlanState *) node)->lefttree, exprCtxt);
V
Vadim B. Mikheev 已提交
732
}