1 /*-------------------------------------------------------------------------
4 * routines supporting merge joins
6 * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/executor/nodeMergejoin.c
13 *-------------------------------------------------------------------------
17 * ExecMergeJoin mergejoin outer and inner relations.
18 * ExecInitMergeJoin creates and initializes run time states
19 * ExecEndMergeJoin cleans up the node.
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).
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
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
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.
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.
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.
61 * Essential operation of the merge join algorithm is as follows:
64 * get initial outer and inner tuples INITIALIZE
66 * while (outer != inner) { SKIP_TEST
68 * advance outer SKIPOUTER_ADVANCE
70 * advance inner SKIPINNER_ADVANCE
72 * mark inner position SKIP_TEST
74 * while (outer == inner) {
75 * join tuples JOINTUPLES
76 * advance inner position NEXTINNER
78 * advance outer position NEXTOUTER
79 * if (outer == mark) TESTOUTER
80 * restore inner position to mark TESTOUTER
82 * break // return to top of outer loop
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
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"
104 * States of the ExecMergeJoin state machine
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
119 * Runtime data for each mergejoin clause
121 typedef struct MergeJoinClauseData
123 /* Executable expression trees */
124 ExprState *lexpr; /* left-hand (outer) input expression */
125 ExprState *rexpr; /* right-hand (inner) input expression */
128 * If we have a current left or right input tuple, the values of the
129 * expressions are loaded into these fields:
131 Datum ldatum; /* current left-hand value */
132 Datum rdatum; /* current right-hand value */
133 bool lisnull; /* and their isnull flags */
137 * Everything we need to know to compare the left and right values is
140 SortSupportData ssup;
141 } MergeJoinClauseData;
143 /* Result type for MJEvalOuterValues and MJEvalInnerValues */
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) */
152 #define MarkInnerTuple(innerTupleSlot, mergestate) \
153 ExecCopySlot((mergestate)->mj_MarkedTupleSlot, (innerTupleSlot))
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.
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.
175 static MergeJoinClause
176 MJExamineQuals(List *mergeclauses,
178 Oid *mergecollations,
179 int *mergestrategies,
180 bool *mergenullsfirst,
183 MergeJoinClause clauses;
184 int nClauses = list_length(mergeclauses);
188 clauses = (MergeJoinClause) palloc0(nClauses * sizeof(MergeJoinClauseData));
191 foreach(cl, mergeclauses)
193 OpExpr *qual = (OpExpr *) lfirst(cl);
194 MergeJoinClause clause = &clauses[iClause];
195 Oid opfamily = mergefamilies[iClause];
196 Oid collation = mergecollations[iClause];
197 StrategyNumber opstrategy = mergestrategies[iClause];
198 bool nulls_first = mergenullsfirst[iClause];
204 if (!IsA(qual, OpExpr))
205 elog(ERROR, "mergejoin clause is not an OpExpr");
208 * Prepare the input expressions for execution.
210 clause->lexpr = ExecInitExpr((Expr *) linitial(qual->args), parent);
211 clause->rexpr = ExecInitExpr((Expr *) lsecond(qual->args), parent);
213 /* Set up sort support data */
214 clause->ssup.ssup_cxt = CurrentMemoryContext;
215 clause->ssup.ssup_collation = collation;
216 if (opstrategy == BTLessStrategyNumber)
217 clause->ssup.ssup_reverse = false;
218 else if (opstrategy == BTGreaterStrategyNumber)
219 clause->ssup.ssup_reverse = true;
220 else /* planner screwed up */
221 elog(ERROR, "unsupported mergejoin strategy %d", opstrategy);
222 clause->ssup.ssup_nulls_first = nulls_first;
224 /* Extract the operator's declared left/right datatypes */
225 get_op_opfamily_properties(qual->opno, opfamily, false,
229 if (op_strategy != BTEqualStrategyNumber) /* should not happen */
230 elog(ERROR, "cannot merge using non-equality operator %u",
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.
239 clause->ssup.abbreviate = false;
241 /* And get the matching support or comparison function */
242 Assert(clause->ssup.comparator == NULL);
243 sortfunc = get_opfamily_proc(opfamily,
247 if (OidIsValid(sortfunc))
249 /* The sort support function can provide a comparator */
250 OidFunctionCall1(sortfunc, PointerGetDatum(&clause->ssup));
252 if (clause->ssup.comparator == NULL)
254 /* support not available, get comparison func */
255 sortfunc = get_opfamily_proc(opfamily,
259 if (!OidIsValid(sortfunc)) /* should not happen */
260 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 PrepareSortSupportComparisonShim(sortfunc, &clause->ssup);
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.
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.
291 * We evaluate the values in OuterEContext, which can be reset each
292 * time we move to a new tuple.
295 MJEvalOuterValues(MergeJoinState *mergestate)
297 ExprContext *econtext = mergestate->mj_OuterEContext;
298 MJEvalResult result = MJEVAL_MATCHABLE;
300 MemoryContext oldContext;
302 /* Check for end of outer subplan */
303 if (TupIsNull(mergestate->mj_OuterTupleSlot))
304 return MJEVAL_ENDOFJOIN;
306 ResetExprContext(econtext);
308 oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
310 econtext->ecxt_outertuple = mergestate->mj_OuterTupleSlot;
312 for (i = 0; i < mergestate->mj_NumClauses; i++)
314 MergeJoinClause clause = &mergestate->mj_Clauses[i];
316 clause->ldatum = ExecEvalExpr(clause->lexpr, econtext,
320 /* match is impossible; can we end the join early? */
321 if (i == 0 && !clause->ssup.ssup_nulls_first &&
322 !mergestate->mj_FillOuter)
323 result = MJEVAL_ENDOFJOIN;
324 else if (result == MJEVAL_MATCHABLE)
325 result = MJEVAL_NONMATCHABLE;
329 MemoryContextSwitchTo(oldContext);
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.
342 MJEvalInnerValues(MergeJoinState *mergestate, TupleTableSlot *innerslot)
344 ExprContext *econtext = mergestate->mj_InnerEContext;
345 MJEvalResult result = MJEVAL_MATCHABLE;
347 MemoryContext oldContext;
349 /* Check for end of inner subplan */
350 if (TupIsNull(innerslot))
351 return MJEVAL_ENDOFJOIN;
353 ResetExprContext(econtext);
355 oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
357 econtext->ecxt_innertuple = innerslot;
359 for (i = 0; i < mergestate->mj_NumClauses; i++)
361 MergeJoinClause clause = &mergestate->mj_Clauses[i];
363 clause->rdatum = ExecEvalExpr(clause->rexpr, econtext,
367 /* match is impossible; can we end the join early? */
368 if (i == 0 && !clause->ssup.ssup_nulls_first &&
369 !mergestate->mj_FillInner)
370 result = MJEVAL_ENDOFJOIN;
371 else if (result == MJEVAL_MATCHABLE)
372 result = MJEVAL_NONMATCHABLE;
376 MemoryContextSwitchTo(oldContext);
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.
388 * MJEvalOuterValues and MJEvalInnerValues must already have been called
389 * for the current outer and inner tuples, respectively.
392 MJCompare(MergeJoinState *mergestate)
395 bool nulleqnull = false;
396 ExprContext *econtext = mergestate->js.ps.ps_ExprContext;
398 MemoryContext oldContext;
401 * Call the comparison functions in short-lived context, in case they leak
404 ResetExprContext(econtext);
406 oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
408 for (i = 0; i < mergestate->mj_NumClauses; i++)
410 MergeJoinClause clause = &mergestate->mj_Clauses[i];
413 * Special case for NULL-vs-NULL, else use standard comparison.
415 if (clause->lisnull && clause->risnull)
417 nulleqnull = true; /* NULL "=" NULL */
421 result = ApplySortComparator(clause->ldatum, clause->lisnull,
422 clause->rdatum, clause->risnull,
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.
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.
439 (nulleqnull || mergestate->mj_ConstFalseJoin))
442 MemoryContextSwitchTo(oldContext);
449 * Generate a fake join tuple with nulls for the inner tuple,
450 * and return it if it passes the non-join quals.
452 static TupleTableSlot *
453 MJFillOuter(MergeJoinState *node)
455 ExprContext *econtext = node->js.ps.ps_ExprContext;
456 ExprState *otherqual = node->js.ps.qual;
458 ResetExprContext(econtext);
460 econtext->ecxt_outertuple = node->mj_OuterTupleSlot;
461 econtext->ecxt_innertuple = node->mj_NullInnerTupleSlot;
463 if (ExecQual(otherqual, econtext))
466 * qualification succeeded. now form the desired projection tuple and
467 * return the slot containing it.
469 MJ_printf("ExecMergeJoin: returning outer fill tuple\n");
471 return ExecProject(node->js.ps.ps_ProjInfo);
474 InstrCountFiltered2(node, 1);
480 * Generate a fake join tuple with nulls for the outer tuple,
481 * and return it if it passes the non-join quals.
483 static TupleTableSlot *
484 MJFillInner(MergeJoinState *node)
486 ExprContext *econtext = node->js.ps.ps_ExprContext;
487 ExprState *otherqual = node->js.ps.qual;
489 ResetExprContext(econtext);
491 econtext->ecxt_outertuple = node->mj_NullOuterTupleSlot;
492 econtext->ecxt_innertuple = node->mj_InnerTupleSlot;
494 if (ExecQual(otherqual, econtext))
497 * qualification succeeded. now form the desired projection tuple and
498 * return the slot containing it.
500 MJ_printf("ExecMergeJoin: returning inner fill tuple\n");
502 return ExecProject(node->js.ps.ps_ProjInfo);
505 InstrCountFiltered2(node, 1);
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.
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.
520 check_constant_qual(List *qual, bool *is_const_false)
526 Const *con = (Const *) lfirst(lc);
528 if (!con || !IsA(con, Const))
530 if (con->constisnull || !DatumGetBool(con->constvalue))
531 *is_const_false = true;
537 /* ----------------------------------------------------------------
540 * This function is called through the MJ_dump() macro
541 * when EXEC_MERGEJOINDEBUG is defined
542 * ----------------------------------------------------------------
544 #ifdef EXEC_MERGEJOINDEBUG
547 ExecMergeTupleDumpOuter(MergeJoinState *mergestate)
549 TupleTableSlot *outerSlot = mergestate->mj_OuterTupleSlot;
551 printf("==== outer tuple ====\n");
552 if (TupIsNull(outerSlot))
555 MJ_debugtup(outerSlot);
559 ExecMergeTupleDumpInner(MergeJoinState *mergestate)
561 TupleTableSlot *innerSlot = mergestate->mj_InnerTupleSlot;
563 printf("==== inner tuple ====\n");
564 if (TupIsNull(innerSlot))
567 MJ_debugtup(innerSlot);
571 ExecMergeTupleDumpMarked(MergeJoinState *mergestate)
573 TupleTableSlot *markedSlot = mergestate->mj_MarkedTupleSlot;
575 printf("==== marked tuple ====\n");
576 if (TupIsNull(markedSlot))
579 MJ_debugtup(markedSlot);
583 ExecMergeTupleDump(MergeJoinState *mergestate)
585 printf("******** ExecMergeTupleDump ********\n");
587 ExecMergeTupleDumpOuter(mergestate);
588 ExecMergeTupleDumpInner(mergestate);
589 ExecMergeTupleDumpMarked(mergestate);
591 printf("********\n");
595 /* ----------------------------------------------------------------
597 * ----------------------------------------------------------------
599 static TupleTableSlot *
600 ExecMergeJoin(PlanState *pstate)
602 MergeJoinState *node = castNode(MergeJoinState, pstate);
604 ExprState *otherqual;
607 PlanState *innerPlan;
608 TupleTableSlot *innerTupleSlot;
609 PlanState *outerPlan;
610 TupleTableSlot *outerTupleSlot;
611 ExprContext *econtext;
615 CHECK_FOR_INTERRUPTS();
618 * get information from node
620 innerPlan = innerPlanState(node);
621 outerPlan = outerPlanState(node);
622 econtext = node->js.ps.ps_ExprContext;
623 joinqual = node->js.joinqual;
624 otherqual = node->js.ps.qual;
625 doFillOuter = node->mj_FillOuter;
626 doFillInner = node->mj_FillInner;
629 * Reset per-tuple memory context to free any expression evaluation
630 * storage allocated in the previous tuple cycle.
632 ResetExprContext(econtext);
635 * ok, everything is setup.. let's go to work
642 * get the current state of the join and do things accordingly.
644 switch (node->mj_JoinState)
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.
653 case EXEC_MJ_INITIALIZE_OUTER:
654 MJ_printf("ExecMergeJoin: EXEC_MJ_INITIALIZE_OUTER\n");
656 outerTupleSlot = ExecProcNode(outerPlan);
657 node->mj_OuterTupleSlot = outerTupleSlot;
659 /* Compute join values and check for unmatchability */
660 switch (MJEvalOuterValues(node))
662 case MJEVAL_MATCHABLE:
663 /* OK to go get the first inner tuple */
664 node->mj_JoinState = EXEC_MJ_INITIALIZE_INNER;
666 case MJEVAL_NONMATCHABLE:
667 /* Stay in same state to fetch next outer tuple */
671 * Generate a fake join tuple with nulls for the
672 * inner tuple, and return it if it passes the
675 TupleTableSlot *result;
677 result = MJFillOuter(node);
682 case MJEVAL_ENDOFJOIN:
683 /* No more outer tuples */
684 MJ_printf("ExecMergeJoin: nothing in outer subplan\n");
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.
692 node->mj_JoinState = EXEC_MJ_ENDOUTER;
693 node->mj_MatchedInner = true;
696 /* Otherwise we're done. */
701 case EXEC_MJ_INITIALIZE_INNER:
702 MJ_printf("ExecMergeJoin: EXEC_MJ_INITIALIZE_INNER\n");
704 innerTupleSlot = ExecProcNode(innerPlan);
705 node->mj_InnerTupleSlot = innerTupleSlot;
707 /* Compute join values and check for unmatchability */
708 switch (MJEvalInnerValues(node, innerTupleSlot))
710 case MJEVAL_MATCHABLE:
713 * OK, we have the initial tuples. Begin by skipping
714 * non-matching tuples.
716 node->mj_JoinState = EXEC_MJ_SKIP_TEST;
718 case MJEVAL_NONMATCHABLE:
719 /* Mark before advancing, if wanted */
720 if (node->mj_ExtraMarks)
721 ExecMarkPos(innerPlan);
722 /* Stay in same state to fetch next inner tuple */
726 * Generate a fake join tuple with nulls for the
727 * outer tuple, and return it if it passes the
730 TupleTableSlot *result;
732 result = MJFillInner(node);
737 case MJEVAL_ENDOFJOIN:
738 /* No more inner tuples */
739 MJ_printf("ExecMergeJoin: nothing in inner subplan\n");
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
749 node->mj_JoinState = EXEC_MJ_ENDINNER;
750 node->mj_MatchedOuter = false;
753 /* Otherwise we're done. */
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).
763 case EXEC_MJ_JOINTUPLES:
764 MJ_printf("ExecMergeJoin: EXEC_MJ_JOINTUPLES\n");
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.
771 node->mj_JoinState = EXEC_MJ_NEXTINNER;
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
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
787 outerTupleSlot = node->mj_OuterTupleSlot;
788 econtext->ecxt_outertuple = outerTupleSlot;
789 innerTupleSlot = node->mj_InnerTupleSlot;
790 econtext->ecxt_innertuple = innerTupleSlot;
792 qualResult = (joinqual == NULL ||
793 ExecQual(joinqual, econtext));
794 MJ_DEBUG_QUAL(joinqual, qualResult);
798 node->mj_MatchedOuter = true;
799 node->mj_MatchedInner = true;
801 /* In an antijoin, we never return a matched tuple */
802 if (node->js.jointype == JOIN_ANTI)
804 node->mj_JoinState = EXEC_MJ_NEXTOUTER;
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.
813 if (node->js.single_match)
814 node->mj_JoinState = EXEC_MJ_NEXTOUTER;
816 qualResult = (otherqual == NULL ||
817 ExecQual(otherqual, econtext));
818 MJ_DEBUG_QUAL(otherqual, qualResult);
823 * qualification succeeded. now form the desired
824 * projection tuple and return the slot containing it.
826 MJ_printf("ExecMergeJoin: returning tuple\n");
828 return ExecProject(node->js.ps.ps_ProjInfo);
831 InstrCountFiltered2(node, 1);
834 InstrCountFiltered1(node, 1);
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.
842 * Before advancing, we check to see if we must emit an
843 * outer-join fill tuple for this inner tuple.
845 case EXEC_MJ_NEXTINNER:
846 MJ_printf("ExecMergeJoin: EXEC_MJ_NEXTINNER\n");
848 if (doFillInner && !node->mj_MatchedInner)
851 * Generate a fake join tuple with nulls for the outer
852 * tuple, and return it if it passes the non-join quals.
854 TupleTableSlot *result;
856 node->mj_MatchedInner = true; /* do it only once */
858 result = MJFillInner(node);
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).
868 * NB: must NOT do "extraMarks" here, since we may need to
869 * return to previously marked tuples.
871 innerTupleSlot = ExecProcNode(innerPlan);
872 node->mj_InnerTupleSlot = innerTupleSlot;
873 MJ_DEBUG_PROC_NODE(innerTupleSlot);
874 node->mj_MatchedInner = false;
876 /* Compute join values and check for unmatchability */
877 switch (MJEvalInnerValues(node, innerTupleSlot))
879 case MJEVAL_MATCHABLE:
882 * Test the new inner tuple to see if it matches
885 * If they do match, then we join them and move on to
886 * the next inner tuple (EXEC_MJ_JOINTUPLES).
888 * If they do not match then advance to next outer
891 compareResult = MJCompare(node);
892 MJ_DEBUG_COMPARE(compareResult);
894 if (compareResult == 0)
895 node->mj_JoinState = EXEC_MJ_JOINTUPLES;
898 Assert(compareResult < 0);
899 node->mj_JoinState = EXEC_MJ_NEXTOUTER;
902 case MJEVAL_NONMATCHABLE:
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.
909 node->mj_JoinState = EXEC_MJ_NEXTOUTER;
911 case MJEVAL_ENDOFJOIN:
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.)
921 node->mj_InnerTupleSlot = NULL;
922 node->mj_JoinState = EXEC_MJ_NEXTOUTER;
927 /*-------------------------------------------
928 * EXEC_MJ_NEXTOUTER means
931 * outer tuple - 5 5 - marked tuple
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)
943 * Before advancing, we check to see if we must emit an
944 * outer-join fill tuple for this outer tuple.
945 *------------------------------------------------
947 case EXEC_MJ_NEXTOUTER:
948 MJ_printf("ExecMergeJoin: EXEC_MJ_NEXTOUTER\n");
950 if (doFillOuter && !node->mj_MatchedOuter)
953 * Generate a fake join tuple with nulls for the inner
954 * tuple, and return it if it passes the non-join quals.
956 TupleTableSlot *result;
958 node->mj_MatchedOuter = true; /* do it only once */
960 result = MJFillOuter(node);
966 * now we get the next outer tuple, if any
968 outerTupleSlot = ExecProcNode(outerPlan);
969 node->mj_OuterTupleSlot = outerTupleSlot;
970 MJ_DEBUG_PROC_NODE(outerTupleSlot);
971 node->mj_MatchedOuter = false;
973 /* Compute join values and check for unmatchability */
974 switch (MJEvalOuterValues(node))
976 case MJEVAL_MATCHABLE:
977 /* Go test the new tuple against the marked tuple */
978 node->mj_JoinState = EXEC_MJ_TESTOUTER;
980 case MJEVAL_NONMATCHABLE:
981 /* Can't match, so fetch next outer tuple */
982 node->mj_JoinState = EXEC_MJ_NEXTOUTER;
984 case MJEVAL_ENDOFJOIN:
985 /* No more outer tuples */
986 MJ_printf("ExecMergeJoin: end of outer subplan\n");
987 innerTupleSlot = node->mj_InnerTupleSlot;
988 if (doFillInner && !TupIsNull(innerTupleSlot))
991 * Need to emit right-join tuples for remaining
994 node->mj_JoinState = EXEC_MJ_ENDOUTER;
997 /* Otherwise we're done. */
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.
1009 * This is the case when
1011 * 4 5 - marked tuple
1013 * new outer tuple - 5 5
1017 * new outer tuple == marked tuple
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).
1025 * This is the case when
1028 * 5 5 - marked tuple
1030 * new outer tuple - 6 8 - inner tuple
1033 * new outer tuple > marked tuple
1035 *---------------------------------------------------------
1037 case EXEC_MJ_TESTOUTER:
1038 MJ_printf("ExecMergeJoin: EXEC_MJ_TESTOUTER\n");
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.)
1045 innerTupleSlot = node->mj_MarkedTupleSlot;
1046 (void) MJEvalInnerValues(node, innerTupleSlot);
1048 compareResult = MJCompare(node);
1049 MJ_DEBUG_COMPARE(compareResult);
1051 if (compareResult == 0)
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.
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.
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
1074 if (!node->mj_SkipMarkRestore)
1076 ExecRestrPos(innerPlan);
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.)
1084 node->mj_InnerTupleSlot = innerTupleSlot;
1085 /* we need not do MJEvalInnerValues again */
1088 node->mj_JoinState = EXEC_MJ_JOINTUPLES;
1093 * if the new outer tuple didn't match the marked inner
1094 * tuple then we have a case like:
1097 * 4 4 - marked tuple
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.
1109 Assert(compareResult > 0);
1110 innerTupleSlot = node->mj_InnerTupleSlot;
1112 /* reload comparison data for current inner */
1113 switch (MJEvalInnerValues(node, innerTupleSlot))
1115 case MJEVAL_MATCHABLE:
1116 /* proceed to compare it to the current outer */
1117 node->mj_JoinState = EXEC_MJ_SKIP_TEST;
1119 case MJEVAL_NONMATCHABLE:
1122 * current inner can't possibly match any outer;
1123 * better to advance the inner scan than the
1126 node->mj_JoinState = EXEC_MJ_SKIPINNER_ADVANCE;
1128 case MJEVAL_ENDOFJOIN:
1129 /* No more inner tuples */
1133 * Need to emit left-join tuples for remaining
1136 node->mj_JoinState = EXEC_MJ_ENDINNER;
1139 /* Otherwise we're done. */
1145 /*----------------------------------------------------------
1146 * EXEC_MJ_SKIP means compare tuples and if they do not
1147 * match, skip whichever is lesser.
1154 * outer tuple - 6 8 - inner tuple
1158 * we have to advance the outer scan
1159 * until we find the outer 8.
1161 * On the other hand:
1166 * outer tuple - 12 8 - inner tuple
1170 * we have to advance the inner scan
1171 * until we find the inner 12.
1172 *----------------------------------------------------------
1174 case EXEC_MJ_SKIP_TEST:
1175 MJ_printf("ExecMergeJoin: EXEC_MJ_SKIP_TEST\n");
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.
1182 compareResult = MJCompare(node);
1183 MJ_DEBUG_COMPARE(compareResult);
1185 if (compareResult == 0)
1187 if (!node->mj_SkipMarkRestore)
1188 ExecMarkPos(innerPlan);
1190 MarkInnerTuple(node->mj_InnerTupleSlot, node);
1192 node->mj_JoinState = EXEC_MJ_JOINTUPLES;
1194 else if (compareResult < 0)
1195 node->mj_JoinState = EXEC_MJ_SKIPOUTER_ADVANCE;
1197 /* compareResult > 0 */
1198 node->mj_JoinState = EXEC_MJ_SKIPINNER_ADVANCE;
1202 * SKIPOUTER_ADVANCE: advance over an outer tuple that is
1203 * known not to join to any inner tuple.
1205 * Before advancing, we check to see if we must emit an
1206 * outer-join fill tuple for this outer tuple.
1208 case EXEC_MJ_SKIPOUTER_ADVANCE:
1209 MJ_printf("ExecMergeJoin: EXEC_MJ_SKIPOUTER_ADVANCE\n");
1211 if (doFillOuter && !node->mj_MatchedOuter)
1214 * Generate a fake join tuple with nulls for the inner
1215 * tuple, and return it if it passes the non-join quals.
1217 TupleTableSlot *result;
1219 node->mj_MatchedOuter = true; /* do it only once */
1221 result = MJFillOuter(node);
1227 * now we get the next outer tuple, if any
1229 outerTupleSlot = ExecProcNode(outerPlan);
1230 node->mj_OuterTupleSlot = outerTupleSlot;
1231 MJ_DEBUG_PROC_NODE(outerTupleSlot);
1232 node->mj_MatchedOuter = false;
1234 /* Compute join values and check for unmatchability */
1235 switch (MJEvalOuterValues(node))
1237 case MJEVAL_MATCHABLE:
1238 /* Go test the new tuple against the current inner */
1239 node->mj_JoinState = EXEC_MJ_SKIP_TEST;
1241 case MJEVAL_NONMATCHABLE:
1242 /* Can't match, so fetch next outer tuple */
1243 node->mj_JoinState = EXEC_MJ_SKIPOUTER_ADVANCE;
1245 case MJEVAL_ENDOFJOIN:
1246 /* No more outer tuples */
1247 MJ_printf("ExecMergeJoin: end of outer subplan\n");
1248 innerTupleSlot = node->mj_InnerTupleSlot;
1249 if (doFillInner && !TupIsNull(innerTupleSlot))
1252 * Need to emit right-join tuples for remaining
1255 node->mj_JoinState = EXEC_MJ_ENDOUTER;
1258 /* Otherwise we're done. */
1264 * SKIPINNER_ADVANCE: advance over an inner tuple that is
1265 * known not to join to any outer tuple.
1267 * Before advancing, we check to see if we must emit an
1268 * outer-join fill tuple for this inner tuple.
1270 case EXEC_MJ_SKIPINNER_ADVANCE:
1271 MJ_printf("ExecMergeJoin: EXEC_MJ_SKIPINNER_ADVANCE\n");
1273 if (doFillInner && !node->mj_MatchedInner)
1276 * Generate a fake join tuple with nulls for the outer
1277 * tuple, and return it if it passes the non-join quals.
1279 TupleTableSlot *result;
1281 node->mj_MatchedInner = true; /* do it only once */
1283 result = MJFillInner(node);
1288 /* Mark before advancing, if wanted */
1289 if (node->mj_ExtraMarks)
1290 ExecMarkPos(innerPlan);
1293 * now we get the next inner tuple, if any
1295 innerTupleSlot = ExecProcNode(innerPlan);
1296 node->mj_InnerTupleSlot = innerTupleSlot;
1297 MJ_DEBUG_PROC_NODE(innerTupleSlot);
1298 node->mj_MatchedInner = false;
1300 /* Compute join values and check for unmatchability */
1301 switch (MJEvalInnerValues(node, innerTupleSlot))
1303 case MJEVAL_MATCHABLE:
1304 /* proceed to compare it to the current outer */
1305 node->mj_JoinState = EXEC_MJ_SKIP_TEST;
1307 case MJEVAL_NONMATCHABLE:
1310 * current inner can't possibly match any outer;
1311 * better to advance the inner scan than the outer.
1313 node->mj_JoinState = EXEC_MJ_SKIPINNER_ADVANCE;
1315 case MJEVAL_ENDOFJOIN:
1316 /* No more inner tuples */
1317 MJ_printf("ExecMergeJoin: end of inner subplan\n");
1318 outerTupleSlot = node->mj_OuterTupleSlot;
1319 if (doFillOuter && !TupIsNull(outerTupleSlot))
1322 * Need to emit left-join tuples for remaining
1325 node->mj_JoinState = EXEC_MJ_ENDINNER;
1328 /* Otherwise we're done. */
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.
1338 case EXEC_MJ_ENDOUTER:
1339 MJ_printf("ExecMergeJoin: EXEC_MJ_ENDOUTER\n");
1341 Assert(doFillInner);
1343 if (!node->mj_MatchedInner)
1346 * Generate a fake join tuple with nulls for the outer
1347 * tuple, and return it if it passes the non-join quals.
1349 TupleTableSlot *result;
1351 node->mj_MatchedInner = true; /* do it only once */
1353 result = MJFillInner(node);
1358 /* Mark before advancing, if wanted */
1359 if (node->mj_ExtraMarks)
1360 ExecMarkPos(innerPlan);
1363 * now we get the next inner tuple, if any
1365 innerTupleSlot = ExecProcNode(innerPlan);
1366 node->mj_InnerTupleSlot = innerTupleSlot;
1367 MJ_DEBUG_PROC_NODE(innerTupleSlot);
1368 node->mj_MatchedInner = false;
1370 if (TupIsNull(innerTupleSlot))
1372 MJ_printf("ExecMergeJoin: end of inner subplan\n");
1376 /* Else remain in ENDOUTER state and process next tuple. */
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.
1384 case EXEC_MJ_ENDINNER:
1385 MJ_printf("ExecMergeJoin: EXEC_MJ_ENDINNER\n");
1387 Assert(doFillOuter);
1389 if (!node->mj_MatchedOuter)
1392 * Generate a fake join tuple with nulls for the inner
1393 * tuple, and return it if it passes the non-join quals.
1395 TupleTableSlot *result;
1397 node->mj_MatchedOuter = true; /* do it only once */
1399 result = MJFillOuter(node);
1405 * now we get the next outer tuple, if any
1407 outerTupleSlot = ExecProcNode(outerPlan);
1408 node->mj_OuterTupleSlot = outerTupleSlot;
1409 MJ_DEBUG_PROC_NODE(outerTupleSlot);
1410 node->mj_MatchedOuter = false;
1412 if (TupIsNull(outerTupleSlot))
1414 MJ_printf("ExecMergeJoin: end of outer subplan\n");
1418 /* Else remain in ENDINNER state and process next tuple. */
1422 * broken state value?
1425 elog(ERROR, "unrecognized mergejoin state: %d",
1426 (int) node->mj_JoinState);
1431 /* ----------------------------------------------------------------
1433 * ----------------------------------------------------------------
1436 ExecInitMergeJoin(MergeJoin *node, EState *estate, int eflags)
1438 MergeJoinState *mergestate;
1439 TupleDesc outerDesc,
1441 const TupleTableSlotOps *innerOps;
1443 /* check for unsupported flags */
1444 Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
1446 MJ1_printf("ExecInitMergeJoin: %s\n",
1447 "initializing node");
1450 * create state structure
1452 mergestate = makeNode(MergeJoinState);
1453 mergestate->js.ps.plan = (Plan *) node;
1454 mergestate->js.ps.state = estate;
1455 mergestate->js.ps.ExecProcNode = ExecMergeJoin;
1456 mergestate->js.jointype = node->join.jointype;
1457 mergestate->mj_ConstFalseJoin = false;
1460 * Miscellaneous initialization
1462 * create expression context for node
1464 ExecAssignExprContext(estate, &mergestate->js.ps);
1467 * we need two additional econtexts in which we can compute the join
1468 * expressions from the left and right input tuples. The node's regular
1469 * econtext won't do because it gets reset too often.
1471 mergestate->mj_OuterEContext = CreateExprContext(estate);
1472 mergestate->mj_InnerEContext = CreateExprContext(estate);
1475 * initialize child nodes
1477 * inner child must support MARK/RESTORE, unless we have detected that we
1478 * don't need that. Note that skip_mark_restore must never be set if
1479 * there are non-mergeclause joinquals, since the logic wouldn't work.
1481 Assert(node->join.joinqual == NIL || !node->skip_mark_restore);
1482 mergestate->mj_SkipMarkRestore = node->skip_mark_restore;
1484 outerPlanState(mergestate) = ExecInitNode(outerPlan(node), estate, eflags);
1485 outerDesc = ExecGetResultType(outerPlanState(mergestate));
1486 innerPlanState(mergestate) = ExecInitNode(innerPlan(node), estate,
1487 mergestate->mj_SkipMarkRestore ?
1489 (eflags | EXEC_FLAG_MARK));
1490 innerDesc = ExecGetResultType(innerPlanState(mergestate));
1493 * For certain types of inner child nodes, it is advantageous to issue
1494 * MARK every time we advance past an inner tuple we will never return to.
1495 * For other types, MARK on a tuple we cannot return to is a waste of
1496 * cycles. Detect which case applies and set mj_ExtraMarks if we want to
1497 * issue "unnecessary" MARK calls.
1499 * Currently, only Material wants the extra MARKs, and it will be helpful
1500 * only if eflags doesn't specify REWIND.
1502 * Note that for IndexScan and IndexOnlyScan, it is *necessary* that we
1503 * not set mj_ExtraMarks; otherwise we might attempt to set a mark before
1504 * the first inner tuple, which they do not support.
1506 if (IsA(innerPlan(node), Material) &&
1507 (eflags & EXEC_FLAG_REWIND) == 0 &&
1508 !mergestate->mj_SkipMarkRestore)
1509 mergestate->mj_ExtraMarks = true;
1511 mergestate->mj_ExtraMarks = false;
1514 * Initialize result slot, type and projection.
1516 ExecInitResultTupleSlotTL(&mergestate->js.ps, &TTSOpsVirtual);
1517 ExecAssignProjectionInfo(&mergestate->js.ps, NULL);
1520 * tuple table initialization
1522 innerOps = ExecGetResultSlotOps(innerPlanState(mergestate), NULL);
1523 mergestate->mj_MarkedTupleSlot = ExecInitExtraTupleSlot(estate, innerDesc,
1527 * initialize child expressions
1529 mergestate->js.ps.qual =
1530 ExecInitQual(node->join.plan.qual, (PlanState *) mergestate);
1531 mergestate->js.joinqual =
1532 ExecInitQual(node->join.joinqual, (PlanState *) mergestate);
1533 /* mergeclauses are handled below */
1536 * detect whether we need only consider the first matching inner tuple
1538 mergestate->js.single_match = (node->join.inner_unique ||
1539 node->join.jointype == JOIN_SEMI);
1541 /* set up null tuples for outer joins, if needed */
1542 switch (node->join.jointype)
1546 mergestate->mj_FillOuter = false;
1547 mergestate->mj_FillInner = false;
1551 mergestate->mj_FillOuter = true;
1552 mergestate->mj_FillInner = false;
1553 mergestate->mj_NullInnerTupleSlot =
1554 ExecInitNullTupleSlot(estate, innerDesc, &TTSOpsVirtual);
1557 mergestate->mj_FillOuter = false;
1558 mergestate->mj_FillInner = true;
1559 mergestate->mj_NullOuterTupleSlot =
1560 ExecInitNullTupleSlot(estate, outerDesc, &TTSOpsVirtual);
1563 * Can't handle right or full join with non-constant extra
1564 * joinclauses. This should have been caught by planner.
1566 if (!check_constant_qual(node->join.joinqual,
1567 &mergestate->mj_ConstFalseJoin))
1569 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1570 errmsg("RIGHT JOIN is only supported with merge-joinable join conditions")));
1573 mergestate->mj_FillOuter = true;
1574 mergestate->mj_FillInner = true;
1575 mergestate->mj_NullOuterTupleSlot =
1576 ExecInitNullTupleSlot(estate, outerDesc, &TTSOpsVirtual);
1577 mergestate->mj_NullInnerTupleSlot =
1578 ExecInitNullTupleSlot(estate, innerDesc, &TTSOpsVirtual);
1581 * Can't handle right or full join with non-constant extra
1582 * joinclauses. This should have been caught by planner.
1584 if (!check_constant_qual(node->join.joinqual,
1585 &mergestate->mj_ConstFalseJoin))
1587 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1588 errmsg("FULL JOIN is only supported with merge-joinable join conditions")));
1591 elog(ERROR, "unrecognized join type: %d",
1592 (int) node->join.jointype);
1596 * preprocess the merge clauses
1598 mergestate->mj_NumClauses = list_length(node->mergeclauses);
1599 mergestate->mj_Clauses = MJExamineQuals(node->mergeclauses,
1600 node->mergeFamilies,
1601 node->mergeCollations,
1602 node->mergeStrategies,
1603 node->mergeNullsFirst,
1604 (PlanState *) mergestate);
1607 * initialize join state
1609 mergestate->mj_JoinState = EXEC_MJ_INITIALIZE_OUTER;
1610 mergestate->mj_MatchedOuter = false;
1611 mergestate->mj_MatchedInner = false;
1612 mergestate->mj_OuterTupleSlot = NULL;
1613 mergestate->mj_InnerTupleSlot = NULL;
1616 * initialization successful
1618 MJ1_printf("ExecInitMergeJoin: %s\n",
1619 "node initialized");
1624 /* ----------------------------------------------------------------
1628 * frees storage allocated through C routines.
1629 * ----------------------------------------------------------------
1632 ExecEndMergeJoin(MergeJoinState *node)
1634 MJ1_printf("ExecEndMergeJoin: %s\n",
1635 "ending node processing");
1638 * Free the exprcontext
1640 ExecFreeExprContext(&node->js.ps);
1643 * clean out the tuple table
1645 ExecClearTuple(node->js.ps.ps_ResultTupleSlot);
1646 ExecClearTuple(node->mj_MarkedTupleSlot);
1649 * shut down the subplans
1651 ExecEndNode(innerPlanState(node));
1652 ExecEndNode(outerPlanState(node));
1654 MJ1_printf("ExecEndMergeJoin: %s\n",
1655 "node processing ended");
1659 ExecReScanMergeJoin(MergeJoinState *node)
1661 ExecClearTuple(node->mj_MarkedTupleSlot);
1663 node->mj_JoinState = EXEC_MJ_INITIALIZE_OUTER;
1664 node->mj_MatchedOuter = false;
1665 node->mj_MatchedInner = false;
1666 node->mj_OuterTupleSlot = NULL;
1667 node->mj_InnerTupleSlot = NULL;
1670 * if chgParam of subnodes is not null then plans will be re-scanned by
1671 * first ExecProcNode.
1673 if (node->js.ps.lefttree->chgParam == NULL)
1674 ExecReScan(node->js.ps.lefttree);
1675 if (node->js.ps.righttree->chgParam == NULL)
1676 ExecReScan(node->js.ps.righttree);