aboutsummaryrefslogtreecommitdiff
path: root/src/backend/executor
diff options
context:
space:
mode:
Diffstat (limited to 'src/backend/executor')
-rw-r--r--src/backend/executor/execMain.c27
-rw-r--r--src/backend/executor/execTuples.c73
-rw-r--r--src/backend/executor/execUtils.c46
-rw-r--r--src/backend/executor/nodeHashjoin.c144
-rw-r--r--src/backend/executor/nodeMergejoin.c1010
-rw-r--r--src/backend/executor/nodeNestloop.c194
6 files changed, 953 insertions, 541 deletions
diff --git a/src/backend/executor/execMain.c b/src/backend/executor/execMain.c
index d25530b44fb..d46e0d30f55 100644
--- a/src/backend/executor/execMain.c
+++ b/src/backend/executor/execMain.c
@@ -27,7 +27,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/executor/execMain.c,v 1.126 2000/09/12 04:49:08 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/executor/execMain.c,v 1.127 2000/09/12 21:06:48 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -399,16 +399,17 @@ ExecCheckQueryPerms(CmdType operation, Query *parseTree, Plan *plan)
* If we have a result relation, determine whether the result rel is
* scanned or merely written. If scanned, we will insist on read
* permission as well as modify permission.
+ *
+ * Note: it might look faster to apply rangeTableEntry_used(), but
+ * that's not correct since it will trigger on jointree references
+ * to the RTE. We only want to know about actual Var nodes.
*/
if (resultRelation > 0)
{
- List *qvars = pull_varnos(parseTree->qual);
- List *tvars = pull_varnos((Node *) parseTree->targetList);
+ List *qvars = pull_varnos((Node *) parseTree);
- resultIsScanned = (intMember(resultRelation, qvars) ||
- intMember(resultRelation, tvars));
+ resultIsScanned = intMember(resultRelation, qvars);
freeList(qvars);
- freeList(tvars);
}
/*
@@ -571,8 +572,8 @@ ExecCheckRTEPerms(RangeTblEntry *rte, CmdType operation,
bool isResultRelation, bool resultIsScanned)
{
char *relName;
+ Oid userid;
int32 aclcheck_result;
- Oid userid;
if (rte->skipAcl)
{
@@ -703,13 +704,11 @@ InitPlan(CmdType operation, Query *parseTree, Plan *plan, EState *estate)
*/
RelationInfo *resultRelationInfo;
Index resultRelationIndex;
- RangeTblEntry *rtentry;
Oid resultRelationOid;
Relation resultRelationDesc;
resultRelationIndex = resultRelation;
- rtentry = rt_fetch(resultRelationIndex, rangeTable);
- resultRelationOid = rtentry->relid;
+ resultRelationOid = getrelid(resultRelationIndex, rangeTable);
resultRelationDesc = heap_open(resultRelationOid, RowExclusiveLock);
if (resultRelationDesc->rd_rel->relkind == RELKIND_SEQUENCE)
@@ -770,7 +769,7 @@ InitPlan(CmdType operation, Query *parseTree, Plan *plan, EState *estate)
if (!(rm->info & ROW_MARK_FOR_UPDATE))
continue;
- relid = rt_fetch(rm->rti, rangeTable)->relid;
+ relid = getrelid(rm->rti, rangeTable);
relation = heap_open(relid, RowShareLock);
erm = (execRowMark *) palloc(sizeof(execRowMark));
erm->relation = relation;
@@ -1623,10 +1622,10 @@ ExecRelCheck(Relation rel, TupleTableSlot *slot, EState *estate)
rte = makeNode(RangeTblEntry);
rte->relname = RelationGetRelationName(rel);
- rte->ref = makeNode(Attr);
- rte->ref->relname = rte->relname;
rte->relid = RelationGetRelid(rel);
- /* inh, inFromCl, inJoinSet, skipAcl won't be used, leave them zero */
+ rte->eref = makeNode(Attr);
+ rte->eref->relname = rte->relname;
+ /* inh, inFromCl, skipAcl won't be used, leave them zero */
/* Set up single-entry range table */
econtext->ecxt_range_table = lcons(rte, NIL);
diff --git a/src/backend/executor/execTuples.c b/src/backend/executor/execTuples.c
index 37b092fc20f..05474bc64bc 100644
--- a/src/backend/executor/execTuples.c
+++ b/src/backend/executor/execTuples.c
@@ -15,7 +15,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/executor/execTuples.c,v 1.38 2000/07/12 02:37:02 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/executor/execTuples.c,v 1.39 2000/09/12 21:06:48 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -46,11 +46,10 @@
* type of tuple in a slot
*
* CONVENIENCE INITIALIZATION ROUTINES
- * ExecInitResultTupleSlot \ convience routines to initialize
+ * ExecInitResultTupleSlot \ convenience routines to initialize
* ExecInitScanTupleSlot \ the various tuple slots for nodes
- * ExecInitMarkedTupleSlot / which store copies of tuples.
- * ExecInitOuterTupleSlot /
- * ExecInitHashTupleSlot /
+ * ExecInitExtraTupleSlot / which store copies of tuples.
+ * ExecInitNullTupleSlot /
*
* old routines:
* ExecGetTupType - get type of tuple returned by this node
@@ -560,10 +559,11 @@ ExecSlotDescriptorIsNew(TupleTableSlot *slot) /* slot to inspect */
* ----------------------------------------------------------------
*/
/* --------------------------------
- * ExecInit{Result,Scan,Raw,Marked,Outer,Hash}TupleSlot
+ * ExecInit{Result,Scan,Extra}TupleSlot
*
- * These are convenience routines to initialize the specfied slot
- * in nodes inheriting the appropriate state.
+ * These are convenience routines to initialize the specified slot
+ * in nodes inheriting the appropriate state. ExecInitExtraTupleSlot
+ * is used for initializing special-purpose slots.
* --------------------------------
*/
#define INIT_SLOT_DEFS \
@@ -583,7 +583,7 @@ ExecInitResultTupleSlot(EState *estate, CommonState *commonstate)
{
INIT_SLOT_DEFS;
INIT_SLOT_ALLOC;
- commonstate->cs_ResultTupleSlot = (TupleTableSlot *) slot;
+ commonstate->cs_ResultTupleSlot = slot;
}
/* ----------------
@@ -595,50 +595,51 @@ ExecInitScanTupleSlot(EState *estate, CommonScanState *commonscanstate)
{
INIT_SLOT_DEFS;
INIT_SLOT_ALLOC;
- commonscanstate->css_ScanTupleSlot = (TupleTableSlot *) slot;
+ commonscanstate->css_ScanTupleSlot = slot;
}
-#ifdef NOT_USED
/* ----------------
- * ExecInitMarkedTupleSlot
+ * ExecInitExtraTupleSlot
* ----------------
*/
-void
-ExecInitMarkedTupleSlot(EState *estate, MergeJoinState *mergestate)
+TupleTableSlot *
+ExecInitExtraTupleSlot(EState *estate)
{
INIT_SLOT_DEFS;
INIT_SLOT_ALLOC;
- mergestate->mj_MarkedTupleSlot = (TupleTableSlot *) slot;
+ return slot;
}
-#endif
-
/* ----------------
- * ExecInitOuterTupleSlot
+ * ExecInitNullTupleSlot
+ *
+ * Build a slot containing an all-nulls tuple of the given type.
+ * This is used as a substitute for an input tuple when performing an
+ * outer join.
* ----------------
*/
-void
-ExecInitOuterTupleSlot(EState *estate, HashJoinState *hashstate)
+TupleTableSlot *
+ExecInitNullTupleSlot(EState *estate, TupleDesc tupType)
{
- INIT_SLOT_DEFS;
- INIT_SLOT_ALLOC;
- hashstate->hj_OuterTupleSlot = slot;
-}
+ TupleTableSlot* slot = ExecInitExtraTupleSlot(estate);
+ /*
+ * Since heap_getattr() will treat attributes beyond a tuple's t_natts
+ * as being NULL, we can make an all-nulls tuple just by making it be of
+ * zero length. However, the slot descriptor must match the real tupType.
+ */
+ HeapTuple nullTuple;
+ Datum values[1];
+ char nulls[1];
+ static struct tupleDesc NullTupleDesc; /* we assume this inits to
+ * zeroes */
-/* ----------------
- * ExecInitHashTupleSlot
- * ----------------
- */
-#ifdef NOT_USED
-void
-ExecInitHashTupleSlot(EState *estate, HashJoinState *hashstate)
-{
- INIT_SLOT_DEFS;
- INIT_SLOT_ALLOC;
- hashstate->hj_HashTupleSlot = slot;
+ ExecSetSlotDescriptor(slot, tupType);
+
+ nullTuple = heap_formtuple(&NullTupleDesc, values, nulls);
+
+ return ExecStoreTuple(nullTuple, slot, InvalidBuffer, true);
}
-#endif
static TupleTableSlot *
NodeGetResultTupleSlot(Plan *node)
diff --git a/src/backend/executor/execUtils.c b/src/backend/executor/execUtils.c
index 63c1e9e157f..39ae7dff10a 100644
--- a/src/backend/executor/execUtils.c
+++ b/src/backend/executor/execUtils.c
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/executor/execUtils.c,v 1.65 2000/08/22 04:06:19 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/executor/execUtils.c,v 1.66 2000/09/12 21:06:48 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -275,53 +275,17 @@ void
ExecAssignResultTypeFromTL(Plan *node, CommonState *commonstate)
{
List *targetList;
- int i;
+ TupleDesc tupDesc;
int len;
- List *tl;
- TargetEntry *tle;
- List *fjtl;
- TupleDesc origTupDesc;
targetList = node->targetlist;
- origTupDesc = ExecTypeFromTL(targetList);
+ tupDesc = ExecTypeFromTL(targetList);
len = ExecTargetListLength(targetList);
- fjtl = NIL;
- tl = targetList;
- i = 0;
- while (tl != NIL || fjtl != NIL)
- {
- if (fjtl != NIL)
- {
- tle = lfirst(fjtl);
- fjtl = lnext(fjtl);
- }
- else
- {
- tle = lfirst(tl);
- tl = lnext(tl);
- }
-#ifdef SETS_FIXED
- if (!tl_is_resdom(tle))
- {
- Fjoin *fj = (Fjoin *) lfirst(tle);
-
- /* it is a FJoin */
- fjtl = lnext(tle);
- tle = fj->fj_innerNode;
- }
-#endif
- i++;
- }
-
if (len > 0)
- {
- ExecAssignResultType(commonstate,
- origTupDesc);
- }
+ ExecAssignResultType(commonstate, tupDesc);
else
- ExecAssignResultType(commonstate,
- (TupleDesc) NULL);
+ ExecAssignResultType(commonstate, (TupleDesc) NULL);
}
/* ----------------
diff --git a/src/backend/executor/nodeHashjoin.c b/src/backend/executor/nodeHashjoin.c
index 4b3b4a82505..d0eef4380b7 100644
--- a/src/backend/executor/nodeHashjoin.c
+++ b/src/backend/executor/nodeHashjoin.c
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/executor/nodeHashjoin.c,v 1.33 2000/08/24 03:29:03 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/executor/nodeHashjoin.c,v 1.34 2000/09/12 21:06:48 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -50,7 +50,8 @@ ExecHashJoin(HashJoin *node)
Hash *hashNode;
List *hjclauses;
Expr *clause;
- List *qual;
+ List *joinqual;
+ List *otherqual;
ScanDirection dir;
TupleTableSlot *inntuple;
Node *outerVar;
@@ -70,11 +71,12 @@ ExecHashJoin(HashJoin *node)
hjstate = node->hashjoinstate;
hjclauses = node->hashclauses;
clause = lfirst(hjclauses);
- estate = node->join.state;
- qual = node->join.qual;
+ estate = node->join.plan.state;
+ joinqual = node->join.joinqual;
+ otherqual = node->join.plan.qual;
hashNode = (Hash *) innerPlan(node);
outerNode = outerPlan(node);
- hashPhaseDone = node->hashdone;
+ hashPhaseDone = hjstate->hj_hashdone;
dir = estate->es_direction;
/* -----------------
@@ -132,7 +134,7 @@ ExecHashJoin(HashJoin *node)
hashNode->hashstate->hashtable = hashtable;
innerTupleSlot = ExecProcNode((Plan *) hashNode, (Plan *) node);
}
- node->hashdone = true;
+ hjstate->hj_hashdone = true;
/* ----------------
* Open temp files for outer batches, if needed.
* Note that file buffers are palloc'd in regular executor context.
@@ -153,11 +155,10 @@ ExecHashJoin(HashJoin *node)
for (;;)
{
-
/*
- * if the current outer tuple is nil, get a new one
+ * If we don't have an outer tuple, get the next one
*/
- if (TupIsNull(outerTupleSlot))
+ if (hjstate->hj_NeedNewOuter)
{
outerTupleSlot = ExecHashJoinOuterGetTuple(outerNode,
(Plan *) node,
@@ -173,11 +174,15 @@ ExecHashJoin(HashJoin *node)
return NULL;
}
+ hjstate->jstate.cs_OuterTupleSlot = outerTupleSlot;
+ econtext->ecxt_outertuple = outerTupleSlot;
+ hjstate->hj_NeedNewOuter = false;
+ hjstate->hj_MatchedOuter = false;
+
/*
* now we have an outer tuple, find the corresponding bucket
* for this tuple from the hash table
*/
- econtext->ecxt_outertuple = outerTupleSlot;
hjstate->hj_CurBucketNo = ExecHashGetBucket(hashtable, econtext,
outerVar);
hjstate->hj_CurTuple = NULL;
@@ -205,7 +210,7 @@ ExecHashJoin(HashJoin *node)
hashtable->outerBatchSize[batchno]++;
ExecHashJoinSaveTuple(outerTupleSlot->val,
hashtable->outerBatchFile[batchno]);
- ExecClearTuple(outerTupleSlot);
+ hjstate->hj_NeedNewOuter = true;
continue; /* loop around for a new outer tuple */
}
}
@@ -223,7 +228,7 @@ ExecHashJoin(HashJoin *node)
break; /* out of matches */
/*
- * we've got a match, but still need to test qpqual
+ * we've got a match, but still need to test non-hashed quals
*/
inntuple = ExecStoreTuple(curtuple,
hjstate->hj_HashTupleSlot,
@@ -231,35 +236,77 @@ ExecHashJoin(HashJoin *node)
false); /* don't pfree this tuple */
econtext->ecxt_innertuple = inntuple;
- /* reset temp memory each time to avoid leaks from qpqual */
+ /* reset temp memory each time to avoid leaks from qual expr */
ResetExprContext(econtext);
/* ----------------
* if we pass the qual, then save state for next call and
* have ExecProject form the projection, store it
* in the tuple table, and return the slot.
+ *
+ * Only the joinquals determine MatchedOuter status,
+ * but all quals must pass to actually return the tuple.
* ----------------
*/
- if (ExecQual(qual, econtext, false))
+ if (ExecQual(joinqual, econtext, false))
{
- TupleTableSlot *result;
+ hjstate->hj_MatchedOuter = true;
- hjstate->jstate.cs_OuterTupleSlot = outerTupleSlot;
- result = ExecProject(hjstate->jstate.cs_ProjInfo, &isDone);
- if (isDone != ExprEndResult)
+ if (otherqual == NIL || ExecQual(otherqual, econtext, false))
{
- hjstate->jstate.cs_TupFromTlist = (isDone == ExprMultipleResult);
- return result;
+ TupleTableSlot *result;
+
+ result = ExecProject(hjstate->jstate.cs_ProjInfo, &isDone);
+
+ if (isDone != ExprEndResult)
+ {
+ hjstate->jstate.cs_TupFromTlist =
+ (isDone == ExprMultipleResult);
+ return result;
+ }
}
}
}
/* ----------------
* Now the current outer tuple has run out of matches,
- * so we free it and loop around to get a new outer tuple.
+ * so check whether to emit a dummy outer-join tuple.
+ * If not, loop around to get a new outer tuple.
* ----------------
*/
- ExecClearTuple(outerTupleSlot);
+ hjstate->hj_NeedNewOuter = true;
+
+ if (! hjstate->hj_MatchedOuter &&
+ node->join.jointype == JOIN_LEFT)
+ {
+ /*
+ * We are doing an outer join and there were no join matches
+ * for this outer tuple. Generate a fake join tuple with
+ * nulls for the inner tuple, and return it if it passes
+ * the non-join quals.
+ */
+ econtext->ecxt_innertuple = hjstate->hj_NullInnerTupleSlot;
+
+ if (ExecQual(otherqual, econtext, false))
+ {
+ /* ----------------
+ * qualification was satisfied so we project and
+ * return the slot containing the result tuple
+ * using ExecProject().
+ * ----------------
+ */
+ TupleTableSlot *result;
+
+ result = ExecProject(hjstate->jstate.cs_ProjInfo, &isDone);
+
+ if (isDone != ExprEndResult)
+ {
+ hjstate->jstate.cs_TupFromTlist =
+ (isDone == ExprMultipleResult);
+ return result;
+ }
+ }
+ }
}
}
@@ -280,14 +327,13 @@ ExecInitHashJoin(HashJoin *node, EState *estate, Plan *parent)
* assign the node's execution state
* ----------------
*/
- node->join.state = estate;
+ node->join.plan.state = estate;
/* ----------------
* create state structure
* ----------------
*/
hjstate = makeNode(HashJoinState);
-
node->hashjoinstate = hjstate;
/* ----------------
@@ -298,14 +344,6 @@ ExecInitHashJoin(HashJoin *node, EState *estate, Plan *parent)
*/
ExecAssignExprContext(estate, &hjstate->jstate);
-#define HASHJOIN_NSLOTS 2
- /* ----------------
- * tuple table initialization
- * ----------------
- */
- ExecInitResultTupleSlot(estate, &hjstate->jstate);
- ExecInitOuterTupleSlot(estate, hjstate);
-
/* ----------------
* initializes child nodes
* ----------------
@@ -316,6 +354,28 @@ ExecInitHashJoin(HashJoin *node, EState *estate, Plan *parent)
ExecInitNode(outerNode, estate, (Plan *) node);
ExecInitNode((Plan *) hashNode, estate, (Plan *) node);
+#define HASHJOIN_NSLOTS 3
+ /* ----------------
+ * tuple table initialization
+ * ----------------
+ */
+ ExecInitResultTupleSlot(estate, &hjstate->jstate);
+ hjstate->hj_OuterTupleSlot = ExecInitExtraTupleSlot(estate);
+
+ switch (node->join.jointype)
+ {
+ case JOIN_INNER:
+ break;
+ case JOIN_LEFT:
+ hjstate->hj_NullInnerTupleSlot =
+ ExecInitNullTupleSlot(estate,
+ ExecGetTupType((Plan *) hashNode));
+ break;
+ default:
+ elog(ERROR, "ExecInitHashJoin: unsupported join type %d",
+ (int) node->join.jointype);
+ }
+
/* ----------------
* now for some voodoo. our temporary tuple slot
* is actually the result tuple slot of the Hash node
@@ -331,11 +391,6 @@ ExecInitHashJoin(HashJoin *node, EState *estate, Plan *parent)
hjstate->hj_HashTupleSlot = slot;
}
- hjstate->hj_OuterTupleSlot->ttc_tupleDescriptor = ExecGetTupType(outerNode);
-
-/*
- hjstate->hj_OuterTupleSlot->ttc_execTupDescriptor = ExecGetExecTupDesc(outerNode);
-*/
/* ----------------
* initialize tuple type and projection info
@@ -344,20 +399,25 @@ ExecInitHashJoin(HashJoin *node, EState *estate, Plan *parent)
ExecAssignResultTypeFromTL((Plan *) node, &hjstate->jstate);
ExecAssignProjectionInfo((Plan *) node, &hjstate->jstate);
+ ExecSetSlotDescriptor(hjstate->hj_OuterTupleSlot,
+ ExecGetTupType(outerNode));
+
/* ----------------
* initialize hash-specific info
* ----------------
*/
- node->hashdone = false;
+ hjstate->hj_hashdone = false;
hjstate->hj_HashTable = (HashJoinTable) NULL;
hjstate->hj_CurBucketNo = 0;
hjstate->hj_CurTuple = (HashJoinTuple) NULL;
hjstate->hj_InnerHashKey = (Node *) NULL;
- hjstate->jstate.cs_OuterTupleSlot = (TupleTableSlot *) NULL;
+ hjstate->jstate.cs_OuterTupleSlot = NULL;
hjstate->jstate.cs_TupFromTlist = false;
+ hjstate->hj_NeedNewOuter = true;
+ hjstate->hj_MatchedOuter = false;
return TRUE;
}
@@ -646,10 +706,10 @@ ExecReScanHashJoin(HashJoin *node, ExprContext *exprCtxt, Plan *parent)
{
HashJoinState *hjstate = node->hashjoinstate;
- if (!node->hashdone)
+ if (!hjstate->hj_hashdone)
return;
- node->hashdone = false;
+ hjstate->hj_hashdone = false;
/*
* Unfortunately, currently we have to destroy hashtable in all
@@ -667,6 +727,8 @@ ExecReScanHashJoin(HashJoin *node, ExprContext *exprCtxt, Plan *parent)
hjstate->jstate.cs_OuterTupleSlot = (TupleTableSlot *) NULL;
hjstate->jstate.cs_TupFromTlist = false;
+ hjstate->hj_NeedNewOuter = true;
+ hjstate->hj_MatchedOuter = false;
/*
* if chgParam of subnodes is not null then plans will be re-scanned
diff --git a/src/backend/executor/nodeMergejoin.c b/src/backend/executor/nodeMergejoin.c
index 5a2f45028a0..9d4ca0a8d54 100644
--- a/src/backend/executor/nodeMergejoin.c
+++ b/src/backend/executor/nodeMergejoin.c
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/executor/nodeMergejoin.c,v 1.37 2000/08/24 03:29:03 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/executor/nodeMergejoin.c,v 1.38 2000/09/12 21:06:48 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -16,11 +16,10 @@
* INTERFACE ROUTINES
* ExecMergeJoin mergejoin outer and inner relations.
* ExecInitMergeJoin creates and initializes run time states
- * ExecEndMergeJoin cleand up the node.
+ * ExecEndMergeJoin cleans up the node.
*
* NOTES
* Essential operation of the merge join algorithm is as follows:
- * (** indicates the tuples satisfy the merge clause).
*
* Join { -
* get initial outer and inner tuples INITIALIZE
@@ -42,19 +41,19 @@
* } -
* } -
*
- * Skip Outer { SKIPOUTER
+ * Skip Outer { SKIPOUTER_BEGIN
* if (inner == outer) Join Tuples JOINTUPLES
- * while (outer < inner) SKIPOUTER
- * advance outer SKIPOUTER
- * if (outer > inner) SKIPOUTER
+ * while (outer < inner) SKIPOUTER_TEST
+ * advance outer SKIPOUTER_ADVANCE
+ * if (outer > inner) SKIPOUTER_TEST
* Skip Inner SKIPINNER
* } -
*
- * Skip Inner { SKIPINNER
+ * Skip Inner { SKIPINNER_BEGIN
* if (inner == outer) Join Tuples JOINTUPLES
- * while (outer > inner) SKIPINNER
- * advance inner SKIPINNER
- * if (outer < inner) SKIPINNER
+ * while (outer > inner) SKIPINNER_TEST
+ * advance inner SKIPINNER_ADVANCE
+ * if (outer < inner) SKIPINNER_TEST
* Skip Outer SKIPOUTER
* } -
*
@@ -68,6 +67,7 @@
#include "postgres.h"
#include "access/heapam.h"
+#include "access/printtup.h"
#include "catalog/pg_operator.h"
#include "executor/execdebug.h"
#include "executor/execdefs.h"
@@ -273,52 +273,39 @@ MergeCompare(List *eqQual, List *compareQual, ExprContext *econtext)
* ----------------------------------------------------------------
*/
#ifdef EXEC_MERGEJOINDEBUG
-void
- ExecMergeTupleDumpInner(ExprContext *econtext);
-void
-ExecMergeTupleDumpInner(ExprContext *econtext)
+static void
+ExecMergeTupleDumpOuter(MergeJoinState *mergestate)
{
- TupleTableSlot *innerSlot;
+ TupleTableSlot *outerSlot = mergestate->mj_OuterTupleSlot;
- printf("==== inner tuple ====\n");
- innerSlot = econtext->ecxt_innertuple;
- if (TupIsNull(innerSlot))
+ printf("==== outer tuple ====\n");
+ if (TupIsNull(outerSlot))
printf("(nil)\n");
else
- MJ_debugtup(innerSlot->val,
- innerSlot->ttc_tupleDescriptor);
+ MJ_debugtup(outerSlot->val,
+ outerSlot->ttc_tupleDescriptor);
}
-void
- ExecMergeTupleDumpOuter(ExprContext *econtext);
-
-void
-ExecMergeTupleDumpOuter(ExprContext *econtext)
+static void
+ExecMergeTupleDumpInner(MergeJoinState *mergestate)
{
- TupleTableSlot *outerSlot;
+ TupleTableSlot *innerSlot = mergestate->mj_InnerTupleSlot;
- printf("==== outer tuple ====\n");
- outerSlot = econtext->ecxt_outertuple;
- if (TupIsNull(outerSlot))
+ printf("==== inner tuple ====\n");
+ if (TupIsNull(innerSlot))
printf("(nil)\n");
else
- MJ_debugtup(outerSlot->val,
- outerSlot->ttc_tupleDescriptor);
+ MJ_debugtup(innerSlot->val,
+ innerSlot->ttc_tupleDescriptor);
}
-void ExecMergeTupleDumpMarked(ExprContext *econtext,
- MergeJoinState *mergestate);
-
-void
-ExecMergeTupleDumpMarked(ExprContext *econtext,
- MergeJoinState *mergestate)
+static void
+ExecMergeTupleDumpMarked(MergeJoinState *mergestate)
{
- TupleTableSlot *markedSlot;
+ TupleTableSlot *markedSlot = mergestate->mj_MarkedTupleSlot;
printf("==== marked tuple ====\n");
- markedSlot = mergestate->mj_MarkedTupleSlot;
-
if (TupIsNull(markedSlot))
printf("(nil)\n");
else
@@ -326,17 +313,14 @@ ExecMergeTupleDumpMarked(ExprContext *econtext,
markedSlot->ttc_tupleDescriptor);
}
-void
- ExecMergeTupleDump(ExprContext *econtext, MergeJoinState *mergestate);
-
-void
-ExecMergeTupleDump(ExprContext *econtext, MergeJoinState *mergestate)
+static void
+ExecMergeTupleDump(MergeJoinState *mergestate)
{
printf("******** ExecMergeTupleDump ********\n");
- ExecMergeTupleDumpInner(econtext);
- ExecMergeTupleDumpOuter(econtext);
- ExecMergeTupleDumpMarked(econtext, mergestate);
+ ExecMergeTupleDumpOuter(mergestate);
+ ExecMergeTupleDumpInner(mergestate);
+ ExecMergeTupleDumpMarked(mergestate);
printf("******** \n");
}
@@ -404,7 +388,8 @@ ExecMergeJoin(MergeJoin *node)
List *innerSkipQual;
List *outerSkipQual;
List *mergeclauses;
- List *qual;
+ List *joinqual;
+ List *otherqual;
bool qualResult;
bool compareResult;
Plan *innerPlan;
@@ -412,27 +397,48 @@ ExecMergeJoin(MergeJoin *node)
Plan *outerPlan;
TupleTableSlot *outerTupleSlot;
ExprContext *econtext;
-#ifdef ENABLE_OUTER_JOINS
- /*
- * These should be set from the expression context! - thomas
- * 1999-02-20
- */
- static bool isLeftJoin = true;
- static bool isRightJoin = false;
-#endif
+ bool doFillOuter;
+ bool doFillInner;
/* ----------------
* get information from node
* ----------------
*/
mergestate = node->mergestate;
- estate = node->join.state;
+ estate = node->join.plan.state;
direction = estate->es_direction;
innerPlan = innerPlan((Plan *) node);
outerPlan = outerPlan((Plan *) node);
econtext = mergestate->jstate.cs_ExprContext;
mergeclauses = node->mergeclauses;
- qual = node->join.qual;
+ joinqual = node->join.joinqual;
+ otherqual = node->join.plan.qual;
+
+ switch (node->join.jointype)
+ {
+ case JOIN_INNER:
+ doFillOuter = false;
+ doFillInner = false;
+ break;
+ case JOIN_LEFT:
+ doFillOuter = true;
+ doFillInner = false;
+ break;
+ case JOIN_FULL:
+ doFillOuter = true;
+ doFillInner = true;
+ break;
+ case JOIN_RIGHT:
+ doFillOuter = false;
+ doFillInner = true;
+ break;
+ default:
+ elog(ERROR, "ExecMergeJoin: unsupported join type %d",
+ (int) node->join.jointype);
+ doFillOuter = false; /* keep compiler quiet */
+ doFillInner = false;
+ break;
+ }
if (ScanDirectionIsForward(direction))
{
@@ -483,7 +489,7 @@ ExecMergeJoin(MergeJoin *node)
* improved readability.
* ----------------
*/
- MJ_dump(econtext, mergestate);
+ MJ_dump(mergestate);
switch (mergestate->mj_JoinState)
{
@@ -491,46 +497,60 @@ ExecMergeJoin(MergeJoin *node)
/*
* EXEC_MJ_INITIALIZE means that this is the first time
* ExecMergeJoin() has been called and so we have to
- * initialize the inner, outer and marked tuples as well
- * as various stuff in the expression context.
+ * fetch the first tuple for both outer and inner subplans.
+ * If we fail to get a tuple here, then that subplan is
+ * empty, and we either end the join or go to one of the
+ * fill-remaining-tuples states.
*/
case EXEC_MJ_INITIALIZE:
MJ_printf("ExecMergeJoin: EXEC_MJ_INITIALIZE\n");
- /*
- * Note: at this point, if either of our inner or outer
- * tuples are nil, then the join ends immediately because
- * we know one of the subplans is empty.
- */
- innerTupleSlot = ExecProcNode(innerPlan, (Plan *) node);
- if (TupIsNull(innerTupleSlot))
+ outerTupleSlot = ExecProcNode(outerPlan, (Plan *) node);
+ mergestate->mj_OuterTupleSlot = outerTupleSlot;
+ if (TupIsNull(outerTupleSlot))
{
- MJ_printf("ExecMergeJoin: **** inner tuple is nil ****\n");
+ MJ_printf("ExecMergeJoin: outer subplan is empty\n");
+ if (doFillInner)
+ {
+ /*
+ * Need to emit right-join tuples for remaining
+ * inner tuples. We set MatchedInner = true to
+ * force the ENDOUTER state to advance inner.
+ */
+ mergestate->mj_JoinState = EXEC_MJ_ENDOUTER;
+ mergestate->mj_MatchedInner = true;
+ break;
+ }
+ /* Otherwise we're done. */
return NULL;
}
- outerTupleSlot = ExecProcNode(outerPlan, (Plan *) node);
- if (TupIsNull(outerTupleSlot))
+ innerTupleSlot = ExecProcNode(innerPlan, (Plan *) node);
+ mergestate->mj_InnerTupleSlot = innerTupleSlot;
+ if (TupIsNull(innerTupleSlot))
{
- MJ_printf("ExecMergeJoin: **** outer tuple is nil ****\n");
+ MJ_printf("ExecMergeJoin: inner subplan is empty\n");
+ if (doFillOuter)
+ {
+ /*
+ * Need to emit left-join tuples for remaining
+ * outer tuples. We set MatchedOuter = true to
+ * force the ENDINNER state to advance outer.
+ */
+ mergestate->mj_JoinState = EXEC_MJ_ENDINNER;
+ mergestate->mj_MatchedOuter = true;
+ break;
+ }
+ /* Otherwise we're done. */
return NULL;
}
/* ----------------
- * store the inner and outer tuple in the merge state
+ * OK, we have the initial tuples. Begin by skipping
+ * unmatched inner tuples.
* ----------------
*/
- econtext->ecxt_innertuple = innerTupleSlot;
- econtext->ecxt_outertuple = outerTupleSlot;
-
- mergestate->mj_MarkedTupleSlot->ttc_tupleDescriptor =
- innerTupleSlot->ttc_tupleDescriptor;
-
- /* ----------------
- * initialize merge join state to skip inner tuples.
- * ----------------
- */
- mergestate->mj_JoinState = EXEC_MJ_SKIPINNER;
+ mergestate->mj_JoinState = EXEC_MJ_SKIPINNER_BEGIN;
break;
/*
@@ -541,9 +561,10 @@ ExecMergeJoin(MergeJoin *node)
*/
case EXEC_MJ_JOINMARK:
MJ_printf("ExecMergeJoin: EXEC_MJ_JOINMARK\n");
+
ExecMarkPos(innerPlan);
- MarkInnerTuple(econtext->ecxt_innertuple, mergestate);
+ MarkInnerTuple(mergestate->mj_InnerTupleSlot, mergestate);
mergestate->mj_JoinState = EXEC_MJ_JOINTEST;
break;
@@ -562,7 +583,12 @@ ExecMergeJoin(MergeJoin *node)
ResetExprContext(econtext);
- qualResult = ExecQual((List *) mergeclauses, econtext, false);
+ outerTupleSlot = mergestate->mj_OuterTupleSlot;
+ econtext->ecxt_outertuple = outerTupleSlot;
+ innerTupleSlot = mergestate->mj_InnerTupleSlot;
+ econtext->ecxt_innertuple = innerTupleSlot;
+
+ qualResult = ExecQual(mergeclauses, econtext, false);
MJ_DEBUG_QUAL(mergeclauses, qualResult);
if (qualResult)
@@ -578,38 +604,57 @@ ExecMergeJoin(MergeJoin *node)
*/
case EXEC_MJ_JOINTUPLES:
MJ_printf("ExecMergeJoin: EXEC_MJ_JOINTUPLES\n");
+
mergestate->mj_JoinState = EXEC_MJ_NEXTINNER;
/*
- * Check the qpqual to see if we actually want to return
- * this join tuple. If not, can proceed with merge.
+ * Check the extra qual conditions to see if we actually
+ * want to return this join tuple. If not, can proceed with
+ * merge. We must distinguish the additional joinquals
+ * (which must pass to consider the tuples "matched" for
+ * outer-join logic) from the otherquals (which must pass
+ * before we actually return the tuple).
*
- * (We don't bother with a ResetExprContext here, on the
+ * We don't bother with a ResetExprContext here, on the
* assumption that we just did one before checking the merge
- * qual. One per tuple should be sufficient.)
+ * qual. One per tuple should be sufficient. Also, the
+ * econtext's tuple pointers were set up before checking
+ * the merge qual, so we needn't do it again.
*/
- qualResult = ExecQual((List *) qual, econtext, false);
- MJ_DEBUG_QUAL(qual, qualResult);
+ qualResult = (joinqual == NIL ||
+ ExecQual(joinqual, econtext, false));
+ MJ_DEBUG_QUAL(joinqual, qualResult);
if (qualResult)
{
- /* ----------------
- * qualification succeeded. now form the desired
- * projection tuple and return the slot containing it.
- * ----------------
- */
- TupleTableSlot *result;
- ExprDoneCond isDone;
+ mergestate->mj_MatchedOuter = true;
+ mergestate->mj_MatchedInner = true;
- MJ_printf("ExecMergeJoin: **** returning tuple ****\n");
+ qualResult = (otherqual == NIL ||
+ ExecQual(otherqual, econtext, false));
+ MJ_DEBUG_QUAL(otherqual, qualResult);
- result = ExecProject(mergestate->jstate.cs_ProjInfo,
- &isDone);
-
- if (isDone != ExprEndResult)
+ if (qualResult)
{
- mergestate->jstate.cs_TupFromTlist = (isDone == ExprMultipleResult);
- return result;
+ /* ----------------
+ * qualification succeeded. now form the desired
+ * projection tuple and return the slot containing it.
+ * ----------------
+ */
+ TupleTableSlot *result;
+ ExprDoneCond isDone;
+
+ MJ_printf("ExecMergeJoin: returning tuple\n");
+
+ result = ExecProject(mergestate->jstate.cs_ProjInfo,
+ &isDone);
+
+ if (isDone != ExprEndResult)
+ {
+ mergestate->jstate.cs_TupFromTlist =
+ (isDone == ExprMultipleResult);
+ return result;
+ }
}
}
break;
@@ -618,17 +663,60 @@ ExecMergeJoin(MergeJoin *node)
* EXEC_MJ_NEXTINNER means advance the inner scan to the
* next tuple. If the tuple is not nil, we then proceed to
* test it against the join qualification.
+ *
+ * Before advancing, we check to see if we must emit an
+ * outer-join fill tuple for this inner tuple.
*/
case EXEC_MJ_NEXTINNER:
MJ_printf("ExecMergeJoin: EXEC_MJ_NEXTINNER\n");
+ if (doFillInner && !mergestate->mj_MatchedInner)
+ {
+ /*
+ * Generate a fake join tuple with nulls for the outer
+ * tuple, and return it if it passes the non-join quals.
+ */
+ mergestate->mj_MatchedInner = true; /* do it only once */
+
+ ResetExprContext(econtext);
+
+ outerTupleSlot = mergestate->mj_NullOuterTupleSlot;
+ econtext->ecxt_outertuple = outerTupleSlot;
+ innerTupleSlot = mergestate->mj_InnerTupleSlot;
+ econtext->ecxt_innertuple = innerTupleSlot;
+
+ if (ExecQual(otherqual, econtext, false))
+ {
+ /* ----------------
+ * qualification succeeded. now form the desired
+ * projection tuple and return the slot containing it.
+ * ----------------
+ */
+ TupleTableSlot *result;
+ ExprDoneCond isDone;
+
+ MJ_printf("ExecMergeJoin: returning fill tuple\n");
+
+ result = ExecProject(mergestate->jstate.cs_ProjInfo,
+ &isDone);
+
+ if (isDone != ExprEndResult)
+ {
+ mergestate->jstate.cs_TupFromTlist =
+ (isDone == ExprMultipleResult);
+ return result;
+ }
+ }
+ }
+
/* ----------------
* now we get the next inner tuple, if any
* ----------------
*/
innerTupleSlot = ExecProcNode(innerPlan, (Plan *) node);
+ mergestate->mj_InnerTupleSlot = innerTupleSlot;
MJ_DEBUG_PROC_NODE(innerTupleSlot);
- econtext->ecxt_innertuple = innerTupleSlot;
+ mergestate->mj_MatchedInner = false;
if (TupIsNull(innerTupleSlot))
mergestate->mj_JoinState = EXEC_MJ_NEXTOUTER;
@@ -650,23 +738,81 @@ ExecMergeJoin(MergeJoin *node)
* so get a new outer tuple and then
* proceed to test it against the marked tuple
* (EXEC_MJ_TESTOUTER)
+ *
+ * Before advancing, we check to see if we must emit an
+ * outer-join fill tuple for this outer tuple.
*------------------------------------------------
*/
case EXEC_MJ_NEXTOUTER:
MJ_printf("ExecMergeJoin: EXEC_MJ_NEXTOUTER\n");
+ if (doFillOuter && !mergestate->mj_MatchedOuter)
+ {
+ /*
+ * Generate a fake join tuple with nulls for the inner
+ * tuple, and return it if it passes the non-join quals.
+ */
+ mergestate->mj_MatchedOuter = true; /* do it only once */
+
+ ResetExprContext(econtext);
+
+ outerTupleSlot = mergestate->mj_OuterTupleSlot;
+ econtext->ecxt_outertuple = outerTupleSlot;
+ innerTupleSlot = mergestate->mj_NullInnerTupleSlot;
+ econtext->ecxt_innertuple = innerTupleSlot;
+
+ if (ExecQual(otherqual, econtext, false))
+ {
+ /* ----------------
+ * qualification succeeded. now form the desired
+ * projection tuple and return the slot containing it.
+ * ----------------
+ */
+ TupleTableSlot *result;
+ ExprDoneCond isDone;
+
+ MJ_printf("ExecMergeJoin: returning fill tuple\n");
+
+ result = ExecProject(mergestate->jstate.cs_ProjInfo,
+ &isDone);
+
+ if (isDone != ExprEndResult)
+ {
+ mergestate->jstate.cs_TupFromTlist =
+ (isDone == ExprMultipleResult);
+ return result;
+ }
+ }
+ }
+
+ /* ----------------
+ * now we get the next outer tuple, if any
+ * ----------------
+ */
outerTupleSlot = ExecProcNode(outerPlan, (Plan *) node);
+ mergestate->mj_OuterTupleSlot = outerTupleSlot;
MJ_DEBUG_PROC_NODE(outerTupleSlot);
- econtext->ecxt_outertuple = outerTupleSlot;
+ mergestate->mj_MatchedOuter = false;
/* ----------------
- * if the outer tuple is null then we know
- * we are done with the join
+ * if the outer tuple is null then we are done with the
+ * join, unless we have inner tuples we need to null-fill.
* ----------------
*/
if (TupIsNull(outerTupleSlot))
{
- MJ_printf("ExecMergeJoin: **** outer tuple is nil ****\n");
+ MJ_printf("ExecMergeJoin: end of outer subplan\n");
+ innerTupleSlot = mergestate->mj_InnerTupleSlot;
+ if (doFillInner && !TupIsNull(innerTupleSlot))
+ {
+ /*
+ * Need to emit right-join tuples for remaining
+ * inner tuples.
+ */
+ mergestate->mj_JoinState = EXEC_MJ_ENDOUTER;
+ break;
+ }
+ /* Otherwise we're done. */
return NULL;
}
@@ -712,39 +858,45 @@ ExecMergeJoin(MergeJoin *node)
/* ----------------
* here we compare the outer tuple with the marked inner tuple
- * by using the marked tuple in place of the inner tuple.
* ----------------
*/
- innerTupleSlot = econtext->ecxt_innertuple;
- econtext->ecxt_innertuple = mergestate->mj_MarkedTupleSlot;
-
ResetExprContext(econtext);
- qualResult = ExecQual((List *) mergeclauses, econtext, false);
+ outerTupleSlot = mergestate->mj_OuterTupleSlot;
+ econtext->ecxt_outertuple = outerTupleSlot;
+ innerTupleSlot = mergestate->mj_MarkedTupleSlot;
+ econtext->ecxt_innertuple = innerTupleSlot;
+
+ qualResult = ExecQual(mergeclauses, econtext, false);
MJ_DEBUG_QUAL(mergeclauses, qualResult);
if (qualResult)
{
/*
- * the merge clause matched so now we juggle the slots
- * back the way they were and proceed to JOINTEST.
+ * the merge clause matched so now we restore the inner
+ * scan position to the first mark, and loop back to
+ * JOINTEST. Actually, since we know the mergeclause
+ * matches, we can skip JOINTEST and go straight to
+ * JOINTUPLES.
*
- * I can't understand why we have to go to JOINTEST and
- * compare outer tuple with the same inner one again
- * -> go to JOINTUPLES... - vadim 02/27/98
+ * NOTE: we do not need to worry about the MatchedInner
+ * state for the rescanned inner tuples. We know all
+ * of them will match this new outer tuple and therefore
+ * won't be emitted as fill tuples. This works *only*
+ * because we require the extra joinquals to be nil when
+ * doing a right or full join --- otherwise some of the
+ * rescanned tuples might fail the extra joinquals.
*/
-
ExecRestrPos(innerPlan);
mergestate->mj_JoinState = EXEC_MJ_JOINTUPLES;
}
else
{
- econtext->ecxt_innertuple = innerTupleSlot;
/* ----------------
* if the inner tuple was nil and the new outer
* tuple didn't match the marked outer tuple then
- * we may have the case:
+ * we have the case:
*
* outer inner
* 4 4 - marked tuple
@@ -753,31 +905,33 @@ ExecMergeJoin(MergeJoin *node)
* 7
*
* which means that all subsequent outer tuples will be
- * larger than our inner tuples.
+ * larger than our marked inner tuples. So we're done.
* ----------------
*/
+ innerTupleSlot = mergestate->mj_InnerTupleSlot;
if (TupIsNull(innerTupleSlot))
{
-#ifdef ENABLE_OUTER_JOINS
- if (isLeftJoin)
+ if (doFillOuter)
{
- /* continue on to null fill outer tuples */
- mergestate->mj_JoinState = EXEC_MJ_FILLOUTER;
+ /*
+ * Need to emit left-join tuples for remaining
+ * outer tuples.
+ */
+ mergestate->mj_JoinState = EXEC_MJ_ENDINNER;
break;
}
-#endif
- MJ_printf("ExecMergeJoin: **** weird case 1 ****\n");
+ /* Otherwise we're done. */
return NULL;
}
/* continue on to skip outer tuples */
- mergestate->mj_JoinState = EXEC_MJ_SKIPOUTER;
+ mergestate->mj_JoinState = EXEC_MJ_SKIPOUTER_BEGIN;
}
break;
/*----------------------------------------------------------
* EXEC_MJ_SKIPOUTER means skip over tuples in the outer plan
- * until we find an outer tuple > current inner tuple.
+ * until we find an outer tuple >= current inner tuple.
*
* For example:
*
@@ -790,10 +944,14 @@ ExecMergeJoin(MergeJoin *node)
*
* we have to advance the outer scan
* until we find the outer 8.
+ *
+ * To avoid redundant tests, we divide this into three
+ * sub-states: BEGIN, TEST, ADVANCE.
*----------------------------------------------------------
*/
- case EXEC_MJ_SKIPOUTER:
- MJ_printf("ExecMergeJoin: EXEC_MJ_SKIPOUTER\n");
+ case EXEC_MJ_SKIPOUTER_BEGIN:
+ MJ_printf("ExecMergeJoin: EXEC_MJ_SKIPOUTER_BEGIN\n");
+
/* ----------------
* before we advance, make sure the current tuples
* do not satisfy the mergeclauses. If they do, then
@@ -802,23 +960,39 @@ ExecMergeJoin(MergeJoin *node)
*/
ResetExprContext(econtext);
- qualResult = ExecQual((List *) mergeclauses, econtext, false);
+ outerTupleSlot = mergestate->mj_OuterTupleSlot;
+ econtext->ecxt_outertuple = outerTupleSlot;
+ innerTupleSlot = mergestate->mj_InnerTupleSlot;
+ econtext->ecxt_innertuple = innerTupleSlot;
+
+ qualResult = ExecQual(mergeclauses, econtext, false);
MJ_DEBUG_QUAL(mergeclauses, qualResult);
if (qualResult)
{
ExecMarkPos(innerPlan);
- MarkInnerTuple(econtext->ecxt_innertuple, mergestate);
+ MarkInnerTuple(innerTupleSlot, mergestate);
mergestate->mj_JoinState = EXEC_MJ_JOINTUPLES;
break;
}
+ mergestate->mj_JoinState = EXEC_MJ_SKIPOUTER_TEST;
+ break;
+
+ case EXEC_MJ_SKIPOUTER_TEST:
+ MJ_printf("ExecMergeJoin: EXEC_MJ_SKIPOUTER_TEST\n");
+
/* ----------------
* ok, now test the skip qualification
* ----------------
*/
+ outerTupleSlot = mergestate->mj_OuterTupleSlot;
+ econtext->ecxt_outertuple = outerTupleSlot;
+ innerTupleSlot = mergestate->mj_InnerTupleSlot;
+ econtext->ecxt_innertuple = innerTupleSlot;
+
compareResult = MergeCompare(mergeclauses,
outerSkipQual,
econtext);
@@ -827,42 +1001,12 @@ ExecMergeJoin(MergeJoin *node)
/* ----------------
* compareResult is true as long as we should
- * continue skipping tuples.
+ * continue skipping outer tuples.
* ----------------
*/
if (compareResult)
{
-#ifdef ENABLE_OUTER_JOINS
- /* ----------------
- * if this is a left or full outer join, then fill
- * ----------------
- */
- if (isLeftJoin)
- {
- mergestate->mj_JoinState = EXEC_MJ_FILLOUTER;
- break;
- }
-#endif
-
- outerTupleSlot = ExecProcNode(outerPlan, (Plan *) node);
- MJ_DEBUG_PROC_NODE(outerTupleSlot);
- econtext->ecxt_outertuple = outerTupleSlot;
-
- /* ----------------
- * if the outer tuple is null then we know
- * we are done with the join
- * ----------------
- */
- if (TupIsNull(outerTupleSlot))
- {
- MJ_printf("ExecMergeJoin: **** outerTuple is nil ****\n");
- return NULL;
- }
- /* ----------------
- * otherwise test the new tuple against the skip qual.
- * (we remain in the EXEC_MJ_SKIPOUTER state)
- * ----------------
- */
+ mergestate->mj_JoinState = EXEC_MJ_SKIPOUTER_ADVANCE;
break;
}
@@ -880,14 +1024,99 @@ ExecMergeJoin(MergeJoin *node)
MJ_DEBUG_MERGE_COMPARE(innerSkipQual, compareResult);
if (compareResult)
- mergestate->mj_JoinState = EXEC_MJ_SKIPINNER;
+ mergestate->mj_JoinState = EXEC_MJ_SKIPINNER_BEGIN;
else
mergestate->mj_JoinState = EXEC_MJ_JOINMARK;
break;
+ /*------------------------------------------------
+ * Before advancing, we check to see if we must emit an
+ * outer-join fill tuple for this outer tuple.
+ *------------------------------------------------
+ */
+ case EXEC_MJ_SKIPOUTER_ADVANCE:
+ MJ_printf("ExecMergeJoin: EXEC_MJ_SKIPOUTER_ADVANCE\n");
+
+ if (doFillOuter && !mergestate->mj_MatchedOuter)
+ {
+ /*
+ * Generate a fake join tuple with nulls for the inner
+ * tuple, and return it if it passes the non-join quals.
+ */
+ mergestate->mj_MatchedOuter = true; /* do it only once */
+
+ ResetExprContext(econtext);
+
+ outerTupleSlot = mergestate->mj_OuterTupleSlot;
+ econtext->ecxt_outertuple = outerTupleSlot;
+ innerTupleSlot = mergestate->mj_NullInnerTupleSlot;
+ econtext->ecxt_innertuple = innerTupleSlot;
+
+ if (ExecQual(otherqual, econtext, false))
+ {
+ /* ----------------
+ * qualification succeeded. now form the desired
+ * projection tuple and return the slot containing it.
+ * ----------------
+ */
+ TupleTableSlot *result;
+ ExprDoneCond isDone;
+
+ MJ_printf("ExecMergeJoin: returning fill tuple\n");
+
+ result = ExecProject(mergestate->jstate.cs_ProjInfo,
+ &isDone);
+
+ if (isDone != ExprEndResult)
+ {
+ mergestate->jstate.cs_TupFromTlist =
+ (isDone == ExprMultipleResult);
+ return result;
+ }
+ }
+ }
+
+ /* ----------------
+ * now we get the next outer tuple, if any
+ * ----------------
+ */
+ outerTupleSlot = ExecProcNode(outerPlan, (Plan *) node);
+ mergestate->mj_OuterTupleSlot = outerTupleSlot;
+ MJ_DEBUG_PROC_NODE(outerTupleSlot);
+ mergestate->mj_MatchedOuter = false;
+
+ /* ----------------
+ * if the outer tuple is null then we are done with the
+ * join, unless we have inner tuples we need to null-fill.
+ * ----------------
+ */
+ if (TupIsNull(outerTupleSlot))
+ {
+ MJ_printf("ExecMergeJoin: end of outer subplan\n");
+ innerTupleSlot = mergestate->mj_InnerTupleSlot;
+ if (doFillInner && !TupIsNull(innerTupleSlot))
+ {
+ /*
+ * Need to emit right-join tuples for remaining
+ * inner tuples.
+ */
+ mergestate->mj_JoinState = EXEC_MJ_ENDOUTER;
+ break;
+ }
+ /* Otherwise we're done. */
+ return NULL;
+ }
+
+ /* ----------------
+ * otherwise test the new tuple against the skip qual.
+ * ----------------
+ */
+ mergestate->mj_JoinState = EXEC_MJ_SKIPOUTER_TEST;
+ break;
+
/*-----------------------------------------------------------
* EXEC_MJ_SKIPINNER means skip over tuples in the inner plan
- * until we find an inner tuple > current outer tuple.
+ * until we find an inner tuple >= current outer tuple.
*
* For example:
*
@@ -901,10 +1130,13 @@ ExecMergeJoin(MergeJoin *node)
* we have to advance the inner scan
* until we find the inner 12.
*
+ * To avoid redundant tests, we divide this into three
+ * sub-states: BEGIN, TEST, ADVANCE.
*-------------------------------------------------------
*/
- case EXEC_MJ_SKIPINNER:
- MJ_printf("ExecMergeJoin: EXEC_MJ_SKIPINNER\n");
+ case EXEC_MJ_SKIPINNER_BEGIN:
+ MJ_printf("ExecMergeJoin: EXEC_MJ_SKIPINNER_BEGIN\n");
+
/* ----------------
* before we advance, make sure the current tuples
* do not satisfy the mergeclauses. If they do, then
@@ -913,23 +1145,39 @@ ExecMergeJoin(MergeJoin *node)
*/
ResetExprContext(econtext);
- qualResult = ExecQual((List *) mergeclauses, econtext, false);
+ outerTupleSlot = mergestate->mj_OuterTupleSlot;
+ econtext->ecxt_outertuple = outerTupleSlot;
+ innerTupleSlot = mergestate->mj_InnerTupleSlot;
+ econtext->ecxt_innertuple = innerTupleSlot;
+
+ qualResult = ExecQual(mergeclauses, econtext, false);
MJ_DEBUG_QUAL(mergeclauses, qualResult);
if (qualResult)
{
ExecMarkPos(innerPlan);
- MarkInnerTuple(econtext->ecxt_innertuple, mergestate);
+ MarkInnerTuple(innerTupleSlot, mergestate);
mergestate->mj_JoinState = EXEC_MJ_JOINTUPLES;
break;
}
+ mergestate->mj_JoinState = EXEC_MJ_SKIPINNER_TEST;
+ break;
+
+ case EXEC_MJ_SKIPINNER_TEST:
+ MJ_printf("ExecMergeJoin: EXEC_MJ_SKIPINNER_TEST\n");
+
/* ----------------
* ok, now test the skip qualification
* ----------------
*/
+ outerTupleSlot = mergestate->mj_OuterTupleSlot;
+ econtext->ecxt_outertuple = outerTupleSlot;
+ innerTupleSlot = mergestate->mj_InnerTupleSlot;
+ econtext->ecxt_innertuple = innerTupleSlot;
+
compareResult = MergeCompare(mergeclauses,
innerSkipQual,
econtext);
@@ -938,70 +1186,20 @@ ExecMergeJoin(MergeJoin *node)
/* ----------------
* compareResult is true as long as we should
- * continue skipping tuples.
+ * continue skipping inner tuples.
* ----------------
*/
if (compareResult)
{
-#ifdef ENABLE_OUTER_JOINS
- /* ----------------
- * if this is a right or full outer join, then fill
- * ----------------
- */
- if (isRightJoin)
- {
- mergestate->mj_JoinState = EXEC_MJ_FILLINNER;
- break;
- }
-#endif
-
- /* ----------------
- * now try and get a new inner tuple
- * ----------------
- */
- innerTupleSlot = ExecProcNode(innerPlan, (Plan *) node);
- MJ_DEBUG_PROC_NODE(innerTupleSlot);
- econtext->ecxt_innertuple = innerTupleSlot;
-
- /* ----------------
- * if the inner tuple is null then we know
- * we have to restore the inner scan
- * and advance to the next outer tuple
- * ----------------
- */
- if (TupIsNull(innerTupleSlot))
- {
- /* ----------------
- * this is an interesting case.. all our
- * inner tuples are smaller then our outer
- * tuples so we never found an inner tuple
- * to mark.
- *
- * outer inner
- * outer tuple - 5 4
- * 5 4
- * 6 nil - inner tuple
- * 7
- *
- * This means the join should end.
- * ----------------
- */
- MJ_printf("ExecMergeJoin: **** weird case 2 ****\n");
- return NULL;
- }
-
- /* ----------------
- * otherwise test the new tuple against the skip qual.
- * (we remain in the EXEC_MJ_SKIPINNER state)
- * ----------------
- */
+ mergestate->mj_JoinState = EXEC_MJ_SKIPINNER_ADVANCE;
break;
}
/* ----------------
- * compare finally failed and we have stopped skipping
- * inner tuples so now check the outer skip qual
- * to see if we should now skip outer tuples...
+ * now check the outer skip qual to see if we
+ * should now skip outer tuples... if we fail the
+ * outer skip qual, then we know we have a new pair
+ * of matching tuples.
* ----------------
*/
compareResult = MergeCompare(mergeclauses,
@@ -1011,120 +1209,237 @@ ExecMergeJoin(MergeJoin *node)
MJ_DEBUG_MERGE_COMPARE(outerSkipQual, compareResult);
if (compareResult)
- mergestate->mj_JoinState = EXEC_MJ_SKIPOUTER;
+ mergestate->mj_JoinState = EXEC_MJ_SKIPOUTER_BEGIN;
else
mergestate->mj_JoinState = EXEC_MJ_JOINMARK;
-
break;
-#ifdef ENABLE_OUTER_JOINS
-
- /*
- * EXEC_MJ_FILLINNER means we have an unmatched inner
- * tuple which must be null-expanded into the projection
- * tuple. get the next inner tuple and reset markers
- * (EXEC_MJ_JOINMARK).
+ /*------------------------------------------------
+ * Before advancing, we check to see if we must emit an
+ * outer-join fill tuple for this inner tuple.
+ *------------------------------------------------
*/
- case EXEC_MJ_FILLINNER:
- MJ_printf("ExecMergeJoin: EXEC_MJ_FILLINNER\n");
- mergestate->mj_JoinState = EXEC_MJ_JOINMARK;
+ case EXEC_MJ_SKIPINNER_ADVANCE:
+ MJ_printf("ExecMergeJoin: EXEC_MJ_SKIPINNER_ADVANCE\n");
- /* ----------------
- * project the inner tuple into the result
- * ----------------
- */
- MJ_printf("ExecMergeJoin: project inner tuple into the result (not yet implemented)\n");
+ if (doFillInner && !mergestate->mj_MatchedInner)
+ {
+ /*
+ * Generate a fake join tuple with nulls for the outer
+ * tuple, and return it if it passes the non-join quals.
+ */
+ mergestate->mj_MatchedInner = true; /* do it only once */
+
+ ResetExprContext(econtext);
+
+ outerTupleSlot = mergestate->mj_NullOuterTupleSlot;
+ econtext->ecxt_outertuple = outerTupleSlot;
+ innerTupleSlot = mergestate->mj_InnerTupleSlot;
+ econtext->ecxt_innertuple = innerTupleSlot;
+
+ if (ExecQual(otherqual, econtext, false))
+ {
+ /* ----------------
+ * qualification succeeded. now form the desired
+ * projection tuple and return the slot containing it.
+ * ----------------
+ */
+ TupleTableSlot *result;
+ ExprDoneCond isDone;
+
+ MJ_printf("ExecMergeJoin: returning fill tuple\n");
+
+ result = ExecProject(mergestate->jstate.cs_ProjInfo,
+ &isDone);
+
+ if (isDone != ExprEndResult)
+ {
+ mergestate->jstate.cs_TupFromTlist =
+ (isDone == ExprMultipleResult);
+ return result;
+ }
+ }
+ }
/* ----------------
- * now skip this inner tuple
+ * now we get the next inner tuple, if any
* ----------------
*/
innerTupleSlot = ExecProcNode(innerPlan, (Plan *) node);
+ mergestate->mj_InnerTupleSlot = innerTupleSlot;
MJ_DEBUG_PROC_NODE(innerTupleSlot);
- econtext->ecxt_innertuple = innerTupleSlot;
+ mergestate->mj_MatchedInner = false;
/* ----------------
- * if the inner tuple is null then we know
- * we have to restore the inner scan
- * and advance to the next outer tuple
+ * if the inner tuple is null then we are done with the
+ * join, unless we have outer tuples we need to null-fill.
* ----------------
*/
if (TupIsNull(innerTupleSlot))
{
- if (isLeftJoin && !TupIsNull(outerTupleSlot))
+ MJ_printf("ExecMergeJoin: end of inner subplan\n");
+ outerTupleSlot = mergestate->mj_OuterTupleSlot;
+ if (doFillOuter && !TupIsNull(outerTupleSlot))
{
- mergestate->mj_JoinState = EXEC_MJ_FILLOUTER;
- MJ_printf("ExecMergeJoin: try to complete outer fill\n");
+ /*
+ * Need to emit left-join tuples for remaining
+ * outer tuples.
+ */
+ mergestate->mj_JoinState = EXEC_MJ_ENDINNER;
break;
}
-
- MJ_printf("ExecMergeJoin: **** weird case 2 ****\n");
+ /* Otherwise we're done. */
return NULL;
}
/* ----------------
* otherwise test the new tuple against the skip qual.
- * (we move to the EXEC_MJ_JOINMARK state)
* ----------------
*/
+ mergestate->mj_JoinState = EXEC_MJ_SKIPINNER_TEST;
break;
/*
- * EXEC_MJ_FILLOUTER means we have an unmatched outer
- * tuple which must be null-expanded into the projection
- * tuple. get the next outer tuple and reset markers
- * (EXEC_MJ_JOINMARK).
+ * EXEC_MJ_ENDOUTER means we have run out of outer tuples,
+ * but are doing a right/full join and therefore must null-
+ * fill any remaing unmatched inner tuples.
*/
- case EXEC_MJ_FILLOUTER:
- MJ_printf("ExecMergeJoin: EXEC_MJ_FILLOUTER\n");
- mergestate->mj_JoinState = EXEC_MJ_JOINMARK;
+ case EXEC_MJ_ENDOUTER:
+ MJ_printf("ExecMergeJoin: EXEC_MJ_ENDOUTER\n");
+
+ Assert(doFillInner);
+
+ if (!mergestate->mj_MatchedInner)
+ {
+ /*
+ * Generate a fake join tuple with nulls for the outer
+ * tuple, and return it if it passes the non-join quals.
+ */
+ mergestate->mj_MatchedInner = true; /* do it only once */
+
+ ResetExprContext(econtext);
+
+ outerTupleSlot = mergestate->mj_NullOuterTupleSlot;
+ econtext->ecxt_outertuple = outerTupleSlot;
+ innerTupleSlot = mergestate->mj_InnerTupleSlot;
+ econtext->ecxt_innertuple = innerTupleSlot;
+
+ if (ExecQual(otherqual, econtext, false))
+ {
+ /* ----------------
+ * qualification succeeded. now form the desired
+ * projection tuple and return the slot containing it.
+ * ----------------
+ */
+ TupleTableSlot *result;
+ ExprDoneCond isDone;
+
+ MJ_printf("ExecMergeJoin: returning fill tuple\n");
+
+ result = ExecProject(mergestate->jstate.cs_ProjInfo,
+ &isDone);
+
+ if (isDone != ExprEndResult)
+ {
+ mergestate->jstate.cs_TupFromTlist =
+ (isDone == ExprMultipleResult);
+ return result;
+ }
+ }
+ }
/* ----------------
- * project the outer tuple into the result
+ * now we get the next inner tuple, if any
* ----------------
*/
- MJ_printf("ExecMergeJoin: project outer tuple into the result (not yet implemented)\n");
+ innerTupleSlot = ExecProcNode(innerPlan, (Plan *) node);
+ mergestate->mj_InnerTupleSlot = innerTupleSlot;
+ MJ_DEBUG_PROC_NODE(innerTupleSlot);
+ mergestate->mj_MatchedInner = false;
+
+ if (TupIsNull(innerTupleSlot))
+ {
+ MJ_printf("ExecMergeJoin: end of inner subplan\n");
+ return NULL;
+ }
+
+ /* Else remain in ENDOUTER state and process next tuple. */
+ break;
+
+ /*
+ * EXEC_MJ_ENDINNER means we have run out of inner tuples,
+ * but are doing a left/full join and therefore must null-
+ * fill any remaing unmatched outer tuples.
+ */
+ case EXEC_MJ_ENDINNER:
+ MJ_printf("ExecMergeJoin: EXEC_MJ_ENDINNER\n");
+
+ Assert(doFillOuter);
+
+ if (!mergestate->mj_MatchedOuter)
+ {
+ /*
+ * Generate a fake join tuple with nulls for the inner
+ * tuple, and return it if it passes the non-join quals.
+ */
+ mergestate->mj_MatchedOuter = true; /* do it only once */
+
+ ResetExprContext(econtext);
+
+ outerTupleSlot = mergestate->mj_OuterTupleSlot;
+ econtext->ecxt_outertuple = outerTupleSlot;
+ innerTupleSlot = mergestate->mj_NullInnerTupleSlot;
+ econtext->ecxt_innertuple = innerTupleSlot;
+
+ if (ExecQual(otherqual, econtext, false))
+ {
+ /* ----------------
+ * qualification succeeded. now form the desired
+ * projection tuple and return the slot containing it.
+ * ----------------
+ */
+ TupleTableSlot *result;
+ ExprDoneCond isDone;
+
+ MJ_printf("ExecMergeJoin: returning fill tuple\n");
+
+ result = ExecProject(mergestate->jstate.cs_ProjInfo,
+ &isDone);
+
+ if (isDone != ExprEndResult)
+ {
+ mergestate->jstate.cs_TupFromTlist =
+ (isDone == ExprMultipleResult);
+ return result;
+ }
+ }
+ }
/* ----------------
- * now skip this outer tuple
+ * now we get the next outer tuple, if any
* ----------------
*/
outerTupleSlot = ExecProcNode(outerPlan, (Plan *) node);
+ mergestate->mj_OuterTupleSlot = outerTupleSlot;
MJ_DEBUG_PROC_NODE(outerTupleSlot);
- econtext->ecxt_outertuple = outerTupleSlot;
+ mergestate->mj_MatchedOuter = false;
- /* ----------------
- * if the outer tuple is null then we know
- * we are done with the left half of the join
- * ----------------
- */
if (TupIsNull(outerTupleSlot))
{
- if (isRightJoin && !TupIsNull(innerTupleSlot))
- {
- mergestate->mj_JoinState = EXEC_MJ_FILLINNER;
- MJ_printf("ExecMergeJoin: try to complete inner fill\n");
- break;
- }
-
- MJ_printf("ExecMergeJoin: **** outerTuple is nil ****\n");
+ MJ_printf("ExecMergeJoin: end of outer subplan\n");
return NULL;
}
- /* ----------------
- * otherwise test the new tuple against the skip qual.
- * (we move to the EXEC_MJ_JOINMARK state)
- * ----------------
- */
+ /* Else remain in ENDINNER state and process next tuple. */
break;
-#endif
/*
* if we get here it means our code is fouled up and so we
* just end the join prematurely.
*/
default:
- elog(NOTICE, "ExecMergeJoin: invalid join state. aborting");
+ elog(NOTICE, "ExecMergeJoin: invalid join state %d, aborting",
+ mergestate->mj_JoinState);
return NULL;
}
}
@@ -1143,7 +1458,6 @@ ExecInitMergeJoin(MergeJoin *node, EState *estate, Plan *parent)
{
MergeJoinState *mergestate;
List *joinclauses;
- TupleTableSlot *mjSlot;
MJ1_printf("ExecInitMergeJoin: %s\n",
"initializing node");
@@ -1153,17 +1467,13 @@ ExecInitMergeJoin(MergeJoin *node, EState *estate, Plan *parent)
* get the range table and direction from it
* ----------------
*/
- node->join.state = estate;
+ node->join.plan.state = estate;
/* ----------------
* create new merge state for node
* ----------------
*/
mergestate = makeNode(MergeJoinState);
- mergestate->mj_OuterSkipQual = NIL;
- mergestate->mj_InnerSkipQual = NIL;
- mergestate->mj_JoinState = 0;
- mergestate->mj_MarkedTupleSlot = NULL;
node->mergestate = mergestate;
/* ----------------
@@ -1174,22 +1484,67 @@ ExecInitMergeJoin(MergeJoin *node, EState *estate, Plan *parent)
*/
ExecAssignExprContext(estate, &mergestate->jstate);
-#define MERGEJOIN_NSLOTS 2
+ /* ----------------
+ * initialize subplans
+ * ----------------
+ */
+ ExecInitNode(outerPlan((Plan *) node), estate, (Plan *) node);
+ ExecInitNode(innerPlan((Plan *) node), estate, (Plan *) node);
+
+#define MERGEJOIN_NSLOTS 4
/* ----------------
* tuple table initialization
- *
- * XXX why aren't we getting a tuple table slot in the normal way?
* ----------------
*/
ExecInitResultTupleSlot(estate, &mergestate->jstate);
- mjSlot = makeNode(TupleTableSlot);
- mjSlot->val = NULL;
- mjSlot->ttc_shouldFree = true;
- mjSlot->ttc_descIsNew = true;
- mjSlot->ttc_tupleDescriptor = NULL;
- mjSlot->ttc_buffer = InvalidBuffer;
- mjSlot->ttc_whichplan = -1;
- mergestate->mj_MarkedTupleSlot = mjSlot;
+
+ mergestate->mj_MarkedTupleSlot = ExecInitExtraTupleSlot(estate);
+ ExecSetSlotDescriptor(mergestate->mj_MarkedTupleSlot,
+ ExecGetTupType(innerPlan((Plan *) node)));
+
+ switch (node->join.jointype)
+ {
+ case JOIN_INNER:
+ break;
+ case JOIN_LEFT:
+ mergestate->mj_NullInnerTupleSlot =
+ ExecInitNullTupleSlot(estate,
+ ExecGetTupType(innerPlan((Plan*) node)));
+ break;
+ case JOIN_RIGHT:
+ mergestate->mj_NullOuterTupleSlot =
+ ExecInitNullTupleSlot(estate,
+ ExecGetTupType(outerPlan((Plan*) node)));
+ /*
+ * Can't handle right or full join with non-nil extra joinclauses.
+ */
+ if (node->join.joinqual != NIL)
+ elog(ERROR, "RIGHT JOIN is only supported with mergejoinable join conditions");
+ break;
+ case JOIN_FULL:
+ mergestate->mj_NullOuterTupleSlot =
+ ExecInitNullTupleSlot(estate,
+ ExecGetTupType(outerPlan((Plan*) node)));
+ mergestate->mj_NullInnerTupleSlot =
+ ExecInitNullTupleSlot(estate,
+ ExecGetTupType(innerPlan((Plan*) node)));
+ /*
+ * Can't handle right or full join with non-nil extra joinclauses.
+ */
+ if (node->join.joinqual != NIL)
+ elog(ERROR, "FULL JOIN is only supported with mergejoinable join conditions");
+ break;
+ default:
+ elog(ERROR, "ExecInitMergeJoin: unsupported join type %d",
+ (int) node->join.jointype);
+ }
+
+ /* ----------------
+ * initialize tuple type and projection info
+ * ----------------
+ */
+ ExecAssignResultTypeFromTL((Plan *) node, &mergestate->jstate);
+ ExecAssignProjectionInfo((Plan *) node, &mergestate->jstate);
/* ----------------
* form merge skip qualifications
@@ -1210,22 +1565,12 @@ ExecInitMergeJoin(MergeJoin *node, EState *estate, Plan *parent)
* ----------------
*/
mergestate->mj_JoinState = EXEC_MJ_INITIALIZE;
-
- /* ----------------
- * initialize subplans
- * ----------------
- */
- ExecInitNode(outerPlan((Plan *) node), estate, (Plan *) node);
- ExecInitNode(innerPlan((Plan *) node), estate, (Plan *) node);
-
- /* ----------------
- * initialize tuple type and projection info
- * ----------------
- */
- ExecAssignResultTypeFromTL((Plan *) node, &mergestate->jstate);
- ExecAssignProjectionInfo((Plan *) node, &mergestate->jstate);
-
mergestate->jstate.cs_TupFromTlist = false;
+ mergestate->mj_MatchedOuter = false;
+ mergestate->mj_MatchedInner = false;
+ mergestate->mj_OuterTupleSlot = NULL;
+ mergestate->mj_InnerTupleSlot = NULL;
+
/* ----------------
* initialization successful
* ----------------
@@ -1285,15 +1630,11 @@ ExecEndMergeJoin(MergeJoin *node)
ExecEndNode((Plan *) outerPlan((Plan *) node), (Plan *) node);
/* ----------------
- * clean out the tuple table so that we don't try and
- * pfree the marked tuples.. see HACK ALERT at the top of
- * this file.
+ * clean out the tuple table
* ----------------
*/
ExecClearTuple(mergestate->jstate.cs_ResultTupleSlot);
ExecClearTuple(mergestate->mj_MarkedTupleSlot);
- pfree(mergestate->mj_MarkedTupleSlot);
- mergestate->mj_MarkedTupleSlot = NULL;
MJ1_printf("ExecEndMergeJoin: %s\n",
"node processing ended");
@@ -1303,14 +1644,15 @@ void
ExecReScanMergeJoin(MergeJoin *node, ExprContext *exprCtxt, Plan *parent)
{
MergeJoinState *mergestate = node->mergestate;
- TupleTableSlot *mjSlot = mergestate->mj_MarkedTupleSlot;
- ExecClearTuple(mjSlot);
- mjSlot->ttc_tupleDescriptor = NULL;
- mjSlot->ttc_descIsNew = true;
- mjSlot->ttc_whichplan = -1;
+ ExecClearTuple(mergestate->mj_MarkedTupleSlot);
mergestate->mj_JoinState = EXEC_MJ_INITIALIZE;
+ mergestate->jstate.cs_TupFromTlist = false;
+ mergestate->mj_MatchedOuter = false;
+ mergestate->mj_MatchedInner = false;
+ mergestate->mj_OuterTupleSlot = NULL;
+ mergestate->mj_InnerTupleSlot = NULL;
/*
* if chgParam of subnodes is not null then plans will be re-scanned
diff --git a/src/backend/executor/nodeNestloop.c b/src/backend/executor/nodeNestloop.c
index 3685232c7e4..5abd4ffc3a1 100644
--- a/src/backend/executor/nodeNestloop.c
+++ b/src/backend/executor/nodeNestloop.c
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/executor/nodeNestloop.c,v 1.20 2000/08/24 03:29:03 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/executor/nodeNestloop.c,v 1.21 2000/09/12 21:06:48 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -62,10 +62,10 @@ ExecNestLoop(NestLoop *node)
NestLoopState *nlstate;
Plan *innerPlan;
Plan *outerPlan;
- bool needNewOuterTuple;
TupleTableSlot *outerTupleSlot;
TupleTableSlot *innerTupleSlot;
- List *qual;
+ List *joinqual;
+ List *otherqual;
ExprContext *econtext;
/* ----------------
@@ -75,9 +75,10 @@ ExecNestLoop(NestLoop *node)
ENL1_printf("getting info from node");
nlstate = node->nlstate;
- qual = node->join.qual;
- outerPlan = outerPlan(&node->join);
- innerPlan = innerPlan(&node->join);
+ joinqual = node->join.joinqual;
+ otherqual = node->join.plan.qual;
+ outerPlan = outerPlan((Plan *) node);
+ innerPlan = innerPlan((Plan *) node);
econtext = nlstate->jstate.cs_ExprContext;
/* ----------------
@@ -115,7 +116,7 @@ ExecNestLoop(NestLoop *node)
/* ----------------
* Ok, everything is setup for the join so now loop until
- * we return a qualifying join tuple..
+ * we return a qualifying join tuple.
* ----------------
*/
ENL1_printf("entering main loop");
@@ -123,44 +124,14 @@ ExecNestLoop(NestLoop *node)
for (;;)
{
/* ----------------
- * The essential idea now is to get the next inner tuple
- * and join it with the current outer tuple.
+ * If we don't have an outer tuple, get the next one and
+ * reset the inner scan.
* ----------------
*/
- needNewOuterTuple = TupIsNull(outerTupleSlot);
-
- /* ----------------
- * if we have an outerTuple, try to get the next inner tuple.
- * ----------------
- */
- if (!needNewOuterTuple)
- {
- ENL1_printf("getting new inner tuple");
-
- innerTupleSlot = ExecProcNode(innerPlan, (Plan *) node);
- econtext->ecxt_innertuple = innerTupleSlot;
-
- if (TupIsNull(innerTupleSlot))
- {
- ENL1_printf("no inner tuple, need new outer tuple");
- needNewOuterTuple = true;
- }
- }
-
- /* ----------------
- * loop until we have a new outer tuple and a new
- * inner tuple.
- * ----------------
- */
- while (needNewOuterTuple)
+ if (nlstate->nl_NeedNewOuter)
{
- /* ----------------
- * now try to get the next outer tuple
- * ----------------
- */
ENL1_printf("getting new outer tuple");
outerTupleSlot = ExecProcNode(outerPlan, (Plan *) node);
- econtext->ecxt_outertuple = outerTupleSlot;
/* ----------------
* if there are no more outer tuples, then the join
@@ -175,12 +146,14 @@ ExecNestLoop(NestLoop *node)
ENL1_printf("saving new outer tuple information");
nlstate->jstate.cs_OuterTupleSlot = outerTupleSlot;
+ econtext->ecxt_outertuple = outerTupleSlot;
+ nlstate->nl_NeedNewOuter = false;
+ nlstate->nl_MatchedOuter = false;
/* ----------------
- * now rescan the inner plan and get a new inner tuple
+ * now rescan the inner plan
* ----------------
*/
-
ENL1_printf("rescanning inner plan");
/*
@@ -189,48 +162,101 @@ ExecNestLoop(NestLoop *node)
* expr context.
*/
ExecReScan(innerPlan, econtext, (Plan *) node);
+ }
+
+ /* ----------------
+ * we have an outerTuple, try to get the next inner tuple.
+ * ----------------
+ */
+ ENL1_printf("getting new inner tuple");
+
+ innerTupleSlot = ExecProcNode(innerPlan, (Plan *) node);
+ econtext->ecxt_innertuple = innerTupleSlot;
- ENL1_printf("getting new inner tuple");
+ if (TupIsNull(innerTupleSlot))
+ {
+ ENL1_printf("no inner tuple, need new outer tuple");
- innerTupleSlot = ExecProcNode(innerPlan, (Plan *) node);
- econtext->ecxt_innertuple = innerTupleSlot;
+ nlstate->nl_NeedNewOuter = true;
- if (TupIsNull(innerTupleSlot))
- ENL1_printf("couldn't get inner tuple - need new outer tuple");
- else
+ if (! nlstate->nl_MatchedOuter &&
+ node->join.jointype == JOIN_LEFT)
{
- ENL1_printf("got inner and outer tuples");
- needNewOuterTuple = false;
+ /*
+ * We are doing an outer join and there were no join matches
+ * for this outer tuple. Generate a fake join tuple with
+ * nulls for the inner tuple, and return it if it passes
+ * the non-join quals.
+ */
+ econtext->ecxt_innertuple = nlstate->nl_NullInnerTupleSlot;
+
+ ENL1_printf("testing qualification for outer-join tuple");
+
+ if (ExecQual(otherqual, econtext, false))
+ {
+ /* ----------------
+ * qualification was satisfied so we project and
+ * return the slot containing the result tuple
+ * using ExecProject().
+ * ----------------
+ */
+ TupleTableSlot *result;
+ ExprDoneCond isDone;
+
+ ENL1_printf("qualification succeeded, projecting tuple");
+
+ result = ExecProject(nlstate->jstate.cs_ProjInfo, &isDone);
+
+ if (isDone != ExprEndResult)
+ {
+ nlstate->jstate.cs_TupFromTlist =
+ (isDone == ExprMultipleResult);
+ return result;
+ }
+ }
}
- } /* while (needNewOuterTuple) */
+ /*
+ * Otherwise just return to top of loop for a new outer tuple.
+ */
+ continue;
+ }
/* ----------------
* at this point we have a new pair of inner and outer
* tuples so we test the inner and outer tuples to see
- * if they satisify the node's qualification.
+ * if they satisfy the node's qualification.
+ *
+ * Only the joinquals determine MatchedOuter status,
+ * but all quals must pass to actually return the tuple.
* ----------------
*/
ENL1_printf("testing qualification");
- if (ExecQual((List *) qual, econtext, false))
+ if (ExecQual(joinqual, econtext, false))
{
- /* ----------------
- * qualification was satisified so we project and
- * return the slot containing the result tuple
- * using ExecProject().
- * ----------------
- */
- TupleTableSlot *result;
- ExprDoneCond isDone;
+ nlstate->nl_MatchedOuter = true;
- ENL1_printf("qualification succeeded, projecting tuple");
-
- result = ExecProject(nlstate->jstate.cs_ProjInfo, &isDone);
-
- if (isDone != ExprEndResult)
+ if (otherqual == NIL || ExecQual(otherqual, econtext, false))
{
- nlstate->jstate.cs_TupFromTlist = (isDone == ExprMultipleResult);
- return result;
+ /* ----------------
+ * qualification was satisfied so we project and
+ * return the slot containing the result tuple
+ * using ExecProject().
+ * ----------------
+ */
+ TupleTableSlot *result;
+ ExprDoneCond isDone;
+
+ ENL1_printf("qualification succeeded, projecting tuple");
+
+ result = ExecProject(nlstate->jstate.cs_ProjInfo, &isDone);
+
+ if (isDone != ExprEndResult)
+ {
+ nlstate->jstate.cs_TupFromTlist =
+ (isDone == ExprMultipleResult);
+ return result;
+ }
}
}
@@ -264,7 +290,7 @@ ExecInitNestLoop(NestLoop *node, EState *estate, Plan *parent)
* assign execution state to node
* ----------------
*/
- node->join.state = estate;
+ node->join.plan.state = estate;
/* ----------------
* create new nest loop state
@@ -281,19 +307,33 @@ ExecInitNestLoop(NestLoop *node, EState *estate, Plan *parent)
*/
ExecAssignExprContext(estate, &nlstate->jstate);
-#define NESTLOOP_NSLOTS 1
/* ----------------
- * tuple table initialization
+ * now initialize children
* ----------------
*/
- ExecInitResultTupleSlot(estate, &nlstate->jstate);
+ ExecInitNode(outerPlan((Plan *) node), estate, (Plan *) node);
+ ExecInitNode(innerPlan((Plan *) node), estate, (Plan *) node);
+#define NESTLOOP_NSLOTS 2
/* ----------------
- * now initialize children
+ * tuple table initialization
* ----------------
*/
- ExecInitNode(outerPlan((Plan *) node), estate, (Plan *) node);
- ExecInitNode(innerPlan((Plan *) node), estate, (Plan *) node);
+ ExecInitResultTupleSlot(estate, &nlstate->jstate);
+
+ switch (node->join.jointype)
+ {
+ case JOIN_INNER:
+ break;
+ case JOIN_LEFT:
+ nlstate->nl_NullInnerTupleSlot =
+ ExecInitNullTupleSlot(estate,
+ ExecGetTupType(innerPlan((Plan*) node)));
+ break;
+ default:
+ elog(ERROR, "ExecInitNestLoop: unsupported join type %d",
+ (int) node->join.jointype);
+ }
/* ----------------
* initialize tuple type and projection info
@@ -308,6 +348,8 @@ ExecInitNestLoop(NestLoop *node, EState *estate, Plan *parent)
*/
nlstate->jstate.cs_OuterTupleSlot = NULL;
nlstate->jstate.cs_TupFromTlist = false;
+ nlstate->nl_NeedNewOuter = true;
+ nlstate->nl_MatchedOuter = false;
NL1_printf("ExecInitNestLoop: %s\n",
"node initialized");
@@ -394,4 +436,6 @@ ExecReScanNestLoop(NestLoop *node, ExprContext *exprCtxt, Plan *parent)
/* let outerPlan to free its result tuple ... */
nlstate->jstate.cs_OuterTupleSlot = NULL;
nlstate->jstate.cs_TupFromTlist = false;
+ nlstate->nl_NeedNewOuter = true;
+ nlstate->nl_MatchedOuter = false;
}