*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/executor/execQual.c,v 1.110 2002/11/25 21:29:35 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/executor/execQual.c,v 1.111 2002/11/30 21:25:04 tgl Exp $
*
*-------------------------------------------------------------------------
*/
* they are NULL; if either is NULL then the result is already
* known. If neither is NULL, then proceed to evaluate the
* function. Note that this is *always* derived from the equals
- * operator, but since we've already evaluated the arguments
+ * operator, but since we need special processing of the arguments
* we can not simply reuse ExecEvalOper() or ExecEvalFunc().
* ----------------------------------------------------------------
*/
bool *isNull,
ExprDoneCond *isDone)
{
- bool result;
+ Datum result;
FunctionCachePtr fcache;
FunctionCallInfoData fcinfo;
ExprDoneCond argDone;
List *argList;
/*
- * we extract the oid of the function associated with the op and then
- * pass the work onto ExecMakeFunctionResult which evaluates the
- * arguments and returns the result of calling the function on the
- * evaluated arguments.
+ * extract info from opClause
*/
op = (Oper *) opClause->oper;
argList = opClause->args;
econtext->ecxt_per_query_memory);
op->op_fcache = fcache;
}
- Assert(fcache->func.fn_retset == FALSE);
+ Assert(!fcache->func.fn_retset);
/* Need to prep callinfo structure */
MemSet(&fcinfo, 0, sizeof(fcinfo));
fcinfo.flinfo = &(fcache->func);
argDone = ExecEvalFuncArgs(&fcinfo, argList, econtext);
+ if (argDone != ExprSingleResult)
+ elog(ERROR, "IS DISTINCT FROM does not support set arguments");
Assert(fcinfo.nargs == 2);
if (fcinfo.argnull[0] && fcinfo.argnull[1])
{
/* Both NULL? Then is not distinct... */
- result = FALSE;
+ result = BoolGetDatum(FALSE);
}
else if (fcinfo.argnull[0] || fcinfo.argnull[1])
{
- /* One is NULL? Then is distinct... */
- result = TRUE;
+ /* Only one is NULL? Then is distinct... */
+ result = BoolGetDatum(TRUE);
}
else
{
fcinfo.isnull = false;
result = FunctionCallInvoke(&fcinfo);
*isNull = fcinfo.isnull;
-
- result = (!DatumGetBool(result));
+ /* Must invert result of "=" */
+ result = BoolGetDatum(!DatumGetBool(result));
}
- return BoolGetDatum(result);
+ return result;
}
/* ----------------------------------------------------------------
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/optimizer/util/clauses.c,v 1.113 2002/11/26 03:01:58 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/optimizer/util/clauses.c,v 1.114 2002/11/30 21:25:04 tgl Exp $
*
* HISTORY
* AUTHOR DATE MAJOR EVENT
*
* Returns t iff the clause is an operator clause:
* (op expr expr) or (op expr).
- *
- * [historical note: is_clause has the exact functionality and is used
- * throughout the code. They're renamed to is_opclause for clarity.
- * - ay 10/94.]
*/
bool
is_opclause(Node *clause)
{
return (clause != NULL &&
IsA(clause, Expr) &&
- ((((Expr *) clause)->opType == OP_EXPR) ||
- ((Expr *) clause)->opType == DISTINCT_EXPR));
+ ((Expr *) clause)->opType == OP_EXPR);
}
/*
* make_opclause
- * Creates a clause given its operator left operand and right
- * operand (if it is non-null).
- *
+ * Creates a clause given its operator, left operand, and right
+ * operand (pass NULL to create single-operand clause).
*/
Expr *
make_opclause(Oper *op, Var *leftop, Var *rightop)
bool has_nonconst_input = false;
/*
- * Check for constant inputs and especially
- * constant-NULL inputs.
+ * We must do our own check for NULLs because
+ * DISTINCT_EXPR has different results for NULL input
+ * than the underlying operator does.
*/
- Assert(length(args) == 2);
foreach(arg, args)
{
if (IsA(lfirst(arg), Const))
else
has_nonconst_input = true;
}
- /* all nulls? then not distinct */
- if (all_null_input)
- return MAKEBOOLCONST(false, false);
- /* one null? then distinct */
- if (has_null_input)
- return MAKEBOOLCONST(true, false);
-
- /* all constants? then optimize this out */
+ /* all constants? then can optimize this out */
if (!has_nonconst_input)
{
- Oid result_typeid;
- int16 resultTypLen;
- bool resultTypByVal;
- ExprContext *econtext;
- Datum const_val;
- bool const_is_null;
-
- Oper *oper = (Oper *) expr->oper;
-
- replace_opid(oper); /* OK to scribble on input
- * to this extent */
- result_typeid = oper->opresulttype;
-
- /*
- * OK, looks like we can simplify this
- * operator/function.
- *
- * We use the executor's routine ExecEvalExpr() to
- * avoid duplication of code and ensure we get the
- * same result as the executor would get.
- *
- * Build a new Expr node containing the
- * already-simplified arguments. The only other
- * setup needed here is the replace_opid() that we
- * already did for the OP_EXPR case.
- */
- newexpr = makeNode(Expr);
- newexpr->typeOid = expr->typeOid;
- newexpr->opType = expr->opType;
- newexpr->oper = expr->oper;
- newexpr->args = args;
-
- /* Get info needed about result datatype */
- get_typlenbyval(result_typeid, &resultTypLen, &resultTypByVal);
-
- /*
- * It is OK to pass a dummy econtext because none
- * of the ExecEvalExpr() code used in this
- * situation will use econtext. That might seem
- * fortuitous, but it's not so unreasonable --- a
- * constant expression does not depend on context,
- * by definition, n'est ce pas?
- */
- econtext = MakeExprContext(NULL, CurrentMemoryContext);
-
- const_val = ExecEvalExprSwitchContext((Node *) newexpr, econtext,
- &const_is_null, NULL);
-
- /*
- * Must copy result out of sub-context used by
- * expression eval
- */
- if (!const_is_null)
- const_val = datumCopy(const_val, resultTypByVal, resultTypLen);
-
- FreeExprContext(econtext);
- pfree(newexpr);
-
- /*
- * Make the constant result node.
- */
- return (Node *) makeConst(result_typeid, resultTypLen,
- const_val, const_is_null,
- resultTypByVal);
+ /* all nulls? then not distinct */
+ if (all_null_input)
+ return MAKEBOOLCONST(false, false);
+
+ /* one null? then distinct */
+ if (has_null_input)
+ return MAKEBOOLCONST(true, false);
+
+ /* otherwise try to evaluate the '=' operator */
+ newexpr = simplify_op_or_func(expr, args);
+ if (newexpr) /* successfully simplified it */
+ return (Node *) newexpr;
}
+
+ /*
+ * else fall out to build new Expr node with simplified
+ * args
+ */
break;
}
case OR_EXPR:
* Note we take the result type from the Oper or Func node, not the
* pg_proc tuple; probably necessary for binary-compatibility cases.
*/
- if (expr->opType == OP_EXPR)
+ if (expr->opType == FUNC_EXPR)
+ {
+ Func *func = (Func *) expr->oper;
+
+ funcid = func->funcid;
+ result_typeid = func->funcresulttype;
+ }
+ else /* OP_EXPR or DISTINCT_EXPR */
{
Oper *oper = (Oper *) expr->oper;
funcid = oper->opid;
result_typeid = oper->opresulttype;
}
- else
- {
- Func *func = (Func *) expr->oper;
-
- funcid = func->funcid;
- result_typeid = func->funcresulttype;
- }
/*
* we could use func_volatile() here, but we need several fields out
*
* Build a new Expr node containing the already-simplified arguments. The
* only other setup needed here is the replace_opid() that we already
- * did for the OP_EXPR case.
+ * did for the OP_EXPR/DISTINCT_EXPR case.
*/
newexpr = makeNode(Expr);
newexpr->typeOid = expr->typeOid;