8
8
*
9
9
*
10
10
* 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 $
12
12
*
13
13
* HISTORY
14
14
* AUTHOR DATE MAJOR EVENT
@@ -92,7 +92,7 @@ static List *simplify_or_arguments(List *args,
92
92
static List * simplify_and_arguments (List * args ,
93
93
eval_const_expressions_context * context ,
94
94
bool * haveNull , bool * forceFalse );
95
- static Expr * simplify_boolean_equality (List * args );
95
+ static Expr * simplify_boolean_equality (Oid opno , List * args );
96
96
static Expr * simplify_function (Oid funcid ,
97
97
Oid result_type , int32 result_typmod , List * * args ,
98
98
bool allow_inline ,
@@ -2186,12 +2186,14 @@ eval_const_expressions_mutator(Node *node,
2186
2186
return (Node * ) simple ;
2187
2187
2188
2188
/*
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.
2191
2192
*/
2192
- if (expr -> opno == BooleanEqualOperator )
2193
+ if (expr -> opno == BooleanEqualOperator ||
2194
+ expr -> opno == BooleanNotEqualOperator )
2193
2195
{
2194
- simple = simplify_boolean_equality (args );
2196
+ simple = simplify_boolean_equality (expr -> opno , args );
2195
2197
if (simple ) /* successfully simplified it */
2196
2198
return (Node * ) simple ;
2197
2199
}
@@ -3165,21 +3167,23 @@ simplify_and_arguments(List *args,
3165
3167
3166
3168
/*
3167
3169
* Subroutine for eval_const_expressions: try to simplify boolean equality
3170
+ * or inequality condition
3168
3171
*
3169
- * Input is the list of simplified arguments to the operator.
3172
+ * Inputs are the operator OID and the simplified arguments to the operator.
3170
3173
* Returns a simplified expression if successful, or NULL if cannot
3171
3174
* simplify the expression.
3172
3175
*
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".
3174
3178
* This is only marginally useful in itself, but doing it in constant folding
3175
- * ensures that we will recognize the two forms as being equivalent in, for
3179
+ * ensures that we will recognize these forms as being equivalent in, for
3176
3180
* example, partial index matching.
3177
3181
*
3178
3182
* We come here only if simplify_function has failed; therefore we cannot
3179
3183
* see two constant inputs, nor a constant-NULL input.
3180
3184
*/
3181
3185
static Expr *
3182
- simplify_boolean_equality (List * args )
3186
+ simplify_boolean_equality (Oid opno , List * args )
3183
3187
{
3184
3188
Expr * leftop ;
3185
3189
Expr * rightop ;
@@ -3190,18 +3194,38 @@ simplify_boolean_equality(List *args)
3190
3194
if (leftop && IsA (leftop , Const ))
3191
3195
{
3192
3196
Assert (!((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
+ }
3195
3204
else
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
+ }
3197
3211
}
3198
3212
if (rightop && IsA (rightop , Const ))
3199
3213
{
3200
3214
Assert (!((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
+ }
3203
3222
else
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
+ }
3205
3229
}
3206
3230
return NULL ;
3207
3231
}
0 commit comments