88 *
99 *
1010 * IDENTIFICATION
11- * $PostgreSQL: pgsql/src/backend/optimizer/util/clauses.c,v 1.277 2009/06/11 14:48:59 momjian Exp $
11+ * $PostgreSQL: pgsql/src/backend/optimizer/util/clauses.c,v 1.278 2009/07/20 00:24:30 tgl Exp $
1212 *
1313 * HISTORY
1414 * AUTHORDATEMAJOR EVENT
@@ -92,7 +92,7 @@ static List *simplify_or_arguments(List *args,
9292static List * simplify_and_arguments (List * args ,
9393eval_const_expressions_context * context ,
9494bool * haveNull ,bool * forceFalse );
95- static Expr * simplify_boolean_equality (List * args );
95+ static Expr * simplify_boolean_equality (Oid opno , List * args );
9696static Expr * simplify_function (Oid funcid ,
9797Oid result_type ,int32 result_typmod ,List * * args ,
9898bool allow_inline ,
@@ -2186,12 +2186,14 @@ eval_const_expressions_mutator(Node *node,
21862186return (Node * )simple ;
21872187
21882188/*
2189- * If the operator is boolean equality, we know how to simplify cases
2190- * involving one constant and one non-constant argument.
2189+ * If the operator is boolean equality or inequality, we know how to
2190+ * simplify cases involving one constant and one non-constant
2191+ * argument.
21912192 */
2192- if (expr -> opno == BooleanEqualOperator )
2193+ if (expr -> opno == BooleanEqualOperator ||
2194+ expr -> opno == BooleanNotEqualOperator )
21932195{
2194- simple = simplify_boolean_equality (args );
2196+ simple = simplify_boolean_equality (expr -> opno , args );
21952197if (simple )/* successfully simplified it */
21962198return (Node * )simple ;
21972199}
@@ -3165,21 +3167,23 @@ simplify_and_arguments(List *args,
31653167
31663168/*
31673169 * Subroutine for eval_const_expressions: try to simplify boolean equality
3170+ * or inequality condition
31683171 *
3169- *Input is thelist of simplified arguments to the operator.
3172+ *Inputs are theoperator OID and the simplified arguments to the operator.
31703173 * Returns a simplified expression if successful, or NULL if cannot
31713174 * simplify the expression.
31723175 *
3173- * The idea here is to reduce "x = true" to "x" and "x = false" to "NOT x".
3176+ * The idea here is to reduce "x = true" to "x" and "x = false" to "NOT x",
3177+ * or similarly "x <> true" to "NOT x" and "x <> false" to "x".
31743178 * This is only marginally useful in itself, but doing it in constant folding
3175- * ensures that we will recognizethe two forms as being equivalent in, for
3179+ * ensures that we will recognizethese forms as being equivalent in, for
31763180 * example, partial index matching.
31773181 *
31783182 * We come here only if simplify_function has failed; therefore we cannot
31793183 * see two constant inputs, nor a constant-NULL input.
31803184 */
31813185static Expr *
3182- simplify_boolean_equality (List * args )
3186+ simplify_boolean_equality (Oid opno , List * args )
31833187{
31843188Expr * leftop ;
31853189Expr * rightop ;
@@ -3190,18 +3194,38 @@ simplify_boolean_equality(List *args)
31903194if (leftop && IsA (leftop ,Const ))
31913195{
31923196Assert (!((Const * )leftop )-> constisnull );
3193- if (DatumGetBool (((Const * )leftop )-> constvalue ))
3194- return rightop ;/* true = foo */
3197+ if (opno == BooleanEqualOperator )
3198+ {
3199+ if (DatumGetBool (((Const * )leftop )-> constvalue ))
3200+ return rightop ;/* true = foo */
3201+ else
3202+ return make_notclause (rightop );/* false = foo */
3203+ }
31953204else
3196- return make_notclause (rightop );/* false = foo */
3205+ {
3206+ if (DatumGetBool (((Const * )leftop )-> constvalue ))
3207+ return make_notclause (rightop );/* true <> foo */
3208+ else
3209+ return rightop ;/* false <> foo */
3210+ }
31973211}
31983212if (rightop && IsA (rightop ,Const ))
31993213{
32003214Assert (!((Const * )rightop )-> constisnull );
3201- if (DatumGetBool (((Const * )rightop )-> constvalue ))
3202- return leftop ;/* foo = true */
3215+ if (opno == BooleanEqualOperator )
3216+ {
3217+ if (DatumGetBool (((Const * )rightop )-> constvalue ))
3218+ return leftop ;/* foo = true */
3219+ else
3220+ return make_notclause (leftop );/* foo = false */
3221+ }
32033222else
3204- return make_notclause (leftop );/* foo = false */
3223+ {
3224+ if (DatumGetBool (((Const * )rightop )-> constvalue ))
3225+ return make_notclause (leftop );/* foo <> true */
3226+ else
3227+ return leftop ;/* foo <> false */
3228+ }
32053229}
32063230return NULL ;
32073231}