PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
nodeMergejoin.c File Reference
#include "postgres.h"
#include "access/nbtree.h"
#include "executor/execdebug.h"
#include "executor/nodeMergejoin.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)
 
TupleTableSlotExecMergeJoin (MergeJoinState *node)
 
MergeJoinStateExecInitMergeJoin (MergeJoin *node, EState *estate, int eflags)
 
void ExecEndMergeJoin (MergeJoinState *node)
 
void ExecReScanMergeJoin (MergeJoinState *node)
 

Macro Definition Documentation

#define EXEC_MJ_ENDINNER   11

Definition at line 115 of file nodeMergejoin.c.

Referenced by ExecMergeJoin().

#define EXEC_MJ_ENDOUTER   10

Definition at line 114 of file nodeMergejoin.c.

Referenced by ExecMergeJoin().

#define EXEC_MJ_INITIALIZE_INNER   2

Definition at line 106 of file nodeMergejoin.c.

Referenced by ExecMergeJoin().

#define EXEC_MJ_INITIALIZE_OUTER   1

Definition at line 105 of file nodeMergejoin.c.

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

#define EXEC_MJ_JOINTUPLES   3

Definition at line 107 of file nodeMergejoin.c.

Referenced by ExecMergeJoin().

#define EXEC_MJ_NEXTINNER   6

Definition at line 110 of file nodeMergejoin.c.

Referenced by ExecMergeJoin().

#define EXEC_MJ_NEXTOUTER   4

Definition at line 108 of file nodeMergejoin.c.

Referenced by ExecMergeJoin().

#define EXEC_MJ_SKIP_TEST   7

Definition at line 111 of file nodeMergejoin.c.

Referenced by ExecMergeJoin().

#define EXEC_MJ_SKIPINNER_ADVANCE   9

Definition at line 113 of file nodeMergejoin.c.

Referenced by ExecMergeJoin().

#define EXEC_MJ_SKIPOUTER_ADVANCE   8

Definition at line 112 of file nodeMergejoin.c.

Referenced by ExecMergeJoin().

#define EXEC_MJ_TESTOUTER   5

Definition at line 109 of file nodeMergejoin.c.

Referenced by ExecMergeJoin().

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

Definition at line 151 of file nodeMergejoin.c.

Referenced by ExecMergeJoin().

Typedef Documentation

Enumeration Type Documentation

Enumerator
MJEVAL_MATCHABLE 
MJEVAL_NONMATCHABLE 
MJEVAL_ENDOFJOIN 

Definition at line 143 of file nodeMergejoin.c.

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

Function Documentation

static bool check_constant_qual ( List qual,
bool is_const_false 
)
static

Definition at line 519 of file nodeMergejoin.c.

References Const::constisnull, Const::constvalue, DatumGetBool, IsA, and lfirst.

Referenced by ExecInitMergeJoin().

520 {
521  ListCell *lc;
522 
523  foreach(lc, qual)
524  {
525  Const *con = (Const *) lfirst(lc);
526 
527  if (!con || !IsA(con, Const))
528  return false;
529  if (con->constisnull || !DatumGetBool(con->constvalue))
530  *is_const_false = true;
531  }
532  return true;
533 }
Datum constvalue
Definition: primnodes.h:196
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
#define DatumGetBool(X)
Definition: postgres.h:399
#define lfirst(lc)
Definition: pg_list.h:106
bool constisnull
Definition: primnodes.h:197
void ExecEndMergeJoin ( MergeJoinState node)

Definition at line 1624 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().

1625 {
1626  MJ1_printf("ExecEndMergeJoin: %s\n",
1627  "ending node processing");
1628 
1629  /*
1630  * Free the exprcontext
1631  */
1632  ExecFreeExprContext(&node->js.ps);
1633 
1634  /*
1635  * clean out the tuple table
1636  */
1639 
1640  /*
1641  * shut down the subplans
1642  */
1643  ExecEndNode(innerPlanState(node));
1644  ExecEndNode(outerPlanState(node));
1645 
1646  MJ1_printf("ExecEndMergeJoin: %s\n",
1647  "node processing ended");
1648 }
#define MJ1_printf(s, p)
Definition: execdebug.h:119
PlanState ps
Definition: execnodes.h:1548
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:654
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
TupleTableSlot * mj_MarkedTupleSlot
Definition: execnodes.h:1611
void ExecFreeExprContext(PlanState *planstate)
Definition: execUtils.c:516
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:842
#define outerPlanState(node)
Definition: execnodes.h:855
JoinState js
Definition: execnodes.h:1598
#define innerPlanState(node)
Definition: execnodes.h:854
MergeJoinState* ExecInitMergeJoin ( MergeJoin node,
EState estate,
int  eflags 
)

Definition at line 1432 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(), ExecSetSlotDescriptor(), Join::inner_unique, innerPlan, innerPlanState, IsA, MergeJoin::join, JOIN_ANTI, JOIN_FULL, JOIN_INNER, JOIN_LEFT, JOIN_RIGHT, JOIN_SEMI, Join::joinqual, JoinState::joinqual, Join::jointype, JoinState::jointype, MergeJoinState::js, list_length(), makeNode, MergeJoin::mergeclauses, MergeJoin::mergeCollations, MergeJoin::mergeFamilies, MergeJoin::mergeNullsFirst, MergeJoin::mergeStrategies, 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, NULL, outerPlan, outerPlanState, Join::plan, PlanState::plan, JoinState::ps, Plan::qual, PlanState::qual, JoinState::single_match, MergeJoin::skip_mark_restore, and PlanState::state.

Referenced by ExecInitNode().

1433 {
1434  MergeJoinState *mergestate;
1435 
1436  /* check for unsupported flags */
1437  Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
1438 
1439  MJ1_printf("ExecInitMergeJoin: %s\n",
1440  "initializing node");
1441 
1442  /*
1443  * create state structure
1444  */
1445  mergestate = makeNode(MergeJoinState);
1446  mergestate->js.ps.plan = (Plan *) node;
1447  mergestate->js.ps.state = estate;
1448 
1449  /*
1450  * Miscellaneous initialization
1451  *
1452  * create expression context for node
1453  */
1454  ExecAssignExprContext(estate, &mergestate->js.ps);
1455 
1456  /*
1457  * we need two additional econtexts in which we can compute the join
1458  * expressions from the left and right input tuples. The node's regular
1459  * econtext won't do because it gets reset too often.
1460  */
1461  mergestate->mj_OuterEContext = CreateExprContext(estate);
1462  mergestate->mj_InnerEContext = CreateExprContext(estate);
1463 
1464  /*
1465  * initialize child expressions
1466  */
1467  mergestate->js.ps.qual =
1468  ExecInitQual(node->join.plan.qual, (PlanState *) mergestate);
1469  mergestate->js.jointype = node->join.jointype;
1470  mergestate->js.joinqual =
1471  ExecInitQual(node->join.joinqual, (PlanState *) mergestate);
1472  mergestate->mj_ConstFalseJoin = false;
1473  /* mergeclauses are handled below */
1474 
1475  /*
1476  * initialize child nodes
1477  *
1478  * inner child must support MARK/RESTORE, unless we have detected that we
1479  * don't need that. Note that skip_mark_restore must never be set if
1480  * there are non-mergeclause joinquals, since the logic wouldn't work.
1481  */
1482  Assert(node->join.joinqual == NIL || !node->skip_mark_restore);
1483  mergestate->mj_SkipMarkRestore = node->skip_mark_restore;
1484 
1485  outerPlanState(mergestate) = ExecInitNode(outerPlan(node), estate, eflags);
1486  innerPlanState(mergestate) = ExecInitNode(innerPlan(node), estate,
1487  mergestate->mj_SkipMarkRestore ?
1488  eflags :
1489  (eflags | EXEC_FLAG_MARK));
1490 
1491  /*
1492  * For certain types of inner child nodes, it is advantageous to issue
1493  * MARK every time we advance past an inner tuple we will never return to.
1494  * For other types, MARK on a tuple we cannot return to is a waste of
1495  * cycles. Detect which case applies and set mj_ExtraMarks if we want to
1496  * issue "unnecessary" MARK calls.
1497  *
1498  * Currently, only Material wants the extra MARKs, and it will be helpful
1499  * only if eflags doesn't specify REWIND.
1500  */
1501  if (IsA(innerPlan(node), Material) &&
1502  (eflags & EXEC_FLAG_REWIND) == 0 &&
1503  !mergestate->mj_SkipMarkRestore)
1504  mergestate->mj_ExtraMarks = true;
1505  else
1506  mergestate->mj_ExtraMarks = false;
1507 
1508  /*
1509  * tuple table initialization
1510  */
1511  ExecInitResultTupleSlot(estate, &mergestate->js.ps);
1512 
1513  mergestate->mj_MarkedTupleSlot = ExecInitExtraTupleSlot(estate);
1515  ExecGetResultType(innerPlanState(mergestate)));
1516 
1517  /*
1518  * detect whether we need only consider the first matching inner tuple
1519  */
1520  mergestate->js.single_match = (node->join.inner_unique ||
1521  node->join.jointype == JOIN_SEMI);
1522 
1523  /* set up null tuples for outer joins, if needed */
1524  switch (node->join.jointype)
1525  {
1526  case JOIN_INNER:
1527  case JOIN_SEMI:
1528  mergestate->mj_FillOuter = false;
1529  mergestate->mj_FillInner = false;
1530  break;
1531  case JOIN_LEFT:
1532  case JOIN_ANTI:
1533  mergestate->mj_FillOuter = true;
1534  mergestate->mj_FillInner = false;
1535  mergestate->mj_NullInnerTupleSlot =
1536  ExecInitNullTupleSlot(estate,
1537  ExecGetResultType(innerPlanState(mergestate)));
1538  break;
1539  case JOIN_RIGHT:
1540  mergestate->mj_FillOuter = false;
1541  mergestate->mj_FillInner = true;
1542  mergestate->mj_NullOuterTupleSlot =
1543  ExecInitNullTupleSlot(estate,
1544  ExecGetResultType(outerPlanState(mergestate)));
1545 
1546  /*
1547  * Can't handle right or full join with non-constant extra
1548  * joinclauses. This should have been caught by planner.
1549  */
1550  if (!check_constant_qual(node->join.joinqual,
1551  &mergestate->mj_ConstFalseJoin))
1552  ereport(ERROR,
1553  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1554  errmsg("RIGHT JOIN is only supported with merge-joinable join conditions")));
1555  break;
1556  case JOIN_FULL:
1557  mergestate->mj_FillOuter = true;
1558  mergestate->mj_FillInner = true;
1559  mergestate->mj_NullOuterTupleSlot =
1560  ExecInitNullTupleSlot(estate,
1561  ExecGetResultType(outerPlanState(mergestate)));
1562  mergestate->mj_NullInnerTupleSlot =
1563  ExecInitNullTupleSlot(estate,
1564  ExecGetResultType(innerPlanState(mergestate)));
1565 
1566  /*
1567  * Can't handle right or full join with non-constant extra
1568  * joinclauses. This should have been caught by planner.
1569  */
1570  if (!check_constant_qual(node->join.joinqual,
1571  &mergestate->mj_ConstFalseJoin))
1572  ereport(ERROR,
1573  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1574  errmsg("FULL JOIN is only supported with merge-joinable join conditions")));
1575  break;
1576  default:
1577  elog(ERROR, "unrecognized join type: %d",
1578  (int) node->join.jointype);
1579  }
1580 
1581  /*
1582  * initialize tuple type and projection info
1583  */
1584  ExecAssignResultTypeFromTL(&mergestate->js.ps);
1585  ExecAssignProjectionInfo(&mergestate->js.ps, NULL);
1586 
1587  /*
1588  * preprocess the merge clauses
1589  */
1590  mergestate->mj_NumClauses = list_length(node->mergeclauses);
1591  mergestate->mj_Clauses = MJExamineQuals(node->mergeclauses,
1592  node->mergeFamilies,
1593  node->mergeCollations,
1594  node->mergeStrategies,
1595  node->mergeNullsFirst,
1596  (PlanState *) mergestate);
1597 
1598  /*
1599  * initialize join state
1600  */
1601  mergestate->mj_JoinState = EXEC_MJ_INITIALIZE_OUTER;
1602  mergestate->mj_MatchedOuter = false;
1603  mergestate->mj_MatchedInner = false;
1604  mergestate->mj_OuterTupleSlot = NULL;
1605  mergestate->mj_InnerTupleSlot = NULL;
1606 
1607  /*
1608  * initialization successful
1609  */
1610  MJ1_printf("ExecInitMergeJoin: %s\n",
1611  "node initialized");
1612 
1613  return mergestate;
1614 }
JoinType jointype
Definition: execnodes.h:1549
#define NIL
Definition: pg_list.h:69
List * qual
Definition: plannodes.h:145
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
TupleTableSlot * ExecInitExtraTupleSlot(EState *estate)
Definition: execTuples.c:852
#define MJ1_printf(s, p)
Definition: execdebug.h:119
ExprState * joinqual
Definition: execnodes.h:1552
ExprContext * mj_InnerEContext
Definition: execnodes.h:1615
bool mj_ConstFalseJoin
Definition: execnodes.h:1604
PlanState ps
Definition: execnodes.h:1548
bool single_match
Definition: execnodes.h:1550
TupleTableSlot * mj_MarkedTupleSlot
Definition: execnodes.h:1611
TupleTableSlot * mj_NullInnerTupleSlot
Definition: execnodes.h:1613
int errcode(int sqlerrcode)
Definition: elog.c:575
EState * state
Definition: execnodes.h:815
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition: execExpr.c:160
void ExecAssignResultTypeFromTL(PlanState *planstate)
Definition: execUtils.c:440
List * mergeclauses
Definition: plannodes.h:713
JoinType jointype
Definition: plannodes.h:669
bool mj_MatchedOuter
Definition: execnodes.h:1607
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:712
#define ERROR
Definition: elog.h:43
void ExecInitResultTupleSlot(EState *estate, PlanState *planstate)
Definition: execTuples.c:832
bool mj_MatchedInner
Definition: execnodes.h:1608
#define EXEC_FLAG_BACKWARD
Definition: executor.h:60
#define outerPlanState(node)
Definition: execnodes.h:855
#define innerPlan(node)
Definition: plannodes.h:173
void ExecAssignProjectionInfo(PlanState *planstate, TupleDesc inputDesc)
Definition: execUtils.c:487
MergeJoinClause mj_Clauses
Definition: execnodes.h:1600
#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:1602
void ExecSetSlotDescriptor(TupleTableSlot *slot, TupleDesc tupdesc)
Definition: execTuples.c:247
Oid * mergeFamilies
Definition: plannodes.h:715
Oid * mergeCollations
Definition: plannodes.h:716
Plan * plan
Definition: execnodes.h:813
JoinState js
Definition: execnodes.h:1598
bool * mergeNullsFirst
Definition: plannodes.h:718
#define makeNode(_type_)
Definition: nodes.h:557
#define NULL
Definition: c.h:229
int * mergeStrategies
Definition: plannodes.h:717
#define Assert(condition)
Definition: c.h:675
#define EXEC_FLAG_MARK
Definition: executor.h:61
TupleTableSlot * mj_InnerTupleSlot
Definition: execnodes.h:1610
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:418
ExprContext * mj_OuterEContext
Definition: execnodes.h:1614
static int list_length(const List *l)
Definition: pg_list.h:89
TupleTableSlot * mj_NullOuterTupleSlot
Definition: execnodes.h:1612
TupleDesc ExecGetResultType(PlanState *planstate)
Definition: execUtils.c:469
ExprState * qual
Definition: execnodes.h:827
int errmsg(const char *fmt,...)
Definition: elog.c:797
Join join
Definition: plannodes.h:711
ExprContext * CreateExprContext(EState *estate)
Definition: execUtils.c:218
#define elog
Definition: elog.h:219
bool inner_unique
Definition: plannodes.h:670
bool mj_ExtraMarks
Definition: execnodes.h:1603
#define innerPlanState(node)
Definition: execnodes.h:854
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:140
List * joinqual
Definition: plannodes.h:671
TupleTableSlot * mj_OuterTupleSlot
Definition: execnodes.h:1609
Plan plan
Definition: plannodes.h:668
TupleTableSlot* ExecMergeJoin ( MergeJoinState node)

Definition at line 599 of file nodeMergejoin.c.

References Assert, 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(), NULL, outerPlan, outerPlanState, JoinState::ps, PlanState::ps_ExprContext, PlanState::ps_ProjInfo, PlanState::qual, ResetExprContext, result, JoinState::single_match, and TupIsNull.

Referenced by ExecProcNode().

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

Definition at line 1651 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, NULL, JoinState::ps, and PlanState::righttree.

Referenced by ExecReScan().

1652 {
1654 
1656  node->mj_MatchedOuter = false;
1657  node->mj_MatchedInner = false;
1658  node->mj_OuterTupleSlot = NULL;
1659  node->mj_InnerTupleSlot = NULL;
1660 
1661  /*
1662  * if chgParam of subnodes is not null then plans will be re-scanned by
1663  * first ExecProcNode.
1664  */
1665  if (node->js.ps.lefttree->chgParam == NULL)
1666  ExecReScan(node->js.ps.lefttree);
1667  if (node->js.ps.righttree->chgParam == NULL)
1668  ExecReScan(node->js.ps.righttree);
1669 
1670 }
PlanState ps
Definition: execnodes.h:1548
void ExecReScan(PlanState *node)
Definition: execAmi.c:75
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
TupleTableSlot * mj_MarkedTupleSlot
Definition: execnodes.h:1611
struct PlanState * righttree
Definition: execnodes.h:829
struct PlanState * lefttree
Definition: execnodes.h:828
bool mj_MatchedOuter
Definition: execnodes.h:1607
bool mj_MatchedInner
Definition: execnodes.h:1608
#define EXEC_MJ_INITIALIZE_OUTER
Bitmapset * chgParam
Definition: execnodes.h:837
JoinState js
Definition: execnodes.h:1598
#define NULL
Definition: c.h:229
TupleTableSlot * mj_InnerTupleSlot
Definition: execnodes.h:1610
TupleTableSlot * mj_OuterTupleSlot
Definition: execnodes.h:1609
static int MJCompare ( MergeJoinState mergestate)
static

Definition at line 391 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, result, MergeJoinClauseData::risnull, and MergeJoinClauseData::ssup.

Referenced by ExecMergeJoin().

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

Definition at line 341 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, result, MergeJoinClauseData::rexpr, MergeJoinClauseData::risnull, MergeJoinClauseData::ssup, SortSupportData::ssup_nulls_first, and TupIsNull.

Referenced by ExecMergeJoin().

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

Definition at line 294 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, result, MergeJoinClauseData::ssup, SortSupportData::ssup_nulls_first, and TupIsNull.

Referenced by ExecMergeJoin().

295 {
296  ExprContext *econtext = mergestate->mj_OuterEContext;
298  int i;
299  MemoryContext oldContext;
300 
301  /* Check for end of outer subplan */
302  if (TupIsNull(mergestate->mj_OuterTupleSlot))
303  return MJEVAL_ENDOFJOIN;
304 
305  ResetExprContext(econtext);
306 
307  oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
308 
309  econtext->ecxt_outertuple = mergestate->mj_OuterTupleSlot;
310 
311  for (i = 0; i < mergestate->mj_NumClauses; i++)
312  {
313  MergeJoinClause clause = &mergestate->mj_Clauses[i];
314 
315  clause->ldatum = ExecEvalExpr(clause->lexpr, econtext,
316  &clause->lisnull);
317  if (clause->lisnull)
318  {
319  /* match is impossible; can we end the join early? */
320  if (i == 0 && !clause->ssup.ssup_nulls_first &&
321  !mergestate->mj_FillOuter)
322  result = MJEVAL_ENDOFJOIN;
323  else if (result == MJEVAL_MATCHABLE)
324  result = MJEVAL_NONMATCHABLE;
325  }
326  }
327 
328  MemoryContextSwitchTo(oldContext);
329 
330  return result;
331 }
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
return result
Definition: formatting.c:1632
static Datum ExecEvalExpr(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:266
#define TupIsNull(slot)
Definition: tuptable.h:138
MergeJoinClause mj_Clauses
Definition: execnodes.h:1600
TupleTableSlot * ecxt_outertuple
Definition: execnodes.h:199
ExprContext * mj_OuterEContext
Definition: execnodes.h:1614
int i
#define ResetExprContext(econtext)
Definition: executor.h:450
TupleTableSlot * mj_OuterTupleSlot
Definition: execnodes.h:1609
static MergeJoinClause MJExamineQuals ( List mergeclauses,
Oid mergefamilies,
Oid mergecollations,
int *  mergestrategies,
bool mergenullsfirst,
PlanState parent 
)
static

Definition at line 175 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, NULL, 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().

181 {
182  MergeJoinClause clauses;
183  int nClauses = list_length(mergeclauses);
184  int iClause;
185  ListCell *cl;
186 
187  clauses = (MergeJoinClause) palloc0(nClauses * sizeof(MergeJoinClauseData));
188 
189  iClause = 0;
190  foreach(cl, mergeclauses)
191  {
192  OpExpr *qual = (OpExpr *) lfirst(cl);
193  MergeJoinClause clause = &clauses[iClause];
194  Oid opfamily = mergefamilies[iClause];
195  Oid collation = mergecollations[iClause];
196  StrategyNumber opstrategy = mergestrategies[iClause];
197  bool nulls_first = mergenullsfirst[iClause];
198  int op_strategy;
199  Oid op_lefttype;
200  Oid op_righttype;
201  Oid sortfunc;
202 
203  if (!IsA(qual, OpExpr))
204  elog(ERROR, "mergejoin clause is not an OpExpr");
205 
206  /*
207  * Prepare the input expressions for execution.
208  */
209  clause->lexpr = ExecInitExpr((Expr *) linitial(qual->args), parent);
210  clause->rexpr = ExecInitExpr((Expr *) lsecond(qual->args), parent);
211 
212  /* Set up sort support data */
214  clause->ssup.ssup_collation = collation;
215  if (opstrategy == BTLessStrategyNumber)
216  clause->ssup.ssup_reverse = false;
217  else if (opstrategy == BTGreaterStrategyNumber)
218  clause->ssup.ssup_reverse = true;
219  else /* planner screwed up */
220  elog(ERROR, "unsupported mergejoin strategy %d", opstrategy);
221  clause->ssup.ssup_nulls_first = nulls_first;
222 
223  /* Extract the operator's declared left/right datatypes */
224  get_op_opfamily_properties(qual->opno, opfamily, false,
225  &op_strategy,
226  &op_lefttype,
227  &op_righttype);
228  if (op_strategy != BTEqualStrategyNumber) /* should not happen */
229  elog(ERROR, "cannot merge using non-equality operator %u",
230  qual->opno);
231 
232  /*
233  * sortsupport routine must know if abbreviation optimization is
234  * applicable in principle. It is never applicable for merge joins
235  * because there is no convenient opportunity to convert to
236  * alternative representation.
237  */
238  clause->ssup.abbreviate = false;
239 
240  /* And get the matching support or comparison function */
241  Assert(clause->ssup.comparator == NULL);
242  sortfunc = get_opfamily_proc(opfamily,
243  op_lefttype,
244  op_righttype,
246  if (OidIsValid(sortfunc))
247  {
248  /* The sort support function can provide a comparator */
249  OidFunctionCall1(sortfunc, PointerGetDatum(&clause->ssup));
250  }
251  if (clause->ssup.comparator == NULL)
252  {
253  /* support not available, get comparison func */
254  sortfunc = get_opfamily_proc(opfamily,
255  op_lefttype,
256  op_righttype,
257  BTORDER_PROC);
258  if (!OidIsValid(sortfunc)) /* should not happen */
259  elog(ERROR, "missing support function %d(%u,%u) in opfamily %u",
260  BTORDER_PROC, op_lefttype, op_righttype, opfamily);
261  /* We'll use a shim to call the old-style btree comparator */
262  PrepareSortSupportComparisonShim(sortfunc, &clause->ssup);
263  }
264 
265  iClause++;
266  }
267 
268  return clauses;
269 }
int(* comparator)(Datum x, Datum y, SortSupport ssup)
Definition: sortsupport.h:107
bool ssup_nulls_first
Definition: sortsupport.h:75
SortSupportData ssup
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
#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:538
#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:622
struct MergeJoinClauseData * MergeJoinClause
Definition: execnodes.h:1594
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
void * palloc0(Size size)
Definition: mcxt.c:878
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#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:495
#define elog
Definition: elog.h:219
List * args
Definition: primnodes.h:501
#define BTLessStrategyNumber
Definition: stratnum.h:29
#define BTEqualStrategyNumber
Definition: stratnum.h:31
static TupleTableSlot* MJFillInner ( MergeJoinState node)
static

Definition at line 483 of file nodeMergejoin.c.

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

Referenced by ExecMergeJoin().

484 {
485  ExprContext *econtext = node->js.ps.ps_ExprContext;
486  ExprState *otherqual = node->js.ps.qual;
487 
488  ResetExprContext(econtext);
489 
490  econtext->ecxt_outertuple = node->mj_NullOuterTupleSlot;
491  econtext->ecxt_innertuple = node->mj_InnerTupleSlot;
492 
493  if (ExecQual(otherqual, econtext))
494  {
495  /*
496  * qualification succeeded. now form the desired projection tuple and
497  * return the slot containing it.
498  */
499  MJ_printf("ExecMergeJoin: returning inner fill tuple\n");
500 
501  return ExecProject(node->js.ps.ps_ProjInfo);
502  }
503  else
504  InstrCountFiltered2(node, 1);
505 
506  return NULL;
507 }
ProjectionInfo * ps_ProjInfo
Definition: execnodes.h:844
PlanState ps
Definition: execnodes.h:1548
ExprContext * ps_ExprContext
Definition: execnodes.h:843
#define MJ_printf(s)
Definition: execdebug.h:118
static bool ExecQual(ExprState *state, ExprContext *econtext)
Definition: executor.h:346
TupleTableSlot * ecxt_innertuple
Definition: execnodes.h:198
JoinState js
Definition: execnodes.h:1598
TupleTableSlot * ecxt_outertuple
Definition: execnodes.h:199
#define NULL
Definition: c.h:229
TupleTableSlot * mj_InnerTupleSlot
Definition: execnodes.h:1610
#define InstrCountFiltered2(node, delta)
Definition: execnodes.h:863
TupleTableSlot * mj_NullOuterTupleSlot
Definition: execnodes.h:1612
ExprState * qual
Definition: execnodes.h:827
static TupleTableSlot * ExecProject(ProjectionInfo *projInfo)
Definition: executor.h:309
#define ResetExprContext(econtext)
Definition: executor.h:450
static TupleTableSlot* MJFillOuter ( MergeJoinState node)
static

Definition at line 452 of file nodeMergejoin.c.

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

Referenced by ExecMergeJoin().

453 {
454  ExprContext *econtext = node->js.ps.ps_ExprContext;
455  ExprState *otherqual = node->js.ps.qual;
456 
457  ResetExprContext(econtext);
458 
459  econtext->ecxt_outertuple = node->mj_OuterTupleSlot;
460  econtext->ecxt_innertuple = node->mj_NullInnerTupleSlot;
461 
462  if (ExecQual(otherqual, econtext))
463  {
464  /*
465  * qualification succeeded. now form the desired projection tuple and
466  * return the slot containing it.
467  */
468  MJ_printf("ExecMergeJoin: returning outer fill tuple\n");
469 
470  return ExecProject(node->js.ps.ps_ProjInfo);
471  }
472  else
473  InstrCountFiltered2(node, 1);
474 
475  return NULL;
476 }
ProjectionInfo * ps_ProjInfo
Definition: execnodes.h:844
PlanState ps
Definition: execnodes.h:1548
ExprContext * ps_ExprContext
Definition: execnodes.h:843
#define MJ_printf(s)
Definition: execdebug.h:118
TupleTableSlot * mj_NullInnerTupleSlot
Definition: execnodes.h:1613
static bool ExecQual(ExprState *state, ExprContext *econtext)
Definition: executor.h:346
TupleTableSlot * ecxt_innertuple
Definition: execnodes.h:198
JoinState js
Definition: execnodes.h:1598
TupleTableSlot * ecxt_outertuple
Definition: execnodes.h:199
#define NULL
Definition: c.h:229
#define InstrCountFiltered2(node, delta)
Definition: execnodes.h:863
ExprState * qual
Definition: execnodes.h:827
static TupleTableSlot * ExecProject(ProjectionInfo *projInfo)
Definition: executor.h:309
#define ResetExprContext(econtext)
Definition: executor.h:450
TupleTableSlot * mj_OuterTupleSlot
Definition: execnodes.h:1609