PostgreSQL Source Code git master
Loading...
Searching...
No Matches
nodeMergejoin.c File Reference
#include "postgres.h"
#include "access/nbtree.h"
#include "executor/execdebug.h"
#include "executor/nodeMergejoin.h"
#include "miscadmin.h"
#include "utils/lsyscache.h"
Include dependency graph for nodeMergejoin.c:

Go to the source code of this file.

Data Structures

struct  MergeJoinClauseData
 

Macros

#define EXEC_MJ_INITIALIZE_OUTER   1
 
#define EXEC_MJ_INITIALIZE_INNER   2
 
#define EXEC_MJ_JOINTUPLES   3
 
#define EXEC_MJ_NEXTOUTER   4
 
#define EXEC_MJ_TESTOUTER   5
 
#define EXEC_MJ_NEXTINNER   6
 
#define EXEC_MJ_SKIP_TEST   7
 
#define EXEC_MJ_SKIPOUTER_ADVANCE   8
 
#define EXEC_MJ_SKIPINNER_ADVANCE   9
 
#define EXEC_MJ_ENDOUTER   10
 
#define EXEC_MJ_ENDINNER   11
 
#define MarkInnerTuple(innerTupleSlot, mergestate)    ExecCopySlot((mergestate)->mj_MarkedTupleSlot, (innerTupleSlot))
 

Typedefs

typedef struct MergeJoinClauseData MergeJoinClauseData
 

Enumerations

enum  MJEvalResult { MJEVAL_MATCHABLE , MJEVAL_NONMATCHABLE , MJEVAL_ENDOFJOIN }
 

Functions

static MergeJoinClause MJExamineQuals (List *mergeclauses, Oid *mergefamilies, Oid *mergecollations, bool *mergereversals, bool *mergenullsfirst, PlanState *parent)
 
static MJEvalResult MJEvalOuterValues (MergeJoinState *mergestate)
 
static MJEvalResult MJEvalInnerValues (MergeJoinState *mergestate, TupleTableSlot *innerslot)
 
static int MJCompare (MergeJoinState *mergestate)
 
static TupleTableSlotMJFillOuter (MergeJoinState *node)
 
static TupleTableSlotMJFillInner (MergeJoinState *node)
 
static bool check_constant_qual (List *qual, bool *is_const_false)
 
static TupleTableSlotExecMergeJoin (PlanState *pstate)
 
MergeJoinStateExecInitMergeJoin (MergeJoin *node, EState *estate, int eflags)
 
void ExecEndMergeJoin (MergeJoinState *node)
 
void ExecReScanMergeJoin (MergeJoinState *node)
 

Macro Definition Documentation

◆ EXEC_MJ_ENDINNER

#define EXEC_MJ_ENDINNER   11

Definition at line 115 of file nodeMergejoin.c.

◆ EXEC_MJ_ENDOUTER

#define EXEC_MJ_ENDOUTER   10

Definition at line 114 of file nodeMergejoin.c.

◆ EXEC_MJ_INITIALIZE_INNER

#define EXEC_MJ_INITIALIZE_INNER   2

Definition at line 106 of file nodeMergejoin.c.

◆ EXEC_MJ_INITIALIZE_OUTER

#define EXEC_MJ_INITIALIZE_OUTER   1

Definition at line 105 of file nodeMergejoin.c.

◆ EXEC_MJ_JOINTUPLES

#define EXEC_MJ_JOINTUPLES   3

Definition at line 107 of file nodeMergejoin.c.

◆ EXEC_MJ_NEXTINNER

#define EXEC_MJ_NEXTINNER   6

Definition at line 110 of file nodeMergejoin.c.

◆ EXEC_MJ_NEXTOUTER

#define EXEC_MJ_NEXTOUTER   4

Definition at line 108 of file nodeMergejoin.c.

◆ EXEC_MJ_SKIP_TEST

#define EXEC_MJ_SKIP_TEST   7

Definition at line 111 of file nodeMergejoin.c.

◆ EXEC_MJ_SKIPINNER_ADVANCE

#define EXEC_MJ_SKIPINNER_ADVANCE   9

Definition at line 113 of file nodeMergejoin.c.

◆ EXEC_MJ_SKIPOUTER_ADVANCE

#define EXEC_MJ_SKIPOUTER_ADVANCE   8

Definition at line 112 of file nodeMergejoin.c.

◆ EXEC_MJ_TESTOUTER

#define EXEC_MJ_TESTOUTER   5

Definition at line 109 of file nodeMergejoin.c.

◆ MarkInnerTuple

#define MarkInnerTuple (   innerTupleSlot,
  mergestate 
)     ExecCopySlot((mergestate)->mj_MarkedTupleSlot, (innerTupleSlot))

Definition at line 151 of file nodeMergejoin.c.

180{
181 MergeJoinClause clauses;
182 int nClauses = list_length(mergeclauses);
183 int iClause;
184 ListCell *cl;
185
187
188 iClause = 0;
189 foreach(cl, mergeclauses)
190 {
191 OpExpr *qual = (OpExpr *) lfirst(cl);
192 MergeJoinClause clause = &clauses[iClause];
193 Oid opfamily = mergefamilies[iClause];
194 Oid collation = mergecollations[iClause];
196 bool nulls_first = mergenullsfirst[iClause];
197 int op_strategy;
201
202 if (!IsA(qual, OpExpr))
203 elog(ERROR, "mergejoin clause is not an OpExpr");
204
205 /*
206 * Prepare the input expressions for execution.
207 */
208 clause->lexpr = ExecInitExpr((Expr *) linitial(qual->args), parent);
209 clause->rexpr = ExecInitExpr((Expr *) lsecond(qual->args), parent);
210
211 /* Set up sort support data */
213 clause->ssup.ssup_collation = collation;
214 clause->ssup.ssup_reverse = reversed;
215 clause->ssup.ssup_nulls_first = nulls_first;
216
217 /* Extract the operator's declared left/right datatypes */
218 get_op_opfamily_properties(qual->opno, opfamily, false,
219 &op_strategy,
221 &op_righttype);
222 if (IndexAmTranslateStrategy(op_strategy, get_opfamily_method(opfamily), opfamily, true) != COMPARE_EQ) /* should not happen */
223 elog(ERROR, "cannot merge using non-equality operator %u",
224 qual->opno);
225
226 /*
227 * sortsupport routine must know if abbreviation optimization is
228 * applicable in principle. It is never applicable for merge joins
229 * because there is no convenient opportunity to convert to
230 * alternative representation.
231 */
232 clause->ssup.abbreviate = false;
233
234 /* And get the matching support or comparison function */
235 Assert(clause->ssup.comparator == NULL);
236 sortfunc = get_opfamily_proc(opfamily,
240 if (OidIsValid(sortfunc))
241 {
242 /* The sort support function can provide a comparator */
244 }
245 if (clause->ssup.comparator == NULL)
246 {
247 /* support not available, get comparison func */
248 sortfunc = get_opfamily_proc(opfamily,
252 if (!OidIsValid(sortfunc)) /* should not happen */
253 elog(ERROR, "missing support function %d(%u,%u) in opfamily %u",
255 /* We'll use a shim to call the old-style btree comparator */
257 }
258
259 iClause++;
260 }
261
262 return clauses;
263}
264
265/*
266 * MJEvalOuterValues
267 *
268 * Compute the values of the mergejoined expressions for the current
269 * outer tuple. We also detect whether it's impossible for the current
270 * outer tuple to match anything --- this is true if it yields a NULL
271 * input, since we assume mergejoin operators are strict. If the NULL
272 * is in the first join column, and that column sorts nulls last, then
273 * we can further conclude that no following tuple can match anything
274 * either, since they must all have nulls in the first column. However,
275 * that case is only interesting if we're not in FillOuter mode, else
276 * we have to visit all the tuples anyway.
277 *
278 * For the convenience of callers, we also make this routine responsible
279 * for testing for end-of-input (null outer tuple), and returning
280 * MJEVAL_ENDOFJOIN when that's seen. This allows the same code to be used
281 * for both real end-of-input and the effective end-of-input represented by
282 * a first-column NULL.
283 *
284 * We evaluate the values in OuterEContext, which can be reset each
285 * time we move to a new tuple.
286 */
287static MJEvalResult
289{
290 ExprContext *econtext = mergestate->mj_OuterEContext;
292 int i;
294
295 /* Check for end of outer subplan */
296 if (TupIsNull(mergestate->mj_OuterTupleSlot))
297 return MJEVAL_ENDOFJOIN;
298
299 ResetExprContext(econtext);
300
302
303 econtext->ecxt_outertuple = mergestate->mj_OuterTupleSlot;
304
305 for (i = 0; i < mergestate->mj_NumClauses; i++)
306 {
307 MergeJoinClause clause = &mergestate->mj_Clauses[i];
308
309 clause->ldatum = ExecEvalExpr(clause->lexpr, econtext,
310 &clause->lisnull);
311 if (clause->lisnull)
312 {
313 /* match is impossible; can we end the join early? */
314 if (i == 0 && !clause->ssup.ssup_nulls_first &&
315 !mergestate->mj_FillOuter)
316 result = MJEVAL_ENDOFJOIN;
317 else if (result == MJEVAL_MATCHABLE)
318 result = MJEVAL_NONMATCHABLE;
319 }
320 }
321
323
324 return result;
325}
326
327/*
328 * MJEvalInnerValues
329 *
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.
333 */
334static MJEvalResult
336{
337 ExprContext *econtext = mergestate->mj_InnerEContext;
339 int i;
341
342 /* Check for end of inner subplan */
343 if (TupIsNull(innerslot))
344 return MJEVAL_ENDOFJOIN;
345
346 ResetExprContext(econtext);
347
349
350 econtext->ecxt_innertuple = innerslot;
351
352 for (i = 0; i < mergestate->mj_NumClauses; i++)
353 {
354 MergeJoinClause clause = &mergestate->mj_Clauses[i];
355
356 clause->rdatum = ExecEvalExpr(clause->rexpr, econtext,
357 &clause->risnull);
358 if (clause->risnull)
359 {
360 /* match is impossible; can we end the join early? */
361 if (i == 0 && !clause->ssup.ssup_nulls_first &&
362 !mergestate->mj_FillInner)
363 result = MJEVAL_ENDOFJOIN;
364 else if (result == MJEVAL_MATCHABLE)
365 result = MJEVAL_NONMATCHABLE;
366 }
367 }
368
370
371 return result;
372}
373
374/*
375 * MJCompare
376 *
377 * Compare the mergejoinable values of the current two input tuples
378 * and return 0 if they are equal (ie, the mergejoin equalities all
379 * succeed), >0 if outer > inner, <0 if outer < inner.
380 *
381 * MJEvalOuterValues and MJEvalInnerValues must already have been called
382 * for the current outer and inner tuples, respectively.
383 */
384static int
386{
387 int result = 0;
388 bool nulleqnull = false;
389 ExprContext *econtext = mergestate->js.ps.ps_ExprContext;
390 int i;
392
393 /*
394 * Call the comparison functions in short-lived context, in case they leak
395 * memory.
396 */
397 ResetExprContext(econtext);
398
400
401 for (i = 0; i < mergestate->mj_NumClauses; i++)
402 {
403 MergeJoinClause clause = &mergestate->mj_Clauses[i];
404
405 /*
406 * Special case for NULL-vs-NULL, else use standard comparison.
407 */
408 if (clause->lisnull && clause->risnull)
409 {
410 nulleqnull = true; /* NULL "=" NULL */
411 continue;
412 }
413
414 result = ApplySortComparator(clause->ldatum, clause->lisnull,
415 clause->rdatum, clause->risnull,
416 &clause->ssup);
417
418 if (result != 0)
419 break;
420 }
421
422 /*
423 * If we had any NULL-vs-NULL inputs, we do not want to report that the
424 * tuples are equal. Instead, if result is still 0, change it to +1. This
425 * will result in advancing the inner side of the join.
426 *
427 * Likewise, if there was a constant-false joinqual, do not report
428 * equality. We have to check this as part of the mergequals, else the
429 * rescan logic will do the wrong thing.
430 */
431 if (result == 0 &&
432 (nulleqnull || mergestate->mj_ConstFalseJoin))
433 result = 1;
434
436
437 return result;
438}
439
440
441/*
442 * Generate a fake join tuple with nulls for the inner tuple,
443 * and return it if it passes the non-join quals.
444 */
445static TupleTableSlot *
447{
448 ExprContext *econtext = node->js.ps.ps_ExprContext;
449 ExprState *otherqual = node->js.ps.qual;
450
451 ResetExprContext(econtext);
452
453 econtext->ecxt_outertuple = node->mj_OuterTupleSlot;
454 econtext->ecxt_innertuple = node->mj_NullInnerTupleSlot;
455
456 if (ExecQual(otherqual, econtext))
457 {
458 /*
459 * qualification succeeded. now form the desired projection tuple and
460 * return the slot containing it.
461 */
462 MJ_printf("ExecMergeJoin: returning outer fill tuple\n");
463
464 return ExecProject(node->js.ps.ps_ProjInfo);
465 }
466 else
467 InstrCountFiltered2(node, 1);
468
469 return NULL;
470}
471
472/*
473 * Generate a fake join tuple with nulls for the outer tuple,
474 * and return it if it passes the non-join quals.
475 */
476static TupleTableSlot *
478{
479 ExprContext *econtext = node->js.ps.ps_ExprContext;
480 ExprState *otherqual = node->js.ps.qual;
481
482 ResetExprContext(econtext);
483
484 econtext->ecxt_outertuple = node->mj_NullOuterTupleSlot;
485 econtext->ecxt_innertuple = node->mj_InnerTupleSlot;
486
487 if (ExecQual(otherqual, econtext))
488 {
489 /*
490 * qualification succeeded. now form the desired projection tuple and
491 * return the slot containing it.
492 */
493 MJ_printf("ExecMergeJoin: returning inner fill tuple\n");
494
495 return ExecProject(node->js.ps.ps_ProjInfo);
496 }
497 else
498 InstrCountFiltered2(node, 1);
499
500 return NULL;
501}
502
503
504/*
505 * Check that a qual condition is constant true or constant false.
506 * If it is constant false (or null), set *is_const_false to true.
507 *
508 * Constant true would normally be represented by a NIL list, but we allow an
509 * actual bool Const as well. We do expect that the planner will have thrown
510 * away any non-constant terms that have been ANDed with a constant false.
511 */
512static bool
514{
515 ListCell *lc;
516
517 foreach(lc, qual)
518 {
519 Const *con = (Const *) lfirst(lc);
520
521 if (!con || !IsA(con, Const))
522 return false;
523 if (con->constisnull || !DatumGetBool(con->constvalue))
524 *is_const_false = true;
525 }
526 return true;
527}
528
529
530/* ----------------------------------------------------------------
531 * ExecMergeTupleDump
532 *
533 * This function is called through the MJ_dump() macro
534 * when EXEC_MERGEJOINDEBUG is defined
535 * ----------------------------------------------------------------
536 */
537#ifdef EXEC_MERGEJOINDEBUG
538
539static void
541{
542 TupleTableSlot *outerSlot = mergestate->mj_OuterTupleSlot;
543
544 printf("==== outer tuple ====\n");
545 if (TupIsNull(outerSlot))
546 printf("(nil)\n");
547 else
549}
550
551static void
553{
554 TupleTableSlot *innerSlot = mergestate->mj_InnerTupleSlot;
555
556 printf("==== inner tuple ====\n");
557 if (TupIsNull(innerSlot))
558 printf("(nil)\n");
559 else
561}
562
563static void
565{
566 TupleTableSlot *markedSlot = mergestate->mj_MarkedTupleSlot;
567
568 printf("==== marked tuple ====\n");
570 printf("(nil)\n");
571 else
573}
574
575static void
577{
578 printf("******** ExecMergeTupleDump ********\n");
579
583
584 printf("********\n");
585}
586#endif
587
588/* ----------------------------------------------------------------
589 * ExecMergeJoin
590 * ----------------------------------------------------------------
591 */
592static TupleTableSlot *
594{
595 MergeJoinState *node = castNode(MergeJoinState, pstate);
596 ExprState *joinqual;
598 bool qualResult;
599 int compareResult;
604 ExprContext *econtext;
605 bool doFillOuter;
606 bool doFillInner;
607
609
610 /*
611 * get information from node
612 */
615 econtext = node->js.ps.ps_ExprContext;
616 joinqual = node->js.joinqual;
617 otherqual = node->js.ps.qual;
620
621 /*
622 * Reset per-tuple memory context to free any expression evaluation
623 * storage allocated in the previous tuple cycle.
624 */
625 ResetExprContext(econtext);
626
627 /*
628 * ok, everything is setup.. let's go to work
629 */
630 for (;;)
631 {
632 MJ_dump(node);
633
634 /*
635 * get the current state of the join and do things accordingly.
636 */
637 switch (node->mj_JoinState)
638 {
639 /*
640 * EXEC_MJ_INITIALIZE_OUTER means that this is the first time
641 * ExecMergeJoin() has been called and so we have to fetch the
642 * first matchable tuple for both outer and inner subplans. We
643 * do the outer side in INITIALIZE_OUTER state, then advance
644 * to INITIALIZE_INNER state for the inner subplan.
645 */
647 MJ_printf("ExecMergeJoin: EXEC_MJ_INITIALIZE_OUTER\n");
648
651
652 /* Compute join values and check for unmatchability */
653 switch (MJEvalOuterValues(node))
654 {
655 case MJEVAL_MATCHABLE:
656 /* OK to go get the first inner tuple */
658 break;
660 /* Stay in same state to fetch next outer tuple */
661 if (doFillOuter)
662 {
663 /*
664 * Generate a fake join tuple with nulls for the
665 * inner tuple, and return it if it passes the
666 * non-join quals.
667 */
668 TupleTableSlot *result;
669
670 result = MJFillOuter(node);
671 if (result)
672 return result;
673 }
674 break;
675 case MJEVAL_ENDOFJOIN:
676 /* No more outer tuples */
677 MJ_printf("ExecMergeJoin: nothing in outer subplan\n");
678 if (doFillInner)
679 {
680 /*
681 * Need to emit right-join tuples for remaining
682 * inner tuples. We set MatchedInner = true to
683 * force the ENDOUTER state to advance inner.
684 */
686 node->mj_MatchedInner = true;
687 break;
688 }
689 /* Otherwise we're done. */
690 return NULL;
691 }
692 break;
693
695 MJ_printf("ExecMergeJoin: EXEC_MJ_INITIALIZE_INNER\n");
696
699
700 /* Compute join values and check for unmatchability */
701 switch (MJEvalInnerValues(node, innerTupleSlot))
702 {
703 case MJEVAL_MATCHABLE:
704
705 /*
706 * OK, we have the initial tuples. Begin by skipping
707 * non-matching tuples.
708 */
710 break;
712 /* Mark before advancing, if wanted */
713 if (node->mj_ExtraMarks)
715 /* Stay in same state to fetch next inner tuple */
716 if (doFillInner)
717 {
718 /*
719 * Generate a fake join tuple with nulls for the
720 * outer tuple, and return it if it passes the
721 * non-join quals.
722 */
723 TupleTableSlot *result;
724
725 result = MJFillInner(node);
726 if (result)
727 return result;
728 }
729 break;
730 case MJEVAL_ENDOFJOIN:
731 /* No more inner tuples */
732 MJ_printf("ExecMergeJoin: nothing in inner subplan\n");
733 if (doFillOuter)
734 {
735 /*
736 * Need to emit left-join tuples for all outer
737 * tuples, including the one we just fetched. We
738 * set MatchedOuter = false to force the ENDINNER
739 * state to emit first tuple before advancing
740 * outer.
741 */
743 node->mj_MatchedOuter = false;
744 break;
745 }
746 /* Otherwise we're done. */
747 return NULL;
748 }
749 break;
750
751 /*
752 * EXEC_MJ_JOINTUPLES means we have two tuples which satisfied
753 * the merge clause so we join them and then proceed to get
754 * the next inner tuple (EXEC_MJ_NEXTINNER).
755 */
757 MJ_printf("ExecMergeJoin: EXEC_MJ_JOINTUPLES\n");
758
759 /*
760 * Set the next state machine state. The right things will
761 * happen whether we return this join tuple or just fall
762 * through to continue the state machine execution.
763 */
765
766 /*
767 * Check the extra qual conditions to see if we actually want
768 * to return this join tuple. If not, can proceed with merge.
769 * We must distinguish the additional joinquals (which must
770 * pass to consider the tuples "matched" for outer-join logic)
771 * from the otherquals (which must pass before we actually
772 * return the tuple).
773 *
774 * We don't bother with a ResetExprContext here, on the
775 * assumption that we just did one while checking the merge
776 * qual. One per tuple should be sufficient. We do have to
777 * set up the econtext links to the tuples for ExecQual to
778 * use.
779 */
784
785 qualResult = (joinqual == NULL ||
786 ExecQual(joinqual, econtext));
787 MJ_DEBUG_QUAL(joinqual, qualResult);
788
789 if (qualResult)
790 {
791 node->mj_MatchedOuter = true;
792 node->mj_MatchedInner = true;
793
794 /* In an antijoin, we never return a matched tuple */
795 if (node->js.jointype == JOIN_ANTI)
796 {
798 break;
799 }
800
801 /*
802 * If we only need to consider the first matching inner
803 * tuple, then advance to next outer tuple after we've
804 * processed this one.
805 */
806 if (node->js.single_match)
808
809 /*
810 * In a right-antijoin, we never return a matched tuple.
811 * If it's not an inner_unique join, we need to stay on
812 * the current outer tuple to continue scanning the inner
813 * side for matches.
814 */
815 if (node->js.jointype == JOIN_RIGHT_ANTI)
816 break;
817
818 qualResult = (otherqual == NULL ||
819 ExecQual(otherqual, econtext));
821
822 if (qualResult)
823 {
824 /*
825 * qualification succeeded. now form the desired
826 * projection tuple and return the slot containing it.
827 */
828 MJ_printf("ExecMergeJoin: returning tuple\n");
829
830 return ExecProject(node->js.ps.ps_ProjInfo);
831 }
832 else
833 InstrCountFiltered2(node, 1);
834 }
835 else
836 InstrCountFiltered1(node, 1);
837 break;
838
839 /*
840 * EXEC_MJ_NEXTINNER means advance the inner scan to the next
841 * tuple. If the tuple is not nil, we then proceed to test it
842 * against the join qualification.
843 *
844 * Before advancing, we check to see if we must emit an
845 * outer-join fill tuple for this inner tuple.
846 */
848 MJ_printf("ExecMergeJoin: EXEC_MJ_NEXTINNER\n");
849
850 if (doFillInner && !node->mj_MatchedInner)
851 {
852 /*
853 * Generate a fake join tuple with nulls for the outer
854 * tuple, and return it if it passes the non-join quals.
855 */
856 TupleTableSlot *result;
857
858 node->mj_MatchedInner = true; /* do it only once */
859
860 result = MJFillInner(node);
861 if (result)
862 return result;
863 }
864
865 /*
866 * now we get the next inner tuple, if any. If there's none,
867 * advance to next outer tuple (which may be able to join to
868 * previously marked tuples).
869 *
870 * NB: must NOT do "extraMarks" here, since we may need to
871 * return to previously marked tuples.
872 */
876 node->mj_MatchedInner = false;
877
878 /* Compute join values and check for unmatchability */
879 switch (MJEvalInnerValues(node, innerTupleSlot))
880 {
881 case MJEVAL_MATCHABLE:
882
883 /*
884 * Test the new inner tuple to see if it matches
885 * outer.
886 *
887 * If they do match, then we join them and move on to
888 * the next inner tuple (EXEC_MJ_JOINTUPLES).
889 *
890 * If they do not match then advance to next outer
891 * tuple.
892 */
893 compareResult = MJCompare(node);
895
896 if (compareResult == 0)
898 else if (compareResult < 0)
900 else /* compareResult > 0 should not happen */
901 elog(ERROR, "mergejoin input data is out of order");
902 break;
904
905 /*
906 * It contains a NULL and hence can't match any outer
907 * tuple, so we can skip the comparison and assume the
908 * new tuple is greater than current outer.
909 */
911 break;
912 case MJEVAL_ENDOFJOIN:
913
914 /*
915 * No more inner tuples. However, this might be only
916 * effective and not physical end of inner plan, so
917 * force mj_InnerTupleSlot to null to make sure we
918 * don't fetch more inner tuples. (We need this hack
919 * because we are not transiting to a state where the
920 * inner plan is assumed to be exhausted.)
921 */
922 node->mj_InnerTupleSlot = NULL;
924 break;
925 }
926 break;
927
928 /*-------------------------------------------
929 * EXEC_MJ_NEXTOUTER means
930 *
931 * outer inner
932 * outer tuple - 5 5 - marked tuple
933 * 5 5
934 * 6 6 - inner tuple
935 * 7 7
936 *
937 * we know we just bumped into the
938 * first inner tuple > current outer tuple (or possibly
939 * the end of the inner stream)
940 * so get a new outer tuple and then
941 * proceed to test it against the marked tuple
942 * (EXEC_MJ_TESTOUTER)
943 *
944 * Before advancing, we check to see if we must emit an
945 * outer-join fill tuple for this outer tuple.
946 *------------------------------------------------
947 */
949 MJ_printf("ExecMergeJoin: EXEC_MJ_NEXTOUTER\n");
950
951 if (doFillOuter && !node->mj_MatchedOuter)
952 {
953 /*
954 * Generate a fake join tuple with nulls for the inner
955 * tuple, and return it if it passes the non-join quals.
956 */
957 TupleTableSlot *result;
958
959 node->mj_MatchedOuter = true; /* do it only once */
960
961 result = MJFillOuter(node);
962 if (result)
963 return result;
964 }
965
966 /*
967 * now we get the next outer tuple, if any
968 */
972 node->mj_MatchedOuter = false;
973
974 /* Compute join values and check for unmatchability */
975 switch (MJEvalOuterValues(node))
976 {
977 case MJEVAL_MATCHABLE:
978 /* Go test the new tuple against the marked tuple */
980 break;
982 /* Can't match, so fetch next outer tuple */
984 break;
985 case MJEVAL_ENDOFJOIN:
986 /* No more outer tuples */
987 MJ_printf("ExecMergeJoin: end of outer subplan\n");
990 {
991 /*
992 * Need to emit right-join tuples for remaining
993 * inner tuples.
994 */
996 break;
997 }
998 /* Otherwise we're done. */
999 return NULL;
1000 }
1001 break;
1002
1003 /*--------------------------------------------------------
1004 * EXEC_MJ_TESTOUTER If the new outer tuple and the marked
1005 * tuple satisfy the merge clause then we know we have
1006 * duplicates in the outer scan so we have to restore the
1007 * inner scan to the marked tuple and proceed to join the
1008 * new outer tuple with the inner tuples.
1009 *
1010 * This is the case when
1011 * outer inner
1012 * 4 5 - marked tuple
1013 * outer tuple - 5 5
1014 * new outer tuple - 5 5
1015 * 6 8 - inner tuple
1016 * 7 12
1017 *
1018 * new outer tuple == marked tuple
1019 *
1020 * If the outer tuple fails the test, then we are done
1021 * with the marked tuples, and we have to look for a
1022 * match to the current inner tuple. So we will
1023 * proceed to skip outer tuples until outer >= inner
1024 * (EXEC_MJ_SKIP_TEST).
1025 *
1026 * This is the case when
1027 *
1028 * outer inner
1029 * 5 5 - marked tuple
1030 * outer tuple - 5 5
1031 * new outer tuple - 6 8 - inner tuple
1032 * 7 12
1033 *
1034 * new outer tuple > marked tuple
1035 *
1036 *---------------------------------------------------------
1037 */
1038 case EXEC_MJ_TESTOUTER:
1039 MJ_printf("ExecMergeJoin: EXEC_MJ_TESTOUTER\n");
1040
1041 /*
1042 * Here we must compare the outer tuple with the marked inner
1043 * tuple. (We can ignore the result of MJEvalInnerValues,
1044 * since the marked inner tuple is certainly matchable.)
1045 */
1048
1049 compareResult = MJCompare(node);
1051
1052 if (compareResult == 0)
1053 {
1054 /*
1055 * the merge clause matched so now we restore the inner
1056 * scan position to the first mark, and go join that tuple
1057 * (and any following ones) to the new outer.
1058 *
1059 * If we were able to determine mark and restore are not
1060 * needed, then we don't have to back up; the current
1061 * inner is already the first possible match.
1062 *
1063 * NOTE: we do not need to worry about the MatchedInner
1064 * state for the rescanned inner tuples. We know all of
1065 * them will match this new outer tuple and therefore
1066 * won't be emitted as fill tuples. This works *only*
1067 * because we require the extra joinquals to be constant
1068 * when doing a right, right-anti or full join ---
1069 * otherwise some of the rescanned tuples might fail the
1070 * extra joinquals. This obviously won't happen for a
1071 * constant-true extra joinqual, while the constant-false
1072 * case is handled by forcing the merge clause to never
1073 * match, so we never get here.
1074 */
1075 if (!node->mj_SkipMarkRestore)
1076 {
1078
1079 /*
1080 * ExecRestrPos probably should give us back a new
1081 * Slot, but since it doesn't, use the marked slot.
1082 * (The previously returned mj_InnerTupleSlot cannot
1083 * be assumed to hold the required tuple.)
1084 */
1086 /* we need not do MJEvalInnerValues again */
1087 }
1088
1090 }
1091 else if (compareResult > 0)
1092 {
1093 /* ----------------
1094 * if the new outer tuple didn't match the marked inner
1095 * tuple then we have a case like:
1096 *
1097 * outer inner
1098 * 4 4 - marked tuple
1099 * new outer - 5 4
1100 * 6 5 - inner tuple
1101 * 7
1102 *
1103 * which means that all subsequent outer tuples will be
1104 * larger than our marked inner tuples. So we need not
1105 * revisit any of the marked tuples but can proceed to
1106 * look for a match to the current inner. If there's
1107 * no more inners, no more matches are possible.
1108 * ----------------
1109 */
1111
1112 /* reload comparison data for current inner */
1113 switch (MJEvalInnerValues(node, innerTupleSlot))
1114 {
1115 case MJEVAL_MATCHABLE:
1116 /* proceed to compare it to the current outer */
1118 break;
1120
1121 /*
1122 * current inner can't possibly match any outer;
1123 * better to advance the inner scan than the
1124 * outer.
1125 */
1127 break;
1128 case MJEVAL_ENDOFJOIN:
1129 /* No more inner tuples */
1130 if (doFillOuter)
1131 {
1132 /*
1133 * Need to emit left-join tuples for remaining
1134 * outer tuples.
1135 */
1137 break;
1138 }
1139 /* Otherwise we're done. */
1140 return NULL;
1141 }
1142 }
1143 else /* compareResult < 0 should not happen */
1144 elog(ERROR, "mergejoin input data is out of order");
1145 break;
1146
1147 /*----------------------------------------------------------
1148 * EXEC_MJ_SKIP_TEST means compare tuples and if they do not
1149 * match, skip whichever is lesser.
1150 *
1151 * For example:
1152 *
1153 * outer inner
1154 * 5 5
1155 * 5 5
1156 * outer tuple - 6 8 - inner tuple
1157 * 7 12
1158 * 8 14
1159 *
1160 * we have to advance the outer scan
1161 * until we find the outer 8.
1162 *
1163 * On the other hand:
1164 *
1165 * outer inner
1166 * 5 5
1167 * 5 5
1168 * outer tuple - 12 8 - inner tuple
1169 * 14 10
1170 * 17 12
1171 *
1172 * we have to advance the inner scan
1173 * until we find the inner 12.
1174 *----------------------------------------------------------
1175 */
1176 case EXEC_MJ_SKIP_TEST:
1177 MJ_printf("ExecMergeJoin: EXEC_MJ_SKIP_TEST\n");
1178
1179 /*
1180 * before we advance, make sure the current tuples do not
1181 * satisfy the mergeclauses. If they do, then we update the
1182 * marked tuple position and go join them.
1183 */
1184 compareResult = MJCompare(node);
1186
1187 if (compareResult == 0)
1188 {
1189 if (!node->mj_SkipMarkRestore)
1191
1192 MarkInnerTuple(node->mj_InnerTupleSlot, node);
1193
1195 }
1196 else if (compareResult < 0)
1198 else
1199 /* compareResult > 0 */
1201 break;
1202
1203 /*
1204 * EXEC_MJ_SKIPOUTER_ADVANCE: advance over an outer tuple that
1205 * is known not to join to any inner tuple.
1206 *
1207 * Before advancing, we check to see if we must emit an
1208 * outer-join fill tuple for this outer tuple.
1209 */
1211 MJ_printf("ExecMergeJoin: EXEC_MJ_SKIPOUTER_ADVANCE\n");
1212
1213 if (doFillOuter && !node->mj_MatchedOuter)
1214 {
1215 /*
1216 * Generate a fake join tuple with nulls for the inner
1217 * tuple, and return it if it passes the non-join quals.
1218 */
1219 TupleTableSlot *result;
1220
1221 node->mj_MatchedOuter = true; /* do it only once */
1222
1223 result = MJFillOuter(node);
1224 if (result)
1225 return result;
1226 }
1227
1228 /*
1229 * now we get the next outer tuple, if any
1230 */
1234 node->mj_MatchedOuter = false;
1235
1236 /* Compute join values and check for unmatchability */
1237 switch (MJEvalOuterValues(node))
1238 {
1239 case MJEVAL_MATCHABLE:
1240 /* Go test the new tuple against the current inner */
1242 break;
1244 /* Can't match, so fetch next outer tuple */
1246 break;
1247 case MJEVAL_ENDOFJOIN:
1248 /* No more outer tuples */
1249 MJ_printf("ExecMergeJoin: end of outer subplan\n");
1252 {
1253 /*
1254 * Need to emit right-join tuples for remaining
1255 * inner tuples.
1256 */
1258 break;
1259 }
1260 /* Otherwise we're done. */
1261 return NULL;
1262 }
1263 break;
1264
1265 /*
1266 * EXEC_MJ_SKIPINNER_ADVANCE: advance over an inner tuple that
1267 * is known not to join to any outer tuple.
1268 *
1269 * Before advancing, we check to see if we must emit an
1270 * outer-join fill tuple for this inner tuple.
1271 */
1273 MJ_printf("ExecMergeJoin: EXEC_MJ_SKIPINNER_ADVANCE\n");
1274
1275 if (doFillInner && !node->mj_MatchedInner)
1276 {
1277 /*
1278 * Generate a fake join tuple with nulls for the outer
1279 * tuple, and return it if it passes the non-join quals.
1280 */
1281 TupleTableSlot *result;
1282
1283 node->mj_MatchedInner = true; /* do it only once */
1284
1285 result = MJFillInner(node);
1286 if (result)
1287 return result;
1288 }
1289
1290 /* Mark before advancing, if wanted */
1291 if (node->mj_ExtraMarks)
1293
1294 /*
1295 * now we get the next inner tuple, if any
1296 */
1300 node->mj_MatchedInner = false;
1301
1302 /* Compute join values and check for unmatchability */
1303 switch (MJEvalInnerValues(node, innerTupleSlot))
1304 {
1305 case MJEVAL_MATCHABLE:
1306 /* proceed to compare it to the current outer */
1308 break;
1310
1311 /*
1312 * current inner can't possibly match any outer;
1313 * better to advance the inner scan than the outer.
1314 */
1316 break;
1317 case MJEVAL_ENDOFJOIN:
1318 /* No more inner tuples */
1319 MJ_printf("ExecMergeJoin: end of inner subplan\n");
1322 {
1323 /*
1324 * Need to emit left-join tuples for remaining
1325 * outer tuples.
1326 */
1328 break;
1329 }
1330 /* Otherwise we're done. */
1331 return NULL;
1332 }
1333 break;
1334
1335 /*
1336 * EXEC_MJ_ENDOUTER means we have run out of outer tuples, but
1337 * are doing a right/right-anti/full join and therefore must
1338 * null-fill any remaining unmatched inner tuples.
1339 */
1340 case EXEC_MJ_ENDOUTER:
1341 MJ_printf("ExecMergeJoin: EXEC_MJ_ENDOUTER\n");
1342
1344
1345 if (!node->mj_MatchedInner)
1346 {
1347 /*
1348 * Generate a fake join tuple with nulls for the outer
1349 * tuple, and return it if it passes the non-join quals.
1350 */
1351 TupleTableSlot *result;
1352
1353 node->mj_MatchedInner = true; /* do it only once */
1354
1355 result = MJFillInner(node);
1356 if (result)
1357 return result;
1358 }
1359
1360 /* Mark before advancing, if wanted */
1361 if (node->mj_ExtraMarks)
1363
1364 /*
1365 * now we get the next inner tuple, if any
1366 */
1370 node->mj_MatchedInner = false;
1371
1373 {
1374 MJ_printf("ExecMergeJoin: end of inner subplan\n");
1375 return NULL;
1376 }
1377
1378 /* Else remain in ENDOUTER state and process next tuple. */
1379 break;
1380
1381 /*
1382 * EXEC_MJ_ENDINNER means we have run out of inner tuples, but
1383 * are doing a left/full join and therefore must null- fill
1384 * any remaining unmatched outer tuples.
1385 */
1386 case EXEC_MJ_ENDINNER:
1387 MJ_printf("ExecMergeJoin: EXEC_MJ_ENDINNER\n");
1388
1390
1391 if (!node->mj_MatchedOuter)
1392 {
1393 /*
1394 * Generate a fake join tuple with nulls for the inner
1395 * tuple, and return it if it passes the non-join quals.
1396 */
1397 TupleTableSlot *result;
1398
1399 node->mj_MatchedOuter = true; /* do it only once */
1400
1401 result = MJFillOuter(node);
1402 if (result)
1403 return result;
1404 }
1405
1406 /*
1407 * now we get the next outer tuple, if any
1408 */
1412 node->mj_MatchedOuter = false;
1413
1415 {
1416 MJ_printf("ExecMergeJoin: end of outer subplan\n");
1417 return NULL;
1418 }
1419
1420 /* Else remain in ENDINNER state and process next tuple. */
1421 break;
1422
1423 /*
1424 * broken state value?
1425 */
1426 default:
1427 elog(ERROR, "unrecognized mergejoin state: %d",
1428 (int) node->mj_JoinState);
1429 }
1430 }
1431}
1432
1433/* ----------------------------------------------------------------
1434 * ExecInitMergeJoin
1435 * ----------------------------------------------------------------
1436 */
1438ExecInitMergeJoin(MergeJoin *node, EState *estate, int eflags)
1439{
1442 innerDesc;
1444
1445 /* check for unsupported flags */
1446 Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
1447
1448 MJ1_printf("ExecInitMergeJoin: %s\n",
1449 "initializing node");
1450
1451 /*
1452 * create state structure
1453 */
1455 mergestate->js.ps.plan = (Plan *) node;
1456 mergestate->js.ps.state = estate;
1457 mergestate->js.ps.ExecProcNode = ExecMergeJoin;
1458 mergestate->js.jointype = node->join.jointype;
1459 mergestate->mj_ConstFalseJoin = false;
1460
1461 /*
1462 * Miscellaneous initialization
1463 *
1464 * create expression context for node
1465 */
1466 ExecAssignExprContext(estate, &mergestate->js.ps);
1467
1468 /*
1469 * we need two additional econtexts in which we can compute the join
1470 * expressions from the left and right input tuples. The node's regular
1471 * econtext won't do because it gets reset too often.
1472 */
1473 mergestate->mj_OuterEContext = CreateExprContext(estate);
1474 mergestate->mj_InnerEContext = CreateExprContext(estate);
1475
1476 /*
1477 * initialize child nodes
1478 *
1479 * inner child must support MARK/RESTORE, unless we have detected that we
1480 * don't need that. Note that skip_mark_restore must never be set if
1481 * there are non-mergeclause joinquals, since the logic wouldn't work.
1482 */
1483 Assert(node->join.joinqual == NIL || !node->skip_mark_restore);
1484 mergestate->mj_SkipMarkRestore = node->skip_mark_restore;
1485
1486 outerPlanState(mergestate) = ExecInitNode(outerPlan(node), estate, eflags);
1489 mergestate->mj_SkipMarkRestore ?
1490 eflags :
1491 (eflags | EXEC_FLAG_MARK));
1493
1494 /*
1495 * For certain types of inner child nodes, it is advantageous to issue
1496 * MARK every time we advance past an inner tuple we will never return to.
1497 * For other types, MARK on a tuple we cannot return to is a waste of
1498 * cycles. Detect which case applies and set mj_ExtraMarks if we want to
1499 * issue "unnecessary" MARK calls.
1500 *
1501 * Currently, only Material wants the extra MARKs, and it will be helpful
1502 * only if eflags doesn't specify REWIND.
1503 *
1504 * Note that for IndexScan and IndexOnlyScan, it is *necessary* that we
1505 * not set mj_ExtraMarks; otherwise we might attempt to set a mark before
1506 * the first inner tuple, which they do not support.
1507 */
1508 if (IsA(innerPlan(node), Material) &&
1509 (eflags & EXEC_FLAG_REWIND) == 0 &&
1510 !mergestate->mj_SkipMarkRestore)
1511 mergestate->mj_ExtraMarks = true;
1512 else
1513 mergestate->mj_ExtraMarks = false;
1514
1515 /*
1516 * Initialize result slot, type and projection.
1517 */
1520
1521 /*
1522 * tuple table initialization
1523 */
1525 mergestate->mj_MarkedTupleSlot = ExecInitExtraTupleSlot(estate, innerDesc,
1526 innerOps);
1527
1528 /*
1529 * initialize child expressions
1530 */
1531 mergestate->js.ps.qual =
1532 ExecInitQual(node->join.plan.qual, (PlanState *) mergestate);
1533 mergestate->js.joinqual =
1535 /* mergeclauses are handled below */
1536
1537 /*
1538 * detect whether we need only consider the first matching inner tuple
1539 */
1540 mergestate->js.single_match = (node->join.inner_unique ||
1541 node->join.jointype == JOIN_SEMI);
1542
1543 /* set up null tuples for outer joins, if needed */
1544 switch (node->join.jointype)
1545 {
1546 case JOIN_INNER:
1547 case JOIN_SEMI:
1548 mergestate->mj_FillOuter = false;
1549 mergestate->mj_FillInner = false;
1550 break;
1551 case JOIN_LEFT:
1552 case JOIN_ANTI:
1553 mergestate->mj_FillOuter = true;
1554 mergestate->mj_FillInner = false;
1555 mergestate->mj_NullInnerTupleSlot =
1557 break;
1558 case JOIN_RIGHT:
1559 case JOIN_RIGHT_ANTI:
1560 mergestate->mj_FillOuter = false;
1561 mergestate->mj_FillInner = true;
1562 mergestate->mj_NullOuterTupleSlot =
1564
1565 /*
1566 * Can't handle right, right-anti or full join with non-constant
1567 * extra joinclauses. This should have been caught by planner.
1568 */
1570 &mergestate->mj_ConstFalseJoin))
1571 ereport(ERROR,
1573 errmsg("RIGHT JOIN is only supported with merge-joinable join conditions")));
1574 break;
1575 case JOIN_FULL:
1576 mergestate->mj_FillOuter = true;
1577 mergestate->mj_FillInner = true;
1578 mergestate->mj_NullOuterTupleSlot =
1580 mergestate->mj_NullInnerTupleSlot =
1582
1583 /*
1584 * Can't handle right, right-anti or full join with non-constant
1585 * extra joinclauses. This should have been caught by planner.
1586 */
1588 &mergestate->mj_ConstFalseJoin))
1589 ereport(ERROR,
1591 errmsg("FULL JOIN is only supported with merge-joinable join conditions")));
1592 break;
1593 default:
1594 elog(ERROR, "unrecognized join type: %d",
1595 (int) node->join.jointype);
1596 }
1597
1598 /*
1599 * preprocess the merge clauses
1600 */
1601 mergestate->mj_NumClauses = list_length(node->mergeclauses);
1602 mergestate->mj_Clauses = MJExamineQuals(node->mergeclauses,
1603 node->mergeFamilies,
1604 node->mergeCollations,
1605 node->mergeReversals,
1606 node->mergeNullsFirst,
1607 (PlanState *) mergestate);
1608
1609 /*
1610 * initialize join state
1611 */
1612 mergestate->mj_JoinState = EXEC_MJ_INITIALIZE_OUTER;
1613 mergestate->mj_MatchedOuter = false;
1614 mergestate->mj_MatchedInner = false;
1615 mergestate->mj_OuterTupleSlot = NULL;
1616 mergestate->mj_InnerTupleSlot = NULL;
1617
1618 /*
1619 * initialization successful
1620 */
1621 MJ1_printf("ExecInitMergeJoin: %s\n",
1622 "node initialized");
1623
1624 return mergestate;
1625}
1626
1627/* ----------------------------------------------------------------
1628 * ExecEndMergeJoin
1629 *
1630 * old comments
1631 * frees storage allocated through C routines.
1632 * ----------------------------------------------------------------
1633 */
1634void
1636{
1637 MJ1_printf("ExecEndMergeJoin: %s\n",
1638 "ending node processing");
1639
1640 /*
1641 * shut down the subplans
1642 */
1645
1646 MJ1_printf("ExecEndMergeJoin: %s\n",
1647 "node processing ended");
1648}
1649
1650void
1652{
1655
1657
1659 node->mj_MatchedOuter = false;
1660 node->mj_MatchedInner = false;
1661 node->mj_OuterTupleSlot = NULL;
1662 node->mj_InnerTupleSlot = NULL;
1663
1664 /*
1665 * if chgParam of subnodes is not null then plans will be re-scanned by
1666 * first ExecProcNode.
1667 */
1668 if (outerPlan->chgParam == NULL)
1670 if (innerPlan->chgParam == NULL)
1672}
CompareType IndexAmTranslateStrategy(StrategyNumber strategy, Oid amoid, Oid opfamily, bool missing_ok)
Definition amapi.c:131
#define Assert(condition)
Definition c.h:873
#define OidIsValid(objectId)
Definition c.h:788
@ COMPARE_EQ
Definition cmptype.h:36
int errcode(int sqlerrcode)
Definition elog.c:863
int errmsg(const char *fmt,...)
Definition elog.c:1080
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
#define ereport(elevel,...)
Definition elog.h:150
void ExecMarkPos(PlanState *node)
Definition execAmi.c:327
void ExecReScan(PlanState *node)
Definition execAmi.c:77
void ExecRestrPos(PlanState *node)
Definition execAmi.c:376
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition execExpr.c:143
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition execExpr.c:229
void ExecEndNode(PlanState *node)
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
const TupleTableSlotOps TTSOpsVirtual
Definition execTuples.c:84
TupleTableSlot * ExecInitExtraTupleSlot(EState *estate, TupleDesc tupledesc, const TupleTableSlotOps *tts_ops)
void ExecInitResultTupleSlotTL(PlanState *planstate, const TupleTableSlotOps *tts_ops)
TupleTableSlot * ExecInitNullTupleSlot(EState *estate, TupleDesc tupType, const TupleTableSlotOps *tts_ops)
TupleDesc ExecGetResultType(PlanState *planstate)
Definition execUtils.c:495
ExprContext * CreateExprContext(EState *estate)
Definition execUtils.c:307
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition execUtils.c:485
void ExecAssignProjectionInfo(PlanState *planstate, TupleDesc inputDesc)
Definition execUtils.c:583
const TupleTableSlotOps * ExecGetResultSlotOps(PlanState *planstate, bool *isfixed)
Definition execUtils.c:504
#define MJ_DEBUG_COMPARE(res)
Definition execdebug.h:125
#define MJ_dump(state)
Definition execdebug.h:124
#define MJ_printf(s)
Definition execdebug.h:120
#define MJ_DEBUG_QUAL(clause, res)
Definition execdebug.h:126
#define MJ_DEBUG_PROC_NODE(slot)
Definition execdebug.h:127
#define MJ_debugtup(slot)
Definition execdebug.h:123
#define MJ1_printf(s, p)
Definition execdebug.h:121
#define InstrCountFiltered1(node, delta)
Definition execnodes.h:1271
#define outerPlanState(node)
Definition execnodes.h:1263
#define InstrCountFiltered2(node, delta)
Definition execnodes.h:1276
#define innerPlanState(node)
Definition execnodes.h:1262
struct MergeJoinClauseData * MergeJoinClause
Definition execnodes.h:2182
#define EXEC_FLAG_BACKWARD
Definition executor.h:69
#define EXEC_FLAG_REWIND
Definition executor.h:68
static TupleTableSlot * ExecProject(ProjectionInfo *projInfo)
Definition executor.h:483
#define ResetExprContext(econtext)
Definition executor.h:650
static bool ExecQual(ExprState *state, ExprContext *econtext)
Definition executor.h:519
static TupleTableSlot * ExecProcNode(PlanState *node)
Definition executor.h:314
static Datum ExecEvalExpr(ExprState *state, ExprContext *econtext, bool *isNull)
Definition executor.h:393
#define EXEC_FLAG_MARK
Definition executor.h:70
#define OidFunctionCall1(functionId, arg1)
Definition fmgr.h:722
int i
Definition isn.c:77
void get_op_opfamily_properties(Oid opno, Oid opfamily, bool ordering_op, int *strategy, Oid *lefttype, Oid *righttype)
Definition lsyscache.c:138
Oid get_opfamily_proc(Oid opfamily, Oid lefttype, Oid righttype, int16 procnum)
Definition lsyscache.c:872
Oid get_opfamily_method(Oid opfid)
Definition lsyscache.c:1386
void * palloc0(Size size)
Definition mcxt.c:1417
MemoryContext CurrentMemoryContext
Definition mcxt.c:160
#define CHECK_FOR_INTERRUPTS()
Definition miscadmin.h:123
#define BTORDER_PROC
Definition nbtree.h:717
#define BTSORTSUPPORT_PROC
Definition nbtree.h:718
static MergeJoinClause MJExamineQuals(List *mergeclauses, Oid *mergefamilies, Oid *mergecollations, bool *mergereversals, bool *mergenullsfirst, PlanState *parent)
static int MJCompare(MergeJoinState *mergestate)
#define EXEC_MJ_SKIP_TEST
#define EXEC_MJ_JOINTUPLES
static TupleTableSlot * ExecMergeJoin(PlanState *pstate)
#define EXEC_MJ_SKIPOUTER_ADVANCE
#define MarkInnerTuple(innerTupleSlot, mergestate)
#define EXEC_MJ_TESTOUTER
static TupleTableSlot * MJFillOuter(MergeJoinState *node)
void ExecReScanMergeJoin(MergeJoinState *node)
static MJEvalResult MJEvalOuterValues(MergeJoinState *mergestate)
#define EXEC_MJ_ENDINNER
#define EXEC_MJ_INITIALIZE_OUTER
#define EXEC_MJ_SKIPINNER_ADVANCE
#define EXEC_MJ_ENDOUTER
MergeJoinState * ExecInitMergeJoin(MergeJoin *node, EState *estate, int eflags)
#define EXEC_MJ_NEXTOUTER
void ExecEndMergeJoin(MergeJoinState *node)
#define EXEC_MJ_INITIALIZE_INNER
static MJEvalResult MJEvalInnerValues(MergeJoinState *mergestate, TupleTableSlot *innerslot)
#define EXEC_MJ_NEXTINNER
MJEvalResult
@ MJEVAL_NONMATCHABLE
@ MJEVAL_MATCHABLE
@ MJEVAL_ENDOFJOIN
static TupleTableSlot * MJFillInner(MergeJoinState *node)
static bool check_constant_qual(List *qual, bool *is_const_false)
#define IsA(nodeptr, _type_)
Definition nodes.h:164
#define makeNode(_type_)
Definition nodes.h:161
#define castNode(_type_, nodeptr)
Definition nodes.h:182
@ JOIN_SEMI
Definition nodes.h:317
@ JOIN_FULL
Definition nodes.h:305
@ JOIN_INNER
Definition nodes.h:303
@ JOIN_RIGHT
Definition nodes.h:306
@ JOIN_LEFT
Definition nodes.h:304
@ JOIN_RIGHT_ANTI
Definition nodes.h:320
@ JOIN_ANTI
Definition nodes.h:318
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:124
#define lfirst(lc)
Definition pg_list.h:172
static int list_length(const List *l)
Definition pg_list.h:152
#define NIL
Definition pg_list.h:68
#define linitial(l)
Definition pg_list.h:178
#define lsecond(l)
Definition pg_list.h:183
#define innerPlan(node)
Definition plannodes.h:260
#define outerPlan(node)
Definition plannodes.h:261
#define printf(...)
Definition port.h:266
static bool DatumGetBool(Datum X)
Definition postgres.h:100
static Datum PointerGetDatum(const void *X)
Definition postgres.h:352
unsigned int Oid
static int fb(int x)
void PrepareSortSupportComparisonShim(Oid cmpFunc, SortSupport ssup)
Definition sortsupport.c:68
static int ApplySortComparator(Datum datum1, bool isNull1, Datum datum2, bool isNull2, SortSupport ssup)
MemoryContext ecxt_per_tuple_memory
Definition execnodes.h:283
TupleTableSlot * ecxt_innertuple
Definition execnodes.h:277
TupleTableSlot * ecxt_outertuple
Definition execnodes.h:279
JoinType jointype
Definition execnodes.h:2137
PlanState ps
Definition execnodes.h:2136
ExprState * joinqual
Definition execnodes.h:2140
bool single_match
Definition execnodes.h:2138
List * joinqual
Definition plannodes.h:971
JoinType jointype
Definition plannodes.h:968
bool inner_unique
Definition plannodes.h:969
Definition pg_list.h:54
SortSupportData ssup
bool mj_SkipMarkRestore
Definition execnodes.h:2190
TupleTableSlot * mj_MarkedTupleSlot
Definition execnodes.h:2199
TupleTableSlot * mj_NullInnerTupleSlot
Definition execnodes.h:2201
TupleTableSlot * mj_NullOuterTupleSlot
Definition execnodes.h:2200
TupleTableSlot * mj_InnerTupleSlot
Definition execnodes.h:2198
JoinState js
Definition execnodes.h:2186
TupleTableSlot * mj_OuterTupleSlot
Definition execnodes.h:2197
List * mergeclauses
Definition plannodes.h:1022
bool skip_mark_restore
Definition plannodes.h:1019
Oid opno
Definition primnodes.h:850
List * args
Definition primnodes.h:868
ExprState * qual
Definition execnodes.h:1188
ExprContext * ps_ExprContext
Definition execnodes.h:1206
ProjectionInfo * ps_ProjInfo
Definition execnodes.h:1207
int(* comparator)(Datum x, Datum y, SortSupport ssup)
MemoryContext ssup_cxt
Definition sortsupport.h:66
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition tuptable.h:457
#define TupIsNull(slot)
Definition tuptable.h:309

Typedef Documentation

◆ MergeJoinClauseData

Enumeration Type Documentation

◆ MJEvalResult

Enumerator
MJEVAL_MATCHABLE 
MJEVAL_NONMATCHABLE 
MJEVAL_ENDOFJOIN 

Definition at line 143 of file nodeMergejoin.c.

144{
145 MJEVAL_MATCHABLE, /* normal, potentially matchable tuple */
146 MJEVAL_NONMATCHABLE, /* tuple cannot join because it has a null */
147 MJEVAL_ENDOFJOIN, /* end of input (physical or effective) */

Function Documentation

◆ check_constant_qual()

static bool check_constant_qual ( List qual,
bool is_const_false 
)
static

Definition at line 514 of file nodeMergejoin.c.

515{
516 ListCell *lc;
517
518 foreach(lc, qual)
519 {
520 Const *con = (Const *) lfirst(lc);
521
522 if (!con || !IsA(con, Const))
523 return false;
524 if (con->constisnull || !DatumGetBool(con->constvalue))
525 *is_const_false = true;
526 }
527 return true;
528}

References DatumGetBool(), fb(), IsA, and lfirst.

Referenced by ExecInitMergeJoin().

◆ ExecEndMergeJoin()

void ExecEndMergeJoin ( MergeJoinState node)

Definition at line 1636 of file nodeMergejoin.c.

1637{
1638 MJ1_printf("ExecEndMergeJoin: %s\n",
1639 "ending node processing");
1640
1641 /*
1642 * shut down the subplans
1643 */
1646
1647 MJ1_printf("ExecEndMergeJoin: %s\n",
1648 "node processing ended");
1649}

References ExecEndNode(), innerPlanState, MJ1_printf, and outerPlanState.

Referenced by ExecEndNode().

◆ ExecInitMergeJoin()

MergeJoinState * ExecInitMergeJoin ( MergeJoin node,
EState estate,
int  eflags 
)

Definition at line 1439 of file nodeMergejoin.c.

1440{
1443 innerDesc;
1445
1446 /* check for unsupported flags */
1447 Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
1448
1449 MJ1_printf("ExecInitMergeJoin: %s\n",
1450 "initializing node");
1451
1452 /*
1453 * create state structure
1454 */
1456 mergestate->js.ps.plan = (Plan *) node;
1457 mergestate->js.ps.state = estate;
1458 mergestate->js.ps.ExecProcNode = ExecMergeJoin;
1459 mergestate->js.jointype = node->join.jointype;
1460 mergestate->mj_ConstFalseJoin = false;
1461
1462 /*
1463 * Miscellaneous initialization
1464 *
1465 * create expression context for node
1466 */
1467 ExecAssignExprContext(estate, &mergestate->js.ps);
1468
1469 /*
1470 * we need two additional econtexts in which we can compute the join
1471 * expressions from the left and right input tuples. The node's regular
1472 * econtext won't do because it gets reset too often.
1473 */
1474 mergestate->mj_OuterEContext = CreateExprContext(estate);
1475 mergestate->mj_InnerEContext = CreateExprContext(estate);
1476
1477 /*
1478 * initialize child nodes
1479 *
1480 * inner child must support MARK/RESTORE, unless we have detected that we
1481 * don't need that. Note that skip_mark_restore must never be set if
1482 * there are non-mergeclause joinquals, since the logic wouldn't work.
1483 */
1484 Assert(node->join.joinqual == NIL || !node->skip_mark_restore);
1485 mergestate->mj_SkipMarkRestore = node->skip_mark_restore;
1486
1487 outerPlanState(mergestate) = ExecInitNode(outerPlan(node), estate, eflags);
1490 mergestate->mj_SkipMarkRestore ?
1491 eflags :
1492 (eflags | EXEC_FLAG_MARK));
1494
1495 /*
1496 * For certain types of inner child nodes, it is advantageous to issue
1497 * MARK every time we advance past an inner tuple we will never return to.
1498 * For other types, MARK on a tuple we cannot return to is a waste of
1499 * cycles. Detect which case applies and set mj_ExtraMarks if we want to
1500 * issue "unnecessary" MARK calls.
1501 *
1502 * Currently, only Material wants the extra MARKs, and it will be helpful
1503 * only if eflags doesn't specify REWIND.
1504 *
1505 * Note that for IndexScan and IndexOnlyScan, it is *necessary* that we
1506 * not set mj_ExtraMarks; otherwise we might attempt to set a mark before
1507 * the first inner tuple, which they do not support.
1508 */
1509 if (IsA(innerPlan(node), Material) &&
1510 (eflags & EXEC_FLAG_REWIND) == 0 &&
1511 !mergestate->mj_SkipMarkRestore)
1512 mergestate->mj_ExtraMarks = true;
1513 else
1514 mergestate->mj_ExtraMarks = false;
1515
1516 /*
1517 * Initialize result slot, type and projection.
1518 */
1521
1522 /*
1523 * tuple table initialization
1524 */
1526 mergestate->mj_MarkedTupleSlot = ExecInitExtraTupleSlot(estate, innerDesc,
1527 innerOps);
1528
1529 /*
1530 * initialize child expressions
1531 */
1532 mergestate->js.ps.qual =
1533 ExecInitQual(node->join.plan.qual, (PlanState *) mergestate);
1534 mergestate->js.joinqual =
1536 /* mergeclauses are handled below */
1537
1538 /*
1539 * detect whether we need only consider the first matching inner tuple
1540 */
1541 mergestate->js.single_match = (node->join.inner_unique ||
1542 node->join.jointype == JOIN_SEMI);
1543
1544 /* set up null tuples for outer joins, if needed */
1545 switch (node->join.jointype)
1546 {
1547 case JOIN_INNER:
1548 case JOIN_SEMI:
1549 mergestate->mj_FillOuter = false;
1550 mergestate->mj_FillInner = false;
1551 break;
1552 case JOIN_LEFT:
1553 case JOIN_ANTI:
1554 mergestate->mj_FillOuter = true;
1555 mergestate->mj_FillInner = false;
1556 mergestate->mj_NullInnerTupleSlot =
1558 break;
1559 case JOIN_RIGHT:
1560 case JOIN_RIGHT_ANTI:
1561 mergestate->mj_FillOuter = false;
1562 mergestate->mj_FillInner = true;
1563 mergestate->mj_NullOuterTupleSlot =
1565
1566 /*
1567 * Can't handle right, right-anti or full join with non-constant
1568 * extra joinclauses. This should have been caught by planner.
1569 */
1571 &mergestate->mj_ConstFalseJoin))
1572 ereport(ERROR,
1574 errmsg("RIGHT JOIN is only supported with merge-joinable join conditions")));
1575 break;
1576 case JOIN_FULL:
1577 mergestate->mj_FillOuter = true;
1578 mergestate->mj_FillInner = true;
1579 mergestate->mj_NullOuterTupleSlot =
1581 mergestate->mj_NullInnerTupleSlot =
1583
1584 /*
1585 * Can't handle right, right-anti or full join with non-constant
1586 * extra joinclauses. This should have been caught by planner.
1587 */
1589 &mergestate->mj_ConstFalseJoin))
1590 ereport(ERROR,
1592 errmsg("FULL JOIN is only supported with merge-joinable join conditions")));
1593 break;
1594 default:
1595 elog(ERROR, "unrecognized join type: %d",
1596 (int) node->join.jointype);
1597 }
1598
1599 /*
1600 * preprocess the merge clauses
1601 */
1602 mergestate->mj_NumClauses = list_length(node->mergeclauses);
1603 mergestate->mj_Clauses = MJExamineQuals(node->mergeclauses,
1604 node->mergeFamilies,
1605 node->mergeCollations,
1606 node->mergeReversals,
1607 node->mergeNullsFirst,
1608 (PlanState *) mergestate);
1609
1610 /*
1611 * initialize join state
1612 */
1613 mergestate->mj_JoinState = EXEC_MJ_INITIALIZE_OUTER;
1614 mergestate->mj_MatchedOuter = false;
1615 mergestate->mj_MatchedInner = false;
1616 mergestate->mj_OuterTupleSlot = NULL;
1617 mergestate->mj_InnerTupleSlot = NULL;
1618
1619 /*
1620 * initialization successful
1621 */
1622 MJ1_printf("ExecInitMergeJoin: %s\n",
1623 "node initialized");
1624
1625 return mergestate;
1626}

References Assert, check_constant_qual(), CreateExprContext(), elog, ereport, errcode(), errmsg(), ERROR, EXEC_FLAG_BACKWARD, EXEC_FLAG_MARK, EXEC_FLAG_REWIND, EXEC_MJ_INITIALIZE_OUTER, ExecAssignExprContext(), ExecAssignProjectionInfo(), ExecGetResultSlotOps(), ExecGetResultType(), ExecInitExtraTupleSlot(), ExecInitNode(), ExecInitNullTupleSlot(), ExecInitQual(), ExecInitResultTupleSlotTL(), ExecMergeJoin(), fb(), Join::inner_unique, innerPlan, innerPlanState, IsA, MergeJoin::join, JOIN_ANTI, JOIN_FULL, JOIN_INNER, JOIN_LEFT, JOIN_RIGHT, JOIN_RIGHT_ANTI, JOIN_SEMI, Join::joinqual, Join::jointype, list_length(), makeNode, MergeJoin::mergeclauses, MJ1_printf, MJExamineQuals(), NIL, outerPlan, outerPlanState, MergeJoin::skip_mark_restore, and TTSOpsVirtual.

Referenced by ExecInitNode().

◆ ExecMergeJoin()

static TupleTableSlot * ExecMergeJoin ( PlanState pstate)
static

Definition at line 594 of file nodeMergejoin.c.

595{
596 MergeJoinState *node = castNode(MergeJoinState, pstate);
597 ExprState *joinqual;
599 bool qualResult;
600 int compareResult;
605 ExprContext *econtext;
606 bool doFillOuter;
607 bool doFillInner;
608
610
611 /*
612 * get information from node
613 */
616 econtext = node->js.ps.ps_ExprContext;
617 joinqual = node->js.joinqual;
618 otherqual = node->js.ps.qual;
621
622 /*
623 * Reset per-tuple memory context to free any expression evaluation
624 * storage allocated in the previous tuple cycle.
625 */
626 ResetExprContext(econtext);
627
628 /*
629 * ok, everything is setup.. let's go to work
630 */
631 for (;;)
632 {
633 MJ_dump(node);
634
635 /*
636 * get the current state of the join and do things accordingly.
637 */
638 switch (node->mj_JoinState)
639 {
640 /*
641 * EXEC_MJ_INITIALIZE_OUTER means that this is the first time
642 * ExecMergeJoin() has been called and so we have to fetch the
643 * first matchable tuple for both outer and inner subplans. We
644 * do the outer side in INITIALIZE_OUTER state, then advance
645 * to INITIALIZE_INNER state for the inner subplan.
646 */
648 MJ_printf("ExecMergeJoin: EXEC_MJ_INITIALIZE_OUTER\n");
649
652
653 /* Compute join values and check for unmatchability */
654 switch (MJEvalOuterValues(node))
655 {
656 case MJEVAL_MATCHABLE:
657 /* OK to go get the first inner tuple */
659 break;
661 /* Stay in same state to fetch next outer tuple */
662 if (doFillOuter)
663 {
664 /*
665 * Generate a fake join tuple with nulls for the
666 * inner tuple, and return it if it passes the
667 * non-join quals.
668 */
669 TupleTableSlot *result;
670
671 result = MJFillOuter(node);
672 if (result)
673 return result;
674 }
675 break;
676 case MJEVAL_ENDOFJOIN:
677 /* No more outer tuples */
678 MJ_printf("ExecMergeJoin: nothing in outer subplan\n");
679 if (doFillInner)
680 {
681 /*
682 * Need to emit right-join tuples for remaining
683 * inner tuples. We set MatchedInner = true to
684 * force the ENDOUTER state to advance inner.
685 */
687 node->mj_MatchedInner = true;
688 break;
689 }
690 /* Otherwise we're done. */
691 return NULL;
692 }
693 break;
694
696 MJ_printf("ExecMergeJoin: EXEC_MJ_INITIALIZE_INNER\n");
697
700
701 /* Compute join values and check for unmatchability */
702 switch (MJEvalInnerValues(node, innerTupleSlot))
703 {
704 case MJEVAL_MATCHABLE:
705
706 /*
707 * OK, we have the initial tuples. Begin by skipping
708 * non-matching tuples.
709 */
711 break;
713 /* Mark before advancing, if wanted */
714 if (node->mj_ExtraMarks)
716 /* Stay in same state to fetch next inner tuple */
717 if (doFillInner)
718 {
719 /*
720 * Generate a fake join tuple with nulls for the
721 * outer tuple, and return it if it passes the
722 * non-join quals.
723 */
724 TupleTableSlot *result;
725
726 result = MJFillInner(node);
727 if (result)
728 return result;
729 }
730 break;
731 case MJEVAL_ENDOFJOIN:
732 /* No more inner tuples */
733 MJ_printf("ExecMergeJoin: nothing in inner subplan\n");
734 if (doFillOuter)
735 {
736 /*
737 * Need to emit left-join tuples for all outer
738 * tuples, including the one we just fetched. We
739 * set MatchedOuter = false to force the ENDINNER
740 * state to emit first tuple before advancing
741 * outer.
742 */
744 node->mj_MatchedOuter = false;
745 break;
746 }
747 /* Otherwise we're done. */
748 return NULL;
749 }
750 break;
751
752 /*
753 * EXEC_MJ_JOINTUPLES means we have two tuples which satisfied
754 * the merge clause so we join them and then proceed to get
755 * the next inner tuple (EXEC_MJ_NEXTINNER).
756 */
758 MJ_printf("ExecMergeJoin: EXEC_MJ_JOINTUPLES\n");
759
760 /*
761 * Set the next state machine state. The right things will
762 * happen whether we return this join tuple or just fall
763 * through to continue the state machine execution.
764 */
766
767 /*
768 * Check the extra qual conditions to see if we actually want
769 * to return this join tuple. If not, can proceed with merge.
770 * We must distinguish the additional joinquals (which must
771 * pass to consider the tuples "matched" for outer-join logic)
772 * from the otherquals (which must pass before we actually
773 * return the tuple).
774 *
775 * We don't bother with a ResetExprContext here, on the
776 * assumption that we just did one while checking the merge
777 * qual. One per tuple should be sufficient. We do have to
778 * set up the econtext links to the tuples for ExecQual to
779 * use.
780 */
785
786 qualResult = (joinqual == NULL ||
787 ExecQual(joinqual, econtext));
788 MJ_DEBUG_QUAL(joinqual, qualResult);
789
790 if (qualResult)
791 {
792 node->mj_MatchedOuter = true;
793 node->mj_MatchedInner = true;
794
795 /* In an antijoin, we never return a matched tuple */
796 if (node->js.jointype == JOIN_ANTI)
797 {
799 break;
800 }
801
802 /*
803 * If we only need to consider the first matching inner
804 * tuple, then advance to next outer tuple after we've
805 * processed this one.
806 */
807 if (node->js.single_match)
809
810 /*
811 * In a right-antijoin, we never return a matched tuple.
812 * If it's not an inner_unique join, we need to stay on
813 * the current outer tuple to continue scanning the inner
814 * side for matches.
815 */
816 if (node->js.jointype == JOIN_RIGHT_ANTI)
817 break;
818
819 qualResult = (otherqual == NULL ||
820 ExecQual(otherqual, econtext));
822
823 if (qualResult)
824 {
825 /*
826 * qualification succeeded. now form the desired
827 * projection tuple and return the slot containing it.
828 */
829 MJ_printf("ExecMergeJoin: returning tuple\n");
830
831 return ExecProject(node->js.ps.ps_ProjInfo);
832 }
833 else
834 InstrCountFiltered2(node, 1);
835 }
836 else
837 InstrCountFiltered1(node, 1);
838 break;
839
840 /*
841 * EXEC_MJ_NEXTINNER means advance the inner scan to the next
842 * tuple. If the tuple is not nil, we then proceed to test it
843 * against the join qualification.
844 *
845 * Before advancing, we check to see if we must emit an
846 * outer-join fill tuple for this inner tuple.
847 */
849 MJ_printf("ExecMergeJoin: EXEC_MJ_NEXTINNER\n");
850
851 if (doFillInner && !node->mj_MatchedInner)
852 {
853 /*
854 * Generate a fake join tuple with nulls for the outer
855 * tuple, and return it if it passes the non-join quals.
856 */
857 TupleTableSlot *result;
858
859 node->mj_MatchedInner = true; /* do it only once */
860
861 result = MJFillInner(node);
862 if (result)
863 return result;
864 }
865
866 /*
867 * now we get the next inner tuple, if any. If there's none,
868 * advance to next outer tuple (which may be able to join to
869 * previously marked tuples).
870 *
871 * NB: must NOT do "extraMarks" here, since we may need to
872 * return to previously marked tuples.
873 */
877 node->mj_MatchedInner = false;
878
879 /* Compute join values and check for unmatchability */
880 switch (MJEvalInnerValues(node, innerTupleSlot))
881 {
882 case MJEVAL_MATCHABLE:
883
884 /*
885 * Test the new inner tuple to see if it matches
886 * outer.
887 *
888 * If they do match, then we join them and move on to
889 * the next inner tuple (EXEC_MJ_JOINTUPLES).
890 *
891 * If they do not match then advance to next outer
892 * tuple.
893 */
894 compareResult = MJCompare(node);
896
897 if (compareResult == 0)
899 else if (compareResult < 0)
901 else /* compareResult > 0 should not happen */
902 elog(ERROR, "mergejoin input data is out of order");
903 break;
905
906 /*
907 * It contains a NULL and hence can't match any outer
908 * tuple, so we can skip the comparison and assume the
909 * new tuple is greater than current outer.
910 */
912 break;
913 case MJEVAL_ENDOFJOIN:
914
915 /*
916 * No more inner tuples. However, this might be only
917 * effective and not physical end of inner plan, so
918 * force mj_InnerTupleSlot to null to make sure we
919 * don't fetch more inner tuples. (We need this hack
920 * because we are not transiting to a state where the
921 * inner plan is assumed to be exhausted.)
922 */
923 node->mj_InnerTupleSlot = NULL;
925 break;
926 }
927 break;
928
929 /*-------------------------------------------
930 * EXEC_MJ_NEXTOUTER means
931 *
932 * outer inner
933 * outer tuple - 5 5 - marked tuple
934 * 5 5
935 * 6 6 - inner tuple
936 * 7 7
937 *
938 * we know we just bumped into the
939 * first inner tuple > current outer tuple (or possibly
940 * the end of the inner stream)
941 * so get a new outer tuple and then
942 * proceed to test it against the marked tuple
943 * (EXEC_MJ_TESTOUTER)
944 *
945 * Before advancing, we check to see if we must emit an
946 * outer-join fill tuple for this outer tuple.
947 *------------------------------------------------
948 */
950 MJ_printf("ExecMergeJoin: EXEC_MJ_NEXTOUTER\n");
951
952 if (doFillOuter && !node->mj_MatchedOuter)
953 {
954 /*
955 * Generate a fake join tuple with nulls for the inner
956 * tuple, and return it if it passes the non-join quals.
957 */
958 TupleTableSlot *result;
959
960 node->mj_MatchedOuter = true; /* do it only once */
961
962 result = MJFillOuter(node);
963 if (result)
964 return result;
965 }
966
967 /*
968 * now we get the next outer tuple, if any
969 */
973 node->mj_MatchedOuter = false;
974
975 /* Compute join values and check for unmatchability */
976 switch (MJEvalOuterValues(node))
977 {
978 case MJEVAL_MATCHABLE:
979 /* Go test the new tuple against the marked tuple */
981 break;
983 /* Can't match, so fetch next outer tuple */
985 break;
986 case MJEVAL_ENDOFJOIN:
987 /* No more outer tuples */
988 MJ_printf("ExecMergeJoin: end of outer subplan\n");
991 {
992 /*
993 * Need to emit right-join tuples for remaining
994 * inner tuples.
995 */
997 break;
998 }
999 /* Otherwise we're done. */
1000 return NULL;
1001 }
1002 break;
1003
1004 /*--------------------------------------------------------
1005 * EXEC_MJ_TESTOUTER If the new outer tuple and the marked
1006 * tuple satisfy the merge clause then we know we have
1007 * duplicates in the outer scan so we have to restore the
1008 * inner scan to the marked tuple and proceed to join the
1009 * new outer tuple with the inner tuples.
1010 *
1011 * This is the case when
1012 * outer inner
1013 * 4 5 - marked tuple
1014 * outer tuple - 5 5
1015 * new outer tuple - 5 5
1016 * 6 8 - inner tuple
1017 * 7 12
1018 *
1019 * new outer tuple == marked tuple
1020 *
1021 * If the outer tuple fails the test, then we are done
1022 * with the marked tuples, and we have to look for a
1023 * match to the current inner tuple. So we will
1024 * proceed to skip outer tuples until outer >= inner
1025 * (EXEC_MJ_SKIP_TEST).
1026 *
1027 * This is the case when
1028 *
1029 * outer inner
1030 * 5 5 - marked tuple
1031 * outer tuple - 5 5
1032 * new outer tuple - 6 8 - inner tuple
1033 * 7 12
1034 *
1035 * new outer tuple > marked tuple
1036 *
1037 *---------------------------------------------------------
1038 */
1039 case EXEC_MJ_TESTOUTER:
1040 MJ_printf("ExecMergeJoin: EXEC_MJ_TESTOUTER\n");
1041
1042 /*
1043 * Here we must compare the outer tuple with the marked inner
1044 * tuple. (We can ignore the result of MJEvalInnerValues,
1045 * since the marked inner tuple is certainly matchable.)
1046 */
1049
1050 compareResult = MJCompare(node);
1052
1053 if (compareResult == 0)
1054 {
1055 /*
1056 * the merge clause matched so now we restore the inner
1057 * scan position to the first mark, and go join that tuple
1058 * (and any following ones) to the new outer.
1059 *
1060 * If we were able to determine mark and restore are not
1061 * needed, then we don't have to back up; the current
1062 * inner is already the first possible match.
1063 *
1064 * NOTE: we do not need to worry about the MatchedInner
1065 * state for the rescanned inner tuples. We know all of
1066 * them will match this new outer tuple and therefore
1067 * won't be emitted as fill tuples. This works *only*
1068 * because we require the extra joinquals to be constant
1069 * when doing a right, right-anti or full join ---
1070 * otherwise some of the rescanned tuples might fail the
1071 * extra joinquals. This obviously won't happen for a
1072 * constant-true extra joinqual, while the constant-false
1073 * case is handled by forcing the merge clause to never
1074 * match, so we never get here.
1075 */
1076 if (!node->mj_SkipMarkRestore)
1077 {
1079
1080 /*
1081 * ExecRestrPos probably should give us back a new
1082 * Slot, but since it doesn't, use the marked slot.
1083 * (The previously returned mj_InnerTupleSlot cannot
1084 * be assumed to hold the required tuple.)
1085 */
1087 /* we need not do MJEvalInnerValues again */
1088 }
1089
1091 }
1092 else if (compareResult > 0)
1093 {
1094 /* ----------------
1095 * if the new outer tuple didn't match the marked inner
1096 * tuple then we have a case like:
1097 *
1098 * outer inner
1099 * 4 4 - marked tuple
1100 * new outer - 5 4
1101 * 6 5 - inner tuple
1102 * 7
1103 *
1104 * which means that all subsequent outer tuples will be
1105 * larger than our marked inner tuples. So we need not
1106 * revisit any of the marked tuples but can proceed to
1107 * look for a match to the current inner. If there's
1108 * no more inners, no more matches are possible.
1109 * ----------------
1110 */
1112
1113 /* reload comparison data for current inner */
1114 switch (MJEvalInnerValues(node, innerTupleSlot))
1115 {
1116 case MJEVAL_MATCHABLE:
1117 /* proceed to compare it to the current outer */
1119 break;
1121
1122 /*
1123 * current inner can't possibly match any outer;
1124 * better to advance the inner scan than the
1125 * outer.
1126 */
1128 break;
1129 case MJEVAL_ENDOFJOIN:
1130 /* No more inner tuples */
1131 if (doFillOuter)
1132 {
1133 /*
1134 * Need to emit left-join tuples for remaining
1135 * outer tuples.
1136 */
1138 break;
1139 }
1140 /* Otherwise we're done. */
1141 return NULL;
1142 }
1143 }
1144 else /* compareResult < 0 should not happen */
1145 elog(ERROR, "mergejoin input data is out of order");
1146 break;
1147
1148 /*----------------------------------------------------------
1149 * EXEC_MJ_SKIP_TEST means compare tuples and if they do not
1150 * match, skip whichever is lesser.
1151 *
1152 * For example:
1153 *
1154 * outer inner
1155 * 5 5
1156 * 5 5
1157 * outer tuple - 6 8 - inner tuple
1158 * 7 12
1159 * 8 14
1160 *
1161 * we have to advance the outer scan
1162 * until we find the outer 8.
1163 *
1164 * On the other hand:
1165 *
1166 * outer inner
1167 * 5 5
1168 * 5 5
1169 * outer tuple - 12 8 - inner tuple
1170 * 14 10
1171 * 17 12
1172 *
1173 * we have to advance the inner scan
1174 * until we find the inner 12.
1175 *----------------------------------------------------------
1176 */
1177 case EXEC_MJ_SKIP_TEST:
1178 MJ_printf("ExecMergeJoin: EXEC_MJ_SKIP_TEST\n");
1179
1180 /*
1181 * before we advance, make sure the current tuples do not
1182 * satisfy the mergeclauses. If they do, then we update the
1183 * marked tuple position and go join them.
1184 */
1185 compareResult = MJCompare(node);
1187
1188 if (compareResult == 0)
1189 {
1190 if (!node->mj_SkipMarkRestore)
1192
1193 MarkInnerTuple(node->mj_InnerTupleSlot, node);
1194
1196 }
1197 else if (compareResult < 0)
1199 else
1200 /* compareResult > 0 */
1202 break;
1203
1204 /*
1205 * EXEC_MJ_SKIPOUTER_ADVANCE: advance over an outer tuple that
1206 * is known not to join to any inner tuple.
1207 *
1208 * Before advancing, we check to see if we must emit an
1209 * outer-join fill tuple for this outer tuple.
1210 */
1212 MJ_printf("ExecMergeJoin: EXEC_MJ_SKIPOUTER_ADVANCE\n");
1213
1214 if (doFillOuter && !node->mj_MatchedOuter)
1215 {
1216 /*
1217 * Generate a fake join tuple with nulls for the inner
1218 * tuple, and return it if it passes the non-join quals.
1219 */
1220 TupleTableSlot *result;
1221
1222 node->mj_MatchedOuter = true; /* do it only once */
1223
1224 result = MJFillOuter(node);
1225 if (result)
1226 return result;
1227 }
1228
1229 /*
1230 * now we get the next outer tuple, if any
1231 */
1235 node->mj_MatchedOuter = false;
1236
1237 /* Compute join values and check for unmatchability */
1238 switch (MJEvalOuterValues(node))
1239 {
1240 case MJEVAL_MATCHABLE:
1241 /* Go test the new tuple against the current inner */
1243 break;
1245 /* Can't match, so fetch next outer tuple */
1247 break;
1248 case MJEVAL_ENDOFJOIN:
1249 /* No more outer tuples */
1250 MJ_printf("ExecMergeJoin: end of outer subplan\n");
1253 {
1254 /*
1255 * Need to emit right-join tuples for remaining
1256 * inner tuples.
1257 */
1259 break;
1260 }
1261 /* Otherwise we're done. */
1262 return NULL;
1263 }
1264 break;
1265
1266 /*
1267 * EXEC_MJ_SKIPINNER_ADVANCE: advance over an inner tuple that
1268 * is known not to join to any outer tuple.
1269 *
1270 * Before advancing, we check to see if we must emit an
1271 * outer-join fill tuple for this inner tuple.
1272 */
1274 MJ_printf("ExecMergeJoin: EXEC_MJ_SKIPINNER_ADVANCE\n");
1275
1276 if (doFillInner && !node->mj_MatchedInner)
1277 {
1278 /*
1279 * Generate a fake join tuple with nulls for the outer
1280 * tuple, and return it if it passes the non-join quals.
1281 */
1282 TupleTableSlot *result;
1283
1284 node->mj_MatchedInner = true; /* do it only once */
1285
1286 result = MJFillInner(node);
1287 if (result)
1288 return result;
1289 }
1290
1291 /* Mark before advancing, if wanted */
1292 if (node->mj_ExtraMarks)
1294
1295 /*
1296 * now we get the next inner tuple, if any
1297 */
1301 node->mj_MatchedInner = false;
1302
1303 /* Compute join values and check for unmatchability */
1304 switch (MJEvalInnerValues(node, innerTupleSlot))
1305 {
1306 case MJEVAL_MATCHABLE:
1307 /* proceed to compare it to the current outer */
1309 break;
1311
1312 /*
1313 * current inner can't possibly match any outer;
1314 * better to advance the inner scan than the outer.
1315 */
1317 break;
1318 case MJEVAL_ENDOFJOIN:
1319 /* No more inner tuples */
1320 MJ_printf("ExecMergeJoin: end of inner subplan\n");
1323 {
1324 /*
1325 * Need to emit left-join tuples for remaining
1326 * outer tuples.
1327 */
1329 break;
1330 }
1331 /* Otherwise we're done. */
1332 return NULL;
1333 }
1334 break;
1335
1336 /*
1337 * EXEC_MJ_ENDOUTER means we have run out of outer tuples, but
1338 * are doing a right/right-anti/full join and therefore must
1339 * null-fill any remaining unmatched inner tuples.
1340 */
1341 case EXEC_MJ_ENDOUTER:
1342 MJ_printf("ExecMergeJoin: EXEC_MJ_ENDOUTER\n");
1343
1345
1346 if (!node->mj_MatchedInner)
1347 {
1348 /*
1349 * Generate a fake join tuple with nulls for the outer
1350 * tuple, and return it if it passes the non-join quals.
1351 */
1352 TupleTableSlot *result;
1353
1354 node->mj_MatchedInner = true; /* do it only once */
1355
1356 result = MJFillInner(node);
1357 if (result)
1358 return result;
1359 }
1360
1361 /* Mark before advancing, if wanted */
1362 if (node->mj_ExtraMarks)
1364
1365 /*
1366 * now we get the next inner tuple, if any
1367 */
1371 node->mj_MatchedInner = false;
1372
1374 {
1375 MJ_printf("ExecMergeJoin: end of inner subplan\n");
1376 return NULL;
1377 }
1378
1379 /* Else remain in ENDOUTER state and process next tuple. */
1380 break;
1381
1382 /*
1383 * EXEC_MJ_ENDINNER means we have run out of inner tuples, but
1384 * are doing a left/full join and therefore must null- fill
1385 * any remaining unmatched outer tuples.
1386 */
1387 case EXEC_MJ_ENDINNER:
1388 MJ_printf("ExecMergeJoin: EXEC_MJ_ENDINNER\n");
1389
1391
1392 if (!node->mj_MatchedOuter)
1393 {
1394 /*
1395 * Generate a fake join tuple with nulls for the inner
1396 * tuple, and return it if it passes the non-join quals.
1397 */
1398 TupleTableSlot *result;
1399
1400 node->mj_MatchedOuter = true; /* do it only once */
1401
1402 result = MJFillOuter(node);
1403 if (result)
1404 return result;
1405 }
1406
1407 /*
1408 * now we get the next outer tuple, if any
1409 */
1413 node->mj_MatchedOuter = false;
1414
1416 {
1417 MJ_printf("ExecMergeJoin: end of outer subplan\n");
1418 return NULL;
1419 }
1420
1421 /* Else remain in ENDINNER state and process next tuple. */
1422 break;
1423
1424 /*
1425 * broken state value?
1426 */
1427 default:
1428 elog(ERROR, "unrecognized mergejoin state: %d",
1429 (int) node->mj_JoinState);
1430 }
1431 }
1432}

References Assert, castNode, CHECK_FOR_INTERRUPTS, ExprContext::ecxt_innertuple, ExprContext::ecxt_outertuple, elog, ERROR, EXEC_MJ_ENDINNER, EXEC_MJ_ENDOUTER, EXEC_MJ_INITIALIZE_INNER, EXEC_MJ_INITIALIZE_OUTER, EXEC_MJ_JOINTUPLES, EXEC_MJ_NEXTINNER, EXEC_MJ_NEXTOUTER, EXEC_MJ_SKIP_TEST, EXEC_MJ_SKIPINNER_ADVANCE, EXEC_MJ_SKIPOUTER_ADVANCE, EXEC_MJ_TESTOUTER, ExecMarkPos(), ExecProcNode(), ExecProject(), ExecQual(), ExecRestrPos(), fb(), innerPlan, innerPlanState, InstrCountFiltered1, InstrCountFiltered2, JOIN_ANTI, JOIN_RIGHT_ANTI, JoinState::joinqual, JoinState::jointype, MergeJoinState::js, MarkInnerTuple, MJ_DEBUG_COMPARE, MJ_DEBUG_PROC_NODE, MJ_DEBUG_QUAL, MJ_dump, MergeJoinState::mj_ExtraMarks, MergeJoinState::mj_FillInner, MergeJoinState::mj_FillOuter, MergeJoinState::mj_InnerTupleSlot, MergeJoinState::mj_JoinState, MergeJoinState::mj_MarkedTupleSlot, MergeJoinState::mj_MatchedInner, MergeJoinState::mj_MatchedOuter, MergeJoinState::mj_OuterTupleSlot, MJ_printf, MergeJoinState::mj_SkipMarkRestore, MJCompare(), MJEVAL_ENDOFJOIN, MJEVAL_MATCHABLE, MJEVAL_NONMATCHABLE, MJEvalInnerValues(), MJEvalOuterValues(), MJFillInner(), MJFillOuter(), outerPlan, outerPlanState, JoinState::ps, PlanState::ps_ExprContext, PlanState::ps_ProjInfo, PlanState::qual, ResetExprContext, JoinState::single_match, and TupIsNull.

Referenced by ExecInitMergeJoin().

◆ ExecReScanMergeJoin()

void ExecReScanMergeJoin ( MergeJoinState node)

Definition at line 1652 of file nodeMergejoin.c.

1653{
1656
1658
1660 node->mj_MatchedOuter = false;
1661 node->mj_MatchedInner = false;
1662 node->mj_OuterTupleSlot = NULL;
1663 node->mj_InnerTupleSlot = NULL;
1664
1665 /*
1666 * if chgParam of subnodes is not null then plans will be re-scanned by
1667 * first ExecProcNode.
1668 */
1669 if (outerPlan->chgParam == NULL)
1671 if (innerPlan->chgParam == NULL)
1673}

References EXEC_MJ_INITIALIZE_OUTER, ExecClearTuple(), ExecReScan(), fb(), innerPlan, innerPlanState, MergeJoinState::mj_InnerTupleSlot, MergeJoinState::mj_JoinState, MergeJoinState::mj_MarkedTupleSlot, MergeJoinState::mj_MatchedInner, MergeJoinState::mj_MatchedOuter, MergeJoinState::mj_OuterTupleSlot, outerPlan, and outerPlanState.

Referenced by ExecReScan().

◆ MJCompare()

static int MJCompare ( MergeJoinState mergestate)
static

Definition at line 386 of file nodeMergejoin.c.

387{
388 int result = 0;
389 bool nulleqnull = false;
390 ExprContext *econtext = mergestate->js.ps.ps_ExprContext;
391 int i;
393
394 /*
395 * Call the comparison functions in short-lived context, in case they leak
396 * memory.
397 */
398 ResetExprContext(econtext);
399
401
402 for (i = 0; i < mergestate->mj_NumClauses; i++)
403 {
404 MergeJoinClause clause = &mergestate->mj_Clauses[i];
405
406 /*
407 * Special case for NULL-vs-NULL, else use standard comparison.
408 */
409 if (clause->lisnull && clause->risnull)
410 {
411 nulleqnull = true; /* NULL "=" NULL */
412 continue;
413 }
414
415 result = ApplySortComparator(clause->ldatum, clause->lisnull,
416 clause->rdatum, clause->risnull,
417 &clause->ssup);
418
419 if (result != 0)
420 break;
421 }
422
423 /*
424 * If we had any NULL-vs-NULL inputs, we do not want to report that the
425 * tuples are equal. Instead, if result is still 0, change it to +1. This
426 * will result in advancing the inner side of the join.
427 *
428 * Likewise, if there was a constant-false joinqual, do not report
429 * equality. We have to check this as part of the mergequals, else the
430 * rescan logic will do the wrong thing.
431 */
432 if (result == 0 &&
433 (nulleqnull || mergestate->mj_ConstFalseJoin))
434 result = 1;
435
437
438 return result;
439}

References ApplySortComparator(), ExprContext::ecxt_per_tuple_memory, fb(), i, MergeJoinClauseData::ldatum, MergeJoinClauseData::lisnull, MemoryContextSwitchTo(), MergeJoinClauseData::rdatum, ResetExprContext, MergeJoinClauseData::risnull, and MergeJoinClauseData::ssup.

Referenced by ExecMergeJoin().

◆ MJEvalInnerValues()

static MJEvalResult MJEvalInnerValues ( MergeJoinState mergestate,
TupleTableSlot innerslot 
)
static

Definition at line 336 of file nodeMergejoin.c.

337{
338 ExprContext *econtext = mergestate->mj_InnerEContext;
340 int i;
342
343 /* Check for end of inner subplan */
344 if (TupIsNull(innerslot))
345 return MJEVAL_ENDOFJOIN;
346
347 ResetExprContext(econtext);
348
350
351 econtext->ecxt_innertuple = innerslot;
352
353 for (i = 0; i < mergestate->mj_NumClauses; i++)
354 {
355 MergeJoinClause clause = &mergestate->mj_Clauses[i];
356
357 clause->rdatum = ExecEvalExpr(clause->rexpr, econtext,
358 &clause->risnull);
359 if (clause->risnull)
360 {
361 /* match is impossible; can we end the join early? */
362 if (i == 0 && !clause->ssup.ssup_nulls_first &&
363 !mergestate->mj_FillInner)
364 result = MJEVAL_ENDOFJOIN;
365 else if (result == MJEVAL_MATCHABLE)
366 result = MJEVAL_NONMATCHABLE;
367 }
368 }
369
371
372 return result;
373}

References ExprContext::ecxt_innertuple, ExprContext::ecxt_per_tuple_memory, ExecEvalExpr(), fb(), i, MemoryContextSwitchTo(), MJEVAL_ENDOFJOIN, MJEVAL_MATCHABLE, MJEVAL_NONMATCHABLE, MergeJoinClauseData::rdatum, ResetExprContext, MergeJoinClauseData::rexpr, MergeJoinClauseData::risnull, MergeJoinClauseData::ssup, SortSupportData::ssup_nulls_first, and TupIsNull.

Referenced by ExecMergeJoin().

◆ MJEvalOuterValues()

static MJEvalResult MJEvalOuterValues ( MergeJoinState mergestate)
static

Definition at line 289 of file nodeMergejoin.c.

290{
291 ExprContext *econtext = mergestate->mj_OuterEContext;
293 int i;
295
296 /* Check for end of outer subplan */
297 if (TupIsNull(mergestate->mj_OuterTupleSlot))
298 return MJEVAL_ENDOFJOIN;
299
300 ResetExprContext(econtext);
301
303
304 econtext->ecxt_outertuple = mergestate->mj_OuterTupleSlot;
305
306 for (i = 0; i < mergestate->mj_NumClauses; i++)
307 {
308 MergeJoinClause clause = &mergestate->mj_Clauses[i];
309
310 clause->ldatum = ExecEvalExpr(clause->lexpr, econtext,
311 &clause->lisnull);
312 if (clause->lisnull)
313 {
314 /* match is impossible; can we end the join early? */
315 if (i == 0 && !clause->ssup.ssup_nulls_first &&
316 !mergestate->mj_FillOuter)
317 result = MJEVAL_ENDOFJOIN;
318 else if (result == MJEVAL_MATCHABLE)
319 result = MJEVAL_NONMATCHABLE;
320 }
321 }
322
324
325 return result;
326}

References ExprContext::ecxt_outertuple, ExprContext::ecxt_per_tuple_memory, ExecEvalExpr(), fb(), i, MergeJoinClauseData::ldatum, MergeJoinClauseData::lexpr, MergeJoinClauseData::lisnull, MemoryContextSwitchTo(), MJEVAL_ENDOFJOIN, MJEVAL_MATCHABLE, MJEVAL_NONMATCHABLE, ResetExprContext, MergeJoinClauseData::ssup, SortSupportData::ssup_nulls_first, and TupIsNull.

Referenced by ExecMergeJoin().

◆ MJExamineQuals()

static MergeJoinClause MJExamineQuals ( List mergeclauses,
Oid mergefamilies,
Oid mergecollations,
bool mergereversals,
bool mergenullsfirst,
PlanState parent 
)
static

Definition at line 175 of file nodeMergejoin.c.

181{
182 MergeJoinClause clauses;
183 int nClauses = list_length(mergeclauses);
184 int iClause;
185 ListCell *cl;
186
188
189 iClause = 0;
190 foreach(cl, mergeclauses)
191 {
192 OpExpr *qual = (OpExpr *) lfirst(cl);
193 MergeJoinClause clause = &clauses[iClause];
194 Oid opfamily = mergefamilies[iClause];
195 Oid collation = mergecollations[iClause];
197 bool nulls_first = mergenullsfirst[iClause];
198 int op_strategy;
202
203 if (!IsA(qual, OpExpr))
204 elog(ERROR, "mergejoin clause is not an OpExpr");
205
206 /*
207 * Prepare the input expressions for execution.
208 */
209 clause->lexpr = ExecInitExpr((Expr *) linitial(qual->args), parent);
210 clause->rexpr = ExecInitExpr((Expr *) lsecond(qual->args), parent);
211
212 /* Set up sort support data */
214 clause->ssup.ssup_collation = collation;
215 clause->ssup.ssup_reverse = reversed;
216 clause->ssup.ssup_nulls_first = nulls_first;
217
218 /* Extract the operator's declared left/right datatypes */
219 get_op_opfamily_properties(qual->opno, opfamily, false,
220 &op_strategy,
222 &op_righttype);
223 if (IndexAmTranslateStrategy(op_strategy, get_opfamily_method(opfamily), opfamily, true) != COMPARE_EQ) /* should not happen */
224 elog(ERROR, "cannot merge using non-equality operator %u",
225 qual->opno);
226
227 /*
228 * sortsupport routine must know if abbreviation optimization is
229 * applicable in principle. It is never applicable for merge joins
230 * because there is no convenient opportunity to convert to
231 * alternative representation.
232 */
233 clause->ssup.abbreviate = false;
234
235 /* And get the matching support or comparison function */
236 Assert(clause->ssup.comparator == NULL);
237 sortfunc = get_opfamily_proc(opfamily,
241 if (OidIsValid(sortfunc))
242 {
243 /* The sort support function can provide a comparator */
245 }
246 if (clause->ssup.comparator == NULL)
247 {
248 /* support not available, get comparison func */
249 sortfunc = get_opfamily_proc(opfamily,
253 if (!OidIsValid(sortfunc)) /* should not happen */
254 elog(ERROR, "missing support function %d(%u,%u) in opfamily %u",
256 /* We'll use a shim to call the old-style btree comparator */
258 }
259
260 iClause++;
261 }
262
263 return clauses;
264}

References SortSupportData::abbreviate, OpExpr::args, Assert, BTORDER_PROC, BTSORTSUPPORT_PROC, SortSupportData::comparator, COMPARE_EQ, CurrentMemoryContext, elog, ERROR, ExecInitExpr(), fb(), get_op_opfamily_properties(), get_opfamily_method(), get_opfamily_proc(), IndexAmTranslateStrategy(), IsA, MergeJoinClauseData::lexpr, lfirst, linitial, list_length(), lsecond, OidFunctionCall1, OidIsValid, OpExpr::opno, palloc0(), PointerGetDatum(), PrepareSortSupportComparisonShim(), MergeJoinClauseData::rexpr, MergeJoinClauseData::ssup, SortSupportData::ssup_collation, SortSupportData::ssup_cxt, SortSupportData::ssup_nulls_first, and SortSupportData::ssup_reverse.

Referenced by ExecInitMergeJoin().

◆ MJFillInner()

static TupleTableSlot * MJFillInner ( MergeJoinState node)
static

Definition at line 478 of file nodeMergejoin.c.

479{
480 ExprContext *econtext = node->js.ps.ps_ExprContext;
481 ExprState *otherqual = node->js.ps.qual;
482
483 ResetExprContext(econtext);
484
485 econtext->ecxt_outertuple = node->mj_NullOuterTupleSlot;
486 econtext->ecxt_innertuple = node->mj_InnerTupleSlot;
487
488 if (ExecQual(otherqual, econtext))
489 {
490 /*
491 * qualification succeeded. now form the desired projection tuple and
492 * return the slot containing it.
493 */
494 MJ_printf("ExecMergeJoin: returning inner fill tuple\n");
495
496 return ExecProject(node->js.ps.ps_ProjInfo);
497 }
498 else
499 InstrCountFiltered2(node, 1);
500
501 return NULL;
502}

References ExprContext::ecxt_innertuple, ExprContext::ecxt_outertuple, ExecProject(), ExecQual(), fb(), InstrCountFiltered2, MergeJoinState::js, MergeJoinState::mj_InnerTupleSlot, MergeJoinState::mj_NullOuterTupleSlot, MJ_printf, JoinState::ps, PlanState::ps_ExprContext, PlanState::ps_ProjInfo, PlanState::qual, and ResetExprContext.

Referenced by ExecMergeJoin().

◆ MJFillOuter()

static TupleTableSlot * MJFillOuter ( MergeJoinState node)
static

Definition at line 447 of file nodeMergejoin.c.

448{
449 ExprContext *econtext = node->js.ps.ps_ExprContext;
450 ExprState *otherqual = node->js.ps.qual;
451
452 ResetExprContext(econtext);
453
454 econtext->ecxt_outertuple = node->mj_OuterTupleSlot;
455 econtext->ecxt_innertuple = node->mj_NullInnerTupleSlot;
456
457 if (ExecQual(otherqual, econtext))
458 {
459 /*
460 * qualification succeeded. now form the desired projection tuple and
461 * return the slot containing it.
462 */
463 MJ_printf("ExecMergeJoin: returning outer fill tuple\n");
464
465 return ExecProject(node->js.ps.ps_ProjInfo);
466 }
467 else
468 InstrCountFiltered2(node, 1);
469
470 return NULL;
471}

References ExprContext::ecxt_innertuple, ExprContext::ecxt_outertuple, ExecProject(), ExecQual(), fb(), InstrCountFiltered2, MergeJoinState::js, MergeJoinState::mj_NullInnerTupleSlot, MergeJoinState::mj_OuterTupleSlot, MJ_printf, JoinState::ps, PlanState::ps_ExprContext, PlanState::ps_ProjInfo, PlanState::qual, and ResetExprContext.

Referenced by ExecMergeJoin().