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.

Referenced by ExecMergeJoin().

◆ EXEC_MJ_ENDOUTER

#define EXEC_MJ_ENDOUTER   10

Definition at line 115 of file nodeMergejoin.c.

Referenced by ExecMergeJoin().

◆ EXEC_MJ_INITIALIZE_INNER

#define EXEC_MJ_INITIALIZE_INNER   2

Definition at line 107 of file nodeMergejoin.c.

Referenced by ExecMergeJoin().

◆ EXEC_MJ_INITIALIZE_OUTER

#define EXEC_MJ_INITIALIZE_OUTER   1

Definition at line 106 of file nodeMergejoin.c.

Referenced by ExecInitMergeJoin(), ExecMergeJoin(), and ExecReScanMergeJoin().

◆ EXEC_MJ_JOINTUPLES

#define EXEC_MJ_JOINTUPLES   3

Definition at line 108 of file nodeMergejoin.c.

Referenced by ExecMergeJoin().

◆ EXEC_MJ_NEXTINNER

#define EXEC_MJ_NEXTINNER   6

Definition at line 111 of file nodeMergejoin.c.

Referenced by ExecMergeJoin().

◆ EXEC_MJ_NEXTOUTER

#define EXEC_MJ_NEXTOUTER   4

Definition at line 109 of file nodeMergejoin.c.

Referenced by ExecMergeJoin().

◆ EXEC_MJ_SKIP_TEST

#define EXEC_MJ_SKIP_TEST   7

Definition at line 112 of file nodeMergejoin.c.

Referenced by ExecMergeJoin().

◆ EXEC_MJ_SKIPINNER_ADVANCE

#define EXEC_MJ_SKIPINNER_ADVANCE   9

Definition at line 114 of file nodeMergejoin.c.

Referenced by ExecMergeJoin().

◆ EXEC_MJ_SKIPOUTER_ADVANCE

#define EXEC_MJ_SKIPOUTER_ADVANCE   8

Definition at line 113 of file nodeMergejoin.c.

Referenced by ExecMergeJoin().

◆ EXEC_MJ_TESTOUTER

#define EXEC_MJ_TESTOUTER   5

Definition at line 110 of file nodeMergejoin.c.

Referenced by ExecMergeJoin().

◆ MarkInnerTuple

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

Definition at line 152 of file nodeMergejoin.c.

Referenced by ExecMergeJoin().

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

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.

References Const::constisnull, Const::constvalue, DatumGetBool, IsA, lfirst, MJ_debugtup, MergeJoinState::mj_InnerTupleSlot, MergeJoinState::mj_MarkedTupleSlot, MergeJoinState::mj_OuterTupleSlot, and TupIsNull.

Referenced by ExecInitMergeJoin().

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 }
Datum constvalue
Definition: primnodes.h:196
#define IsA(nodeptr, _type_)
Definition: nodes.h:561
#define DatumGetBool(X)
Definition: postgres.h:399
#define lfirst(lc)
Definition: pg_list.h:106
bool constisnull
Definition: primnodes.h:197

◆ ExecEndMergeJoin()

void ExecEndMergeJoin ( MergeJoinState node)

Definition at line 1629 of file nodeMergejoin.c.

References ExecClearTuple(), ExecEndNode(), ExecFreeExprContext(), innerPlanState, MergeJoinState::js, MJ1_printf, MergeJoinState::mj_MarkedTupleSlot, outerPlanState, JoinState::ps, and PlanState::ps_ResultTupleSlot.

Referenced by ExecEndNode().

1630 {
1631  MJ1_printf("ExecEndMergeJoin: %s\n",
1632  "ending node processing");
1633 
1634  /*
1635  * Free the exprcontext
1636  */
1637  ExecFreeExprContext(&node->js.ps);
1638 
1639  /*
1640  * clean out the tuple table
1641  */
1644 
1645  /*
1646  * shut down the subplans
1647  */
1648  ExecEndNode(innerPlanState(node));
1649  ExecEndNode(outerPlanState(node));
1650 
1651  MJ1_printf("ExecEndMergeJoin: %s\n",
1652  "node processing ended");
1653 }
#define MJ1_printf(s, p)
Definition: execdebug.h:119
PlanState ps
Definition: execnodes.h:1600
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:523
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
TupleTableSlot * mj_MarkedTupleSlot
Definition: execnodes.h:1663
void ExecFreeExprContext(PlanState *planstate)
Definition: execUtils.c:523
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:882
#define outerPlanState(node)
Definition: execnodes.h:895
JoinState js
Definition: execnodes.h:1650
#define innerPlanState(node)
Definition: execnodes.h:894

◆ ExecInitMergeJoin()

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

Definition at line 1436 of file nodeMergejoin.c.

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(), ExecAssignResultTypeFromTL(), ExecGetResultType(), ExecInitExtraTupleSlot(), ExecInitNode(), ExecInitNullTupleSlot(), ExecInitQual(), ExecInitResultTupleSlot(), ExecMergeJoin(), PlanState::ExecProcNode, ExecSetSlotDescriptor(), Join::inner_unique, innerPlan, innerPlanState, IsA, MergeJoin::join, JOIN_ANTI, JOIN_FULL, JOIN_INNER, JOIN_LEFT, JOIN_RIGHT, JOIN_SEMI, Join::joinqual, Join::jointype, MergeJoinState::js, list_length(), makeNode, MergeJoin::mergeclauses, MergeJoin::mergeCollations, MergeJoin::mergeFamilies, MergeJoin::mergeNullsFirst, MergeJoin::mergeStrategies, MJ1_printf, MergeJoinState::mj_InnerEContext, MergeJoinState::mj_OuterEContext, MJExamineQuals(), NIL, outerPlan, outerPlanState, Join::plan, PlanState::plan, JoinState::ps, Plan::qual, PlanState::qual, MergeJoin::skip_mark_restore, and PlanState::state.

Referenced by ExecInitNode().

1437 {
1438  MergeJoinState *mergestate;
1439 
1440  /* check for unsupported flags */
1441  Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
1442 
1443  MJ1_printf("ExecInitMergeJoin: %s\n",
1444  "initializing node");
1445 
1446  /*
1447  * create state structure
1448  */
1449  mergestate = makeNode(MergeJoinState);
1450  mergestate->js.ps.plan = (Plan *) node;
1451  mergestate->js.ps.state = estate;
1452  mergestate->js.ps.ExecProcNode = ExecMergeJoin;
1453 
1454  /*
1455  * Miscellaneous initialization
1456  *
1457  * create expression context for node
1458  */
1459  ExecAssignExprContext(estate, &mergestate->js.ps);
1460 
1461  /*
1462  * we need two additional econtexts in which we can compute the join
1463  * expressions from the left and right input tuples. The node's regular
1464  * econtext won't do because it gets reset too often.
1465  */
1466  mergestate->mj_OuterEContext = CreateExprContext(estate);
1467  mergestate->mj_InnerEContext = CreateExprContext(estate);
1468 
1469  /*
1470  * initialize child expressions
1471  */
1472  mergestate->js.ps.qual =
1473  ExecInitQual(node->join.plan.qual, (PlanState *) mergestate);
1474  mergestate->js.jointype = node->join.jointype;
1475  mergestate->js.joinqual =
1476  ExecInitQual(node->join.joinqual, (PlanState *) mergestate);
1477  mergestate->mj_ConstFalseJoin = false;
1478  /* mergeclauses are handled below */
1479 
1480  /*
1481  * initialize child nodes
1482  *
1483  * inner child must support MARK/RESTORE, unless we have detected that we
1484  * don't need that. Note that skip_mark_restore must never be set if
1485  * there are non-mergeclause joinquals, since the logic wouldn't work.
1486  */
1487  Assert(node->join.joinqual == NIL || !node->skip_mark_restore);
1488  mergestate->mj_SkipMarkRestore = node->skip_mark_restore;
1489 
1490  outerPlanState(mergestate) = ExecInitNode(outerPlan(node), estate, eflags);
1491  innerPlanState(mergestate) = ExecInitNode(innerPlan(node), estate,
1492  mergestate->mj_SkipMarkRestore ?
1493  eflags :
1494  (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  if (IsA(innerPlan(node), Material) &&
1507  (eflags & EXEC_FLAG_REWIND) == 0 &&
1508  !mergestate->mj_SkipMarkRestore)
1509  mergestate->mj_ExtraMarks = true;
1510  else
1511  mergestate->mj_ExtraMarks = false;
1512 
1513  /*
1514  * tuple table initialization
1515  */
1516  ExecInitResultTupleSlot(estate, &mergestate->js.ps);
1517 
1518  mergestate->mj_MarkedTupleSlot = ExecInitExtraTupleSlot(estate);
1520  ExecGetResultType(innerPlanState(mergestate)));
1521 
1522  /*
1523  * detect whether we need only consider the first matching inner tuple
1524  */
1525  mergestate->js.single_match = (node->join.inner_unique ||
1526  node->join.jointype == JOIN_SEMI);
1527 
1528  /* set up null tuples for outer joins, if needed */
1529  switch (node->join.jointype)
1530  {
1531  case JOIN_INNER:
1532  case JOIN_SEMI:
1533  mergestate->mj_FillOuter = false;
1534  mergestate->mj_FillInner = false;
1535  break;
1536  case JOIN_LEFT:
1537  case JOIN_ANTI:
1538  mergestate->mj_FillOuter = true;
1539  mergestate->mj_FillInner = false;
1540  mergestate->mj_NullInnerTupleSlot =
1541  ExecInitNullTupleSlot(estate,
1542  ExecGetResultType(innerPlanState(mergestate)));
1543  break;
1544  case JOIN_RIGHT:
1545  mergestate->mj_FillOuter = false;
1546  mergestate->mj_FillInner = true;
1547  mergestate->mj_NullOuterTupleSlot =
1548  ExecInitNullTupleSlot(estate,
1549  ExecGetResultType(outerPlanState(mergestate)));
1550 
1551  /*
1552  * Can't handle right or full join with non-constant extra
1553  * joinclauses. This should have been caught by planner.
1554  */
1555  if (!check_constant_qual(node->join.joinqual,
1556  &mergestate->mj_ConstFalseJoin))
1557  ereport(ERROR,
1558  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1559  errmsg("RIGHT JOIN is only supported with merge-joinable join conditions")));
1560  break;
1561  case JOIN_FULL:
1562  mergestate->mj_FillOuter = true;
1563  mergestate->mj_FillInner = true;
1564  mergestate->mj_NullOuterTupleSlot =
1565  ExecInitNullTupleSlot(estate,
1566  ExecGetResultType(outerPlanState(mergestate)));
1567  mergestate->mj_NullInnerTupleSlot =
1568  ExecInitNullTupleSlot(estate,
1569  ExecGetResultType(innerPlanState(mergestate)));
1570 
1571  /*
1572  * Can't handle right or full join with non-constant extra
1573  * 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("FULL JOIN is only supported with merge-joinable join conditions")));
1580  break;
1581  default:
1582  elog(ERROR, "unrecognized join type: %d",
1583  (int) node->join.jointype);
1584  }
1585 
1586  /*
1587  * initialize tuple type and projection info
1588  */
1589  ExecAssignResultTypeFromTL(&mergestate->js.ps);
1590  ExecAssignProjectionInfo(&mergestate->js.ps, NULL);
1591 
1592  /*
1593  * preprocess the merge clauses
1594  */
1595  mergestate->mj_NumClauses = list_length(node->mergeclauses);
1596  mergestate->mj_Clauses = MJExamineQuals(node->mergeclauses,
1597  node->mergeFamilies,
1598  node->mergeCollations,
1599  node->mergeStrategies,
1600  node->mergeNullsFirst,
1601  (PlanState *) mergestate);
1602 
1603  /*
1604  * initialize join state
1605  */
1606  mergestate->mj_JoinState = EXEC_MJ_INITIALIZE_OUTER;
1607  mergestate->mj_MatchedOuter = false;
1608  mergestate->mj_MatchedInner = false;
1609  mergestate->mj_OuterTupleSlot = NULL;
1610  mergestate->mj_InnerTupleSlot = NULL;
1611 
1612  /*
1613  * initialization successful
1614  */
1615  MJ1_printf("ExecInitMergeJoin: %s\n",
1616  "node initialized");
1617 
1618  return mergestate;
1619 }
JoinType jointype
Definition: execnodes.h:1601
#define NIL
Definition: pg_list.h:69
List * qual
Definition: plannodes.h:145
#define IsA(nodeptr, _type_)
Definition: nodes.h:561
TupleTableSlot * ExecInitExtraTupleSlot(EState *estate)
Definition: execTuples.c:852
#define MJ1_printf(s, p)
Definition: execdebug.h:119
ExprState * joinqual
Definition: execnodes.h:1604
ExprContext * mj_InnerEContext
Definition: execnodes.h:1667
bool mj_ConstFalseJoin
Definition: execnodes.h:1656
PlanState ps
Definition: execnodes.h:1600
bool single_match
Definition: execnodes.h:1602
TupleTableSlot * mj_MarkedTupleSlot
Definition: execnodes.h:1663
TupleTableSlot * mj_NullInnerTupleSlot
Definition: execnodes.h:1665
int errcode(int sqlerrcode)
Definition: elog.c:575
EState * state
Definition: execnodes.h:851
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition: execExpr.c:160
void ExecAssignResultTypeFromTL(PlanState *planstate)
Definition: execUtils.c:447
List * mergeclauses
Definition: plannodes.h:711
JoinType jointype
Definition: plannodes.h:667
bool mj_MatchedOuter
Definition: execnodes.h:1659
TupleTableSlot * ExecInitNullTupleSlot(EState *estate, TupleDesc tupType)
Definition: execTuples.c:866
static MergeJoinClause MJExamineQuals(List *mergeclauses, Oid *mergefamilies, Oid *mergecollations, int *mergestrategies, bool *mergenullsfirst, PlanState *parent)
bool skip_mark_restore
Definition: plannodes.h:710
#define ERROR
Definition: elog.h:43
void ExecInitResultTupleSlot(EState *estate, PlanState *planstate)
Definition: execTuples.c:832
bool mj_MatchedInner
Definition: execnodes.h:1660
#define EXEC_FLAG_BACKWARD
Definition: executor.h:60
#define outerPlanState(node)
Definition: execnodes.h:895
#define innerPlan(node)
Definition: plannodes.h:173
void ExecAssignProjectionInfo(PlanState *planstate, TupleDesc inputDesc)
Definition: execUtils.c:494
MergeJoinClause mj_Clauses
Definition: execnodes.h:1652
#define EXEC_FLAG_REWIND
Definition: executor.h:59
#define ereport(elevel, rest)
Definition: elog.h:122
#define EXEC_MJ_INITIALIZE_OUTER
#define outerPlan(node)
Definition: plannodes.h:174
static bool check_constant_qual(List *qual, bool *is_const_false)
bool mj_SkipMarkRestore
Definition: execnodes.h:1654
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:855
void ExecSetSlotDescriptor(TupleTableSlot *slot, TupleDesc tupdesc)
Definition: execTuples.c:247
Oid * mergeFamilies
Definition: plannodes.h:713
Oid * mergeCollations
Definition: plannodes.h:714
Plan * plan
Definition: execnodes.h:849
JoinState js
Definition: execnodes.h:1650
bool * mergeNullsFirst
Definition: plannodes.h:716
#define makeNode(_type_)
Definition: nodes.h:558
int * mergeStrategies
Definition: plannodes.h:715
#define Assert(condition)
Definition: c.h:670
#define EXEC_FLAG_MARK
Definition: executor.h:61
TupleTableSlot * mj_InnerTupleSlot
Definition: execnodes.h:1662
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:425
ExprContext * mj_OuterEContext
Definition: execnodes.h:1666
static int list_length(const List *l)
Definition: pg_list.h:89
TupleTableSlot * mj_NullOuterTupleSlot
Definition: execnodes.h:1664
TupleDesc ExecGetResultType(PlanState *planstate)
Definition: execUtils.c:476
ExprState * qual
Definition: execnodes.h:867
static TupleTableSlot * ExecMergeJoin(PlanState *pstate)
int errmsg(const char *fmt,...)
Definition: elog.c:797
Join join
Definition: plannodes.h:709
ExprContext * CreateExprContext(EState *estate)
Definition: execUtils.c:225
#define elog
Definition: elog.h:219
bool inner_unique
Definition: plannodes.h:668
bool mj_ExtraMarks
Definition: execnodes.h:1655
#define innerPlanState(node)
Definition: execnodes.h:894
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:139
List * joinqual
Definition: plannodes.h:669
TupleTableSlot * mj_OuterTupleSlot
Definition: execnodes.h:1661
Plan plan
Definition: plannodes.h:666

◆ ExecMergeJoin()

static TupleTableSlot* ExecMergeJoin ( PlanState pstate)
static

Definition at line 600 of file nodeMergejoin.c.

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

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)
721  ExecMarkPos(innerPlan);
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  * If we only need to join to the first matching inner
810  * tuple, then consider returning this one, but after that
811  * continue with next outer tuple.
812  */
813  if (node->js.single_match)
815 
816  qualResult = (otherqual == NULL ||
817  ExecQual(otherqual, econtext));
818  MJ_DEBUG_QUAL(otherqual, qualResult);
819 
820  if (qualResult)
821  {
822  /*
823  * qualification succeeded. now form the desired
824  * projection tuple and return the slot containing it.
825  */
826  MJ_printf("ExecMergeJoin: returning tuple\n");
827 
828  return ExecProject(node->js.ps.ps_ProjInfo);
829  }
830  else
831  InstrCountFiltered2(node, 1);
832  }
833  else
834  InstrCountFiltered1(node, 1);
835  break;
836 
837  /*
838  * EXEC_MJ_NEXTINNER means advance the inner scan to the next
839  * tuple. If the tuple is not nil, we then proceed to test it
840  * against the join qualification.
841  *
842  * Before advancing, we check to see if we must emit an
843  * outer-join fill tuple for this inner tuple.
844  */
845  case EXEC_MJ_NEXTINNER:
846  MJ_printf("ExecMergeJoin: EXEC_MJ_NEXTINNER\n");
847 
848  if (doFillInner && !node->mj_MatchedInner)
849  {
850  /*
851  * Generate a fake join tuple with nulls for the outer
852  * tuple, and return it if it passes the non-join quals.
853  */
854  TupleTableSlot *result;
855 
856  node->mj_MatchedInner = true; /* do it only once */
857 
858  result = MJFillInner(node);
859  if (result)
860  return result;
861  }
862 
863  /*
864  * now we get the next inner tuple, if any. If there's none,
865  * advance to next outer tuple (which may be able to join to
866  * previously marked tuples).
867  *
868  * NB: must NOT do "extraMarks" here, since we may need to
869  * return to previously marked tuples.
870  */
871  innerTupleSlot = ExecProcNode(innerPlan);
872  node->mj_InnerTupleSlot = innerTupleSlot;
873  MJ_DEBUG_PROC_NODE(innerTupleSlot);
874  node->mj_MatchedInner = false;
875 
876  /* Compute join values and check for unmatchability */
877  switch (MJEvalInnerValues(node, innerTupleSlot))
878  {
879  case MJEVAL_MATCHABLE:
880 
881  /*
882  * Test the new inner tuple to see if it matches
883  * outer.
884  *
885  * If they do match, then we join them and move on to
886  * the next inner tuple (EXEC_MJ_JOINTUPLES).
887  *
888  * If they do not match then advance to next outer
889  * tuple.
890  */
891  compareResult = MJCompare(node);
892  MJ_DEBUG_COMPARE(compareResult);
893 
894  if (compareResult == 0)
896  else
897  {
898  Assert(compareResult < 0);
900  }
901  break;
902  case MJEVAL_NONMATCHABLE:
903 
904  /*
905  * It contains a NULL and hence can't match any outer
906  * tuple, so we can skip the comparison and assume the
907  * new tuple is greater than current outer.
908  */
910  break;
911  case MJEVAL_ENDOFJOIN:
912 
913  /*
914  * No more inner tuples. However, this might be only
915  * effective and not physical end of inner plan, so
916  * force mj_InnerTupleSlot to null to make sure we
917  * don't fetch more inner tuples. (We need this hack
918  * because we are not transiting to a state where the
919  * inner plan is assumed to be exhausted.)
920  */
921  node->mj_InnerTupleSlot = NULL;
923  break;
924  }
925  break;
926 
927  /*-------------------------------------------
928  * EXEC_MJ_NEXTOUTER means
929  *
930  * outer inner
931  * outer tuple - 5 5 - marked tuple
932  * 5 5
933  * 6 6 - inner tuple
934  * 7 7
935  *
936  * we know we just bumped into the
937  * first inner tuple > current outer tuple (or possibly
938  * the end of the inner stream)
939  * so get a new outer tuple and then
940  * proceed to test it against the marked tuple
941  * (EXEC_MJ_TESTOUTER)
942  *
943  * Before advancing, we check to see if we must emit an
944  * outer-join fill tuple for this outer tuple.
945  *------------------------------------------------
946  */
947  case EXEC_MJ_NEXTOUTER:
948  MJ_printf("ExecMergeJoin: EXEC_MJ_NEXTOUTER\n");
949 
950  if (doFillOuter && !node->mj_MatchedOuter)
951  {
952  /*
953  * Generate a fake join tuple with nulls for the inner
954  * tuple, and return it if it passes the non-join quals.
955  */
956  TupleTableSlot *result;
957 
958  node->mj_MatchedOuter = true; /* do it only once */
959 
960  result = MJFillOuter(node);
961  if (result)
962  return result;
963  }
964 
965  /*
966  * now we get the next outer tuple, if any
967  */
968  outerTupleSlot = ExecProcNode(outerPlan);
969  node->mj_OuterTupleSlot = outerTupleSlot;
970  MJ_DEBUG_PROC_NODE(outerTupleSlot);
971  node->mj_MatchedOuter = false;
972 
973  /* Compute join values and check for unmatchability */
974  switch (MJEvalOuterValues(node))
975  {
976  case MJEVAL_MATCHABLE:
977  /* Go test the new tuple against the marked tuple */
979  break;
980  case MJEVAL_NONMATCHABLE:
981  /* Can't match, so fetch next outer tuple */
983  break;
984  case MJEVAL_ENDOFJOIN:
985  /* No more outer tuples */
986  MJ_printf("ExecMergeJoin: end of outer subplan\n");
987  innerTupleSlot = node->mj_InnerTupleSlot;
988  if (doFillInner && !TupIsNull(innerTupleSlot))
989  {
990  /*
991  * Need to emit right-join tuples for remaining
992  * inner tuples.
993  */
995  break;
996  }
997  /* Otherwise we're done. */
998  return NULL;
999  }
1000  break;
1001 
1002  /*--------------------------------------------------------
1003  * EXEC_MJ_TESTOUTER If the new outer tuple and the marked
1004  * tuple satisfy the merge clause then we know we have
1005  * duplicates in the outer scan so we have to restore the
1006  * inner scan to the marked tuple and proceed to join the
1007  * new outer tuple with the inner tuples.
1008  *
1009  * This is the case when
1010  * outer inner
1011  * 4 5 - marked tuple
1012  * outer tuple - 5 5
1013  * new outer tuple - 5 5
1014  * 6 8 - inner tuple
1015  * 7 12
1016  *
1017  * new outer tuple == marked tuple
1018  *
1019  * If the outer tuple fails the test, then we are done
1020  * with the marked tuples, and we have to look for a
1021  * match to the current inner tuple. So we will
1022  * proceed to skip outer tuples until outer >= inner
1023  * (EXEC_MJ_SKIP_TEST).
1024  *
1025  * This is the case when
1026  *
1027  * outer inner
1028  * 5 5 - marked tuple
1029  * outer tuple - 5 5
1030  * new outer tuple - 6 8 - inner tuple
1031  * 7 12
1032  *
1033  * new outer tuple > marked tuple
1034  *
1035  *---------------------------------------------------------
1036  */
1037  case EXEC_MJ_TESTOUTER:
1038  MJ_printf("ExecMergeJoin: EXEC_MJ_TESTOUTER\n");
1039 
1040  /*
1041  * Here we must compare the outer tuple with the marked inner
1042  * tuple. (We can ignore the result of MJEvalInnerValues,
1043  * since the marked inner tuple is certainly matchable.)
1044  */
1045  innerTupleSlot = node->mj_MarkedTupleSlot;
1046  (void) MJEvalInnerValues(node, innerTupleSlot);
1047 
1048  compareResult = MJCompare(node);
1049  MJ_DEBUG_COMPARE(compareResult);
1050 
1051  if (compareResult == 0)
1052  {
1053  /*
1054  * the merge clause matched so now we restore the inner
1055  * scan position to the first mark, and go join that tuple
1056  * (and any following ones) to the new outer.
1057  *
1058  * If we were able to determine mark and restore are not
1059  * needed, then we don't have to back up; the current
1060  * inner is already the first possible match.
1061  *
1062  * NOTE: we do not need to worry about the MatchedInner
1063  * state for the rescanned inner tuples. We know all of
1064  * them will match this new outer tuple and therefore
1065  * won't be emitted as fill tuples. This works *only*
1066  * because we require the extra joinquals to be constant
1067  * when doing a right or full join --- otherwise some of
1068  * the rescanned tuples might fail the extra joinquals.
1069  * This obviously won't happen for a constant-true extra
1070  * joinqual, while the constant-false case is handled by
1071  * forcing the merge clause to never match, so we never
1072  * get here.
1073  */
1074  if (!node->mj_SkipMarkRestore)
1075  {
1076  ExecRestrPos(innerPlan);
1077 
1078  /*
1079  * ExecRestrPos probably should give us back a new
1080  * Slot, but since it doesn't, use the marked slot.
1081  * (The previously returned mj_InnerTupleSlot cannot
1082  * be assumed to hold the required tuple.)
1083  */
1084  node->mj_InnerTupleSlot = innerTupleSlot;
1085  /* we need not do MJEvalInnerValues again */
1086  }
1087 
1089  }
1090  else
1091  {
1092  /* ----------------
1093  * if the new outer tuple didn't match the marked inner
1094  * tuple then we have a case like:
1095  *
1096  * outer inner
1097  * 4 4 - marked tuple
1098  * new outer - 5 4
1099  * 6 5 - inner tuple
1100  * 7
1101  *
1102  * which means that all subsequent outer tuples will be
1103  * larger than our marked inner tuples. So we need not
1104  * revisit any of the marked tuples but can proceed to
1105  * look for a match to the current inner. If there's
1106  * no more inners, no more matches are possible.
1107  * ----------------
1108  */
1109  Assert(compareResult > 0);
1110  innerTupleSlot = node->mj_InnerTupleSlot;
1111 
1112  /* reload comparison data for current inner */
1113  switch (MJEvalInnerValues(node, innerTupleSlot))
1114  {
1115  case MJEVAL_MATCHABLE:
1116  /* proceed to compare it to the current outer */
1118  break;
1119  case MJEVAL_NONMATCHABLE:
1120 
1121  /*
1122  * current inner can't possibly match any outer;
1123  * better to advance the inner scan than the
1124  * outer.
1125  */
1127  break;
1128  case MJEVAL_ENDOFJOIN:
1129  /* No more inner tuples */
1130  if (doFillOuter)
1131  {
1132  /*
1133  * Need to emit left-join tuples for remaining
1134  * outer tuples.
1135  */
1137  break;
1138  }
1139  /* Otherwise we're done. */
1140  return NULL;
1141  }
1142  }
1143  break;
1144 
1145  /*----------------------------------------------------------
1146  * EXEC_MJ_SKIP means compare tuples and if they do not
1147  * match, skip whichever is lesser.
1148  *
1149  * For example:
1150  *
1151  * outer inner
1152  * 5 5
1153  * 5 5
1154  * outer tuple - 6 8 - inner tuple
1155  * 7 12
1156  * 8 14
1157  *
1158  * we have to advance the outer scan
1159  * until we find the outer 8.
1160  *
1161  * On the other hand:
1162  *
1163  * outer inner
1164  * 5 5
1165  * 5 5
1166  * outer tuple - 12 8 - inner tuple
1167  * 14 10
1168  * 17 12
1169  *
1170  * we have to advance the inner scan
1171  * until we find the inner 12.
1172  *----------------------------------------------------------
1173  */
1174  case EXEC_MJ_SKIP_TEST:
1175  MJ_printf("ExecMergeJoin: EXEC_MJ_SKIP_TEST\n");
1176 
1177  /*
1178  * before we advance, make sure the current tuples do not
1179  * satisfy the mergeclauses. If they do, then we update the
1180  * marked tuple position and go join them.
1181  */
1182  compareResult = MJCompare(node);
1183  MJ_DEBUG_COMPARE(compareResult);
1184 
1185  if (compareResult == 0)
1186  {
1187  if (!node->mj_SkipMarkRestore)
1188  ExecMarkPos(innerPlan);
1189 
1190  MarkInnerTuple(node->mj_InnerTupleSlot, node);
1191 
1193  }
1194  else if (compareResult < 0)
1196  else
1197  /* compareResult > 0 */
1199  break;
1200 
1201  /*
1202  * SKIPOUTER_ADVANCE: advance over an outer tuple that is
1203  * known not to join to any inner tuple.
1204  *
1205  * Before advancing, we check to see if we must emit an
1206  * outer-join fill tuple for this outer tuple.
1207  */
1209  MJ_printf("ExecMergeJoin: EXEC_MJ_SKIPOUTER_ADVANCE\n");
1210 
1211  if (doFillOuter && !node->mj_MatchedOuter)
1212  {
1213  /*
1214  * Generate a fake join tuple with nulls for the inner
1215  * tuple, and return it if it passes the non-join quals.
1216  */
1217  TupleTableSlot *result;
1218 
1219  node->mj_MatchedOuter = true; /* do it only once */
1220 
1221  result = MJFillOuter(node);
1222  if (result)
1223  return result;
1224  }
1225 
1226  /*
1227  * now we get the next outer tuple, if any
1228  */
1229  outerTupleSlot = ExecProcNode(outerPlan);
1230  node->mj_OuterTupleSlot = outerTupleSlot;
1231  MJ_DEBUG_PROC_NODE(outerTupleSlot);
1232  node->mj_MatchedOuter = false;
1233 
1234  /* Compute join values and check for unmatchability */
1235  switch (MJEvalOuterValues(node))
1236  {
1237  case MJEVAL_MATCHABLE:
1238  /* Go test the new tuple against the current inner */
1240  break;
1241  case MJEVAL_NONMATCHABLE:
1242  /* Can't match, so fetch next outer tuple */
1244  break;
1245  case MJEVAL_ENDOFJOIN:
1246  /* No more outer tuples */
1247  MJ_printf("ExecMergeJoin: end of outer subplan\n");
1248  innerTupleSlot = node->mj_InnerTupleSlot;
1249  if (doFillInner && !TupIsNull(innerTupleSlot))
1250  {
1251  /*
1252  * Need to emit right-join tuples for remaining
1253  * inner tuples.
1254  */
1256  break;
1257  }
1258  /* Otherwise we're done. */
1259  return NULL;
1260  }
1261  break;
1262 
1263  /*
1264  * SKIPINNER_ADVANCE: advance over an inner tuple that is
1265  * known not to join to any outer tuple.
1266  *
1267  * Before advancing, we check to see if we must emit an
1268  * outer-join fill tuple for this inner tuple.
1269  */
1271  MJ_printf("ExecMergeJoin: EXEC_MJ_SKIPINNER_ADVANCE\n");
1272 
1273  if (doFillInner && !node->mj_MatchedInner)
1274  {
1275  /*
1276  * Generate a fake join tuple with nulls for the outer
1277  * tuple, and return it if it passes the non-join quals.
1278  */
1279  TupleTableSlot *result;
1280 
1281  node->mj_MatchedInner = true; /* do it only once */
1282 
1283  result = MJFillInner(node);
1284  if (result)
1285  return result;
1286  }
1287 
1288  /* Mark before advancing, if wanted */
1289  if (node->mj_ExtraMarks)
1290  ExecMarkPos(innerPlan);
1291 
1292  /*
1293  * now we get the next inner tuple, if any
1294  */
1295  innerTupleSlot = ExecProcNode(innerPlan);
1296  node->mj_InnerTupleSlot = innerTupleSlot;
1297  MJ_DEBUG_PROC_NODE(innerTupleSlot);
1298  node->mj_MatchedInner = false;
1299 
1300  /* Compute join values and check for unmatchability */
1301  switch (MJEvalInnerValues(node, innerTupleSlot))
1302  {
1303  case MJEVAL_MATCHABLE:
1304  /* proceed to compare it to the current outer */
1306  break;
1307  case MJEVAL_NONMATCHABLE:
1308 
1309  /*
1310  * current inner can't possibly match any outer;
1311  * better to advance the inner scan than the outer.
1312  */
1314  break;
1315  case MJEVAL_ENDOFJOIN:
1316  /* No more inner tuples */
1317  MJ_printf("ExecMergeJoin: end of inner subplan\n");
1318  outerTupleSlot = node->mj_OuterTupleSlot;
1319  if (doFillOuter && !TupIsNull(outerTupleSlot))
1320  {
1321  /*
1322  * Need to emit left-join tuples for remaining
1323  * outer tuples.
1324  */
1326  break;
1327  }
1328  /* Otherwise we're done. */
1329  return NULL;
1330  }
1331  break;
1332 
1333  /*
1334  * EXEC_MJ_ENDOUTER means we have run out of outer tuples, but
1335  * are doing a right/full join and therefore must null-fill
1336  * any remaining unmatched inner tuples.
1337  */
1338  case EXEC_MJ_ENDOUTER:
1339  MJ_printf("ExecMergeJoin: EXEC_MJ_ENDOUTER\n");
1340 
1341  Assert(doFillInner);
1342 
1343  if (!node->mj_MatchedInner)
1344  {
1345  /*
1346  * Generate a fake join tuple with nulls for the outer
1347  * tuple, and return it if it passes the non-join quals.
1348  */
1349  TupleTableSlot *result;
1350 
1351  node->mj_MatchedInner = true; /* do it only once */
1352 
1353  result = MJFillInner(node);
1354  if (result)
1355  return result;
1356  }
1357 
1358  /* Mark before advancing, if wanted */
1359  if (node->mj_ExtraMarks)
1360  ExecMarkPos(innerPlan);
1361 
1362  /*
1363  * now we get the next inner tuple, if any
1364  */
1365  innerTupleSlot = ExecProcNode(innerPlan);
1366  node->mj_InnerTupleSlot = innerTupleSlot;
1367  MJ_DEBUG_PROC_NODE(innerTupleSlot);
1368  node->mj_MatchedInner = false;
1369 
1370  if (TupIsNull(innerTupleSlot))
1371  {
1372  MJ_printf("ExecMergeJoin: end of inner subplan\n");
1373  return NULL;
1374  }
1375 
1376  /* Else remain in ENDOUTER state and process next tuple. */
1377  break;
1378 
1379  /*
1380  * EXEC_MJ_ENDINNER means we have run out of inner tuples, but
1381  * are doing a left/full join and therefore must null- fill
1382  * any remaining unmatched outer tuples.
1383  */
1384  case EXEC_MJ_ENDINNER:
1385  MJ_printf("ExecMergeJoin: EXEC_MJ_ENDINNER\n");
1386 
1387  Assert(doFillOuter);
1388 
1389  if (!node->mj_MatchedOuter)
1390  {
1391  /*
1392  * Generate a fake join tuple with nulls for the inner
1393  * tuple, and return it if it passes the non-join quals.
1394  */
1395  TupleTableSlot *result;
1396 
1397  node->mj_MatchedOuter = true; /* do it only once */
1398 
1399  result = MJFillOuter(node);
1400  if (result)
1401  return result;
1402  }
1403 
1404  /*
1405  * now we get the next outer tuple, if any
1406  */
1407  outerTupleSlot = ExecProcNode(outerPlan);
1408  node->mj_OuterTupleSlot = outerTupleSlot;
1409  MJ_DEBUG_PROC_NODE(outerTupleSlot);
1410  node->mj_MatchedOuter = false;
1411 
1412  if (TupIsNull(outerTupleSlot))
1413  {
1414  MJ_printf("ExecMergeJoin: end of outer subplan\n");
1415  return NULL;
1416  }
1417 
1418  /* Else remain in ENDINNER state and process next tuple. */
1419  break;
1420 
1421  /*
1422  * broken state value?
1423  */
1424  default:
1425  elog(ERROR, "unrecognized mergejoin state: %d",
1426  (int) node->mj_JoinState);
1427  }
1428  }
1429 }
#define EXEC_MJ_NEXTOUTER
JoinType jointype
Definition: execnodes.h:1601
#define EXEC_MJ_SKIPOUTER_ADVANCE
ExprState * joinqual
Definition: execnodes.h:1604
ProjectionInfo * ps_ProjInfo
Definition: execnodes.h:884
#define EXEC_MJ_TESTOUTER
#define MJ_DEBUG_COMPARE(res)
Definition: execdebug.h:123
#define MJ_dump(state)
Definition: execdebug.h:122
PlanState ps
Definition: execnodes.h:1600
#define castNode(_type_, nodeptr)
Definition: nodes.h:579
static MJEvalResult MJEvalInnerValues(MergeJoinState *mergestate, TupleTableSlot *innerslot)
static int MJCompare(MergeJoinState *mergestate)
ExprContext * ps_ExprContext
Definition: execnodes.h:883
bool single_match
Definition: execnodes.h:1602
#define EXEC_MJ_ENDOUTER
TupleTableSlot * mj_MarkedTupleSlot
Definition: execnodes.h:1663
#define MJ_printf(s)
Definition: execdebug.h:118
#define EXEC_MJ_INITIALIZE_INNER
#define EXEC_MJ_SKIP_TEST
static bool ExecQual(ExprState *state, ExprContext *econtext)
Definition: executor.h:356
static TupleTableSlot * MJFillInner(MergeJoinState *node)
static MJEvalResult MJEvalOuterValues(MergeJoinState *mergestate)
void ExecRestrPos(PlanState *node)
Definition: execAmi.c:363
bool mj_MatchedOuter
Definition: execnodes.h:1659
#define ERROR
Definition: elog.h:43
#define EXEC_MJ_NEXTINNER
bool mj_MatchedInner
Definition: execnodes.h:1660
#define MarkInnerTuple(innerTupleSlot, mergestate)
#define outerPlanState(node)
Definition: execnodes.h:895
#define innerPlan(node)
Definition: plannodes.h:173
TupleTableSlot * ecxt_innertuple
Definition: execnodes.h:198
#define TupIsNull(slot)
Definition: tuptable.h:138
#define InstrCountFiltered1(node, delta)
Definition: execnodes.h:898
#define EXEC_MJ_INITIALIZE_OUTER
#define outerPlan(node)
Definition: plannodes.h:174
#define EXEC_MJ_JOINTUPLES
bool mj_SkipMarkRestore
Definition: execnodes.h:1654
#define EXEC_MJ_ENDINNER
#define MJ_DEBUG_PROC_NODE(slot)
Definition: execdebug.h:125
static TupleTableSlot * ExecProcNode(PlanState *node)
Definition: executor.h:236
void ExecMarkPos(PlanState *node)
Definition: execAmi.c:314
JoinState js
Definition: execnodes.h:1650
TupleTableSlot * ecxt_outertuple
Definition: execnodes.h:199
#define Assert(condition)
Definition: c.h:670
TupleTableSlot * mj_InnerTupleSlot
Definition: execnodes.h:1662
#define InstrCountFiltered2(node, delta)
Definition: execnodes.h:903
#define MJ_DEBUG_QUAL(clause, res)
Definition: execdebug.h:124
ExprState * qual
Definition: execnodes.h:867
#define EXEC_MJ_SKIPINNER_ADVANCE
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
#define elog
Definition: elog.h:219
bool mj_ExtraMarks
Definition: execnodes.h:1655
#define innerPlanState(node)
Definition: execnodes.h:894
static TupleTableSlot * MJFillOuter(MergeJoinState *node)
static TupleTableSlot * ExecProject(ProjectionInfo *projInfo)
Definition: executor.h:319
#define ResetExprContext(econtext)
Definition: executor.h:461
TupleTableSlot * mj_OuterTupleSlot
Definition: execnodes.h:1661

◆ ExecReScanMergeJoin()

void ExecReScanMergeJoin ( MergeJoinState node)

Definition at line 1656 of file nodeMergejoin.c.

References PlanState::chgParam, EXEC_MJ_INITIALIZE_OUTER, ExecClearTuple(), ExecReScan(), MergeJoinState::js, PlanState::lefttree, MergeJoinState::mj_InnerTupleSlot, MergeJoinState::mj_JoinState, MergeJoinState::mj_MarkedTupleSlot, MergeJoinState::mj_MatchedInner, MergeJoinState::mj_MatchedOuter, MergeJoinState::mj_OuterTupleSlot, JoinState::ps, and PlanState::righttree.

Referenced by ExecReScan().

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 (node->js.ps.lefttree->chgParam == NULL)
1671  ExecReScan(node->js.ps.lefttree);
1672  if (node->js.ps.righttree->chgParam == NULL)
1673  ExecReScan(node->js.ps.righttree);
1674 
1675 }
PlanState ps
Definition: execnodes.h:1600
void ExecReScan(PlanState *node)
Definition: execAmi.c:76
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
TupleTableSlot * mj_MarkedTupleSlot
Definition: execnodes.h:1663
struct PlanState * righttree
Definition: execnodes.h:869
struct PlanState * lefttree
Definition: execnodes.h:868
bool mj_MatchedOuter
Definition: execnodes.h:1659
bool mj_MatchedInner
Definition: execnodes.h:1660
#define EXEC_MJ_INITIALIZE_OUTER
Bitmapset * chgParam
Definition: execnodes.h:877
JoinState js
Definition: execnodes.h:1650
TupleTableSlot * mj_InnerTupleSlot
Definition: execnodes.h:1662
TupleTableSlot * mj_OuterTupleSlot
Definition: execnodes.h:1661

◆ MJCompare()

static int MJCompare ( MergeJoinState mergestate)
static

Definition at line 392 of file nodeMergejoin.c.

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

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 }
SortSupportData ssup
bool mj_ConstFalseJoin
Definition: execnodes.h:1656
PlanState ps
Definition: execnodes.h:1600
ExprContext * ps_ExprContext
Definition: execnodes.h:883
MemoryContext ecxt_per_tuple_memory
Definition: execnodes.h:203
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MergeJoinClause mj_Clauses
Definition: execnodes.h:1652
JoinState js
Definition: execnodes.h:1650
int i
static int ApplySortComparator(Datum datum1, bool isNull1, Datum datum2, bool isNull2, SortSupport ssup)
Definition: sortsupport.h:201
#define ResetExprContext(econtext)
Definition: executor.h:461

◆ MJEvalInnerValues()

static MJEvalResult MJEvalInnerValues ( MergeJoinState mergestate,
TupleTableSlot innerslot 
)
static

Definition at line 342 of file nodeMergejoin.c.

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

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 }
bool ssup_nulls_first
Definition: sortsupport.h:75
SortSupportData ssup
ExprContext * mj_InnerEContext
Definition: execnodes.h:1667
MemoryContext ecxt_per_tuple_memory
Definition: execnodes.h:203
MJEvalResult
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static Datum ExecEvalExpr(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:276
TupleTableSlot * ecxt_innertuple
Definition: execnodes.h:198
#define TupIsNull(slot)
Definition: tuptable.h:138
MergeJoinClause mj_Clauses
Definition: execnodes.h:1652
int i
#define ResetExprContext(econtext)
Definition: executor.h:461

◆ MJEvalOuterValues()

static MJEvalResult MJEvalOuterValues ( MergeJoinState mergestate)
static

Definition at line 295 of file nodeMergejoin.c.

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

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 }
bool ssup_nulls_first
Definition: sortsupport.h:75
SortSupportData ssup
MemoryContext ecxt_per_tuple_memory
Definition: execnodes.h:203
MJEvalResult
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static Datum ExecEvalExpr(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:276
#define TupIsNull(slot)
Definition: tuptable.h:138
MergeJoinClause mj_Clauses
Definition: execnodes.h:1652
TupleTableSlot * ecxt_outertuple
Definition: execnodes.h:199
ExprContext * mj_OuterEContext
Definition: execnodes.h:1666
int i
#define ResetExprContext(econtext)
Definition: executor.h:461
TupleTableSlot * mj_OuterTupleSlot
Definition: execnodes.h:1661

◆ 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.

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

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 }
bool ssup_nulls_first
Definition: sortsupport.h:75
SortSupportData ssup
#define IsA(nodeptr, _type_)
Definition: nodes.h:561
#define BTORDER_PROC
Definition: nbtree.h:229
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
#define BTSORTSUPPORT_PROC
Definition: nbtree.h:230
void PrepareSortSupportComparisonShim(Oid cmpFunc, SortSupport ssup)
Definition: sortsupport.c:67
#define PointerGetDatum(X)
Definition: postgres.h:562
uint16 StrategyNumber
Definition: stratnum.h:22
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:576
#define lsecond(l)
Definition: pg_list.h:116
#define linitial(l)
Definition: pg_list.h:111
#define ERROR
Definition: elog.h:43
MemoryContext ssup_cxt
Definition: sortsupport.h:66
#define OidFunctionCall1(functionId, arg1)
Definition: fmgr.h:623
int(* comparator)(Datum x, Datum y, SortSupport ssup)
Definition: sortsupport.h:107
struct MergeJoinClauseData * MergeJoinClause
Definition: execnodes.h:1646
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
void * palloc0(Size size)
Definition: mcxt.c:877
#define Assert(condition)
Definition: c.h:670
#define lfirst(lc)
Definition: pg_list.h:106
Oid get_opfamily_proc(Oid opfamily, Oid lefttype, Oid righttype, int16 procnum)
Definition: lsyscache.c:744
static int list_length(const List *l)
Definition: pg_list.h:89
void get_op_opfamily_properties(Oid opno, Oid opfamily, bool ordering_op, int *strategy, Oid *lefttype, Oid *righttype)
Definition: lsyscache.c:133
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition: execExpr.c:113
Oid opno
Definition: primnodes.h:496
#define elog
Definition: elog.h:219
List * args
Definition: primnodes.h:502
#define BTLessStrategyNumber
Definition: stratnum.h:29
#define BTEqualStrategyNumber
Definition: stratnum.h:31

◆ MJFillInner()

static TupleTableSlot* MJFillInner ( MergeJoinState node)
static

Definition at line 484 of file nodeMergejoin.c.

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

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 }
ProjectionInfo * ps_ProjInfo
Definition: execnodes.h:884
PlanState ps
Definition: execnodes.h:1600
ExprContext * ps_ExprContext
Definition: execnodes.h:883
#define MJ_printf(s)
Definition: execdebug.h:118
static bool ExecQual(ExprState *state, ExprContext *econtext)
Definition: executor.h:356
TupleTableSlot * ecxt_innertuple
Definition: execnodes.h:198
JoinState js
Definition: execnodes.h:1650
TupleTableSlot * ecxt_outertuple
Definition: execnodes.h:199
TupleTableSlot * mj_InnerTupleSlot
Definition: execnodes.h:1662
#define InstrCountFiltered2(node, delta)
Definition: execnodes.h:903
TupleTableSlot * mj_NullOuterTupleSlot
Definition: execnodes.h:1664
ExprState * qual
Definition: execnodes.h:867
static TupleTableSlot * ExecProject(ProjectionInfo *projInfo)
Definition: executor.h:319
#define ResetExprContext(econtext)
Definition: executor.h:461

◆ MJFillOuter()

static TupleTableSlot* MJFillOuter ( MergeJoinState node)
static

Definition at line 453 of file nodeMergejoin.c.

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

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 }
ProjectionInfo * ps_ProjInfo
Definition: execnodes.h:884
PlanState ps
Definition: execnodes.h:1600
ExprContext * ps_ExprContext
Definition: execnodes.h:883
#define MJ_printf(s)
Definition: execdebug.h:118
TupleTableSlot * mj_NullInnerTupleSlot
Definition: execnodes.h:1665
static bool ExecQual(ExprState *state, ExprContext *econtext)
Definition: executor.h:356
TupleTableSlot * ecxt_innertuple
Definition: execnodes.h:198
JoinState js
Definition: execnodes.h:1650
TupleTableSlot * ecxt_outertuple
Definition: execnodes.h:199
#define InstrCountFiltered2(node, delta)
Definition: execnodes.h:903
ExprState * qual
Definition: execnodes.h:867
static TupleTableSlot * ExecProject(ProjectionInfo *projInfo)
Definition: executor.h:319
#define ResetExprContext(econtext)
Definition: executor.h:461
TupleTableSlot * mj_OuterTupleSlot
Definition: execnodes.h:1661