PostgreSQL Source Code  git master
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 "utils/memutils.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, int *mergestrategies, 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 116 of file nodeMergejoin.c.

◆ EXEC_MJ_ENDOUTER

#define EXEC_MJ_ENDOUTER   10

Definition at line 115 of file nodeMergejoin.c.

◆ EXEC_MJ_INITIALIZE_INNER

#define EXEC_MJ_INITIALIZE_INNER   2

Definition at line 107 of file nodeMergejoin.c.

◆ EXEC_MJ_INITIALIZE_OUTER

#define EXEC_MJ_INITIALIZE_OUTER   1

Definition at line 106 of file nodeMergejoin.c.

◆ EXEC_MJ_JOINTUPLES

#define EXEC_MJ_JOINTUPLES   3

Definition at line 108 of file nodeMergejoin.c.

◆ EXEC_MJ_NEXTINNER

#define EXEC_MJ_NEXTINNER   6

Definition at line 111 of file nodeMergejoin.c.

◆ EXEC_MJ_NEXTOUTER

#define EXEC_MJ_NEXTOUTER   4

Definition at line 109 of file nodeMergejoin.c.

◆ EXEC_MJ_SKIP_TEST

#define EXEC_MJ_SKIP_TEST   7

Definition at line 112 of file nodeMergejoin.c.

◆ EXEC_MJ_SKIPINNER_ADVANCE

#define EXEC_MJ_SKIPINNER_ADVANCE   9

Definition at line 114 of file nodeMergejoin.c.

◆ EXEC_MJ_SKIPOUTER_ADVANCE

#define EXEC_MJ_SKIPOUTER_ADVANCE   8

Definition at line 113 of file nodeMergejoin.c.

◆ EXEC_MJ_TESTOUTER

#define EXEC_MJ_TESTOUTER   5

Definition at line 110 of file nodeMergejoin.c.

◆ MarkInnerTuple

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

Definition at line 152 of file nodeMergejoin.c.

Typedef Documentation

◆ MergeJoinClauseData

Enumeration Type Documentation

◆ MJEvalResult

Enumerator
MJEVAL_MATCHABLE 
MJEVAL_NONMATCHABLE 
MJEVAL_ENDOFJOIN 

Definition at line 144 of file nodeMergejoin.c.

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

Function Documentation

◆ check_constant_qual()

static bool check_constant_qual ( List qual,
bool is_const_false 
)
static

Definition at line 520 of file nodeMergejoin.c.

521 {
522  ListCell *lc;
523 
524  foreach(lc, qual)
525  {
526  Const *con = (Const *) lfirst(lc);
527 
528  if (!con || !IsA(con, Const))
529  return false;
530  if (con->constisnull || !DatumGetBool(con->constvalue))
531  *is_const_false = true;
532  }
533  return true;
534 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
#define lfirst(lc)
Definition: pg_list.h:172
static bool DatumGetBool(Datum X)
Definition: postgres.h:90

References DatumGetBool(), IsA, and lfirst.

Referenced by ExecInitMergeJoin().

◆ ExecEndMergeJoin()

void ExecEndMergeJoin ( MergeJoinState node)

Definition at line 1641 of file nodeMergejoin.c.

1642 {
1643  MJ1_printf("ExecEndMergeJoin: %s\n",
1644  "ending node processing");
1645 
1646  /*
1647  * shut down the subplans
1648  */
1649  ExecEndNode(innerPlanState(node));
1650  ExecEndNode(outerPlanState(node));
1651 
1652  MJ1_printf("ExecEndMergeJoin: %s\n",
1653  "node processing ended");
1654 }
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:557
#define MJ1_printf(s, p)
Definition: execdebug.h:121
#define outerPlanState(node)
Definition: execnodes.h:1139
#define innerPlanState(node)
Definition: execnodes.h:1138

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

Referenced by ExecEndNode().

◆ ExecInitMergeJoin()

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

Definition at line 1444 of file nodeMergejoin.c.

1445 {
1446  MergeJoinState *mergestate;
1447  TupleDesc outerDesc,
1448  innerDesc;
1449  const TupleTableSlotOps *innerOps;
1450 
1451  /* check for unsupported flags */
1452  Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
1453 
1454  MJ1_printf("ExecInitMergeJoin: %s\n",
1455  "initializing node");
1456 
1457  /*
1458  * create state structure
1459  */
1460  mergestate = makeNode(MergeJoinState);
1461  mergestate->js.ps.plan = (Plan *) node;
1462  mergestate->js.ps.state = estate;
1463  mergestate->js.ps.ExecProcNode = ExecMergeJoin;
1464  mergestate->js.jointype = node->join.jointype;
1465  mergestate->mj_ConstFalseJoin = false;
1466 
1467  /*
1468  * Miscellaneous initialization
1469  *
1470  * create expression context for node
1471  */
1472  ExecAssignExprContext(estate, &mergestate->js.ps);
1473 
1474  /*
1475  * we need two additional econtexts in which we can compute the join
1476  * expressions from the left and right input tuples. The node's regular
1477  * econtext won't do because it gets reset too often.
1478  */
1479  mergestate->mj_OuterEContext = CreateExprContext(estate);
1480  mergestate->mj_InnerEContext = CreateExprContext(estate);
1481 
1482  /*
1483  * initialize child nodes
1484  *
1485  * inner child must support MARK/RESTORE, unless we have detected that we
1486  * don't need that. Note that skip_mark_restore must never be set if
1487  * there are non-mergeclause joinquals, since the logic wouldn't work.
1488  */
1489  Assert(node->join.joinqual == NIL || !node->skip_mark_restore);
1490  mergestate->mj_SkipMarkRestore = node->skip_mark_restore;
1491 
1492  outerPlanState(mergestate) = ExecInitNode(outerPlan(node), estate, eflags);
1493  outerDesc = ExecGetResultType(outerPlanState(mergestate));
1494  innerPlanState(mergestate) = ExecInitNode(innerPlan(node), estate,
1495  mergestate->mj_SkipMarkRestore ?
1496  eflags :
1497  (eflags | EXEC_FLAG_MARK));
1498  innerDesc = ExecGetResultType(innerPlanState(mergestate));
1499 
1500  /*
1501  * For certain types of inner child nodes, it is advantageous to issue
1502  * MARK every time we advance past an inner tuple we will never return to.
1503  * For other types, MARK on a tuple we cannot return to is a waste of
1504  * cycles. Detect which case applies and set mj_ExtraMarks if we want to
1505  * issue "unnecessary" MARK calls.
1506  *
1507  * Currently, only Material wants the extra MARKs, and it will be helpful
1508  * only if eflags doesn't specify REWIND.
1509  *
1510  * Note that for IndexScan and IndexOnlyScan, it is *necessary* that we
1511  * not set mj_ExtraMarks; otherwise we might attempt to set a mark before
1512  * the first inner tuple, which they do not support.
1513  */
1514  if (IsA(innerPlan(node), Material) &&
1515  (eflags & EXEC_FLAG_REWIND) == 0 &&
1516  !mergestate->mj_SkipMarkRestore)
1517  mergestate->mj_ExtraMarks = true;
1518  else
1519  mergestate->mj_ExtraMarks = false;
1520 
1521  /*
1522  * Initialize result slot, type and projection.
1523  */
1524  ExecInitResultTupleSlotTL(&mergestate->js.ps, &TTSOpsVirtual);
1525  ExecAssignProjectionInfo(&mergestate->js.ps, NULL);
1526 
1527  /*
1528  * tuple table initialization
1529  */
1530  innerOps = ExecGetResultSlotOps(innerPlanState(mergestate), NULL);
1531  mergestate->mj_MarkedTupleSlot = ExecInitExtraTupleSlot(estate, innerDesc,
1532  innerOps);
1533 
1534  /*
1535  * initialize child expressions
1536  */
1537  mergestate->js.ps.qual =
1538  ExecInitQual(node->join.plan.qual, (PlanState *) mergestate);
1539  mergestate->js.joinqual =
1540  ExecInitQual(node->join.joinqual, (PlanState *) mergestate);
1541  /* mergeclauses are handled below */
1542 
1543  /*
1544  * detect whether we need only consider the first matching inner tuple
1545  */
1546  mergestate->js.single_match = (node->join.inner_unique ||
1547  node->join.jointype == JOIN_SEMI);
1548 
1549  /* set up null tuples for outer joins, if needed */
1550  switch (node->join.jointype)
1551  {
1552  case JOIN_INNER:
1553  case JOIN_SEMI:
1554  mergestate->mj_FillOuter = false;
1555  mergestate->mj_FillInner = false;
1556  break;
1557  case JOIN_LEFT:
1558  case JOIN_ANTI:
1559  mergestate->mj_FillOuter = true;
1560  mergestate->mj_FillInner = false;
1561  mergestate->mj_NullInnerTupleSlot =
1562  ExecInitNullTupleSlot(estate, innerDesc, &TTSOpsVirtual);
1563  break;
1564  case JOIN_RIGHT:
1565  case JOIN_RIGHT_ANTI:
1566  mergestate->mj_FillOuter = false;
1567  mergestate->mj_FillInner = true;
1568  mergestate->mj_NullOuterTupleSlot =
1569  ExecInitNullTupleSlot(estate, outerDesc, &TTSOpsVirtual);
1570 
1571  /*
1572  * Can't handle right, right-anti or full join with non-constant
1573  * extra joinclauses. This should have been caught by planner.
1574  */
1575  if (!check_constant_qual(node->join.joinqual,
1576  &mergestate->mj_ConstFalseJoin))
1577  ereport(ERROR,
1578  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1579  errmsg("RIGHT JOIN is only supported with merge-joinable join conditions")));
1580  break;
1581  case JOIN_FULL:
1582  mergestate->mj_FillOuter = true;
1583  mergestate->mj_FillInner = true;
1584  mergestate->mj_NullOuterTupleSlot =
1585  ExecInitNullTupleSlot(estate, outerDesc, &TTSOpsVirtual);
1586  mergestate->mj_NullInnerTupleSlot =
1587  ExecInitNullTupleSlot(estate, innerDesc, &TTSOpsVirtual);
1588 
1589  /*
1590  * Can't handle right, right-anti or full join with non-constant
1591  * extra joinclauses. This should have been caught by planner.
1592  */
1593  if (!check_constant_qual(node->join.joinqual,
1594  &mergestate->mj_ConstFalseJoin))
1595  ereport(ERROR,
1596  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1597  errmsg("FULL JOIN is only supported with merge-joinable join conditions")));
1598  break;
1599  default:
1600  elog(ERROR, "unrecognized join type: %d",
1601  (int) node->join.jointype);
1602  }
1603 
1604  /*
1605  * preprocess the merge clauses
1606  */
1607  mergestate->mj_NumClauses = list_length(node->mergeclauses);
1608  mergestate->mj_Clauses = MJExamineQuals(node->mergeclauses,
1609  node->mergeFamilies,
1610  node->mergeCollations,
1611  node->mergeStrategies,
1612  node->mergeNullsFirst,
1613  (PlanState *) mergestate);
1614 
1615  /*
1616  * initialize join state
1617  */
1618  mergestate->mj_JoinState = EXEC_MJ_INITIALIZE_OUTER;
1619  mergestate->mj_MatchedOuter = false;
1620  mergestate->mj_MatchedInner = false;
1621  mergestate->mj_OuterTupleSlot = NULL;
1622  mergestate->mj_InnerTupleSlot = NULL;
1623 
1624  /*
1625  * initialization successful
1626  */
1627  MJ1_printf("ExecInitMergeJoin: %s\n",
1628  "node initialized");
1629 
1630  return mergestate;
1631 }
int errcode(int sqlerrcode)
Definition: elog.c:860
int errmsg(const char *fmt,...)
Definition: elog.c:1073
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
#define ereport(elevel,...)
Definition: elog.h:149
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition: execExpr.c:214
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:142
const TupleTableSlotOps TTSOpsVirtual
Definition: execTuples.c:83
TupleTableSlot * ExecInitNullTupleSlot(EState *estate, TupleDesc tupType, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1846
TupleTableSlot * ExecInitExtraTupleSlot(EState *estate, TupleDesc tupledesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1830
void ExecInitResultTupleSlotTL(PlanState *planstate, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1798
TupleDesc ExecGetResultType(PlanState *planstate)
Definition: execUtils.c:498
const TupleTableSlotOps * ExecGetResultSlotOps(PlanState *planstate, bool *isfixed)
Definition: execUtils.c:507
ExprContext * CreateExprContext(EState *estate)
Definition: execUtils.c:309
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:488
void ExecAssignProjectionInfo(PlanState *planstate, TupleDesc inputDesc)
Definition: execUtils.c:543
#define EXEC_FLAG_BACKWARD
Definition: executor.h:68
#define EXEC_FLAG_REWIND
Definition: executor.h:67
#define EXEC_FLAG_MARK
Definition: executor.h:69
Assert(fmt[strlen(fmt) - 1] !='\n')
static MergeJoinClause MJExamineQuals(List *mergeclauses, Oid *mergefamilies, Oid *mergecollations, int *mergestrategies, bool *mergenullsfirst, PlanState *parent)
#define EXEC_MJ_INITIALIZE_OUTER
static TupleTableSlot * ExecMergeJoin(PlanState *pstate)
static bool check_constant_qual(List *qual, bool *is_const_false)
#define makeNode(_type_)
Definition: nodes.h:155
@ JOIN_SEMI
Definition: nodes.h:297
@ JOIN_FULL
Definition: nodes.h:285
@ JOIN_INNER
Definition: nodes.h:283
@ JOIN_RIGHT
Definition: nodes.h:286
@ JOIN_LEFT
Definition: nodes.h:284
@ JOIN_RIGHT_ANTI
Definition: nodes.h:299
@ JOIN_ANTI
Definition: nodes.h:298
static int list_length(const List *l)
Definition: pg_list.h:152
#define NIL
Definition: pg_list.h:68
#define innerPlan(node)
Definition: plannodes.h:181
#define outerPlan(node)
Definition: plannodes.h:182
JoinType jointype
Definition: execnodes.h:2009
PlanState ps
Definition: execnodes.h:2008
ExprState * joinqual
Definition: execnodes.h:2012
bool single_match
Definition: execnodes.h:2010
List * joinqual
Definition: plannodes.h:791
JoinType jointype
Definition: plannodes.h:789
bool inner_unique
Definition: plannodes.h:790
bool mj_MatchedOuter
Definition: execnodes.h:2067
bool mj_SkipMarkRestore
Definition: execnodes.h:2062
bool mj_ConstFalseJoin
Definition: execnodes.h:2064
TupleTableSlot * mj_MarkedTupleSlot
Definition: execnodes.h:2071
TupleTableSlot * mj_NullInnerTupleSlot
Definition: execnodes.h:2073
ExprContext * mj_InnerEContext
Definition: execnodes.h:2075
TupleTableSlot * mj_NullOuterTupleSlot
Definition: execnodes.h:2072
bool mj_ExtraMarks
Definition: execnodes.h:2063
MergeJoinClause mj_Clauses
Definition: execnodes.h:2060
bool mj_MatchedInner
Definition: execnodes.h:2068
TupleTableSlot * mj_InnerTupleSlot
Definition: execnodes.h:2070
ExprContext * mj_OuterEContext
Definition: execnodes.h:2074
JoinState js
Definition: execnodes.h:2058
TupleTableSlot * mj_OuterTupleSlot
Definition: execnodes.h:2069
List * mergeclauses
Definition: plannodes.h:839
Join join
Definition: plannodes.h:833
bool skip_mark_restore
Definition: plannodes.h:836
ExprState * qual
Definition: execnodes.h:1064
Plan * plan
Definition: execnodes.h:1043
EState * state
Definition: execnodes.h:1045
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:1049

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(), PlanState::ExecProcNode, Join::inner_unique, innerPlan, innerPlanState, IsA, MergeJoin::join, JOIN_ANTI, JOIN_FULL, JOIN_INNER, JOIN_LEFT, JOIN_RIGHT, JOIN_RIGHT_ANTI, JOIN_SEMI, JoinState::joinqual, Join::joinqual, JoinState::jointype, Join::jointype, MergeJoinState::js, list_length(), makeNode, MergeJoin::mergeclauses, MJ1_printf, MergeJoinState::mj_Clauses, MergeJoinState::mj_ConstFalseJoin, MergeJoinState::mj_ExtraMarks, MergeJoinState::mj_FillInner, MergeJoinState::mj_FillOuter, MergeJoinState::mj_InnerEContext, MergeJoinState::mj_InnerTupleSlot, MergeJoinState::mj_JoinState, MergeJoinState::mj_MarkedTupleSlot, MergeJoinState::mj_MatchedInner, MergeJoinState::mj_MatchedOuter, MergeJoinState::mj_NullInnerTupleSlot, MergeJoinState::mj_NullOuterTupleSlot, MergeJoinState::mj_NumClauses, MergeJoinState::mj_OuterEContext, MergeJoinState::mj_OuterTupleSlot, MergeJoinState::mj_SkipMarkRestore, MJExamineQuals(), NIL, outerPlan, outerPlanState, PlanState::plan, JoinState::ps, PlanState::qual, JoinState::single_match, MergeJoin::skip_mark_restore, PlanState::state, and TTSOpsVirtual.

Referenced by ExecInitNode().

◆ ExecMergeJoin()

static TupleTableSlot* ExecMergeJoin ( PlanState pstate)
static

Definition at line 600 of file nodeMergejoin.c.

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

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(), 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 1657 of file nodeMergejoin.c.

1658 {
1661 
1663 
1665  node->mj_MatchedOuter = false;
1666  node->mj_MatchedInner = false;
1667  node->mj_OuterTupleSlot = NULL;
1668  node->mj_InnerTupleSlot = NULL;
1669 
1670  /*
1671  * if chgParam of subnodes is not null then plans will be re-scanned by
1672  * first ExecProcNode.
1673  */
1674  if (outerPlan->chgParam == NULL)
1676  if (innerPlan->chgParam == NULL)
1678 }
void ExecReScan(PlanState *node)
Definition: execAmi.c:78
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:433

References EXEC_MJ_INITIALIZE_OUTER, ExecClearTuple(), ExecReScan(), 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 392 of file nodeMergejoin.c.

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

References ApplySortComparator(), ExprContext::ecxt_per_tuple_memory, i, MergeJoinState::js, MergeJoinClauseData::ldatum, MergeJoinClauseData::lisnull, MemoryContextSwitchTo(), MergeJoinState::mj_Clauses, MergeJoinState::mj_ConstFalseJoin, MergeJoinState::mj_NumClauses, JoinState::ps, PlanState::ps_ExprContext, MergeJoinClauseData::rdatum, ResetExprContext, MergeJoinClauseData::risnull, and MergeJoinClauseData::ssup.

Referenced by ExecMergeJoin().

◆ MJEvalInnerValues()

static MJEvalResult MJEvalInnerValues ( MergeJoinState mergestate,
TupleTableSlot innerslot 
)
static

Definition at line 342 of file nodeMergejoin.c.

343 {
344  ExprContext *econtext = mergestate->mj_InnerEContext;
346  int i;
347  MemoryContext oldContext;
348 
349  /* Check for end of inner subplan */
350  if (TupIsNull(innerslot))
351  return MJEVAL_ENDOFJOIN;
352 
353  ResetExprContext(econtext);
354 
355  oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
356 
357  econtext->ecxt_innertuple = innerslot;
358 
359  for (i = 0; i < mergestate->mj_NumClauses; i++)
360  {
361  MergeJoinClause clause = &mergestate->mj_Clauses[i];
362 
363  clause->rdatum = ExecEvalExpr(clause->rexpr, econtext,
364  &clause->risnull);
365  if (clause->risnull)
366  {
367  /* match is impossible; can we end the join early? */
368  if (i == 0 && !clause->ssup.ssup_nulls_first &&
369  !mergestate->mj_FillInner)
370  result = MJEVAL_ENDOFJOIN;
371  else if (result == MJEVAL_MATCHABLE)
372  result = MJEVAL_NONMATCHABLE;
373  }
374  }
375 
376  MemoryContextSwitchTo(oldContext);
377 
378  return result;
379 }
static Datum ExecEvalExpr(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:333
bool ssup_nulls_first
Definition: sortsupport.h:75

References ExprContext::ecxt_innertuple, ExprContext::ecxt_per_tuple_memory, ExecEvalExpr(), i, MemoryContextSwitchTo(), MergeJoinState::mj_Clauses, MergeJoinState::mj_FillInner, MergeJoinState::mj_InnerEContext, MergeJoinState::mj_NumClauses, 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 295 of file nodeMergejoin.c.

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

References ExprContext::ecxt_outertuple, ExprContext::ecxt_per_tuple_memory, ExecEvalExpr(), i, MergeJoinClauseData::ldatum, MergeJoinClauseData::lexpr, MergeJoinClauseData::lisnull, MemoryContextSwitchTo(), MergeJoinState::mj_Clauses, MergeJoinState::mj_FillOuter, MergeJoinState::mj_NumClauses, MergeJoinState::mj_OuterEContext, MergeJoinState::mj_OuterTupleSlot, 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,
int *  mergestrategies,
bool mergenullsfirst,
PlanState parent 
)
static

Definition at line 176 of file nodeMergejoin.c.

182 {
183  MergeJoinClause clauses;
184  int nClauses = list_length(mergeclauses);
185  int iClause;
186  ListCell *cl;
187 
188  clauses = (MergeJoinClause) palloc0(nClauses * sizeof(MergeJoinClauseData));
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];
197  StrategyNumber opstrategy = mergestrategies[iClause];
198  bool nulls_first = mergenullsfirst[iClause];
199  int op_strategy;
200  Oid op_lefttype;
201  Oid op_righttype;
202  Oid sortfunc;
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  if (opstrategy == BTLessStrategyNumber)
217  clause->ssup.ssup_reverse = false;
218  else if (opstrategy == BTGreaterStrategyNumber)
219  clause->ssup.ssup_reverse = true;
220  else /* planner screwed up */
221  elog(ERROR, "unsupported mergejoin strategy %d", opstrategy);
222  clause->ssup.ssup_nulls_first = nulls_first;
223 
224  /* Extract the operator's declared left/right datatypes */
225  get_op_opfamily_properties(qual->opno, opfamily, false,
226  &op_strategy,
227  &op_lefttype,
228  &op_righttype);
229  if (op_strategy != BTEqualStrategyNumber) /* should not happen */
230  elog(ERROR, "cannot merge using non-equality operator %u",
231  qual->opno);
232 
233  /*
234  * sortsupport routine must know if abbreviation optimization is
235  * applicable in principle. It is never applicable for merge joins
236  * because there is no convenient opportunity to convert to
237  * alternative representation.
238  */
239  clause->ssup.abbreviate = false;
240 
241  /* And get the matching support or comparison function */
242  Assert(clause->ssup.comparator == NULL);
243  sortfunc = get_opfamily_proc(opfamily,
244  op_lefttype,
245  op_righttype,
247  if (OidIsValid(sortfunc))
248  {
249  /* The sort support function can provide a comparator */
250  OidFunctionCall1(sortfunc, PointerGetDatum(&clause->ssup));
251  }
252  if (clause->ssup.comparator == NULL)
253  {
254  /* support not available, get comparison func */
255  sortfunc = get_opfamily_proc(opfamily,
256  op_lefttype,
257  op_righttype,
258  BTORDER_PROC);
259  if (!OidIsValid(sortfunc)) /* should not happen */
260  elog(ERROR, "missing support function %d(%u,%u) in opfamily %u",
261  BTORDER_PROC, op_lefttype, op_righttype, opfamily);
262  /* We'll use a shim to call the old-style btree comparator */
263  PrepareSortSupportComparisonShim(sortfunc, &clause->ssup);
264  }
265 
266  iClause++;
267  }
268 
269  return clauses;
270 }
#define OidIsValid(objectId)
Definition: c.h:762
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition: execExpr.c:128
struct MergeJoinClauseData * MergeJoinClause
Definition: execnodes.h:2054
#define OidFunctionCall1(functionId, arg1)
Definition: fmgr.h:680
void get_op_opfamily_properties(Oid opno, Oid opfamily, bool ordering_op, int *strategy, Oid *lefttype, Oid *righttype)
Definition: lsyscache.c:135
Oid get_opfamily_proc(Oid opfamily, Oid lefttype, Oid righttype, int16 procnum)
Definition: lsyscache.c:795
void * palloc0(Size size)
Definition: mcxt.c:1231
MemoryContext CurrentMemoryContext
Definition: mcxt.c:135
#define BTORDER_PROC
Definition: nbtree.h:707
#define BTSORTSUPPORT_PROC
Definition: nbtree.h:708
#define linitial(l)
Definition: pg_list.h:178
#define lsecond(l)
Definition: pg_list.h:183
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
unsigned int Oid
Definition: postgres_ext.h:31
void PrepareSortSupportComparisonShim(Oid cmpFunc, SortSupport ssup)
Definition: sortsupport.c:69
uint16 StrategyNumber
Definition: stratnum.h:22
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
#define BTLessStrategyNumber
Definition: stratnum.h:29
#define BTEqualStrategyNumber
Definition: stratnum.h:31
Oid opno
Definition: primnodes.h:753
List * args
Definition: primnodes.h:771
int(* comparator)(Datum x, Datum y, SortSupport ssup)
Definition: sortsupport.h:106
MemoryContext ssup_cxt
Definition: sortsupport.h:66

References SortSupportData::abbreviate, OpExpr::args, Assert(), BTEqualStrategyNumber, BTGreaterStrategyNumber, BTLessStrategyNumber, BTORDER_PROC, BTSORTSUPPORT_PROC, SortSupportData::comparator, CurrentMemoryContext, elog, ERROR, ExecInitExpr(), get_op_opfamily_properties(), get_opfamily_proc(), 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 484 of file nodeMergejoin.c.

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

References ExprContext::ecxt_innertuple, ExprContext::ecxt_outertuple, ExecProject(), ExecQual(), 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 453 of file nodeMergejoin.c.

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

References ExprContext::ecxt_innertuple, ExprContext::ecxt_outertuple, ExecProject(), ExecQual(), 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().