From f818f8b6d65b0756fc1166e4c8c8fe663df23222 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 11 Jan 2021 22:47:12 -0500 Subject: [PATCH] add nullif case for eval_const_expressions --- src/backend/optimizer/util/clauses.c | 38 ++++++++++++++++++++++++++++++++++++ src/test/regress/expected/case.out | 24 +++++++++++++++++++++++ src/test/regress/sql/case.sql | 9 +++++++++ 3 files changed, 71 insertions(+) diff --git a/src/backend/optimizer/util/clauses.c b/src/backend/optimizer/util/clauses.c index 51d26a0..b5a2f00 100644 --- a/src/backend/optimizer/util/clauses.c +++ b/src/backend/optimizer/util/clauses.c @@ -2329,6 +2329,44 @@ eval_const_expressions_mutator(Node *node, newexpr->location = expr->location; return (Node *) newexpr; } + case T_NullIfExpr: + { + NullIfExpr *expr = (NullIfExpr *) node; + NullIfExpr *newexpr; + ListCell *arg; + List *args; + bool has_nonconst_input = false; + + args = (List *) ece_generic_processing(expr->args); + + /* If either argument is NULL they can't be equal */ + foreach(arg, args) + { + if (!IsA(lfirst(arg), Const)) + has_nonconst_input = true; + else if (((Const *) lfirst(arg))->constisnull) + return (Node *) linitial(args); + } + + if(!has_nonconst_input) + return ece_evaluate_expr(expr); + + /* + * The expression cannot be simplified any further, so build + * and return a replacement NullIfExpr node using the + * possibly-simplified arguments. + */ + newexpr = makeNode(NullIfExpr); + newexpr->opno = expr->opno; + newexpr->opfuncid = expr->opfuncid; + newexpr->opresulttype = expr->opresulttype; + newexpr->opretset = expr->opretset; + newexpr->opcollid = expr->opcollid; + newexpr->inputcollid = expr->inputcollid; + newexpr->args = args; + newexpr->location = expr->location; + return (Node *) newexpr; + } case T_DistinctExpr: { DistinctExpr *expr = (DistinctExpr *) node; diff --git a/src/test/regress/expected/case.out b/src/test/regress/expected/case.out index 7fcfe9a..2063c73 100644 --- a/src/test/regress/expected/case.out +++ b/src/test/regress/expected/case.out @@ -263,6 +263,30 @@ SELECT * 4 | | 2 | -4 (2 rows) +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 -- diff --git a/src/test/regress/sql/case.sql b/src/test/regress/sql/case.sql index 0655d26..4742e1d 100644 --- a/src/test/regress/sql/case.sql +++ b/src/test/regress/sql/case.sql @@ -137,6 +137,15 @@ SELECT * FROM CASE_TBL a, CASE2_TBL b WHERE COALESCE(f,b.i) = 2; +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 -- -- 1.8.3.1