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/instrument.h"
#include "executor/nodeMergejoin.h"
#include "miscadmin.h"
#include "utils/lsyscache.h"
#include "utils/sortsupport.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 117 of file nodeMergejoin.c.

◆ EXEC_MJ_ENDOUTER

#define EXEC_MJ_ENDOUTER   10

Definition at line 116 of file nodeMergejoin.c.

◆ EXEC_MJ_INITIALIZE_INNER

#define EXEC_MJ_INITIALIZE_INNER   2

Definition at line 108 of file nodeMergejoin.c.

◆ EXEC_MJ_INITIALIZE_OUTER

#define EXEC_MJ_INITIALIZE_OUTER   1

Definition at line 107 of file nodeMergejoin.c.

◆ EXEC_MJ_JOINTUPLES

#define EXEC_MJ_JOINTUPLES   3

Definition at line 109 of file nodeMergejoin.c.

◆ EXEC_MJ_NEXTINNER

#define EXEC_MJ_NEXTINNER   6

Definition at line 112 of file nodeMergejoin.c.

◆ EXEC_MJ_NEXTOUTER

#define EXEC_MJ_NEXTOUTER   4

Definition at line 110 of file nodeMergejoin.c.

◆ EXEC_MJ_SKIP_TEST

#define EXEC_MJ_SKIP_TEST   7

Definition at line 113 of file nodeMergejoin.c.

◆ EXEC_MJ_SKIPINNER_ADVANCE

#define EXEC_MJ_SKIPINNER_ADVANCE   9

Definition at line 115 of file nodeMergejoin.c.

◆ EXEC_MJ_SKIPOUTER_ADVANCE

#define EXEC_MJ_SKIPOUTER_ADVANCE   8

Definition at line 114 of file nodeMergejoin.c.

◆ EXEC_MJ_TESTOUTER

#define EXEC_MJ_TESTOUTER   5

Definition at line 111 of file nodeMergejoin.c.

◆ MarkInnerTuple

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

Definition at line 153 of file nodeMergejoin.c.

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

Typedef Documentation

◆ MergeJoinClauseData

Enumeration Type Documentation

◆ MJEvalResult

Enumerator
MJEVAL_MATCHABLE 
MJEVAL_NONMATCHABLE 
MJEVAL_ENDOFJOIN 

Definition at line 145 of file nodeMergejoin.c.

146{
147 MJEVAL_MATCHABLE, /* normal, potentially matchable tuple */
148 MJEVAL_NONMATCHABLE, /* tuple cannot join because it has a null */
149 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 516 of file nodeMergejoin.c.

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

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

Referenced by ExecInitMergeJoin().

◆ ExecEndMergeJoin()

void ExecEndMergeJoin ( MergeJoinState node)

Definition at line 1638 of file nodeMergejoin.c.

1639{
1640 MJ1_printf("ExecEndMergeJoin: %s\n",
1641 "ending node processing");
1642
1643 /*
1644 * shut down the subplans
1645 */
1648
1649 MJ1_printf("ExecEndMergeJoin: %s\n",
1650 "node processing ended");
1651}

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

Referenced by ExecEndNode().

◆ ExecInitMergeJoin()

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

Definition at line 1441 of file nodeMergejoin.c.

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

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 596 of file nodeMergejoin.c.

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

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 1654 of file nodeMergejoin.c.

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

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 388 of file nodeMergejoin.c.

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

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 338 of file nodeMergejoin.c.

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

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 291 of file nodeMergejoin.c.

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

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 177 of file nodeMergejoin.c.

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

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 480 of file nodeMergejoin.c.

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

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 449 of file nodeMergejoin.c.

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

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().