Add support for user-defined I/O conversion casts.
[PostgreSQL.git] / src / backend / executor / nodeMergejoin.c
blob35af68771ceecab1b9e00ab3fb155379fe252645
1 /*-------------------------------------------------------------------------
3 * nodeMergejoin.c
4 * routines supporting merge joins
6 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
10 * IDENTIFICATION
11 * $PostgreSQL$
13 *-------------------------------------------------------------------------
16 * INTERFACE ROUTINES
17 * ExecMergeJoin mergejoin outer and inner relations.
18 * ExecInitMergeJoin creates and initializes run time states
19 * ExecEndMergeJoin cleans up the node.
21 * NOTES
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
31 * relations:
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:
63 * Join {
64 * get initial outer and inner tuples INITIALIZE
65 * do forever {
66 * while (outer != inner) { SKIP_TEST
67 * if (outer < inner)
68 * advance outer SKIPOUTER_ADVANCE
69 * else
70 * advance inner SKIPINNER_ADVANCE
71 * }
72 * mark inner position SKIP_TEST
73 * do forever {
74 * while (outer == inner) {
75 * join tuples JOINTUPLES
76 * advance inner position NEXTINNER
77 * }
78 * advance outer position NEXTOUTER
79 * if (outer == mark) TESTOUTER
80 * restore inner position to mark TESTOUTER
81 * else
82 * break // return to top of outer loop
83 * }
84 * }
85 * }
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
93 #include "postgres.h"
95 #include "access/nbtree.h"
96 #include "catalog/pg_amop.h"
97 #include "executor/execdebug.h"
98 #include "executor/execdefs.h"
99 #include "executor/nodeMergejoin.h"
100 #include "miscadmin.h"
101 #include "utils/acl.h"
102 #include "utils/lsyscache.h"
103 #include "utils/memutils.h"
104 #include "utils/syscache.h"
108 * Runtime data for each mergejoin clause
110 typedef struct MergeJoinClauseData
112 /* Executable expression trees */
113 ExprState *lexpr; /* left-hand (outer) input expression */
114 ExprState *rexpr; /* right-hand (inner) input expression */
117 * If we have a current left or right input tuple, the values of the
118 * expressions are loaded into these fields:
120 Datum ldatum; /* current left-hand value */
121 Datum rdatum; /* current right-hand value */
122 bool lisnull; /* and their isnull flags */
123 bool risnull;
126 * The comparison strategy in use, and the lookup info to let us call the
127 * btree comparison support function.
129 bool reverse; /* if true, negate the cmpfn's output */
130 bool nulls_first; /* if true, nulls sort low */
131 FmgrInfo cmpfinfo;
132 } MergeJoinClauseData;
135 #define MarkInnerTuple(innerTupleSlot, mergestate) \
136 ExecCopySlot((mergestate)->mj_MarkedTupleSlot, (innerTupleSlot))
140 * MJExamineQuals
142 * This deconstructs the list of mergejoinable expressions, which is given
143 * to us by the planner in the form of a list of "leftexpr = rightexpr"
144 * expression trees in the order matching the sort columns of the inputs.
145 * We build an array of MergeJoinClause structs containing the information
146 * we will need at runtime. Each struct essentially tells us how to compare
147 * the two expressions from the original clause.
149 * In addition to the expressions themselves, the planner passes the btree
150 * opfamily OID, btree strategy number (BTLessStrategyNumber or
151 * BTGreaterStrategyNumber), and nulls-first flag that identify the intended
152 * sort ordering for each merge key. The mergejoinable operator is an
153 * equality operator in this opfamily, and the two inputs are guaranteed to be
154 * ordered in either increasing or decreasing (respectively) order according
155 * to this opfamily, with nulls at the indicated end of the range. This
156 * allows us to obtain the needed comparison function from the opfamily.
158 static MergeJoinClause
159 MJExamineQuals(List *mergeclauses,
160 Oid *mergefamilies,
161 int *mergestrategies,
162 bool *mergenullsfirst,
163 PlanState *parent)
165 MergeJoinClause clauses;
166 int nClauses = list_length(mergeclauses);
167 int iClause;
168 ListCell *cl;
170 clauses = (MergeJoinClause) palloc0(nClauses * sizeof(MergeJoinClauseData));
172 iClause = 0;
173 foreach(cl, mergeclauses)
175 OpExpr *qual = (OpExpr *) lfirst(cl);
176 MergeJoinClause clause = &clauses[iClause];
177 Oid opfamily = mergefamilies[iClause];
178 StrategyNumber opstrategy = mergestrategies[iClause];
179 bool nulls_first = mergenullsfirst[iClause];
180 int op_strategy;
181 Oid op_lefttype;
182 Oid op_righttype;
183 RegProcedure cmpproc;
184 AclResult aclresult;
186 if (!IsA(qual, OpExpr))
187 elog(ERROR, "mergejoin clause is not an OpExpr");
190 * Prepare the input expressions for execution.
192 clause->lexpr = ExecInitExpr((Expr *) linitial(qual->args), parent);
193 clause->rexpr = ExecInitExpr((Expr *) lsecond(qual->args), parent);
195 /* Extract the operator's declared left/right datatypes */
196 get_op_opfamily_properties(qual->opno, opfamily,
197 &op_strategy,
198 &op_lefttype,
199 &op_righttype);
200 if (op_strategy != BTEqualStrategyNumber) /* should not happen */
201 elog(ERROR, "cannot merge using non-equality operator %u",
202 qual->opno);
204 /* And get the matching support procedure (comparison function) */
205 cmpproc = get_opfamily_proc(opfamily,
206 op_lefttype,
207 op_righttype,
208 BTORDER_PROC);
209 if (!RegProcedureIsValid(cmpproc)) /* should not happen */
210 elog(ERROR, "missing support function %d(%u,%u) in opfamily %u",
211 BTORDER_PROC, op_lefttype, op_righttype, opfamily);
213 /* Check permission to call cmp function */
214 aclresult = pg_proc_aclcheck(cmpproc, GetUserId(), ACL_EXECUTE);
215 if (aclresult != ACLCHECK_OK)
216 aclcheck_error(aclresult, ACL_KIND_PROC,
217 get_func_name(cmpproc));
219 /* Set up the fmgr lookup information */
220 fmgr_info(cmpproc, &(clause->cmpfinfo));
222 /* Fill the additional comparison-strategy flags */
223 if (opstrategy == BTLessStrategyNumber)
224 clause->reverse = false;
225 else if (opstrategy == BTGreaterStrategyNumber)
226 clause->reverse = true;
227 else /* planner screwed up */
228 elog(ERROR, "unsupported mergejoin strategy %d", opstrategy);
230 clause->nulls_first = nulls_first;
232 iClause++;
235 return clauses;
239 * MJEvalOuterValues
241 * Compute the values of the mergejoined expressions for the current
242 * outer tuple. We also detect whether it's impossible for the current
243 * outer tuple to match anything --- this is true if it yields a NULL
244 * input, since we assume mergejoin operators are strict.
246 * We evaluate the values in OuterEContext, which can be reset each
247 * time we move to a new tuple.
249 static bool
250 MJEvalOuterValues(MergeJoinState *mergestate)
252 ExprContext *econtext = mergestate->mj_OuterEContext;
253 bool canmatch = true;
254 int i;
255 MemoryContext oldContext;
257 ResetExprContext(econtext);
259 oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
261 econtext->ecxt_outertuple = mergestate->mj_OuterTupleSlot;
263 for (i = 0; i < mergestate->mj_NumClauses; i++)
265 MergeJoinClause clause = &mergestate->mj_Clauses[i];
267 clause->ldatum = ExecEvalExpr(clause->lexpr, econtext,
268 &clause->lisnull, NULL);
269 if (clause->lisnull)
270 canmatch = false;
273 MemoryContextSwitchTo(oldContext);
275 return canmatch;
279 * MJEvalInnerValues
281 * Same as above, but for the inner tuple. Here, we have to be prepared
282 * to load data from either the true current inner, or the marked inner,
283 * so caller must tell us which slot to load from.
285 static bool
286 MJEvalInnerValues(MergeJoinState *mergestate, TupleTableSlot *innerslot)
288 ExprContext *econtext = mergestate->mj_InnerEContext;
289 bool canmatch = true;
290 int i;
291 MemoryContext oldContext;
293 ResetExprContext(econtext);
295 oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
297 econtext->ecxt_innertuple = innerslot;
299 for (i = 0; i < mergestate->mj_NumClauses; i++)
301 MergeJoinClause clause = &mergestate->mj_Clauses[i];
303 clause->rdatum = ExecEvalExpr(clause->rexpr, econtext,
304 &clause->risnull, NULL);
305 if (clause->risnull)
306 canmatch = false;
309 MemoryContextSwitchTo(oldContext);
311 return canmatch;
315 * MJCompare
317 * Compare the mergejoinable values of the current two input tuples
318 * and return 0 if they are equal (ie, the mergejoin equalities all
319 * succeed), +1 if outer > inner, -1 if outer < inner.
321 * MJEvalOuterValues and MJEvalInnerValues must already have been called
322 * for the current outer and inner tuples, respectively.
324 static int32
325 MJCompare(MergeJoinState *mergestate)
327 int32 result = 0;
328 bool nulleqnull = false;
329 ExprContext *econtext = mergestate->js.ps.ps_ExprContext;
330 int i;
331 MemoryContext oldContext;
332 FunctionCallInfoData fcinfo;
335 * Call the comparison functions in short-lived context, in case they leak
336 * memory.
338 ResetExprContext(econtext);
340 oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
342 for (i = 0; i < mergestate->mj_NumClauses; i++)
344 MergeJoinClause clause = &mergestate->mj_Clauses[i];
345 Datum fresult;
348 * Deal with null inputs.
350 if (clause->lisnull)
352 if (clause->risnull)
354 nulleqnull = true; /* NULL "=" NULL */
355 continue;
357 if (clause->nulls_first)
358 result = -1; /* NULL "<" NOT_NULL */
359 else
360 result = 1; /* NULL ">" NOT_NULL */
361 break;
363 if (clause->risnull)
365 if (clause->nulls_first)
366 result = 1; /* NOT_NULL ">" NULL */
367 else
368 result = -1; /* NOT_NULL "<" NULL */
369 break;
373 * OK to call the comparison function.
375 InitFunctionCallInfoData(fcinfo, &(clause->cmpfinfo), 2,
376 NULL, NULL);
377 fcinfo.arg[0] = clause->ldatum;
378 fcinfo.arg[1] = clause->rdatum;
379 fcinfo.argnull[0] = false;
380 fcinfo.argnull[1] = false;
381 fresult = FunctionCallInvoke(&fcinfo);
382 if (fcinfo.isnull)
384 nulleqnull = true; /* treat like NULL = NULL */
385 continue;
387 result = DatumGetInt32(fresult);
389 if (clause->reverse)
390 result = -result;
392 if (result != 0)
393 break;
397 * If we had any null comparison results or NULL-vs-NULL inputs, we do not
398 * want to report that the tuples are equal. Instead, if result is still
399 * 0, change it to +1. This will result in advancing the inner side of
400 * the join.
402 if (nulleqnull && result == 0)
403 result = 1;
405 MemoryContextSwitchTo(oldContext);
407 return result;
412 * Generate a fake join tuple with nulls for the inner tuple,
413 * and return it if it passes the non-join quals.
415 static TupleTableSlot *
416 MJFillOuter(MergeJoinState *node)
418 ExprContext *econtext = node->js.ps.ps_ExprContext;
419 List *otherqual = node->js.ps.qual;
421 ResetExprContext(econtext);
423 econtext->ecxt_outertuple = node->mj_OuterTupleSlot;
424 econtext->ecxt_innertuple = node->mj_NullInnerTupleSlot;
426 if (ExecQual(otherqual, econtext, false))
429 * qualification succeeded. now form the desired projection tuple and
430 * return the slot containing it.
432 TupleTableSlot *result;
433 ExprDoneCond isDone;
435 MJ_printf("ExecMergeJoin: returning outer fill tuple\n");
437 result = ExecProject(node->js.ps.ps_ProjInfo, &isDone);
439 if (isDone != ExprEndResult)
441 node->js.ps.ps_TupFromTlist =
442 (isDone == ExprMultipleResult);
443 return result;
447 return NULL;
451 * Generate a fake join tuple with nulls for the outer tuple,
452 * and return it if it passes the non-join quals.
454 static TupleTableSlot *
455 MJFillInner(MergeJoinState *node)
457 ExprContext *econtext = node->js.ps.ps_ExprContext;
458 List *otherqual = node->js.ps.qual;
460 ResetExprContext(econtext);
462 econtext->ecxt_outertuple = node->mj_NullOuterTupleSlot;
463 econtext->ecxt_innertuple = node->mj_InnerTupleSlot;
465 if (ExecQual(otherqual, econtext, false))
468 * qualification succeeded. now form the desired projection tuple and
469 * return the slot containing it.
471 TupleTableSlot *result;
472 ExprDoneCond isDone;
474 MJ_printf("ExecMergeJoin: returning inner fill tuple\n");
476 result = ExecProject(node->js.ps.ps_ProjInfo, &isDone);
478 if (isDone != ExprEndResult)
480 node->js.ps.ps_TupFromTlist =
481 (isDone == ExprMultipleResult);
482 return result;
486 return NULL;
490 /* ----------------------------------------------------------------
491 * ExecMergeTupleDump
493 * This function is called through the MJ_dump() macro
494 * when EXEC_MERGEJOINDEBUG is defined
495 * ----------------------------------------------------------------
497 #ifdef EXEC_MERGEJOINDEBUG
499 static void
500 ExecMergeTupleDumpOuter(MergeJoinState *mergestate)
502 TupleTableSlot *outerSlot = mergestate->mj_OuterTupleSlot;
504 printf("==== outer tuple ====\n");
505 if (TupIsNull(outerSlot))
506 printf("(nil)\n");
507 else
508 MJ_debugtup(outerSlot);
511 static void
512 ExecMergeTupleDumpInner(MergeJoinState *mergestate)
514 TupleTableSlot *innerSlot = mergestate->mj_InnerTupleSlot;
516 printf("==== inner tuple ====\n");
517 if (TupIsNull(innerSlot))
518 printf("(nil)\n");
519 else
520 MJ_debugtup(innerSlot);
523 static void
524 ExecMergeTupleDumpMarked(MergeJoinState *mergestate)
526 TupleTableSlot *markedSlot = mergestate->mj_MarkedTupleSlot;
528 printf("==== marked tuple ====\n");
529 if (TupIsNull(markedSlot))
530 printf("(nil)\n");
531 else
532 MJ_debugtup(markedSlot);
535 static void
536 ExecMergeTupleDump(MergeJoinState *mergestate)
538 printf("******** ExecMergeTupleDump ********\n");
540 ExecMergeTupleDumpOuter(mergestate);
541 ExecMergeTupleDumpInner(mergestate);
542 ExecMergeTupleDumpMarked(mergestate);
544 printf("******** \n");
546 #endif
548 /* ----------------------------------------------------------------
549 * ExecMergeJoin
550 * ----------------------------------------------------------------
552 TupleTableSlot *
553 ExecMergeJoin(MergeJoinState *node)
555 EState *estate;
556 List *joinqual;
557 List *otherqual;
558 bool qualResult;
559 int32 compareResult;
560 PlanState *innerPlan;
561 TupleTableSlot *innerTupleSlot;
562 PlanState *outerPlan;
563 TupleTableSlot *outerTupleSlot;
564 ExprContext *econtext;
565 bool doFillOuter;
566 bool doFillInner;
569 * get information from node
571 estate = node->js.ps.state;
572 innerPlan = innerPlanState(node);
573 outerPlan = outerPlanState(node);
574 econtext = node->js.ps.ps_ExprContext;
575 joinqual = node->js.joinqual;
576 otherqual = node->js.ps.qual;
577 doFillOuter = node->mj_FillOuter;
578 doFillInner = node->mj_FillInner;
581 * Check to see if we're still projecting out tuples from a previous join
582 * tuple (because there is a function-returning-set in the projection
583 * expressions). If so, try to project another one.
585 if (node->js.ps.ps_TupFromTlist)
587 TupleTableSlot *result;
588 ExprDoneCond isDone;
590 result = ExecProject(node->js.ps.ps_ProjInfo, &isDone);
591 if (isDone == ExprMultipleResult)
592 return result;
593 /* Done with that source tuple... */
594 node->js.ps.ps_TupFromTlist = false;
598 * Reset per-tuple memory context to free any expression evaluation
599 * storage allocated in the previous tuple cycle. Note this can't happen
600 * until we're done projecting out tuples from a join tuple.
602 ResetExprContext(econtext);
605 * ok, everything is setup.. let's go to work
607 for (;;)
609 MJ_dump(node);
612 * get the current state of the join and do things accordingly.
614 switch (node->mj_JoinState)
617 * EXEC_MJ_INITIALIZE_OUTER means that this is the first time
618 * ExecMergeJoin() has been called and so we have to fetch the
619 * first matchable tuple for both outer and inner subplans. We
620 * do the outer side in INITIALIZE_OUTER state, then advance
621 * to INITIALIZE_INNER state for the inner subplan.
623 case EXEC_MJ_INITIALIZE_OUTER:
624 MJ_printf("ExecMergeJoin: EXEC_MJ_INITIALIZE_OUTER\n");
626 outerTupleSlot = ExecProcNode(outerPlan);
627 node->mj_OuterTupleSlot = outerTupleSlot;
628 if (TupIsNull(outerTupleSlot))
630 MJ_printf("ExecMergeJoin: nothing in outer subplan\n");
631 if (doFillInner)
634 * Need to emit right-join tuples for remaining inner
635 * tuples. We set MatchedInner = true to force the
636 * ENDOUTER state to advance inner.
638 node->mj_JoinState = EXEC_MJ_ENDOUTER;
639 node->mj_MatchedInner = true;
640 break;
642 /* Otherwise we're done. */
643 return NULL;
646 /* Compute join values and check for unmatchability */
647 if (MJEvalOuterValues(node))
649 /* OK to go get the first inner tuple */
650 node->mj_JoinState = EXEC_MJ_INITIALIZE_INNER;
652 else
654 /* Stay in same state to fetch next outer tuple */
655 if (doFillOuter)
658 * Generate a fake join tuple with nulls for the inner
659 * tuple, and return it if it passes the non-join
660 * quals.
662 TupleTableSlot *result;
664 result = MJFillOuter(node);
665 if (result)
666 return result;
669 break;
671 case EXEC_MJ_INITIALIZE_INNER:
672 MJ_printf("ExecMergeJoin: EXEC_MJ_INITIALIZE_INNER\n");
674 innerTupleSlot = ExecProcNode(innerPlan);
675 node->mj_InnerTupleSlot = innerTupleSlot;
676 if (TupIsNull(innerTupleSlot))
678 MJ_printf("ExecMergeJoin: nothing in inner subplan\n");
679 if (doFillOuter)
682 * Need to emit left-join tuples for all outer tuples,
683 * including the one we just fetched. We set
684 * MatchedOuter = false to force the ENDINNER state to
685 * emit first tuple before advancing outer.
687 node->mj_JoinState = EXEC_MJ_ENDINNER;
688 node->mj_MatchedOuter = false;
689 break;
691 /* Otherwise we're done. */
692 return NULL;
695 /* Compute join values and check for unmatchability */
696 if (MJEvalInnerValues(node, innerTupleSlot))
699 * OK, we have the initial tuples. Begin by skipping
700 * non-matching tuples.
702 node->mj_JoinState = EXEC_MJ_SKIP_TEST;
704 else
706 /* Mark before advancing, if wanted */
707 if (node->mj_ExtraMarks)
708 ExecMarkPos(innerPlan);
709 /* Stay in same state to fetch next inner tuple */
710 if (doFillInner)
713 * Generate a fake join tuple with nulls for the outer
714 * tuple, and return it if it passes the non-join
715 * quals.
717 TupleTableSlot *result;
719 result = MJFillInner(node);
720 if (result)
721 return result;
724 break;
727 * EXEC_MJ_JOINTUPLES means we have two tuples which satisfied
728 * the merge clause so we join them and then proceed to get
729 * the next inner tuple (EXEC_MJ_NEXTINNER).
731 case EXEC_MJ_JOINTUPLES:
732 MJ_printf("ExecMergeJoin: EXEC_MJ_JOINTUPLES\n");
735 * Set the next state machine state. The right things will
736 * happen whether we return this join tuple or just fall
737 * through to continue the state machine execution.
739 node->mj_JoinState = EXEC_MJ_NEXTINNER;
742 * Check the extra qual conditions to see if we actually want
743 * to return this join tuple. If not, can proceed with merge.
744 * We must distinguish the additional joinquals (which must
745 * pass to consider the tuples "matched" for outer-join logic)
746 * from the otherquals (which must pass before we actually
747 * return the tuple).
749 * We don't bother with a ResetExprContext here, on the
750 * assumption that we just did one while checking the merge
751 * qual. One per tuple should be sufficient. We do have to
752 * set up the econtext links to the tuples for ExecQual to
753 * use.
755 outerTupleSlot = node->mj_OuterTupleSlot;
756 econtext->ecxt_outertuple = outerTupleSlot;
757 innerTupleSlot = node->mj_InnerTupleSlot;
758 econtext->ecxt_innertuple = innerTupleSlot;
760 qualResult = (joinqual == NIL ||
761 ExecQual(joinqual, econtext, false));
762 MJ_DEBUG_QUAL(joinqual, qualResult);
764 if (qualResult)
766 node->mj_MatchedOuter = true;
767 node->mj_MatchedInner = true;
769 /* In an antijoin, we never return a matched tuple */
770 if (node->js.jointype == JOIN_ANTI)
772 node->mj_JoinState = EXEC_MJ_NEXTOUTER;
773 break;
777 * In a semijoin, we'll consider returning the first match,
778 * but after that we're done with this outer tuple.
780 if (node->js.jointype == JOIN_SEMI)
781 node->mj_JoinState = EXEC_MJ_NEXTOUTER;
783 qualResult = (otherqual == NIL ||
784 ExecQual(otherqual, econtext, false));
785 MJ_DEBUG_QUAL(otherqual, qualResult);
787 if (qualResult)
790 * qualification succeeded. now form the desired
791 * projection tuple and return the slot containing it.
793 TupleTableSlot *result;
794 ExprDoneCond isDone;
796 MJ_printf("ExecMergeJoin: returning tuple\n");
798 result = ExecProject(node->js.ps.ps_ProjInfo,
799 &isDone);
801 if (isDone != ExprEndResult)
803 node->js.ps.ps_TupFromTlist =
804 (isDone == ExprMultipleResult);
805 return result;
809 break;
812 * EXEC_MJ_NEXTINNER means advance the inner scan to the next
813 * tuple. If the tuple is not nil, we then proceed to test it
814 * against the join qualification.
816 * Before advancing, we check to see if we must emit an
817 * outer-join fill tuple for this inner tuple.
819 case EXEC_MJ_NEXTINNER:
820 MJ_printf("ExecMergeJoin: EXEC_MJ_NEXTINNER\n");
822 if (doFillInner && !node->mj_MatchedInner)
825 * Generate a fake join tuple with nulls for the outer
826 * tuple, and return it if it passes the non-join quals.
828 TupleTableSlot *result;
830 node->mj_MatchedInner = true; /* do it only once */
832 result = MJFillInner(node);
833 if (result)
834 return result;
838 * now we get the next inner tuple, if any. If there's none,
839 * advance to next outer tuple (which may be able to join to
840 * previously marked tuples).
842 * NB: must NOT do "extraMarks" here, since we may need to
843 * return to previously marked tuples.
845 innerTupleSlot = ExecProcNode(innerPlan);
846 node->mj_InnerTupleSlot = innerTupleSlot;
847 MJ_DEBUG_PROC_NODE(innerTupleSlot);
848 node->mj_MatchedInner = false;
850 if (TupIsNull(innerTupleSlot))
852 node->mj_JoinState = EXEC_MJ_NEXTOUTER;
853 break;
857 * Load up the new inner tuple's comparison values. If we see
858 * that it contains a NULL and hence can't match any outer
859 * tuple, we can skip the comparison and assume the new tuple
860 * is greater than current outer.
862 if (!MJEvalInnerValues(node, innerTupleSlot))
864 node->mj_JoinState = EXEC_MJ_NEXTOUTER;
865 break;
869 * Test the new inner tuple to see if it matches outer.
871 * If they do match, then we join them and move on to the next
872 * inner tuple (EXEC_MJ_JOINTUPLES).
874 * If they do not match then advance to next outer tuple.
876 compareResult = MJCompare(node);
877 MJ_DEBUG_COMPARE(compareResult);
879 if (compareResult == 0)
880 node->mj_JoinState = EXEC_MJ_JOINTUPLES;
881 else
883 Assert(compareResult < 0);
884 node->mj_JoinState = EXEC_MJ_NEXTOUTER;
886 break;
888 /*-------------------------------------------
889 * EXEC_MJ_NEXTOUTER means
891 * outer inner
892 * outer tuple - 5 5 - marked tuple
893 * 5 5
894 * 6 6 - inner tuple
895 * 7 7
897 * we know we just bumped into the
898 * first inner tuple > current outer tuple (or possibly
899 * the end of the inner stream)
900 * so get a new outer tuple and then
901 * proceed to test it against the marked tuple
902 * (EXEC_MJ_TESTOUTER)
904 * Before advancing, we check to see if we must emit an
905 * outer-join fill tuple for this outer tuple.
906 *------------------------------------------------
908 case EXEC_MJ_NEXTOUTER:
909 MJ_printf("ExecMergeJoin: EXEC_MJ_NEXTOUTER\n");
911 if (doFillOuter && !node->mj_MatchedOuter)
914 * Generate a fake join tuple with nulls for the inner
915 * tuple, and return it if it passes the non-join quals.
917 TupleTableSlot *result;
919 node->mj_MatchedOuter = true; /* do it only once */
921 result = MJFillOuter(node);
922 if (result)
923 return result;
927 * now we get the next outer tuple, if any
929 outerTupleSlot = ExecProcNode(outerPlan);
930 node->mj_OuterTupleSlot = outerTupleSlot;
931 MJ_DEBUG_PROC_NODE(outerTupleSlot);
932 node->mj_MatchedOuter = false;
935 * if the outer tuple is null then we are done with the join,
936 * unless we have inner tuples we need to null-fill.
938 if (TupIsNull(outerTupleSlot))
940 MJ_printf("ExecMergeJoin: end of outer subplan\n");
941 innerTupleSlot = node->mj_InnerTupleSlot;
942 if (doFillInner && !TupIsNull(innerTupleSlot))
945 * Need to emit right-join tuples for remaining inner
946 * tuples.
948 node->mj_JoinState = EXEC_MJ_ENDOUTER;
949 break;
951 /* Otherwise we're done. */
952 return NULL;
955 /* Compute join values and check for unmatchability */
956 if (MJEvalOuterValues(node))
958 /* Go test the new tuple against the marked tuple */
959 node->mj_JoinState = EXEC_MJ_TESTOUTER;
961 else
963 /* Can't match, so fetch next outer tuple */
964 node->mj_JoinState = EXEC_MJ_NEXTOUTER;
966 break;
968 /*--------------------------------------------------------
969 * EXEC_MJ_TESTOUTER If the new outer tuple and the marked
970 * tuple satisfy the merge clause then we know we have
971 * duplicates in the outer scan so we have to restore the
972 * inner scan to the marked tuple and proceed to join the
973 * new outer tuple with the inner tuples.
975 * This is the case when
976 * outer inner
977 * 4 5 - marked tuple
978 * outer tuple - 5 5
979 * new outer tuple - 5 5
980 * 6 8 - inner tuple
981 * 7 12
983 * new outer tuple == marked tuple
985 * If the outer tuple fails the test, then we are done
986 * with the marked tuples, and we have to look for a
987 * match to the current inner tuple. So we will
988 * proceed to skip outer tuples until outer >= inner
989 * (EXEC_MJ_SKIP_TEST).
991 * This is the case when
993 * outer inner
994 * 5 5 - marked tuple
995 * outer tuple - 5 5
996 * new outer tuple - 6 8 - inner tuple
997 * 7 12
999 * new outer tuple > marked tuple
1001 *---------------------------------------------------------
1003 case EXEC_MJ_TESTOUTER:
1004 MJ_printf("ExecMergeJoin: EXEC_MJ_TESTOUTER\n");
1007 * Here we must compare the outer tuple with the marked inner
1008 * tuple. (We can ignore the result of MJEvalInnerValues,
1009 * since the marked inner tuple is certainly matchable.)
1011 innerTupleSlot = node->mj_MarkedTupleSlot;
1012 (void) MJEvalInnerValues(node, innerTupleSlot);
1014 compareResult = MJCompare(node);
1015 MJ_DEBUG_COMPARE(compareResult);
1017 if (compareResult == 0)
1020 * the merge clause matched so now we restore the inner
1021 * scan position to the first mark, and go join that tuple
1022 * (and any following ones) to the new outer.
1024 * NOTE: we do not need to worry about the MatchedInner
1025 * state for the rescanned inner tuples. We know all of
1026 * them will match this new outer tuple and therefore
1027 * won't be emitted as fill tuples. This works *only*
1028 * because we require the extra joinquals to be nil when
1029 * doing a right or full join --- otherwise some of the
1030 * rescanned tuples might fail the extra joinquals.
1032 ExecRestrPos(innerPlan);
1035 * ExecRestrPos probably should give us back a new Slot,
1036 * but since it doesn't, use the marked slot. (The
1037 * previously returned mj_InnerTupleSlot cannot be assumed
1038 * to hold the required tuple.)
1040 node->mj_InnerTupleSlot = innerTupleSlot;
1041 /* we need not do MJEvalInnerValues again */
1043 node->mj_JoinState = EXEC_MJ_JOINTUPLES;
1045 else
1047 /* ----------------
1048 * if the new outer tuple didn't match the marked inner
1049 * tuple then we have a case like:
1051 * outer inner
1052 * 4 4 - marked tuple
1053 * new outer - 5 4
1054 * 6 5 - inner tuple
1057 * which means that all subsequent outer tuples will be
1058 * larger than our marked inner tuples. So we need not
1059 * revisit any of the marked tuples but can proceed to
1060 * look for a match to the current inner. If there's
1061 * no more inners, we are done.
1062 * ----------------
1064 Assert(compareResult > 0);
1065 innerTupleSlot = node->mj_InnerTupleSlot;
1066 if (TupIsNull(innerTupleSlot))
1068 if (doFillOuter)
1071 * Need to emit left-join tuples for remaining
1072 * outer tuples.
1074 node->mj_JoinState = EXEC_MJ_ENDINNER;
1075 break;
1077 /* Otherwise we're done. */
1078 return NULL;
1081 /* reload comparison data for current inner */
1082 if (MJEvalInnerValues(node, innerTupleSlot))
1084 /* proceed to compare it to the current outer */
1085 node->mj_JoinState = EXEC_MJ_SKIP_TEST;
1087 else
1090 * current inner can't possibly match any outer;
1091 * better to advance the inner scan than the outer.
1093 node->mj_JoinState = EXEC_MJ_SKIPINNER_ADVANCE;
1096 break;
1098 /*----------------------------------------------------------
1099 * EXEC_MJ_SKIP means compare tuples and if they do not
1100 * match, skip whichever is lesser.
1102 * For example:
1104 * outer inner
1105 * 5 5
1106 * 5 5
1107 * outer tuple - 6 8 - inner tuple
1108 * 7 12
1109 * 8 14
1111 * we have to advance the outer scan
1112 * until we find the outer 8.
1114 * On the other hand:
1116 * outer inner
1117 * 5 5
1118 * 5 5
1119 * outer tuple - 12 8 - inner tuple
1120 * 14 10
1121 * 17 12
1123 * we have to advance the inner scan
1124 * until we find the inner 12.
1125 *----------------------------------------------------------
1127 case EXEC_MJ_SKIP_TEST:
1128 MJ_printf("ExecMergeJoin: EXEC_MJ_SKIP_TEST\n");
1131 * before we advance, make sure the current tuples do not
1132 * satisfy the mergeclauses. If they do, then we update the
1133 * marked tuple position and go join them.
1135 compareResult = MJCompare(node);
1136 MJ_DEBUG_COMPARE(compareResult);
1138 if (compareResult == 0)
1140 ExecMarkPos(innerPlan);
1142 MarkInnerTuple(node->mj_InnerTupleSlot, node);
1144 node->mj_JoinState = EXEC_MJ_JOINTUPLES;
1146 else if (compareResult < 0)
1147 node->mj_JoinState = EXEC_MJ_SKIPOUTER_ADVANCE;
1148 else
1149 /* compareResult > 0 */
1150 node->mj_JoinState = EXEC_MJ_SKIPINNER_ADVANCE;
1151 break;
1154 * SKIPOUTER_ADVANCE: advance over an outer tuple that is
1155 * known not to join to any inner tuple.
1157 * Before advancing, we check to see if we must emit an
1158 * outer-join fill tuple for this outer tuple.
1160 case EXEC_MJ_SKIPOUTER_ADVANCE:
1161 MJ_printf("ExecMergeJoin: EXEC_MJ_SKIPOUTER_ADVANCE\n");
1163 if (doFillOuter && !node->mj_MatchedOuter)
1166 * Generate a fake join tuple with nulls for the inner
1167 * tuple, and return it if it passes the non-join quals.
1169 TupleTableSlot *result;
1171 node->mj_MatchedOuter = true; /* do it only once */
1173 result = MJFillOuter(node);
1174 if (result)
1175 return result;
1179 * now we get the next outer tuple, if any
1181 outerTupleSlot = ExecProcNode(outerPlan);
1182 node->mj_OuterTupleSlot = outerTupleSlot;
1183 MJ_DEBUG_PROC_NODE(outerTupleSlot);
1184 node->mj_MatchedOuter = false;
1187 * if the outer tuple is null then we are done with the join,
1188 * unless we have inner tuples we need to null-fill.
1190 if (TupIsNull(outerTupleSlot))
1192 MJ_printf("ExecMergeJoin: end of outer subplan\n");
1193 innerTupleSlot = node->mj_InnerTupleSlot;
1194 if (doFillInner && !TupIsNull(innerTupleSlot))
1197 * Need to emit right-join tuples for remaining inner
1198 * tuples.
1200 node->mj_JoinState = EXEC_MJ_ENDOUTER;
1201 break;
1203 /* Otherwise we're done. */
1204 return NULL;
1207 /* Compute join values and check for unmatchability */
1208 if (MJEvalOuterValues(node))
1210 /* Go test the new tuple against the current inner */
1211 node->mj_JoinState = EXEC_MJ_SKIP_TEST;
1213 else
1215 /* Can't match, so fetch next outer tuple */
1216 node->mj_JoinState = EXEC_MJ_SKIPOUTER_ADVANCE;
1218 break;
1221 * SKIPINNER_ADVANCE: advance over an inner tuple that is
1222 * known not to join to any outer tuple.
1224 * Before advancing, we check to see if we must emit an
1225 * outer-join fill tuple for this inner tuple.
1227 case EXEC_MJ_SKIPINNER_ADVANCE:
1228 MJ_printf("ExecMergeJoin: EXEC_MJ_SKIPINNER_ADVANCE\n");
1230 if (doFillInner && !node->mj_MatchedInner)
1233 * Generate a fake join tuple with nulls for the outer
1234 * tuple, and return it if it passes the non-join quals.
1236 TupleTableSlot *result;
1238 node->mj_MatchedInner = true; /* do it only once */
1240 result = MJFillInner(node);
1241 if (result)
1242 return result;
1245 /* Mark before advancing, if wanted */
1246 if (node->mj_ExtraMarks)
1247 ExecMarkPos(innerPlan);
1250 * now we get the next inner tuple, if any
1252 innerTupleSlot = ExecProcNode(innerPlan);
1253 node->mj_InnerTupleSlot = innerTupleSlot;
1254 MJ_DEBUG_PROC_NODE(innerTupleSlot);
1255 node->mj_MatchedInner = false;
1258 * if the inner tuple is null then we are done with the join,
1259 * unless we have outer tuples we need to null-fill.
1261 if (TupIsNull(innerTupleSlot))
1263 MJ_printf("ExecMergeJoin: end of inner subplan\n");
1264 outerTupleSlot = node->mj_OuterTupleSlot;
1265 if (doFillOuter && !TupIsNull(outerTupleSlot))
1268 * Need to emit left-join tuples for remaining outer
1269 * tuples.
1271 node->mj_JoinState = EXEC_MJ_ENDINNER;
1272 break;
1274 /* Otherwise we're done. */
1275 return NULL;
1278 /* Compute join values and check for unmatchability */
1279 if (MJEvalInnerValues(node, innerTupleSlot))
1281 /* proceed to compare it to the current outer */
1282 node->mj_JoinState = EXEC_MJ_SKIP_TEST;
1284 else
1287 * current inner can't possibly match any outer; better to
1288 * advance the inner scan than the outer.
1290 node->mj_JoinState = EXEC_MJ_SKIPINNER_ADVANCE;
1292 break;
1295 * EXEC_MJ_ENDOUTER means we have run out of outer tuples, but
1296 * are doing a right/full join and therefore must null-fill
1297 * any remaing unmatched inner tuples.
1299 case EXEC_MJ_ENDOUTER:
1300 MJ_printf("ExecMergeJoin: EXEC_MJ_ENDOUTER\n");
1302 Assert(doFillInner);
1304 if (!node->mj_MatchedInner)
1307 * Generate a fake join tuple with nulls for the outer
1308 * tuple, and return it if it passes the non-join quals.
1310 TupleTableSlot *result;
1312 node->mj_MatchedInner = true; /* do it only once */
1314 result = MJFillInner(node);
1315 if (result)
1316 return result;
1319 /* Mark before advancing, if wanted */
1320 if (node->mj_ExtraMarks)
1321 ExecMarkPos(innerPlan);
1324 * now we get the next inner tuple, if any
1326 innerTupleSlot = ExecProcNode(innerPlan);
1327 node->mj_InnerTupleSlot = innerTupleSlot;
1328 MJ_DEBUG_PROC_NODE(innerTupleSlot);
1329 node->mj_MatchedInner = false;
1331 if (TupIsNull(innerTupleSlot))
1333 MJ_printf("ExecMergeJoin: end of inner subplan\n");
1334 return NULL;
1337 /* Else remain in ENDOUTER state and process next tuple. */
1338 break;
1341 * EXEC_MJ_ENDINNER means we have run out of inner tuples, but
1342 * are doing a left/full join and therefore must null- fill
1343 * any remaing unmatched outer tuples.
1345 case EXEC_MJ_ENDINNER:
1346 MJ_printf("ExecMergeJoin: EXEC_MJ_ENDINNER\n");
1348 Assert(doFillOuter);
1350 if (!node->mj_MatchedOuter)
1353 * Generate a fake join tuple with nulls for the inner
1354 * tuple, and return it if it passes the non-join quals.
1356 TupleTableSlot *result;
1358 node->mj_MatchedOuter = true; /* do it only once */
1360 result = MJFillOuter(node);
1361 if (result)
1362 return result;
1366 * now we get the next outer tuple, if any
1368 outerTupleSlot = ExecProcNode(outerPlan);
1369 node->mj_OuterTupleSlot = outerTupleSlot;
1370 MJ_DEBUG_PROC_NODE(outerTupleSlot);
1371 node->mj_MatchedOuter = false;
1373 if (TupIsNull(outerTupleSlot))
1375 MJ_printf("ExecMergeJoin: end of outer subplan\n");
1376 return NULL;
1379 /* Else remain in ENDINNER state and process next tuple. */
1380 break;
1383 * broken state value?
1385 default:
1386 elog(ERROR, "unrecognized mergejoin state: %d",
1387 (int) node->mj_JoinState);
1392 /* ----------------------------------------------------------------
1393 * ExecInitMergeJoin
1394 * ----------------------------------------------------------------
1396 MergeJoinState *
1397 ExecInitMergeJoin(MergeJoin *node, EState *estate, int eflags)
1399 MergeJoinState *mergestate;
1401 /* check for unsupported flags */
1402 Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
1404 MJ1_printf("ExecInitMergeJoin: %s\n",
1405 "initializing node");
1408 * create state structure
1410 mergestate = makeNode(MergeJoinState);
1411 mergestate->js.ps.plan = (Plan *) node;
1412 mergestate->js.ps.state = estate;
1415 * Miscellaneous initialization
1417 * create expression context for node
1419 ExecAssignExprContext(estate, &mergestate->js.ps);
1422 * we need two additional econtexts in which we can compute the join
1423 * expressions from the left and right input tuples. The node's regular
1424 * econtext won't do because it gets reset too often.
1426 mergestate->mj_OuterEContext = CreateExprContext(estate);
1427 mergestate->mj_InnerEContext = CreateExprContext(estate);
1430 * initialize child expressions
1432 mergestate->js.ps.targetlist = (List *)
1433 ExecInitExpr((Expr *) node->join.plan.targetlist,
1434 (PlanState *) mergestate);
1435 mergestate->js.ps.qual = (List *)
1436 ExecInitExpr((Expr *) node->join.plan.qual,
1437 (PlanState *) mergestate);
1438 mergestate->js.jointype = node->join.jointype;
1439 mergestate->js.joinqual = (List *)
1440 ExecInitExpr((Expr *) node->join.joinqual,
1441 (PlanState *) mergestate);
1442 /* mergeclauses are handled below */
1445 * initialize child nodes
1447 * inner child must support MARK/RESTORE.
1449 outerPlanState(mergestate) = ExecInitNode(outerPlan(node), estate, eflags);
1450 innerPlanState(mergestate) = ExecInitNode(innerPlan(node), estate,
1451 eflags | EXEC_FLAG_MARK);
1454 * For certain types of inner child nodes, it is advantageous to issue
1455 * MARK every time we advance past an inner tuple we will never return to.
1456 * For other types, MARK on a tuple we cannot return to is a waste of
1457 * cycles. Detect which case applies and set mj_ExtraMarks if we want to
1458 * issue "unnecessary" MARK calls.
1460 * Currently, only Material wants the extra MARKs, and it will be helpful
1461 * only if eflags doesn't specify REWIND.
1463 if (IsA(innerPlan(node), Material) &&
1464 (eflags & EXEC_FLAG_REWIND) == 0)
1465 mergestate->mj_ExtraMarks = true;
1466 else
1467 mergestate->mj_ExtraMarks = false;
1469 #define MERGEJOIN_NSLOTS 4
1472 * tuple table initialization
1474 ExecInitResultTupleSlot(estate, &mergestate->js.ps);
1476 mergestate->mj_MarkedTupleSlot = ExecInitExtraTupleSlot(estate);
1477 ExecSetSlotDescriptor(mergestate->mj_MarkedTupleSlot,
1478 ExecGetResultType(innerPlanState(mergestate)));
1480 switch (node->join.jointype)
1482 case JOIN_INNER:
1483 case JOIN_SEMI:
1484 mergestate->mj_FillOuter = false;
1485 mergestate->mj_FillInner = false;
1486 break;
1487 case JOIN_LEFT:
1488 case JOIN_ANTI:
1489 mergestate->mj_FillOuter = true;
1490 mergestate->mj_FillInner = false;
1491 mergestate->mj_NullInnerTupleSlot =
1492 ExecInitNullTupleSlot(estate,
1493 ExecGetResultType(innerPlanState(mergestate)));
1494 break;
1495 case JOIN_RIGHT:
1496 mergestate->mj_FillOuter = false;
1497 mergestate->mj_FillInner = true;
1498 mergestate->mj_NullOuterTupleSlot =
1499 ExecInitNullTupleSlot(estate,
1500 ExecGetResultType(outerPlanState(mergestate)));
1503 * Can't handle right or full join with non-nil extra joinclauses.
1504 * This should have been caught by planner.
1506 if (node->join.joinqual != NIL)
1507 ereport(ERROR,
1508 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1509 errmsg("RIGHT JOIN is only supported with merge-joinable join conditions")));
1510 break;
1511 case JOIN_FULL:
1512 mergestate->mj_FillOuter = true;
1513 mergestate->mj_FillInner = true;
1514 mergestate->mj_NullOuterTupleSlot =
1515 ExecInitNullTupleSlot(estate,
1516 ExecGetResultType(outerPlanState(mergestate)));
1517 mergestate->mj_NullInnerTupleSlot =
1518 ExecInitNullTupleSlot(estate,
1519 ExecGetResultType(innerPlanState(mergestate)));
1522 * Can't handle right or full join with non-nil extra joinclauses.
1524 if (node->join.joinqual != NIL)
1525 ereport(ERROR,
1526 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1527 errmsg("FULL JOIN is only supported with merge-joinable join conditions")));
1528 break;
1529 default:
1530 elog(ERROR, "unrecognized join type: %d",
1531 (int) node->join.jointype);
1535 * initialize tuple type and projection info
1537 ExecAssignResultTypeFromTL(&mergestate->js.ps);
1538 ExecAssignProjectionInfo(&mergestate->js.ps, NULL);
1541 * preprocess the merge clauses
1543 mergestate->mj_NumClauses = list_length(node->mergeclauses);
1544 mergestate->mj_Clauses = MJExamineQuals(node->mergeclauses,
1545 node->mergeFamilies,
1546 node->mergeStrategies,
1547 node->mergeNullsFirst,
1548 (PlanState *) mergestate);
1551 * initialize join state
1553 mergestate->mj_JoinState = EXEC_MJ_INITIALIZE_OUTER;
1554 mergestate->js.ps.ps_TupFromTlist = false;
1555 mergestate->mj_MatchedOuter = false;
1556 mergestate->mj_MatchedInner = false;
1557 mergestate->mj_OuterTupleSlot = NULL;
1558 mergestate->mj_InnerTupleSlot = NULL;
1561 * initialization successful
1563 MJ1_printf("ExecInitMergeJoin: %s\n",
1564 "node initialized");
1566 return mergestate;
1570 ExecCountSlotsMergeJoin(MergeJoin *node)
1572 return ExecCountSlotsNode(outerPlan((Plan *) node)) +
1573 ExecCountSlotsNode(innerPlan((Plan *) node)) +
1574 MERGEJOIN_NSLOTS;
1577 /* ----------------------------------------------------------------
1578 * ExecEndMergeJoin
1580 * old comments
1581 * frees storage allocated through C routines.
1582 * ----------------------------------------------------------------
1584 void
1585 ExecEndMergeJoin(MergeJoinState *node)
1587 MJ1_printf("ExecEndMergeJoin: %s\n",
1588 "ending node processing");
1591 * Free the exprcontext
1593 ExecFreeExprContext(&node->js.ps);
1596 * clean out the tuple table
1598 ExecClearTuple(node->js.ps.ps_ResultTupleSlot);
1599 ExecClearTuple(node->mj_MarkedTupleSlot);
1602 * shut down the subplans
1604 ExecEndNode(innerPlanState(node));
1605 ExecEndNode(outerPlanState(node));
1607 MJ1_printf("ExecEndMergeJoin: %s\n",
1608 "node processing ended");
1611 void
1612 ExecReScanMergeJoin(MergeJoinState *node, ExprContext *exprCtxt)
1614 ExecClearTuple(node->mj_MarkedTupleSlot);
1616 node->mj_JoinState = EXEC_MJ_INITIALIZE_OUTER;
1617 node->js.ps.ps_TupFromTlist = false;
1618 node->mj_MatchedOuter = false;
1619 node->mj_MatchedInner = false;
1620 node->mj_OuterTupleSlot = NULL;
1621 node->mj_InnerTupleSlot = NULL;
1624 * if chgParam of subnodes is not null then plans will be re-scanned by
1625 * first ExecProcNode.
1627 if (((PlanState *) node)->lefttree->chgParam == NULL)
1628 ExecReScan(((PlanState *) node)->lefttree, exprCtxt);
1629 if (((PlanState *) node)->righttree->chgParam == NULL)
1630 ExecReScan(((PlanState *) node)->righttree, exprCtxt);