Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * nodeMergejoin.c
4 : * routines supporting merge joins
5 : *
6 : * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
7 : * Portions Copyright (c) 1994, Regents of the University of California
8 : *
9 : *
10 : * IDENTIFICATION
11 : * src/backend/executor/nodeMergejoin.c
12 : *
13 : *-------------------------------------------------------------------------
14 : */
15 : /*
16 : * INTERFACE ROUTINES
17 : * ExecMergeJoin mergejoin outer and inner relations.
18 : * ExecInitMergeJoin creates and initializes run time states
19 : * ExecEndMergeJoin cleans up the node.
20 : *
21 : * NOTES
22 : *
23 : * Merge-join is done by joining the inner and outer tuples satisfying
24 : * join clauses of the form ((= outerKey innerKey) ...).
25 : * The join clause list is provided by the query planner and may contain
26 : * more than one (= outerKey innerKey) clause (for composite sort key).
27 : *
28 : * However, the query executor needs to know whether an outer
29 : * tuple is "greater/smaller" than an inner tuple so that it can
30 : * "synchronize" the two relations. For example, consider the following
31 : * relations:
32 : *
33 : * outer: (0 ^1 1 2 5 5 5 6 6 7) current tuple: 1
34 : * inner: (1 ^3 5 5 5 5 6) current tuple: 3
35 : *
36 : * To continue the merge-join, the executor needs to scan both inner
37 : * and outer relations till the matching tuples 5. It needs to know
38 : * that currently inner tuple 3 is "greater" than outer tuple 1 and
39 : * therefore it should scan the outer relation first to find a
40 : * matching tuple and so on.
41 : *
42 : * Therefore, rather than directly executing the merge join clauses,
43 : * we evaluate the left and right key expressions separately and then
44 : * compare the columns one at a time (see MJCompare). The planner
45 : * passes us enough information about the sort ordering of the inputs
46 : * to allow us to determine how to make the comparison. We may use the
47 : * appropriate btree comparison function, since Postgres' only notion
48 : * of ordering is specified by btree opfamilies.
49 : *
50 : *
51 : * Consider the above relations and suppose that the executor has
52 : * just joined the first outer "5" with the last inner "5". The
53 : * next step is of course to join the second outer "5" with all
54 : * the inner "5's". This requires repositioning the inner "cursor"
55 : * to point at the first inner "5". This is done by "marking" the
56 : * first inner 5 so we can restore the "cursor" to it before joining
57 : * with the second outer 5. The access method interface provides
58 : * routines to mark and restore to a tuple.
59 : *
60 : *
61 : * Essential operation of the merge join algorithm is as follows:
62 : *
63 : * Join {
64 : * get initial outer and inner tuples INITIALIZE
65 : * do forever {
66 : * while (outer != inner) { SKIP_TEST
67 : * if (outer < inner)
68 : * advance outer SKIPOUTER_ADVANCE
69 : * else
70 : * advance inner SKIPINNER_ADVANCE
71 : * }
72 : * mark inner position SKIP_TEST
73 : * do forever {
74 : * while (outer == inner) {
75 : * join tuples JOINTUPLES
76 : * advance inner position NEXTINNER
77 : * }
78 : * advance outer position NEXTOUTER
79 : * if (outer == mark) TESTOUTER
80 : * restore inner position to mark TESTOUTER
81 : * else
82 : * break // return to top of outer loop
83 : * }
84 : * }
85 : * }
86 : *
87 : * The merge join operation is coded in the fashion
88 : * of a state machine. At each state, we do something and then
89 : * proceed to another state. This state is stored in the node's
90 : * execution state information and is preserved across calls to
91 : * ExecMergeJoin. -cim 10/31/89
92 : */
93 : #include "postgres.h"
94 :
95 : #include "access/nbtree.h"
96 : #include "executor/execdebug.h"
97 : #include "executor/nodeMergejoin.h"
98 : #include "miscadmin.h"
99 : #include "utils/lsyscache.h"
100 : #include "utils/memutils.h"
101 :
102 :
103 : /*
104 : * States of the ExecMergeJoin state machine
105 : */
106 : #define EXEC_MJ_INITIALIZE_OUTER 1
107 : #define EXEC_MJ_INITIALIZE_INNER 2
108 : #define EXEC_MJ_JOINTUPLES 3
109 : #define EXEC_MJ_NEXTOUTER 4
110 : #define EXEC_MJ_TESTOUTER 5
111 : #define EXEC_MJ_NEXTINNER 6
112 : #define EXEC_MJ_SKIP_TEST 7
113 : #define EXEC_MJ_SKIPOUTER_ADVANCE 8
114 : #define EXEC_MJ_SKIPINNER_ADVANCE 9
115 : #define EXEC_MJ_ENDOUTER 10
116 : #define EXEC_MJ_ENDINNER 11
117 :
118 : /*
119 : * Runtime data for each mergejoin clause
120 : */
121 : typedef struct MergeJoinClauseData
122 : {
123 : /* Executable expression trees */
124 : ExprState *lexpr; /* left-hand (outer) input expression */
125 : ExprState *rexpr; /* right-hand (inner) input expression */
126 :
127 : /*
128 : * If we have a current left or right input tuple, the values of the
129 : * expressions are loaded into these fields:
130 : */
131 : Datum ldatum; /* current left-hand value */
132 : Datum rdatum; /* current right-hand value */
133 : bool lisnull; /* and their isnull flags */
134 : bool risnull;
135 :
136 : /*
137 : * Everything we need to know to compare the left and right values is
138 : * stored here.
139 : */
140 : SortSupportData ssup;
141 : } MergeJoinClauseData;
142 :
143 : /* Result type for MJEvalOuterValues and MJEvalInnerValues */
144 : typedef enum
145 : {
146 : MJEVAL_MATCHABLE, /* normal, potentially matchable tuple */
147 : MJEVAL_NONMATCHABLE, /* tuple cannot join because it has a null */
148 : MJEVAL_ENDOFJOIN /* end of input (physical or effective) */
149 : } MJEvalResult;
150 :
151 :
152 : #define MarkInnerTuple(innerTupleSlot, mergestate) \
153 : ExecCopySlot((mergestate)->mj_MarkedTupleSlot, (innerTupleSlot))
154 :
155 :
156 : /*
157 : * MJExamineQuals
158 : *
159 : * This deconstructs the list of mergejoinable expressions, which is given
160 : * to us by the planner in the form of a list of "leftexpr = rightexpr"
161 : * expression trees in the order matching the sort columns of the inputs.
162 : * We build an array of MergeJoinClause structs containing the information
163 : * we will need at runtime. Each struct essentially tells us how to compare
164 : * the two expressions from the original clause.
165 : *
166 : * In addition to the expressions themselves, the planner passes the btree
167 : * opfamily OID, collation OID, btree strategy number (BTLessStrategyNumber or
168 : * BTGreaterStrategyNumber), and nulls-first flag that identify the intended
169 : * sort ordering for each merge key. The mergejoinable operator is an
170 : * equality operator in the opfamily, and the two inputs are guaranteed to be
171 : * ordered in either increasing or decreasing (respectively) order according
172 : * to the opfamily and collation, with nulls at the indicated end of the range.
173 : * This allows us to obtain the needed comparison function from the opfamily.
174 : */
175 : static MergeJoinClause
176 168 : MJExamineQuals(List *mergeclauses,
177 : Oid *mergefamilies,
178 : Oid *mergecollations,
179 : int *mergestrategies,
180 : bool *mergenullsfirst,
181 : PlanState *parent)
182 : {
183 : MergeJoinClause clauses;
184 168 : int nClauses = list_length(mergeclauses);
185 : int iClause;
186 : ListCell *cl;
187 :
188 168 : clauses = (MergeJoinClause) palloc0(nClauses * sizeof(MergeJoinClauseData));
189 :
190 168 : iClause = 0;
191 341 : foreach(cl, mergeclauses)
192 : {
193 173 : OpExpr *qual = (OpExpr *) lfirst(cl);
194 173 : MergeJoinClause clause = &clauses[iClause];
195 173 : Oid opfamily = mergefamilies[iClause];
196 173 : Oid collation = mergecollations[iClause];
197 173 : StrategyNumber opstrategy = mergestrategies[iClause];
198 173 : bool nulls_first = mergenullsfirst[iClause];
199 : int op_strategy;
200 : Oid op_lefttype;
201 : Oid op_righttype;
202 : Oid sortfunc;
203 :
204 173 : if (!IsA(qual, OpExpr))
205 0 : elog(ERROR, "mergejoin clause is not an OpExpr");
206 :
207 : /*
208 : * Prepare the input expressions for execution.
209 : */
210 173 : clause->lexpr = ExecInitExpr((Expr *) linitial(qual->args), parent);
211 173 : clause->rexpr = ExecInitExpr((Expr *) lsecond(qual->args), parent);
212 :
213 : /* Set up sort support data */
214 173 : clause->ssup.ssup_cxt = CurrentMemoryContext;
215 173 : clause->ssup.ssup_collation = collation;
216 173 : if (opstrategy == BTLessStrategyNumber)
217 173 : clause->ssup.ssup_reverse = false;
218 0 : else if (opstrategy == BTGreaterStrategyNumber)
219 0 : clause->ssup.ssup_reverse = true;
220 : else /* planner screwed up */
221 0 : elog(ERROR, "unsupported mergejoin strategy %d", opstrategy);
222 173 : clause->ssup.ssup_nulls_first = nulls_first;
223 :
224 : /* Extract the operator's declared left/right datatypes */
225 173 : get_op_opfamily_properties(qual->opno, opfamily, false,
226 : &op_strategy,
227 : &op_lefttype,
228 : &op_righttype);
229 173 : if (op_strategy != BTEqualStrategyNumber) /* should not happen */
230 0 : elog(ERROR, "cannot merge using non-equality operator %u",
231 : qual->opno);
232 :
233 : /*
234 : * sortsupport routine must know if abbreviation optimization is
235 : * applicable in principle. It is never applicable for merge joins
236 : * because there is no convenient opportunity to convert to
237 : * alternative representation.
238 : */
239 173 : clause->ssup.abbreviate = false;
240 :
241 : /* And get the matching support or comparison function */
242 173 : Assert(clause->ssup.comparator == NULL);
243 173 : sortfunc = get_opfamily_proc(opfamily,
244 : op_lefttype,
245 : op_righttype,
246 : BTSORTSUPPORT_PROC);
247 173 : if (OidIsValid(sortfunc))
248 : {
249 : /* The sort support function can provide a comparator */
250 163 : OidFunctionCall1(sortfunc, PointerGetDatum(&clause->ssup));
251 : }
252 173 : if (clause->ssup.comparator == NULL)
253 : {
254 : /* support not available, get comparison func */
255 10 : sortfunc = get_opfamily_proc(opfamily,
256 : op_lefttype,
257 : op_righttype,
258 : BTORDER_PROC);
259 10 : if (!OidIsValid(sortfunc)) /* should not happen */
260 0 : elog(ERROR, "missing support function %d(%u,%u) in opfamily %u",
261 : BTORDER_PROC, op_lefttype, op_righttype, opfamily);
262 : /* We'll use a shim to call the old-style btree comparator */
263 10 : PrepareSortSupportComparisonShim(sortfunc, &clause->ssup);
264 : }
265 :
266 173 : iClause++;
267 : }
268 :
269 168 : return clauses;
270 : }
271 :
272 : /*
273 : * MJEvalOuterValues
274 : *
275 : * Compute the values of the mergejoined expressions for the current
276 : * outer tuple. We also detect whether it's impossible for the current
277 : * outer tuple to match anything --- this is true if it yields a NULL
278 : * input, since we assume mergejoin operators are strict. If the NULL
279 : * is in the first join column, and that column sorts nulls last, then
280 : * we can further conclude that no following tuple can match anything
281 : * either, since they must all have nulls in the first column. However,
282 : * that case is only interesting if we're not in FillOuter mode, else
283 : * we have to visit all the tuples anyway.
284 : *
285 : * For the convenience of callers, we also make this routine responsible
286 : * for testing for end-of-input (null outer tuple), and returning
287 : * MJEVAL_ENDOFJOIN when that's seen. This allows the same code to be used
288 : * for both real end-of-input and the effective end-of-input represented by
289 : * a first-column NULL.
290 : *
291 : * We evaluate the values in OuterEContext, which can be reset each
292 : * time we move to a new tuple.
293 : */
294 : static MJEvalResult
295 64440 : MJEvalOuterValues(MergeJoinState *mergestate)
296 : {
297 64440 : ExprContext *econtext = mergestate->mj_OuterEContext;
298 64440 : MJEvalResult result = MJEVAL_MATCHABLE;
299 : int i;
300 : MemoryContext oldContext;
301 :
302 : /* Check for end of outer subplan */
303 64440 : if (TupIsNull(mergestate->mj_OuterTupleSlot))
304 105 : return MJEVAL_ENDOFJOIN;
305 :
306 64335 : ResetExprContext(econtext);
307 :
308 64335 : oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
309 :
310 64335 : econtext->ecxt_outertuple = mergestate->mj_OuterTupleSlot;
311 :
312 132027 : for (i = 0; i < mergestate->mj_NumClauses; i++)
313 : {
314 67692 : MergeJoinClause clause = &mergestate->mj_Clauses[i];
315 :
316 67692 : clause->ldatum = ExecEvalExpr(clause->lexpr, econtext,
317 : &clause->lisnull);
318 67692 : if (clause->lisnull)
319 : {
320 : /* match is impossible; can we end the join early? */
321 4 : if (i == 0 && !clause->ssup.ssup_nulls_first &&
322 2 : !mergestate->mj_FillOuter)
323 0 : result = MJEVAL_ENDOFJOIN;
324 2 : else if (result == MJEVAL_MATCHABLE)
325 2 : result = MJEVAL_NONMATCHABLE;
326 : }
327 : }
328 :
329 64335 : MemoryContextSwitchTo(oldContext);
330 :
331 64335 : return result;
332 : }
333 :
334 : /*
335 : * MJEvalInnerValues
336 : *
337 : * Same as above, but for the inner tuple. Here, we have to be prepared
338 : * to load data from either the true current inner, or the marked inner,
339 : * so caller must tell us which slot to load from.
340 : */
341 : static MJEvalResult
342 217661 : MJEvalInnerValues(MergeJoinState *mergestate, TupleTableSlot *innerslot)
343 : {
344 217661 : ExprContext *econtext = mergestate->mj_InnerEContext;
345 217661 : MJEvalResult result = MJEVAL_MATCHABLE;
346 : int i;
347 : MemoryContext oldContext;
348 :
349 : /* Check for end of inner subplan */
350 217661 : if (TupIsNull(innerslot))
351 150 : return MJEVAL_ENDOFJOIN;
352 :
353 217511 : ResetExprContext(econtext);
354 :
355 217511 : oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
356 :
357 217511 : econtext->ecxt_innertuple = innerslot;
358 :
359 445071 : for (i = 0; i < mergestate->mj_NumClauses; i++)
360 : {
361 227560 : MergeJoinClause clause = &mergestate->mj_Clauses[i];
362 :
363 227560 : clause->rdatum = ExecEvalExpr(clause->rexpr, econtext,
364 : &clause->risnull);
365 227560 : if (clause->risnull)
366 : {
367 : /* match is impossible; can we end the join early? */
368 52 : if (i == 0 && !clause->ssup.ssup_nulls_first &&
369 26 : !mergestate->mj_FillInner)
370 14 : result = MJEVAL_ENDOFJOIN;
371 12 : else if (result == MJEVAL_MATCHABLE)
372 12 : result = MJEVAL_NONMATCHABLE;
373 : }
374 : }
375 :
376 217511 : MemoryContextSwitchTo(oldContext);
377 :
378 217511 : return result;
379 : }
380 :
381 : /*
382 : * MJCompare
383 : *
384 : * Compare the mergejoinable values of the current two input tuples
385 : * and return 0 if they are equal (ie, the mergejoin equalities all
386 : * succeed), >0 if outer > inner, <0 if outer < inner.
387 : *
388 : * MJEvalOuterValues and MJEvalInnerValues must already have been called
389 : * for the current outer and inner tuples, respectively.
390 : */
391 : static int
392 217576 : MJCompare(MergeJoinState *mergestate)
393 : {
394 217576 : int result = 0;
395 217576 : bool nulleqnull = false;
396 217576 : ExprContext *econtext = mergestate->js.ps.ps_ExprContext;
397 : int i;
398 : MemoryContext oldContext;
399 :
400 : /*
401 : * Call the comparison functions in short-lived context, in case they leak
402 : * memory.
403 : */
404 217576 : ResetExprContext(econtext);
405 :
406 217576 : oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
407 :
408 312478 : for (i = 0; i < mergestate->mj_NumClauses; i++)
409 : {
410 225977 : MergeJoinClause clause = &mergestate->mj_Clauses[i];
411 :
412 : /*
413 : * Special case for NULL-vs-NULL, else use standard comparison.
414 : */
415 225977 : if (clause->lisnull && clause->risnull)
416 : {
417 0 : nulleqnull = true; /* NULL "=" NULL */
418 0 : continue;
419 : }
420 :
421 225977 : result = ApplySortComparator(clause->ldatum, clause->lisnull,
422 225977 : clause->rdatum, clause->risnull,
423 225977 : &clause->ssup);
424 :
425 225977 : if (result != 0)
426 131075 : break;
427 : }
428 :
429 : /*
430 : * If we had any NULL-vs-NULL inputs, we do not want to report that the
431 : * tuples are equal. Instead, if result is still 0, change it to +1. This
432 : * will result in advancing the inner side of the join.
433 : *
434 : * Likewise, if there was a constant-false joinqual, do not report
435 : * equality. We have to check this as part of the mergequals, else the
436 : * rescan logic will do the wrong thing.
437 : */
438 217576 : if (result == 0 &&
439 86501 : (nulleqnull || mergestate->mj_ConstFalseJoin))
440 5 : result = 1;
441 :
442 217576 : MemoryContextSwitchTo(oldContext);
443 :
444 217576 : return result;
445 : }
446 :
447 :
448 : /*
449 : * Generate a fake join tuple with nulls for the inner tuple,
450 : * and return it if it passes the non-join quals.
451 : */
452 : static TupleTableSlot *
453 10065 : MJFillOuter(MergeJoinState *node)
454 : {
455 10065 : ExprContext *econtext = node->js.ps.ps_ExprContext;
456 10065 : ExprState *otherqual = node->js.ps.qual;
457 :
458 10065 : ResetExprContext(econtext);
459 :
460 10065 : econtext->ecxt_outertuple = node->mj_OuterTupleSlot;
461 10065 : econtext->ecxt_innertuple = node->mj_NullInnerTupleSlot;
462 :
463 10065 : if (ExecQual(otherqual, econtext))
464 : {
465 : /*
466 : * qualification succeeded. now form the desired projection tuple and
467 : * return the slot containing it.
468 : */
469 : MJ_printf("ExecMergeJoin: returning outer fill tuple\n");
470 :
471 10065 : return ExecProject(node->js.ps.ps_ProjInfo);
472 : }
473 : else
474 0 : InstrCountFiltered2(node, 1);
475 :
476 0 : return NULL;
477 : }
478 :
479 : /*
480 : * Generate a fake join tuple with nulls for the outer tuple,
481 : * and return it if it passes the non-join quals.
482 : */
483 : static TupleTableSlot *
484 33 : MJFillInner(MergeJoinState *node)
485 : {
486 33 : ExprContext *econtext = node->js.ps.ps_ExprContext;
487 33 : ExprState *otherqual = node->js.ps.qual;
488 :
489 33 : ResetExprContext(econtext);
490 :
491 33 : econtext->ecxt_outertuple = node->mj_NullOuterTupleSlot;
492 33 : econtext->ecxt_innertuple = node->mj_InnerTupleSlot;
493 :
494 33 : if (ExecQual(otherqual, econtext))
495 : {
496 : /*
497 : * qualification succeeded. now form the desired projection tuple and
498 : * return the slot containing it.
499 : */
500 : MJ_printf("ExecMergeJoin: returning inner fill tuple\n");
501 :
502 33 : return ExecProject(node->js.ps.ps_ProjInfo);
503 : }
504 : else
505 0 : InstrCountFiltered2(node, 1);
506 :
507 0 : return NULL;
508 : }
509 :
510 :
511 : /*
512 : * Check that a qual condition is constant true or constant false.
513 : * If it is constant false (or null), set *is_const_false to TRUE.
514 : *
515 : * Constant true would normally be represented by a NIL list, but we allow an
516 : * actual bool Const as well. We do expect that the planner will have thrown
517 : * away any non-constant terms that have been ANDed with a constant false.
518 : */
519 : static bool
520 28 : check_constant_qual(List *qual, bool *is_const_false)
521 : {
522 : ListCell *lc;
523 :
524 29 : foreach(lc, qual)
525 : {
526 1 : Const *con = (Const *) lfirst(lc);
527 :
528 1 : if (!con || !IsA(con, Const))
529 0 : return false;
530 1 : if (con->constisnull || !DatumGetBool(con->constvalue))
531 1 : *is_const_false = true;
532 : }
533 28 : return true;
534 : }
535 :
536 :
537 : /* ----------------------------------------------------------------
538 : * ExecMergeTupleDump
539 : *
540 : * This function is called through the MJ_dump() macro
541 : * when EXEC_MERGEJOINDEBUG is defined
542 : * ----------------------------------------------------------------
543 : */
544 : #ifdef EXEC_MERGEJOINDEBUG
545 :
546 : static void
547 : ExecMergeTupleDumpOuter(MergeJoinState *mergestate)
548 : {
549 : TupleTableSlot *outerSlot = mergestate->mj_OuterTupleSlot;
550 :
551 : printf("==== outer tuple ====\n");
552 : if (TupIsNull(outerSlot))
553 : printf("(nil)\n");
554 : else
555 : MJ_debugtup(outerSlot);
556 : }
557 :
558 : static void
559 : ExecMergeTupleDumpInner(MergeJoinState *mergestate)
560 : {
561 : TupleTableSlot *innerSlot = mergestate->mj_InnerTupleSlot;
562 :
563 : printf("==== inner tuple ====\n");
564 : if (TupIsNull(innerSlot))
565 : printf("(nil)\n");
566 : else
567 : MJ_debugtup(innerSlot);
568 : }
569 :
570 : static void
571 : ExecMergeTupleDumpMarked(MergeJoinState *mergestate)
572 : {
573 : TupleTableSlot *markedSlot = mergestate->mj_MarkedTupleSlot;
574 :
575 : printf("==== marked tuple ====\n");
576 : if (TupIsNull(markedSlot))
577 : printf("(nil)\n");
578 : else
579 : MJ_debugtup(markedSlot);
580 : }
581 :
582 : static void
583 : ExecMergeTupleDump(MergeJoinState *mergestate)
584 : {
585 : printf("******** ExecMergeTupleDump ********\n");
586 :
587 : ExecMergeTupleDumpOuter(mergestate);
588 : ExecMergeTupleDumpInner(mergestate);
589 : ExecMergeTupleDumpMarked(mergestate);
590 :
591 : printf("********\n");
592 : }
593 : #endif
594 :
595 : /* ----------------------------------------------------------------
596 : * ExecMergeJoin
597 : * ----------------------------------------------------------------
598 : */
599 : static TupleTableSlot *
600 43107 : ExecMergeJoin(PlanState *pstate)
601 : {
602 43107 : MergeJoinState *node = castNode(MergeJoinState, pstate);
603 : ExprState *joinqual;
604 : ExprState *otherqual;
605 : bool qualResult;
606 : int compareResult;
607 : PlanState *innerPlan;
608 : TupleTableSlot *innerTupleSlot;
609 : PlanState *outerPlan;
610 : TupleTableSlot *outerTupleSlot;
611 : ExprContext *econtext;
612 : bool doFillOuter;
613 : bool doFillInner;
614 :
615 43107 : CHECK_FOR_INTERRUPTS();
616 :
617 : /*
618 : * get information from node
619 : */
620 43107 : innerPlan = innerPlanState(node);
621 43107 : outerPlan = outerPlanState(node);
622 43107 : econtext = node->js.ps.ps_ExprContext;
623 43107 : joinqual = node->js.joinqual;
624 43107 : otherqual = node->js.ps.qual;
625 43107 : doFillOuter = node->mj_FillOuter;
626 43107 : doFillInner = node->mj_FillInner;
627 :
628 : /*
629 : * Reset per-tuple memory context to free any expression evaluation
630 : * storage allocated in the previous tuple cycle.
631 : */
632 43107 : ResetExprContext(econtext);
633 :
634 : /*
635 : * ok, everything is setup.. let's go to work
636 : */
637 : for (;;)
638 : {
639 : MJ_dump(node);
640 :
641 : /*
642 : * get the current state of the join and do things accordingly.
643 : */
644 411206 : switch (node->mj_JoinState)
645 : {
646 : /*
647 : * EXEC_MJ_INITIALIZE_OUTER means that this is the first time
648 : * ExecMergeJoin() has been called and so we have to fetch the
649 : * first matchable tuple for both outer and inner subplans. We
650 : * do the outer side in INITIALIZE_OUTER state, then advance
651 : * to INITIALIZE_INNER state for the inner subplan.
652 : */
653 : case EXEC_MJ_INITIALIZE_OUTER:
654 : MJ_printf("ExecMergeJoin: EXEC_MJ_INITIALIZE_OUTER\n");
655 :
656 161 : outerTupleSlot = ExecProcNode(outerPlan);
657 161 : node->mj_OuterTupleSlot = outerTupleSlot;
658 :
659 : /* Compute join values and check for unmatchability */
660 161 : switch (MJEvalOuterValues(node))
661 : {
662 : case MJEVAL_MATCHABLE:
663 : /* OK to go get the first inner tuple */
664 155 : node->mj_JoinState = EXEC_MJ_INITIALIZE_INNER;
665 155 : break;
666 : case MJEVAL_NONMATCHABLE:
667 : /* Stay in same state to fetch next outer tuple */
668 0 : if (doFillOuter)
669 : {
670 : /*
671 : * Generate a fake join tuple with nulls for the
672 : * inner tuple, and return it if it passes the
673 : * non-join quals.
674 : */
675 : TupleTableSlot *result;
676 :
677 0 : result = MJFillOuter(node);
678 0 : if (result)
679 0 : return result;
680 : }
681 0 : break;
682 : case MJEVAL_ENDOFJOIN:
683 : /* No more outer tuples */
684 : MJ_printf("ExecMergeJoin: nothing in outer subplan\n");
685 6 : if (doFillInner)
686 : {
687 : /*
688 : * Need to emit right-join tuples for remaining
689 : * inner tuples. We set MatchedInner = true to
690 : * force the ENDOUTER state to advance inner.
691 : */
692 0 : node->mj_JoinState = EXEC_MJ_ENDOUTER;
693 0 : node->mj_MatchedInner = true;
694 0 : break;
695 : }
696 : /* Otherwise we're done. */
697 6 : return NULL;
698 : }
699 155 : break;
700 :
701 : case EXEC_MJ_INITIALIZE_INNER:
702 : MJ_printf("ExecMergeJoin: EXEC_MJ_INITIALIZE_INNER\n");
703 :
704 155 : innerTupleSlot = ExecProcNode(innerPlan);
705 155 : node->mj_InnerTupleSlot = innerTupleSlot;
706 :
707 : /* Compute join values and check for unmatchability */
708 155 : switch (MJEvalInnerValues(node, innerTupleSlot))
709 : {
710 : case MJEVAL_MATCHABLE:
711 :
712 : /*
713 : * OK, we have the initial tuples. Begin by skipping
714 : * non-matching tuples.
715 : */
716 153 : node->mj_JoinState = EXEC_MJ_SKIP_TEST;
717 153 : break;
718 : case MJEVAL_NONMATCHABLE:
719 : /* Mark before advancing, if wanted */
720 0 : if (node->mj_ExtraMarks)
721 0 : ExecMarkPos(innerPlan);
722 : /* Stay in same state to fetch next inner tuple */
723 0 : if (doFillInner)
724 : {
725 : /*
726 : * Generate a fake join tuple with nulls for the
727 : * outer tuple, and return it if it passes the
728 : * non-join quals.
729 : */
730 : TupleTableSlot *result;
731 :
732 0 : result = MJFillInner(node);
733 0 : if (result)
734 0 : return result;
735 : }
736 0 : break;
737 : case MJEVAL_ENDOFJOIN:
738 : /* No more inner tuples */
739 : MJ_printf("ExecMergeJoin: nothing in inner subplan\n");
740 2 : if (doFillOuter)
741 : {
742 : /*
743 : * Need to emit left-join tuples for all outer
744 : * tuples, including the one we just fetched. We
745 : * set MatchedOuter = false to force the ENDINNER
746 : * state to emit first tuple before advancing
747 : * outer.
748 : */
749 1 : node->mj_JoinState = EXEC_MJ_ENDINNER;
750 1 : node->mj_MatchedOuter = false;
751 1 : break;
752 : }
753 : /* Otherwise we're done. */
754 1 : return NULL;
755 : }
756 154 : break;
757 :
758 : /*
759 : * EXEC_MJ_JOINTUPLES means we have two tuples which satisfied
760 : * the merge clause so we join them and then proceed to get
761 : * the next inner tuple (EXEC_MJ_NEXTINNER).
762 : */
763 : case EXEC_MJ_JOINTUPLES:
764 : MJ_printf("ExecMergeJoin: EXEC_MJ_JOINTUPLES\n");
765 :
766 : /*
767 : * Set the next state machine state. The right things will
768 : * happen whether we return this join tuple or just fall
769 : * through to continue the state machine execution.
770 : */
771 86496 : node->mj_JoinState = EXEC_MJ_NEXTINNER;
772 :
773 : /*
774 : * Check the extra qual conditions to see if we actually want
775 : * to return this join tuple. If not, can proceed with merge.
776 : * We must distinguish the additional joinquals (which must
777 : * pass to consider the tuples "matched" for outer-join logic)
778 : * from the otherquals (which must pass before we actually
779 : * return the tuple).
780 : *
781 : * We don't bother with a ResetExprContext here, on the
782 : * assumption that we just did one while checking the merge
783 : * qual. One per tuple should be sufficient. We do have to
784 : * set up the econtext links to the tuples for ExecQual to
785 : * use.
786 : */
787 86496 : outerTupleSlot = node->mj_OuterTupleSlot;
788 86496 : econtext->ecxt_outertuple = outerTupleSlot;
789 86496 : innerTupleSlot = node->mj_InnerTupleSlot;
790 86496 : econtext->ecxt_innertuple = innerTupleSlot;
791 :
792 135972 : qualResult = (joinqual == NULL ||
793 49476 : ExecQual(joinqual, econtext));
794 : MJ_DEBUG_QUAL(joinqual, qualResult);
795 :
796 86496 : if (qualResult)
797 : {
798 37103 : node->mj_MatchedOuter = true;
799 37103 : node->mj_MatchedInner = true;
800 :
801 : /* In an antijoin, we never return a matched tuple */
802 37103 : if (node->js.jointype == JOIN_ANTI)
803 : {
804 4254 : node->mj_JoinState = EXEC_MJ_NEXTOUTER;
805 4254 : break;
806 : }
807 :
808 : /*
809 : * If we only need to join to the first matching inner
810 : * tuple, then consider returning this one, but after that
811 : * continue with next outer tuple.
812 : */
813 32849 : if (node->js.single_match)
814 2202 : node->mj_JoinState = EXEC_MJ_NEXTOUTER;
815 :
816 32857 : qualResult = (otherqual == NULL ||
817 8 : ExecQual(otherqual, econtext));
818 : MJ_DEBUG_QUAL(otherqual, qualResult);
819 :
820 32849 : if (qualResult)
821 : {
822 : /*
823 : * qualification succeeded. now form the desired
824 : * projection tuple and return the slot containing it.
825 : */
826 : MJ_printf("ExecMergeJoin: returning tuple\n");
827 :
828 32849 : return ExecProject(node->js.ps.ps_ProjInfo);
829 : }
830 : else
831 0 : InstrCountFiltered2(node, 1);
832 : }
833 : else
834 49393 : InstrCountFiltered1(node, 1);
835 49393 : break;
836 :
837 : /*
838 : * EXEC_MJ_NEXTINNER means advance the inner scan to the next
839 : * tuple. If the tuple is not nil, we then proceed to test it
840 : * against the join qualification.
841 : *
842 : * Before advancing, we check to see if we must emit an
843 : * outer-join fill tuple for this inner tuple.
844 : */
845 : case EXEC_MJ_NEXTINNER:
846 : MJ_printf("ExecMergeJoin: EXEC_MJ_NEXTINNER\n");
847 :
848 80040 : if (doFillInner && !node->mj_MatchedInner)
849 : {
850 : /*
851 : * Generate a fake join tuple with nulls for the outer
852 : * tuple, and return it if it passes the non-join quals.
853 : */
854 : TupleTableSlot *result;
855 :
856 0 : node->mj_MatchedInner = true; /* do it only once */
857 :
858 0 : result = MJFillInner(node);
859 0 : if (result)
860 0 : return result;
861 : }
862 :
863 : /*
864 : * now we get the next inner tuple, if any. If there's none,
865 : * advance to next outer tuple (which may be able to join to
866 : * previously marked tuples).
867 : *
868 : * NB: must NOT do "extraMarks" here, since we may need to
869 : * return to previously marked tuples.
870 : */
871 80040 : innerTupleSlot = ExecProcNode(innerPlan);
872 80040 : node->mj_InnerTupleSlot = innerTupleSlot;
873 : MJ_DEBUG_PROC_NODE(innerTupleSlot);
874 80040 : node->mj_MatchedInner = false;
875 :
876 : /* Compute join values and check for unmatchability */
877 80040 : switch (MJEvalInnerValues(node, innerTupleSlot))
878 : {
879 : case MJEVAL_MATCHABLE:
880 :
881 : /*
882 : * Test the new inner tuple to see if it matches
883 : * outer.
884 : *
885 : * If they do match, then we join them and move on to
886 : * the next inner tuple (EXEC_MJ_JOINTUPLES).
887 : *
888 : * If they do not match then advance to next outer
889 : * tuple.
890 : */
891 79927 : compareResult = MJCompare(node);
892 : MJ_DEBUG_COMPARE(compareResult);
893 :
894 79927 : if (compareResult == 0)
895 22313 : node->mj_JoinState = EXEC_MJ_JOINTUPLES;
896 : else
897 : {
898 57614 : Assert(compareResult < 0);
899 57614 : node->mj_JoinState = EXEC_MJ_NEXTOUTER;
900 : }
901 79927 : break;
902 : case MJEVAL_NONMATCHABLE:
903 :
904 : /*
905 : * It contains a NULL and hence can't match any outer
906 : * tuple, so we can skip the comparison and assume the
907 : * new tuple is greater than current outer.
908 : */
909 4 : node->mj_JoinState = EXEC_MJ_NEXTOUTER;
910 4 : break;
911 : case MJEVAL_ENDOFJOIN:
912 :
913 : /*
914 : * No more inner tuples. However, this might be only
915 : * effective and not physical end of inner plan, so
916 : * force mj_InnerTupleSlot to null to make sure we
917 : * don't fetch more inner tuples. (We need this hack
918 : * because we are not transiting to a state where the
919 : * inner plan is assumed to be exhausted.)
920 : */
921 109 : node->mj_InnerTupleSlot = NULL;
922 109 : node->mj_JoinState = EXEC_MJ_NEXTOUTER;
923 109 : break;
924 : }
925 80040 : break;
926 :
927 : /*-------------------------------------------
928 : * EXEC_MJ_NEXTOUTER means
929 : *
930 : * outer inner
931 : * outer tuple - 5 5 - marked tuple
932 : * 5 5
933 : * 6 6 - inner tuple
934 : * 7 7
935 : *
936 : * we know we just bumped into the
937 : * first inner tuple > current outer tuple (or possibly
938 : * the end of the inner stream)
939 : * so get a new outer tuple and then
940 : * proceed to test it against the marked tuple
941 : * (EXEC_MJ_TESTOUTER)
942 : *
943 : * Before advancing, we check to see if we must emit an
944 : * outer-join fill tuple for this outer tuple.
945 : *------------------------------------------------
946 : */
947 : case EXEC_MJ_NEXTOUTER:
948 : MJ_printf("ExecMergeJoin: EXEC_MJ_NEXTOUTER\n");
949 :
950 64192 : if (doFillOuter && !node->mj_MatchedOuter)
951 : {
952 : /*
953 : * Generate a fake join tuple with nulls for the inner
954 : * tuple, and return it if it passes the non-join quals.
955 : */
956 : TupleTableSlot *result;
957 :
958 7 : node->mj_MatchedOuter = true; /* do it only once */
959 :
960 7 : result = MJFillOuter(node);
961 7 : if (result)
962 7 : return result;
963 : }
964 :
965 : /*
966 : * now we get the next outer tuple, if any
967 : */
968 64185 : outerTupleSlot = ExecProcNode(outerPlan);
969 64185 : node->mj_OuterTupleSlot = outerTupleSlot;
970 : MJ_DEBUG_PROC_NODE(outerTupleSlot);
971 64185 : node->mj_MatchedOuter = false;
972 :
973 : /* Compute join values and check for unmatchability */
974 64185 : switch (MJEvalOuterValues(node))
975 : {
976 : case MJEVAL_MATCHABLE:
977 : /* Go test the new tuple against the marked tuple */
978 64087 : node->mj_JoinState = EXEC_MJ_TESTOUTER;
979 64087 : break;
980 : case MJEVAL_NONMATCHABLE:
981 : /* Can't match, so fetch next outer tuple */
982 2 : node->mj_JoinState = EXEC_MJ_NEXTOUTER;
983 2 : break;
984 : case MJEVAL_ENDOFJOIN:
985 : /* No more outer tuples */
986 : MJ_printf("ExecMergeJoin: end of outer subplan\n");
987 96 : innerTupleSlot = node->mj_InnerTupleSlot;
988 96 : if (doFillInner && !TupIsNull(innerTupleSlot))
989 : {
990 : /*
991 : * Need to emit right-join tuples for remaining
992 : * inner tuples.
993 : */
994 7 : node->mj_JoinState = EXEC_MJ_ENDOUTER;
995 7 : break;
996 : }
997 : /* Otherwise we're done. */
998 89 : return NULL;
999 : }
1000 64096 : break;
1001 :
1002 : /*--------------------------------------------------------
1003 : * EXEC_MJ_TESTOUTER If the new outer tuple and the marked
1004 : * tuple satisfy the merge clause then we know we have
1005 : * duplicates in the outer scan so we have to restore the
1006 : * inner scan to the marked tuple and proceed to join the
1007 : * new outer tuple with the inner tuples.
1008 : *
1009 : * This is the case when
1010 : * outer inner
1011 : * 4 5 - marked tuple
1012 : * outer tuple - 5 5
1013 : * new outer tuple - 5 5
1014 : * 6 8 - inner tuple
1015 : * 7 12
1016 : *
1017 : * new outer tuple == marked tuple
1018 : *
1019 : * If the outer tuple fails the test, then we are done
1020 : * with the marked tuples, and we have to look for a
1021 : * match to the current inner tuple. So we will
1022 : * proceed to skip outer tuples until outer >= inner
1023 : * (EXEC_MJ_SKIP_TEST).
1024 : *
1025 : * This is the case when
1026 : *
1027 : * outer inner
1028 : * 5 5 - marked tuple
1029 : * outer tuple - 5 5
1030 : * new outer tuple - 6 8 - inner tuple
1031 : * 7 12
1032 : *
1033 : * new outer tuple > marked tuple
1034 : *
1035 : *---------------------------------------------------------
1036 : */
1037 : case EXEC_MJ_TESTOUTER:
1038 : MJ_printf("ExecMergeJoin: EXEC_MJ_TESTOUTER\n");
1039 :
1040 : /*
1041 : * Here we must compare the outer tuple with the marked inner
1042 : * tuple. (We can ignore the result of MJEvalInnerValues,
1043 : * since the marked inner tuple is certainly matchable.)
1044 : */
1045 64087 : innerTupleSlot = node->mj_MarkedTupleSlot;
1046 64087 : (void) MJEvalInnerValues(node, innerTupleSlot);
1047 :
1048 64087 : compareResult = MJCompare(node);
1049 : MJ_DEBUG_COMPARE(compareResult);
1050 :
1051 64087 : if (compareResult == 0)
1052 : {
1053 : /*
1054 : * the merge clause matched so now we restore the inner
1055 : * scan position to the first mark, and go join that tuple
1056 : * (and any following ones) to the new outer.
1057 : *
1058 : * If we were able to determine mark and restore are not
1059 : * needed, then we don't have to back up; the current
1060 : * inner is already the first possible match.
1061 : *
1062 : * NOTE: we do not need to worry about the MatchedInner
1063 : * state for the rescanned inner tuples. We know all of
1064 : * them will match this new outer tuple and therefore
1065 : * won't be emitted as fill tuples. This works *only*
1066 : * because we require the extra joinquals to be constant
1067 : * when doing a right or full join --- otherwise some of
1068 : * the rescanned tuples might fail the extra joinquals.
1069 : * This obviously won't happen for a constant-true extra
1070 : * joinqual, while the constant-false case is handled by
1071 : * forcing the merge clause to never match, so we never
1072 : * get here.
1073 : */
1074 12983 : if (!node->mj_SkipMarkRestore)
1075 : {
1076 10330 : ExecRestrPos(innerPlan);
1077 :
1078 : /*
1079 : * ExecRestrPos probably should give us back a new
1080 : * Slot, but since it doesn't, use the marked slot.
1081 : * (The previously returned mj_InnerTupleSlot cannot
1082 : * be assumed to hold the required tuple.)
1083 : */
1084 10330 : node->mj_InnerTupleSlot = innerTupleSlot;
1085 : /* we need not do MJEvalInnerValues again */
1086 : }
1087 :
1088 12983 : node->mj_JoinState = EXEC_MJ_JOINTUPLES;
1089 : }
1090 : else
1091 : {
1092 : /* ----------------
1093 : * if the new outer tuple didn't match the marked inner
1094 : * tuple then we have a case like:
1095 : *
1096 : * outer inner
1097 : * 4 4 - marked tuple
1098 : * new outer - 5 4
1099 : * 6 5 - inner tuple
1100 : * 7
1101 : *
1102 : * which means that all subsequent outer tuples will be
1103 : * larger than our marked inner tuples. So we need not
1104 : * revisit any of the marked tuples but can proceed to
1105 : * look for a match to the current inner. If there's
1106 : * no more inners, no more matches are possible.
1107 : * ----------------
1108 : */
1109 51104 : Assert(compareResult > 0);
1110 51104 : innerTupleSlot = node->mj_InnerTupleSlot;
1111 :
1112 : /* reload comparison data for current inner */
1113 51104 : switch (MJEvalInnerValues(node, innerTupleSlot))
1114 : {
1115 : case MJEVAL_MATCHABLE:
1116 : /* proceed to compare it to the current outer */
1117 51074 : node->mj_JoinState = EXEC_MJ_SKIP_TEST;
1118 51074 : break;
1119 : case MJEVAL_NONMATCHABLE:
1120 :
1121 : /*
1122 : * current inner can't possibly match any outer;
1123 : * better to advance the inner scan than the
1124 : * outer.
1125 : */
1126 4 : node->mj_JoinState = EXEC_MJ_SKIPINNER_ADVANCE;
1127 4 : break;
1128 : case MJEVAL_ENDOFJOIN:
1129 : /* No more inner tuples */
1130 26 : if (doFillOuter)
1131 : {
1132 : /*
1133 : * Need to emit left-join tuples for remaining
1134 : * outer tuples.
1135 : */
1136 8 : node->mj_JoinState = EXEC_MJ_ENDINNER;
1137 8 : break;
1138 : }
1139 : /* Otherwise we're done. */
1140 18 : return NULL;
1141 : }
1142 : }
1143 64069 : break;
1144 :
1145 : /*----------------------------------------------------------
1146 : * EXEC_MJ_SKIP means compare tuples and if they do not
1147 : * match, skip whichever is lesser.
1148 : *
1149 : * For example:
1150 : *
1151 : * outer inner
1152 : * 5 5
1153 : * 5 5
1154 : * outer tuple - 6 8 - inner tuple
1155 : * 7 12
1156 : * 8 14
1157 : *
1158 : * we have to advance the outer scan
1159 : * until we find the outer 8.
1160 : *
1161 : * On the other hand:
1162 : *
1163 : * outer inner
1164 : * 5 5
1165 : * 5 5
1166 : * outer tuple - 12 8 - inner tuple
1167 : * 14 10
1168 : * 17 12
1169 : *
1170 : * we have to advance the inner scan
1171 : * until we find the inner 12.
1172 : *----------------------------------------------------------
1173 : */
1174 : case EXEC_MJ_SKIP_TEST:
1175 : MJ_printf("ExecMergeJoin: EXEC_MJ_SKIP_TEST\n");
1176 :
1177 : /*
1178 : * before we advance, make sure the current tuples do not
1179 : * satisfy the mergeclauses. If they do, then we update the
1180 : * marked tuple position and go join them.
1181 : */
1182 73562 : compareResult = MJCompare(node);
1183 : MJ_DEBUG_COMPARE(compareResult);
1184 :
1185 73562 : if (compareResult == 0)
1186 : {
1187 51200 : if (!node->mj_SkipMarkRestore)
1188 47399 : ExecMarkPos(innerPlan);
1189 :
1190 51200 : MarkInnerTuple(node->mj_InnerTupleSlot, node);
1191 :
1192 51200 : node->mj_JoinState = EXEC_MJ_JOINTUPLES;
1193 : }
1194 22362 : else if (compareResult < 0)
1195 95 : node->mj_JoinState = EXEC_MJ_SKIPOUTER_ADVANCE;
1196 : else
1197 : /* compareResult > 0 */
1198 22267 : node->mj_JoinState = EXEC_MJ_SKIPINNER_ADVANCE;
1199 73562 : break;
1200 :
1201 : /*
1202 : * SKIPOUTER_ADVANCE: advance over an outer tuple that is
1203 : * known not to join to any inner tuple.
1204 : *
1205 : * Before advancing, we check to see if we must emit an
1206 : * outer-join fill tuple for this outer tuple.
1207 : */
1208 : case EXEC_MJ_SKIPOUTER_ADVANCE:
1209 : MJ_printf("ExecMergeJoin: EXEC_MJ_SKIPOUTER_ADVANCE\n");
1210 :
1211 115 : if (doFillOuter && !node->mj_MatchedOuter)
1212 : {
1213 : /*
1214 : * Generate a fake join tuple with nulls for the inner
1215 : * tuple, and return it if it passes the non-join quals.
1216 : */
1217 : TupleTableSlot *result;
1218 :
1219 21 : node->mj_MatchedOuter = true; /* do it only once */
1220 :
1221 21 : result = MJFillOuter(node);
1222 21 : if (result)
1223 21 : return result;
1224 : }
1225 :
1226 : /*
1227 : * now we get the next outer tuple, if any
1228 : */
1229 94 : outerTupleSlot = ExecProcNode(outerPlan);
1230 94 : node->mj_OuterTupleSlot = outerTupleSlot;
1231 : MJ_DEBUG_PROC_NODE(outerTupleSlot);
1232 94 : node->mj_MatchedOuter = false;
1233 :
1234 : /* Compute join values and check for unmatchability */
1235 94 : switch (MJEvalOuterValues(node))
1236 : {
1237 : case MJEVAL_MATCHABLE:
1238 : /* Go test the new tuple against the current inner */
1239 91 : node->mj_JoinState = EXEC_MJ_SKIP_TEST;
1240 91 : break;
1241 : case MJEVAL_NONMATCHABLE:
1242 : /* Can't match, so fetch next outer tuple */
1243 0 : node->mj_JoinState = EXEC_MJ_SKIPOUTER_ADVANCE;
1244 0 : break;
1245 : case MJEVAL_ENDOFJOIN:
1246 : /* No more outer tuples */
1247 : MJ_printf("ExecMergeJoin: end of outer subplan\n");
1248 3 : innerTupleSlot = node->mj_InnerTupleSlot;
1249 3 : if (doFillInner && !TupIsNull(innerTupleSlot))
1250 : {
1251 : /*
1252 : * Need to emit right-join tuples for remaining
1253 : * inner tuples.
1254 : */
1255 2 : node->mj_JoinState = EXEC_MJ_ENDOUTER;
1256 2 : break;
1257 : }
1258 : /* Otherwise we're done. */
1259 1 : return NULL;
1260 : }
1261 93 : break;
1262 :
1263 : /*
1264 : * SKIPINNER_ADVANCE: advance over an inner tuple that is
1265 : * known not to join to any outer tuple.
1266 : *
1267 : * Before advancing, we check to see if we must emit an
1268 : * outer-join fill tuple for this inner tuple.
1269 : */
1270 : case EXEC_MJ_SKIPINNER_ADVANCE:
1271 : MJ_printf("ExecMergeJoin: EXEC_MJ_SKIPINNER_ADVANCE\n");
1272 :
1273 22292 : if (doFillInner && !node->mj_MatchedInner)
1274 : {
1275 : /*
1276 : * Generate a fake join tuple with nulls for the outer
1277 : * tuple, and return it if it passes the non-join quals.
1278 : */
1279 : TupleTableSlot *result;
1280 :
1281 17 : node->mj_MatchedInner = true; /* do it only once */
1282 :
1283 17 : result = MJFillInner(node);
1284 17 : if (result)
1285 17 : return result;
1286 : }
1287 :
1288 : /* Mark before advancing, if wanted */
1289 22275 : if (node->mj_ExtraMarks)
1290 11 : ExecMarkPos(innerPlan);
1291 :
1292 : /*
1293 : * now we get the next inner tuple, if any
1294 : */
1295 22275 : innerTupleSlot = ExecProcNode(innerPlan);
1296 22275 : node->mj_InnerTupleSlot = innerTupleSlot;
1297 : MJ_DEBUG_PROC_NODE(innerTupleSlot);
1298 22275 : node->mj_MatchedInner = false;
1299 :
1300 : /* Compute join values and check for unmatchability */
1301 22275 : switch (MJEvalInnerValues(node, innerTupleSlot))
1302 : {
1303 : case MJEVAL_MATCHABLE:
1304 : /* proceed to compare it to the current outer */
1305 22244 : node->mj_JoinState = EXEC_MJ_SKIP_TEST;
1306 22244 : break;
1307 : case MJEVAL_NONMATCHABLE:
1308 :
1309 : /*
1310 : * current inner can't possibly match any outer;
1311 : * better to advance the inner scan than the outer.
1312 : */
1313 4 : node->mj_JoinState = EXEC_MJ_SKIPINNER_ADVANCE;
1314 4 : break;
1315 : case MJEVAL_ENDOFJOIN:
1316 : /* No more inner tuples */
1317 : MJ_printf("ExecMergeJoin: end of inner subplan\n");
1318 27 : outerTupleSlot = node->mj_OuterTupleSlot;
1319 27 : if (doFillOuter && !TupIsNull(outerTupleSlot))
1320 : {
1321 : /*
1322 : * Need to emit left-join tuples for remaining
1323 : * outer tuples.
1324 : */
1325 9 : node->mj_JoinState = EXEC_MJ_ENDINNER;
1326 9 : break;
1327 : }
1328 : /* Otherwise we're done. */
1329 18 : return NULL;
1330 : }
1331 22257 : break;
1332 :
1333 : /*
1334 : * EXEC_MJ_ENDOUTER means we have run out of outer tuples, but
1335 : * are doing a right/full join and therefore must null-fill
1336 : * any remaining unmatched inner tuples.
1337 : */
1338 : case EXEC_MJ_ENDOUTER:
1339 : MJ_printf("ExecMergeJoin: EXEC_MJ_ENDOUTER\n");
1340 :
1341 32 : Assert(doFillInner);
1342 :
1343 32 : if (!node->mj_MatchedInner)
1344 : {
1345 : /*
1346 : * Generate a fake join tuple with nulls for the outer
1347 : * tuple, and return it if it passes the non-join quals.
1348 : */
1349 : TupleTableSlot *result;
1350 :
1351 16 : node->mj_MatchedInner = true; /* do it only once */
1352 :
1353 16 : result = MJFillInner(node);
1354 16 : if (result)
1355 16 : return result;
1356 : }
1357 :
1358 : /* Mark before advancing, if wanted */
1359 16 : if (node->mj_ExtraMarks)
1360 2 : ExecMarkPos(innerPlan);
1361 :
1362 : /*
1363 : * now we get the next inner tuple, if any
1364 : */
1365 16 : innerTupleSlot = ExecProcNode(innerPlan);
1366 16 : node->mj_InnerTupleSlot = innerTupleSlot;
1367 : MJ_DEBUG_PROC_NODE(innerTupleSlot);
1368 16 : node->mj_MatchedInner = false;
1369 :
1370 16 : if (TupIsNull(innerTupleSlot))
1371 : {
1372 : MJ_printf("ExecMergeJoin: end of inner subplan\n");
1373 9 : return NULL;
1374 : }
1375 :
1376 : /* Else remain in ENDOUTER state and process next tuple. */
1377 7 : break;
1378 :
1379 : /*
1380 : * EXEC_MJ_ENDINNER means we have run out of inner tuples, but
1381 : * are doing a left/full join and therefore must null- fill
1382 : * any remaining unmatched outer tuples.
1383 : */
1384 : case EXEC_MJ_ENDINNER:
1385 : MJ_printf("ExecMergeJoin: EXEC_MJ_ENDINNER\n");
1386 :
1387 20074 : Assert(doFillOuter);
1388 :
1389 20074 : if (!node->mj_MatchedOuter)
1390 : {
1391 : /*
1392 : * Generate a fake join tuple with nulls for the inner
1393 : * tuple, and return it if it passes the non-join quals.
1394 : */
1395 : TupleTableSlot *result;
1396 :
1397 10037 : node->mj_MatchedOuter = true; /* do it only once */
1398 :
1399 10037 : result = MJFillOuter(node);
1400 10037 : if (result)
1401 10037 : return result;
1402 : }
1403 :
1404 : /*
1405 : * now we get the next outer tuple, if any
1406 : */
1407 10037 : outerTupleSlot = ExecProcNode(outerPlan);
1408 10037 : node->mj_OuterTupleSlot = outerTupleSlot;
1409 : MJ_DEBUG_PROC_NODE(outerTupleSlot);
1410 10037 : node->mj_MatchedOuter = false;
1411 :
1412 10037 : if (TupIsNull(outerTupleSlot))
1413 : {
1414 : MJ_printf("ExecMergeJoin: end of outer subplan\n");
1415 18 : return NULL;
1416 : }
1417 :
1418 : /* Else remain in ENDINNER state and process next tuple. */
1419 10019 : break;
1420 :
1421 : /*
1422 : * broken state value?
1423 : */
1424 : default:
1425 0 : elog(ERROR, "unrecognized mergejoin state: %d",
1426 : (int) node->mj_JoinState);
1427 : }
1428 368099 : }
1429 : }
1430 :
1431 : /* ----------------------------------------------------------------
1432 : * ExecInitMergeJoin
1433 : * ----------------------------------------------------------------
1434 : */
1435 : MergeJoinState *
1436 168 : ExecInitMergeJoin(MergeJoin *node, EState *estate, int eflags)
1437 : {
1438 : MergeJoinState *mergestate;
1439 :
1440 : /* check for unsupported flags */
1441 168 : Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
1442 :
1443 : MJ1_printf("ExecInitMergeJoin: %s\n",
1444 : "initializing node");
1445 :
1446 : /*
1447 : * create state structure
1448 : */
1449 168 : mergestate = makeNode(MergeJoinState);
1450 168 : mergestate->js.ps.plan = (Plan *) node;
1451 168 : mergestate->js.ps.state = estate;
1452 168 : mergestate->js.ps.ExecProcNode = ExecMergeJoin;
1453 :
1454 : /*
1455 : * Miscellaneous initialization
1456 : *
1457 : * create expression context for node
1458 : */
1459 168 : ExecAssignExprContext(estate, &mergestate->js.ps);
1460 :
1461 : /*
1462 : * we need two additional econtexts in which we can compute the join
1463 : * expressions from the left and right input tuples. The node's regular
1464 : * econtext won't do because it gets reset too often.
1465 : */
1466 168 : mergestate->mj_OuterEContext = CreateExprContext(estate);
1467 168 : mergestate->mj_InnerEContext = CreateExprContext(estate);
1468 :
1469 : /*
1470 : * initialize child expressions
1471 : */
1472 168 : mergestate->js.ps.qual =
1473 168 : ExecInitQual(node->join.plan.qual, (PlanState *) mergestate);
1474 168 : mergestate->js.jointype = node->join.jointype;
1475 168 : mergestate->js.joinqual =
1476 168 : ExecInitQual(node->join.joinqual, (PlanState *) mergestate);
1477 168 : mergestate->mj_ConstFalseJoin = false;
1478 : /* mergeclauses are handled below */
1479 :
1480 : /*
1481 : * initialize child nodes
1482 : *
1483 : * inner child must support MARK/RESTORE, unless we have detected that we
1484 : * don't need that. Note that skip_mark_restore must never be set if
1485 : * there are non-mergeclause joinquals, since the logic wouldn't work.
1486 : */
1487 168 : Assert(node->join.joinqual == NIL || !node->skip_mark_restore);
1488 168 : mergestate->mj_SkipMarkRestore = node->skip_mark_restore;
1489 :
1490 168 : outerPlanState(mergestate) = ExecInitNode(outerPlan(node), estate, eflags);
1491 168 : innerPlanState(mergestate) = ExecInitNode(innerPlan(node), estate,
1492 168 : mergestate->mj_SkipMarkRestore ?
1493 : eflags :
1494 : (eflags | EXEC_FLAG_MARK));
1495 :
1496 : /*
1497 : * For certain types of inner child nodes, it is advantageous to issue
1498 : * MARK every time we advance past an inner tuple we will never return to.
1499 : * For other types, MARK on a tuple we cannot return to is a waste of
1500 : * cycles. Detect which case applies and set mj_ExtraMarks if we want to
1501 : * issue "unnecessary" MARK calls.
1502 : *
1503 : * Currently, only Material wants the extra MARKs, and it will be helpful
1504 : * only if eflags doesn't specify REWIND.
1505 : */
1506 182 : if (IsA(innerPlan(node), Material) &&
1507 28 : (eflags & EXEC_FLAG_REWIND) == 0 &&
1508 14 : !mergestate->mj_SkipMarkRestore)
1509 14 : mergestate->mj_ExtraMarks = true;
1510 : else
1511 154 : mergestate->mj_ExtraMarks = false;
1512 :
1513 : /*
1514 : * tuple table initialization
1515 : */
1516 168 : ExecInitResultTupleSlot(estate, &mergestate->js.ps);
1517 :
1518 168 : mergestate->mj_MarkedTupleSlot = ExecInitExtraTupleSlot(estate);
1519 168 : ExecSetSlotDescriptor(mergestate->mj_MarkedTupleSlot,
1520 168 : ExecGetResultType(innerPlanState(mergestate)));
1521 :
1522 : /*
1523 : * detect whether we need only consider the first matching inner tuple
1524 : */
1525 328 : mergestate->js.single_match = (node->join.inner_unique ||
1526 160 : node->join.jointype == JOIN_SEMI);
1527 :
1528 : /* set up null tuples for outer joins, if needed */
1529 168 : switch (node->join.jointype)
1530 : {
1531 : case JOIN_INNER:
1532 : case JOIN_SEMI:
1533 102 : mergestate->mj_FillOuter = false;
1534 102 : mergestate->mj_FillInner = false;
1535 102 : break;
1536 : case JOIN_LEFT:
1537 : case JOIN_ANTI:
1538 38 : mergestate->mj_FillOuter = true;
1539 38 : mergestate->mj_FillInner = false;
1540 38 : mergestate->mj_NullInnerTupleSlot =
1541 38 : ExecInitNullTupleSlot(estate,
1542 38 : ExecGetResultType(innerPlanState(mergestate)));
1543 38 : break;
1544 : case JOIN_RIGHT:
1545 13 : mergestate->mj_FillOuter = false;
1546 13 : mergestate->mj_FillInner = true;
1547 13 : mergestate->mj_NullOuterTupleSlot =
1548 13 : ExecInitNullTupleSlot(estate,
1549 13 : ExecGetResultType(outerPlanState(mergestate)));
1550 :
1551 : /*
1552 : * Can't handle right or full join with non-constant extra
1553 : * joinclauses. This should have been caught by planner.
1554 : */
1555 13 : if (!check_constant_qual(node->join.joinqual,
1556 : &mergestate->mj_ConstFalseJoin))
1557 0 : ereport(ERROR,
1558 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1559 : errmsg("RIGHT JOIN is only supported with merge-joinable join conditions")));
1560 13 : break;
1561 : case JOIN_FULL:
1562 15 : mergestate->mj_FillOuter = true;
1563 15 : mergestate->mj_FillInner = true;
1564 15 : mergestate->mj_NullOuterTupleSlot =
1565 15 : ExecInitNullTupleSlot(estate,
1566 15 : ExecGetResultType(outerPlanState(mergestate)));
1567 15 : mergestate->mj_NullInnerTupleSlot =
1568 15 : ExecInitNullTupleSlot(estate,
1569 15 : ExecGetResultType(innerPlanState(mergestate)));
1570 :
1571 : /*
1572 : * Can't handle right or full join with non-constant extra
1573 : * joinclauses. This should have been caught by planner.
1574 : */
1575 15 : if (!check_constant_qual(node->join.joinqual,
1576 : &mergestate->mj_ConstFalseJoin))
1577 0 : ereport(ERROR,
1578 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1579 : errmsg("FULL JOIN is only supported with merge-joinable join conditions")));
1580 15 : break;
1581 : default:
1582 0 : elog(ERROR, "unrecognized join type: %d",
1583 : (int) node->join.jointype);
1584 : }
1585 :
1586 : /*
1587 : * initialize tuple type and projection info
1588 : */
1589 168 : ExecAssignResultTypeFromTL(&mergestate->js.ps);
1590 168 : ExecAssignProjectionInfo(&mergestate->js.ps, NULL);
1591 :
1592 : /*
1593 : * preprocess the merge clauses
1594 : */
1595 168 : mergestate->mj_NumClauses = list_length(node->mergeclauses);
1596 168 : mergestate->mj_Clauses = MJExamineQuals(node->mergeclauses,
1597 : node->mergeFamilies,
1598 : node->mergeCollations,
1599 : node->mergeStrategies,
1600 : node->mergeNullsFirst,
1601 : (PlanState *) mergestate);
1602 :
1603 : /*
1604 : * initialize join state
1605 : */
1606 168 : mergestate->mj_JoinState = EXEC_MJ_INITIALIZE_OUTER;
1607 168 : mergestate->mj_MatchedOuter = false;
1608 168 : mergestate->mj_MatchedInner = false;
1609 168 : mergestate->mj_OuterTupleSlot = NULL;
1610 168 : mergestate->mj_InnerTupleSlot = NULL;
1611 :
1612 : /*
1613 : * initialization successful
1614 : */
1615 : MJ1_printf("ExecInitMergeJoin: %s\n",
1616 : "node initialized");
1617 :
1618 168 : return mergestate;
1619 : }
1620 :
1621 : /* ----------------------------------------------------------------
1622 : * ExecEndMergeJoin
1623 : *
1624 : * old comments
1625 : * frees storage allocated through C routines.
1626 : * ----------------------------------------------------------------
1627 : */
1628 : void
1629 167 : ExecEndMergeJoin(MergeJoinState *node)
1630 : {
1631 : MJ1_printf("ExecEndMergeJoin: %s\n",
1632 : "ending node processing");
1633 :
1634 : /*
1635 : * Free the exprcontext
1636 : */
1637 167 : ExecFreeExprContext(&node->js.ps);
1638 :
1639 : /*
1640 : * clean out the tuple table
1641 : */
1642 167 : ExecClearTuple(node->js.ps.ps_ResultTupleSlot);
1643 167 : ExecClearTuple(node->mj_MarkedTupleSlot);
1644 :
1645 : /*
1646 : * shut down the subplans
1647 : */
1648 167 : ExecEndNode(innerPlanState(node));
1649 167 : ExecEndNode(outerPlanState(node));
1650 :
1651 : MJ1_printf("ExecEndMergeJoin: %s\n",
1652 : "node processing ended");
1653 167 : }
1654 :
1655 : void
1656 8 : ExecReScanMergeJoin(MergeJoinState *node)
1657 : {
1658 8 : ExecClearTuple(node->mj_MarkedTupleSlot);
1659 :
1660 8 : node->mj_JoinState = EXEC_MJ_INITIALIZE_OUTER;
1661 8 : node->mj_MatchedOuter = false;
1662 8 : node->mj_MatchedInner = false;
1663 8 : node->mj_OuterTupleSlot = NULL;
1664 8 : node->mj_InnerTupleSlot = NULL;
1665 :
1666 : /*
1667 : * if chgParam of subnodes is not null then plans will be re-scanned by
1668 : * first ExecProcNode.
1669 : */
1670 8 : if (node->js.ps.lefttree->chgParam == NULL)
1671 8 : ExecReScan(node->js.ps.lefttree);
1672 8 : if (node->js.ps.righttree->chgParam == NULL)
1673 0 : ExecReScan(node->js.ps.righttree);
1674 :
1675 8 : }
|