1 /*-------------------------------------------------------------------------
4 * routines supporting merge joins
6 * Portions Copyright (c) 1996-2012, 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 "utils/lsyscache.h"
99 #include "utils/memutils.h"
103 * States of the ExecMergeJoin state machine
105 #define EXEC_MJ_INITIALIZE_OUTER 1
106 #define EXEC_MJ_INITIALIZE_INNER 2
107 #define EXEC_MJ_JOINTUPLES 3
108 #define EXEC_MJ_NEXTOUTER 4
109 #define EXEC_MJ_TESTOUTER 5
110 #define EXEC_MJ_NEXTINNER 6
111 #define EXEC_MJ_SKIP_TEST 7
112 #define EXEC_MJ_SKIPOUTER_ADVANCE 8
113 #define EXEC_MJ_SKIPINNER_ADVANCE 9
114 #define EXEC_MJ_ENDOUTER 10
115 #define EXEC_MJ_ENDINNER 11
118 * Runtime data for each mergejoin clause
120 typedef struct MergeJoinClauseData
122 /* Executable expression trees */
123 ExprState *lexpr; /* left-hand (outer) input expression */
124 ExprState *rexpr; /* right-hand (inner) input expression */
127 * If we have a current left or right input tuple, the values of the
128 * expressions are loaded into these fields:
130 Datum ldatum; /* current left-hand value */
131 Datum rdatum; /* current right-hand value */
132 bool lisnull; /* and their isnull flags */
136 * Everything we need to know to compare the left and right values is
139 SortSupportData ssup;
140 } MergeJoinClauseData;
142 /* Result type for MJEvalOuterValues and MJEvalInnerValues */
145 MJEVAL_MATCHABLE, /* normal, potentially matchable tuple */
146 MJEVAL_NONMATCHABLE, /* tuple cannot join because it has a null */
147 MJEVAL_ENDOFJOIN /* end of input (physical or effective) */
151 #define MarkInnerTuple(innerTupleSlot, mergestate) \
152 ExecCopySlot((mergestate)->mj_MarkedTupleSlot, (innerTupleSlot))
158 * This deconstructs the list of mergejoinable expressions, which is given
159 * to us by the planner in the form of a list of "leftexpr = rightexpr"
160 * expression trees in the order matching the sort columns of the inputs.
161 * We build an array of MergeJoinClause structs containing the information
162 * we will need at runtime. Each struct essentially tells us how to compare
163 * the two expressions from the original clause.
165 * In addition to the expressions themselves, the planner passes the btree
166 * opfamily OID, collation OID, btree strategy number (BTLessStrategyNumber or
167 * BTGreaterStrategyNumber), and nulls-first flag that identify the intended
168 * sort ordering for each merge key. The mergejoinable operator is an
169 * equality operator in the opfamily, and the two inputs are guaranteed to be
170 * ordered in either increasing or decreasing (respectively) order according
171 * to the opfamily and collation, with nulls at the indicated end of the range.
172 * This allows us to obtain the needed comparison function from the opfamily.
174 static MergeJoinClause
175 MJExamineQuals(List *mergeclauses,
177 Oid *mergecollations,
178 int *mergestrategies,
179 bool *mergenullsfirst,
182 MergeJoinClause clauses;
183 int nClauses = list_length(mergeclauses);
187 clauses = (MergeJoinClause) palloc0(nClauses * sizeof(MergeJoinClauseData));
190 foreach(cl, mergeclauses)
192 OpExpr *qual = (OpExpr *) lfirst(cl);
193 MergeJoinClause clause = &clauses[iClause];
194 Oid opfamily = mergefamilies[iClause];
195 Oid collation = mergecollations[iClause];
196 StrategyNumber opstrategy = mergestrategies[iClause];
197 bool nulls_first = mergenullsfirst[iClause];
203 if (!IsA(qual, OpExpr))
204 elog(ERROR, "mergejoin clause is not an OpExpr");
207 * Prepare the input expressions for execution.
209 clause->lexpr = ExecInitExpr((Expr *) linitial(qual->args), parent);
210 clause->rexpr = ExecInitExpr((Expr *) lsecond(qual->args), parent);
212 /* Set up sort support data */
213 clause->ssup.ssup_cxt = CurrentMemoryContext;
214 clause->ssup.ssup_collation = collation;
215 if (opstrategy == BTLessStrategyNumber)
216 clause->ssup.ssup_reverse = false;
217 else if (opstrategy == BTGreaterStrategyNumber)
218 clause->ssup.ssup_reverse = true;
219 else /* planner screwed up */
220 elog(ERROR, "unsupported mergejoin strategy %d", opstrategy);
221 clause->ssup.ssup_nulls_first = nulls_first;
223 /* Extract the operator's declared left/right datatypes */
224 get_op_opfamily_properties(qual->opno, opfamily, false,
228 if (op_strategy != BTEqualStrategyNumber) /* should not happen */
229 elog(ERROR, "cannot merge using non-equality operator %u",
232 /* And get the matching support or comparison function */
233 sortfunc = get_opfamily_proc(opfamily,
237 if (OidIsValid(sortfunc))
239 /* The sort support function should provide a comparator */
240 OidFunctionCall1(sortfunc, PointerGetDatum(&clause->ssup));
241 Assert(clause->ssup.comparator != NULL);
245 /* opfamily doesn't provide sort support, get comparison func */
246 sortfunc = get_opfamily_proc(opfamily,
250 if (!OidIsValid(sortfunc)) /* should not happen */
251 elog(ERROR, "missing support function %d(%u,%u) in opfamily %u",
252 BTORDER_PROC, op_lefttype, op_righttype, opfamily);
253 /* We'll use a shim to call the old-style btree comparator */
254 PrepareSortSupportComparisonShim(sortfunc, &clause->ssup);
266 * Compute the values of the mergejoined expressions for the current
267 * outer tuple. We also detect whether it's impossible for the current
268 * outer tuple to match anything --- this is true if it yields a NULL
269 * input, since we assume mergejoin operators are strict. If the NULL
270 * is in the first join column, and that column sorts nulls last, then
271 * we can further conclude that no following tuple can match anything
272 * either, since they must all have nulls in the first column. However,
273 * that case is only interesting if we're not in FillOuter mode, else
274 * we have to visit all the tuples anyway.
276 * For the convenience of callers, we also make this routine responsible
277 * for testing for end-of-input (null outer tuple), and returning
278 * MJEVAL_ENDOFJOIN when that's seen. This allows the same code to be used
279 * for both real end-of-input and the effective end-of-input represented by
280 * a first-column NULL.
282 * We evaluate the values in OuterEContext, which can be reset each
283 * time we move to a new tuple.
286 MJEvalOuterValues(MergeJoinState *mergestate)
288 ExprContext *econtext = mergestate->mj_OuterEContext;
289 MJEvalResult result = MJEVAL_MATCHABLE;
291 MemoryContext oldContext;
293 /* Check for end of outer subplan */
294 if (TupIsNull(mergestate->mj_OuterTupleSlot))
295 return MJEVAL_ENDOFJOIN;
297 ResetExprContext(econtext);
299 oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
301 econtext->ecxt_outertuple = mergestate->mj_OuterTupleSlot;
303 for (i = 0; i < mergestate->mj_NumClauses; i++)
305 MergeJoinClause clause = &mergestate->mj_Clauses[i];
307 clause->ldatum = ExecEvalExpr(clause->lexpr, econtext,
308 &clause->lisnull, NULL);
311 /* match is impossible; can we end the join early? */
312 if (i == 0 && !clause->ssup.ssup_nulls_first &&
313 !mergestate->mj_FillOuter)
314 result = MJEVAL_ENDOFJOIN;
315 else if (result == MJEVAL_MATCHABLE)
316 result = MJEVAL_NONMATCHABLE;
320 MemoryContextSwitchTo(oldContext);
328 * Same as above, but for the inner tuple. Here, we have to be prepared
329 * to load data from either the true current inner, or the marked inner,
330 * so caller must tell us which slot to load from.
333 MJEvalInnerValues(MergeJoinState *mergestate, TupleTableSlot *innerslot)
335 ExprContext *econtext = mergestate->mj_InnerEContext;
336 MJEvalResult result = MJEVAL_MATCHABLE;
338 MemoryContext oldContext;
340 /* Check for end of inner subplan */
341 if (TupIsNull(innerslot))
342 return MJEVAL_ENDOFJOIN;
344 ResetExprContext(econtext);
346 oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
348 econtext->ecxt_innertuple = innerslot;
350 for (i = 0; i < mergestate->mj_NumClauses; i++)
352 MergeJoinClause clause = &mergestate->mj_Clauses[i];
354 clause->rdatum = ExecEvalExpr(clause->rexpr, econtext,
355 &clause->risnull, NULL);
358 /* match is impossible; can we end the join early? */
359 if (i == 0 && !clause->ssup.ssup_nulls_first &&
360 !mergestate->mj_FillInner)
361 result = MJEVAL_ENDOFJOIN;
362 else if (result == MJEVAL_MATCHABLE)
363 result = MJEVAL_NONMATCHABLE;
367 MemoryContextSwitchTo(oldContext);
375 * Compare the mergejoinable values of the current two input tuples
376 * and return 0 if they are equal (ie, the mergejoin equalities all
377 * succeed), >0 if outer > inner, <0 if outer < inner.
379 * MJEvalOuterValues and MJEvalInnerValues must already have been called
380 * for the current outer and inner tuples, respectively.
383 MJCompare(MergeJoinState *mergestate)
386 bool nulleqnull = false;
387 ExprContext *econtext = mergestate->js.ps.ps_ExprContext;
389 MemoryContext oldContext;
392 * Call the comparison functions in short-lived context, in case they leak
395 ResetExprContext(econtext);
397 oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
399 for (i = 0; i < mergestate->mj_NumClauses; i++)
401 MergeJoinClause clause = &mergestate->mj_Clauses[i];
404 * Special case for NULL-vs-NULL, else use standard comparison.
406 if (clause->lisnull && clause->risnull)
408 nulleqnull = true; /* NULL "=" NULL */
412 result = ApplySortComparator(clause->ldatum, clause->lisnull,
413 clause->rdatum, clause->risnull,
421 * If we had any NULL-vs-NULL inputs, we do not want to report that the
422 * tuples are equal. Instead, if result is still 0, change it to +1.
423 * This will result in advancing the inner side of the join.
425 * Likewise, if there was a constant-false joinqual, do not report
426 * equality. We have to check this as part of the mergequals, else the
427 * rescan logic will do the wrong thing.
430 (nulleqnull || mergestate->mj_ConstFalseJoin))
433 MemoryContextSwitchTo(oldContext);
440 * Generate a fake join tuple with nulls for the inner tuple,
441 * and return it if it passes the non-join quals.
443 static TupleTableSlot *
444 MJFillOuter(MergeJoinState *node)
446 ExprContext *econtext = node->js.ps.ps_ExprContext;
447 List *otherqual = node->js.ps.qual;
449 ResetExprContext(econtext);
451 econtext->ecxt_outertuple = node->mj_OuterTupleSlot;
452 econtext->ecxt_innertuple = node->mj_NullInnerTupleSlot;
454 if (ExecQual(otherqual, econtext, false))
457 * qualification succeeded. now form the desired projection tuple and
458 * return the slot containing it.
460 TupleTableSlot *result;
463 MJ_printf("ExecMergeJoin: returning outer fill tuple\n");
465 result = ExecProject(node->js.ps.ps_ProjInfo, &isDone);
467 if (isDone != ExprEndResult)
469 node->js.ps.ps_TupFromTlist =
470 (isDone == ExprMultipleResult);
475 InstrCountFiltered2(node, 1);
481 * Generate a fake join tuple with nulls for the outer tuple,
482 * and return it if it passes the non-join quals.
484 static TupleTableSlot *
485 MJFillInner(MergeJoinState *node)
487 ExprContext *econtext = node->js.ps.ps_ExprContext;
488 List *otherqual = node->js.ps.qual;
490 ResetExprContext(econtext);
492 econtext->ecxt_outertuple = node->mj_NullOuterTupleSlot;
493 econtext->ecxt_innertuple = node->mj_InnerTupleSlot;
495 if (ExecQual(otherqual, econtext, false))
498 * qualification succeeded. now form the desired projection tuple and
499 * return the slot containing it.
501 TupleTableSlot *result;
504 MJ_printf("ExecMergeJoin: returning inner fill tuple\n");
506 result = ExecProject(node->js.ps.ps_ProjInfo, &isDone);
508 if (isDone != ExprEndResult)
510 node->js.ps.ps_TupFromTlist =
511 (isDone == ExprMultipleResult);
516 InstrCountFiltered2(node, 1);
523 * Check that a qual condition is constant true or constant false.
524 * If it is constant false (or null), set *is_const_false to TRUE.
526 * Constant true would normally be represented by a NIL list, but we allow an
527 * actual bool Const as well. We do expect that the planner will have thrown
528 * away any non-constant terms that have been ANDed with a constant false.
531 check_constant_qual(List *qual, bool *is_const_false)
537 Const *con = (Const *) lfirst(lc);
539 if (!con || !IsA(con, Const))
541 if (con->constisnull || !DatumGetBool(con->constvalue))
542 *is_const_false = true;
548 /* ----------------------------------------------------------------
551 * This function is called through the MJ_dump() macro
552 * when EXEC_MERGEJOINDEBUG is defined
553 * ----------------------------------------------------------------
555 #ifdef EXEC_MERGEJOINDEBUG
558 ExecMergeTupleDumpOuter(MergeJoinState *mergestate)
560 TupleTableSlot *outerSlot = mergestate->mj_OuterTupleSlot;
562 printf("==== outer tuple ====\n");
563 if (TupIsNull(outerSlot))
566 MJ_debugtup(outerSlot);
570 ExecMergeTupleDumpInner(MergeJoinState *mergestate)
572 TupleTableSlot *innerSlot = mergestate->mj_InnerTupleSlot;
574 printf("==== inner tuple ====\n");
575 if (TupIsNull(innerSlot))
578 MJ_debugtup(innerSlot);
582 ExecMergeTupleDumpMarked(MergeJoinState *mergestate)
584 TupleTableSlot *markedSlot = mergestate->mj_MarkedTupleSlot;
586 printf("==== marked tuple ====\n");
587 if (TupIsNull(markedSlot))
590 MJ_debugtup(markedSlot);
594 ExecMergeTupleDump(MergeJoinState *mergestate)
596 printf("******** ExecMergeTupleDump ********\n");
598 ExecMergeTupleDumpOuter(mergestate);
599 ExecMergeTupleDumpInner(mergestate);
600 ExecMergeTupleDumpMarked(mergestate);
602 printf("******** \n");
606 /* ----------------------------------------------------------------
608 * ----------------------------------------------------------------
611 ExecMergeJoin(MergeJoinState *node)
617 PlanState *innerPlan;
618 TupleTableSlot *innerTupleSlot;
619 PlanState *outerPlan;
620 TupleTableSlot *outerTupleSlot;
621 ExprContext *econtext;
626 * get information from node
628 innerPlan = innerPlanState(node);
629 outerPlan = outerPlanState(node);
630 econtext = node->js.ps.ps_ExprContext;
631 joinqual = node->js.joinqual;
632 otherqual = node->js.ps.qual;
633 doFillOuter = node->mj_FillOuter;
634 doFillInner = node->mj_FillInner;
637 * Check to see if we're still projecting out tuples from a previous join
638 * tuple (because there is a function-returning-set in the projection
639 * expressions). If so, try to project another one.
641 if (node->js.ps.ps_TupFromTlist)
643 TupleTableSlot *result;
646 result = ExecProject(node->js.ps.ps_ProjInfo, &isDone);
647 if (isDone == ExprMultipleResult)
649 /* Done with that source tuple... */
650 node->js.ps.ps_TupFromTlist = false;
654 * Reset per-tuple memory context to free any expression evaluation
655 * storage allocated in the previous tuple cycle. Note this can't happen
656 * until we're done projecting out tuples from a join tuple.
658 ResetExprContext(econtext);
661 * ok, everything is setup.. let's go to work
668 * get the current state of the join and do things accordingly.
670 switch (node->mj_JoinState)
673 * EXEC_MJ_INITIALIZE_OUTER means that this is the first time
674 * ExecMergeJoin() has been called and so we have to fetch the
675 * first matchable tuple for both outer and inner subplans. We
676 * do the outer side in INITIALIZE_OUTER state, then advance
677 * to INITIALIZE_INNER state for the inner subplan.
679 case EXEC_MJ_INITIALIZE_OUTER:
680 MJ_printf("ExecMergeJoin: EXEC_MJ_INITIALIZE_OUTER\n");
682 outerTupleSlot = ExecProcNode(outerPlan);
683 node->mj_OuterTupleSlot = outerTupleSlot;
685 /* Compute join values and check for unmatchability */
686 switch (MJEvalOuterValues(node))
688 case MJEVAL_MATCHABLE:
689 /* OK to go get the first inner tuple */
690 node->mj_JoinState = EXEC_MJ_INITIALIZE_INNER;
692 case MJEVAL_NONMATCHABLE:
693 /* Stay in same state to fetch next outer tuple */
697 * Generate a fake join tuple with nulls for the
698 * inner tuple, and return it if it passes the
701 TupleTableSlot *result;
703 result = MJFillOuter(node);
708 case MJEVAL_ENDOFJOIN:
709 /* No more outer tuples */
710 MJ_printf("ExecMergeJoin: nothing in outer subplan\n");
714 * Need to emit right-join tuples for remaining
715 * inner tuples. We set MatchedInner = true to
716 * force the ENDOUTER state to advance inner.
718 node->mj_JoinState = EXEC_MJ_ENDOUTER;
719 node->mj_MatchedInner = true;
722 /* Otherwise we're done. */
727 case EXEC_MJ_INITIALIZE_INNER:
728 MJ_printf("ExecMergeJoin: EXEC_MJ_INITIALIZE_INNER\n");
730 innerTupleSlot = ExecProcNode(innerPlan);
731 node->mj_InnerTupleSlot = innerTupleSlot;
733 /* Compute join values and check for unmatchability */
734 switch (MJEvalInnerValues(node, innerTupleSlot))
736 case MJEVAL_MATCHABLE:
739 * OK, we have the initial tuples. Begin by skipping
740 * non-matching tuples.
742 node->mj_JoinState = EXEC_MJ_SKIP_TEST;
744 case MJEVAL_NONMATCHABLE:
745 /* Mark before advancing, if wanted */
746 if (node->mj_ExtraMarks)
747 ExecMarkPos(innerPlan);
748 /* Stay in same state to fetch next inner tuple */
752 * Generate a fake join tuple with nulls for the
753 * outer tuple, and return it if it passes the
756 TupleTableSlot *result;
758 result = MJFillInner(node);
763 case MJEVAL_ENDOFJOIN:
764 /* No more inner tuples */
765 MJ_printf("ExecMergeJoin: nothing in inner subplan\n");
769 * Need to emit left-join tuples for all outer
770 * tuples, including the one we just fetched. We
771 * set MatchedOuter = false to force the ENDINNER
772 * state to emit first tuple before advancing
775 node->mj_JoinState = EXEC_MJ_ENDINNER;
776 node->mj_MatchedOuter = false;
779 /* Otherwise we're done. */
785 * EXEC_MJ_JOINTUPLES means we have two tuples which satisfied
786 * the merge clause so we join them and then proceed to get
787 * the next inner tuple (EXEC_MJ_NEXTINNER).
789 case EXEC_MJ_JOINTUPLES:
790 MJ_printf("ExecMergeJoin: EXEC_MJ_JOINTUPLES\n");
793 * Set the next state machine state. The right things will
794 * happen whether we return this join tuple or just fall
795 * through to continue the state machine execution.
797 node->mj_JoinState = EXEC_MJ_NEXTINNER;
800 * Check the extra qual conditions to see if we actually want
801 * to return this join tuple. If not, can proceed with merge.
802 * We must distinguish the additional joinquals (which must
803 * pass to consider the tuples "matched" for outer-join logic)
804 * from the otherquals (which must pass before we actually
807 * We don't bother with a ResetExprContext here, on the
808 * assumption that we just did one while checking the merge
809 * qual. One per tuple should be sufficient. We do have to
810 * set up the econtext links to the tuples for ExecQual to
813 outerTupleSlot = node->mj_OuterTupleSlot;
814 econtext->ecxt_outertuple = outerTupleSlot;
815 innerTupleSlot = node->mj_InnerTupleSlot;
816 econtext->ecxt_innertuple = innerTupleSlot;
818 qualResult = (joinqual == NIL ||
819 ExecQual(joinqual, econtext, false));
820 MJ_DEBUG_QUAL(joinqual, qualResult);
824 node->mj_MatchedOuter = true;
825 node->mj_MatchedInner = true;
827 /* In an antijoin, we never return a matched tuple */
828 if (node->js.jointype == JOIN_ANTI)
830 node->mj_JoinState = EXEC_MJ_NEXTOUTER;
835 * In a semijoin, we'll consider returning the first
836 * match, but after that we're done with this outer tuple.
838 if (node->js.jointype == JOIN_SEMI)
839 node->mj_JoinState = EXEC_MJ_NEXTOUTER;
841 qualResult = (otherqual == NIL ||
842 ExecQual(otherqual, econtext, false));
843 MJ_DEBUG_QUAL(otherqual, qualResult);
848 * qualification succeeded. now form the desired
849 * projection tuple and return the slot containing it.
851 TupleTableSlot *result;
854 MJ_printf("ExecMergeJoin: returning tuple\n");
856 result = ExecProject(node->js.ps.ps_ProjInfo,
859 if (isDone != ExprEndResult)
861 node->js.ps.ps_TupFromTlist =
862 (isDone == ExprMultipleResult);
867 InstrCountFiltered2(node, 1);
870 InstrCountFiltered1(node, 1);
874 * EXEC_MJ_NEXTINNER means advance the inner scan to the next
875 * tuple. If the tuple is not nil, we then proceed to test it
876 * against the join qualification.
878 * Before advancing, we check to see if we must emit an
879 * outer-join fill tuple for this inner tuple.
881 case EXEC_MJ_NEXTINNER:
882 MJ_printf("ExecMergeJoin: EXEC_MJ_NEXTINNER\n");
884 if (doFillInner && !node->mj_MatchedInner)
887 * Generate a fake join tuple with nulls for the outer
888 * tuple, and return it if it passes the non-join quals.
890 TupleTableSlot *result;
892 node->mj_MatchedInner = true; /* do it only once */
894 result = MJFillInner(node);
900 * now we get the next inner tuple, if any. If there's none,
901 * advance to next outer tuple (which may be able to join to
902 * previously marked tuples).
904 * NB: must NOT do "extraMarks" here, since we may need to
905 * return to previously marked tuples.
907 innerTupleSlot = ExecProcNode(innerPlan);
908 node->mj_InnerTupleSlot = innerTupleSlot;
909 MJ_DEBUG_PROC_NODE(innerTupleSlot);
910 node->mj_MatchedInner = false;
912 /* Compute join values and check for unmatchability */
913 switch (MJEvalInnerValues(node, innerTupleSlot))
915 case MJEVAL_MATCHABLE:
918 * Test the new inner tuple to see if it matches
921 * If they do match, then we join them and move on to
922 * the next inner tuple (EXEC_MJ_JOINTUPLES).
924 * If they do not match then advance to next outer
927 compareResult = MJCompare(node);
928 MJ_DEBUG_COMPARE(compareResult);
930 if (compareResult == 0)
931 node->mj_JoinState = EXEC_MJ_JOINTUPLES;
934 Assert(compareResult < 0);
935 node->mj_JoinState = EXEC_MJ_NEXTOUTER;
938 case MJEVAL_NONMATCHABLE:
941 * It contains a NULL and hence can't match any outer
942 * tuple, so we can skip the comparison and assume the
943 * new tuple is greater than current outer.
945 node->mj_JoinState = EXEC_MJ_NEXTOUTER;
947 case MJEVAL_ENDOFJOIN:
950 * No more inner tuples. However, this might be only
951 * effective and not physical end of inner plan, so
952 * force mj_InnerTupleSlot to null to make sure we
953 * don't fetch more inner tuples. (We need this hack
954 * because we are not transiting to a state where the
955 * inner plan is assumed to be exhausted.)
957 node->mj_InnerTupleSlot = NULL;
958 node->mj_JoinState = EXEC_MJ_NEXTOUTER;
963 /*-------------------------------------------
964 * EXEC_MJ_NEXTOUTER means
967 * outer tuple - 5 5 - marked tuple
972 * we know we just bumped into the
973 * first inner tuple > current outer tuple (or possibly
974 * the end of the inner stream)
975 * so get a new outer tuple and then
976 * proceed to test it against the marked tuple
977 * (EXEC_MJ_TESTOUTER)
979 * Before advancing, we check to see if we must emit an
980 * outer-join fill tuple for this outer tuple.
981 *------------------------------------------------
983 case EXEC_MJ_NEXTOUTER:
984 MJ_printf("ExecMergeJoin: EXEC_MJ_NEXTOUTER\n");
986 if (doFillOuter && !node->mj_MatchedOuter)
989 * Generate a fake join tuple with nulls for the inner
990 * tuple, and return it if it passes the non-join quals.
992 TupleTableSlot *result;
994 node->mj_MatchedOuter = true; /* do it only once */
996 result = MJFillOuter(node);
1002 * now we get the next outer tuple, if any
1004 outerTupleSlot = ExecProcNode(outerPlan);
1005 node->mj_OuterTupleSlot = outerTupleSlot;
1006 MJ_DEBUG_PROC_NODE(outerTupleSlot);
1007 node->mj_MatchedOuter = false;
1009 /* Compute join values and check for unmatchability */
1010 switch (MJEvalOuterValues(node))
1012 case MJEVAL_MATCHABLE:
1013 /* Go test the new tuple against the marked tuple */
1014 node->mj_JoinState = EXEC_MJ_TESTOUTER;
1016 case MJEVAL_NONMATCHABLE:
1017 /* Can't match, so fetch next outer tuple */
1018 node->mj_JoinState = EXEC_MJ_NEXTOUTER;
1020 case MJEVAL_ENDOFJOIN:
1021 /* No more outer tuples */
1022 MJ_printf("ExecMergeJoin: end of outer subplan\n");
1023 innerTupleSlot = node->mj_InnerTupleSlot;
1024 if (doFillInner && !TupIsNull(innerTupleSlot))
1027 * Need to emit right-join tuples for remaining
1030 node->mj_JoinState = EXEC_MJ_ENDOUTER;
1033 /* Otherwise we're done. */
1038 /*--------------------------------------------------------
1039 * EXEC_MJ_TESTOUTER If the new outer tuple and the marked
1040 * tuple satisfy the merge clause then we know we have
1041 * duplicates in the outer scan so we have to restore the
1042 * inner scan to the marked tuple and proceed to join the
1043 * new outer tuple with the inner tuples.
1045 * This is the case when
1047 * 4 5 - marked tuple
1049 * new outer tuple - 5 5
1053 * new outer tuple == marked tuple
1055 * If the outer tuple fails the test, then we are done
1056 * with the marked tuples, and we have to look for a
1057 * match to the current inner tuple. So we will
1058 * proceed to skip outer tuples until outer >= inner
1059 * (EXEC_MJ_SKIP_TEST).
1061 * This is the case when
1064 * 5 5 - marked tuple
1066 * new outer tuple - 6 8 - inner tuple
1069 * new outer tuple > marked tuple
1071 *---------------------------------------------------------
1073 case EXEC_MJ_TESTOUTER:
1074 MJ_printf("ExecMergeJoin: EXEC_MJ_TESTOUTER\n");
1077 * Here we must compare the outer tuple with the marked inner
1078 * tuple. (We can ignore the result of MJEvalInnerValues,
1079 * since the marked inner tuple is certainly matchable.)
1081 innerTupleSlot = node->mj_MarkedTupleSlot;
1082 (void) MJEvalInnerValues(node, innerTupleSlot);
1084 compareResult = MJCompare(node);
1085 MJ_DEBUG_COMPARE(compareResult);
1087 if (compareResult == 0)
1090 * the merge clause matched so now we restore the inner
1091 * scan position to the first mark, and go join that tuple
1092 * (and any following ones) to the new outer.
1094 * NOTE: we do not need to worry about the MatchedInner
1095 * state for the rescanned inner tuples. We know all of
1096 * them will match this new outer tuple and therefore
1097 * won't be emitted as fill tuples. This works *only*
1098 * because we require the extra joinquals to be constant
1099 * when doing a right or full join --- otherwise some of
1100 * the rescanned tuples might fail the extra joinquals.
1101 * This obviously won't happen for a constant-true extra
1102 * joinqual, while the constant-false case is handled by
1103 * forcing the merge clause to never match, so we never
1106 ExecRestrPos(innerPlan);
1109 * ExecRestrPos probably should give us back a new Slot,
1110 * but since it doesn't, use the marked slot. (The
1111 * previously returned mj_InnerTupleSlot cannot be assumed
1112 * to hold the required tuple.)
1114 node->mj_InnerTupleSlot = innerTupleSlot;
1115 /* we need not do MJEvalInnerValues again */
1117 node->mj_JoinState = EXEC_MJ_JOINTUPLES;
1122 * if the new outer tuple didn't match the marked inner
1123 * tuple then we have a case like:
1126 * 4 4 - marked tuple
1131 * which means that all subsequent outer tuples will be
1132 * larger than our marked inner tuples. So we need not
1133 * revisit any of the marked tuples but can proceed to
1134 * look for a match to the current inner. If there's
1135 * no more inners, no more matches are possible.
1138 Assert(compareResult > 0);
1139 innerTupleSlot = node->mj_InnerTupleSlot;
1141 /* reload comparison data for current inner */
1142 switch (MJEvalInnerValues(node, innerTupleSlot))
1144 case MJEVAL_MATCHABLE:
1145 /* proceed to compare it to the current outer */
1146 node->mj_JoinState = EXEC_MJ_SKIP_TEST;
1148 case MJEVAL_NONMATCHABLE:
1151 * current inner can't possibly match any outer;
1152 * better to advance the inner scan than the
1155 node->mj_JoinState = EXEC_MJ_SKIPINNER_ADVANCE;
1157 case MJEVAL_ENDOFJOIN:
1158 /* No more inner tuples */
1162 * Need to emit left-join tuples for remaining
1165 node->mj_JoinState = EXEC_MJ_ENDINNER;
1168 /* Otherwise we're done. */
1174 /*----------------------------------------------------------
1175 * EXEC_MJ_SKIP means compare tuples and if they do not
1176 * match, skip whichever is lesser.
1183 * outer tuple - 6 8 - inner tuple
1187 * we have to advance the outer scan
1188 * until we find the outer 8.
1190 * On the other hand:
1195 * outer tuple - 12 8 - inner tuple
1199 * we have to advance the inner scan
1200 * until we find the inner 12.
1201 *----------------------------------------------------------
1203 case EXEC_MJ_SKIP_TEST:
1204 MJ_printf("ExecMergeJoin: EXEC_MJ_SKIP_TEST\n");
1207 * before we advance, make sure the current tuples do not
1208 * satisfy the mergeclauses. If they do, then we update the
1209 * marked tuple position and go join them.
1211 compareResult = MJCompare(node);
1212 MJ_DEBUG_COMPARE(compareResult);
1214 if (compareResult == 0)
1216 ExecMarkPos(innerPlan);
1218 MarkInnerTuple(node->mj_InnerTupleSlot, node);
1220 node->mj_JoinState = EXEC_MJ_JOINTUPLES;
1222 else if (compareResult < 0)
1223 node->mj_JoinState = EXEC_MJ_SKIPOUTER_ADVANCE;
1225 /* compareResult > 0 */
1226 node->mj_JoinState = EXEC_MJ_SKIPINNER_ADVANCE;
1230 * SKIPOUTER_ADVANCE: advance over an outer tuple that is
1231 * known not to join to any inner tuple.
1233 * Before advancing, we check to see if we must emit an
1234 * outer-join fill tuple for this outer tuple.
1236 case EXEC_MJ_SKIPOUTER_ADVANCE:
1237 MJ_printf("ExecMergeJoin: EXEC_MJ_SKIPOUTER_ADVANCE\n");
1239 if (doFillOuter && !node->mj_MatchedOuter)
1242 * Generate a fake join tuple with nulls for the inner
1243 * tuple, and return it if it passes the non-join quals.
1245 TupleTableSlot *result;
1247 node->mj_MatchedOuter = true; /* do it only once */
1249 result = MJFillOuter(node);
1255 * now we get the next outer tuple, if any
1257 outerTupleSlot = ExecProcNode(outerPlan);
1258 node->mj_OuterTupleSlot = outerTupleSlot;
1259 MJ_DEBUG_PROC_NODE(outerTupleSlot);
1260 node->mj_MatchedOuter = false;
1262 /* Compute join values and check for unmatchability */
1263 switch (MJEvalOuterValues(node))
1265 case MJEVAL_MATCHABLE:
1266 /* Go test the new tuple against the current inner */
1267 node->mj_JoinState = EXEC_MJ_SKIP_TEST;
1269 case MJEVAL_NONMATCHABLE:
1270 /* Can't match, so fetch next outer tuple */
1271 node->mj_JoinState = EXEC_MJ_SKIPOUTER_ADVANCE;
1273 case MJEVAL_ENDOFJOIN:
1274 /* No more outer tuples */
1275 MJ_printf("ExecMergeJoin: end of outer subplan\n");
1276 innerTupleSlot = node->mj_InnerTupleSlot;
1277 if (doFillInner && !TupIsNull(innerTupleSlot))
1280 * Need to emit right-join tuples for remaining
1283 node->mj_JoinState = EXEC_MJ_ENDOUTER;
1286 /* Otherwise we're done. */
1292 * SKIPINNER_ADVANCE: advance over an inner tuple that is
1293 * known not to join to any outer tuple.
1295 * Before advancing, we check to see if we must emit an
1296 * outer-join fill tuple for this inner tuple.
1298 case EXEC_MJ_SKIPINNER_ADVANCE:
1299 MJ_printf("ExecMergeJoin: EXEC_MJ_SKIPINNER_ADVANCE\n");
1301 if (doFillInner && !node->mj_MatchedInner)
1304 * Generate a fake join tuple with nulls for the outer
1305 * tuple, and return it if it passes the non-join quals.
1307 TupleTableSlot *result;
1309 node->mj_MatchedInner = true; /* do it only once */
1311 result = MJFillInner(node);
1316 /* Mark before advancing, if wanted */
1317 if (node->mj_ExtraMarks)
1318 ExecMarkPos(innerPlan);
1321 * now we get the next inner tuple, if any
1323 innerTupleSlot = ExecProcNode(innerPlan);
1324 node->mj_InnerTupleSlot = innerTupleSlot;
1325 MJ_DEBUG_PROC_NODE(innerTupleSlot);
1326 node->mj_MatchedInner = false;
1328 /* Compute join values and check for unmatchability */
1329 switch (MJEvalInnerValues(node, innerTupleSlot))
1331 case MJEVAL_MATCHABLE:
1332 /* proceed to compare it to the current outer */
1333 node->mj_JoinState = EXEC_MJ_SKIP_TEST;
1335 case MJEVAL_NONMATCHABLE:
1338 * current inner can't possibly match any outer;
1339 * better to advance the inner scan than the outer.
1341 node->mj_JoinState = EXEC_MJ_SKIPINNER_ADVANCE;
1343 case MJEVAL_ENDOFJOIN:
1344 /* No more inner tuples */
1345 MJ_printf("ExecMergeJoin: end of inner subplan\n");
1346 outerTupleSlot = node->mj_OuterTupleSlot;
1347 if (doFillOuter && !TupIsNull(outerTupleSlot))
1350 * Need to emit left-join tuples for remaining
1353 node->mj_JoinState = EXEC_MJ_ENDINNER;
1356 /* Otherwise we're done. */
1362 * EXEC_MJ_ENDOUTER means we have run out of outer tuples, but
1363 * are doing a right/full join and therefore must null-fill
1364 * any remaing unmatched inner tuples.
1366 case EXEC_MJ_ENDOUTER:
1367 MJ_printf("ExecMergeJoin: EXEC_MJ_ENDOUTER\n");
1369 Assert(doFillInner);
1371 if (!node->mj_MatchedInner)
1374 * Generate a fake join tuple with nulls for the outer
1375 * tuple, and return it if it passes the non-join quals.
1377 TupleTableSlot *result;
1379 node->mj_MatchedInner = true; /* do it only once */
1381 result = MJFillInner(node);
1386 /* Mark before advancing, if wanted */
1387 if (node->mj_ExtraMarks)
1388 ExecMarkPos(innerPlan);
1391 * now we get the next inner tuple, if any
1393 innerTupleSlot = ExecProcNode(innerPlan);
1394 node->mj_InnerTupleSlot = innerTupleSlot;
1395 MJ_DEBUG_PROC_NODE(innerTupleSlot);
1396 node->mj_MatchedInner = false;
1398 if (TupIsNull(innerTupleSlot))
1400 MJ_printf("ExecMergeJoin: end of inner subplan\n");
1404 /* Else remain in ENDOUTER state and process next tuple. */
1408 * EXEC_MJ_ENDINNER means we have run out of inner tuples, but
1409 * are doing a left/full join and therefore must null- fill
1410 * any remaing unmatched outer tuples.
1412 case EXEC_MJ_ENDINNER:
1413 MJ_printf("ExecMergeJoin: EXEC_MJ_ENDINNER\n");
1415 Assert(doFillOuter);
1417 if (!node->mj_MatchedOuter)
1420 * Generate a fake join tuple with nulls for the inner
1421 * tuple, and return it if it passes the non-join quals.
1423 TupleTableSlot *result;
1425 node->mj_MatchedOuter = true; /* do it only once */
1427 result = MJFillOuter(node);
1433 * now we get the next outer tuple, if any
1435 outerTupleSlot = ExecProcNode(outerPlan);
1436 node->mj_OuterTupleSlot = outerTupleSlot;
1437 MJ_DEBUG_PROC_NODE(outerTupleSlot);
1438 node->mj_MatchedOuter = false;
1440 if (TupIsNull(outerTupleSlot))
1442 MJ_printf("ExecMergeJoin: end of outer subplan\n");
1446 /* Else remain in ENDINNER state and process next tuple. */
1450 * broken state value?
1453 elog(ERROR, "unrecognized mergejoin state: %d",
1454 (int) node->mj_JoinState);
1459 /* ----------------------------------------------------------------
1461 * ----------------------------------------------------------------
1464 ExecInitMergeJoin(MergeJoin *node, EState *estate, int eflags)
1466 MergeJoinState *mergestate;
1468 /* check for unsupported flags */
1469 Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
1471 MJ1_printf("ExecInitMergeJoin: %s\n",
1472 "initializing node");
1475 * create state structure
1477 mergestate = makeNode(MergeJoinState);
1478 mergestate->js.ps.plan = (Plan *) node;
1479 mergestate->js.ps.state = estate;
1482 * Miscellaneous initialization
1484 * create expression context for node
1486 ExecAssignExprContext(estate, &mergestate->js.ps);
1489 * we need two additional econtexts in which we can compute the join
1490 * expressions from the left and right input tuples. The node's regular
1491 * econtext won't do because it gets reset too often.
1493 mergestate->mj_OuterEContext = CreateExprContext(estate);
1494 mergestate->mj_InnerEContext = CreateExprContext(estate);
1497 * initialize child expressions
1499 mergestate->js.ps.targetlist = (List *)
1500 ExecInitExpr((Expr *) node->join.plan.targetlist,
1501 (PlanState *) mergestate);
1502 mergestate->js.ps.qual = (List *)
1503 ExecInitExpr((Expr *) node->join.plan.qual,
1504 (PlanState *) mergestate);
1505 mergestate->js.jointype = node->join.jointype;
1506 mergestate->js.joinqual = (List *)
1507 ExecInitExpr((Expr *) node->join.joinqual,
1508 (PlanState *) mergestate);
1509 mergestate->mj_ConstFalseJoin = false;
1510 /* mergeclauses are handled below */
1513 * initialize child nodes
1515 * inner child must support MARK/RESTORE.
1517 outerPlanState(mergestate) = ExecInitNode(outerPlan(node), estate, eflags);
1518 innerPlanState(mergestate) = ExecInitNode(innerPlan(node), estate,
1519 eflags | EXEC_FLAG_MARK);
1522 * For certain types of inner child nodes, it is advantageous to issue
1523 * MARK every time we advance past an inner tuple we will never return to.
1524 * For other types, MARK on a tuple we cannot return to is a waste of
1525 * cycles. Detect which case applies and set mj_ExtraMarks if we want to
1526 * issue "unnecessary" MARK calls.
1528 * Currently, only Material wants the extra MARKs, and it will be helpful
1529 * only if eflags doesn't specify REWIND.
1531 if (IsA(innerPlan(node), Material) &&
1532 (eflags & EXEC_FLAG_REWIND) == 0)
1533 mergestate->mj_ExtraMarks = true;
1535 mergestate->mj_ExtraMarks = false;
1538 * tuple table initialization
1540 ExecInitResultTupleSlot(estate, &mergestate->js.ps);
1542 mergestate->mj_MarkedTupleSlot = ExecInitExtraTupleSlot(estate);
1543 ExecSetSlotDescriptor(mergestate->mj_MarkedTupleSlot,
1544 ExecGetResultType(innerPlanState(mergestate)));
1546 switch (node->join.jointype)
1550 mergestate->mj_FillOuter = false;
1551 mergestate->mj_FillInner = false;
1555 mergestate->mj_FillOuter = true;
1556 mergestate->mj_FillInner = false;
1557 mergestate->mj_NullInnerTupleSlot =
1558 ExecInitNullTupleSlot(estate,
1559 ExecGetResultType(innerPlanState(mergestate)));
1562 mergestate->mj_FillOuter = false;
1563 mergestate->mj_FillInner = true;
1564 mergestate->mj_NullOuterTupleSlot =
1565 ExecInitNullTupleSlot(estate,
1566 ExecGetResultType(outerPlanState(mergestate)));
1569 * Can't handle right or full join with non-constant extra
1570 * joinclauses. This should have been caught by planner.
1572 if (!check_constant_qual(node->join.joinqual,
1573 &mergestate->mj_ConstFalseJoin))
1575 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1576 errmsg("RIGHT JOIN is only supported with merge-joinable join conditions")));
1579 mergestate->mj_FillOuter = true;
1580 mergestate->mj_FillInner = true;
1581 mergestate->mj_NullOuterTupleSlot =
1582 ExecInitNullTupleSlot(estate,
1583 ExecGetResultType(outerPlanState(mergestate)));
1584 mergestate->mj_NullInnerTupleSlot =
1585 ExecInitNullTupleSlot(estate,
1586 ExecGetResultType(innerPlanState(mergestate)));
1589 * Can't handle right or full join with non-constant extra
1590 * joinclauses. This should have been caught by planner.
1592 if (!check_constant_qual(node->join.joinqual,
1593 &mergestate->mj_ConstFalseJoin))
1595 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1596 errmsg("FULL JOIN is only supported with merge-joinable join conditions")));
1599 elog(ERROR, "unrecognized join type: %d",
1600 (int) node->join.jointype);
1604 * initialize tuple type and projection info
1606 ExecAssignResultTypeFromTL(&mergestate->js.ps);
1607 ExecAssignProjectionInfo(&mergestate->js.ps, NULL);
1610 * preprocess the merge clauses
1612 mergestate->mj_NumClauses = list_length(node->mergeclauses);
1613 mergestate->mj_Clauses = MJExamineQuals(node->mergeclauses,
1614 node->mergeFamilies,
1615 node->mergeCollations,
1616 node->mergeStrategies,
1617 node->mergeNullsFirst,
1618 (PlanState *) mergestate);
1621 * initialize join state
1623 mergestate->mj_JoinState = EXEC_MJ_INITIALIZE_OUTER;
1624 mergestate->js.ps.ps_TupFromTlist = false;
1625 mergestate->mj_MatchedOuter = false;
1626 mergestate->mj_MatchedInner = false;
1627 mergestate->mj_OuterTupleSlot = NULL;
1628 mergestate->mj_InnerTupleSlot = NULL;
1631 * initialization successful
1633 MJ1_printf("ExecInitMergeJoin: %s\n",
1634 "node initialized");
1639 /* ----------------------------------------------------------------
1643 * frees storage allocated through C routines.
1644 * ----------------------------------------------------------------
1647 ExecEndMergeJoin(MergeJoinState *node)
1649 MJ1_printf("ExecEndMergeJoin: %s\n",
1650 "ending node processing");
1653 * Free the exprcontext
1655 ExecFreeExprContext(&node->js.ps);
1658 * clean out the tuple table
1660 ExecClearTuple(node->js.ps.ps_ResultTupleSlot);
1661 ExecClearTuple(node->mj_MarkedTupleSlot);
1664 * shut down the subplans
1666 ExecEndNode(innerPlanState(node));
1667 ExecEndNode(outerPlanState(node));
1669 MJ1_printf("ExecEndMergeJoin: %s\n",
1670 "node processing ended");
1674 ExecReScanMergeJoin(MergeJoinState *node)
1676 ExecClearTuple(node->mj_MarkedTupleSlot);
1678 node->mj_JoinState = EXEC_MJ_INITIALIZE_OUTER;
1679 node->js.ps.ps_TupFromTlist = false;
1680 node->mj_MatchedOuter = false;
1681 node->mj_MatchedInner = false;
1682 node->mj_OuterTupleSlot = NULL;
1683 node->mj_InnerTupleSlot = NULL;
1686 * if chgParam of subnodes is not null then plans will be re-scanned by
1687 * first ExecProcNode.
1689 if (node->js.ps.lefttree->chgParam == NULL)
1690 ExecReScan(node->js.ps.lefttree);
1691 if (node->js.ps.righttree->chgParam == NULL)
1692 ExecReScan(node->js.ps.righttree);