Skip to content

Commit d730424

Browse files
committed
Fix old oversight in const-simplification of COALESCE() expressions.
Once we have found a non-null constant argument, there is no need to examine additional arguments of the COALESCE. The previous coding got it right only if the constant was in the first argument position; otherwise it tried to simplify following arguments too, leading to unexpected behavior like this: regression=# select coalesce(f1, 42, 1/0) from int4_tbl; ERROR: division by zero It's a minor corner case, but a bug is a bug, so back-patch all the way.
1 parent 19e231b commit d730424

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/backend/optimizer/util/clauses.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2746,14 +2746,18 @@ eval_const_expressions_mutator(Node *node,
27462746
/*
27472747
* We can remove null constants from the list. For a non-null
27482748
* constant, if it has not been preceded by any other
2749-
* non-null-constant expressions then that is the result.
2749+
* non-null-constant expressions then it is the result. Otherwise,
2750+
* it's the next argument, but we can drop following arguments
2751+
* since they will never be reached.
27502752
*/
27512753
if (IsA(e, Const))
27522754
{
27532755
if (((Const *) e)->constisnull)
27542756
continue; /* drop null constant */
27552757
if (newargs == NIL)
27562758
return e; /* first expr */
2759+
newargs = lappend(newargs, e);
2760+
break;
27572761
}
27582762
newargs = lappend(newargs, e);
27592763
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy