1 /*-------------------------------------------------------------------------
4 * routines supporting merge joins
6 * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/backend/executor/nodeMergejoin.c,v 1.80 2006/06/16 18:42:22 tgl Exp $
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, when initializing the merge-join node, we look up the
43 * associated sort operators. We assume the planner has seen to it
44 * that the inputs are correctly sorted by these operators. Rather
45 * than directly executing the merge join clauses, we evaluate the
46 * left and right key expressions separately and then compare the
47 * columns one at a time (see MJCompare).
50 * Consider the above relations and suppose that the executor has
51 * just joined the first outer "5" with the last inner "5". The
52 * next step is of course to join the second outer "5" with all
53 * the inner "5's". This requires repositioning the inner "cursor"
54 * to point at the first inner "5". This is done by "marking" the
55 * first inner 5 so we can restore the "cursor" to it before joining
56 * with the second outer 5. The access method interface provides
57 * routines to mark and restore to a tuple.
60 * Essential operation of the merge join algorithm is as follows:
63 * get initial outer and inner tuples INITIALIZE
65 * while (outer != inner) { SKIP_TEST
67 * advance outer SKIPOUTER_ADVANCE
69 * advance inner SKIPINNER_ADVANCE
71 * mark inner position SKIP_TEST
73 * while (outer == inner) {
74 * join tuples JOINTUPLES
75 * advance inner position NEXTINNER
77 * advance outer position NEXTOUTER
78 * if (outer == mark) TESTOUTER
79 * restore inner position to mark TESTOUTER
81 * break // return to top of outer loop
86 * The merge join operation is coded in the fashion
87 * of a state machine. At each state, we do something and then
88 * proceed to another state. This state is stored in the node's
89 * execution state information and is preserved across calls to
90 * ExecMergeJoin. -cim 10/31/89
94 #include "access/heapam.h"
95 #include "access/nbtree.h"
96 #include "access/printtup.h"
97 #include "catalog/pg_amop.h"
98 #include "catalog/pg_operator.h"
99 #include "executor/execdebug.h"
100 #include "executor/execdefs.h"
101 #include "executor/nodeMergejoin.h"
102 #include "miscadmin.h"
103 #include "utils/acl.h"
104 #include "utils/catcache.h"
105 #include "utils/lsyscache.h"
106 #include "utils/memutils.h"
107 #include "utils/syscache.h"
111 * Comparison strategies supported by MJCompare
113 * XXX eventually should extend these to support descending-order sorts.
114 * There are some tricky issues however about being sure we are on the same
115 * page as the underlying sort or index as to which end NULLs sort to.
119 MERGEFUNC_LT, /* raw "<" operator */
120 MERGEFUNC_CMP /* -1 / 0 / 1 three-way comparator */
123 /* Runtime data for each mergejoin clause */
124 typedef struct MergeJoinClauseData
126 /* Executable expression trees */
127 ExprState *lexpr; /* left-hand (outer) input expression */
128 ExprState *rexpr; /* right-hand (inner) input expression */
131 * If we have a current left or right input tuple, the values of the
132 * expressions are loaded into these fields:
134 Datum ldatum; /* current left-hand value */
135 Datum rdatum; /* current right-hand value */
136 bool lisnull; /* and their isnull flags */
140 * Remember whether mergejoin operator is strict (usually it will be).
141 * NOTE: if it's not strict, we still assume it cannot return true for one
142 * null and one non-null input.
147 * The comparison strategy in use, and the lookup info to let us call the
148 * needed comparison routines. eqfinfo is the "=" operator itself.
149 * cmpfinfo is either the btree comparator or the "<" operator.
151 MergeFunctionKind cmpstrategy;
154 } MergeJoinClauseData;
157 #define MarkInnerTuple(innerTupleSlot, mergestate) \
158 ExecCopySlot((mergestate)->mj_MarkedTupleSlot, (innerTupleSlot))
164 * This deconstructs the list of mergejoinable expressions, which is given
165 * to us by the planner in the form of a list of "leftexpr = rightexpr"
166 * expression trees in the order matching the sort columns of the inputs.
167 * We build an array of MergeJoinClause structs containing the information
168 * we will need at runtime. Each struct essentially tells us how to compare
169 * the two expressions from the original clause.
171 * The best, most efficient way to compare two expressions is to use a btree
172 * comparison support routine, since that requires only one function call
173 * per comparison. Hence we try to find a btree opclass that matches the
174 * mergejoinable operator. If we cannot find one, we'll have to call both
175 * the "=" and (often) the "<" operator for each comparison.
177 static MergeJoinClause
178 MJExamineQuals(List *qualList, PlanState *parent)
180 MergeJoinClause clauses;
181 int nClauses = list_length(qualList);
185 clauses = (MergeJoinClause) palloc0(nClauses * sizeof(MergeJoinClauseData));
190 OpExpr *qual = (OpExpr *) lfirst(l);
191 MergeJoinClause clause = &clauses[iClause];
200 if (!IsA(qual, OpExpr))
201 elog(ERROR, "mergejoin clause is not an OpExpr");
204 * Prepare the input expressions for execution.
206 clause->lexpr = ExecInitExpr((Expr *) linitial(qual->args), parent);
207 clause->rexpr = ExecInitExpr((Expr *) lsecond(qual->args), parent);
210 * Check permission to call the mergejoinable operator. For
211 * predictability, we check this even if we end up not using it.
213 aclresult = pg_proc_aclcheck(qual->opfuncid, GetUserId(), ACL_EXECUTE);
214 if (aclresult != ACLCHECK_OK)
215 aclcheck_error(aclresult, ACL_KIND_PROC,
216 get_func_name(qual->opfuncid));
218 /* Set up the fmgr lookup information */
219 fmgr_info(qual->opfuncid, &(clause->eqfinfo));
221 /* And remember strictness */
222 clause->mergestrict = clause->eqfinfo.fn_strict;
225 * Lookup the comparison operators that go with the mergejoinable
226 * top-level operator. (This will elog if the operator isn't
227 * mergejoinable, which would be the planner's mistake.)
229 op_mergejoin_crossops(qual->opno,
235 clause->cmpstrategy = MERGEFUNC_LT;
238 * Look for a btree opclass including all three operators. This is
239 * much like SelectSortFunction except we insist on matching all the
240 * operators provided, and it can be a cross-type opclass.
242 * XXX for now, insist on forward sort so that NULLs can be counted on
245 catlist = SearchSysCacheList(AMOPOPID, 1,
246 ObjectIdGetDatum(qual->opno),
249 for (i = 0; i < catlist->n_members; i++)
251 HeapTuple tuple = &catlist->members[i]->tuple;
252 Form_pg_amop aform = (Form_pg_amop) GETSTRUCT(tuple);
253 Oid opcid = aform->amopclaid;
255 if (aform->amopstrategy != BTEqualStrategyNumber)
257 if (!opclass_is_btree(opcid))
259 if (get_op_opclass_strategy(ltop, opcid) == BTLessStrategyNumber &&
260 get_op_opclass_strategy(gtop, opcid) == BTGreaterStrategyNumber)
262 clause->cmpstrategy = MERGEFUNC_CMP;
263 ltproc = get_opclass_proc(opcid, aform->amopsubtype,
265 Assert(RegProcedureIsValid(ltproc));
266 break; /* done looking */
270 ReleaseSysCacheList(catlist);
272 /* Check permission to call "<" operator or cmp function */
273 aclresult = pg_proc_aclcheck(ltproc, GetUserId(), ACL_EXECUTE);
274 if (aclresult != ACLCHECK_OK)
275 aclcheck_error(aclresult, ACL_KIND_PROC,
276 get_func_name(ltproc));
278 /* Set up the fmgr lookup information */
279 fmgr_info(ltproc, &(clause->cmpfinfo));
290 * Compute the values of the mergejoined expressions for the current
291 * outer tuple. We also detect whether it's impossible for the current
292 * outer tuple to match anything --- this is true if it yields a NULL
293 * input for any strict mergejoin operator.
295 * We evaluate the values in OuterEContext, which can be reset each
296 * time we move to a new tuple.
299 MJEvalOuterValues(MergeJoinState *mergestate)
301 ExprContext *econtext = mergestate->mj_OuterEContext;
302 bool canmatch = true;
304 MemoryContext oldContext;
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,
317 &clause->lisnull, NULL);
318 if (clause->lisnull && clause->mergestrict)
322 MemoryContextSwitchTo(oldContext);
330 * Same as above, but for the inner tuple. Here, we have to be prepared
331 * to load data from either the true current inner, or the marked inner,
332 * so caller must tell us which slot to load from.
335 MJEvalInnerValues(MergeJoinState *mergestate, TupleTableSlot *innerslot)
337 ExprContext *econtext = mergestate->mj_InnerEContext;
338 bool canmatch = true;
340 MemoryContext oldContext;
342 ResetExprContext(econtext);
344 oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
346 econtext->ecxt_innertuple = innerslot;
348 for (i = 0; i < mergestate->mj_NumClauses; i++)
350 MergeJoinClause clause = &mergestate->mj_Clauses[i];
352 clause->rdatum = ExecEvalExpr(clause->rexpr, econtext,
353 &clause->risnull, NULL);
354 if (clause->risnull && clause->mergestrict)
358 MemoryContextSwitchTo(oldContext);
366 * Compare the mergejoinable values of the current two input tuples
367 * and return 0 if they are equal (ie, the mergejoin equalities all
368 * succeed), +1 if outer > inner, -1 if outer < inner.
370 * MJEvalOuterValues and MJEvalInnerValues must already have been called
371 * for the current outer and inner tuples, respectively.
374 MJCompare(MergeJoinState *mergestate)
377 bool nulleqnull = false;
378 ExprContext *econtext = mergestate->js.ps.ps_ExprContext;
380 MemoryContext oldContext;
381 FunctionCallInfoData fcinfo;
384 * Call the comparison functions in short-lived context, in case they leak
387 ResetExprContext(econtext);
389 oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
391 for (i = 0; i < mergestate->mj_NumClauses; i++)
393 MergeJoinClause clause = &mergestate->mj_Clauses[i];
397 * Deal with null inputs. We treat NULL as sorting after non-NULL.
399 * If both inputs are NULL, and the comparison function isn't strict,
400 * then we call it and check for a true result (this allows operators
401 * that behave like IS NOT DISTINCT to be mergejoinable). If the
402 * function is strict or returns false, we temporarily pretend NULL ==
403 * NULL and contine checking remaining columns.
409 if (!clause->eqfinfo.fn_strict)
411 InitFunctionCallInfoData(fcinfo, &(clause->eqfinfo), 2,
413 fcinfo.arg[0] = clause->ldatum;
414 fcinfo.arg[1] = clause->rdatum;
415 fcinfo.argnull[0] = true;
416 fcinfo.argnull[1] = true;
417 fresult = FunctionCallInvoke(&fcinfo);
418 if (!fcinfo.isnull && DatumGetBool(fresult))
420 /* treat nulls as really equal */
427 /* NULL > non-NULL */
433 /* non-NULL < NULL */
438 if (clause->cmpstrategy == MERGEFUNC_LT)
440 InitFunctionCallInfoData(fcinfo, &(clause->eqfinfo), 2,
442 fcinfo.arg[0] = clause->ldatum;
443 fcinfo.arg[1] = clause->rdatum;
444 fcinfo.argnull[0] = false;
445 fcinfo.argnull[1] = false;
446 fresult = FunctionCallInvoke(&fcinfo);
452 else if (DatumGetBool(fresult))
457 InitFunctionCallInfoData(fcinfo, &(clause->cmpfinfo), 2,
459 fcinfo.arg[0] = clause->ldatum;
460 fcinfo.arg[1] = clause->rdatum;
461 fcinfo.argnull[0] = false;
462 fcinfo.argnull[1] = false;
463 fresult = FunctionCallInvoke(&fcinfo);
469 else if (DatumGetBool(fresult))
483 /* must be MERGEFUNC_CMP */
485 InitFunctionCallInfoData(fcinfo, &(clause->cmpfinfo), 2,
487 fcinfo.arg[0] = clause->ldatum;
488 fcinfo.arg[1] = clause->rdatum;
489 fcinfo.argnull[0] = false;
490 fcinfo.argnull[1] = false;
491 fresult = FunctionCallInvoke(&fcinfo);
497 else if (DatumGetInt32(fresult) == 0)
502 else if (DatumGetInt32(fresult) < 0)
518 * If we had any null comparison results or NULL-vs-NULL inputs, we do not
519 * want to report that the tuples are equal. Instead, if result is still
520 * 0, change it to +1. This will result in advancing the inner side of
523 if (nulleqnull && result == 0)
526 MemoryContextSwitchTo(oldContext);
533 * Generate a fake join tuple with nulls for the inner tuple,
534 * and return it if it passes the non-join quals.
536 static TupleTableSlot *
537 MJFillOuter(MergeJoinState *node)
539 ExprContext *econtext = node->js.ps.ps_ExprContext;
540 List *otherqual = node->js.ps.qual;
542 ResetExprContext(econtext);
544 econtext->ecxt_outertuple = node->mj_OuterTupleSlot;
545 econtext->ecxt_innertuple = node->mj_NullInnerTupleSlot;
547 if (ExecQual(otherqual, econtext, false))
550 * qualification succeeded. now form the desired projection tuple and
551 * return the slot containing it.
553 TupleTableSlot *result;
556 MJ_printf("ExecMergeJoin: returning outer fill tuple\n");
558 result = ExecProject(node->js.ps.ps_ProjInfo, &isDone);
560 if (isDone != ExprEndResult)
562 node->js.ps.ps_TupFromTlist =
563 (isDone == ExprMultipleResult);
572 * Generate a fake join tuple with nulls for the outer tuple,
573 * and return it if it passes the non-join quals.
575 static TupleTableSlot *
576 MJFillInner(MergeJoinState *node)
578 ExprContext *econtext = node->js.ps.ps_ExprContext;
579 List *otherqual = node->js.ps.qual;
581 ResetExprContext(econtext);
583 econtext->ecxt_outertuple = node->mj_NullOuterTupleSlot;
584 econtext->ecxt_innertuple = node->mj_InnerTupleSlot;
586 if (ExecQual(otherqual, econtext, false))
589 * qualification succeeded. now form the desired projection tuple and
590 * return the slot containing it.
592 TupleTableSlot *result;
595 MJ_printf("ExecMergeJoin: returning inner fill tuple\n");
597 result = ExecProject(node->js.ps.ps_ProjInfo, &isDone);
599 if (isDone != ExprEndResult)
601 node->js.ps.ps_TupFromTlist =
602 (isDone == ExprMultipleResult);
611 /* ----------------------------------------------------------------
614 * This function is called through the MJ_dump() macro
615 * when EXEC_MERGEJOINDEBUG is defined
616 * ----------------------------------------------------------------
618 #ifdef EXEC_MERGEJOINDEBUG
621 ExecMergeTupleDumpOuter(MergeJoinState *mergestate)
623 TupleTableSlot *outerSlot = mergestate->mj_OuterTupleSlot;
625 printf("==== outer tuple ====\n");
626 if (TupIsNull(outerSlot))
629 MJ_debugtup(outerSlot);
633 ExecMergeTupleDumpInner(MergeJoinState *mergestate)
635 TupleTableSlot *innerSlot = mergestate->mj_InnerTupleSlot;
637 printf("==== inner tuple ====\n");
638 if (TupIsNull(innerSlot))
641 MJ_debugtup(innerSlot);
645 ExecMergeTupleDumpMarked(MergeJoinState *mergestate)
647 TupleTableSlot *markedSlot = mergestate->mj_MarkedTupleSlot;
649 printf("==== marked tuple ====\n");
650 if (TupIsNull(markedSlot))
653 MJ_debugtup(markedSlot);
657 ExecMergeTupleDump(MergeJoinState *mergestate)
659 printf("******** ExecMergeTupleDump ********\n");
661 ExecMergeTupleDumpOuter(mergestate);
662 ExecMergeTupleDumpInner(mergestate);
663 ExecMergeTupleDumpMarked(mergestate);
665 printf("******** \n");
669 /* ----------------------------------------------------------------
671 * ----------------------------------------------------------------
674 ExecMergeJoin(MergeJoinState *node)
681 PlanState *innerPlan;
682 TupleTableSlot *innerTupleSlot;
683 PlanState *outerPlan;
684 TupleTableSlot *outerTupleSlot;
685 ExprContext *econtext;
690 * get information from node
692 estate = node->js.ps.state;
693 innerPlan = innerPlanState(node);
694 outerPlan = outerPlanState(node);
695 econtext = node->js.ps.ps_ExprContext;
696 joinqual = node->js.joinqual;
697 otherqual = node->js.ps.qual;
698 doFillOuter = node->mj_FillOuter;
699 doFillInner = node->mj_FillInner;
702 * Check to see if we're still projecting out tuples from a previous join
703 * tuple (because there is a function-returning-set in the projection
704 * expressions). If so, try to project another one.
706 if (node->js.ps.ps_TupFromTlist)
708 TupleTableSlot *result;
711 result = ExecProject(node->js.ps.ps_ProjInfo, &isDone);
712 if (isDone == ExprMultipleResult)
714 /* Done with that source tuple... */
715 node->js.ps.ps_TupFromTlist = false;
719 * Reset per-tuple memory context to free any expression evaluation
720 * storage allocated in the previous tuple cycle. Note this can't happen
721 * until we're done projecting out tuples from a join tuple.
723 ResetExprContext(econtext);
726 * ok, everything is setup.. let's go to work
733 * get the current state of the join and do things accordingly.
735 switch (node->mj_JoinState)
738 * EXEC_MJ_INITIALIZE_OUTER means that this is the first time
739 * ExecMergeJoin() has been called and so we have to fetch the
740 * first matchable tuple for both outer and inner subplans. We
741 * do the outer side in INITIALIZE_OUTER state, then advance
742 * to INITIALIZE_INNER state for the inner subplan.
744 case EXEC_MJ_INITIALIZE_OUTER:
745 MJ_printf("ExecMergeJoin: EXEC_MJ_INITIALIZE_OUTER\n");
747 outerTupleSlot = ExecProcNode(outerPlan);
748 node->mj_OuterTupleSlot = outerTupleSlot;
749 if (TupIsNull(outerTupleSlot))
751 MJ_printf("ExecMergeJoin: nothing in outer subplan\n");
755 * Need to emit right-join tuples for remaining inner
756 * tuples. We set MatchedInner = true to force the
757 * ENDOUTER state to advance inner.
759 node->mj_JoinState = EXEC_MJ_ENDOUTER;
760 node->mj_MatchedInner = true;
763 /* Otherwise we're done. */
767 /* Compute join values and check for unmatchability */
768 if (MJEvalOuterValues(node))
770 /* OK to go get the first inner tuple */
771 node->mj_JoinState = EXEC_MJ_INITIALIZE_INNER;
775 /* Stay in same state to fetch next outer tuple */
779 * Generate a fake join tuple with nulls for the inner
780 * tuple, and return it if it passes the non-join
783 TupleTableSlot *result;
785 result = MJFillOuter(node);
792 case EXEC_MJ_INITIALIZE_INNER:
793 MJ_printf("ExecMergeJoin: EXEC_MJ_INITIALIZE_INNER\n");
795 innerTupleSlot = ExecProcNode(innerPlan);
796 node->mj_InnerTupleSlot = innerTupleSlot;
797 if (TupIsNull(innerTupleSlot))
799 MJ_printf("ExecMergeJoin: nothing in inner subplan\n");
803 * Need to emit left-join tuples for all outer tuples,
804 * including the one we just fetched. We set
805 * MatchedOuter = false to force the ENDINNER state to
806 * emit first tuple before advancing outer.
808 node->mj_JoinState = EXEC_MJ_ENDINNER;
809 node->mj_MatchedOuter = false;
812 /* Otherwise we're done. */
816 /* Compute join values and check for unmatchability */
817 if (MJEvalInnerValues(node, innerTupleSlot))
820 * OK, we have the initial tuples. Begin by skipping
821 * non-matching tuples.
823 node->mj_JoinState = EXEC_MJ_SKIP_TEST;
827 /* Stay in same state to fetch next inner tuple */
831 * Generate a fake join tuple with nulls for the outer
832 * tuple, and return it if it passes the non-join
835 TupleTableSlot *result;
837 result = MJFillInner(node);
845 * EXEC_MJ_JOINTUPLES means we have two tuples which satisfied
846 * the merge clause so we join them and then proceed to get
847 * the next inner tuple (EXEC_MJ_NEXTINNER).
849 case EXEC_MJ_JOINTUPLES:
850 MJ_printf("ExecMergeJoin: EXEC_MJ_JOINTUPLES\n");
853 * Set the next state machine state. The right things will
854 * happen whether we return this join tuple or just fall
855 * through to continue the state machine execution.
857 node->mj_JoinState = EXEC_MJ_NEXTINNER;
860 * Check the extra qual conditions to see if we actually want
861 * to return this join tuple. If not, can proceed with merge.
862 * We must distinguish the additional joinquals (which must
863 * pass to consider the tuples "matched" for outer-join logic)
864 * from the otherquals (which must pass before we actually
867 * We don't bother with a ResetExprContext here, on the
868 * assumption that we just did one while checking the merge
869 * qual. One per tuple should be sufficient. We do have to
870 * set up the econtext links to the tuples for ExecQual to
873 outerTupleSlot = node->mj_OuterTupleSlot;
874 econtext->ecxt_outertuple = outerTupleSlot;
875 innerTupleSlot = node->mj_InnerTupleSlot;
876 econtext->ecxt_innertuple = innerTupleSlot;
878 if (node->js.jointype == JOIN_IN &&
879 node->mj_MatchedOuter)
883 qualResult = (joinqual == NIL ||
884 ExecQual(joinqual, econtext, false));
885 MJ_DEBUG_QUAL(joinqual, qualResult);
890 node->mj_MatchedOuter = true;
891 node->mj_MatchedInner = true;
893 qualResult = (otherqual == NIL ||
894 ExecQual(otherqual, econtext, false));
895 MJ_DEBUG_QUAL(otherqual, qualResult);
900 * qualification succeeded. now form the desired
901 * projection tuple and return the slot containing it.
903 TupleTableSlot *result;
906 MJ_printf("ExecMergeJoin: returning tuple\n");
908 result = ExecProject(node->js.ps.ps_ProjInfo,
911 if (isDone != ExprEndResult)
913 node->js.ps.ps_TupFromTlist =
914 (isDone == ExprMultipleResult);
922 * EXEC_MJ_NEXTINNER means advance the inner scan to the next
923 * tuple. If the tuple is not nil, we then proceed to test it
924 * against the join qualification.
926 * Before advancing, we check to see if we must emit an
927 * outer-join fill tuple for this inner tuple.
929 case EXEC_MJ_NEXTINNER:
930 MJ_printf("ExecMergeJoin: EXEC_MJ_NEXTINNER\n");
932 if (doFillInner && !node->mj_MatchedInner)
935 * Generate a fake join tuple with nulls for the outer
936 * tuple, and return it if it passes the non-join quals.
938 TupleTableSlot *result;
940 node->mj_MatchedInner = true; /* do it only once */
942 result = MJFillInner(node);
948 * now we get the next inner tuple, if any. If there's none,
949 * advance to next outer tuple (which may be able to join to
950 * previously marked tuples).
952 innerTupleSlot = ExecProcNode(innerPlan);
953 node->mj_InnerTupleSlot = innerTupleSlot;
954 MJ_DEBUG_PROC_NODE(innerTupleSlot);
955 node->mj_MatchedInner = false;
957 if (TupIsNull(innerTupleSlot))
959 node->mj_JoinState = EXEC_MJ_NEXTOUTER;
964 * Load up the new inner tuple's comparison values. If we
965 * see that it contains a NULL and hence can't match any
966 * outer tuple, we can skip the comparison and assume the
967 * new tuple is greater than current outer.
969 if (!MJEvalInnerValues(node, innerTupleSlot))
971 node->mj_JoinState = EXEC_MJ_NEXTOUTER;
976 * Test the new inner tuple to see if it matches outer.
978 * If they do match, then we join them and move on to the next
979 * inner tuple (EXEC_MJ_JOINTUPLES).
981 * If they do not match then advance to next outer tuple.
983 compareResult = MJCompare(node);
984 MJ_DEBUG_COMPARE(compareResult);
986 if (compareResult == 0)
987 node->mj_JoinState = EXEC_MJ_JOINTUPLES;
990 Assert(compareResult < 0);
991 node->mj_JoinState = EXEC_MJ_NEXTOUTER;
995 /*-------------------------------------------
996 * EXEC_MJ_NEXTOUTER means
999 * outer tuple - 5 5 - marked tuple
1004 * we know we just bumped into the
1005 * first inner tuple > current outer tuple (or possibly
1006 * the end of the inner stream)
1007 * so get a new outer tuple and then
1008 * proceed to test it against the marked tuple
1009 * (EXEC_MJ_TESTOUTER)
1011 * Before advancing, we check to see if we must emit an
1012 * outer-join fill tuple for this outer tuple.
1013 *------------------------------------------------
1015 case EXEC_MJ_NEXTOUTER:
1016 MJ_printf("ExecMergeJoin: EXEC_MJ_NEXTOUTER\n");
1018 if (doFillOuter && !node->mj_MatchedOuter)
1021 * Generate a fake join tuple with nulls for the inner
1022 * tuple, and return it if it passes the non-join quals.
1024 TupleTableSlot *result;
1026 node->mj_MatchedOuter = true; /* do it only once */
1028 result = MJFillOuter(node);
1034 * now we get the next outer tuple, if any
1036 outerTupleSlot = ExecProcNode(outerPlan);
1037 node->mj_OuterTupleSlot = outerTupleSlot;
1038 MJ_DEBUG_PROC_NODE(outerTupleSlot);
1039 node->mj_MatchedOuter = false;
1042 * if the outer tuple is null then we are done with the join,
1043 * unless we have inner tuples we need to null-fill.
1045 if (TupIsNull(outerTupleSlot))
1047 MJ_printf("ExecMergeJoin: end of outer subplan\n");
1048 innerTupleSlot = node->mj_InnerTupleSlot;
1049 if (doFillInner && !TupIsNull(innerTupleSlot))
1052 * Need to emit right-join tuples for remaining inner
1055 node->mj_JoinState = EXEC_MJ_ENDOUTER;
1058 /* Otherwise we're done. */
1062 /* Compute join values and check for unmatchability */
1063 if (MJEvalOuterValues(node))
1065 /* Go test the new tuple against the marked tuple */
1066 node->mj_JoinState = EXEC_MJ_TESTOUTER;
1070 /* Can't match, so fetch next outer tuple */
1071 node->mj_JoinState = EXEC_MJ_NEXTOUTER;
1075 /*--------------------------------------------------------
1076 * EXEC_MJ_TESTOUTER If the new outer tuple and the marked
1077 * tuple satisfy the merge clause then we know we have
1078 * duplicates in the outer scan so we have to restore the
1079 * inner scan to the marked tuple and proceed to join the
1080 * new outer tuple with the inner tuples.
1082 * This is the case when
1084 * 4 5 - marked tuple
1086 * new outer tuple - 5 5
1090 * new outer tuple == marked tuple
1092 * If the outer tuple fails the test, then we are done
1093 * with the marked tuples, and we have to look for a
1094 * match to the current inner tuple. So we will
1095 * proceed to skip outer tuples until outer >= inner
1096 * (EXEC_MJ_SKIP_TEST).
1098 * This is the case when
1101 * 5 5 - marked tuple
1103 * new outer tuple - 6 8 - inner tuple
1106 * new outer tuple > marked tuple
1108 *---------------------------------------------------------
1110 case EXEC_MJ_TESTOUTER:
1111 MJ_printf("ExecMergeJoin: EXEC_MJ_TESTOUTER\n");
1114 * Here we must compare the outer tuple with the marked inner
1115 * tuple. (We can ignore the result of MJEvalInnerValues,
1116 * since the marked inner tuple is certainly matchable.)
1118 innerTupleSlot = node->mj_MarkedTupleSlot;
1119 (void) MJEvalInnerValues(node, innerTupleSlot);
1121 compareResult = MJCompare(node);
1122 MJ_DEBUG_COMPARE(compareResult);
1124 if (compareResult == 0)
1127 * the merge clause matched so now we restore the inner
1128 * scan position to the first mark, and go join that tuple
1129 * (and any following ones) to the new outer.
1131 * NOTE: we do not need to worry about the MatchedInner
1132 * state for the rescanned inner tuples. We know all of
1133 * them will match this new outer tuple and therefore
1134 * won't be emitted as fill tuples. This works *only*
1135 * because we require the extra joinquals to be nil when
1136 * doing a right or full join --- otherwise some of the
1137 * rescanned tuples might fail the extra joinquals.
1139 ExecRestrPos(innerPlan);
1142 * ExecRestrPos probably should give us back a new Slot,
1143 * but since it doesn't, use the marked slot. (The
1144 * previously returned mj_InnerTupleSlot cannot be assumed
1145 * to hold the required tuple.)
1147 node->mj_InnerTupleSlot = innerTupleSlot;
1148 /* we need not do MJEvalInnerValues again */
1150 node->mj_JoinState = EXEC_MJ_JOINTUPLES;
1155 * if the new outer tuple didn't match the marked inner
1156 * tuple then we have a case like:
1159 * 4 4 - marked tuple
1164 * which means that all subsequent outer tuples will be
1165 * larger than our marked inner tuples. So we need not
1166 * revisit any of the marked tuples but can proceed to
1167 * look for a match to the current inner. If there's
1168 * no more inners, we are done.
1171 Assert(compareResult > 0);
1172 innerTupleSlot = node->mj_InnerTupleSlot;
1173 if (TupIsNull(innerTupleSlot))
1178 * Need to emit left-join tuples for remaining
1181 node->mj_JoinState = EXEC_MJ_ENDINNER;
1184 /* Otherwise we're done. */
1188 /* reload comparison data for current inner */
1189 if (MJEvalInnerValues(node, innerTupleSlot))
1191 /* proceed to compare it to the current outer */
1192 node->mj_JoinState = EXEC_MJ_SKIP_TEST;
1197 * current inner can't possibly match any outer;
1198 * better to advance the inner scan than the outer.
1200 node->mj_JoinState = EXEC_MJ_SKIPINNER_ADVANCE;
1205 /*----------------------------------------------------------
1206 * EXEC_MJ_SKIP means compare tuples and if they do not
1207 * match, skip whichever is lesser.
1214 * outer tuple - 6 8 - inner tuple
1218 * we have to advance the outer scan
1219 * until we find the outer 8.
1221 * On the other hand:
1226 * outer tuple - 12 8 - inner tuple
1230 * we have to advance the inner scan
1231 * until we find the inner 12.
1232 *----------------------------------------------------------
1234 case EXEC_MJ_SKIP_TEST:
1235 MJ_printf("ExecMergeJoin: EXEC_MJ_SKIP_TEST\n");
1238 * before we advance, make sure the current tuples do not
1239 * satisfy the mergeclauses. If they do, then we update the
1240 * marked tuple position and go join them.
1242 compareResult = MJCompare(node);
1243 MJ_DEBUG_COMPARE(compareResult);
1245 if (compareResult == 0)
1247 ExecMarkPos(innerPlan);
1249 MarkInnerTuple(node->mj_InnerTupleSlot, node);
1251 node->mj_JoinState = EXEC_MJ_JOINTUPLES;
1253 else if (compareResult < 0)
1254 node->mj_JoinState = EXEC_MJ_SKIPOUTER_ADVANCE;
1256 /* compareResult > 0 */
1257 node->mj_JoinState = EXEC_MJ_SKIPINNER_ADVANCE;
1261 * Before advancing, we check to see if we must emit an
1262 * outer-join fill tuple for this outer tuple.
1264 case EXEC_MJ_SKIPOUTER_ADVANCE:
1265 MJ_printf("ExecMergeJoin: EXEC_MJ_SKIPOUTER_ADVANCE\n");
1267 if (doFillOuter && !node->mj_MatchedOuter)
1270 * Generate a fake join tuple with nulls for the inner
1271 * tuple, and return it if it passes the non-join quals.
1273 TupleTableSlot *result;
1275 node->mj_MatchedOuter = true; /* do it only once */
1277 result = MJFillOuter(node);
1283 * now we get the next outer tuple, if any
1285 outerTupleSlot = ExecProcNode(outerPlan);
1286 node->mj_OuterTupleSlot = outerTupleSlot;
1287 MJ_DEBUG_PROC_NODE(outerTupleSlot);
1288 node->mj_MatchedOuter = false;
1291 * if the outer tuple is null then we are done with the join,
1292 * unless we have inner tuples we need to null-fill.
1294 if (TupIsNull(outerTupleSlot))
1296 MJ_printf("ExecMergeJoin: end of outer subplan\n");
1297 innerTupleSlot = node->mj_InnerTupleSlot;
1298 if (doFillInner && !TupIsNull(innerTupleSlot))
1301 * Need to emit right-join tuples for remaining inner
1304 node->mj_JoinState = EXEC_MJ_ENDOUTER;
1307 /* Otherwise we're done. */
1311 /* Compute join values and check for unmatchability */
1312 if (MJEvalOuterValues(node))
1314 /* Go test the new tuple against the current inner */
1315 node->mj_JoinState = EXEC_MJ_SKIP_TEST;
1319 /* Can't match, so fetch next outer tuple */
1320 node->mj_JoinState = EXEC_MJ_SKIPOUTER_ADVANCE;
1325 * Before advancing, we check to see if we must emit an
1326 * outer-join fill tuple for this inner tuple.
1328 case EXEC_MJ_SKIPINNER_ADVANCE:
1329 MJ_printf("ExecMergeJoin: EXEC_MJ_SKIPINNER_ADVANCE\n");
1331 if (doFillInner && !node->mj_MatchedInner)
1334 * Generate a fake join tuple with nulls for the outer
1335 * tuple, and return it if it passes the non-join quals.
1337 TupleTableSlot *result;
1339 node->mj_MatchedInner = true; /* do it only once */
1341 result = MJFillInner(node);
1347 * now we get the next inner tuple, if any
1349 innerTupleSlot = ExecProcNode(innerPlan);
1350 node->mj_InnerTupleSlot = innerTupleSlot;
1351 MJ_DEBUG_PROC_NODE(innerTupleSlot);
1352 node->mj_MatchedInner = false;
1355 * if the inner tuple is null then we are done with the join,
1356 * unless we have outer tuples we need to null-fill.
1358 if (TupIsNull(innerTupleSlot))
1360 MJ_printf("ExecMergeJoin: end of inner subplan\n");
1361 outerTupleSlot = node->mj_OuterTupleSlot;
1362 if (doFillOuter && !TupIsNull(outerTupleSlot))
1365 * Need to emit left-join tuples for remaining outer
1368 node->mj_JoinState = EXEC_MJ_ENDINNER;
1371 /* Otherwise we're done. */
1375 /* Compute join values and check for unmatchability */
1376 if (MJEvalInnerValues(node, innerTupleSlot))
1378 /* proceed to compare it to the current outer */
1379 node->mj_JoinState = EXEC_MJ_SKIP_TEST;
1384 * current inner can't possibly match any outer;
1385 * better to advance the inner scan than the outer.
1387 node->mj_JoinState = EXEC_MJ_SKIPINNER_ADVANCE;
1392 * EXEC_MJ_ENDOUTER means we have run out of outer tuples, but
1393 * are doing a right/full join and therefore must null-fill
1394 * any remaing unmatched inner tuples.
1396 case EXEC_MJ_ENDOUTER:
1397 MJ_printf("ExecMergeJoin: EXEC_MJ_ENDOUTER\n");
1399 Assert(doFillInner);
1401 if (!node->mj_MatchedInner)
1404 * Generate a fake join tuple with nulls for the outer
1405 * tuple, and return it if it passes the non-join quals.
1407 TupleTableSlot *result;
1409 node->mj_MatchedInner = true; /* do it only once */
1411 result = MJFillInner(node);
1417 * now we get the next inner tuple, if any
1419 innerTupleSlot = ExecProcNode(innerPlan);
1420 node->mj_InnerTupleSlot = innerTupleSlot;
1421 MJ_DEBUG_PROC_NODE(innerTupleSlot);
1422 node->mj_MatchedInner = false;
1424 if (TupIsNull(innerTupleSlot))
1426 MJ_printf("ExecMergeJoin: end of inner subplan\n");
1430 /* Else remain in ENDOUTER state and process next tuple. */
1434 * EXEC_MJ_ENDINNER means we have run out of inner tuples, but
1435 * are doing a left/full join and therefore must null- fill
1436 * any remaing unmatched outer tuples.
1438 case EXEC_MJ_ENDINNER:
1439 MJ_printf("ExecMergeJoin: EXEC_MJ_ENDINNER\n");
1441 Assert(doFillOuter);
1443 if (!node->mj_MatchedOuter)
1446 * Generate a fake join tuple with nulls for the inner
1447 * tuple, and return it if it passes the non-join quals.
1449 TupleTableSlot *result;
1451 node->mj_MatchedOuter = true; /* do it only once */
1453 result = MJFillOuter(node);
1459 * now we get the next outer tuple, if any
1461 outerTupleSlot = ExecProcNode(outerPlan);
1462 node->mj_OuterTupleSlot = outerTupleSlot;
1463 MJ_DEBUG_PROC_NODE(outerTupleSlot);
1464 node->mj_MatchedOuter = false;
1466 if (TupIsNull(outerTupleSlot))
1468 MJ_printf("ExecMergeJoin: end of outer subplan\n");
1472 /* Else remain in ENDINNER state and process next tuple. */
1476 * broken state value?
1479 elog(ERROR, "unrecognized mergejoin state: %d",
1480 (int) node->mj_JoinState);
1485 /* ----------------------------------------------------------------
1487 * ----------------------------------------------------------------
1490 ExecInitMergeJoin(MergeJoin *node, EState *estate, int eflags)
1492 MergeJoinState *mergestate;
1494 /* check for unsupported flags */
1495 Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
1497 MJ1_printf("ExecInitMergeJoin: %s\n",
1498 "initializing node");
1501 * create state structure
1503 mergestate = makeNode(MergeJoinState);
1504 mergestate->js.ps.plan = (Plan *) node;
1505 mergestate->js.ps.state = estate;
1508 * Miscellaneous initialization
1510 * create expression context for node
1512 ExecAssignExprContext(estate, &mergestate->js.ps);
1515 * we need two additional econtexts in which we can compute the join
1516 * expressions from the left and right input tuples. The node's regular
1517 * econtext won't do because it gets reset too often.
1519 mergestate->mj_OuterEContext = CreateExprContext(estate);
1520 mergestate->mj_InnerEContext = CreateExprContext(estate);
1523 * initialize child expressions
1525 mergestate->js.ps.targetlist = (List *)
1526 ExecInitExpr((Expr *) node->join.plan.targetlist,
1527 (PlanState *) mergestate);
1528 mergestate->js.ps.qual = (List *)
1529 ExecInitExpr((Expr *) node->join.plan.qual,
1530 (PlanState *) mergestate);
1531 mergestate->js.jointype = node->join.jointype;
1532 mergestate->js.joinqual = (List *)
1533 ExecInitExpr((Expr *) node->join.joinqual,
1534 (PlanState *) mergestate);
1535 /* mergeclauses are handled below */
1538 * initialize child nodes
1540 * inner child must support MARK/RESTORE.
1542 outerPlanState(mergestate) = ExecInitNode(outerPlan(node), estate, eflags);
1543 innerPlanState(mergestate) = ExecInitNode(innerPlan(node), estate,
1544 eflags | EXEC_FLAG_MARK);
1546 #define MERGEJOIN_NSLOTS 4
1549 * tuple table initialization
1551 ExecInitResultTupleSlot(estate, &mergestate->js.ps);
1553 mergestate->mj_MarkedTupleSlot = ExecInitExtraTupleSlot(estate);
1554 ExecSetSlotDescriptor(mergestate->mj_MarkedTupleSlot,
1555 ExecGetResultType(innerPlanState(mergestate)));
1557 switch (node->join.jointype)
1561 mergestate->mj_FillOuter = false;
1562 mergestate->mj_FillInner = false;
1565 mergestate->mj_FillOuter = true;
1566 mergestate->mj_FillInner = false;
1567 mergestate->mj_NullInnerTupleSlot =
1568 ExecInitNullTupleSlot(estate,
1569 ExecGetResultType(innerPlanState(mergestate)));
1572 mergestate->mj_FillOuter = false;
1573 mergestate->mj_FillInner = true;
1574 mergestate->mj_NullOuterTupleSlot =
1575 ExecInitNullTupleSlot(estate,
1576 ExecGetResultType(outerPlanState(mergestate)));
1579 * Can't handle right or full join with non-nil extra joinclauses.
1580 * This should have been caught by planner.
1582 if (node->join.joinqual != NIL)
1584 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1585 errmsg("RIGHT JOIN is only supported with merge-joinable join conditions")));
1588 mergestate->mj_FillOuter = true;
1589 mergestate->mj_FillInner = true;
1590 mergestate->mj_NullOuterTupleSlot =
1591 ExecInitNullTupleSlot(estate,
1592 ExecGetResultType(outerPlanState(mergestate)));
1593 mergestate->mj_NullInnerTupleSlot =
1594 ExecInitNullTupleSlot(estate,
1595 ExecGetResultType(innerPlanState(mergestate)));
1598 * Can't handle right or full join with non-nil extra joinclauses.
1600 if (node->join.joinqual != NIL)
1602 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1603 errmsg("FULL JOIN is only supported with merge-joinable join conditions")));
1606 elog(ERROR, "unrecognized join type: %d",
1607 (int) node->join.jointype);
1611 * initialize tuple type and projection info
1613 ExecAssignResultTypeFromTL(&mergestate->js.ps);
1614 ExecAssignProjectionInfo(&mergestate->js.ps);
1617 * preprocess the merge clauses
1619 mergestate->mj_NumClauses = list_length(node->mergeclauses);
1620 mergestate->mj_Clauses = MJExamineQuals(node->mergeclauses,
1621 (PlanState *) mergestate);
1624 * initialize join state
1626 mergestate->mj_JoinState = EXEC_MJ_INITIALIZE_OUTER;
1627 mergestate->js.ps.ps_TupFromTlist = false;
1628 mergestate->mj_MatchedOuter = false;
1629 mergestate->mj_MatchedInner = false;
1630 mergestate->mj_OuterTupleSlot = NULL;
1631 mergestate->mj_InnerTupleSlot = NULL;
1634 * initialization successful
1636 MJ1_printf("ExecInitMergeJoin: %s\n",
1637 "node initialized");
1643 ExecCountSlotsMergeJoin(MergeJoin *node)
1645 return ExecCountSlotsNode(outerPlan((Plan *) node)) +
1646 ExecCountSlotsNode(innerPlan((Plan *) node)) +
1650 /* ----------------------------------------------------------------
1654 * frees storage allocated through C routines.
1655 * ----------------------------------------------------------------
1658 ExecEndMergeJoin(MergeJoinState *node)
1660 MJ1_printf("ExecEndMergeJoin: %s\n",
1661 "ending node processing");
1664 * Free the exprcontext
1666 ExecFreeExprContext(&node->js.ps);
1669 * clean out the tuple table
1671 ExecClearTuple(node->js.ps.ps_ResultTupleSlot);
1672 ExecClearTuple(node->mj_MarkedTupleSlot);
1675 * shut down the subplans
1677 ExecEndNode(innerPlanState(node));
1678 ExecEndNode(outerPlanState(node));
1680 MJ1_printf("ExecEndMergeJoin: %s\n",
1681 "node processing ended");
1685 ExecReScanMergeJoin(MergeJoinState *node, ExprContext *exprCtxt)
1687 ExecClearTuple(node->mj_MarkedTupleSlot);
1689 node->mj_JoinState = EXEC_MJ_INITIALIZE_OUTER;
1690 node->js.ps.ps_TupFromTlist = false;
1691 node->mj_MatchedOuter = false;
1692 node->mj_MatchedInner = false;
1693 node->mj_OuterTupleSlot = NULL;
1694 node->mj_InnerTupleSlot = NULL;
1697 * if chgParam of subnodes is not null then plans will be re-scanned by
1698 * first ExecProcNode.
1700 if (((PlanState *) node)->lefttree->chgParam == NULL)
1701 ExecReScan(((PlanState *) node)->lefttree, exprCtxt);
1702 if (((PlanState *) node)->righttree->chgParam == NULL)
1703 ExecReScan(((PlanState *) node)->righttree, exprCtxt);