Add support for NullIfExpr in eval_const_expressions
authorPeter Eisentraut <[email protected]>
Fri, 2 Apr 2021 09:01:49 +0000 (11:01 +0200)
committerPeter Eisentraut <[email protected]>
Fri, 2 Apr 2021 09:01:49 +0000 (11:01 +0200)
Author: Hou Zhijie <[email protected]>
Discussion: https://www.postgresql.org/message-id/flat/7ea5ce773bbc4eea9ff1a381acd3b102@G08CNEXMBPEKD05.g08.fujitsu.local

src/backend/optimizer/util/clauses.c
src/test/regress/expected/case.out
src/test/regress/sql/case.sql

index e89571ed40d6d1bfab3008e4ee2dc27b59af2ef8..bea1cc4d67e93a97dba3d0589a6292412ea03009 100644 (file)
@@ -2497,6 +2497,36 @@ eval_const_expressions_mutator(Node *node,
                newexpr->location = expr->location;
                return (Node *) newexpr;
            }
+       case T_NullIfExpr:
+           {
+               NullIfExpr     *expr;
+               ListCell       *arg;
+               bool            has_nonconst_input = false;
+
+               /* Copy the node and const-simplify its arguments */
+               expr = (NullIfExpr *) ece_generic_processing(node);
+
+               /* If either argument is NULL they can't be equal */
+               foreach(arg, expr->args)
+               {
+                   if (!IsA(lfirst(arg), Const))
+                       has_nonconst_input = true;
+                   else if (((Const *) lfirst(arg))->constisnull)
+                       return (Node *) linitial(expr->args);
+               }
+
+               /*
+                * Need to get OID of underlying function before checking if
+                * the function is OK to evaluate.
+                */
+               set_opfuncid((OpExpr *) expr);
+
+               if (!has_nonconst_input &&
+                   ece_function_is_safe(expr->opfuncid, context))
+                   return ece_evaluate_expr(expr);
+
+               return (Node *) expr;
+           }
        case T_ScalarArrayOpExpr:
            {
                ScalarArrayOpExpr *saop;
index 7fcfe9a7a6069b03ffd2ba3b664bddf49e56b8d5..f5136c17abbf0eaed00ab76ece3d9ee933dd9190 100644 (file)
@@ -263,6 +263,31 @@ SELECT *
  4 |   | 2 | -4
 (2 rows)
 
+-- Tests for constant subexpression simplification
+explain (costs off)
+SELECT * FROM CASE_TBL WHERE NULLIF(1, 2) = 2;
+        QUERY PLAN        
+--------------------------
+ Result
+   One-Time Filter: false
+(2 rows)
+
+explain (costs off)
+SELECT * FROM CASE_TBL WHERE NULLIF(1, 1) IS NOT NULL;
+        QUERY PLAN        
+--------------------------
+ Result
+   One-Time Filter: false
+(2 rows)
+
+explain (costs off)
+SELECT * FROM CASE_TBL WHERE NULLIF(1, null) = 2;
+        QUERY PLAN        
+--------------------------
+ Result
+   One-Time Filter: false
+(2 rows)
+
 --
 -- Examples of updates involving tables
 --
index 0655d266f60bdee0f5f8438f234d44d1457f28b9..83fe43be6b84f1b96db4285e2e9eeca037ff261f 100644 (file)
@@ -137,6 +137,17 @@ SELECT *
   FROM CASE_TBL a, CASE2_TBL b
   WHERE COALESCE(f,b.i) = 2;
 
+-- Tests for constant subexpression simplification
+
+explain (costs off)
+SELECT * FROM CASE_TBL WHERE NULLIF(1, 2) = 2;
+
+explain (costs off)
+SELECT * FROM CASE_TBL WHERE NULLIF(1, 1) IS NOT NULL;
+
+explain (costs off)
+SELECT * FROM CASE_TBL WHERE NULLIF(1, null) = 2;
+
 --
 -- Examples of updates involving tables
 --