Teach eval_const_expressions to simplify BooleanTest nodes that have
authorTom Lane <tgl@sss.pgh.pa.us>
Fri, 4 Aug 2006 14:09:51 +0000 (14:09 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Fri, 4 Aug 2006 14:09:51 +0000 (14:09 +0000)
constant input.  Seems worth doing because rule rewriter inserts
IS NOT TRUE tests into WHERE clauses.

src/backend/optimizer/util/clauses.c

index 5570b33f485d0a346c8a88d9e6788ea4a5d53102..73fe60bd24eb39eb9c3ab95d186792902d37010d 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/optimizer/util/clauses.c,v 1.216 2006/08/02 01:59:46 joe Exp $
+ *       $PostgreSQL: pgsql/src/backend/optimizer/util/clauses.c,v 1.217 2006/08/04 14:09:51 tgl Exp $
  *
  * HISTORY
  *       AUTHOR                        DATE                    MAJOR EVENT
@@ -2092,6 +2092,58 @@ eval_const_expressions_mutator(Node *node,
                newfselect->resulttypmod = fselect->resulttypmod;
                return (Node *) newfselect;
        }
+       if (IsA(node, BooleanTest))
+       {
+               BooleanTest *btest = (BooleanTest *) node;
+               BooleanTest *newbtest;
+               Node       *arg;
+
+               arg = eval_const_expressions_mutator((Node *) btest->arg,
+                                                                                        context);
+               if (arg && IsA(arg, Const))
+               {
+                       Const  *carg = (Const *) arg;
+                       bool    result;
+
+                       switch (btest->booltesttype)
+                       {
+                               case IS_TRUE:
+                                       result = (!carg->constisnull &&
+                                                         DatumGetBool(carg->constvalue));
+                                       break;
+                               case IS_NOT_TRUE:
+                                       result = (carg->constisnull ||
+                                                         !DatumGetBool(carg->constvalue));
+                                       break;
+                               case IS_FALSE:
+                                       result = (!carg->constisnull &&
+                                                         !DatumGetBool(carg->constvalue));
+                                       break;
+                               case IS_NOT_FALSE:
+                                       result = (carg->constisnull ||
+                                                         DatumGetBool(carg->constvalue));
+                                       break;
+                               case IS_UNKNOWN:
+                                       result = carg->constisnull;
+                                       break;
+                               case IS_NOT_UNKNOWN:
+                                       result = !carg->constisnull;
+                                       break;
+                               default:
+                                       elog(ERROR, "unrecognized booltesttype: %d",
+                                                (int) btest->booltesttype);
+                                       result = false; /* keep compiler quiet */
+                                       break;
+                       }
+
+                       return makeBoolConst(result, false);
+               }
+
+               newbtest = makeNode(BooleanTest);
+               newbtest->arg = (Expr *) arg;
+               newbtest->booltesttype = btest->booltesttype;
+               return (Node *) newbtest;
+       }
 
        /*
         * For any node type not handled above, we recurse using