Skip to content

Commit 9c5f67f

Browse files
committed
Add support for NullIfExpr in eval_const_expressions
Author: Hou Zhijie <houzj.fnst@cn.fujitsu.com> Discussion: https://www.postgresql.org/message-id/flat/7ea5ce773bbc4eea9ff1a381acd3b102@G08CNEXMBPEKD05.g08.fujitsu.local
1 parent 96bdb7e commit 9c5f67f

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

src/backend/optimizer/util/clauses.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2497,6 +2497,36 @@ eval_const_expressions_mutator(Node *node,
24972497
newexpr->location = expr->location;
24982498
return (Node *) newexpr;
24992499
}
2500+
case T_NullIfExpr:
2501+
{
2502+
NullIfExpr *expr;
2503+
ListCell *arg;
2504+
bool has_nonconst_input = false;
2505+
2506+
/* Copy the node and const-simplify its arguments */
2507+
expr = (NullIfExpr *) ece_generic_processing(node);
2508+
2509+
/* If either argument is NULL they can't be equal */
2510+
foreach(arg, expr->args)
2511+
{
2512+
if (!IsA(lfirst(arg), Const))
2513+
has_nonconst_input = true;
2514+
else if (((Const *) lfirst(arg))->constisnull)
2515+
return (Node *) linitial(expr->args);
2516+
}
2517+
2518+
/*
2519+
* Need to get OID of underlying function before checking if
2520+
* the function is OK to evaluate.
2521+
*/
2522+
set_opfuncid((OpExpr *) expr);
2523+
2524+
if (!has_nonconst_input &&
2525+
ece_function_is_safe(expr->opfuncid, context))
2526+
return ece_evaluate_expr(expr);
2527+
2528+
return (Node *) expr;
2529+
}
25002530
case T_ScalarArrayOpExpr:
25012531
{
25022532
ScalarArrayOpExpr *saop;

src/test/regress/expected/case.out

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,31 @@ SELECT *
263263
4 | | 2 | -4
264264
(2 rows)
265265

266+
-- Tests for constant subexpression simplification
267+
explain (costs off)
268+
SELECT * FROM CASE_TBL WHERE NULLIF(1, 2) = 2;
269+
QUERY PLAN
270+
--------------------------
271+
Result
272+
One-Time Filter: false
273+
(2 rows)
274+
275+
explain (costs off)
276+
SELECT * FROM CASE_TBL WHERE NULLIF(1, 1) IS NOT NULL;
277+
QUERY PLAN
278+
--------------------------
279+
Result
280+
One-Time Filter: false
281+
(2 rows)
282+
283+
explain (costs off)
284+
SELECT * FROM CASE_TBL WHERE NULLIF(1, null) = 2;
285+
QUERY PLAN
286+
--------------------------
287+
Result
288+
One-Time Filter: false
289+
(2 rows)
290+
266291
--
267292
-- Examples of updates involving tables
268293
--

src/test/regress/sql/case.sql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,17 @@ SELECT *
137137
FROM CASE_TBL a, CASE2_TBL b
138138
WHERE COALESCE(f,b.i) = 2;
139139

140+
-- Tests for constant subexpression simplification
141+
142+
explain (costs off)
143+
SELECT * FROM CASE_TBL WHERE NULLIF(1, 2) = 2;
144+
145+
explain (costs off)
146+
SELECT * FROM CASE_TBL WHERE NULLIF(1, 1) IS NOT NULL;
147+
148+
explain (costs off)
149+
SELECT * FROM CASE_TBL WHERE NULLIF(1, null) = 2;
150+
140151
--
141152
-- Examples of updates involving tables
142153
--

0 commit comments

Comments
 (0)