1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-05 07:21:24 +03:00

First cut at full support for OUTER JOINs. There are still a few loose

ends to clean up (see my message of same date to pghackers), but mostly
it works.  INITDB REQUIRED!
This commit is contained in:
Tom Lane
2000-09-12 21:07:18 +00:00
parent b5c0ab278b
commit ed5003c584
93 changed files with 6386 additions and 4262 deletions

View File

@ -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);

View File

@ -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;
}
#ifdef NOT_USED
/* ----------------
* ExecInitMarkedTupleSlot
* ----------------
*/
void
ExecInitMarkedTupleSlot(EState *estate, MergeJoinState *mergestate)
{
INIT_SLOT_DEFS;
INIT_SLOT_ALLOC;
mergestate->mj_MarkedTupleSlot = (TupleTableSlot *) slot;
}
#endif
/* ----------------
* ExecInitOuterTupleSlot
* ----------------
*/
void
ExecInitOuterTupleSlot(EState *estate, HashJoinState *hashstate)
{
INIT_SLOT_DEFS;
INIT_SLOT_ALLOC;
hashstate->hj_OuterTupleSlot = slot;
commonscanstate->css_ScanTupleSlot = slot;
}
/* ----------------
* ExecInitHashTupleSlot
* ExecInitExtraTupleSlot
* ----------------
*/
#ifdef NOT_USED
void
ExecInitHashTupleSlot(EState *estate, HashJoinState *hashstate)
TupleTableSlot *
ExecInitExtraTupleSlot(EState *estate)
{
INIT_SLOT_DEFS;
INIT_SLOT_ALLOC;
hashstate->hj_HashTupleSlot = slot;
return slot;
}
/* ----------------
* 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.
* ----------------
*/
TupleTableSlot *
ExecInitNullTupleSlot(EState *estate, TupleDesc tupType)
{
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 */
ExecSetSlotDescriptor(slot, tupType);
nullTuple = heap_formtuple(&NullTupleDesc, values, nulls);
return ExecStoreTuple(nullTuple, slot, InvalidBuffer, true);
}
#endif
static TupleTableSlot *
NodeGetResultTupleSlot(Plan *node)

View File

@ -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);
}
/* ----------------

View File

@ -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

File diff suppressed because it is too large Load Diff

View File

@ -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)
if (nlstate->nl_NeedNewOuter)
{
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)
{
/* ----------------
* 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);
}
ENL1_printf("getting new inner tuple");
/* ----------------
* 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;
innerTupleSlot = ExecProcNode(innerPlan, (Plan *) node);
econtext->ecxt_innertuple = innerTupleSlot;
if (TupIsNull(innerTupleSlot))
ENL1_printf("couldn't get inner tuple - need new outer tuple");
else
if (TupIsNull(innerTupleSlot))
{
ENL1_printf("no inner tuple, need new outer tuple");
nlstate->nl_NeedNewOuter = true;
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,13 +307,6 @@ ExecInitNestLoop(NestLoop *node, EState *estate, Plan *parent)
*/
ExecAssignExprContext(estate, &nlstate->jstate);
#define NESTLOOP_NSLOTS 1
/* ----------------
* tuple table initialization
* ----------------
*/
ExecInitResultTupleSlot(estate, &nlstate->jstate);
/* ----------------
* now initialize children
* ----------------
@ -295,6 +314,27 @@ ExecInitNestLoop(NestLoop *node, EState *estate, Plan *parent)
ExecInitNode(outerPlan((Plan *) node), estate, (Plan *) node);
ExecInitNode(innerPlan((Plan *) node), estate, (Plan *) node);
#define NESTLOOP_NSLOTS 2
/* ----------------
* tuple table initialization
* ----------------
*/
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;
}