Commit 220e45bf authored by Tom Lane's avatar Tom Lane

Improve the planner's simplification of NOT constructs.

This patch merges the responsibility for NOT-flattening into
eval_const_expressions' processing.  It wasn't done that way originally
because prepqual.c is far older than eval_const_expressions.  But putting
this work into eval_const_expressions saves one pass over the qual trees,
and in fact saves even more than that because we can exploit the knowledge
that the subexpressions have already been recursively simplified.  Doing it
this way also lets us do it uniformly over all expressions, whereas
prepqual.c formerly just did it at top level to save cycles.  That should
improve the planner's ability to recognize logically-equivalent constructs.

While at it, also add the ability to fold a NOT into BooleanTest and
NullTest constructs (the latter only for the scalar-datatype case).

Per discussion of bug #5702.
parent b48b9cb3
This diff is collapsed.
...@@ -98,7 +98,7 @@ static List *simplify_or_arguments(List *args, ...@@ -98,7 +98,7 @@ static List *simplify_or_arguments(List *args,
static List *simplify_and_arguments(List *args, static List *simplify_and_arguments(List *args,
eval_const_expressions_context *context, eval_const_expressions_context *context,
bool *haveNull, bool *forceFalse); bool *haveNull, bool *forceFalse);
static Expr *simplify_boolean_equality(Oid opno, List *args); static Node *simplify_boolean_equality(Oid opno, List *args);
static Expr *simplify_function(Oid funcid, static Expr *simplify_function(Oid funcid,
Oid result_type, int32 result_typmod, List **args, Oid result_type, int32 result_typmod, List **args,
bool has_named_args, bool has_named_args,
...@@ -2229,7 +2229,7 @@ eval_const_expressions_mutator(Node *node, ...@@ -2229,7 +2229,7 @@ eval_const_expressions_mutator(Node *node,
if (expr->opno == BooleanEqualOperator || if (expr->opno == BooleanEqualOperator ||
expr->opno == BooleanNotEqualOperator) expr->opno == BooleanNotEqualOperator)
{ {
simple = simplify_boolean_equality(expr->opno, args); simple = (Expr *) simplify_boolean_equality(expr->opno, args);
if (simple) /* successfully simplified it */ if (simple) /* successfully simplified it */
return (Node *) simple; return (Node *) simple;
} }
...@@ -2395,24 +2395,12 @@ eval_const_expressions_mutator(Node *node, ...@@ -2395,24 +2395,12 @@ eval_const_expressions_mutator(Node *node,
Assert(list_length(expr->args) == 1); Assert(list_length(expr->args) == 1);
arg = eval_const_expressions_mutator(linitial(expr->args), arg = eval_const_expressions_mutator(linitial(expr->args),
context); context);
if (IsA(arg, Const))
{ /*
Const *const_input = (Const *) arg; * Use negate_clause() to see if we can simplify away
* the NOT.
/* NOT NULL => NULL */ */
if (const_input->constisnull) return negate_clause(arg);
return makeBoolConst(false, true);
/* otherwise pretty easy */
return makeBoolConst(!DatumGetBool(const_input->constvalue),
false);
}
else if (not_clause(arg))
{
/* Cancel NOT/NOT */
return (Node *) get_notclausearg((Expr *) arg);
}
/* Else we still need a NOT node */
return (Node *) make_notclause((Expr *) arg);
} }
default: default:
elog(ERROR, "unrecognized boolop: %d", elog(ERROR, "unrecognized boolop: %d",
...@@ -3222,11 +3210,11 @@ simplify_and_arguments(List *args, ...@@ -3222,11 +3210,11 @@ simplify_and_arguments(List *args,
* We come here only if simplify_function has failed; therefore we cannot * We come here only if simplify_function has failed; therefore we cannot
* see two constant inputs, nor a constant-NULL input. * see two constant inputs, nor a constant-NULL input.
*/ */
static Expr * static Node *
simplify_boolean_equality(Oid opno, List *args) simplify_boolean_equality(Oid opno, List *args)
{ {
Expr *leftop; Node *leftop;
Expr *rightop; Node *rightop;
Assert(list_length(args) == 2); Assert(list_length(args) == 2);
leftop = linitial(args); leftop = linitial(args);
...@@ -3239,12 +3227,12 @@ simplify_boolean_equality(Oid opno, List *args) ...@@ -3239,12 +3227,12 @@ simplify_boolean_equality(Oid opno, List *args)
if (DatumGetBool(((Const *) leftop)->constvalue)) if (DatumGetBool(((Const *) leftop)->constvalue))
return rightop; /* true = foo */ return rightop; /* true = foo */
else else
return make_notclause(rightop); /* false = foo */ return negate_clause(rightop); /* false = foo */
} }
else else
{ {
if (DatumGetBool(((Const *) leftop)->constvalue)) if (DatumGetBool(((Const *) leftop)->constvalue))
return make_notclause(rightop); /* true <> foo */ return negate_clause(rightop); /* true <> foo */
else else
return rightop; /* false <> foo */ return rightop; /* false <> foo */
} }
...@@ -3257,12 +3245,12 @@ simplify_boolean_equality(Oid opno, List *args) ...@@ -3257,12 +3245,12 @@ simplify_boolean_equality(Oid opno, List *args)
if (DatumGetBool(((Const *) rightop)->constvalue)) if (DatumGetBool(((Const *) rightop)->constvalue))
return leftop; /* foo = true */ return leftop; /* foo = true */
else else
return make_notclause(leftop); /* foo = false */ return negate_clause(leftop); /* foo = false */
} }
else else
{ {
if (DatumGetBool(((Const *) rightop)->constvalue)) if (DatumGetBool(((Const *) rightop)->constvalue))
return make_notclause(leftop); /* foo <> true */ return negate_clause(leftop); /* foo <> true */
else else
return leftop; /* foo <> false */ return leftop; /* foo <> false */
} }
......
...@@ -33,6 +33,7 @@ extern Relids get_relids_for_join(PlannerInfo *root, int joinrelid); ...@@ -33,6 +33,7 @@ extern Relids get_relids_for_join(PlannerInfo *root, int joinrelid);
/* /*
* prototypes for prepqual.c * prototypes for prepqual.c
*/ */
extern Node *negate_clause(Node *node);
extern Expr *canonicalize_qual(Expr *qual); extern Expr *canonicalize_qual(Expr *qual);
/* /*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment