1 /*-------------------------------------------------------------------------
4 * routines supporting merge joins
6 * Portions Copyright (c) 1996-2005, 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.72 2005/05/13 21:20:16 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 */
130 * If we have a current left or right input tuple, the values of the
131 * expressions are loaded into these fields:
133 Datum ldatum; /* current left-hand value */
134 Datum rdatum; /* current right-hand value */
135 bool lisnull; /* and their isnull flags */
138 * Remember whether mergejoin operator is strict (usually it will be).
139 * NOTE: if it's not strict, we still assume it cannot return true for
140 * one null and one non-null input.
144 * The comparison strategy in use, and the lookup info to let us call
145 * the needed comparison routines. eqfinfo is the "=" operator itself.
146 * cmpfinfo is either the btree comparator or the "<" operator.
148 MergeFunctionKind cmpstrategy;
151 } MergeJoinClauseData;
154 #define MarkInnerTuple(innerTupleSlot, mergestate) \
155 ExecCopySlot((mergestate)->mj_MarkedTupleSlot, (innerTupleSlot))
161 * This deconstructs the list of mergejoinable expressions, which is given
162 * to us by the planner in the form of a list of "leftexpr = rightexpr"
163 * expression trees in the order matching the sort columns of the inputs.
164 * We build an array of MergeJoinClause structs containing the information
165 * we will need at runtime. Each struct essentially tells us how to compare
166 * the two expressions from the original clause.
168 * The best, most efficient way to compare two expressions is to use a btree
169 * comparison support routine, since that requires only one function call
170 * per comparison. Hence we try to find a btree opclass that matches the
171 * mergejoinable operator. If we cannot find one, we'll have to call both
172 * the "=" and (often) the "<" operator for each comparison.
174 static MergeJoinClause
175 MJExamineQuals(List *qualList, PlanState *parent)
177 MergeJoinClause clauses;
178 int nClauses = list_length(qualList);
182 clauses = (MergeJoinClause) palloc0(nClauses * sizeof(MergeJoinClauseData));
187 OpExpr *qual = (OpExpr *) lfirst(l);
188 MergeJoinClause clause = &clauses[iClause];
197 if (!IsA(qual, OpExpr))
198 elog(ERROR, "mergejoin clause is not an OpExpr");
201 * Prepare the input expressions for execution.
203 clause->lexpr = ExecInitExpr((Expr *) linitial(qual->args), parent);
204 clause->rexpr = ExecInitExpr((Expr *) lsecond(qual->args), parent);
207 * Check permission to call the mergejoinable operator.
208 * For predictability, we check this even if we end up not using it.
210 aclresult = pg_proc_aclcheck(qual->opfuncid, GetUserId(), ACL_EXECUTE);
211 if (aclresult != ACLCHECK_OK)
212 aclcheck_error(aclresult, ACL_KIND_PROC,
213 get_func_name(qual->opfuncid));
215 /* Set up the fmgr lookup information */
216 fmgr_info(qual->opfuncid, &(clause->eqfinfo));
218 /* And remember strictness */
219 clause->mergestrict = clause->eqfinfo.fn_strict;
222 * Lookup the comparison operators that go with the mergejoinable
223 * top-level operator. (This will elog if the operator isn't
224 * mergejoinable, which would be the planner's mistake.)
226 op_mergejoin_crossops(qual->opno,
232 clause->cmpstrategy = MERGEFUNC_LT;
235 * Look for a btree opclass including all three operators.
236 * This is much like SelectSortFunction except we insist on
237 * matching all the operators provided, and it can be a cross-type
240 * XXX for now, insist on forward sort so that NULLs can be counted
243 catlist = SearchSysCacheList(AMOPOPID, 1,
244 ObjectIdGetDatum(qual->opno),
247 for (i = 0; i < catlist->n_members; i++)
249 HeapTuple tuple = &catlist->members[i]->tuple;
250 Form_pg_amop aform = (Form_pg_amop) GETSTRUCT(tuple);
251 Oid opcid = aform->amopclaid;
253 if (aform->amopstrategy != BTEqualStrategyNumber)
255 if (!opclass_is_btree(opcid))
257 if (get_op_opclass_strategy(ltop, opcid) == BTLessStrategyNumber &&
258 get_op_opclass_strategy(gtop, opcid) == BTGreaterStrategyNumber)
260 clause->cmpstrategy = MERGEFUNC_CMP;
261 ltproc = get_opclass_proc(opcid, aform->amopsubtype,
263 Assert(RegProcedureIsValid(ltproc));
264 break; /* done looking */
268 ReleaseSysCacheList(catlist);
270 /* Check permission to call "<" operator or cmp function */
271 aclresult = pg_proc_aclcheck(ltproc, GetUserId(), ACL_EXECUTE);
272 if (aclresult != ACLCHECK_OK)
273 aclcheck_error(aclresult, ACL_KIND_PROC,
274 get_func_name(ltproc));
276 /* Set up the fmgr lookup information */
277 fmgr_info(ltproc, &(clause->cmpfinfo));
288 * Compute the values of the mergejoined expressions for the current
289 * outer tuple. We also detect whether it's impossible for the current
290 * outer tuple to match anything --- this is true if it yields a NULL
291 * input for any strict mergejoin operator.
293 * We evaluate the values in OuterEContext, which can be reset each
294 * time we move to a new tuple.
297 MJEvalOuterValues(MergeJoinState *mergestate)
299 ExprContext *econtext = mergestate->mj_OuterEContext;
300 bool canmatch = true;
302 MemoryContext oldContext;
304 ResetExprContext(econtext);
306 oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
308 econtext->ecxt_outertuple = mergestate->mj_OuterTupleSlot;
310 for (i = 0; i < mergestate->mj_NumClauses; i++)
312 MergeJoinClause clause = &mergestate->mj_Clauses[i];
314 clause->ldatum = ExecEvalExpr(clause->lexpr, econtext,
315 &clause->lisnull, NULL);
316 if (clause->lisnull && clause->mergestrict)
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 bool canmatch = true;
338 MemoryContext oldContext;
340 ResetExprContext(econtext);
342 oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
344 econtext->ecxt_innertuple = innerslot;
346 for (i = 0; i < mergestate->mj_NumClauses; i++)
348 MergeJoinClause clause = &mergestate->mj_Clauses[i];
350 clause->rdatum = ExecEvalExpr(clause->rexpr, econtext,
351 &clause->risnull, NULL);
352 if (clause->risnull && clause->mergestrict)
356 MemoryContextSwitchTo(oldContext);
364 * Compare the mergejoinable values of the current two input tuples
365 * and return 0 if they are equal (ie, the mergejoin equalities all
366 * succeed), +1 if outer > inner, -1 if outer < inner.
368 * MJEvalOuterValues and MJEvalInnerValues must already have been called
369 * for the current outer and inner tuples, respectively.
372 MJCompare(MergeJoinState *mergestate)
375 bool nulleqnull = false;
376 ExprContext *econtext = mergestate->js.ps.ps_ExprContext;
378 MemoryContext oldContext;
379 FunctionCallInfoData fcinfo;
382 * Call the comparison functions in short-lived context, in case they
385 ResetExprContext(econtext);
387 oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
389 for (i = 0; i < mergestate->mj_NumClauses; i++)
391 MergeJoinClause clause = &mergestate->mj_Clauses[i];
395 * Deal with null inputs. We treat NULL as sorting after non-NULL.
397 * If both inputs are NULL, and the comparison function isn't
398 * strict, then we call it and check for a true result (this allows
399 * operators that behave like IS NOT DISTINCT to be mergejoinable).
400 * If the function is strict or returns false, we temporarily
401 * pretend NULL == NULL and contine checking remaining columns.
407 if (!clause->eqfinfo.fn_strict)
409 InitFunctionCallInfoData(fcinfo, &(clause->eqfinfo), 2,
411 fcinfo.arg[0] = clause->ldatum;
412 fcinfo.arg[1] = clause->rdatum;
413 fcinfo.argnull[0] = true;
414 fcinfo.argnull[1] = true;
415 fresult = FunctionCallInvoke(&fcinfo);
416 if (!fcinfo.isnull && DatumGetBool(fresult))
418 /* treat nulls as really equal */
425 /* NULL > non-NULL */
431 /* non-NULL < NULL */
436 if (clause->cmpstrategy == MERGEFUNC_LT)
438 InitFunctionCallInfoData(fcinfo, &(clause->eqfinfo), 2,
440 fcinfo.arg[0] = clause->ldatum;
441 fcinfo.arg[1] = clause->rdatum;
442 fcinfo.argnull[0] = false;
443 fcinfo.argnull[1] = false;
444 fresult = FunctionCallInvoke(&fcinfo);
450 else if (DatumGetBool(fresult))
455 InitFunctionCallInfoData(fcinfo, &(clause->cmpfinfo), 2,
457 fcinfo.arg[0] = clause->ldatum;
458 fcinfo.arg[1] = clause->rdatum;
459 fcinfo.argnull[0] = false;
460 fcinfo.argnull[1] = false;
461 fresult = FunctionCallInvoke(&fcinfo);
467 else if (DatumGetBool(fresult))
480 else /* must be MERGEFUNC_CMP */
482 InitFunctionCallInfoData(fcinfo, &(clause->cmpfinfo), 2,
484 fcinfo.arg[0] = clause->ldatum;
485 fcinfo.arg[1] = clause->rdatum;
486 fcinfo.argnull[0] = false;
487 fcinfo.argnull[1] = false;
488 fresult = FunctionCallInvoke(&fcinfo);
494 else if (DatumGetInt32(fresult) == 0)
499 else if (DatumGetInt32(fresult) < 0)
515 * If we had any null comparison results or NULL-vs-NULL inputs,
516 * we do not want to report that the tuples are equal. Instead,
517 * if result is still 0, change it to +1. This will result in
518 * advancing the inner side of the join.
520 if (nulleqnull && result == 0)
523 MemoryContextSwitchTo(oldContext);
528 /* ----------------------------------------------------------------
531 * This function is called through the MJ_dump() macro
532 * when EXEC_MERGEJOINDEBUG is defined
533 * ----------------------------------------------------------------
535 #ifdef EXEC_MERGEJOINDEBUG
538 ExecMergeTupleDumpOuter(MergeJoinState *mergestate)
540 TupleTableSlot *outerSlot = mergestate->mj_OuterTupleSlot;
542 printf("==== outer tuple ====\n");
543 if (TupIsNull(outerSlot))
546 MJ_debugtup(outerSlot);
550 ExecMergeTupleDumpInner(MergeJoinState *mergestate)
552 TupleTableSlot *innerSlot = mergestate->mj_InnerTupleSlot;
554 printf("==== inner tuple ====\n");
555 if (TupIsNull(innerSlot))
558 MJ_debugtup(innerSlot);
562 ExecMergeTupleDumpMarked(MergeJoinState *mergestate)
564 TupleTableSlot *markedSlot = mergestate->mj_MarkedTupleSlot;
566 printf("==== marked tuple ====\n");
567 if (TupIsNull(markedSlot))
570 MJ_debugtup(markedSlot);
574 ExecMergeTupleDump(MergeJoinState *mergestate)
576 printf("******** ExecMergeTupleDump ********\n");
578 ExecMergeTupleDumpOuter(mergestate);
579 ExecMergeTupleDumpInner(mergestate);
580 ExecMergeTupleDumpMarked(mergestate);
582 printf("******** \n");
586 /* ----------------------------------------------------------------
588 * ----------------------------------------------------------------
591 ExecMergeJoin(MergeJoinState *node)
598 PlanState *innerPlan;
599 TupleTableSlot *innerTupleSlot;
600 PlanState *outerPlan;
601 TupleTableSlot *outerTupleSlot;
602 ExprContext *econtext;
607 * get information from node
609 estate = node->js.ps.state;
610 innerPlan = innerPlanState(node);
611 outerPlan = outerPlanState(node);
612 econtext = node->js.ps.ps_ExprContext;
613 joinqual = node->js.joinqual;
614 otherqual = node->js.ps.qual;
616 switch (node->js.jointype)
636 elog(ERROR, "unrecognized join type: %d",
637 (int) node->js.jointype);
638 doFillOuter = false; /* keep compiler quiet */
644 * Check to see if we're still projecting out tuples from a previous
645 * join tuple (because there is a function-returning-set in the
646 * projection expressions). If so, try to project another one.
648 if (node->js.ps.ps_TupFromTlist)
650 TupleTableSlot *result;
653 result = ExecProject(node->js.ps.ps_ProjInfo, &isDone);
654 if (isDone == ExprMultipleResult)
656 /* Done with that source tuple... */
657 node->js.ps.ps_TupFromTlist = false;
661 * Reset per-tuple memory context to free any expression evaluation
662 * storage allocated in the previous tuple cycle. Note this can't
663 * happen until we're done projecting out tuples from a join tuple.
665 ResetExprContext(econtext);
668 * ok, everything is setup.. let's go to work
675 * get the current state of the join and do things accordingly.
677 switch (node->mj_JoinState)
680 * EXEC_MJ_INITIALIZE_OUTER means that this is the first time
681 * ExecMergeJoin() has been called and so we have to fetch
682 * the first matchable tuple for both outer and inner subplans.
683 * We do the outer side in INITIALIZE_OUTER state, then
684 * advance to INITIALIZE_INNER state for the inner subplan.
686 case EXEC_MJ_INITIALIZE_OUTER:
687 MJ_printf("ExecMergeJoin: EXEC_MJ_INITIALIZE_OUTER\n");
689 outerTupleSlot = ExecProcNode(outerPlan);
690 node->mj_OuterTupleSlot = outerTupleSlot;
691 if (TupIsNull(outerTupleSlot))
693 MJ_printf("ExecMergeJoin: nothing in outer subplan\n");
697 * Need to emit right-join tuples for remaining
698 * inner tuples. We set MatchedInner = true to
699 * force the ENDOUTER state to advance inner.
701 node->mj_JoinState = EXEC_MJ_ENDOUTER;
702 node->mj_MatchedInner = true;
705 /* Otherwise we're done. */
709 /* Compute join values and check for unmatchability */
710 if (!MJEvalOuterValues(node) && !doFillOuter)
712 /* Stay in same state to fetch next outer tuple */
713 node->mj_JoinState = EXEC_MJ_INITIALIZE_OUTER;
717 /* OK to go get the first inner tuple */
718 node->mj_JoinState = EXEC_MJ_INITIALIZE_INNER;
722 case EXEC_MJ_INITIALIZE_INNER:
723 MJ_printf("ExecMergeJoin: EXEC_MJ_INITIALIZE_INNER\n");
725 innerTupleSlot = ExecProcNode(innerPlan);
726 node->mj_InnerTupleSlot = innerTupleSlot;
727 if (TupIsNull(innerTupleSlot))
729 MJ_printf("ExecMergeJoin: nothing in inner subplan\n");
733 * Need to emit left-join tuples for all outer
734 * tuples, including the one we just fetched. We
735 * set MatchedOuter = false to force the ENDINNER
736 * state to emit first tuple before advancing
739 node->mj_JoinState = EXEC_MJ_ENDINNER;
740 node->mj_MatchedOuter = false;
743 /* Otherwise we're done. */
747 /* Compute join values and check for unmatchability */
748 if (!MJEvalInnerValues(node, innerTupleSlot) && !doFillInner)
750 /* Stay in same state to fetch next inner tuple */
751 node->mj_JoinState = EXEC_MJ_INITIALIZE_INNER;
756 * OK, we have the initial tuples. Begin by skipping
757 * non-matching tuples.
759 node->mj_JoinState = EXEC_MJ_SKIP_TEST;
764 * EXEC_MJ_JOINTUPLES means we have two tuples which
765 * satisfied the merge clause so we join them and then
766 * proceed to get the next inner tuple (EXEC_MJ_NEXTINNER).
768 case EXEC_MJ_JOINTUPLES:
769 MJ_printf("ExecMergeJoin: EXEC_MJ_JOINTUPLES\n");
772 * Set the next state machine state. The right things will
773 * happen whether we return this join tuple or just fall
774 * through to continue the state machine execution.
776 node->mj_JoinState = EXEC_MJ_NEXTINNER;
779 * Check the extra qual conditions to see if we actually
780 * want to return this join tuple. If not, can proceed
781 * with merge. We must distinguish the additional
782 * joinquals (which must pass to consider the tuples
783 * "matched" for outer-join logic) from the otherquals
784 * (which must pass before we actually return the tuple).
786 * We don't bother with a ResetExprContext here, on the
787 * assumption that we just did one while checking the
788 * merge qual. One per tuple should be sufficient. We
789 * do have to set up the econtext links to the tuples
790 * for ExecQual to use.
792 outerTupleSlot = node->mj_OuterTupleSlot;
793 econtext->ecxt_outertuple = outerTupleSlot;
794 innerTupleSlot = node->mj_InnerTupleSlot;
795 econtext->ecxt_innertuple = innerTupleSlot;
797 if (node->js.jointype == JOIN_IN &&
798 node->mj_MatchedOuter)
802 qualResult = (joinqual == NIL ||
803 ExecQual(joinqual, econtext, false));
804 MJ_DEBUG_QUAL(joinqual, qualResult);
809 node->mj_MatchedOuter = true;
810 node->mj_MatchedInner = true;
812 qualResult = (otherqual == NIL ||
813 ExecQual(otherqual, econtext, false));
814 MJ_DEBUG_QUAL(otherqual, qualResult);
819 * qualification succeeded. now form the desired
820 * projection tuple and return the slot containing
823 TupleTableSlot *result;
826 MJ_printf("ExecMergeJoin: returning tuple\n");
828 result = ExecProject(node->js.ps.ps_ProjInfo,
831 if (isDone != ExprEndResult)
833 node->js.ps.ps_TupFromTlist =
834 (isDone == ExprMultipleResult);
842 * EXEC_MJ_NEXTINNER means advance the inner scan to the
843 * next tuple. If the tuple is not nil, we then proceed to
844 * test it against the join qualification.
846 * Before advancing, we check to see if we must emit an
847 * outer-join fill tuple for this inner tuple.
849 case EXEC_MJ_NEXTINNER:
850 MJ_printf("ExecMergeJoin: EXEC_MJ_NEXTINNER\n");
852 if (doFillInner && !node->mj_MatchedInner)
855 * Generate a fake join tuple with nulls for the outer
856 * tuple, and return it if it passes the non-join
859 node->mj_MatchedInner = true; /* do it only once */
861 ResetExprContext(econtext);
863 outerTupleSlot = node->mj_NullOuterTupleSlot;
864 econtext->ecxt_outertuple = outerTupleSlot;
865 innerTupleSlot = node->mj_InnerTupleSlot;
866 econtext->ecxt_innertuple = innerTupleSlot;
868 if (ExecQual(otherqual, econtext, false))
871 * qualification succeeded. now form the desired
872 * projection tuple and return the slot containing
875 TupleTableSlot *result;
878 MJ_printf("ExecMergeJoin: returning fill tuple\n");
880 result = ExecProject(node->js.ps.ps_ProjInfo,
883 if (isDone != ExprEndResult)
885 node->js.ps.ps_TupFromTlist =
886 (isDone == ExprMultipleResult);
893 * now we get the next inner tuple, if any. If there's
894 * none, advance to next outer tuple (which may be able
895 * to join to previously marked tuples).
897 * If we find one but it cannot join to anything, stay
898 * in NEXTINNER state to fetch the next one.
900 innerTupleSlot = ExecProcNode(innerPlan);
901 node->mj_InnerTupleSlot = innerTupleSlot;
902 MJ_DEBUG_PROC_NODE(innerTupleSlot);
903 node->mj_MatchedInner = false;
905 if (TupIsNull(innerTupleSlot))
907 node->mj_JoinState = EXEC_MJ_NEXTOUTER;
911 if (!MJEvalInnerValues(node, innerTupleSlot))
912 break; /* stay in NEXTINNER state */
915 * Test the new inner tuple to see if it matches outer.
917 * If they do match, then we join them and move on to the
918 * next inner tuple (EXEC_MJ_JOINTUPLES).
920 * If they do not match then advance to next outer tuple.
922 compareResult = MJCompare(node);
923 MJ_DEBUG_COMPARE(compareResult);
925 if (compareResult == 0)
926 node->mj_JoinState = EXEC_MJ_JOINTUPLES;
929 Assert(compareResult < 0);
930 node->mj_JoinState = EXEC_MJ_NEXTOUTER;
934 /*-------------------------------------------
935 * EXEC_MJ_NEXTOUTER means
938 * outer tuple - 5 5 - marked tuple
943 * we know we just bumped into the
944 * first inner tuple > current outer tuple (or possibly
945 * the end of the inner stream)
946 * so get a new outer tuple and then
947 * proceed to test it against the marked tuple
948 * (EXEC_MJ_TESTOUTER)
950 * Before advancing, we check to see if we must emit an
951 * outer-join fill tuple for this outer tuple.
952 *------------------------------------------------
954 case EXEC_MJ_NEXTOUTER:
955 MJ_printf("ExecMergeJoin: EXEC_MJ_NEXTOUTER\n");
957 if (doFillOuter && !node->mj_MatchedOuter)
960 * Generate a fake join tuple with nulls for the inner
961 * tuple, and return it if it passes the non-join
964 node->mj_MatchedOuter = true; /* do it only once */
966 ResetExprContext(econtext);
968 outerTupleSlot = node->mj_OuterTupleSlot;
969 econtext->ecxt_outertuple = outerTupleSlot;
970 innerTupleSlot = node->mj_NullInnerTupleSlot;
971 econtext->ecxt_innertuple = innerTupleSlot;
973 if (ExecQual(otherqual, econtext, false))
976 * qualification succeeded. now form the desired
977 * projection tuple and return the slot containing
980 TupleTableSlot *result;
983 MJ_printf("ExecMergeJoin: returning fill tuple\n");
985 result = ExecProject(node->js.ps.ps_ProjInfo,
988 if (isDone != ExprEndResult)
990 node->js.ps.ps_TupFromTlist =
991 (isDone == ExprMultipleResult);
998 * now we get the next outer tuple, if any
1000 outerTupleSlot = ExecProcNode(outerPlan);
1001 node->mj_OuterTupleSlot = outerTupleSlot;
1002 MJ_DEBUG_PROC_NODE(outerTupleSlot);
1003 node->mj_MatchedOuter = false;
1006 * if the outer tuple is null then we are done with the
1007 * join, unless we have inner tuples we need to null-fill.
1009 if (TupIsNull(outerTupleSlot))
1011 MJ_printf("ExecMergeJoin: end of outer subplan\n");
1012 innerTupleSlot = node->mj_InnerTupleSlot;
1013 if (doFillInner && !TupIsNull(innerTupleSlot))
1016 * Need to emit right-join tuples for remaining
1019 node->mj_JoinState = EXEC_MJ_ENDOUTER;
1022 /* Otherwise we're done. */
1026 /* Compute join values and check for unmatchability */
1027 if (!MJEvalOuterValues(node))
1029 /* Stay in same state to fetch next outer tuple */
1030 node->mj_JoinState = EXEC_MJ_NEXTOUTER;
1034 /* Go test the tuple */
1035 node->mj_JoinState = EXEC_MJ_TESTOUTER;
1039 /*--------------------------------------------------------
1040 * EXEC_MJ_TESTOUTER If the new outer tuple and the marked
1041 * tuple satisfy the merge clause then we know we have
1042 * duplicates in the outer scan so we have to restore the
1043 * inner scan to the marked tuple and proceed to join the
1044 * new outer tuples with the inner tuples.
1046 * This is the case when
1048 * 4 5 - marked tuple
1050 * new outer tuple - 5 5
1054 * new outer tuple == marked tuple
1056 * If the outer tuple fails the test, then we are done
1057 * with the marked tuples, and we have to look for a
1058 * match to the current inner tuple. So we will
1059 * proceed to skip outer tuples until outer >= inner
1060 * (EXEC_MJ_SKIP_TEST).
1062 * This is the case when
1065 * 5 5 - marked tuple
1067 * new outer tuple - 6 8 - inner tuple
1070 * new outer tuple > marked tuple
1072 *---------------------------------------------------------
1074 case EXEC_MJ_TESTOUTER:
1075 MJ_printf("ExecMergeJoin: EXEC_MJ_TESTOUTER\n");
1078 * here we must compare the outer tuple with the marked inner
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
1091 * inner scan position to the first mark, and go join
1092 * that tuple (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
1096 * of them will match this new outer tuple and
1097 * therefore won't be emitted as fill tuples. This
1098 * works *only* because we require the extra joinquals
1099 * to be nil when doing a right or full join ---
1100 * otherwise some of the rescanned tuples might fail
1101 * the extra joinquals.
1103 ExecRestrPos(innerPlan);
1106 * ExecRestrPos really should give us back a new Slot,
1107 * but since it doesn't, use the marked slot.
1109 node->mj_InnerTupleSlot = innerTupleSlot;
1110 /* we need not do MJEvalInnerValues again */
1112 node->mj_JoinState = EXEC_MJ_JOINTUPLES;
1117 * if the new outer tuple didn't match the marked inner
1118 * tuple then we have a case like:
1121 * 4 4 - marked tuple
1126 * which means that all subsequent outer tuples will be
1127 * larger than our marked inner tuples. So we need not
1128 * revisit any of the marked tuples but can proceed to
1129 * look for a match to the current inner. If there's
1130 * no more inners, we are done.
1133 Assert(compareResult > 0);
1134 innerTupleSlot = node->mj_InnerTupleSlot;
1135 if (TupIsNull(innerTupleSlot))
1140 * Need to emit left-join tuples for remaining
1143 node->mj_JoinState = EXEC_MJ_ENDINNER;
1146 /* Otherwise we're done. */
1150 /* reload comparison data for current inner */
1151 (void) MJEvalInnerValues(node, innerTupleSlot);
1153 /* continue on to skip outer tuples */
1154 node->mj_JoinState = EXEC_MJ_SKIP_TEST;
1158 /*----------------------------------------------------------
1159 * EXEC_MJ_SKIP means compare tuples and if they do not
1160 * match, skip whichever is lesser.
1167 * outer tuple - 6 8 - inner tuple
1171 * we have to advance the outer scan
1172 * until we find the outer 8.
1174 * On the other hand:
1179 * outer tuple - 12 8 - inner tuple
1183 * we have to advance the inner scan
1184 * until we find the inner 12.
1185 *----------------------------------------------------------
1187 case EXEC_MJ_SKIP_TEST:
1188 MJ_printf("ExecMergeJoin: EXEC_MJ_SKIP_TEST\n");
1191 * before we advance, make sure the current tuples do not
1192 * satisfy the mergeclauses. If they do, then we update
1193 * the marked tuple position and go join them.
1195 compareResult = MJCompare(node);
1196 MJ_DEBUG_COMPARE(compareResult);
1198 if (compareResult == 0)
1200 ExecMarkPos(innerPlan);
1202 MarkInnerTuple(node->mj_InnerTupleSlot, node);
1204 node->mj_JoinState = EXEC_MJ_JOINTUPLES;
1206 else if (compareResult < 0)
1207 node->mj_JoinState = EXEC_MJ_SKIPOUTER_ADVANCE;
1208 else /* compareResult > 0 */
1209 node->mj_JoinState = EXEC_MJ_SKIPINNER_ADVANCE;
1213 * Before advancing, we check to see if we must emit an
1214 * outer-join fill tuple for this outer tuple.
1216 case EXEC_MJ_SKIPOUTER_ADVANCE:
1217 MJ_printf("ExecMergeJoin: EXEC_MJ_SKIPOUTER_ADVANCE\n");
1219 if (doFillOuter && !node->mj_MatchedOuter)
1222 * Generate a fake join tuple with nulls for the inner
1223 * tuple, and return it if it passes the non-join
1226 node->mj_MatchedOuter = true; /* do it only once */
1228 ResetExprContext(econtext);
1230 outerTupleSlot = node->mj_OuterTupleSlot;
1231 econtext->ecxt_outertuple = outerTupleSlot;
1232 innerTupleSlot = node->mj_NullInnerTupleSlot;
1233 econtext->ecxt_innertuple = innerTupleSlot;
1235 if (ExecQual(otherqual, econtext, false))
1238 * qualification succeeded. now form the desired
1239 * projection tuple and return the slot containing
1242 TupleTableSlot *result;
1243 ExprDoneCond isDone;
1245 MJ_printf("ExecMergeJoin: returning fill tuple\n");
1247 result = ExecProject(node->js.ps.ps_ProjInfo,
1250 if (isDone != ExprEndResult)
1252 node->js.ps.ps_TupFromTlist =
1253 (isDone == ExprMultipleResult);
1260 * now we get the next outer tuple, if any
1262 outerTupleSlot = ExecProcNode(outerPlan);
1263 node->mj_OuterTupleSlot = outerTupleSlot;
1264 MJ_DEBUG_PROC_NODE(outerTupleSlot);
1265 node->mj_MatchedOuter = false;
1268 * if the outer tuple is null then we are done with the
1269 * join, unless we have inner tuples we need to null-fill.
1271 if (TupIsNull(outerTupleSlot))
1273 MJ_printf("ExecMergeJoin: end of outer subplan\n");
1274 innerTupleSlot = node->mj_InnerTupleSlot;
1275 if (doFillInner && !TupIsNull(innerTupleSlot))
1278 * Need to emit right-join tuples for remaining
1281 node->mj_JoinState = EXEC_MJ_ENDOUTER;
1284 /* Otherwise we're done. */
1288 /* Compute join values and check for unmatchability */
1289 if (!MJEvalOuterValues(node))
1291 /* Stay in same state to fetch next outer tuple */
1292 node->mj_JoinState = EXEC_MJ_SKIPOUTER_ADVANCE;
1296 /* Test the new tuple against the current inner */
1297 node->mj_JoinState = EXEC_MJ_SKIP_TEST;
1301 * Before advancing, we check to see if we must emit an
1302 * outer-join fill tuple for this inner tuple.
1304 case EXEC_MJ_SKIPINNER_ADVANCE:
1305 MJ_printf("ExecMergeJoin: EXEC_MJ_SKIPINNER_ADVANCE\n");
1307 if (doFillInner && !node->mj_MatchedInner)
1310 * Generate a fake join tuple with nulls for the outer
1311 * tuple, and return it if it passes the non-join
1314 node->mj_MatchedInner = true; /* do it only once */
1316 ResetExprContext(econtext);
1318 outerTupleSlot = node->mj_NullOuterTupleSlot;
1319 econtext->ecxt_outertuple = outerTupleSlot;
1320 innerTupleSlot = node->mj_InnerTupleSlot;
1321 econtext->ecxt_innertuple = innerTupleSlot;
1323 if (ExecQual(otherqual, econtext, false))
1326 * qualification succeeded. now form the desired
1327 * projection tuple and return the slot containing
1330 TupleTableSlot *result;
1331 ExprDoneCond isDone;
1333 MJ_printf("ExecMergeJoin: returning fill tuple\n");
1335 result = ExecProject(node->js.ps.ps_ProjInfo,
1338 if (isDone != ExprEndResult)
1340 node->js.ps.ps_TupFromTlist =
1341 (isDone == ExprMultipleResult);
1348 * now we get the next inner tuple, if any
1350 innerTupleSlot = ExecProcNode(innerPlan);
1351 node->mj_InnerTupleSlot = innerTupleSlot;
1352 MJ_DEBUG_PROC_NODE(innerTupleSlot);
1353 node->mj_MatchedInner = false;
1356 * if the inner tuple is null then we are done with the
1357 * join, unless we have outer tuples we need to null-fill.
1359 if (TupIsNull(innerTupleSlot))
1361 MJ_printf("ExecMergeJoin: end of inner subplan\n");
1362 outerTupleSlot = node->mj_OuterTupleSlot;
1363 if (doFillOuter && !TupIsNull(outerTupleSlot))
1366 * Need to emit left-join tuples for remaining
1369 node->mj_JoinState = EXEC_MJ_ENDINNER;
1372 /* Otherwise we're done. */
1376 /* Compute join values and check for unmatchability */
1377 if (!MJEvalInnerValues(node, innerTupleSlot))
1379 /* Stay in same state to fetch next inner tuple */
1380 node->mj_JoinState = EXEC_MJ_SKIPINNER_ADVANCE;
1384 /* Test the new tuple against the current outer */
1385 node->mj_JoinState = EXEC_MJ_SKIP_TEST;
1389 * EXEC_MJ_ENDOUTER means we have run out of outer tuples,
1390 * but are doing a right/full join and therefore must
1391 * null-fill any remaing unmatched inner tuples.
1393 case EXEC_MJ_ENDOUTER:
1394 MJ_printf("ExecMergeJoin: EXEC_MJ_ENDOUTER\n");
1396 Assert(doFillInner);
1398 if (!node->mj_MatchedInner)
1401 * Generate a fake join tuple with nulls for the outer
1402 * tuple, and return it if it passes the non-join
1405 node->mj_MatchedInner = true; /* do it only once */
1407 ResetExprContext(econtext);
1409 outerTupleSlot = node->mj_NullOuterTupleSlot;
1410 econtext->ecxt_outertuple = outerTupleSlot;
1411 innerTupleSlot = node->mj_InnerTupleSlot;
1412 econtext->ecxt_innertuple = innerTupleSlot;
1414 if (ExecQual(otherqual, econtext, false))
1417 * qualification succeeded. now form the desired
1418 * projection tuple and return the slot containing
1421 TupleTableSlot *result;
1422 ExprDoneCond isDone;
1424 MJ_printf("ExecMergeJoin: returning fill tuple\n");
1426 result = ExecProject(node->js.ps.ps_ProjInfo,
1429 if (isDone != ExprEndResult)
1431 node->js.ps.ps_TupFromTlist =
1432 (isDone == ExprMultipleResult);
1439 * now we get the next inner tuple, if any
1441 innerTupleSlot = ExecProcNode(innerPlan);
1442 node->mj_InnerTupleSlot = innerTupleSlot;
1443 MJ_DEBUG_PROC_NODE(innerTupleSlot);
1444 node->mj_MatchedInner = false;
1446 if (TupIsNull(innerTupleSlot))
1448 MJ_printf("ExecMergeJoin: end of inner subplan\n");
1452 /* Else remain in ENDOUTER state and process next tuple. */
1456 * EXEC_MJ_ENDINNER means we have run out of inner tuples,
1457 * but are doing a left/full join and therefore must null-
1458 * fill any remaing unmatched outer tuples.
1460 case EXEC_MJ_ENDINNER:
1461 MJ_printf("ExecMergeJoin: EXEC_MJ_ENDINNER\n");
1463 Assert(doFillOuter);
1465 if (!node->mj_MatchedOuter)
1468 * Generate a fake join tuple with nulls for the inner
1469 * tuple, and return it if it passes the non-join
1472 node->mj_MatchedOuter = true; /* do it only once */
1474 ResetExprContext(econtext);
1476 outerTupleSlot = node->mj_OuterTupleSlot;
1477 econtext->ecxt_outertuple = outerTupleSlot;
1478 innerTupleSlot = node->mj_NullInnerTupleSlot;
1479 econtext->ecxt_innertuple = innerTupleSlot;
1481 if (ExecQual(otherqual, econtext, false))
1484 * qualification succeeded. now form the desired
1485 * projection tuple and return the slot containing
1488 TupleTableSlot *result;
1489 ExprDoneCond isDone;
1491 MJ_printf("ExecMergeJoin: returning fill tuple\n");
1493 result = ExecProject(node->js.ps.ps_ProjInfo,
1496 if (isDone != ExprEndResult)
1498 node->js.ps.ps_TupFromTlist =
1499 (isDone == ExprMultipleResult);
1506 * now we get the next outer tuple, if any
1508 outerTupleSlot = ExecProcNode(outerPlan);
1509 node->mj_OuterTupleSlot = outerTupleSlot;
1510 MJ_DEBUG_PROC_NODE(outerTupleSlot);
1511 node->mj_MatchedOuter = false;
1513 if (TupIsNull(outerTupleSlot))
1515 MJ_printf("ExecMergeJoin: end of outer subplan\n");
1519 /* Else remain in ENDINNER state and process next tuple. */
1523 * broken state value?
1526 elog(ERROR, "unrecognized mergejoin state: %d",
1527 (int) node->mj_JoinState);
1532 /* ----------------------------------------------------------------
1534 * ----------------------------------------------------------------
1537 ExecInitMergeJoin(MergeJoin *node, EState *estate)
1539 MergeJoinState *mergestate;
1541 MJ1_printf("ExecInitMergeJoin: %s\n",
1542 "initializing node");
1545 * create state structure
1547 mergestate = makeNode(MergeJoinState);
1548 mergestate->js.ps.plan = (Plan *) node;
1549 mergestate->js.ps.state = estate;
1552 * Miscellaneous initialization
1554 * create expression context for node
1556 ExecAssignExprContext(estate, &mergestate->js.ps);
1559 * we need two additional econtexts in which we can compute the
1560 * join expressions from the left and right input tuples. The
1561 * node's regular econtext won't do because it gets reset too
1564 mergestate->mj_OuterEContext = CreateExprContext(estate);
1565 mergestate->mj_InnerEContext = CreateExprContext(estate);
1568 * initialize child expressions
1570 mergestate->js.ps.targetlist = (List *)
1571 ExecInitExpr((Expr *) node->join.plan.targetlist,
1572 (PlanState *) mergestate);
1573 mergestate->js.ps.qual = (List *)
1574 ExecInitExpr((Expr *) node->join.plan.qual,
1575 (PlanState *) mergestate);
1576 mergestate->js.jointype = node->join.jointype;
1577 mergestate->js.joinqual = (List *)
1578 ExecInitExpr((Expr *) node->join.joinqual,
1579 (PlanState *) mergestate);
1580 /* mergeclauses are handled below */
1583 * initialize child nodes
1585 outerPlanState(mergestate) = ExecInitNode(outerPlan(node), estate);
1586 innerPlanState(mergestate) = ExecInitNode(innerPlan(node), estate);
1588 #define MERGEJOIN_NSLOTS 4
1591 * tuple table initialization
1593 ExecInitResultTupleSlot(estate, &mergestate->js.ps);
1595 mergestate->mj_MarkedTupleSlot = ExecInitExtraTupleSlot(estate);
1596 ExecSetSlotDescriptor(mergestate->mj_MarkedTupleSlot,
1597 ExecGetResultType(innerPlanState(mergestate)),
1600 switch (node->join.jointype)
1606 mergestate->mj_NullInnerTupleSlot =
1607 ExecInitNullTupleSlot(estate,
1608 ExecGetResultType(innerPlanState(mergestate)));
1611 mergestate->mj_NullOuterTupleSlot =
1612 ExecInitNullTupleSlot(estate,
1613 ExecGetResultType(outerPlanState(mergestate)));
1616 * Can't handle right or full join with non-nil extra
1617 * joinclauses. This should have been caught by planner.
1619 if (node->join.joinqual != NIL)
1621 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1622 errmsg("RIGHT JOIN is only supported with merge-joinable join conditions")));
1625 mergestate->mj_NullOuterTupleSlot =
1626 ExecInitNullTupleSlot(estate,
1627 ExecGetResultType(outerPlanState(mergestate)));
1628 mergestate->mj_NullInnerTupleSlot =
1629 ExecInitNullTupleSlot(estate,
1630 ExecGetResultType(innerPlanState(mergestate)));
1633 * Can't handle right or full join with non-nil extra
1636 if (node->join.joinqual != NIL)
1638 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1639 errmsg("FULL JOIN is only supported with merge-joinable join conditions")));
1642 elog(ERROR, "unrecognized join type: %d",
1643 (int) node->join.jointype);
1647 * initialize tuple type and projection info
1649 ExecAssignResultTypeFromTL(&mergestate->js.ps);
1650 ExecAssignProjectionInfo(&mergestate->js.ps);
1653 * preprocess the merge clauses
1655 mergestate->mj_NumClauses = list_length(node->mergeclauses);
1656 mergestate->mj_Clauses = MJExamineQuals(node->mergeclauses,
1657 (PlanState *) mergestate);
1660 * initialize join state
1662 mergestate->mj_JoinState = EXEC_MJ_INITIALIZE_OUTER;
1663 mergestate->js.ps.ps_TupFromTlist = false;
1664 mergestate->mj_MatchedOuter = false;
1665 mergestate->mj_MatchedInner = false;
1666 mergestate->mj_OuterTupleSlot = NULL;
1667 mergestate->mj_InnerTupleSlot = NULL;
1670 * initialization successful
1672 MJ1_printf("ExecInitMergeJoin: %s\n",
1673 "node initialized");
1679 ExecCountSlotsMergeJoin(MergeJoin *node)
1681 return ExecCountSlotsNode(outerPlan((Plan *) node)) +
1682 ExecCountSlotsNode(innerPlan((Plan *) node)) +
1686 /* ----------------------------------------------------------------
1690 * frees storage allocated through C routines.
1691 * ----------------------------------------------------------------
1694 ExecEndMergeJoin(MergeJoinState *node)
1696 MJ1_printf("ExecEndMergeJoin: %s\n",
1697 "ending node processing");
1700 * Free the exprcontext
1702 ExecFreeExprContext(&node->js.ps);
1705 * clean out the tuple table
1707 ExecClearTuple(node->js.ps.ps_ResultTupleSlot);
1708 ExecClearTuple(node->mj_MarkedTupleSlot);
1711 * shut down the subplans
1713 ExecEndNode(innerPlanState(node));
1714 ExecEndNode(outerPlanState(node));
1716 MJ1_printf("ExecEndMergeJoin: %s\n",
1717 "node processing ended");
1721 ExecReScanMergeJoin(MergeJoinState *node, ExprContext *exprCtxt)
1723 ExecClearTuple(node->mj_MarkedTupleSlot);
1725 node->mj_JoinState = EXEC_MJ_INITIALIZE_OUTER;
1726 node->js.ps.ps_TupFromTlist = false;
1727 node->mj_MatchedOuter = false;
1728 node->mj_MatchedInner = false;
1729 node->mj_OuterTupleSlot = NULL;
1730 node->mj_InnerTupleSlot = NULL;
1733 * if chgParam of subnodes is not null then plans will be re-scanned
1734 * by first ExecProcNode.
1736 if (((PlanState *) node)->lefttree->chgParam == NULL)
1737 ExecReScan(((PlanState *) node)->lefttree, exprCtxt);
1738 if (((PlanState *) node)->righttree->chgParam == NULL)
1739 ExecReScan(((PlanState *) node)->righttree, exprCtxt);