10000 Fix old oversight in const-simplification of COALESCE() expressions. · justtesting112233/postgres@5c85d11 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5c85d11

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 8000 a minor corner case, but a bug is a bug, so back-patch all the way.
1 parent d434e8f commit 5c85d11

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
@@ -2733,14 +2733,18 @@ eval_const_expressions_mutator(Node *node,
27332733
/*
27342734
* We can remove null constants from the list. For a non-null
27352735
* constant, if it has not been preceded by any other
2736-
* non-null-constant expressions then that is the result.
2736+
* non-null-constant expressions then it is the result. Otherwise,
2737+
* it's the next argument, but we can drop following arguments
2738+
* since they will never be reached.
27372739
*/
27382740
if (IsA(e, Const))
27392741
{
27402742
if (((Const *) e)->constisnull)
27412743
continue; /* drop null constant */
27422744
if (newargs == NIL)
27432745
return e; /* first expr */
2746+
newargs = lappend(newargs, e);
2747+
break;
27442748
}
27452749
newargs = lappend(newargs, e);
27462750
}

0 commit comments

Comments
 (0)
0