Skip to content

Commit 9e4b213

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 bda2856 commit 9e4b213

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
@@ -2758,14 +2758,18 @@ eval_const_expressions_mutator(Node *node,
27582758
/*
27592759
* We can remove null constants from the list. For a non-null
27602760
* constant, if it has not been preceded by any other
2761-
* non-null-constant expressions then that is the result.
2761+
* non-null-constant expressions then it is the result. Otherwise,
2762+
* it's the next argument, but we can drop following arguments
2763+
* since they will never be reached.
27622764
*/
27632765
if (IsA(e, Const))
27642766
{
27652767
if (((Const *) e)->constisnull)
27662768
continue; /* drop null constant */
27672769
if (newargs == NIL)
27682770
return e; /* first expr */
2771+
newargs = lappend(newargs, e);
2772+
break;
27692773
}
27702774
newargs = lappend(newargs, e);
27712775
}

0 commit comments

Comments
 (0)