mirror of
https://github.com/postgres/postgres.git
synced 2025-06-14 18:42:34 +03:00
Fix ExecSubPlan to handle nulls per the SQL spec --- it didn't combine
nulls with non-nulls using proper three-valued boolean logic. Also clean up ExecQual to make it clearer that ExecQual *does* follow the SQL spec for boolean nulls. See '[BUGS] (null) != (null)' thread around 10/26/99 for more detail.
This commit is contained in:
@ -7,14 +7,14 @@
|
|||||||
*
|
*
|
||||||
*
|
*
|
||||||
* IDENTIFICATION
|
* IDENTIFICATION
|
||||||
* $Header: /cvsroot/pgsql/src/backend/executor/execQual.c,v 1.63 1999/10/08 03:49:55 tgl Exp $
|
* $Header: /cvsroot/pgsql/src/backend/executor/execQual.c,v 1.64 1999/11/12 06:39:34 tgl Exp $
|
||||||
*
|
*
|
||||||
*-------------------------------------------------------------------------
|
*-------------------------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
/*
|
/*
|
||||||
* INTERFACE ROUTINES
|
* INTERFACE ROUTINES
|
||||||
* ExecEvalExpr - evaluate an expression and return a datum
|
* ExecEvalExpr - evaluate an expression and return a datum
|
||||||
* ExecQual - return true/false if qualification is satisified
|
* ExecQual - return true/false if qualification is satisfied
|
||||||
* ExecTargetList - form a new tuple by projecting the given tuple
|
* ExecTargetList - form a new tuple by projecting the given tuple
|
||||||
*
|
*
|
||||||
* NOTES
|
* NOTES
|
||||||
@ -71,7 +71,6 @@ static Datum ExecEvalOr(Expr *orExpr, ExprContext *econtext, bool *isNull);
|
|||||||
static Datum ExecEvalVar(Var *variable, ExprContext *econtext, bool *isNull);
|
static Datum ExecEvalVar(Var *variable, ExprContext *econtext, bool *isNull);
|
||||||
static Datum ExecMakeFunctionResult(Node *node, List *arguments,
|
static Datum ExecMakeFunctionResult(Node *node, List *arguments,
|
||||||
ExprContext *econtext, bool *isNull, bool *isDone);
|
ExprContext *econtext, bool *isNull, bool *isDone);
|
||||||
static bool ExecQualClause(Node *clause, ExprContext *econtext);
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* ExecEvalArrayRef
|
* ExecEvalArrayRef
|
||||||
@ -1253,7 +1252,9 @@ ExecEvalExpr(Node *expression,
|
|||||||
retDatum = (Datum) ExecEvalNot(expr, econtext, isNull);
|
retDatum = (Datum) ExecEvalNot(expr, econtext, isNull);
|
||||||
break;
|
break;
|
||||||
case SUBPLAN_EXPR:
|
case SUBPLAN_EXPR:
|
||||||
retDatum = (Datum) ExecSubPlan((SubPlan *) expr->oper, expr->args, econtext);
|
retDatum = (Datum) ExecSubPlan((SubPlan *) expr->oper,
|
||||||
|
expr->args, econtext,
|
||||||
|
isNull);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
elog(ERROR, "ExecEvalExpr: unknown expression type %d", expr->opType);
|
elog(ERROR, "ExecEvalExpr: unknown expression type %d", expr->opType);
|
||||||
@ -1279,46 +1280,6 @@ ExecEvalExpr(Node *expression,
|
|||||||
* ----------------------------------------------------------------
|
* ----------------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* ----------------------------------------------------------------
|
|
||||||
* ExecQualClause
|
|
||||||
*
|
|
||||||
* this is a workhorse for ExecQual. ExecQual has to deal
|
|
||||||
* with a list of qualifications, so it passes each qualification
|
|
||||||
* in the list to this function one at a time. ExecQualClause
|
|
||||||
* returns true when the qualification *fails* and false if
|
|
||||||
* the qualification succeeded (meaning we have to test the
|
|
||||||
* rest of the qualification)
|
|
||||||
* ----------------------------------------------------------------
|
|
||||||
*/
|
|
||||||
static bool
|
|
||||||
ExecQualClause(Node *clause, ExprContext *econtext)
|
|
||||||
{
|
|
||||||
Datum expr_value;
|
|
||||||
bool isNull;
|
|
||||||
bool isDone;
|
|
||||||
|
|
||||||
/* when there is a null clause, consider the qualification to fail */
|
|
||||||
if (clause == NULL)
|
|
||||||
return true;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* pass isDone, but ignore it. We don't iterate over multiple returns
|
|
||||||
* in the qualifications.
|
|
||||||
*/
|
|
||||||
expr_value = ExecEvalExpr(clause, econtext, &isNull, &isDone);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* remember, we return true when the qualification fails;
|
|
||||||
* NULL is considered failure.
|
|
||||||
*/
|
|
||||||
if (isNull)
|
|
||||||
return true;
|
|
||||||
if (DatumGetInt32(expr_value) == 0)
|
|
||||||
return true;
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ----------------------------------------------------------------
|
/* ----------------------------------------------------------------
|
||||||
* ExecQual
|
* ExecQual
|
||||||
*
|
*
|
||||||
@ -1329,7 +1290,7 @@ ExecQualClause(Node *clause, ExprContext *econtext)
|
|||||||
bool
|
bool
|
||||||
ExecQual(List *qual, ExprContext *econtext)
|
ExecQual(List *qual, ExprContext *econtext)
|
||||||
{
|
{
|
||||||
List *clause;
|
List *qlist;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* debugging stuff
|
* debugging stuff
|
||||||
@ -1340,25 +1301,38 @@ ExecQual(List *qual, ExprContext *econtext)
|
|||||||
|
|
||||||
IncrProcessed();
|
IncrProcessed();
|
||||||
|
|
||||||
/*
|
|
||||||
* return true immediately if no qual
|
|
||||||
*/
|
|
||||||
if (qual == NIL)
|
|
||||||
return true;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* a "qual" is a list of clauses. To evaluate the qual, we evaluate
|
* a "qual" is a list of clauses. To evaluate the qual, we evaluate
|
||||||
* each of the clauses in the list.
|
* each of the clauses in the list. (For an empty list, we'll return
|
||||||
|
* TRUE.)
|
||||||
*
|
*
|
||||||
* ExecQualClause returns true when we know the qualification *failed*
|
* If any of the clauses return NULL, we treat this as FALSE. This
|
||||||
* so we just pass each clause in qual to it until we know the qual
|
* is correct per the SQL spec: if any ANDed conditions are NULL, then
|
||||||
* failed or there are no more clauses.
|
* the AND result is either FALSE or NULL, and in either case the
|
||||||
|
* WHERE condition fails. NOTE: it would NOT be correct to use this
|
||||||
|
* simplified logic in a sub-clause; ExecEvalAnd must do the full
|
||||||
|
* three-state condition evaluation. We can get away with simpler
|
||||||
|
* logic here because we know how the result will be used.
|
||||||
*/
|
*/
|
||||||
|
foreach(qlist, qual)
|
||||||
foreach(clause, qual)
|
|
||||||
{
|
{
|
||||||
if (ExecQualClause((Node *) lfirst(clause), econtext))
|
Node *clause = (Node *) lfirst(qlist);
|
||||||
return false; /* qual failed, so return false */
|
Datum expr_value;
|
||||||
|
bool isNull;
|
||||||
|
bool isDone;
|
||||||
|
|
||||||
|
/* if there is a null clause, consider the qualification to fail */
|
||||||
|
if (clause == NULL)
|
||||||
|
return false;
|
||||||
|
/*
|
||||||
|
* pass isDone, but ignore it. We don't iterate over multiple returns
|
||||||
|
* in the qualifications.
|
||||||
|
*/
|
||||||
|
expr_value = ExecEvalExpr(clause, econtext, &isNull, &isDone);
|
||||||
|
if (isNull)
|
||||||
|
return false; /* treat NULL as FALSE */
|
||||||
|
if (DatumGetInt32(expr_value) == 0)
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -18,34 +18,39 @@
|
|||||||
#include "executor/nodeSubplan.h"
|
#include "executor/nodeSubplan.h"
|
||||||
#include "tcop/pquery.h"
|
#include "tcop/pquery.h"
|
||||||
|
|
||||||
|
/* should be exported by execMain.c */
|
||||||
|
extern void ExecCheckPerms(CmdType op, int resRel, List *rtable, Query *q);
|
||||||
|
|
||||||
/* ----------------------------------------------------------------
|
/* ----------------------------------------------------------------
|
||||||
* ExecSubPlan(node)
|
* ExecSubPlan(node)
|
||||||
*
|
*
|
||||||
* ----------------------------------------------------------------
|
* ----------------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
Datum
|
Datum
|
||||||
ExecSubPlan(SubPlan *node, List *pvar, ExprContext *econtext)
|
ExecSubPlan(SubPlan *node, List *pvar, ExprContext *econtext, bool *isNull)
|
||||||
{
|
{
|
||||||
Plan *plan = node->plan;
|
Plan *plan = node->plan;
|
||||||
SubLink *sublink = node->sublink;
|
SubLink *sublink = node->sublink;
|
||||||
SubLinkType subLinkType = sublink->subLinkType;
|
SubLinkType subLinkType = sublink->subLinkType;
|
||||||
|
bool useor = sublink->useor;
|
||||||
TupleTableSlot *slot;
|
TupleTableSlot *slot;
|
||||||
List *lst;
|
Datum result;
|
||||||
Datum result = (Datum) false;
|
|
||||||
bool found = false; /* TRUE if got at least one subplan tuple */
|
bool found = false; /* TRUE if got at least one subplan tuple */
|
||||||
|
List *lst;
|
||||||
|
|
||||||
if (node->setParam != NULL)
|
if (node->setParam != NIL)
|
||||||
elog(ERROR, "ExecSubPlan: can't set parent params from subquery");
|
elog(ERROR, "ExecSubPlan: can't set parent params from subquery");
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Set Params of this plan from parent plan correlation Vars
|
* Set Params of this plan from parent plan correlation Vars
|
||||||
*/
|
*/
|
||||||
if (node->parParam != NULL)
|
if (node->parParam != NIL)
|
||||||
{
|
{
|
||||||
foreach(lst, node->parParam)
|
foreach(lst, node->parParam)
|
||||||
{
|
{
|
||||||
ParamExecData *prm = &(econtext->ecxt_param_exec_vals[lfirsti(lst)]);
|
ParamExecData *prm = &(econtext->ecxt_param_exec_vals[lfirsti(lst)]);
|
||||||
|
|
||||||
|
Assert(pvar != NIL);
|
||||||
prm->value = ExecEvalExpr((Node *) lfirst(pvar),
|
prm->value = ExecEvalExpr((Node *) lfirst(pvar),
|
||||||
econtext,
|
econtext,
|
||||||
&(prm->isnull), NULL);
|
&(prm->isnull), NULL);
|
||||||
@ -53,21 +58,32 @@ ExecSubPlan(SubPlan *node, List *pvar, ExprContext *econtext)
|
|||||||
}
|
}
|
||||||
plan->chgParam = nconc(plan->chgParam, listCopy(node->parParam));
|
plan->chgParam = nconc(plan->chgParam, listCopy(node->parParam));
|
||||||
}
|
}
|
||||||
|
Assert(pvar == NIL);
|
||||||
|
|
||||||
ExecReScan(plan, (ExprContext *) NULL, plan);
|
ExecReScan(plan, (ExprContext *) NULL, plan);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* For all sublink types except EXPR_SUBLINK, the result type is
|
* For all sublink types except EXPR_SUBLINK, the result is boolean
|
||||||
* boolean, and we have a fairly clear idea of how to combine multiple
|
* as are the results of the combining operators. We combine results
|
||||||
* subitems and deal with NULL values or an empty subplan result.
|
* within a tuple (if there are multiple columns) using OR semantics
|
||||||
|
* if "useor" is true, AND semantics if not. We then combine results
|
||||||
|
* across tuples (if the subplan produces more than one) using OR
|
||||||
|
* semantics for ANY_SUBLINK or AND semantics for ALL_SUBLINK. NULL
|
||||||
|
* results from the combining operators are handled according to the
|
||||||
|
* usual SQL semantics for OR and AND. The result for no input
|
||||||
|
* tuples is FALSE for ANY_SUBLINK, TRUE for ALL_SUBLINK.
|
||||||
*
|
*
|
||||||
* For EXPR_SUBLINK, the result type is whatever the combining operator
|
* For EXPR_SUBLINK we require the subplan to produce no more than one
|
||||||
* returns. We have no way to deal with more than one column in the
|
* tuple, else an error is raised. If zero tuples are produced, we
|
||||||
* subplan result --- hopefully the parser forbids that. More
|
* return NULL. (XXX it would probably be more correct to evaluate
|
||||||
* seriously, it's unclear what to do with NULL values or an empty
|
* the combining operator with a NULL input?) Assuming we get a tuple:
|
||||||
* subplan result. For now, we error out, but should something else
|
* if there is only one column then we just return its result as-is, NULL
|
||||||
* happen?
|
* or otherwise. If there is more than one column we combine the results
|
||||||
|
* per "useor" --- this only makes sense if the combining operators yield
|
||||||
|
* boolean, and we assume the parser has checked that.
|
||||||
*/
|
*/
|
||||||
|
result = (Datum) (subLinkType == ALL_SUBLINK ? true : false);
|
||||||
|
*isNull = false;
|
||||||
|
|
||||||
for (slot = ExecProcNode(plan, plan);
|
for (slot = ExecProcNode(plan, plan);
|
||||||
!TupIsNull(slot);
|
!TupIsNull(slot);
|
||||||
@ -75,24 +91,26 @@ ExecSubPlan(SubPlan *node, List *pvar, ExprContext *econtext)
|
|||||||
{
|
{
|
||||||
HeapTuple tup = slot->val;
|
HeapTuple tup = slot->val;
|
||||||
TupleDesc tdesc = slot->ttc_tupleDescriptor;
|
TupleDesc tdesc = slot->ttc_tupleDescriptor;
|
||||||
int i = 1;
|
Datum rowresult = (Datum) (useor ? false : true);
|
||||||
|
bool rownull = false;
|
||||||
if (subLinkType == EXPR_SUBLINK && found)
|
int col = 1;
|
||||||
{
|
|
||||||
elog(ERROR, "ExecSubPlan: more than one tuple returned by expression subselect");
|
|
||||||
return (Datum) false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (subLinkType == EXISTS_SUBLINK)
|
if (subLinkType == EXISTS_SUBLINK)
|
||||||
return (Datum) true;
|
return (Datum) true;
|
||||||
|
|
||||||
|
/* cannot allow multiple input tuples for EXPR sublink */
|
||||||
|
if (subLinkType == EXPR_SUBLINK && found)
|
||||||
|
elog(ERROR, "ExecSubPlan: more than one tuple returned by expression subselect");
|
||||||
|
|
||||||
found = true;
|
found = true;
|
||||||
|
|
||||||
|
/* iterate over combining operators for columns of tuple */
|
||||||
foreach(lst, sublink->oper)
|
foreach(lst, sublink->oper)
|
||||||
{
|
{
|
||||||
Expr *expr = (Expr *) lfirst(lst);
|
Expr *expr = (Expr *) lfirst(lst);
|
||||||
Const *con = lsecond(expr->args);
|
Const *con = lsecond(expr->args);
|
||||||
bool isnull;
|
Datum expresult;
|
||||||
|
bool expnull;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* The righthand side of the expression should be either a Const
|
* The righthand side of the expression should be either a Const
|
||||||
@ -107,41 +125,90 @@ ExecSubPlan(SubPlan *node, List *pvar, ExprContext *econtext)
|
|||||||
con = lfirst(((Expr *) con)->args);
|
con = lfirst(((Expr *) con)->args);
|
||||||
Assert(IsA(con, Const));
|
Assert(IsA(con, Const));
|
||||||
}
|
}
|
||||||
con->constvalue = heap_getattr(tup, i, tdesc, &(con->constisnull));
|
con->constvalue = heap_getattr(tup, col, tdesc,
|
||||||
|
&(con->constisnull));
|
||||||
/*
|
/*
|
||||||
* Now we can eval the expression.
|
* Now we can eval the combining operator for this column.
|
||||||
*/
|
*/
|
||||||
result = ExecEvalExpr((Node *) expr, econtext, &isnull,
|
expresult = ExecEvalExpr((Node *) expr, econtext, &expnull,
|
||||||
(bool *) NULL);
|
(bool *) NULL);
|
||||||
if (isnull)
|
/*
|
||||||
|
* Combine the result into the row result as appropriate.
|
||||||
|
*/
|
||||||
|
if (col == 1)
|
||||||
{
|
{
|
||||||
if (subLinkType == EXPR_SUBLINK)
|
rowresult = expresult;
|
||||||
elog(ERROR, "ExecSubPlan: null value returned by expression subselect");
|
rownull = expnull;
|
||||||
else
|
|
||||||
result = (Datum) false;
|
|
||||||
}
|
}
|
||||||
if (subLinkType != EXPR_SUBLINK)
|
else if (useor)
|
||||||
{
|
{
|
||||||
if ((!(bool) result && !(sublink->useor)) ||
|
/* combine within row per OR semantics */
|
||||||
((bool) result && sublink->useor))
|
if (expnull)
|
||||||
break;
|
rownull = true;
|
||||||
|
else if (DatumGetInt32(expresult) != 0)
|
||||||
|
{
|
||||||
|
rowresult = (Datum) true;
|
||||||
|
rownull = false;
|
||||||
|
break; /* needn't look at any more columns */
|
||||||
|
}
|
||||||
}
|
}
|
||||||
i++;
|
else
|
||||||
|
{
|
||||||
|
/* combine within row per AND semantics */
|
||||||
|
if (expnull)
|
||||||
|
rownull = true;
|
||||||
|
else if (DatumGetInt32(expresult) == 0)
|
||||||
|
{
|
||||||
|
rowresult = (Datum) false;
|
||||||
|
rownull = false;
|
||||||
|
break; /* needn't look at any more columns */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
col++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (subLinkType == ALL_SUBLINK && !(bool) result)
|
if (subLinkType == ANY_SUBLINK)
|
||||||
break;
|
{
|
||||||
if (subLinkType == ANY_SUBLINK && (bool) result)
|
/* combine across rows per OR semantics */
|
||||||
break;
|
if (rownull)
|
||||||
|
*isNull = true;
|
||||||
|
else if (DatumGetInt32(rowresult) != 0)
|
||||||
|
{
|
||||||
|
result = (Datum) true;
|
||||||
|
*isNull = false;
|
||||||
|
break; /* needn't look at any more rows */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (subLinkType == ALL_SUBLINK)
|
||||||
|
{
|
||||||
|
/* combine across rows per AND semantics */
|
||||||
|
if (rownull)
|
||||||
|
*isNull = true;
|
||||||
|
else if (DatumGetInt32(rowresult) == 0)
|
||||||
|
{
|
||||||
|
result = (Datum) false;
|
||||||
|
*isNull = false;
|
||||||
|
break; /* needn't look at any more rows */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* must be EXPR_SUBLINK */
|
||||||
|
result = rowresult;
|
||||||
|
*isNull = rownull;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!found)
|
if (!found)
|
||||||
{
|
{
|
||||||
/* deal with empty subplan result. Note default result is 'false' */
|
/* deal with empty subplan result. result/isNull were previously
|
||||||
if (subLinkType == ALL_SUBLINK)
|
* initialized correctly for all sublink types except EXPR.
|
||||||
result = (Datum) true;
|
*/
|
||||||
else if (subLinkType == EXPR_SUBLINK)
|
if (subLinkType == EXPR_SUBLINK)
|
||||||
elog(ERROR, "ExecSubPlan: no tuples returned by expression subselect");
|
{
|
||||||
|
result = (Datum) false;
|
||||||
|
*isNull = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
@ -152,7 +219,6 @@ ExecSubPlan(SubPlan *node, List *pvar, ExprContext *econtext)
|
|||||||
*
|
*
|
||||||
* ----------------------------------------------------------------
|
* ----------------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
extern void ExecCheckPerms(CmdType op, int resRel, List *rtable, Query *q);
|
|
||||||
bool
|
bool
|
||||||
ExecInitSubPlan(SubPlan *node, EState *estate, Plan *parent)
|
ExecInitSubPlan(SubPlan *node, EState *estate, Plan *parent)
|
||||||
{
|
{
|
||||||
|
@ -9,7 +9,8 @@
|
|||||||
|
|
||||||
#include "nodes/plannodes.h"
|
#include "nodes/plannodes.h"
|
||||||
|
|
||||||
extern Datum ExecSubPlan(SubPlan *node, List *pvar, ExprContext *econtext);
|
extern Datum ExecSubPlan(SubPlan *node, List *pvar, ExprContext *econtext,
|
||||||
|
bool *isNull);
|
||||||
extern bool ExecInitSubPlan(SubPlan *node, EState *estate, Plan *parent);
|
extern bool ExecInitSubPlan(SubPlan *node, EState *estate, Plan *parent);
|
||||||
extern void ExecReScanSetParamPlan(SubPlan *node, Plan *parent);
|
extern void ExecReScanSetParamPlan(SubPlan *node, Plan *parent);
|
||||||
extern void ExecSetParamPlan(SubPlan *node);
|
extern void ExecSetParamPlan(SubPlan *node);
|
||||||
|
Reference in New Issue
Block a user